使用Ansible和OpenSCAP强化服务器
1. 运行初始扫描
在完成安装和扫描角色后,就可以进行首次扫描。 site.yml 文件如下:
---
- hosts: scap
gather_facts: true
become: yes
become_method: sudo
vars_files:
- group_vars/common.yml
roles:
- { role: install, tags: [ "scan" ] }
- { role: scan, tags: [ "scan" ], report_name: "01-initial-scan" }
运行以下命令执行剧本:
$ ansible-playbook -i production site.yml
运行结果示例如下:
PLAY [scap]
***************************************************************************
*************
TASK [Gathering Facts]
***************************************************************************
**
ok: [box1]
TASK [install : install the packages needed]
*******************************************************
changed: [box1] => (item=openscap-scanner)
changed: [box1] => (item=scap-security-guide)
TASK [scan : run the openscap scan]
****************************************************************
fatal: [box1]: FAILED! => {"changed": true, "cmd": ["oscap", "xccdf",
"eval", "--profile", "xccdf_org.ssgproject.content_profile_pci-dss", "--
fetch-remote-resources", "--results-arf", "/tmp/box1_results_01-initial-
scan.xml", "--report", "/tmp/box1_report_01-initial-scan.html",
"/usr/share/xml/scap/ssg/content/ssg-centos7-ds.xml"], "delta":
"0:01:03.459407", "end": "2018-05-16 08:17:50.970321", "msg": "non-zero
return code", "rc": 2, "start": "2018-05-16 08:16:47.510914", "stderr":
"Downloading:
https://www.redhat.com/security/data/oval/com.redhat.rhsa-RHEL7.xml.bz2 ...
ok", "stderr_lines": ["Downloading:
https://www.redhat.com/security/data/oval/com.redhat.rhsa-RHEL7.xml.bz2 ...
ok"], "stdout": "Title\r\tEnsure Red Hat GPG Key
Installed\nRule\r\txccdf_org.ssgproject.content_rule_ensure_redhat_gpgkey_i
nstalled\nResult\r\tpass\n\nTitle\r\tEnsure gpgcheck Enabled In Main Yum
...
初始扫描结果可能会有大量失败项,这是正常现象。可以使用以下命令在浏览器中打开HTML报告:
$ open generated/box1_report_01-initial-scan.html
从示例中可知,主机在OpenSCAP运行的94项检查中失败了51项,接下来需要降低失败检查的数量。
2. 生成修复Ansible剧本
在继续操作前,报告有如下警告:
不要在非操作环境中进行测试之前尝试实施本指南中的任何设置。本指南的创建者对其他方使用本指南不承担任何责任,也不对其质量、可靠性或任何其他特性作出明示或暗示的保证。
创建修复Ansible角色:
$ ansible-galaxy init roles/fix-ansible
在 roles/fix-ansible/defaults/main.yml 中设置默认变量:
playbook_file:
remote: "/tmp/{{ inventory_hostname }}_ansible.yml"
local: "generated/{{ inventory_hostname }}_ansible.yml"
log: "generated/{{ inventory_hostname }}_ansible.log"
ansible_fix_command: >
oscap xccdf generate fix
--profile {{ oscap.profile }}
--template urn:xccdf:fix:script:ansible
--output {{ playbook_file.remote }}
{{ report.results }}
missing_folders:
- "/etc/dconf/db/local.d/locks/"
missing_files:
- "/etc/dconf/db/local.d/locks/00-security-settings-lock"
- "/etc/sysconfig/prelink"
在 roles/fix-ansible/tasks/main.yml 中添加任务:
- name: fix missing folders
file:
path: "{{ item }}"
state: "directory"
with_items: "{{ missing_folders }}"
- name: fix missing files
file:
path: "{{ item }}"
state: "touch"
with_items: "{{ missing_files }}"
- name: do we already have the playbook?
stat:
path: "{{ playbook_file.remote }}"
register: playbook_check
- name: generate the ansible playbook with the fixes
command: "{{ ansible_fix_command }}"
args:
creates: "{{ playbook_file.remote }}"
ignore_errors: yes
- name: download the ansible playbook
fetch:
src: "{{ playbook_file.remote }}"
dest: "{{ playbook_file.local }}"
flat: yes
when: playbook_check.stat.exists == False
- name: run the ansible playbook locally
local_action:
module: "command ansible-playbook -i production --become --become-
method sudo {{ playbook_file.local }}"
become: no
register: playbook_run
when: playbook_check.stat.exists == False
- name: write the results to a log file
local_action:
module: "copy content={{ playbook_run.stdout }} dest={{
playbook_file.log }}"
become: no
when: playbook_check.stat.exists == False
更新 site.yml 文件:
---
- hosts: scap
gather_facts: true
become: yes
become_method: sudo
vars_files:
- group_vars/common.yml
roles:
- { role: install, tags: [ "scan" ] }
- { role: scan, tags: [ "scan" ], report_name: "01-initial-scan" }
- { role: fix-ansible, report_name: "01-initial-scan" }
- { role: scan, report_name: "02-post-ansible-fix" }
运行剧本:
$ ansible-playbook -i production site.yml
运行结果如下:
PLAY [scap]
***************************************************************************
**********
TASK [Gathering Facts]
**************************************************************************
ok: [box1]
TASK [install : update all of the installed packages]
*******************************************
ok: [box1]
TASK [install : install the packages needed]
****************************************************
ok: [box1] => (item=openscap-scanner)
ok: [box1] => (item=scap-security-guide)
TASK [scan : run the openscap scan]
*************************************************************
ok: [box1]
TASK [scan : download the html report]
**********************************************************
ok: [box1]
TASK [fix-ansible : fix missing folders]
********************************************************
changed: [box1] => (item=/etc/dconf/db/local.d/locks/)
TASK [fix-ansible : fix missing files]
**********************************************************
changed: [box1] => (item=/etc/dconf/db/local.d/locks/00-security-settings-
lock)
changed: [box1] => (item=/etc/sysconfig/prelink)
TASK [fix-ansible : do we already have the playbook?]
*******************************************
ok: [box1]
TASK [fix-ansible : generate the ansible playbook with the fixes]
*******************************
changed: [box1]
TASK [fix-ansible : download the ansible playbook]
**********************************************
changed: [box1]
TASK [fix-ansible : run the ansible playbook locally]
*******************************************
changed: [box1 -> localhost]
TASK [fix-ansible : write the results to a log file]
********************************************
changed: [box1 -> localhost]
TASK [scan : run the openscap scan]
*************************************************************
fatal: [box1]: FAILED! =>
...ignoring
TASK [scan : download the html report]
**********************************************************
changed: [box1]
PLAY RECAP
***************************************************************************
***********
box1 : ok=14 changed=8 unreachable=0 failed=0
查看报告:
$ open generated/box1_report_02-post-ansible-fix.html
结果显示失败规则减少到25项,这是因为部分修复规则迁移到Ansible的工作仍在进行中。例如,初始扫描结果中的 “Set SSH Idle Timeout Interval” 检查失败,点击可查看相关信息及修复方案。生成的剧本中应用此修复的任务如下:
- name: Set SSH Idle Timeout Interval
lineinfile:
create: yes
dest: /etc/ssh/sshd_config
regexp: ^ClientAliveInterval
line: "ClientAliveInterval {{ sshd_idle_timeout_value }}"
validate: sshd -t -f %s
#notify: restart sshd
tags:
- sshd_set_idle_timeout
- low_severity
- restrict_strategy
- low_complexity
- low_disruption
- CCE-27433-2
- NIST-800-53-AC-2(5)
- NIST-800-53-SA-8(i)
- NIST-800-53-AC-12
- NIST-800-171-3.1.11
- PCI-DSS-Req-8.1.8
- CJIS-5.5.6
- DISA-STIG-RHEL-07-040320
可以通过以下命令仅运行低中断的更改:
$ ansible-playbook -i production --become --become-method sudo --tags
"low_disruption" generated/box1_ansible.yml
在 box1_ansible.log 文件底部可看到剧本运行的更改结果:
PLAY RECAP
***************************************************************************
***********
box1 : ok=151 changed=85 unreachable=0 failed=0
3. 生成修复Bash脚本
为修复剩余问题,生成并执行Bash脚本:
$ ansible-galaxy init roles/fix-bash
在 roles/fix-bash/defaults/main.yml 中设置变量:
bash_file:
remote: "/tmp/{{ inventory_hostname }}_bash.sh"
log: "generated/{{ inventory_hostname }}_bash.log"
bash_fix_command: >
oscap xccdf generate fix
--profile {{ oscap.profile }}
--output {{ bash_file.remote }}
{{ report.results }}
在 roles/fix-bash/tasks/main.yml 中添加任务:
- name: do we already have the bash script?
stat:
path: "{{ bash_file.remote }}"
register: bash_script_check
- name: generate the bash script
command: "{{ bash_fix_command }}"
args:
creates: "{{ bash_file.remote }}"
ignore_errors: yes
- name: run the bash script
command: "bash {{ bash_file.remote }}"
ignore_errors: yes
register: bash_run
when: bash_script_check.stat.exists == False
- name: write the results to a log file
local_action:
module: "copy content={{ bash_run.stdout }} dest={{ bash_file.log }}"
become: no
when: bash_script_check.stat.exists == False
更新 site.yml 文件:
- hosts: scap
gather_facts: true
become: yes
become_method: sudo
vars_files:
- group_vars/common.yml
roles:
- { role: install, tags: [ "scan" ] }
- { role: scan, tags: [ "scan" ], report_name: "01-initial-scan" }
- { role: fix-ansible, report_name: "01-initial-scan" }
- { role: scan, report_name: "02-post-ansible-fix" }
- { role: fix-bash, report_name: "02-post-ansible-fix" }
- { role: scan, report_name: "03-post-bash-fix" }
运行剧本:
$ ansible-playbook -i production site.yml
运行结果如下:
PLAY [scap]
***************************************************************************
**********
TASK [Gathering Facts]
**************************************************************************
ok: [box1]
TASK [install : update all of the installed packages]
*******************************************
ok: [box1]
TASK [install : install the packages needed]
****************************************************
ok: [box1] => (item=openscap-scanner)
ok: [box1] => (item=scap-security-guide)
TASK [scan : run the openscap scan]
*************************************************************
ok: [box1]
TASK [scan : download the html report]
**********************************************************
ok: [box1]
TASK [fix-ansible : fix missing folders]
********************************************************
ok: [box1] => (item=/etc/dconf/db/local.d/locks/)
TASK [fix-ansible : fix missing files]
**********************************************************
changed: [box1] => (item=/etc/dconf/db/local.d/locks/00-security-settings-
lock)
changed: [box1] => (item=/etc/sysconfig/prelink)
TASK [fix-ansible : do we already have the playbook?]
*******************************************
ok: [box1]
TASK [fix-ansible : generate the ansible playbook with the fixes]
*******************************
skipping: [box1]
TASK [fix-ansible : download the ansible playbook]
**********************************************
skipping: [box1]
TASK [fix-ansible : run the ansible playbook locally]
*******************************************
skipping: [box1]
TASK [fix-ansible : write the results to a log file]
********************************************
skipping: [box1]
TASK [scan : run the openscap scan]
*************************************************************
ok: [box1]
TASK [scan : download the html report]
**********************************************************
ok: [box1]
TASK [fix-bash : do we already have the bash script?]
*******************************************
ok: [box1]
TASK [fix-bash : generate the bash script]
******************************************************
changed: [box1]
TASK [fix-bash : run the bash script]
***********************************************************
changed: [box1]
TASK [fix-bash : write the results to a log file]
***********************************************
changed: [box1 -> localhost]
TASK [scan : run the openscap scan]
*************************************************************
fatal: [box1]: FAILED! =>
...ignoring
TASK [scan : download the html report]
**********************************************************
changed: [box1]
PLAY RECAP
***************************************************************************
***********
box1 : ok=16 changed=6 unreachable=0 failed=0
查看最终报告:
$ open generated/box1_report_03-post-bash-fix.html
结果显示失败检查总数减少到5项。
4. 运行独立扫描
运行以下命令进行独立扫描:
$ ansible-playbook -i production --tags "scan" --extra-vars
"report_name=scan-only" site.yml
这将仅运行标记为 “scan” 的剧本部分,并将报告命名为 box1_report_scan-only.html 。
整个过程的流程图如下:
graph LR
A[运行初始扫描] --> B[生成修复Ansible剧本]
B --> C[生成修复Bash脚本]
C --> D[运行独立扫描]
5. 修复剩余失败检查
目前扫描显示的5个问题中有2个存在已知问题:
- xccdf_org.ssgproject.content_rule_audit_rules_privileged_commands
- xccdf_org.ssgproject.content_rule_audit_rules_login_events
相关修复正在进行中,可在 Red Hat’s Bugzilla 和 Red Hat’s Bugzilla 查看。
创建独立角色和剧本进行修复:
$ ansible-galaxy init roles/final-fixes
在 roles/final-fixes/tasks/main.yml 中添加任务:
- name: sort out the logrotate
lineinfile:
path: "/etc/logrotate.conf"
regexp: "^weekly"
line: "daily"
- name: add the missing line to the modules.rules
lineinfile:
path: "/etc/audit/rules.d/modules.rules"
line: "-a always,exit -F arch=b32 -S init_module -S delete_module -k
modules"
- name: add file for content_rule_file_permissions_var_log_audit
file:
path: "/var/log/audit/audit.log.fix"
state: "touch"
- name: copy the content_rule_file_permissions_var_log_audit.sh script
copy:
src: "content_rule_file_permissions_var_log_audit.sh"
dest: "/tmp/content_rule_file_permissions_var_log_audit.sh"
- name: run the content_rule_file_permissions_var_log_audit.sh script
command: "bash /tmp/content_rule_file_permissions_var_log_audit.sh"
content_rule_file_permissions_var_log_audit.sh 脚本内容如下:
if `grep -q ^log_group /etc/audit/auditd.conf` ; then
GROUP=$(awk -F "=" '/log_group/ {print $2}' /etc/audit/auditd.conf | tr -
d ' ')
if ! [ "${GROUP}" == 'root' ] ; then
chmod 0640 /var/log/audit/audit.log
chmod 0440 /var/log/audit/audit.log.*
else
chmod 0600 /var/log/audit/audit.log
chmod 0400 /var/log/audit/audit.log.*
fi
chmod 0640 /etc/audit/audit*
chmod 0640 /etc/audit/rules.d/*
else
chmod 0600 /var/log/audit/audit.log
chmod 0400 /var/log/audit/audit.log.*
chmod 0640 /etc/audit/audit*
chmod 0640 /etc/audit/rules.d/*
fi
创建 final-fixes.yml 剧本:
---
- hosts: scap
gather_facts: true
become: yes
become_method: sudo
vars_files:
- group_vars/common.yml
roles:
- { role: final-fixes }
- { role: scan, report_name: "04-final-fixes" }
运行剧本:
$ ansible-playbook -i production final-fixes.yml
运行结果如下:
PLAY [scap]
***************************************************************************
**********
TASK [Gathering Facts]
**************************************************************************
ok: [box1]
TASK [final-fixes : sort out the logrotate]
*****************************************************
changed: [box1]
TASK [final-fixes : add the missing line to the modules.rules]
**********************************
changed: [box1]
TASK [final-fixes : add file for
content_rule_file_permissions_var_log_audit] *******************
changed: [box1]
TASK [final-fixes : copy the content_rule_file_permissions_var_log_audit.sh
script] *************
changed: [box1]
TASK [final-fixes : run the content_rule_file_permissions_var_log_audit.sh
script] **************
changed: [box1]
TASK [scan : run the openscap scan]
*************************************************************
fatal: [box1]: FAILED! =>
...ignoring
TASK [scan : download the html report]
**********************************************************
changed: [box1]
PLAY RECAP
综上所述,通过以上步骤可以逐步强化服务器,降低扫描失败的检查数量。整个过程的操作步骤总结如下表:
| 步骤 | 操作 | 命令 |
| — | — | — |
| 1 | 运行初始扫描 | ansible-playbook -i production site.yml |
| 2 | 生成修复Ansible剧本 | ansible-galaxy init roles/fix-ansible 并设置相关变量和任务 |
| 3 | 生成修复Bash脚本 | ansible-galaxy init roles/fix-bash 并设置相关变量和任务 |
| 4 | 运行独立扫描 | ansible-playbook -i production --tags "scan" --extra-vars "report_name=scan-only" site.yml |
| 5 | 修复剩余失败检查 | ansible-galaxy init roles/final-fixes 并设置相关任务,运行 ansible-playbook -i production final-fixes.yml |
使用Ansible和OpenSCAP强化服务器
6. 总结与注意事项
- 强化效果 :从最初扫描时94项检查中失败51项,到经过Ansible剧本修复后失败规则减少到25项,再到生成并执行Bash脚本后失败检查总数减少到5项,最后对剩余问题进行修复,整个过程显著提高了服务器的安全性。
- 注意事项 :在实施修复操作前,务必在非操作环境中进行充分测试。因为修复过程中的更改可能会对目标主机的运行产生严重影响,即使是开发环境也需要谨慎对待。
7. 常见问题及解决方法
| 问题 | 现象 | 解决方法 |
|---|---|---|
| 扫描失败 | 扫描任务返回非零退出码,出现 “non-zero return code” 错误信息 | 检查网络连接,确保可以正常下载远程资源;检查扫描配置文件是否正确 |
| 剧本生成失败 | 生成Ansible剧本或Bash脚本的任务失败 | 检查相关命令的参数是否正确,确保目标文件路径存在且有足够的权限 |
| 修复不生效 | 再次扫描后失败规则数量没有明显减少 | 检查生成的剧本或脚本是否正确执行,查看日志文件以获取详细信息 |
8. 后续优化建议
- 定期扫描 :设置定期扫描任务,及时发现新出现的安全问题。可以使用系统的定时任务工具(如Cron)来实现。
- 自动化流程 :将整个强化过程集成到持续集成/持续部署(CI/CD)流程中,确保每次服务器部署或更新后都能自动进行安全检查和修复。
- 规则定制 :根据实际需求定制扫描规则,只关注与业务相关的安全问题,提高扫描效率和针对性。
9. 技术原理深入解析
- OpenSCAP工作原理 :OpenSCAP通过执行预定义的安全检查规则,对系统进行全面扫描。它使用XCCDF(eXtensible Configuration Checklist Description Format)标准来描述检查规则和结果,能够生成详细的报告,指出系统中存在的安全问题。
- Ansible自动化原理 :Ansible通过SSH协议与目标主机进行通信,使用剧本(Playbook)来定义一系列任务。剧本中的任务可以是安装软件包、修改配置文件、执行脚本等,通过自动化执行这些任务来实现系统的配置管理和修复。
10. 不同环境下的应用
- 开发环境 :在开发环境中,可以使用这些技术进行快速的安全检查和修复,帮助开发人员及时发现和解决安全隐患,确保代码的安全性。
- 测试环境 :在测试环境中进行全面的安全扫描和修复,为生产环境的部署提供保障。可以模拟生产环境的配置,确保系统在上线前具备较高的安全性。
- 生产环境 :在生产环境中,定期进行安全扫描和修复,及时发现并处理新出现的安全问题。同时,要注意在进行修复操作前进行充分的测试,避免对业务造成影响。
11. 与其他安全工具的对比
| 工具 | 优点 | 缺点 |
|---|---|---|
| OpenSCAP + Ansible | 自动化程度高,可定制性强,能够生成详细的报告 | 需要一定的技术基础,配置和维护相对复杂 |
| 传统安全扫描工具 | 操作简单,易于上手 | 自动化程度低,难以进行大规模的系统管理和修复 |
12. 未来发展趋势
随着信息技术的不断发展,服务器安全面临着越来越多的挑战。未来,OpenSCAP和Ansible等工具可能会朝着以下方向发展:
- 智能化 :利用人工智能和机器学习技术,自动分析扫描结果,提供更精准的修复建议。
- 云原生支持 :更好地支持云原生环境,如容器、Kubernetes等,为云环境中的服务器提供安全保障。
- 多平台兼容 :支持更多的操作系统和平台,扩大应用范围。
整个服务器强化过程的详细流程图如下:
graph LR
A[运行初始扫描] --> B{扫描结果分析}
B -->|有失败项| C[生成修复Ansible剧本]
C --> D[执行Ansible剧本修复]
D --> E{再次扫描结果分析}
E -->|仍有失败项| F[生成修复Bash脚本]
F --> G[执行Bash脚本修复]
G --> H{再次扫描结果分析}
H -->|还有失败项| I[修复剩余失败检查]
H -->|无失败项| J[完成强化]
B -->|无失败项| J[完成强化]
E -->|无失败项| J[完成强化]
I --> K[最终扫描]
K --> J[完成强化]
L[定期扫描] --> A
M[自动化流程集成] --> A
通过以上全面的介绍,我们可以看到使用Ansible和OpenSCAP强化服务器是一个系统而有效的过程。通过合理运用这些技术,我们能够提高服务器的安全性,保障业务的稳定运行。同时,我们也需要关注技术的发展趋势,不断优化和改进我们的安全策略。
超级会员免费看
26

被折叠的 条评论
为什么被折叠?



