Skip to content

Commit 6d21d24

Browse files
authored
Merge pull request #120 from dev-sec/combined_sysctl
add more sysctl settings, allow overwriting
2 parents 7bbd479 + 0b8e42c commit 6d21d24

File tree

11 files changed

+128
-81
lines changed

11 files changed

+128
-81
lines changed

.kitchen.vagrant.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ transport:
2424
max_ssh_sessions: 5
2525

2626
platforms:
27-
- name: ubuntu-12.04
28-
driver_config:
29-
box: opscode-ubuntu-12.04
30-
box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_ubuntu-12.04_chef-provisionerless.box
3127
- name: ubuntu-14.04
3228
driver_config:
3329
box: opscode-ubuntu-14.04

.kitchen.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ provisioner:
2222
http_proxy: <%= ENV['http_proxy'] || nil %>
2323
https_proxy: <%= ENV['https_proxy'] || nil %>
2424
playbook: default.yml
25-
ansible_extra_flags:
26-
- "--skip-tags=sysctl"
2725

2826
platforms:
2927
- name: centos6-ansible-latest
@@ -42,10 +40,6 @@ platforms:
4240
driver:
4341
image: rndmh3ro/docker-oracle7-ansible:latest
4442
platform: centos
45-
- name: ubuntu1204-ansible-latest
46-
driver:
47-
image: rndmh3ro/docker-ubuntu1204-ansible:latest
48-
platform: ubuntu
4943
- name: ubuntu1404-ansible-latest
5044
driver:
5145
image: rndmh3ro/docker-ubuntu1404-ansible:latest

.travis.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ env:
2929
version: latest
3030
init: /sbin/init
3131

32-
- distro: ubuntu1204
33-
version: latest
34-
init: /sbin/init
35-
3632
- distro: debian7
3733
version: latest
3834
init: /sbin/init
@@ -55,10 +51,10 @@ script:
5551
# Run container in detached state.
5652
- 'docker run --detach --volume="${PWD}":/etc/ansible/roles/ansible-os-hardening:ro ${run_opts} rndmh3ro/docker-${distro}-ansible:${version} "${init}" > "${container_id}"'
5753

58-
# Test role.
54+
# Test role.
5955
- 'docker exec "$(cat ${container_id})" ansible-playbook /etc/ansible/roles/ansible-os-hardening/default.yml --skip-tags "sysctl"'
6056

61-
# Verify role
57+
# Verify role
6258
- 'inspec exec https://github.com/dev-sec/linux-baseline/ -t docker://$(cat ${container_id}) --controls=os-01 os-02 os-03 os-04 os-05 os-06 os-07 package-01 package-02 package-03 package-04 package-05 package-06'
6359

6460
notifications:

README.md

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -78,59 +78,17 @@ We remove the following packages:
7878

7979

8080
## Changing sysctl variables
81-
82-
If you want to overwrite sysctl-variables, you have to overwrite the *whole* dict, or else only the single overwritten will be actually used.
83-
So for example if you want to change the IPv4 traffic forwarding variable to `1`, you must pass the whole dict like this:
81+
+If you want to overwrite sysctl-variables, you can use the `sysctl_overwrite` variable (in older versions you had to overwrite the whole `sysctl_dict`).
82+
+So for example if you want to change the IPv4 traffic forwarding variable to `1`, do it like this:
8483

