Log4Shell 是一个严重的远程代码执行漏洞(CVE-2021-44228),影响广泛使用的 Java 日志框架 Log4j。以下是一个示例,展示如何使用 Ansible Playbook 自动化检测 Log4Shell 漏洞,包括安装依赖项、验证 GPG 密钥、运行漏洞扫描器,并显示结果。
Log4Shell 漏洞简介
Log4Shell 是一个严重的安全漏洞,允许攻击者通过网络请求在受影响的系统上执行任意代码。该漏洞于 2021 年 11 月 24 日由阿里巴巴云安全团队发现,并迅速引起了全球关注。由于 Log4j 广泛应用于各种服务器和软件中,该漏洞影响了大量企业级云环境和服务,包括 AWS、Cloudflare、iCloud、Minecraft: Java Edition 等。
Ansible Playbook 概述
以下 Playbook 使用 Red Hat 提供的检测脚本(RHSB-2021-009)自动化检测 Log4Shell 漏洞。脚本会扫描系统中的 Log4j 文件,并报告是否存在漏洞。
示例:使用 Ansible 检测 Log4Shell 漏洞
变量文件:vars.yml
# Red Hat 检测器:https://access.redhat.com/security/vulnerabilities/RHSB-2021-009
sh_detector: "cve-2021-44228--2021-12-20-1836.sh"
sh_signature: 'cve-2021-44228--2021-12-20-1836.sh.asc'
detector_baseurl: 'https://access.redhat.com/sites/default/files/'
detector_path: "/var/"
detector_dir: "/tmp/cve-2021-44228/"
detector_run_dir: 'tmp'
detector_options: '-n -d --no-progress --scan {{ detector_path }}'
gpg_keyid: '7514F77D8366B0D9'
gpg_public_key: 'gpg --keyserver pgp.mit.edu --recv {{ gpg_keyid }}'
clean_run_before: true
delete_after: false
verify_gpg: true
Playbook 文件:log4j-cve-2021-44228.yml
---
- name: Apache Log4j (CVE-2021-44228) 漏洞检测器
hosts: all
become: true
tasks:
- include_vars: vars.yml
- name: 确保依赖项已安装
ansible.builtin.package:
name: unzip
state: present
update_cache: true
- name: 创建检测器目录
ansible.builtin.file:
path: '{{ detector_dir }}'
state: directory
- name: 下载检测器文件
ansible.builtin.get_url:
url: "{{ detector_baseurl }}{{ item }}"
dest: "{{ detector_dir }}{{ item }}"
mode: '0755'
owner: root
group: root
with_items:
- '{{ sh_detector }}'
- '{{ sh_signature }}'
- name: 导入 GPG 公钥
ansible.builtin.shell: '{{ gpg_public_key }}'
when: verify_gpg == true
- name: 验证检测器文件
ansible.builtin.shell: 'gpg --verify {{ detector_dir }}{{ sh_signature }} {{ detector_dir }}{{ sh_detector }}'
when: verify_gpg == true
- name: 清理之前的检测运行目录
ansible.builtin.file:
path: '{{ detector_dir }}{{ detector_run_dir }}'
state: absent
when: clean_run_before == true
- name: 创建检测运行目录
ansible.builtin.file:
path: '{{ detector_dir }}{{ detector_run_dir }}'
state: directory
- name: 运行检测器/扫描器
ansible.builtin.shell: '{{ detector_dir }}{{ sh_detector }} {{ detector_options }} --tmp {{ detector_dir }}{{ detector_run_dir }}'
- name: 查找检测运行目录中的文件
ansible.builtin.find:
paths: '{{ detector_dir }}{{ detector_run_dir }}'
register: vulnerable
- name: 打印发现的漏洞路径
ansible.builtin.debug:
var: vulnerable
- name: 删除检测器目录
ansible.builtin.file:
path: '{{ detector_dir }}'
state: absent
when: delete_after == true
执行命令
$ ansible-playbook -i inventory log4j-cve-2021-44228.yml
执行结果
PLAY [Apache Log4j (CVE-2021-44228) 漏洞检测器] ***********************************
TASK [Gathering Facts] *****************************************************
ok: [demo]
TASK [include_vars] *****************************************************
ok: [demo]
TASK [确保依赖项已安装] *****************************************************
ok: [demo]
TASK [创建检测器目录] *****************************************************
ok: [demo]
TASK [下载检测器文件] *****************************************************
ok: [demo] => (item=cve-2021-44228--2021-12-20-1836.sh)
ok: [demo] => (item=cve-2021-44228--2021-12-20-1836.sh.asc)
TASK [导入 GPG 公钥] *****************************************************
changed: [demo]
TASK [验证检测器文件] *****************************************************
changed: [demo]
TASK [清理之前的检测运行目录] *****************************************************
changed: [demo]
TASK [创建检测运行目录] *****************************************************
changed: [demo]
TASK [运行检测器/扫描器] *****************************************************
changed: [demo]
TASK [查找检测运行目录中的文件] *****************************************************
ok: [demo]
TASK [打印发现的漏洞路径] *****************************************************
ok: [demo] => {
"vulnerable": {
"changed": false,
"examined": 1,
"failed": false,
"files": [],
"matched": 0,
"msg": "All paths examined",
"skipped_paths": {}
}
}
TASK [删除检测器目录] *****************************************************
skipping: [demo]
PLAY RECAP *********************************************************************
demo : ok=12 changed=5 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
Ansible Galaxy 角色
您也可以通过 Ansible Galaxy 安装现成的 Ansible 角色来执行此任务:
ansible-galaxy install lucab85.ansible_role_log4shell
注意事项
- 依赖项:确保目标系统已安装
unzip包。 - GPG 验证:通过导入公钥验证检测器脚本的完整性。
- 清理:在运行检测器之前清理之前的运行目录,确保结果准确。
- 幂等性:如果检测器已运行且未发现新漏洞,任务将不会重复执行。
希望这个示例对您有帮助!