-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_evil_ips.sh
97 lines (85 loc) · 2.09 KB
/
block_evil_ips.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
#!/bin/bash
SETNAME="evil_ips"
IPTABLES_CHAIN=INPUT
IPTABLES_RULE_NUM=1
usage() {
cat <<EOF
block_evil_ips.sh [<options] <URL>
Options:
--ip_set_name <name> Name of the ip set (default evil_ips)
--iptables_rule_num <num> Number of the rule of the iptables chain (default 1)
--iptables_chain <name> Name of the iptables chain (default INPUT)
--help Print this help text
EOF
}
while [ $# -gt 1 ]; do
case "$1" in
--ip_set_name)
if [ $# -lt 2 ]; then
echo "Missing <name> for --ip_set_name option" >&2
exit 1
fi
shift
SETNAME=$1
shift
;;
--iptables_rule_name)
if [ $# -lt 2 ]; then
echo "Missing <num> for --iptables_rule_name option" >&2
exit 1
fi
shift
IPTABLES_RULE_NUM=$1
shift
;;
--iptables_chain)
if [ $# -lt 2 ]; then
echo "Missing <name> for --iptables_chain option" >&2
exit 1
fi
shift
IPTABLES_CHAIN=$1
shift
;;
--help)
usage
exit 0
;;
*)
echo "Unknown option $1. Use --help for help" >&2
exit 1
esac
done
if [ $# -lt 1 ]; then
echo "Missung <URL>" >&2
usage
exit 1
fi
if [ $1 = "--help" ]; then
usage
exit 1
fi
URL=$1
evil_ips=$(curl --compressed -f $URL 2>/dev/null)
if [ $? -ne 0 ]; then
echo "Error: Could not download $URL" >&2
exit 1
fi
if [ -z "$evil_ips" ]; then
echo "No IP addressed downloaded from $URL"
exit 0
fi
logger -t "evil_ip_block" "Adding IPs to be blocked."
ipset list $SETNAME &>/dev/null # check if the IP set exists
if [ $? -ne 0 ]; then
ipset create $SETNAME hash:ip family inet # create new IP set
iptables -I INPUT 1 -m set --match-set $SETNAME src -j DROP
else
ipset flush $SETNAME # clear existing IP set
fi
# populate the new or existing empty IP set
for i in $evil_ips ; do
if [[ $i =~ [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ ]]; then
ipset -exist add $SETNAME $i
fi
done