-
Notifications
You must be signed in to change notification settings - Fork 0
/
gnome-security-rules-count.sh
executable file
·210 lines (172 loc) · 6.21 KB
/
gnome-security-rules-count.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
#!/bin/bash
## Author : Quach Chi Cuong
## Last updated : 09/2016
## Description :
## - iptables controls five different tables: filter, nat, mangle, raw and security.
## - Shell script will show how many active iptables rule-lines on Linux server which use netfilter core-firewall.
##
### Variable Settings ###
TMP_FILE="/tmp/iptables-rule-count.tmp.txt"
#################################
####### Function progress #######
## Count number of chains in table filter
cal_rule_table_filter()
{
echo ""
echo "+++++++++++++++++++++++++++++++++++++++"
echo "+ Table 'filter' of gnome-dn firewall +"
echo "+++++++++++++++++++++++++++++++++++++++"
echo ""
iptables -nvL -t filter | grep "^Chain" | awk '{print $2}' 2> /dev/null 1>> ${TMP_FILE}.filter-chains
SUM_RULE_FILTER=0
while read CHAIN
do
NUM_IPT_FILTER_CHAIN=$(iptables -nvL ${CHAIN} -t filter | grep -Ev "^Chain|^pkts|^\ pkts" | wc -l)
echo "-- Chain ${CHAIN} : ${NUM_IPT_FILTER_CHAIN} (rules)"
SUM_RULE_FILTER=$((SUM_RULE_FILTER+${NUM_IPT_FILTER_CHAIN}))
done < ${TMP_FILE}.filter-chains
#echo "---| Sum of amount CHAINS in table 'filter' : $(cat ${TMP_FILE}.filter-chains | wc -l) (chains)"
filtered_value=$((SUM_RULE_FILTER-3))
echo "---| Sum of amount our ATTACKER IPs this far: ${filtered_value} (rules)"
echo ""
}
## Count number of chains in table nat
cal_rule_table_nat()
{
echo ""
echo "++++++++++++++++++++++++++++++++++++"
echo "+ Table 'nat' of gnome-dn firewall +"
echo "++++++++++++++++++++++++++++++++++++"
echo ""
iptables -nvL -t nat | grep "^Chain" | awk '{print $2}' 2> /dev/null 1>> ${TMP_FILE}.nat-chains
SUM_RULE_NAT=0
while read CHAIN
do
NUM_IPT_NAT_CHAIN=$(iptables -nvL ${CHAIN} -t nat | grep -Ev "^Chain|^pkts|^\ pkts" | wc -l)
echo "-- Chain ${CHAIN} : ${NUM_IPT_NAT_CHAIN} (rules)"
SUM_RULE_NAT=$((SUM_RULE_NAT+${NUM_IPT_NAT_CHAIN}))
done < ${TMP_FILE}.nat-chains
echo "---| Sum of amount CHAINS in table 'nat' : $(cat ${TMP_FILE}.nat-chains | wc -l) (chains)"
echo "---| Sum of amount RULES in table 'nat' : ${SUM_RULE_NAT} (rules)"
echo ""
}
## Count number of chains in table mangle
cal_rule_table_mangle()
{
echo ""
echo "+++++++++++++++++++++++++++++++++++++++"
echo "+ Table 'mangle' of gnome-dn firewall +"
echo "+++++++++++++++++++++++++++++++++++++++"
echo ""
iptables -nvL -t mangle | grep "^Chain" | awk '{print $2}' 2> /dev/null 1>> ${TMP_FILE}.mangle-chains
SUM_RULE_MANGLE=0
while read CHAIN
do
NUM_IPT_MANGLE_CHAIN=$(iptables -nvL ${CHAIN} -t mangle | grep -Ev "^Chain|^pkts|^\ pkts" | wc -l)
echo "-- Chain ${CHAIN} : ${NUM_IPT_MANGLE_CHAIN} (rules)"
SUM_RULE_MANGLE=$((SUM_RULE_MANGLE+${NUM_IPT_MANGLE_CHAIN}))
done < ${TMP_FILE}.mangle-chains
echo "---| Sum of amount CHAINS in table 'mangle' : $(cat ${TMP_FILE}.mangle-chains | wc -l) (chains)"
echo "---| Sum of amount RULES in table 'mangle' : ${SUM_RULE_MANGLE} (rules)"
echo ""
}
## Count number of chains in table raw
cal_rule_table_raw()
{
echo ""
echo "++++++++++++++++++++++++++++++++++++"
echo "+ Table 'raw' of gnome-dn firewall +"
echo "++++++++++++++++++++++++++++++++++++"
echo ""
iptables -nvL -t raw | grep "^Chain" | awk '{print $2}' 2> /dev/null 1>> ${TMP_FILE}.raw-chains
SUM_RULE_RAW=0
while read CHAIN
do
NUM_IPT_RAW_CHAIN=$(iptables -nvL ${CHAIN} -t raw | grep -Ev "^Chain|^pkts|^\ pkts" | wc -l)
echo "-- Chain ${CHAIN} : ${NUM_IPT_RAW_CHAIN} (rules)"
SUM_RULE_RAW=$((SUM_RULE_RAW+${NUM_IPT_RAW_CHAIN}))
done < ${TMP_FILE}.raw-chains
echo "---| Sum of amount CHAINS in table 'raw' : $(cat ${TMP_FILE}.raw-chains | wc -l) (chains)"
echo "---| Sum of amount RULES in table 'raw' : ${SUM_RULE_RAW} (rules)"
echo ""
}
## Count number of chains in table security
cal_rule_table_security()
{
echo ""
echo "+++++++++++++++++++++++++++++++++++++++++"
echo "+ Table 'security' of gnome-dn firewall +"
echo "+++++++++++++++++++++++++++++++++++++++++"
echo ""
iptables -nvL -t security | grep "^Chain" | awk '{print $2}' 2> /dev/null 1>> ${TMP_FILE}.security-chains
SUM_RULE_SECURITY=0
while read CHAIN
do
NUM_IPT_SECURITY_CHAIN=$(iptables -nvL ${CHAIN} -t security | grep -Ev "^Chain|^pkts|^\ pkts" | wc -l)
echo "-- Chain ${CHAIN} : ${NUM_IPT_SECURITY_CHAIN} (rules)"
SUM_RULE_SECURITY=$((SUM_RULE_SECURITY+${NUM_IPT_SECURITY_CHAIN}))
done < ${TMP_FILE}.security-chains
echo "---| Sum of amount CHAINS in table 'security' : $(cat ${TMP_FILE}.security-chains | wc -l) (chains)"
echo "---| Sum of amount RULES in table 'security' : ${SUM_RULE_SECURITY} (rules)"
echo ""
}
cal_rule_all()
{
iptables -nvL -t filter 2> /dev/null 1>> ${TMP_FILE}
iptables -nvL -t nat 2> /dev/null 1>> ${TMP_FILE}
iptables -nvL -t mangle 2> /dev/null 1>> ${TMP_FILE}
iptables -nvL -t raw 2> /dev/null 1>> ${TMP_FILE}
iptables -nvL -t security 2> /dev/null 1>> ${TMP_FILE}
## Count active iptables rule line ##
grep -v "^$" ${TMP_FILE} > ${TMP_FILE}.2
rm -f ${TMP_FILE}
mv ${TMP_FILE}.2 ${TMP_FILE}
## Print sum of active iptables rule-lines
NUM_IPT_RULE=$(cat ${TMP_FILE} | grep -Ev "^Chain|^\pkts|^\ pkts" | wc -l)
NUM_IPT_CHAIN=$(cat ${TMP_FILE} | grep "^Chain" | wc -l)
NUM_IPT_RULE=$((NUM_IPT_RULE-3))
echo ""
echo "++++++++++++++++++++++++++++++++++++++++++++"
echo "+ Summary information of gnome-dn firewall +"
echo "++++++++++++++++++++++++++++++++++++++++++++"
echo ""
echo "---| Sum of current rules type : ${NUM_IPT_CHAIN} (chains)"
echo "---| Sum of active attackes IPs : ${NUM_IPT_RULE} (IPs)"
echo ""
}
## Delete file temporary ##
delete_tmp_file()
{
if [ -f ${TMP_FILE} ];then
rm -f ${TMP_FILE}
fi
if [ -f ${TMP_FILE}.filter-chains ];then
rm -f ${TMP_FILE}.filter-chains
fi
if [ -f ${TMP_FILE}.nat-chains ];then
rm -f ${TMP_FILE}.nat-chains
fi
if [ -f ${TMP_FILE}.mangle-chains ];then
rm -f ${TMP_FILE}.mangle-chains
fi
if [ -f ${TMP_FILE}.raw-chains ];then
rm -f ${TMP_FILE}.raw-chains
fi
if [ -f ${TMP_FILE}.security-chains ];then
rm -f ${TMP_FILE}.security-chains
fi
}
####################
## Main Functions ##
if [ -z ${TMP_FILE} ];then
TMP_FILE="/tmp/iptables-rule-count.tmp.txt"
fi
delete_tmp_file
cal_rule_all
cal_rule_table_filter
cal_rule_table_nat
cal_rule_table_mangle
cal_rule_table_raw
cal_rule_table_security
delete_tmp_file
exit 0