-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcheck_iptables.sh
236 lines (210 loc) · 5.75 KB
/
check_iptables.sh
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/bin/bash
#
#
# Author MrCrankHank
#
# Important bins
IPT=`which iptables`
IPT6=`which ip6tables`
IPSET=`which ipset`
SUDO=`which sudo`
if [ -z $1 ]; then
echo " Usage:"
echo " The nagios user needs the permission to list iptables and ipset rules. This is accomplished via sudo. Place this line inside your /etc/sudoers:"
echo " nagios ALL=NOPASSWD: /sbin/iptables -L -n, /sbin/iptables -t nat -L -n, /sbin/iptables -t mangle -L -n, /usr/sbin/ipset list *"
echo
echo
echo " IPTables:"
echo " ./check_iptables iptables <table> <custom chains> <emptyok>"
echo
echo " Possible values for <table> are 'filter', 'nat', 'mangle'"
echo
echo " <custom chains> is the count of chains created in a table. They are added with iptables -N <chain>. The script will produce wrong output, if you specify a wrong count."
echo " For no custom chains use '0'"
echo
echo " If there is a table that is normally empty, you have to specify the <emptyok> parameter. Otherwise the script will trigger a critical alert."
echo
echo " Example:"
echo " ./check_iptables iptables filter 0"
echo
echo " Output:"
echo " OK - 20 rules in chain filter"
echo
echo " Example:"
echo " ./check_iptables iptables filter 0 emptyok"
echo
echo " Output:"
echo " Critical - 20 rules in chain filter"
echo
echo " IP6Tables:"
echo " Works exactly like IPTables"
echo
echo " IPSet:"
echo " ./check_iptables ipset <ipset>"
echo
echo " <ipset> is the name of the ipset you want to check. The script will trigger a critical alert if the set is empty"
echo
echo " Example:"
echo " ./check_iptables ipset blacklist"
echo
echo " Output:"
echo " OK - 40563 ips in ipset blacklist"
echo
echo " Example:"
echo " ./check_iptables ipset blacklist"
echo
echo " Output:"
echo " Critical - No ips in ipset blacklist"
fi
case $1 in
iptables)
case $2 in
filter)
COUNT_FILTER=$($SUDO $IPT -L -n | wc -l)
IPT_FILTER_EMPTY=8
if ! [ -z $3 ]; then
((IPT_FILTER_EMPTY=${3}*3+$IPT_FILTER_EMPTY))
fi
((RULES_FILTER=${COUNT_FILTER}-${IPT_FILTER_EMPTY}))
if ! [ -z $4 ]; then
if [ $4 == "emptyok" ]; then
if [ $COUNT_FILTER -le $IPT_FILTER_EMPTY ]; then
echo "OK - No rules in chain filter"
exit 0
else
echo "Critical - $RULES_FILTER rules in chain filter"
exit 2
fi
fi
fi
if [ $COUNT_FILTER -le $IPT_FILTER_EMPTY ]; then
echo "Critical - No rules in chain filter"
exit 2
else
echo "OK - $RULES_FILTER rules in chain filter"
exit 0
fi
;;
nat)
COUNT_NAT=$($SUDO $IPT -t nat -L -n | wc -l)
IPT_NAT_EMPTY=11
if ! [ -z $3 ]; then
((IPT_NAT_EMPTY=${3}*3+$IPT_NAT_EMPTY))
fi
((RULES_NAT=${COUNT_NAT}-${IPT_NAT_EMPTY}))
if ! [ -z $4 ]; then
if [ $4 == "emptyok" ]; then
if [ $COUNT_NAT -le $IPT_NAT_EMPTY ]; then
echo "OK - No rules in chain nat"
exit 0
else
echo "Critical - $RULES_NAT rules in chain nat"
exit 2
fi
fi
fi
if [ $COUNT_NAT -le $IPT_NAT_EMPTY ]; then
echo "Critical - No rules in chain nat"
exit 2
else
echo "OK - $RULES_NAT rules in chain nat"
exit 0
fi
;;
mangle)
COUNT_MANGLE=$($SUDO $IPT -t mangle -L -n | wc -l)
IPT_MANGLE_EMPTY=14
if ! [ -z $3 ]; then
((IPT_MANGLE_EMPTY=${3}*3+$IPT_MANGLE_EMPTY))
fi
((RULES_MANGLE=${COUNT_MANGLE}-${IPT_MANGLE_EMPTY}))
if [ -z $4 ]; then
if [ $4 == "emptyok" ]; then
if [ $COUNT_MANGLE -le $IPT_MANGLE_EMPTY ]; then
echo "OK - No rules in chain mangle"
exit 0
else
echo "Critical - $RULES_MANGLE rules in chain mangle"
exit 2
fi
fi
fi
if [ $COUNT_MANGLE -le $IPT_MANGLE_EMPTY ]; then
echo "Critical - No rules in chain mangle"
exit 2
else
echo "OK - $RULES_MANGLE rules in chain mangle"
exit 0
fi
;;
esac
;;
ip6tables)
case $2 in
filter)
COUNT_FILTER=$($SUDO $IPT6 -L -n | wc -l)
IPT_FILTER_EMPTY=8
if ! [ -z $3 ]; then
((IPT_FILTER_EMPTY=${3}*3+$IPT_FILTER_EMPTY))
fi
((RULES_FILTER=${COUNT_FILTER}-${IPT_FILTER_EMPTY}))
if ! [ -z $4 ]; then
if [ $4 == "emptyok" ]; then
if [ $COUNT_FILTER -le $IPT_FILTER_EMPTY ]; then
echo "OK - No rules in chain filter"
exit 0
else
echo "Critical - $RULES_FILTER rules in chain filter"
exit 2
fi
fi
fi
if [ $COUNT_FILTER -le $IPT_FILTER_EMPTY ]; then
echo "Critical - No rules in chain filter"
exit 2
else
echo "OK - $RULES_FILTER rules in chain filter"
exit 0
fi
;;
mangle)
COUNT_MANGLE=$($SUDO $IPT6 -t mangle -L -n | wc -l)
IPT_MANGLE_EMPTY=14
if ! [ -z $3 ]; then
((IPT_MANGLE_EMPTY=${3}*3+$IPT_MANGLE_EMPTY))
fi
((RULES_MANGLE=${COUNT_MANGLE}-${IPT_MANGLE_EMPTY}))
if [ -z $4 ]; then
if [ $4 == "emptyok" ]; then
if [ $COUNT_MANGLE -le $IPT_MANGLE_EMPTY ]; then
echo "OK - No rules in chain mangle"
exit 0
else
echo "Critical - $RULES_MANGLE rules in chain mangle"
exit 2
fi
fi
fi
if [ $COUNT_MANGLE -le $IPT_MANGLE_EMPTY ]; then
echo "Critical - No rules in chain mangle"
exit 2
else
echo "OK - $RULES_MANGLE rules in chain mangle"
exit 0
fi
;;
esac
;;
ipset)
LIST=$2
IPSET_EMPTY=6
COUNT_IPSET=$($SUDO $IPSET list $LIST | wc -l)
if [ $COUNT_IPSET -le $IPSET_EMPTY ]; then
echo "Critical - No ips in ipset $LIST"
exit 2
else
echo "OK - $COUNT_IPSET ips in ipset $LIST"
exit 0
fi
;;
esac