-
Notifications
You must be signed in to change notification settings - Fork 0
/
dosboot.pl
293 lines (257 loc) · 6.61 KB
/
dosboot.pl
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
#!/usr/bin/perl
#---------------
# DoS Boot
# Detects and bans DoS attack sources
# created by: Ryan DeShone
#---------------
use strict;
use Getopt::Long;
#Configuration Variables
#-----------------------
my $stime = 30;
my $maxconn = 100;
my $maxban = 500;
#-----------------------
#---------------------------------------------
# There is no need to edit anything past this.
#---------------------------------------------
#Version Number
#--------------
my $vnum = "0.3.1";
#--------------
# Define subroutines
sub search;
sub checkban;
sub doban ($);
sub unban ($);
sub pout;
sub help;
sub genwhitelist;
####################
# Important files
my $confpath = "/etc/dosboot";
my $whitelist = "whitelist.dosboot";
my $logfile = "/var/log/dosboot.log";
#################
#Make sure we are running as root or none of the important stuff will work
if ($> != 0){
print "DosBoot must be run as root!\n\tQuitting...\n";
exit -1;
}
#Command line switches
my $list=0; my $help=0; my $debug=0; my $genwl=0;
#Parse command line options
my $result;
$result = GetOptions("list|l" => \$list,
"help|h|?" => \$help,
"debug|d" => \$debug,
"maxban=i" => \$maxban,
"maxcon=i" => \$maxconn,
"genwhitelist|g" => \$genwl);
if($debug){print "list = $list\nhelp = $help\n";}
if($help){
help();
exit;
}
#Check if conf directory exists. Create it if not.
unless(-d "$confpath"){
print "Creating $confpath\n";
mkdir("$confpath",0750);
}
unless(-e "$confpath/$whitelist" && $genwl==0){
print "Generating whitelist\n";
genwhitelist();
if($genwl != 0){
print "Generated new whitelist\n";
exit;
}
}
#Attempt to detect APF or CSF, fall back on iptables if necessary
my ($PREBAN, $POSTBAN, $PREUBAN, $POSTUBAN);
my $uban = 0;
if (system("apf > /dev/null 2>&1") == 0){
$PREBAN = "apf -d";
$POSTBAN = "";
$PREUBAN = "apf -u";
$POSTUBAN = "";
$uban = 1;
print "Using apf to ban IPs\n";
}elsif(system("csf > /dev/null 2>&1") == 0){
$PREBAN = "csf -d";
$POSTBAN = "";
$PREUBAN = "csf -dr";
$POSTUBAN = "";
$uban = 1;
print "Using csf to ban IPs\n";
}else{
$PREBAN = "/sbin/iptables -I INPUT -s";
$POSTBAN = "-j DROP";
$PREUBAN = "/sbin/iptables -D INPUT -s";
$POSTUBAN = "-j DROP";
$uban = 1;
print "Falling back on iptables for bans\n";
}
##############################################
# Load whitelist so we don't ban the wrong IPs
##############################################
my @WHITELIST;
open(WL, "$confpath/$whitelist") or die "Unable to open $whitelist : $!";
while(<WL>){
my $line = $_;
chomp($line);
unless($line =~ m/^#/) {
if ($line =~ /^*([1-2]*[0-9]*[0-9]\.[1-2]*[0-9]*[0-9]\.[1-2]*[0-9]*[0-9]\.[1-2]*[0-9]*[0-9]).*$/){
push @WHITELIST, $line;
}
}
}
if($debug){print "Whitelist array:\n@WHITELIST\n";}
close(WL);
##############################################
# The most important part of the script,
# the endless program loop of banning goodness
##############################################
while (1){
search();
if($list){
pout();
exit;
}
checkban();
sleep($stime);
}
#End of the most important part of the script.
#Variables to store data, no touchy
my (@DATA, %CONINFO, @BANLIST);
my $totban = 0;
my $banpos = 0;
###############################
# Subroutines past this point #
###############################
################################
#
# Gather and parse data
#
################################
sub search {
chomp(@DATA = qx!/bin/netstat -nt!);
shift(@DATA);shift(@DATA);
while (@DATA){
my $t = shift(@DATA);
my ($prot,$recvq,$sendq,$local,$remote,$state) = split(" ", $t);
$remote =~ s/::ffff://g;
my ($remoteip,$remoteport) = split(":", $remote);
if($CONINFO{$remoteip}){
$CONINFO{$remoteip}++;
}else{
$CONINFO{$remoteip} .= 1;
}
}
}
######################################
#
# Print a table of IPs and the number
# of connections currently open
#
######################################
sub pout{
print "-------------------------------------------------\n";
print "|Source IP\t\t\t|Connections\t|\n";
print "-------------------------------------------------\n";
foreach my $ip (sort keys %CONINFO) {
print "|$ip \t\t|$CONINFO{$ip}\t\t|\n";
}
print "-------------------------------------------------\n";
}
############################################
#
# Print a helpful help message. For helping.
#
############################################
sub help{
print "DoS-Boot v. $vnum\n
Usage:\tdosboot.pl OPTIONS\n
Options:
--genwhitelist,-g\t Regenerate the automatic whitelist
\t\tCAUTION: This will overwrite the current whitelist
--help,-h\tPrint this helpful help message\n
--list,-l\tPrint out list of connected IPs and the number of open connections
--maxban=n\tMaximum number of IPs to ban before removing old bans
\t\tCurrently only works with apf
--maxcon=n\tMaximum number of connections before banning IP\n";
}
#######################################
#
# Check IP table for naughty IPs to ban
#
#######################################
sub checkban {
foreach my $ip (sort keys %CONINFO) {
if ($CONINFO{$ip} >= $maxconn){
unless ( grep ( /$ip$/,@BANLIST ) > 0 || grep ( /$ip$/,@WHITELIST ) > 0 ){
if ($totban >= $maxban && $uban == 1){
print "Removing $BANLIST[$banpos] from firewall.\n";
unban ($BANLIST[$banpos]);
}
print "Blocking $ip with $CONINFO{$ip} connections.\n";
doban ($ip);
$BANLIST[$banpos]="$ip";
$banpos = ($banpos + 1) % $maxban;
$totban++;
}
}
}
}
#################################
#
# Do the actual banning of the IP
#
#################################
sub doban ($){
my ($ip) = @_;
my $rc = system("$PREBAN $ip $POSTBAN");
if($rc != 0){
print "Banning IP failed, bugging out.\n";
exit $rc;
}
}
############################################
#
# Attempt to remove blocked IP from firewall
#
############################################
sub unban ($){
my ($ip) = @_;
my $rc = system("$PREUBAN $ip $POSTUBAN");
if($rc != 0){
print "Unbanning IP failed, bugging out.\n";
if($debug != 0){print "DEBUG INFO:
UBAN IP = $ip
totban = $totban
banpos = $banpos
maxban = $maxban\n";}
exit $rc;
}
}
#################################################
#
# Autogenerates a whitelist containing the IPs
# currently assigned to the server
#
#################################################
sub genwhitelist {
unless (-e "$confpath/$whitelist"){
print "Creating whitelist at $confpath/$whitelist\n";
open(WL, ">$confpath/$whitelist") or die "Can't create $whitelist : $!";
}
my @DATA;
chomp(@DATA = qx!/sbin/ifconfig -a | grep "inet addr"!);
while(@DATA){
my $t = shift(@DATA);
my ($a,$addr) = split(" ",$t);
$addr =~ s/::ffff://g;
my ($b,$ip) = split(":",$addr);
print WL "$ip\n";
}
close(WL);
}