-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathblock
executable file
·240 lines (212 loc) · 4.79 KB
/
block
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
#!/usr/bin/perl
# block - maintain firewall block rules for specified hosts based on specific events
# vi: set sw=4 ts=4 tw=0:
use strict;
use warnings;
use Sys::Syslog;
use Getopt::Long;
use File::Tail;
our @patterns = ('pam_unix\(sshd:auth\): authentication failure; logname=\S* uid=\d* euid=\d* tty=\S* ruser=\S* rhost=(\S*)');
our $maxhits = 3;
our $keeptime = 1;
our $chain = "BLOCK";
our %hits;
our $phone = "0415439838";
openlog "block", "ndelay,pid", "user"
or die "Cannot open connection to log server\n";
# modes
#my $clean; # unblock all IP addresses not seen in last $keeptime minutes
my $block; # block specified IP address
my $daemon; # fork and continue running in the background as a daemon
my $unblock; # unblock specified IP address
my $monitor; # read from the specified file, taking action on lines matching @patterns
my $simulate; # simulation only, no blocking is done
my $list; # list blocked addresses
my $help; # show a brief usage message
our $verbose; # print debugging messages to STDERR
sub sendsms
{
system('smssend "' . $phone . '" "' . "@_" . '"');
}
sub notice
{
syslog("notice", @_);
sendsms(@_);
}
sub error
{
syslog("err", @_);
sendsms(@_);
}
sub debug
{
syslog("debug", @_);
}
sub usage
{
print STDERR "Block: block -b <host>\n";
print STDERR "Unblock: block -u <host>\n";
print STDERR "List: block -l\n";
print STDERR "Monitor: block -m <file>\n";
print STDERR "Simulate: block -n [other options]\n";
print STDERR "Daemon: block -d [other options]\n";
print STDERR "Verbose: block -v [other options]\n";
}
GetOptions('add|block|a|b=s' => \$block,
'daemon|d' => \$daemon,
'monitor|m=s' => \$monitor,
'remove|unblock|r|u=s' => \$unblock,
'list|l' => \$list,
'help|h' => \$help,
'test|simulate|t|s|n' => \$simulate,
'verbose|v' => \$verbose)
or print STDERR "Error reading options\n", usage(), exit(2);
if (defined($block)) {
block($block);
}
if (defined($unblock)) {
unblock($unblock);
}
if (defined($list)) {
list();
}
if (defined($daemon)) {
daemonize();
}
if (defined($monitor)) {
monitor($monitor);
}
if (defined($help)) {
usage();
}
exit(0);
# get the iptables rule for the given address
# this must be the same for block and unblock, so we generalize it here
sub rule
{
my $host = shift @_;
return "-s $host -j REJECT";
}
sub block
{
my $host = shift @_;
my $rule = rule($host);
if ($host =~ /^(0|127|172|192)\./) {
notice("Not blocking special network address $host");
}
else {
if (defined($simulate)) {
notice("Would block $host");
}
else {
if (system("/sbin/iptables -A $chain $rule") == 0) {
notice("Blocked $host");
}
else {
error("Error blocking $host");
}
}
}
}
sub unblock
{
my $host = shift @_;
my $rule = rule($host);
if (defined($simulate)) {
notice("Would unblock $host");
}
else {
if (system("/sbin/iptables -D $chain $rule") == 0) {
notice("Unblocked $host");
}
else {
error("Error unblocking $host");
}
}
}
sub daemonize
{
my $pid = fork();
if (!defined($pid)) {
error("Fork failed, cannot daemonize");
exit(1);
}
if ($pid == 0) {
debug("FORKED");
# child, continue
close(STDERR);
close(STDOUT);
close(STDIN);
}
else {
debug("PARENT EXITING (CHILD IS $pid)");
exit(0);
}
}
# Watch a file and block a host if it matches more than $maxhits times
sub monitor
{
my $filename = shift @_;
my @files;
push(@files, File::Tail->new($filename));
while (1) {
debug("READING");
my $timeout = 10; # this must be > 0
my ($nfound, $timeleft, @pending) = File::Tail::select(undef, undef, undef, $timeout, @files);
if (!$nfound) {
debug("TIMED OUT");
}
elsif (@pending) {
debug("PENDING INPUT");
foreach my $file (@pending) {
my $line = $file->read();
chomp $line;
debug("LINE IS $line");
foreach my $pattern (@patterns) {
if ($line =~ /$pattern/) {
my $host = $1;
push @{$hits{$host}}, time();
debug("$host");
my $nhits = @{$hits{$host}};
if ($nhits > $maxhits) {
debug("BLOCK $host");
block($host);
}
else {
debug("HIT $nhits for $host");
}
}
}
}
}
cleanup();
}
}
# Delete and unblock entries older than $keeptime minutes
sub cleanup
{
my $time = time();
my $expirytime = $time - $keeptime * 60;
foreach my $host (keys %hits) {
my @hits = @{$hits{$host}};
foreach my $i (0..scalar(@hits)-1) {
my $hit = $hits[$i];
if ($hit < $expirytime) {
debug("Deleting old entry for $host");
debug("OLD @{$hits{$host}}");
delete ${$hits{$host}}[$i];
debug("NEW @{$hits{$host}}");
}
}
if (@{$hits{$host}} < $maxhits) {
notice("Unblocking $host after $keeptime minutes");
unblock($host);
}
}
}
# Show the list of hosts currently blocked
sub list
{
debug("LISTING");
system("/sbin/iptables -L $chain -n");
}