Newer
Older
#!/usr/bin/perl
use strict; use warnings;
################################################################
###### Script to check Blocklist.de list. Block new IP ######
###### and unblock deleted entrys ######
################################################################
## config ##
my $listUrl = "http://lists.blocklist.de/lists/all.txt";
my $fileName = "Blocklist.txt";
my $tmpDir = "/tmp";
my $file = "$tmpDir/$fileName";
## binarys ##
my $iptables = "/sbin/iptables";
my $ipset = "/usr/sbin/ipset";
my $grep = "/bin/grep";
my $rm = "/bin/rm";
my $wget = "/usr/bin/wget";
## plain variables ##
my($row, $Blocklist, $line, $check, $checkLine, $result, $output, $ipRegex);
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
my ($added, $removed, $skipped);
$added = $removed = $skipped = 0;
## init arrays ##
my @fileArray = ();
my @ipsetArray = ();
## init hashes for faster searching
my %ipsetArray;
my %fileArray;
#****************************#
#*********** MAIN ***********#
#****************************#
&iptablesCheck();
&getFileArray();
&getIpsetArray();
&addIpsToBlocklist();
&remIpsFromBlocklist();
&cleanup();
exit;
#***** END MAIN *****#
#****************************#
#******* Subroutines ********#
#****************************#
############# iptablesCheck ###############
## checks if all necessary ##
## iptable/ipset Settings have been set ##
###########################################
sub iptablesCheck {
## Do we have an BLOCKLIST/DROP Chain?
if (`$iptables -L -n | $grep BLOCKLIST` =~ m/Chain BLOCKLIST/) {
} else {
print "Creating Chain BLOCKLIST \n";
`$iptables -N BLOCKLIST`;
`$iptables -A BLOCKLIST -m limit --limit 2/min -j LOG --log-prefix "Blocklist Dropped: " --log-level 4`;
`$iptables -A BLOCKLIST -j DROP`;
}
## Do we have an ipset list called blocklist?
if(`$ipset list -n | $grep blocklist` =~ m/blocklist/) {
} else {
`$ipset create blocklist hash:ip hashsize 4096`;
print "Created ipset list blocklist\n";
}
## Is there an forwarded from INPUT to BLOCKLIST?
if (`$iptables -L INPUT | $grep BLOCKLIST`=~ m/BLOCKLIST/) {
} else {
print "Creating forward to BLOCKLIST chain \n";
`$iptables -I INPUT -m set --match-set blocklist src -j BLOCKLIST`;
}
}
######## END iptablesCheck ########
########## getFileArray #############
## downloads the Blocklist.txt and ##
## pushes it into an array ##
#####################################
sub getFileArray {
`$wget -q -O $tmpDir/$fileName $listUrl && echo "Downloaded temp file to $tmpDir/$fileName" || echo "Can not download file.... stopping"`;
open(INFO, $file) or die("Could not open file.");
foreach $line (<INFO>) {
push(@fileArray, $line);
}
close(INFO);
chomp(@fileArray);
%fileArray = map {$_ => 1 } @fileArray;
}
####### END getFileArray ##########
######### getIpsetArray ##########
## runs ipset list blocklist ##
## and pushes it into ##
## array ipsetList ##
##################################
sub getIpsetArray {
$output = `$ipset list blocklist`;
@ipsetArray = split("\n", $output);
# %ipsetArray = map { $_ => 1} @ipsetArray;
#remove the first 6 Elements of our Array using splice (ipset header info)
splice @ipsetArray, 0, 6;
%ipsetArray = map { $_ => 1} split("\n", $output);
}
##### END getIpsetArray #########
######## addIpsToBlocklist ######
## adds IPs to our blocklist ##
#################################
sub addIpsToBlocklist {
foreach $line (@fileArray) {
if (exists $ipsetArray{"$line"}) {
$skipped++;
} else {
if ($line eq &isIpv4($line)) {
$result = `$ipset add blocklist $line`;
$added++;
print "added $line\n"
} else {
$skipped++;
}
}
}
}
######## END addIpsToBlocklist ######
########## remIpsFromBlocklist ########
## remove IPs from our blocklist ##
#####################################
sub remIpsFromBlocklist {
foreach $line (@ipsetArray) {
if (exists $fileArray{"$line"}) {
$skipped++;
} else {
if ($line eq &isIpv4($line)) {
$result = `$ipset del blocklist $line`;
print "removed $line\n";
$removed++;
} else {
$skipped++;
}
}
}
}
######## END remIpsFromBlocklist ########
################## cleanup ###################
#### Cleanup: move tmp file to new place #####
##############################################
sub cleanup {
$result = `$rm $tmpDir/$fileName && echo "Deleted file $tmpDir/$fileName" || echo "Can\t delete file $tmpDir/$fileName"`;
print "\nWe added $added, removed $removed, skipped $skipped Rules\n";
}
############### END cleanup ######################
######## END ipToRegex ########
############ isIpv4 #############
## check if given value looks ##
## like an ipv4 ip address ##
#################################
sub isIpv4 {
my ($isIp) = @_;
if ($isIp =~ m/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/) {
#print "It's an IPv4\n";
return $isIp;
} else {
return 0;
}
}
######### END isIpv4 ##########
######### EOF ###########