一、说明
1.1 覆盖操作系统和数据库版本
- sql server版本要求:需覆盖2008 r2及以上版本。所有脚本中的命令都需在不同版本中经过实际测试,并能正常输出结果。
- 操作系统版本要求:需覆盖linux和windows。
- 如果在不同版本中有不同的命令,请在文档中明确标注版本和命令差异。
1.2 查询最小权限要求
例如创建的shijw用户,授权如下:
alter server role [diskadmin] add member [shijw]; alter server role [processadmin] add member [shijw]; alter server role [securityadmin] add member [shijw]; alter server role [setupadmin] add member [shijw];
二、sql语句
2.1 数据库用户密码复杂度认证
select name, is_policy_checked from sys.sql_logins;
sql返回结果:
- 当返回值=1时,表示合规(pass)。
- 当返回值不等于1时,表示存在风险(risk)。
2.2 数据库用户密码有效期策略
select name, is_policy_checked from sys.sql_logins;
sql返回结果:
- 当返回值=1时,表示合规(pass)。
- 当返回值不等于1时,表示存在风险(risk)。
2.3 连接会话限制
select convert(numeric(18,2), convert(numeric(18,2), c.value_in_use) / convert(numeric(18,2), maximum) * 100) as user_count from sys.configurations c where c.name = 'user connections';
sql返回结果:
- 当返回值<95时,表示合规(pass)。
- 当返回值>95时,表示存在风险(risk)。
2.4 连接数据库进程数限制
select convert(decimal(18,0), (sum(s.current_workers_count) * 1.0 / i.max_workers_count) * 100) as cpu线程使用率 from sys.dm_os_sys_info i, sys.dm_os_schedulers s group by i.max_workers_count;
sql返回结果:
- 当返回值<95时,表示合规(pass)。
- 当返回值>95时,表示存在风险(risk)。
2.5 物理备份
select bs.database_name,
backuptype = case
when bs.type = 'd' and bs.is_copy_only = 0 then 'full database'
when bs.type = 'd' and bs.is_copy_only = 1 then 'full copy-only database'
when bs.type = 'i' then 'differential database backup'
when bs.type = 'l' then 'transaction log'
when bs.type = 'f' then 'file or filegroup'
when bs.type = 'g' then 'differential file'
when bs.type = 'p' then 'partial'
when bs.type = 'q' then 'differential partial' end + ' backup',
case bf.device_type
when 2 then 'disk'
when 5 then 'tape'
when 7 then 'virtual device'
when 9 then 'azure storage'
when 105 then 'a permanent backup device'
else 'other device' end as devicetype,
bs.backup_start_date,
backupfinishdate = bs.backup_finish_date,
[backupstatus] = case bs.backup_start_date when null then '备份失败' else '成功' end,
latestbackuplocation = bf.physical_device_name
from msdb.dbo.backupset bs
left join msdb.dbo.backupmediafamily bf on bs.[media_set_id] = bf.[media_set_id]
where bs.backup_start_date > dateadd(month, - 2, sysdatetime())
order by bs.database_name asc, bs.backup_start_date desc;
sql返回结果:
- 当有返回结果时,表示合规(pass)。
- 当无返回结果时,表示存在风险(risk)。
2.6 网络传输加密
select distinct (encrypt_option) from sys.dm_exec_connections;
sql返回结果:
- 当返回结果=true时,表示合规(pass)。
- 当无返回结果时,表示存在风险(risk)。
2.7 数据库存储加密
select d.name as 'database name',
case
when e.encryption_state = 3 then 'encrypted'
when e.encryption_state = 2 then 'in progress'
else 'not encrypted'
end as state
from sys.dm_database_encryption_keys e
right join sys.databases d on d.database_id = e.database_id
left join sys.certificates c on e.encryptor_thumbprint = c.thumbprint;
sql返回结果:
- 当返回结果=encrypted时,表示合规(pass)。
- 当返回结果不等于encrypted时,表示存在风险(risk)。
2.8 透明加密
select name, is_encrypted from sys.databases;
sql返回结果:
- 当返回结果=1时,表示合规(pass)。
- 当返回结果不等于1时,表示存在风险(risk)。
2.9 连接超时机制
select comment, value from sys.sysconfigures where comment like 'remote%';
sql返回结果:
- 当返回结果≠0时,表示合规(pass)。
- 当返回结果=0时,表示存在风险(risk)。
2.10 数据库版本补丁
select @@version;
select serverproperty('productversion');
- sql返回结果: 需要根据实际输出填写。
2.11 弱口令
select name, create_date, is_disabled from sys.sql_logins where pwdcompare('', password_hash) = 1;
select name, create_date, is_disabled from sys.sql_logins where pwdcompare(name, password_hash) = 1;
- sql返回结果: 需要根据实际输出填写。
2.12 c2审计
select value from sys.sysconfigures where comment = 'c2 audit mode';
sql返回结果:
- 当返回结果为1时,表示合规(pass)。
- 当返回结果为其他值时,表示存在风险(risk)。
2.13 sql审核
select status from sys.dm_server_audit_status where status = 1;
sql返回结果:
- 当返回结果为1时,表示合规(pass)。
- 当返回结果为其他值时,表示存在风险(risk)。
2.14 sql注入
select value from sys.sysconfigures where comment = 'enable or disable command shell';
sql返回结果:
- 当返回结果为0时,表示合规(pass)。
- 当返回结果为其他值时,表示存在风险(risk)。
三、总结
这些sql脚本主要用于检查sql server数据库的安全配置,确保数据库在操作系统、权限控制、备份、加密以及其他关键领域符合最佳实践。根据执行结果,系统管理员可以针对发现的问题采取相应的补救措施,进一步加强数据库的安全性。
以上就是sql server安全配置全面检查与优化方案的详细内容,更多关于sql server安全配置检查与优化的资料请关注代码网其它相关文章!
发表评论