-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy path3_5_firewall_configuration.rb
More file actions
182 lines (143 loc) · 7.67 KB
/
3_5_firewall_configuration.rb
File metadata and controls
182 lines (143 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# frozen_string_literal: true
#
# Copyright:: 2017, Schuberg Philis B.V.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# author: Kristian Vlaardingerbroek
title '3.5 Firewall Configuration'
ipv6 = command('test -f /proc/net/if_inet6').exit_status
control 'cis-dil-benchmark-3.5.1.1' do
title 'Ensure IPv6 default deny firewall policy'
desc "A default deny all policy on connections ensures that any unconfigured network usage will be rejected.\n\nRationale: With a default accept policy the firewall will accept any packet that is not configured to be denied. It is easier to white list acceptable usage than to black list unacceptable usage."
impact 1.0
tag cis: 'distribution-independent-linux:3.5.1.1'
tag level: 1
describe ip6tables do
it { should have_rule('-P INPUT DROP') }
it { should have_rule('-P OUTPUT DROP') }
it { should have_rule('-P FORWARD DROP') }
end
only_if { ipv6.zero? }
end
control 'cis-dil-benchmark-3.5.1.2' do
title 'Ensure IPv6 loopback traffic is configured'
desc "Configure the loopback interface to accept traffic. Configure all other interfaces to deny traffic to the loopback network (::1).\n\nRationale: Loopback traffic is generated between processes on machine and is typically critical to operation of the system. The loopback interface is the only place that loopback network (::1) traffic should be seen, all other interfaces should ignore traffic on this network as an anti-spoofing measure."
impact 1.0
tag cis: 'distribution-independent-linux:3.5.1.2'
tag level: 1
# Using match instead of have_rule to ensure tests work with iptables rule that have comments
# https://github.com/inspec/inspec/issues/3039
rules = ip6tables.retrieve_rules
describe "Check for IPv6 local loopback rules" do
subject { rules.join("\n") }
it { should match(/(?=.*-A INPUT)(?=.*-i lo)(?=.*-j ACCEPT)/) }
it { should match(/(?=.*-A OUTPUT)(?=.*-o lo)(?=.*-j ACCEPT)/) }
it { should match(/(?=.*-A INPUT)(?=.*-s ::1)(?=.*-j DROP)/) }
end
only_if { ipv6.zero? }
end
control 'cis-dil-benchmark-3.5.1.3' do
title 'Ensure IPv6 outbound and established connections are configured'
desc "Configure the firewall rules for new outbound, and established IPv6 connections.\n\nRationale: If rules are not in place for new outbound, and established connections all packets will be dropped by the default policy preventing network usage."
impact 0.0
tag cis: 'distribution-independent-linux:3.5.1.3'
tag level: 1
rules = ip6tables.retrieve_rules
%w(tcp udp icmp).each do |proto|
describe "Check for IPv6 rules on established and new outbound connections" do
subject { rules.join("\n") }
it { should match(/(?=.*-A OUTPUT)(?=.*-p #{proto})(?=.*-m state --state NEW,ESTABLISHED)(?=.*-j ACCEPT)/) }
it { should match(/(?=.*-A INPUT)(?=.*-p #{proto})(?=.*-m state --state ESTABLISHED)(?=.*-j ACCEPT)/) }
end
end
only_if { ipv6.zero? }
end
control 'cis-dil-benchmark-3.5.1.4' do
title 'Ensure IPv6 firewall rules exist for all open ports'
desc "Any ports that have been opened on non-loopback addresses need firewall rules to govern traffic.\n\nRationale: Without a firewall rule configured for open ports default firewall policy will drop all packets to these ports."
impact 0.0
tag cis: 'distribution-independent-linux:3.5.1.4'
tag level: 1
port.where { address !~ /^(127\.0\.0\.1|::1)$/ }.ports.each do |port|
describe "Firewall rule should exist for port #{port}" do
subject { ip6tables.retrieve_rules.any? { |s| s =~ /\s--(dport|dports) #{port}\s/ } }
it { should be true }
end
end
only_if { ipv6.zero? }
end
control 'cis-dil-benchmark-3.5.2.1' do
title 'Ensure default deny firewall policy'
desc "A default deny all policy on connections ensures that any unconfigured network usage will be rejected.\n\nRationale: With a default accept policy the firewall will accept any packet that is not configured to be denied. It is easier to white list acceptable usage than to black list unacceptable usage."
impact 1.0
tag cis: 'distribution-independent-linux:3.5.2.1'
tag level: 1
describe iptables do
it { should have_rule('-P INPUT DROP') }
it { should have_rule('-P OUTPUT DROP') }
it { should have_rule('-P FORWARD DROP') }
end
end
control 'cis-dil-benchmark-3.5.2.2' do
title 'Ensure loopback traffic is configured'
desc "Configure the loopback interface to accept traffic. Configure all other interfaces to deny traffic to the loopback network (127.0.0.0/8).\n\nRationale: Loopback traffic is generated between processes on machine and is typically critical to operation of the system. The loopback interface is the only place that loopback network (127.0.0.0/8) traffic should be seen, all other interfaces should ignore traffic on this network as an anti-spoofing measure."
impact 1.0
tag cis: 'distribution-independent-linux:3.5.2.2'
tag level: 1
rules = iptables.retrieve_rules
describe "Check for local loopback rules" do
subject { rules.join("\n") }
it { should match /(?=.*-A INPUT)(?=.*-i lo)(?=.*-j ACCEPT)/ }
it { should match /(?=.*-A OUTPUT)(?=.*-o lo)(?=.*-j ACCEPT)/ }
it { should match %r{(?=.*-A INPUT)(?=.*-s 127\.0\.0\.0/8)(?=.*-j DROP)} }
end
end
control 'cis-dil-benchmark-3.5.2.3' do
title 'Ensure outbound and established connections are configured'
desc "Configure the firewall rules for new outbound, and established connections.\n\nRationale: If rules are not in place for new outbound, and established connections all packets will be dropped by the default policy preventing network usage."
impact 0.0
tag cis: 'distribution-independent-linux:3.5.2.3'
tag level: 1
rules = iptables.retrieve_rules
%w(tcp udp icmp).each do |proto|
describe "Check for rules on established and new outbound connections" do
subject { rules.join("\n") }
it { should match /(?=.*-A OUTPUT)(?=.*-p #{proto})(?=.*-m state --state NEW,ESTABLISHED)(?=.*-j ACCEPT)/ }
it { should match /(?=.*-A INPUT)(?=.*-p #{proto})(?=.*-m state --state ESTABLISHED)(?=.*-j ACCEPT)/ }
end
end
end
control 'cis-dil-benchmark-3.5.2.4' do
title 'Ensure firewall rules exist for all open ports'
desc "Any ports that have been opened on non-loopback addresses need firewall rules to govern traffic.\n\nRationale: Without a firewall rule configured for open ports default firewall policy will drop all packets to these ports."
impact 1.0
tag cis: 'distribution-independent-linux:3.5.2.4'
tag level: 1
port.where { address !~ /^(127\.0\.0\.1|::1)$/ }.ports.each do |port|
describe "Firewall rule should exist for port #{port}" do
subject { iptables.retrieve_rules.any? { |s| s =~ /\s--(dport|dports) #{port}\s/ } }
it { should be true }
end
end
end
control 'cis-dil-benchmark-3.5.3' do
title 'Ensure iptables is installed'
desc "iptables allows configuration of the IPv4 tables in the linux kernel and the rules stored within them. Most firewall configuration utilities operate as a front end to iptables.\n\nRationale: iptables is required for firewall management and configuration."
impact 1.0
tag cis: 'distribution-independent-linux:3.5.3'
tag level: 1
describe package('iptables') do
it { should be_installed }
end
end