
log4j mitigation work
This Ansible role deploys a Python-based script to programmatically mitigate the Log4Shell vulnerability (CVE-2021-44228) by removing the vulnerable JndiLookup class from affected Log4j JAR files.
It is designed to be run via Ansible, but you can also execute the script manually if you wish.
Read more about the Log4Shell vulnerability here:
.jar, .war and .ear files in specified application directoriesJndiLookup.class is presentJndiLookup.class using Python's zipfile--apply the script opens every archive
read-only and reports the ones still carrying JndiLookup.class; nothing is
backed up or rewritten. The role passes --apply unless
log4shell_apply: falsePyYAML, only for a run that reads a YAML config file (the installed
apply). The role installs it from the distribution (python3-yaml,
py3-yaml or python3-pyyaml), never from PyPI. The detection entry
needs only the standard library: its config goes in as JSON on stdinNo zip or zipinfo needed: archives are read and rewritten with the
standard library's zipfile, which matters on minimal container base images
where neither is installed.
The script is configured via a YAML file, templated automatically by the role.
Example config.yaml:
app_directories:
- /opt/apps
backup_directory: /opt/backups
log_file: /var/log/log4j_mitigation.log
app_directories: list of directories to scan for JAR filesbackup_directory: where to store the original JAR backupslog_file: path to a log file for operation trackingAdd this role to your playbook:
- name: Mitigate Log4Shell CVE-2021-44228
hosts: all
become: true
roles:
- log4shell_mitigation
If you wish to run the script manually, you can do so:
Copy the script and the shared library beside it to your target machine:
files/log4shell_mitigator.py
files/samp_common.py
Create your own config.yaml in the same directory.
Run it:
python3 log4shell_mitigator.py # dry run, config.yaml beside the script
python3 log4shell_mitigator.py --config /path/to/config.yaml
sudo python3 log4shell_mitigator.py --apply # back up and rewrite
scanned=N vulnerable=N patched=N already_stripped=N unreadable=N failed=N nested_needing_review=N dry_run=N to
stdout. vulnerable counts archives still carrying JndiLookup.class;
already_stripped counts log4j-core archives without it. If
nested_needing_review or unreadable is non-zero, those archives were not
judged and need handling by hand; the log names each one.0 nothing left carrying the class, 2 an archive still
carries it or could not be judged, 1 a backup or rewrite failed, 130
interrupted/var/log/log4j_mitigation.log) to see which JARs were patchedThis repository is a SAMP: the Microcelium Substrate imports it from this exact URL, pins it by commit, verifies the release tag's signature, scans it, and has a human approve it before it runs anywhere.
Manifest. samp.yaml declares the CVEs, which hosts it
applies to, the detection and apply entry points, whether each needs root,
the options with their defaults, every path an apply writes (touches),
every program it runs (commands) and the units it may restart. A reviewer
reads it first; the substrate checks the code against it.
Detection writes nothing. tasks/detect.yml stages the script and
samp_common.py in a private temporary directory, runs it with the config
as JSON on stdin and the log on stdout, and removes the directory. No
script is installed, no config templated, no backup directory made, no log
file written and no package installed. It runs under ansible-playbook --check too, because it only reads.
Apply. tasks/main.yml with log4shell_apply: true. Options:
Nothing is restarted or reloaded unless the option says so, and nothing is ever reverted: a downgrade reinstates the vulnerability.
Result. The script's last line on stdout is
SAMP-RESULT {"format":1,"verdict":…,"counters":{…},"backups":[…],…},
after the human key=N summary line. The verdict is one of vulnerable,
no_fix, unknown, fixed (with "workaround":true when a workaround,
not a fixed package, is what is in effect), not_affected, or failed.
Exit codes. 0 clean (fixed, not_affected), 2 vulnerable or
undecided (vulnerable, no_fix, unknown: never clean), 1 the run
itself failed, 130 interrupted.
Shared code. log4shell_mitigation/files/samp_common.py is a byte-for-byte copy of
samp-common 1.0.3 (the dpkg, rpm and apk version
comparisons, the result line, config loading). Its sha256 is in
samp.yaml, and tests/test_samp_pack.py fails if the copy differs. Never
edit it here; re-vendor it with samp-common's scripts/vendor.py.
Releases are SSH-signed annotated tags, samp/v<version> (this one:
samp/v1.0.3), signed with a hardware-backed key. The substrate
imports a signed tag, never a branch.
MIT
PRs and improvements welcome -> please fork and submit a merge request.
This software and accompanying Ansible role are provided “as is” and without warranties of any kind, express or implied. You are solely responsible for evaluating and testing the suitability, safety, and effectiveness of this code before applying it to any environment. AxisOps Ltd (GB08681761) expressly disclaims any liability for damages, data loss, service interruption, or other harm resulting from the use or misuse of this code.
Use at your own risk. Always audit, test, and validate the code in a controlled environment before deployment to production.
SysOps Team @ AxisOps