-
Notifications
You must be signed in to change notification settings - Fork 0
/
sanction
executable file
·402 lines (304 loc) · 14.2 KB
/
sanction
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/bin/bash
#
# Sanction - A quick and effective method to block entire ip blocks belonging
# to a specific country. You can block a specific port or ban the IPs outright.
#
# Version: 1.0.1
#
# Written by Alex Darke (adarke@gmail.com) 5/10/2015
#
# Thanks to IPDeny.com for providing the publicly available IP blocks. www.ipdeny.com
countryList="inc/countryList"
current_time=$(date "+%Y.%m.%d-%H.%M.%S")
if [ "$EUID" -ne 0 ]
then echo "Because this script generates and applies IPtables/IPset rules, root level access is required."
exit
fi
if ! [ -x "$(command -v ipset)" ]; then
echo 'ipset is not installed. Continuing using iptables...' >&2
echo
echo -n "Enter the name of the country to block : "
read Country
echo -n "Enter the specific port number to block or ALL to block all access: "
read Port
Port=$(echo $Port | tr [a-z] [A-Z]);
echo
echo "Country = $Country";
echo "Port = $Port";
echo
while true
do
echo -n "Please confirm (y or n) : "
read CONFIRM
case $CONFIRM in
y|Y|YES|yes|Yes) break ;;
n|N|no|NO|No)
echo Aborting
echo
exit
;;
*) echo Please enter only y or n
esac
done
echo
echo
grep -i "$Country" $countryList >/dev/null 2>/dev/null;
if [ $? -ne 0 ] ; then
echo "Unable to find the country you specified ($Country). Please check the spelling and try again."; echo;
exit 1;
fi
#Check to ensure there's only one match:
matchCount=$(grep -i "$Country" $countryList | awk '{print $1}' | wc -l);
if [ $matchCount -eq 1 ] ; then
countryCode=$(grep -i $Country $countryList | awk '{print $1}' | tr "[:upper:]" "[:lower:]")
countryName=$(grep -i $Country $countryList | awk '{print $2}')
countryName=$(grep -i "$Country" $countryList | awk '{out=$2; for(i=3;i<=NF;i++){out=out" "$i}; print out}')
echo "test"
echo "$countryName is the 2 letter abbreviation: $countryCode";
echo "Obtaining IP blocks now..."
wget -P /tmp http://www.ipdeny.com/ipblocks/data/aggregated/${countryCode}-aggregated.zone >/dev/null 2>/dev/null;
if [ $? -ne 0 ] ; then
echo "Unable to find IP blocks for $countryCode / $countryName. Aborting.";
exit 1;
fi
echo "Formulating IPTables rules..."
echo "#!/bin/bash" > /tmp/sanction.$countryCode.rules;
echo "#!/bin/bash" > /tmp/sanction.remove.$countryCode.$current_time.rules;
if [ $Port = "ALL" ] ; then
for i in $(cat /tmp/$countryCode-aggregated.zone) ;
do echo "iptables -A INPUT -s $i -j DROP" >> /tmp/sanction.$countryCode.rules;
done
for i in $(cat /tmp/$countryCode-aggregated.zone) ;
do echo "iptables -D INPUT -s $i -j DROP" >> /tmp/sanction.remove.$countryCode.$current_time.rules;
done
else
for i in $(cat /tmp/$countryCode-aggregated.zone) ;
do echo "iptables -A INPUT -s $i -p tcp --dport $Port -j DROP" >> /tmp/sanction.$countryCode.rules;
done
for i in $(cat /tmp/$countryCode-aggregated.zone) ;
do echo "iptables -D INPUT -s $i -p tcp --dport $Port -j DROP" >> /tmp/sanction.remove.$countryCode.$current_time.rules;
done
fi
echo "rm /tmp/sanction.remove.$countryCode.$current_time.rules;" >> /tmp/sanction.remove.$countryCode.$current_time.rules;
echo "Applying IPtables rules..."
chmod +x /tmp/sanction.$countryCode.rules;
chmod +x /tmp/sanction.remove.$countryCode.$current_time.rules;
/tmp/sanction.$countryCode.rules;
echo
echo "Success! The requested rules have been applied."
echo "To remove them, run the command (as root):"
echo
echo "/tmp/sanction.remove.$countryCode.$current_time.rules"
echo
echo "or just restart iptables."
else
echo "Warning! There were multiple matches for the country you asked for."
echo
OLDIFS=$IFS
IFS=$'\n'
lines=($(grep -i $Country $countryList))
createmenu () {
PS3="Select which Country to use: "
select option; do
if [ "$REPLY" -gt "$#" ]; then
echo "This shouldn't happen OMG"
break;
elif [ 1 -le "$REPLY" ] || [ "$REPLY" -le $(($#-1)) ]; then
chosen=$option;
break;
else
echo "Incorrect Input: Select a number 1-$#"
fi
done
}
createmenu "${lines[@]}"
IFS=$OLDIFS
echo
echo "You picked: $chosen";
#second pass for select menu option
countryCode=$(echo $chosen| awk '{print $1}' | tr "[:upper:]" "[:lower:]")
countryName=$(echo $chosen| awk '{print $2}')
echo "Obtaining IP blocks now..."
wget -P /tmp http://www.ipdeny.com/ipblocks/data/aggregated/${countryCode}-aggregated.zone >/dev/null 2>/dev/null;
if [ $? -ne 0 ] ; then echo "Unable to find IP blocks for $countryCode / $countryName. Aborting."; exit 1; fi
echo "Formulating IPTables rules..."
echo
echo "#!/bin/bash" > /tmp/sanction.$countryCode.rules;
echo "#!/bin/bash" > /tmp/sanction.remove.$countryCode.$current_time.rules;
if [ $Port = "ALL" ] ; then
for i in $(cat /tmp/$countryCode-aggregated.zone) ;
do echo "iptables -A INPUT -s $i -j DROP" >> /tmp/sanction.$countryCode.rules;
done
for i in $(cat /tmp/$countryCode-aggregated.zone) ;
do echo "iptables -D INPUT -s $i -j DROP" >> /tmp/sanction.remove.$countryCode.$current_time.rules;
done
else
for i in $(cat /tmp/$countryCode-aggregated.zone) ;
do echo "iptables -A INPUT -s $i -p tcp --dport $Port -j DROP" >> /tmp/sanction.$countryCode.rules;
done
for i in $(cat /tmp/$countryCode-aggregated.zone) ;
do echo "iptables -D INPUT -s $i -p tcp --dport $Port -j DROP" >> /tmp/sanction.remove.$countryCode.$current_time.rules;
done
fi
echo "rm /tmp/sanction.remove.$countryCode.$current_time.rules;" >> /tmp/sanction.remove.$countryCode.$current_time.rules;
echo
echo "Applying IPtables rules..."
echo
chmod +x /tmp/sanction.$countryCode.rules;
chmod +x /tmp/sanction.remove.$countryCode.$current_time.rules;
/tmp/sanction.$countryCode.rules;
echo "Rules applied. To remove them, run the command (as root):"
echo
echo "/tmp/sanction.remove.$countryCode.$current_time.rules"
echo
echo "or just restart iptables."
fi
rm /tmp/*.zone;
rm /tmp//sanction.$countryCode.rules;
else
#Make sure the kernel modules are loaded
modprobe -v ip_set >/dev/null 2>/dev/null;
modprobe -v ip_set_hash_netport >/dev/null 2>/dev/null;
#Create the ipset for sanction with port
ipset create sanction-port hash:net,port >/dev/null 2>/dev/null;
#Create the ipset for sanction just by ip
ipset create sanction hash:net >/dev/null 2>/dev/null;
#Create the iptables rule to match the ipset lists
tableRule=`iptables -L | grep sanction`
if [ -z "$tableRule" ] ; then
iptables -I INPUT -m set --set sanction src,dst -j DROP >/dev/null 2>/dev/null;
iptables -I INPUT -m set --set sanction-port src,dst -j DROP >/dev/null 2>/dev/null;
fi
echo
echo -n "Enter the name of the country to block : "
read Country
echo -n "Enter the specific port number to block or ALL to block all access: "
read Port
Port=$(echo $Port | tr [a-z] [A-Z]);
echo
echo "Country = $Country";
echo "Port = $Port";
echo
while true
do
echo -n "Please confirm (y or n) : "
read CONFIRM
case $CONFIRM in
y|Y|YES|yes|Yes) break ;;
n|N|no|NO|No)
echo Aborting
echo
exit
;;
*) echo Please enter only y or n
esac
done
echo
echo
grep -i "$Country" $countryList >/dev/null 2>/dev/null;
if [ $? -ne 0 ] ; then
echo "Unable to find the country you specified ($Country). Please check the spelling and try again."; echo;
exit 1;
fi
#Check to ensure there's only one match:
matchCount=$(grep -i "$Country" $countryList | awk '{print $1}' | wc -l);
if [ $matchCount -eq 1 ] ; then
countryCode=$(grep -i "$Country" $countryList | awk '{print $1}' | tr "[:upper:]" "[:lower:]")
#countryName=$(grep -i "$Country" $countryList | awk '{print $2}')
countryName=$(grep -i "$Country" $countryList | awk '{out=$2; for(i=3;i<=NF;i++){out=out" "$i}; print out}')
echo "$countryName is the 2 letter abbreviation: $countryCode";
echo "Obtaining IP blocks now..."
wget -P /tmp http://www.ipdeny.com/ipblocks/data/aggregated/${countryCode}-aggregated.zone >/dev/null 2>/dev/null;
if [ $? -ne 0 ] ; then
echo "Unable to find IP blocks for $countryCode / $countryName. Aborting.";
exit 1;
fi
echo "Formulating IPset rules..."
echo "#!/bin/bash" > /tmp/sanction.$countryCode.rules;
echo "#!/bin/bash" > /tmp/sanction.remove.$countryCode.$current_time.rules;
if [ $Port = "ALL" ] ; then
for i in $(cat /tmp/$countryCode-aggregated.zone) ;
do echo "ipset add sanction $i" >> /tmp/sanction.$countryCode.rules;
done
for i in $(cat /tmp/$countryCode-aggregated.zone) ;
do echo "ipset del sanction $i" >> /tmp/sanction.remove.$countryCode.$current_time.rules;
done
else
for i in $(cat /tmp/$countryCode-aggregated.zone) ;
do echo "ipset add sanction-port $i,$Port" >> /tmp/sanction.$countryCode.rules;
done
for i in $(cat /tmp/$countryCodez-aggregated.one) ;
do echo "ipset del sanction-port $i,$Port" >> /tmp/sanction.remove.$countryCode.$current_time.rules;
done
fi
echo "rm /tmp/sanction.remove.$countryCode.$current_time.rules;" >> /tmp/sanction.remove.$countryCode.$current_time.rules;
echo "Applying IPset rules..."
chmod +x /tmp/sanction.$countryCode.rules;
chmod +x /tmp/sanction.remove.$countryCode.$current_time.rules;
/tmp/sanction.$countryCode.rules;
echo "Rules applied. To remove them, run the command (as root):"
echo
echo "/tmp/sanction.remove.$countryCode.$current_time.rules"
else
echo "Warning! There were multiple matches for the country you asked for."
echo
OLDIFS=$IFS
IFS=$'\n'
lines=($(grep -i "$Country" $countryList))
createmenu () {
PS3="Select which Country to use: "
select option; do
if [ "$REPLY" -gt "$#" ]; then
echo "This shouldn't happen OMG"
break;
elif [ 1 -le "$REPLY" ] || [ "$REPLY" -le $(($#-1)) ]; then
chosen=$option;
break;
else
echo "Incorrect Input: Select a number 1-$#"
fi
done
}
createmenu "${lines[@]}"
IFS=$OLDIFS
echo
echo "You picked: $chosen";
#second pass for select menu option
countryCode=$(echo $chosen| awk '{print $1}' | tr "[:upper:]" "[:lower:]")
countryName=$(echo $chosen| awk '{print $2}')
echo "Obtaining IP blocks now..."
wget -P /tmp http://www.ipdeny.com/ipblocks/data/aggregated/${countryCode}-aggregated.zone >/dev/null 2>/dev/null;
if [ $? -ne 0 ] ; then echo "Unable to find IP blocks for $countryCode / $countryName. Aborting."; exit 1; fi
echo "Formulating IPset rules..."
echo
echo "#!/bin/bash" > /tmp/sanction.$countryCode.rules;
echo "#!/bin/bash" > /tmp/sanction.remove.$countryCode.$current_time.rules;
if [ $Port = "ALL" ] ; then
for i in $(cat /tmp/$countryCodez-aggregated.one) ;
do echo "ipset add sanction $i" >> /tmp/sanction.$countryCode.rules;
done
for i in $(cat /tmp/$countryCode-aggregated.zone) ;
do echo "ipset del sanction $i" >> /tmp/sanction.remove.$countryCode.$current_time.rules;
done
else
for i in $(cat /tmp/$countryCode-aggregated.zone) ;
do echo "ipset add sanction-port $i,$Port" >> /tmp/sanction.$countryCode.rules;
done
for i in $(cat /tmp/$countryCode-aggregated.zone) ;
do echo "ipset del sanction-port $i,$Port" >> /tmp/sanction.remove.$countryCode.$current_time.rules;
done
fi
echo "rm /tmp/sanction.remove.$countryCode.$current_time.rules;" >> /tmp/sanction.remove.$countryCode.$current_time.rules;
echo
echo "Applying IPset rules..."
echo
chmod +x /tmp/sanction.$countryCode.rules;
chmod +x /tmp/sanction.remove.$countryCode.$current_time.rules;
/tmp/sanction.$countryCode.rules;
echo "Rules applied. To remove them, run the command (as root):"
echo
echo "/tmp/sanction.remove.$countryCode.$current_time.rules"
fi
rm /tmp/*.zone;
rm /tmp//sanction.$countryCode.rules;
fi