diff --git a/.ansible-lint b/.ansible-lint index f2a7e7c..f21e1f4 100644 --- a/.ansible-lint +++ b/.ansible-lint @@ -1,6 +1,9 @@ parseable: true quiet: true skip_list: + - 'schema' + - 'no-changed-when' + - 'fqcn-builtins' - '204' - '305' - '303' diff --git a/.github/workflows/github_vars.tfvars b/.github/workflows/github_vars.tfvars index e50753d..24daeca 100644 --- a/.github/workflows/github_vars.tfvars +++ b/.github/workflows/github_vars.tfvars @@ -8,7 +8,7 @@ environment = "lockdown_github_repo_workflow" // Matching pair name found in AWS for keypairs PEM key ami_key_pair_name = "github_actions" -private_key = "'.ssh/github_actions.pem'" +private_key = ".ssh/github_actions.pem" main_vpc_cidr = "172.22.0.0/24" public_subnets = "172.22.0.128/26" private_subnets = "172.22.0.192/26" diff --git a/.github/workflows/linux_benchmark_testing.yml b/.github/workflows/linux_benchmark_testing.yml index 5b2a708..908ea06 100644 --- a/.github/workflows/linux_benchmark_testing.yml +++ b/.github/workflows/linux_benchmark_testing.yml @@ -81,18 +81,9 @@ jobs: working-directory: .github/workflows run: cat hosts.yml -# Centos 7 images take a while to come up insert sleep or playbook fails +# Aws deployments taking a while to come up insert sleep or playbook fails - - name: Check if test os is rhel7 - working-directory: .github/workflows - id: test_os - run: >- - echo "::set-output name=RHEL7::$( - grep -c RHEL7 OS.tfvars - )" - - - name: if RHEL7 - Sleep for 60 seconds - #if: steps.test_os.outputs.RHEL7 >= 1 + - name: Sleep for 60 seconds run: sleep 60s shell: bash @@ -117,4 +108,4 @@ jobs: env: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - run: terraform destroy -var-file "github_vars.tfvars" -var-file "OS.tfvars" --auto-approve -input=false + run: terraform destroy -var-file "github_vars.tfvars" -var-file "OS.tfvars" --auto-approve -input=false diff --git a/.yamllint b/.yamllint index fdea629..693eec6 100644 --- a/.yamllint +++ b/.yamllint @@ -2,22 +2,34 @@ ignore: | tests/ molecule/ + .github/ .gitlab-ci.yml *molecule.yml extends: default rules: - indentation: - # Requiring 4 space indentation - spaces: 4 - # Requiring consistent indentation within a file, either indented or not - indent-sequences: consistent - truthy: disable - braces: - max-spaces-inside: 1 - level: error - brackets: - max-spaces-inside: 1 - level: error - line-length: disable + indentation: + # Requiring 4 space indentation + spaces: 4 + # Requiring consistent indentation within a file, either indented or not + indent-sequences: consistent + truthy: disable + braces: + max-spaces-inside: 1 + level: error + brackets: + max-spaces-inside: 1 + level: error + indentation: + indent-sequences: consistent + level: error + line-length: disable + key-duplicates: enable + new-line-at-end-of-file: enable + new-lines: + type: unix + trailing-spaces: enable + truthy: + allowed-values: ['true', 'false'] + check-keys: false diff --git a/Changelog.md b/Changelog.md index ee9aff4..2fa85d4 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,33 @@ # Changes to rhel9CIS +## 0.4 + +- Added assertion that ansible_user has password set for rule 5.3.4 +- RockyLinux now supported - release since initial branches +- gpg check updates +- audit out dir now /opt +- lint updates and improvements +- workflow updates and improvements moved to rocky image +- selinux regexp improvements +- warning summary now at end of play +- advanced auditd options to exclude users in POST section +- Issues fixed thanks to fgierlinger + - [#21](https://github.com/ansible-lockdown/RHEL9-CIS/issues/21) + - [#22](https://github.com/ansible-lockdown/RHEL9-CIS/issues/22) + +## 0.3 + +- update to auditd template + - uses facts and template new variable + - update_audit_template (default false) +- sysctl template updates and idempotency improvements +- container discovery usage improvements +- 3.4.1.5 discovery improvement +- 5.6.1.4 discovery improvement +- logrotate process logrotate.timer +- tidy up become: +- logic improvements + ## 0.2 - not all controls work with rhel8 releases any longer diff --git a/README.md b/README.md index 048c85f..4c7324b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Development Only -## RHEL 9 CIS (predicted) - ALPHA - CIS baselines or OS not yet GA +## RHEL 9 CIS (predicted) - Beta - CIS baselines or OS not yet GA ## Testing if you have access to the RH developer branches @@ -17,7 +17,7 @@ Based on [CIS RedHat Enterprise Linux 8 Benchmark v2.0.0. - 02-23-2022 ](https:/ ## Join us -On our [Discord Server](https://discord.gg/JFxpSgPFEJ) to ask questions, discuss features, or just chat with other Ansible-Lockdown users +On our [Discord Server](https://discord.io/ansible-lockdown) to ask questions, discuss features, or just chat with other Ansible-Lockdown users ## Caution(s) @@ -49,7 +49,9 @@ Refer to [RHEL9-CIS-Audit](https://github.com/ansible-lockdown/RHEL9-CIS-Audit). ## Requirements -RHEL 9 - Other versions are not supported. +RHEL 9 +Almalinux 9 +Rocky 9 - Access to download or add the goss binary and content to the system if using auditing (other options are available on how to get the content to the system.) diff --git a/defaults/main.yml b/defaults/main.yml index 6dfa404..510784b 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -369,7 +369,6 @@ rhel9cis_rh_sub_password: password # RedHat Satellite Subscription items rhel9cis_rhnsd_required: false - # 1.4.2 Bootloader password rhel9cis_bootloader_password_hash: 'grub.pbkdf2.sha512.changethispassword' rhel9cis_bootloader_password: random @@ -460,6 +459,11 @@ rhel9cis_tftp_client: false ## Section3 vars +## Sysctl +sysctl_update: false +flush_ipv4_route: false +flush_ipv6_route: false + ### Firewall Service - either firewalld, iptables, or nftables #### Some control allow for services to be removed or masked #### The options are under each heading @@ -498,6 +502,12 @@ rhel9cis_audit_back_log_limit: 8192 # The max_log_file parameter should be based on your sites policy rhel9cis_max_log_file_size: 10 +### 4.1.3.x audit template +update_audit_template: false + +## Advanced option found in auditd post +allow_auditd_uid_user_exclusions: false + ## Preferred method of logging ## Whether rsyslog or journald preferred method for local logging ## Affects rsyslog cis 4.2.1.3 and journald cis 4.2.2.5 @@ -633,8 +643,8 @@ audit_run_script_environment: ### Goss binary settings ### goss_version: - release: v0.3.16 - checksum: 'sha256:827e354b48f93bce933f5efcd1f00dc82569c42a179cf2d384b040d8a80bfbfb' + release: v0.3.18 + checksum: 'sha256:432308ebca0caf8165d45bd27e3262126aad9d15572ac8cb3149b3c91f75aace' audit_bin_path: /usr/local/bin/ audit_bin: "{{ audit_bin_path }}goss" audit_format: json @@ -651,7 +661,7 @@ copy_goss_from_path: /some/accessible/path ## managed by the control audit_content # git audit_file_git: "https://github.com/ansible-lockdown/{{ benchmark }}-Audit.git" -audit_git_version: main +audit_git_version: devel # copy: audit_local_copy: "some path to copy from" @@ -659,12 +669,9 @@ audit_local_copy: "some path to copy from" # get_url: audit_files_url: "some url maybe s3?" -# Where the goss audit configuration will be stored -audit_files: "/var/tmp/{{ benchmark }}-Audit/" - ## Goss configuration information # Where the goss configs and outputs are stored -audit_out_dir: '/var/tmp' +audit_out_dir: '/opt' audit_conf_dir: "{{ audit_out_dir }}/{{ benchmark }}-Audit/" pre_audit_outfile: "{{ audit_out_dir }}/{{ ansible_hostname }}_pre_scan_{{ ansible_date_time.epoch }}.{{ audit_format }}" post_audit_outfile: "{{ audit_out_dir }}/{{ ansible_hostname }}_post_scan_{{ ansible_date_time.epoch }}.{{ audit_format }}" diff --git a/handlers/main.yml b/handlers/main.yml index 08c8026..0fae419 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,14 +1,23 @@ --- # handlers file for RHEL9-CIS +- name: reload sysctl + shell: sysctl --system + args: + warn: false + when: + - sysctl_updated.changed + - name: sysctl flush ipv4 route table become: true sysctl: name: net.ipv4.route.flush value: '1' sysctl_set: true - ignore_errors: true - when: ansible_virtualization_type != "docker" + ignore_errors: true # noqa ignore-errors + when: + - flush_ipv4_route + - not system_is_container tags: - skip_ansible_lint @@ -18,35 +27,9 @@ name: net.ipv6.route.flush value: '1' sysctl_set: true - when: ansible_virtualization_type != "docker" - -- name: update sysctl - template: - src: "etc/sysctl.d/{{ item }}.j2" - dest: "/etc/sysctl.d/{{ item }}" - owner: root - group: root - mode: 0600 - notify: reload sysctl - with_items: - - 60-kernel_sysctl.conf - - 60-disable_ipv6.conf - - 60-netipv4_sysctl.conf - - 60-netipv6_sysctl.conf when: - - ansible_virtualization_type != "docker" - - "'procps-ng' in ansible_facts.packages" - -- name: reload sysctl - sysctl: - name: net.ipv4.route.flush - value: '1' - state: present - reload: true - ignoreerrors: true - when: - - ansible_virtualization_type != "docker" - - "'systemd' in ansible_facts.packages" + - flush_ipv6_route + - not system_is_container - name: systemd restart tmp.mount become: true @@ -72,53 +55,30 @@ warn: false - name: restart firewalld - become: true service: name: firewalld state: restarted - name: restart sshd - become: true service: name: sshd state: restarted - name: restart postfix - become: true service: name: postfix state: restarted - name: reload dconf - become: true shell: dconf update args: warn: false -- name: update auditd - template: - src: audit/99_auditd.rules.j2 - dest: /etc/audit/rules.d/99_auditd.rules - owner: root - group: root - mode: 0600 - notify: restart auditd - -- name: restart auditd - shell: /sbin/service auditd restart - changed_when: false - check_mode: false - failed_when: false - args: - warn: false - tags: - - skip_ansible_lint - - name: grub2cfg shell: "grub2-mkconfig -o /boot/grub2/grub.cfg" args: warn: false - ignore_errors: True + ignore_errors: true # noqa ignore-errors tags: - skip_ansible_lint @@ -142,6 +102,27 @@ systemd: daemon-reload: true +## Auditd tasks note order for handlers to run + +- name: auditd_immutable_check + shell: grep -c "^-e 2" /etc/audit/rules.d/99_auditd.rules + changed_when: false + register: auditd_immutable_check + +- name: audit_immutable_fact + debug: + msg: "Reboot required for auditd to apply new rules as immutable set" + notify: change_requires_reboot + when: + - auditd_immutable_check.stdout == '1' + +- name: restart auditd + shell: service auditd restart + args: + warn: false + tags: + - skip_ansible_lint + - name: change_requires_reboot set_fact: change_requires_reboot: true diff --git a/site.yml b/site.yml index 379549f..4446d3e 100644 --- a/site.yml +++ b/site.yml @@ -1,7 +1,6 @@ --- - hosts: all become: true - roles: - role: "{{ playbook_dir }}" diff --git a/tasks/LE_audit_setup.yml b/tasks/LE_audit_setup.yml index e4cac49..98f3855 100644 --- a/tasks/LE_audit_setup.yml +++ b/tasks/LE_audit_setup.yml @@ -22,7 +22,7 @@ - get_goss_file == 'copy' - name: install git if not present - package: + package: name: git state: present register: git_installed diff --git a/tasks/auditd.yml b/tasks/auditd.yml new file mode 100644 index 0000000..74830ca --- /dev/null +++ b/tasks/auditd.yml @@ -0,0 +1,26 @@ +--- + +- name: POST | AUDITD | Apply auditd template will for section 4.1.3 - only required rules will be added + template: + src: audit/99_auditd.rules.j2 + dest: /etc/audit/rules.d/99_auditd.rules + owner: root + group: root + mode: 0600 + register: audit_rules_updated + notify: + - auditd_immutable_check + - audit_immutable_fact + - restart auditd + +- name: POST | Set up auditd user logging exceptions + template: + src: audit/98_auditd_exception.rules.j2 + dest: /etc/audit/rules.d/98_auditd_exceptions.rules + owner: root + group: root + mode: 0600 + notify: restart auditd + when: + - allow_auditd_uid_user_exclusions + - rhel9cis_auditd_uid_exclude | length > 0 diff --git a/tasks/main.yml b/tasks/main.yml index 1b240f7..0d272b1 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -20,6 +20,28 @@ tags: - always +- name: "Check password set for {{ ansible_user }}" + block: + - name: Capture current password state of "{{ ansible_user }}" + shell: "grep {{ ansible_user }} /etc/shadow | awk -F: '{print $2}'" + changed_when: false + failed_when: false + check_mode: false + register: ansible_user_password_set + + - name: "Assert that password set for {{ ansible_user }} and account not locked" + assert: + that: ansible_user_password_set.stdout | length != 0 and ansible_user_password_set.stdout != "!!" + fail_msg: "You have {{ sudo_password_rule }} enabled but the user = {{ ansible_user }} has no password set - It can break access" + success_msg: "You a password set for the {{ ansible_user }}" + vars: + sudo_password_rule: rhel9cis_rule_5_3_4 + when: + - rhel9cis_rule_5_3_4 + - not system_is_ec2 + tags: + - user_passwd + - name: Setup rules if container block: - name: Discover and set container variable if required @@ -123,49 +145,49 @@ - name: run Section 1 tasks import_tasks: section_1/main.yml - become: true when: rhel9cis_section1 tags: - rhel9cis_section1 - name: run Section 2 tasks import_tasks: section_2/main.yml - become: true when: rhel9cis_section2 tags: - rhel9cis_section2 - name: run Section 3 tasks import_tasks: section_3/main.yml - become: true when: rhel9cis_section3 tags: - rhel9cis_section3 - name: run Section 4 tasks import_tasks: section_4/main.yml - become: true when: rhel9cis_section4 tags: - rhel9cis_section4 - name: run Section 5 tasks import_tasks: section_5/main.yml - become: true when: rhel9cis_section5 tags: - rhel9cis_section5 - name: run Section 6 tasks import_tasks: section_6/main.yml - become: true when: rhel9cis_section6 tags: - rhel9cis_section6 +- name: run auditd logic + import_tasks: auditd.yml + when: + - update_audit_template + tags: + - always + - name: run post remediation tasks import_tasks: post.yml - become: true tags: - post_tasks - always @@ -180,3 +202,10 @@ msg: "{{ audit_results.split('\n') }}" when: - run_audit + +- name: If Warnings found Output count and control IDs affected + debug: + msg: "You have {{ warn_count }} Warning(s) that require investigating that are related to the following benchmark ID(s) {{ control_number }}" + when: warn_count != 0 + tags: + - always diff --git a/tasks/post.yml b/tasks/post.yml index c0f6be8..3b5c3f2 100644 --- a/tasks/post.yml +++ b/tasks/post.yml @@ -12,57 +12,24 @@ tags: - always -- name: trigger update sysctl - shell: /bin/true - args: - warn: false - changed_when: true - check_mode: false - notify: update sysctl +- name: update sysctl + template: + src: "etc/sysctl.d/{{ item }}.j2" + dest: "/etc/sysctl.d/{{ item }}" + owner: root + group: root + mode: 0600 + register: sysctl_updated + notify: reload sysctl + with_items: + - 60-kernel_sysctl.conf + - 60-disable_ipv6.conf + - 60-netipv4_sysctl.conf + - 60-netipv6_sysctl.conf when: - - rhel9cis_rule_3_1_1 or - rhel9cis_rule_3_1_2 or - rhel9cis_rule_3_1_3 or - rhel9cis_rule_3_2_1 or - rhel9cis_rule_3_2_2 or - rhel9cis_rule_3_3_1 or - rhel9cis_rule_3_3_2 or - rhel9cis_rule_3_3_3 or - rhel9cis_rule_3_3_4 or - rhel9cis_rule_3_3_5 or - rhel9cis_rule_3_3_6 or - rhel9cis_rule_3_3_7 or - rhel9cis_rule_3_3_8 or - rhel9cis_rule_3_3_9 - tags: - - sysctl - -- name: trigger update auditd - shell: /bin/true - args: - warn: false - notify: update auditd - changed_when: true - check_mode: false - when: - - rhel9cis_rule_4_1_1_1 or - rhel9cis_rule_4_1_1_2 or - rhel9cis_rule_4_1_1_3 or - rhel9cis_rule_4_1_2_1 or - rhel9cis_rule_4_1_2_2 or - rhel9cis_rule_4_1_2_3 or - rhel9cis_rule_4_1_3 or - rhel9cis_rule_4_1_4 or - rhel9cis_rule_4_1_5 or - rhel9cis_rule_4_1_6 or - rhel9cis_rule_4_1_7 or - rhel9cis_rule_4_1_8 or - rhel9cis_rule_4_1_9 or - rhel9cis_rule_4_1_10 or - rhel9cis_rule_4_1_11 or - rhel9cis_rule_4_1_12 - tags: - - auditd + - sysctl_update + - not system_is_container + - "'procps-ng' in ansible_facts.packages" - name: flush handlers meta: flush_handlers @@ -77,11 +44,20 @@ - name: POST | Warning a reboot required but skip option set debug: - msg: "Warning! changes have been made that require a reboot to be implemented but skip reboot was set - Can affect compliance check results" + msg: "Warning!! changes have been made that require a reboot to be implemented but skip reboot was set - Can affect compliance check results" changed_when: true when: - change_requires_reboot - skip_reboot + + - name: "POST | Warning a reboot required but skip option set | warning count" + set_fact: + control_number: "{{ control_number }} + [ 'Reboot_required' ]" + warn_count: "{{ warn_count | int + 1 }}" + when: + - change_requires_reboot + - skip_reboot + tags: - grub - level1-server diff --git a/tasks/post_remediation_audit.yml b/tasks/post_remediation_audit.yml index 0ab61b2..4429b7e 100644 --- a/tasks/post_remediation_audit.yml +++ b/tasks/post_remediation_audit.yml @@ -2,7 +2,7 @@ - name: "Post Audit | Run post_remediation {{ benchmark }} audit" shell: "{{ audit_conf_dir }}/run_audit.sh -v {{ audit_vars_path }} -o {{ post_audit_outfile }} -g {{ group_names }}" - environment: "{{ audit_run_script_environment|default({}) }}" + environment: "{{ audit_run_script_environment | default({}) }}" changed_when: audit_run_post_remediation.rc == 0 register: audit_run_post_remediation args: @@ -28,7 +28,7 @@ - name: Capture post-audit result set_fact: - post_audit_summary: "{{ post_audit.stdout | from_json |json_query(summary) }}" + post_audit_summary: "{{ post_audit.stdout | from_json | json_query(summary) }}" vars: summary: 'summary."summary-line"' when: diff --git a/tasks/pre_remediation_audit.yml b/tasks/pre_remediation_audit.yml index bb9344a..93c4985 100644 --- a/tasks/pre_remediation_audit.yml +++ b/tasks/pre_remediation_audit.yml @@ -33,6 +33,9 @@ get_url: url: "{{ audit_files_url }}" dest: "{{ audit_conf_dir }}" + owner: root + group: root + mode: 0755 when: - audit_content == 'get_url' @@ -70,7 +73,7 @@ - name: "Pre Audit | Run pre_remediation {{ benchmark }} audit" shell: "{{ audit_conf_dir }}/run_audit.sh -v {{ audit_vars_path }} -o {{ pre_audit_outfile }} -g {{ group_names }}" - environment: "{{ audit_run_script_environment|default({}) }}" + environment: "{{ audit_run_script_environment | default({}) }}" changed_when: audit_run_pre_remediation.rc == 0 register: audit_run_pre_remediation args: @@ -87,7 +90,7 @@ - name: Pre Audit | Capture pre-audit result set_fact: - pre_audit_summary: "{{ pre_audit.stdout | from_json |json_query(summary) }}" + pre_audit_summary: "{{ pre_audit.stdout | from_json | json_query(summary) }}" vars: summary: 'summary."summary-line"' when: diff --git a/tasks/prelim.yml b/tasks/prelim.yml index eb17d00..80a273b 100644 --- a/tasks/prelim.yml +++ b/tasks/prelim.yml @@ -82,6 +82,31 @@ - level1-server - level1-workstation +- name: "PRELIM | Ensure python3-libselinux is installed" + package: + name: python3-libselinux + state: present + when: + - '"python3-libselinux" not in ansible_facts.packages' + +- name: "PRELIM | Set facts based on boot type" + block: + - name: "PRELIM | Check whether machine is UEFI-based" + stat: + path: /sys/firmware/efi + register: rhel_09_efi_boot + + - name: "PRELIM | AUDIT | set legacy boot and grub path | Bios" + set_fact: + rhel9cis_legacy_boot: true + grub2_path: /etc/grub2.cfg + when: not rhel_09_efi_boot.stat.exists + + - name: "PRELIM | set grub fact | UEFI" + set_fact: + grub2_path: /etc/grub2-efi.cfg + when: rhel_09_efi_boot.stat.exists + - name: "PRELIM | Section 4.1 | Configure System Accounting (auditd)" package: name: audit @@ -191,7 +216,9 @@ min_int_uid: "{{ uid_min_id.stdout }}" max_int_uid: "{{ uid_max_id.stdout }}" min_int_gid: "{{ gid_min_id.stdout }}" -- debug: + +- name: Output of uid findings + debug: msg: "{{ min_int_uid }} {{ max_int_uid }}" when: diff --git a/tasks/section_1/cis_1.1.1.x.yml b/tasks/section_1/cis_1.1.1.x.yml index f687901..1c99b62 100644 --- a/tasks/section_1/cis_1.1.1.x.yml +++ b/tasks/section_1/cis_1.1.1.x.yml @@ -7,14 +7,14 @@ path: /etc/modprobe.d/CIS.conf regexp: "^(#)?install cramfs(\\s|$)" line: "install cramfs /bin/true" - create: yes + create: true mode: 0600 - name: "1.1.1.1 | PATCH | Ensure mounting of cramfs filesystems is disabled | Disable cramfs" modprobe: name: cramfs state: absent - when: ansible_connection != 'docker' + when: not system_is_container when: - rhel9cis_rule_1_1_1_1 tags: @@ -32,14 +32,14 @@ path: /etc/modprobe.d/CIS.conf regexp: "^(#)?install squashfs(\\s|$)" line: "install squashfs /bin/true" - create: yes + create: true mode: 0600 - name: "1.1.1.2 | PATCH | Ensure mounting of squashfs filesystems is disabled | Disable squashfs" modprobe: name: squashfs state: absent - when: ansible_connection != 'docker' + when: not system_is_container when: - rhel9cis_rule_1_1_1_2 tags: @@ -57,14 +57,14 @@ path: /etc/modprobe.d/CIS.conf regexp: "^(#)?install udf(\\s|$)" line: "install udf /bin/true" - create: yes + create: true mode: 0600 - name: "1.1.1.3 | PATCH | Ensure mounting of udf filesystems is disable | Disable udf" modprobe: name: udf state: absent - when: ansible_connection != 'docker' + when: not system_is_container when: - rhel9cis_rule_1_1_1_3 tags: diff --git a/tasks/section_1/cis_1.1.2.x.yml b/tasks/section_1/cis_1.1.2.x.yml index a50797d..d7db5a6 100644 --- a/tasks/section_1/cis_1.1.2.x.yml +++ b/tasks/section_1/cis_1.1.2.x.yml @@ -1,11 +1,30 @@ --- - name: "1.1.2.1 | PATCH | Ensure /tmp is a separate partition" - debug: - msg: "Warning! /tmp is not mounted on a separate partition" + block: + - name: "1.1.2.1 | PATCH | Ensure /tmp is a separate partition | Absent" + debug: + msg: "Warning!! /tmp is not mounted on a separate partition" + when: + - required_mount not in mount_names + + - name: "1.1.2.1 | PATCH | Ensure /tmp is a separate partition | Warn Count" + set_fact: + control_number: "{{ control_number }} + [ 'rule_1.1.2.1' ]" + warn_count: "{{ warn_count | int + 1 }}" + when: + - required_mount not in mount_names + + - name: "1.1.3.1 | AUDIT | Ensure separate partition exists for /var | Present" + debug: + msg: "Congratulations: {{ required_mount }} exists." + register: var_mount_present + when: + - required_mount in mount_names + vars: + required_mount: '/tmp' when: - rhel9cis_rule_1_1_2_1 - - ansible_mounts | selectattr('mount', 'match', '^/tmp$') | list | length == 0 tags: - level1-server - level1-workstation @@ -68,7 +87,6 @@ tags: - level1-server - level1-workstation - - scored - patch - mounts - rule_1.1.2.1 diff --git a/tasks/section_1/cis_1.1.3.x.yml b/tasks/section_1/cis_1.1.3.x.yml index 8fa9e4b..9e4feb8 100644 --- a/tasks/section_1/cis_1.1.3.x.yml +++ b/tasks/section_1/cis_1.1.3.x.yml @@ -4,12 +4,19 @@ block: - name: "1.1.3.1 | AUDIT | Ensure separate partition exists for /var | Absent" debug: - msg: "Warning! {{ required_mount }} doesn't exist. This is a manual task" + msg: "Warning!! {{ required_mount }} doesn't exist. This is a manual task" register: var_mount_absent changed_when: var_mount_absent.skipped is undefined when: - required_mount not in mount_names + - name: "1.1.3.1 | AUDIT | Ensure separate partition exists for /var | Warn Count" + set_fact: + control_number: "{{ control_number }} + [ 'rule_1.1.3.1' ]" + warn_count: "{{ warn_count | int + 1 }}" + when: + - required_mount not in mount_names + - name: "1.1.3.1 | AUDIT | Ensure separate partition exists for /var | Present" debug: msg: "Congratulations: {{ required_mount }} exists." @@ -38,7 +45,7 @@ src: "{{ item.device }}" fstype: "{{ item.fstype }}" state: present - opts: defaults,{% if rhel9cis_rule_1_1_3_2 %}nodev,{% endif %}{% if rhel9cis_rule_1_1_3_3 %}noexec,{% endif %}{% if rhel9cis_rule_1_1_3_4 %}nosuid{% endif %} + opts: defaults,{% if rhel9cis_rule_1_1_3_3 %}noexec,{% endif %}{% if rhel9cis_rule_1_1_3_2 %}nodev,{% endif %}{% if rhel9cis_rule_1_1_3_4 %}nosuid{% endif %} with_items: - "{{ ansible_mounts }}" loop_control: diff --git a/tasks/section_1/cis_1.1.4.x.yml b/tasks/section_1/cis_1.1.4.x.yml index c780013..d05db6a 100644 --- a/tasks/section_1/cis_1.1.4.x.yml +++ b/tasks/section_1/cis_1.1.4.x.yml @@ -5,12 +5,19 @@ block: - name: "1.1.4.1 | AUDIT | Ensure separate partition exists for /var/tmp | Absent" debug: - msg: "Warning! {{ required_mount }} doesn't exist. This is a manual task" + msg: "Warning!! {{ required_mount }} doesn't exist. This is a manual task" register: var_tmp_mount_absent changed_when: var_tmp_mount_absent.skipped is undefined when: - required_mount not in mount_names + - name: "1.1.4.1 | AUDIT | Ensure separate partition exists for /var/tmp | Warn Count" + set_fact: + control_number: "{{ control_number }} + [ 'rule_1.1.4.1' ]" + warn_count: "{{ warn_count | int + 1 }}" + when: + - required_mount not in mount_names + - name: "1.1.4.1 | AUDIT | Ensure separate partition exists for /var/tmp | Present" debug: msg: "Congratulations: {{ required_mount }} exists." @@ -39,7 +46,7 @@ src: "{{ item.device }}" fstype: "{{ item.fstype }}" state: present - opts: defaults,{% if rhel9cis_rule_1_1_4_2 %}noexec,{% endif %}{% if rhel9cis_rule_1_1_4_3 %}nosuid,{% endif %}{% if rhel9cis_rule_1_1_4_4 %}nodev{% endif %} + opts: defaults,{% if rhel9cis_rule_1_1_4_2 %}noexec,{% endif %}{% if rhel9cis_rule_1_1_4_4 %}nodev,{% endif %}{% if rhel9cis_rule_1_1_4_3 %}nosuid{% endif %} with_items: - "{{ ansible_mounts }}" loop_control: diff --git a/tasks/section_1/cis_1.1.5.x.yml b/tasks/section_1/cis_1.1.5.x.yml index c9343c4..dd4ab9f 100644 --- a/tasks/section_1/cis_1.1.5.x.yml +++ b/tasks/section_1/cis_1.1.5.x.yml @@ -4,11 +4,19 @@ block: - name: "1.1.5.1 | AUDIT | Ensure separate partition exists for /var/log | Absent" debug: - msg: "Warning! {{ required_mount }} doesn't exist. This is a manual task" + msg: "Warning!! {{ required_mount }} doesn't exist. This is a manual task" register: var_log_mount_absent changed_when: var_log_mount_absent.skipped is undefined when: - required_mount not in mount_names + + - name: "1.1.5.1 | AUDIT | Ensure separate partition exists for /var/log | Warn Count" + set_fact: + control_number: "{{ control_number }} + [ 'rule_1.1.5.1' ]" + warn_count: "{{ warn_count | int + 1 }}" + when: + - required_mount not in mount_names + - name: "1.1.5.1 | AUDIT | Ensure separate partition exists for /var/log | Present" debug: msg: "Congratulations: {{ required_mount }} exists." @@ -37,7 +45,7 @@ src: "{{ item.device }}" fstype: "{{ item.fstype }}" state: present - opts: defaults,{% if rhel9cis_rule_1_1_5_2 %}nodev,{% endif %}{% if rhel9cis_rule_1_1_5_3 %}noexec,{% endif %}{% if rhel9cis_rule_1_1_5_4 %}nosuid{% endif %} + opts: defaults,{% if rhel9cis_rule_1_1_5_3 %}noexec,{% endif %}{% if rhel9cis_rule_1_1_5_2 %}nodev,{% endif %}{% if rhel9cis_rule_1_1_5_4 %}nosuid{% endif %} with_items: - "{{ ansible_mounts }}" loop_control: diff --git a/tasks/section_1/cis_1.1.6.x.yml b/tasks/section_1/cis_1.1.6.x.yml index 1df3e84..afbe41a 100644 --- a/tasks/section_1/cis_1.1.6.x.yml +++ b/tasks/section_1/cis_1.1.6.x.yml @@ -4,11 +4,19 @@ block: - name: "1.1.6.1 | AUDIT | Ensure separate partition exists for /var/log/audit | Absent" debug: - msg: "Warning! {{ required_mount }} doesn't exist. This is a manual task" + msg: "Warning!! {{ required_mount }} doesn't exist. This is a manual task" register: var_log_audit_mount_absent changed_when: var_log_audit_mount_absent.skipped is undefined when: - required_mount not in mount_names + + - name: "1.1.6.1 | AUDIT | Ensure separate partition exists for /var/log/audit | Warn Count" + set_fact: + control_number: "{{ control_number }} + [ 'rule_1.1.6.1' ]" + warn_count: "{{ warn_count | int + 1 }}" + when: + - required_mount not in mount_names + - name: "1.1.6.1 | AUDIT | Ensure separate partition exists for /var/log/audit | Present" debug: msg: "Congratulations: {{ required_mount }} exists." diff --git a/tasks/section_1/cis_1.1.7.x.yml b/tasks/section_1/cis_1.1.7.x.yml index 453fef5..59f28ba 100644 --- a/tasks/section_1/cis_1.1.7.x.yml +++ b/tasks/section_1/cis_1.1.7.x.yml @@ -4,11 +4,19 @@ block: - name: "1.1.7.1 | AUDIT | Ensure separate partition exists for /home | Absent" debug: - msg: "Warning! {{ required_mount }} doesn't exist. This is a manual task" + msg: "Warning!! {{ required_mount }} doesn't exist. This is a manual task" register: home_mount_absent changed_when: home_mount_absent.skipped is undefined when: - required_mount not in mount_names + + - name: "1.1.7.1 | AUDIT | Ensure separate partition exists for /home | Warn Count" + set_fact: + control_number: "{{ control_number }} + [ 'rule_1.1.7.1' ]" + warn_count: "{{ warn_count | int + 1 }}" + when: + - required_mount not in mount_names + - name: "1.1.7.1 | AUDIT | Ensure separate partition exists for /home | Present" debug: msg: "Congratulations: {{ required_mount }} exists." diff --git a/tasks/section_1/cis_1.1.8.x.yml b/tasks/section_1/cis_1.1.8.x.yml index 75bdabb..26ae877 100644 --- a/tasks/section_1/cis_1.1.8.x.yml +++ b/tasks/section_1/cis_1.1.8.x.yml @@ -13,7 +13,7 @@ shell: mount -l | grep -E '\s/dev/shm\s' changed_when: false failed_when: false - check_mode: no + check_mode: false register: rhel9cis_1_1_8_x_dev_shm_status - name: | @@ -25,7 +25,7 @@ src: tmpfs fstype: tmpfs state: mounted - opts: defaults,{% if rhel9cis_rule_1_1_8_1 %}nodev,{% endif %}{% if rhel9cis_rule_1_1_8_2 %}noexec,{% endif %}{% if rhel9cis_rule_1_1_8_3 %}nosuid{% endif %} + opts: defaults,{% if rhel9cis_rule_1_1_8_2 %}noexec,{% endif %}{% if rhel9cis_rule_1_1_8_1 %}nodev,{% endif %}{% if rhel9cis_rule_1_1_8_3 %}nosuid{% endif %} when: "'dev/shm' in rhel9cis_1_1_8_x_dev_shm_status.stdout" notify: change_requires_reboot when: diff --git a/tasks/section_1/cis_1.1.x.yml b/tasks/section_1/cis_1.1.x.yml index a77e524..ea5c862 100644 --- a/tasks/section_1/cis_1.1.x.yml +++ b/tasks/section_1/cis_1.1.x.yml @@ -3,7 +3,7 @@ - name: "1.1.9 | PATCH | Disable Automounting" service: name: autofs - enabled: no + enabled: false when: - not rhel9cis_allow_autofs - "'autofs' in ansible_facts.packages" @@ -24,7 +24,7 @@ path: /etc/modprobe.d/CIS.conf regexp: "^(#)?install usb-storage(\\s|$)" line: "install usb-storage /bin/true" - create: yes + create: true owner: root group: root mode: 0600 diff --git a/tasks/section_1/cis_1.2.x.yml b/tasks/section_1/cis_1.2.x.yml index 960815f..9445d15 100644 --- a/tasks/section_1/cis_1.2.x.yml +++ b/tasks/section_1/cis_1.2.x.yml @@ -20,12 +20,38 @@ - skip_ansible_lint # Added as no_log still errors on ansuible-lint - name: "1.2.2 | AUDIT | Ensure GPG keys are configured" - shell: "PKG=`rpm -qf {{ rpm_gpg_key }}` && rpm -q --queryformat \"%{PACKAGER} %{SIGPGP:pgpsig}\\n\" \"${PKG}\" | grep \"^{{ rpm_packager }}.*Key.ID.{{ rpm_key }}\"" - changed_when: false + block: + - name: "1.2.2 | AUDIT | Ensure GPG keys are configured | list installed pubkey keys" + shell: "rpm -qa | grep {{ os_gpg_key_pubkey_name }}" + changed_when: false + failed_when: false + register: os_installed_pub_keys + + - name: "1.2.2 | AUDIT | Ensure GPG keys are configured | Query found keys" + shell: "rpm -q --queryformat \"%{PACKAGER} %{VERSION}\\n\" {{ os_gpg_key_pubkey_name }} | grep \"{{ os_gpg_key_pubkey_content }}\"" + changed_when: false + failed_when: false + register: os_gpg_key_check + when: os_installed_pub_keys.rc == 0 + + - name: "1.2.2 | AUDIT | Ensure GPG keys are configured | expected keys pass" + debug: + msg: "Congratulations !! - The installed gpg keys match expected values" + when: + - os_installed_pub_keys.rc == 0 + - os_gpg_key_check.rc == 0 + + - name: "1.2.2 | AUDIT | Ensure GPG keys are configured | expected keys fail" + fail: + msg: Installed GPG Keys do not meet expected values or keys installed that are not expected + when: + - os_installed_pub_keys.rc == 1 or + os_gpg_key_check.rc == 1 when: - rhel9cis_rule_1_2_2 - ansible_distribution == "RedHat" or - ansible_distribution == "Rocky" + ansible_distribution == "Rocky" or + ansible_distribution == "AlmaLinux" tags: - level1-server - level1-workstation @@ -45,7 +71,7 @@ - name: "1.2.3 | PATCH | Ensure gpgcheck is globally activated | Update yum.repos" replace: name: "{{ item.path }}" - regexp: '^gpgcheck\s+=\s+0' + regexp: "^gpgcheck=0" replace: "gpgcheck=1" with_items: - "{{ yum_repos.files }}" @@ -67,15 +93,20 @@ changed_when: false failed_when: false register: dnf_configured - check_mode: no + check_mode: false args: warn: false - name: "1.2.4 | AUDIT | Ensure package manager repositories are configured | Display repo list" debug: msg: - - "Warning! Below are the configured repos. Please review and make sure all align with site policy" + - "Warning!! Below are the configured repos. Please review and make sure all align with site policy" - "{{ dnf_configured.stdout_lines }}" + + - name: "1.2.4 | AUDIT | Ensure package manager repositories are configured | Warn Count" + set_fact: + control_number: "{{ control_number }} + ['rule_1.2.4']" + warn_count: "{{ warn_count | int + 1 }}" when: - rhel9cis_rule_1_2_4 tags: diff --git a/tasks/section_1/cis_1.4.x.yml b/tasks/section_1/cis_1.4.x.yml index 6ac4979..8ba419e 100644 --- a/tasks/section_1/cis_1.4.x.yml +++ b/tasks/section_1/cis_1.4.x.yml @@ -58,7 +58,7 @@ path: /etc/systemd/system/rescue.service.d/00-require-auth.conf regexp: '^ExecStart=' line: "ExecStart=-/usr/lib/systemd/systemd-sulogin-shell rescue" - create: yes + create: true owner: root group: root mode: 0644 diff --git a/tasks/section_1/cis_1.5.x.yml b/tasks/section_1/cis_1.5.x.yml index 6573e51..031ba5c 100644 --- a/tasks/section_1/cis_1.5.x.yml +++ b/tasks/section_1/cis_1.5.x.yml @@ -32,10 +32,13 @@ - rule_1.5.2 - name: "1.5.3 | PATCH | Ensure address space layout randomization (ASLR) is enabled" - debug: - msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-kernel_sysctl.conf" - notify: - - update sysctl + block: + - name: "1.5.3 | PATCH | Ensure address space layout randomization (ASLR) is enabled" + set_fact: + sysctl_update: true + - name: "1.5.3 | PATCH | Ensure address space layout randomization (ASLR) is enabled" + debug: + msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-kernel_sysctl.conf" when: - rhel9cis_rule_1_5_3 tags: diff --git a/tasks/section_1/cis_1.6.1.x.yml b/tasks/section_1/cis_1.6.1.x.yml index f917a99..9a8d134 100644 --- a/tasks/section_1/cis_1.6.1.x.yml +++ b/tasks/section_1/cis_1.6.1.x.yml @@ -16,10 +16,10 @@ - name: "1.6.1.2 | PATCH | Ensure SELinux is not disabled in bootloader configuration" replace: dest: /etc/default/grub - regexp: '(selinux|enforcing)\s*=(\s0|0).*' + regexp: 'selinux=0' replace: '' register: selinux_grub_patch - ignore_errors: yes + ignore_errors: true # noqa ignore-errors notify: grub2cfg when: - rhel9cis_rule_1_6_1_2 @@ -78,7 +78,13 @@ - name: "1.6.1.5 | AUDIT | Ensure no unconfined services exist | Message on unconfined services" debug: - msg: "Warning! You have unconfined services: {{ rhelcis_1_6_1_5_unconf_services.stdout_lines }}" + msg: "Warning!! You have unconfined services: {{ rhelcis_1_6_1_5_unconf_services.stdout_lines }}" + when: rhelcis_1_6_1_5_unconf_services.stdout | length > 0 + + - name: "1.6.1.5 | AUDIT | Ensure no unconfined services exist | warning count" + set_fact: + control_number: "{{ control_number }} + [ 'rule_1.6.1.5' ]" + warn_count: "{{ warn_count | int + 1 }}" when: rhelcis_1_6_1_5_unconf_services.stdout | length > 0 when: - rhel9cis_rule_1_6_1_5 diff --git a/tasks/section_1/cis_1.8.x.yml b/tasks/section_1/cis_1.8.x.yml index a126a0a..f47d2a1 100644 --- a/tasks/section_1/cis_1.8.x.yml +++ b/tasks/section_1/cis_1.8.x.yml @@ -21,7 +21,7 @@ regexp: "{{ item.regexp }}" line: "{{ item.line }}" state: present - create: yes + create: true owner: root group: root mode: 0644 @@ -50,7 +50,7 @@ path: "{{ item.file }}" regexp: "{{ item.regexp }}" line: "{{ item.line }}" - create: yes + create: true owner: root group: root mode: 0644 @@ -93,7 +93,10 @@ path: /etc/dconf/db/local.d/00-media-automount regexp: "{{ item.regex }}" line: "{{ item.line }}" - create: yes + create: true + owner: root + group: root + mode: 0644 notify: reload dconf with_items: - { regex: '\[org\/gnome\/desktop\/media-handling\]', line: '[org/gnome/desktop/media-handling]' } diff --git a/tasks/section_2/cis_2.1.x.yml b/tasks/section_2/cis_2.1.x.yml index effe806..1db8179 100644 --- a/tasks/section_2/cis_2.1.x.yml +++ b/tasks/section_2/cis_2.1.x.yml @@ -29,7 +29,7 @@ path: /etc/sysconfig/chronyd regexp: "^(#)?OPTIONS" line: "OPTIONS=\"-u chrony\"" - create: yes + create: true mode: 0644 when: - rhel9cis_rule_2_1_2 diff --git a/tasks/section_2/cis_2.4.yml b/tasks/section_2/cis_2.4.yml index e17ab76..3373e54 100644 --- a/tasks/section_2/cis_2.4.yml +++ b/tasks/section_2/cis_2.4.yml @@ -6,15 +6,20 @@ shell: systemctl list-units --type=service changed_when: false failed_when: false - check_mode: no + check_mode: false register: rhel9cis_2_4_services - name: "2.4 | AUDIT | Ensure nonessential services are removed or masked | Display list of services" debug: msg: - - "Warning! Below are the list of services, both active and inactive" + - "Warning!! Below are the list of services, both active and inactive" - "Please review to make sure all are essential" - "{{ rhel9cis_2_4_services.stdout_lines }}" + + - name: "2.4 | AUDIT | Ensure nonessential services are removed or masked | Warn Count" + set_fact: + control_number: "{{ control_number }} + ['rule_2.4']" + warn_count: "{{ warn_count | int + 1 }}" when: - rhel9cis_rule_2_4 tags: diff --git a/tasks/section_3/cis_3.1.x.yml b/tasks/section_3/cis_3.1.x.yml index db3c0fd..6eaf58f 100644 --- a/tasks/section_3/cis_3.1.x.yml +++ b/tasks/section_3/cis_3.1.x.yml @@ -3,11 +3,15 @@ # The CIS Control wants IPv6 disabled if not in use. # We are using the rhel9cis_ipv6_required to specify if you have IPv6 in use - name: "3.1.1 | PATCH | Verify if IPv6 is enabled on the system" - debug: - msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-disable_ipv6.conf" - notify: - - update sysctl - - sysctl flush ipv6 route table + block: + - name: "3.1.1 | PATCH | Verify if IPv6 is enabled on the system" + set_fact: + sysctl_update: true + flush_ipv6_route: true + + - name: "3.1.1 | PATCH | Verify if IPv6 is enabled on the system" + debug: + msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-disable_ipv6.conf" when: - not rhel9cis_ipv6_required - rhel9cis_rule_3_1_1 @@ -64,9 +68,9 @@ command: rpm -q NetworkManager changed_when: false failed_when: false - check_mode: no + check_mode: false args: - warn: no + warn: false register: rhel_08_nmcli_available - name: "3.1.4 | AUDIT | Ensure wireless interfaces are disabled | Check if wifi is enabled" diff --git a/tasks/section_3/cis_3.2.x.yml b/tasks/section_3/cis_3.2.x.yml index 46295ec..6e07c55 100644 --- a/tasks/section_3/cis_3.2.x.yml +++ b/tasks/section_3/cis_3.2.x.yml @@ -2,19 +2,25 @@ - name: "3.2.1 | PATCH | Ensure IP forwarding is disabled" block: + - name: "3.2.1 | PATCH | Ensure IP forwarding is disabled | Disable IPv4 forwarding | Set Fact" + set_fact: + sysctl_update: true + flush_ipv4_route: true + - name: "3.2.1 | PATCH | Ensure IP forwarding is disabled | Disable IPv4 forwarding" debug: msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv4_sysctl.conf" - notify: - - update sysctl - - sysctl flush ipv4 route table - - name: "3.2.1 | PATCH | Ensure IP forwarding is disabled | Disable IPv6 forwarding" - debug: - msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv6_sysctl.conf" - notify: - - update sysctl - - sysctl flush ipv6 route table + - name: "3.2.1 | PATCH | Ensure IP forwarding is disabled | IPv6" + block: + - name: "3.2.1 | PATCH | Ensure IP forwarding is disabled | Disable IPv6 forwarding | Set Fact" + set_fact: + flush_ipv6_route: true + + - name: "3.2.1 | PATCH | Ensure IP forwarding is disabled | Disable IPv6 forwarding" + debug: + msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv6_sysctl.conf" + when: rhel9cis_ipv6_required when: - not rhel9cis_is_router @@ -28,11 +34,14 @@ - rule_3.2.1 - name: "3.2.2 | PATCH | Ensure packet redirect sending is disabled" - debug: - msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv4_sysctl.conf" - notify: - - update sysctl - - sysctl flush ipv4 route table + block: + - name: "3.2.2 | PATCH | Ensure packet redirect sending is disabled | Set Fact" + set_fact: + sysctl_update: true + flush_ipv4_route: true + - name: "3.2.2 | PATCH | Ensure packet redirect sending is disabled" + debug: + msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv4_sysctl.conf" when: - not rhel9cis_is_router - rhel9cis_rule_3_2_2 diff --git a/tasks/section_3/cis_3.3.x.yml b/tasks/section_3/cis_3.3.x.yml index 139ca65..b78593e 100644 --- a/tasks/section_3/cis_3.3.x.yml +++ b/tasks/section_3/cis_3.3.x.yml @@ -2,19 +2,23 @@ - name: "3.3.1 | PATCH | Ensure source routed packets are not accepted" block: - - name: "3.3.1 | PATCH | Ensure source routed packets are not accepted" + - name: "3.3.1 | PATCH | Ensure source routed packets are not accepted | IPv4 | Set Fact" + set_fact: + sysctl_update: true + flush_ipv4_route: true + - name: "3.3.1 | PATCH | Ensure source routed packets are not accepted | IPv4" debug: msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv4_sysctl.conf" - notify: - - update sysctl - - sysctl flush ipv4 route table - - name: "3.3.1 | PATCH | Ensure source routed packets are not accepted" - debug: - msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv6_sysctl.conf" - notify: - - sysctl flush ipv6 route table - - update sysctl + - name: "3.3.1 | PATCH | Ensure source routed packets are not accepted | IPv6" + block: + - name: "3.3.1 | PATCH | Ensure source routed packets are not accepted | IPv6 | Set Fact" + set_fact: + flush_ipv6_route: true + + - name: "3.3.1 | PATCH | Ensure source routed packets are not accepted | IPv6" + debug: + msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv6_sysctl.conf" when: rhel9cis_ipv6_required when: - rhel9cis_rule_3_3_1 @@ -27,19 +31,24 @@ - name: "3.3.2 | PATCH | Ensure ICMP redirects are not accepted" block: - - name: "3.3.2 | PATCH | Ensure ICMP redirects are not accepted" + - name: "3.3.2 | PATCH | Ensure ICMP redirects are not accepted | IPv4 | Set Fact" + set_fact: + sysctl_update: true + flush_ipv4_route: true + + - name: "3.3.2 | PATCH | Ensure ICMP redirects are not accepted | IPv4" debug: msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv4_sysctl.conf" - notify: - - update sysctl - - sysctl flush ipv4 route table - - name: "3.3.2 | PATCH | Ensure ICMP redirects are not accepted" - debug: - msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv6_sysctl.conf" - notify: - - sysctl flush ipv6 route table - - update sysctl + - name: "3.3.2 | PATCH | Ensure ICMP redirects are not accepted | IPv6" + block: + - name: "3.3.2 | PATCH | Ensure ICMP redirects are not accepted | IPv6 | Set Fact" + set_fact: + flush_ipv6_route: true + + - name: "3.3.2 | PATCH | Ensure ICMP redirects are not accepted | IPv6" + debug: + msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv6_sysctl.conf" when: rhel9cis_ipv6_required when: - rhel9cis_rule_3_3_2 @@ -51,9 +60,15 @@ - rule_3.3.2 - name: "3.3.3 | PATCH | Ensure secure ICMP redirects are not accepted" - debug: - msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv4_sysctl.conf" - notify: update sysctl + block: + - name: "3.3.3 | PATCH | Ensure secure ICMP redirects are not accepted | Set Fact" + set_fact: + sysctl_update: true + flush_ipv4_route: true + + - name: "3.3.3 | PATCH | Ensure secure ICMP redirects are not accepted" + debug: + msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv4_sysctl.conf" when: - rhel9cis_rule_3_3_3 tags: @@ -64,9 +79,15 @@ - rule_3.3.3 - name: "3.3.4 | PATCH | Ensure suspicious packets are logged" - debug: - msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv4_sysctl.conf" - notify: update sysctl + block: + - name: "3.3.4 | PATCH | Ensure suspicious packets are logged | Set Fact" + set_fact: + sysctl_update: true + flush_ipv4_route: true + + - name: "3.3.4 | PATCH | Ensure suspicious packets are logged" + debug: + msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv4_sysctl.conf" when: - rhel9cis_rule_3_3_4 tags: @@ -77,9 +98,15 @@ - rule_3.3.4 - name: "3.3.5 | PATCH | Ensure broadcast ICMP requests are ignored" - debug: - msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv4_sysctl.conf" - notify: update sysctl + block: + - name: "3.3.5 | PATCH | Ensure broadcast ICMP requests are ignored | Set Fact" + set_fact: + sysctl_update: true + flush_ipv4_route: true + + - name: 3.3.5 | PATCH | Ensure broadcast ICMP requests are ignored" + debug: + msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv4_sysctl.conf" when: - rhel9cis_rule_3_3_5 tags: @@ -90,9 +117,15 @@ - rule_3.3.5 - name: "3.3.6 | PATCH | Ensure bogus ICMP responses are ignored" - debug: - msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv4_sysctl.conf" - notify: update sysctl + block: + - name: "3.3.6 | PATCH | Ensure bogus ICMP responses are ignored | Set Fact" + set_fact: + sysctl_update: true + flush_ipv4_route: true + + - name: "3.3.6 | PATCH | Ensure bogus ICMP responses are ignored" + debug: + msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv4_sysctl.conf" when: - rhel9cis_rule_3_3_6 tags: @@ -103,9 +136,15 @@ - rule_3.3.6 - name: "3.3.7 | PATCH | Ensure Reverse Path Filtering is enabled" - debug: - msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv4_sysctl.conf" - notify: update sysctl + block: + - name: "3.3.7 | PATCH | Ensure Reverse Path Filtering is enabled | Set Fact" + set_fact: + sysctl_update: true + flush_ipv4_route: true + + - name: "3.3.7 | PATCH | Ensure Reverse Path Filtering is enabled" + debug: + msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv4_sysctl.conf" when: - rhel9cis_rule_3_3_7 tags: @@ -116,9 +155,15 @@ - rule_3.3.7 - name: "3.3.8 | PATCH | Ensure TCP SYN Cookies is enabled" - debug: - msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv4_sysctl.conf" - notify: update sysctl + block: + - name: "3.3.8 | PATCH | Ensure TCP SYN Cookies is enabled | Set Fact" + set_fact: + sysctl_update: true + flush_ipv4_route: true + + - name: "3.3.8 | PATCH | Ensure TCP SYN Cookies is enabled" + debug: + msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv4_sysctl.conf" when: - rhel9cis_rule_3_3_8 tags: @@ -130,20 +175,14 @@ - name: "3.3.9 | PATCH | Ensure IPv6 router advertisements are not accepted" block: - - name: "3.3.9 | PATCH | Ensure IPv6 router advertisements are not accepted" - debug: - msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv4_sysctl.conf" - notify: - - update sysctl - - sysctl flush ipv4 route table + - name: "3.3.9 | PATCH | Ensure IPv6 router advertisements are not accepted | IPv6 | Set Fact" + set_fact: + sysctl_update: true + flush_ipv6_route: true - - name: "3.3.9 | PATCH | Ensure IPv6 router advertisements are not accepted" + - name: "3.3.9 | PATCH | Ensure IPv6 router advertisements are not accepted | IPv6" debug: msg: "Control being set via Handler 'update sysctl' which writes to /etc/sysctl.d/60-netipv6_sysctl" - notify: - - sysctl flush ipv6 route table - - update sysctl - when: rhel9cis_ipv6_required when: - rhel9cis_ipv6_required - rhel9cis_rule_3_3_9 diff --git a/tasks/section_3/cis_3.4.1.x.yml b/tasks/section_3/cis_3.4.1.x.yml index caabdb5..d43dfe6 100644 --- a/tasks/section_3/cis_3.4.1.x.yml +++ b/tasks/section_3/cis_3.4.1.x.yml @@ -49,7 +49,7 @@ systemd: name: nftables state: stopped - masked: yes + masked: true when: - rhel9cis_firewalld_nftables_state == "masked" @@ -73,7 +73,7 @@ systemd: name: firewalld state: started - enabled: yes + enabled: true when: - rhel9cis_rule_3_4_1_4 tags: @@ -83,10 +83,20 @@ - patch - firewalld - rule_3_4_1_4 - - name: "3.4.1.5 | PATCH | Ensure firewalld default zone is set" - command: firewall-cmd --set-default-zone="{{ rhel9cis_default_zone }}" + block: + - name: "3.4.1.5 | AUDIT | Ensure firewalld default zone is set" + shell: "firewall-cmd --get-default-zone | grep {{ rhel9cis_default_zone }}" + changed_when: false + failed_when: ( firewalld_zone_set.rc not in [ 0, 1 ] ) + register: firewalld_zone_set + + - name: "3.4.1.5 | AUDIT | Ensure firewalld default zone is set" + command: firewall-cmd --set-default-zone="{{ rhel9cis_default_zone }}" + when: + - firewalld_zone_set.rc != 0 when: + - rhel9cis_firewall == "firewalld" - rhel9cis_rule_3_4_1_5 tags: - level1-server @@ -102,7 +112,7 @@ shell: "nmcli -t connection show | awk -F: '{ if($4){print $4} }' | while read INT; do firewall-cmd --get-active-zones | grep -B1 $INT; done" changed_when: false failed_when: false - check_mode: no + check_mode: false register: rhel9cis_3_4_1_6_interfacepolicy - name: "3.4.1.6 | AUDIT | Ensure network interfaces are assigned to appropriate zone | Get list of interfaces and polocies | Show the interface to policy" @@ -125,7 +135,7 @@ shell: "firewall-cmd --get-active-zones | awk '!/:/ {print $1}' | while read ZN; do firewall-cmd --list-all --zone=$ZN; done" changed_when: false failed_when: false - check_mode: no + check_mode: false register: rhel9cis_3_4_1_7_servicesport - name: "3.4.1.7 | AUDIT | Ensure firewalld drops unnecessary services and ports | Show services and ports" diff --git a/tasks/section_3/cis_3.4.2.x.yml b/tasks/section_3/cis_3.4.2.x.yml index a9284c5..ebb3631 100644 --- a/tasks/section_3/cis_3.4.2.x.yml +++ b/tasks/section_3/cis_3.4.2.x.yml @@ -5,6 +5,7 @@ name: nftables state: present when: + - rhel9cis_firewall == "nftables" - rhel9cis_rule_3_4_2_1 tags: - level1-server @@ -17,22 +18,11 @@ # The control allows the service it be masked or not installed # We have chosen not installed - name: "3.4.2.2 | PATCH | Ensure firewalld is either not installed or masked with nftables" - block: - - name: "3.4.2.2 | PATCH | Ensure firewalld is either not installed or masked with nftables | mask service" - systemd: - name: firewalld - masked: true - state: stopped - when: - - rhel9cis_nftables_firewalld_state == "masked" - - - name: "3.4.2.2 | PATCH | Ensure firewalld is either not installed or masked with nftables | pkg removed" - package: - name: firewalld - state: absent - when: - - rhel9cis_nftables_firewalld_state == "absent" + package: + name: firewalld + state: absent when: + - rhel9cis_firewall == "nftables" - rhel9cis_rule_3_4_2_2 tags: - level1-server @@ -49,7 +39,7 @@ name: "{{ item }}" enabled: false masked: true - ignore_errors: true + ignore_errors: true # noqa ignore-errors with_items: - iptables - ip6tables @@ -59,6 +49,7 @@ name: iptables-service state: absent when: + - rhel9cis_firewall == "nftables" - rhel9cis_rule_3_4_2_3 tags: - level1-server @@ -105,17 +96,26 @@ - name: "3.4.2.5 | AUDIT | Ensure an nftables table exists | Alert on no tables" debug: msg: - - "Warning! You currently have no nft tables, please review your setup" + - "Warning!! You currently have no nft tables, please review your setup" - 'Use the command "nft create table inet