8584
```
8685
- hosts: localhost
8786
roles:
8887
- dev-sec.os-hardening
8988
vars:
90-
sysctl_config:
89+
sysctl_overwrite:
9190
# Disable IPv4 traffic forwarding.
9291
net.ipv4.ip_forward: 1
93-
94-
# Disable IPv6 traffic forwarding.
95-
net.ipv6.conf.all.forwarding: 0
96-
97-
# ignore RAs on Ipv6.
98-
net.ipv6.conf.all.accept_ra: 0
99-
net.ipv6.conf.default.accept_ra: 0
100-
101-
# Enable RFC-recommended source validation feature.
102-
net.ipv4.conf.all.rp_filter: 1
103-
net.ipv4.conf.default.rp_filter: 1
104-
105-
# Reduce the surface on SMURF attacks.
106-
# Make sure to ignore ECHO broadcasts, which are only required in broad network analysis.
107-
net.ipv4.icmp_echo_ignore_broadcasts: 1
108-
109-
# There is no reason to accept bogus error responses from ICMP, so ignore them instead.
110-
net.ipv4.icmp_ignore_bogus_error_responses: 1
111-
112-
# Limit the amount of traffic the system uses for ICMP.
113-
net.ipv4.icmp_ratelimit: 100
114-
115-
# Adjust the ICMP ratelimit to include ping, dst unreachable,
116-
# source quench, ime exceed, param problem, timestamp reply, information reply
117-
net.ipv4.icmp_ratemask: 88089
118-
119-
# Disable IPv6
120-
net.ipv6.conf.all.disable_ipv6: 1
121-
122-
# Protect against wrapping sequence numbers at gigabit speeds
123-
net.ipv4.tcp_timestamps: 0
124-
125-
# Define restriction level for announcing the local source IP
126-
net.ipv4.conf.all.arp_ignore: 1
127-
128-
# Define mode for sending replies in response to
129-
# received ARP requests that resolve local target IP addresses
130-
net.ipv4.conf.all.arp_announce: 2
131-
132-
# RFC 1337 fix F1
133-
net.ipv4.tcp_rfc1337: 1
13492
```
13593

13694
Alternatively you can change Ansible's [hash-behaviour](https://docs.ansible.com/ansible/intro_configuration.html#hash-behaviour) to `merge`, then you only have to overwrite the single hash you need to. But please be aware that changing the hash-behaviour changes it for all your playbooks and is not recommended by Ansible.
@@ -152,27 +110,27 @@ bundle install
152110
### Testing with Docker
153111
```
154112
# fast test on one machine
155-
bundle exec kitchen test default-ubuntu-1204
113+
bundle exec kitchen test default-ubuntu-1404
156114
157115
# test on all machines
158116
bundle exec kitchen test
159117
160118
# for development
161-
bundle exec kitchen create default-ubuntu-1204
162-
bundle exec kitchen converge default-ubuntu-1204
119+
bundle exec kitchen create default-ubuntu-1404
120+
bundle exec kitchen converge default-ubuntu-1404
163121
```
164122

165123
### Testing with Virtualbox
166124
```
167125
# fast test on one machine
168-
KITCHEN_YAML=".kitchen.vagrant.yml" bundle exec kitchen test default-ubuntu-1204
126+
KITCHEN_YAML=".kitchen.vagrant.yml" bundle exec kitchen test default-ubuntu-1404
169127
170128
# test on all machines
171129
KITCHEN_YAML=".kitchen.vagrant.yml" bundle exec kitchen test
172130
173131
# for development
174-
KITCHEN_YAML=".kitchen.vagrant.yml" bundle exec kitchen create default-ubuntu-1204
175-
KITCHEN_YAML=".kitchen.vagrant.yml" bundle exec kitchen converge default-ubuntu-1204
132+
KITCHEN_YAML=".kitchen.vagrant.yml" bundle exec kitchen create default-ubuntu-1404
133+
KITCHEN_YAML=".kitchen.vagrant.yml" bundle exec kitchen converge default-ubuntu-1404
176134
```
177135
For more information see [test-kitchen](http://kitchen.ci/docs/getting-started)
178136

default.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,47 @@
1313
os_auth_allow_homeless: true
1414
os_security_suid_sgid_blacklist: ['/bin/umount']
1515
os_security_suid_sgid_whitelist: ['/usr/bin/rlogin']
16+
sysctl_config:
17+
net.ipv4.ip_forward: 0
18+
net.ipv6.conf.all.forwarding: 0
19+
net.ipv6.conf.all.accept_ra: 0
20+
net.ipv6.conf.default.accept_ra: 0
21+
net.ipv4.conf.all.rp_filter: 1
22+
net.ipv4.conf.default.rp_filter: 1
23+
net.ipv4.icmp_echo_ignore_broadcasts: 1
24+
net.ipv4.icmp_ignore_bogus_error_responses: 1
25+
net.ipv4.icmp_ratelimit: 100
26+
net.ipv4.icmp_ratemask: 88089
27+
net.ipv6.conf.all.disable_ipv6: 1
28+
net.ipv4.conf.all.arp_ignore: 1
29+
net.ipv4.conf.all.arp_announce: 2
30+
net.ipv4.conf.all.shared_media: 1
31+
net.ipv4.conf.default.shared_media: 1
32+
net.ipv4.conf.all.accept_source_route: 0
33+
net.ipv4.conf.default.accept_source_route: 0
34+
net.ipv4.conf.default.accept_redirects: 0
35+
net.ipv4.conf.all.accept_redirects: 0
36+
net.ipv4.conf.all.secure_redirects: 0
37+
net.ipv4.conf.default.secure_redirects: 0
38+
net.ipv6.conf.default.accept_redirects: 0
39+
net.ipv6.conf.all.accept_redirects: 0
40+
net.ipv4.conf.all.send_redirects: 0
41+
net.ipv4.conf.default.send_redirects: 0
42+
net.ipv4.conf.all.log_martians: 1
43+
net.ipv6.conf.default.router_solicitations: 0
44+
net.ipv6.conf.default.accept_ra_rtr_pref: 0
45+
net.ipv6.conf.default.accept_ra_pinfo: 0
46+
net.ipv6.conf.default.accept_ra_defrtr: 0
47+
net.ipv6.conf.default.autoconf: 0
48+
net.ipv6.conf.default.dad_transmits: 0
49+
net.ipv6.conf.default.max_addresses: 1
50+
kernel.sysrq: 0
51+
fs.suid_dumpable: 0
52+
kernel.randomize_va_space: 2
53+
1654

1755
- name: wrapper playbook for kitchen testing "ansible-os-hardening"
1856
hosts: localhost
1957
roles:
2058
- ansible-os-hardening
59+

defaults/main.yml

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ ufw_default_application_policy: 'SKIP'
5858
ufw_manage_builtins: 'no'
5959
ufw_ipt_modules: 'nf_conntrack_ftp nf_nat_ftp nf_conntrack_netbios_ns'
6060

61-
# CAUTION
62-
# If you want to overwrite sysctl-variables,
63-
# you have to overwrite the *whole* dict, or else only the single overwritten will be actually used.
64-
6561
sysctl_config:
6662
# Disable IPv4 traffic forwarding.
6763
net.ipv4.ip_forward: 0
@@ -107,9 +103,73 @@ sysctl_config:
107103
# RFC 1337 fix F1
108104
net.ipv4.tcp_rfc1337: 1
109105

110-
# CAUTION
111-
# If you want to overwrite sysctl-variables,
112-
# you have to overwrite the *whole* dict, or else only the single overwritten will be actually used.
106+
# Send(router) or accept(host) RFC1620 shared media redirects
107+
net.ipv4.conf.all.shared_media: 1
108+
net.ipv4.conf.default.shared_media: 1
109+
110+
# Accepting source route can lead to malicious networking behavior,
111+
# so disable it if not needed.
112+
net.ipv4.conf.all.accept_source_route: 0
113+
net.ipv4.conf.default.accept_source_route: 0
114+
115+
# Accepting redirects can lead to malicious networking behavior, so disable
116+
# it if not needed.
117+
net.ipv4.conf.default.accept_redirects: 0
118+
net.ipv4.conf.all.accept_redirects: 0
119+
net.ipv4.conf.all.secure_redirects: 0
120+
net.ipv4.conf.default.secure_redirects: 0
121+
net.ipv6.conf.default.accept_redirects: 0
122+
net.ipv6.conf.all.accept_redirects: 0
123+
124+
# For non-routers: don't send redirects, these settings are 0
125+
net.ipv4.conf.all.send_redirects: 0
126+
net.ipv4.conf.default.send_redirects: 0
127+
128+
# log martian packets
129+
net.ipv4.conf.all.log_martians: 1
130+
131+
# ipv6 config
132+
# NSA 2.5.3.2.5 Limit Network-Transmitted Configuration
133+
net.ipv6.conf.default.router_solicitations: 0
134+
net.ipv6.conf.default.accept_ra_rtr_pref: 0
135+
net.ipv6.conf.default.accept_ra_pinfo: 0
136+
net.ipv6.conf.default.accept_ra_defrtr: 0
137+
net.ipv6.conf.default.autoconf: 0
138+
net.ipv6.conf.default.dad_transmits: 0
139+
net.ipv6.conf.default.max_addresses: 1
140+
141+
# This settings controls how the kernel behaves towards module changes at
142+
# runtime. Setting to 1 will disable module loading at runtime.
143+
# Setting it to 0 is actually never supported.
144+
# kernel.modules_disabled: 1
145+
146+
# Magic Sysrq should be disabled, but can also be set to a safe value if so
147+
# desired for physical machines. It can allow a safe reboot if the system hangs
148+
# and is a 'cleaner' alternative to hitting the reset button.
149+
# The following values are permitted:
150+
# * **0** - disable sysrq
151+
# * **1** - enable sysrq completely
152+
# * **>1** - bitmask of enabled sysrq functions:
153+
# * **2** - control of console logging level
154+
# * **4** - control of keyboard (SAK, unraw)
155+
# * **8** - debugging dumps of processes etc.
156+
# * **16** - sync command
157+
# * **32** - remount read-only
158+
# * **64** - signalling of processes (term, kill, oom-kill)
159+
# * **128** - reboot/poweroff
160+
# * **256** - nicing of all RT tasks
161+
kernel.sysrq: 0
162+
163+
# Prevent core dumps with SUID. These are usually only
164+
# needed by developers and may contain sensitive information.
165+
fs.suid_dumpable: 0
166+
167+
# Virtual memory regions protection
168+
kernel.randomize_va_space: 2
169+
170+
# Do not delete the following line or otherwise the playbook will fail
171+
# at task "create a combined sysctl-dict if overwrites are defined"
172+
sysctl_overwrite:
113173

114174
sysctl_rhel_config:
115175
# ExecShield protection against buffer overflows

tasks/rhosts.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
- name: delete rhosts-files from system | DTAG SEC Req 3.21-4
99
file: dest='~{{ item }}/.rhosts' state=absent
10-
with_items: '{{ users.stdout_lines | default(omit) }}'
10+
with_flattened: '{{ users.stdout_lines | default([]) }}'
1111

1212
- name: delete hosts.equiv from system | DTAG SEC Req 3.21-4
1313
file: dest='/etc/hosts.equiv' state=absent

tasks/suid_sgid.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
- name: remove suid/sgid bit from binaries in blacklist
33
file: path='{{item}}' mode='a-s' state=file follow=yes
44
failed_when: false
5-
with_items:
5+
with_flattened:
66
- '{{ os_security_suid_sgid_system_blacklist }}'
77
- '{{ os_security_suid_sgid_blacklist }}'
88

@@ -19,6 +19,6 @@
1919

2020
- name: remove suid/sgid bit from all binaries except in system and user whitelist
2121
file: path='{{item}}' mode='a-s' state=file follow=yes
22-
with_items:
23-
- '{{ suid | default(omit) | difference(os_security_suid_sgid_whitelist) }}'
22+
with_flattened:
23+
- '{{ suid | default([]) | difference(os_security_suid_sgid_whitelist) }}'
2424
when: os_security_suid_sgid_remove_from_unknown

tasks/sysctl.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
command: 'update-initramfs -u'
2020
when: initramfs.changed
2121

22+
- name: create a combined sysctl-dict if overwrites are defined
23+
set_fact: sysctl_config="{{ sysctl_config | combine(sysctl_overwrite) }}"
24+
when: sysctl_overwrite | default()
25+
2226
- name: Change various sysctl-settings, look at the sysctl-vars file for documentation
2327
sysctl:
2428
name: '{{ item.key }}'

tasks/user_accounts.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131

3232
- name: change system accounts not on the user provided ignore-list
3333
user: name='{{item}}' shell='{{os_nologin_shell_path}}' password='*'
34-
with_items:
35-
- '{{sys_accs_cond | default(omit) | difference(os_ignore_users) | list }}'
34+
with_flattened:
35+
- '{{sys_accs_cond | default([]) | difference(os_ignore_users) | list }}'

0 commit comments

Comments
 (0)