-
Notifications
You must be signed in to change notification settings - Fork 10
/
LongTail_class_c_hall_of_shame.pl
executable file
·302 lines (287 loc) · 9.77 KB
/
LongTail_class_c_hall_of_shame.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
294
295
296
297
298
299
300
301
302
#!/usr/bin/perl
############################################################################
# adds commas to numbers so they are readable
#
sub commify {
my ( $sign, $int, $frac ) = ( $_[0] =~ /^([+-]?)(\d*)(.*)/ );
my $commified = (
reverse scalar join ',',
unpack '(A3)*',
scalar reverse $int
);
return $sign . $commified . $frac;
}
sub this_month_hall_of_shame {
my %my_array;
my $count;
chdir ("/var/www/html/honey/attacks/");
`find . -name '*.*.*.*-$YEAR.$MONTH.*' |sed 's/^..//' >/tmp/LongTail.hall.of.shame.$$`;
open (FILE, "/tmp/LongTail.hall.of.shame.$$");
while (<FILE>){
($ip1,$ip2,$ip3,$trash)=split (/\./,$_);
$my_array{"$ip1.$ip2.$ip3"}++;
}
close (FILE);
open (OUTPUT, ">/tmp/all_class_c.$$") ||die "Can not write to /tmp/all_class_c.$$\n";
#
# This sorts by number of attacks :-)
#
foreach my $name (keys %my_array) {
print (OUTPUT "$name $my_array{$name}\n");
}
close (OUTPUT);
system ("sort -nrk2 /tmp/all_class_c.$$ > /tmp/all_class_c.$$.tmp");
unlink ("/tmp/all_class_c.$$");
system ("mv /tmp/all_class_c.$$.tmp /tmp/all_class_c.$$");
print "</TABLE>\n";
print "<BR><BR>\n";
print "<TABLE BORDER=3>\n";
print "<TR><TH colspan=3>Class C IP ranges with most attacks for $MONTH $YEAR</TD></TR>\n";
print "<TR> <TH>Class C Address</TH> <TH>Number Of Attacks</TH> <TH>Number Of Login Attempts</TH> </TR> \n";
open (INPUT, "/tmp/all_class_c.$$") ;
while (<INPUT>){
chomp;
($name, $number_of_attacks)=split (/ /,$_);
$note="";
if ( -e "/var/www/html/honey/notes/$name" ){
$note=`cat /var/www/html/honey/notes/$name`;
chomp $note;
}
$number_of_login_attempts=0;
open (INPUT2, "/var/www/html/honey/attacks/sum2.data");
while (<INPUT2>){
if (/$name/){
($dict,$stuff)=split(/ /,$_);
open (INPUT3, "/var/www/html/honey/attacks/dict-$dict.txt.wc");
while (<INPUT3>){
$number_of_login_attempts+= $_;
}
close (INPUT3);
}
}
close (INPUT2);
chomp $number_of_login_attempts;
#print "number of attacks is $number_of_login_attempts\n";
if ( $number_of_login_attempts > 10000){
if ($count < 10){
printf "<TR><TD>%s $note</TD><TD>%d</TD>", $name, $number_of_attacks;
$number_of_login_attempts=&commify($number_of_login_attempts);
printf "<TD>$number_of_login_attempts</TD></TR>\n";
$count ++;
}
if ($count >= 10){last;}
}
}
close (INPUT);
unlink ("/tmp/all_class_c.$$") ;
}
sub this_year_hall_of_shame {
#`ls *.*.*.*.*-$YEAR.* >/tmp/LongTail.hall.of.shame.$$`;
my %my_array;
my $count;
chdir ("/var/www/html/honey/attacks/");
`find . -name '*.*.*.*-$YEAR.*' |sed 's/^..//' >/tmp/LongTail.hall.of.shame.$$`;
open (FILE, "/tmp/LongTail.hall.of.shame.$$");
while (<FILE>){
($ip1,$ip2,$ip3,$trash)=split (/\./,$_);
$my_array{"$ip1.$ip2.$ip3"}++;
}
close (FILE);
open (OUTPUT, ">/tmp/all_class_c.$$") ||die "Can not write to /tmp/all_class_c.$$\n";
foreach my $name (keys %my_array) {
print (OUTPUT "$name $my_array{$name}\n");
}
close (OUTPUT);
system ("sort -nrk2 /tmp/all_class_c.$$ > /tmp/all_class_c.$$.tmp");
unlink ("/tmp/all_class_c.$$");
system ("mv /tmp/all_class_c.$$.tmp /tmp/all_class_c.$$");
print "</TABLE>\n";
print "<BR><BR>\n";
print "<TABLE BORDER=3>\n";
print "<TR><TH colspan=3>Class C IP ranges with most attacks for $YEAR</TD></TR>\n";
print "<TR> <TH>Class C Address</TH> <TH>Number Of Attacks</TH> <TH>Number Of Login Attempts</TH> </TR> \n";
open (INPUT, "/tmp/all_class_c.$$") ;
while (<INPUT>){
chomp;
($name, $number_of_attacks)=split (/ /,$_);
$note="";
if ( -e "/var/www/html/honey/notes/$name" ){
$note=`cat /var/www/html/honey/notes/$name`;
chomp $note;
}
$number_of_login_attempts=0;
open (INPUT2, "/var/www/html/honey/attacks/sum2.data");
while (<INPUT2>){
if (/$name/){
($dict,$stuff)=split(/ /,$_);
open (INPUT3, "/var/www/html/honey/attacks/dict-$dict.txt.wc");
while (<INPUT3>){
$number_of_login_attempts+= $_;
}
close (INPUT3);
}
}
close (INPUT2);
chomp $number_of_login_attempts;
if ( $number_of_login_attempts > 10000){
if ($count < 10){
printf "<TR><TD>%s $note</TD><TD>%d</TD>", $name, $number_of_attacks;
$number_of_login_attempts=&commify($number_of_login_attempts);
printf "<TD>$number_of_login_attempts</TD></TR>\n";
$count ++;
}
if ($count >= 10){last;}
}
}
close (INPUT);
unlink ("/tmp/all_class_c.$$") ;
}
sub all_class_c {
my %my_array;
my $count;
chdir ("/var/www/html/honey/attacks/");
#OLD `find . -name '*.*.*.*.*' |sed 's/^..//' >/tmp/LongTail.hall.of.shame.$$`;
#OLD
#OLD open (FILE, "/tmp/LongTail.hall.of.shame.$$");
#OLD while (<FILE>){
#OLD ($ip1,$ip2,$ip3,$trash)=split (/\./,$_);
#OLD $my_array{"$ip1.$ip2.$ip3"}++;
#OLD }
#OLD close (FILE);
#OLD open (OUTPUT, ">/tmp/all_class_c.$$") ||die "Can not write to /tmp/all_class_c.$$\n";
#OLD # foreach takes forever...
#OLD if ($DEBUG >0){print "DEBUG Starting foreach loop at:";$TMP_DATE=`date`; print $TMP_DATE;}
#OLD foreach my $name (keys %my_array) {
#OLD print (OUTPUT "$name $my_array{$name}\n");
#OLD }
#OLD close (OUTPUT);
#OLD if ($DEBUG >0){print "DEBUG done with foreach loop at:";$TMP_DATE=`date`; print $TMP_DATE;}
#OLD system ("sort -nrk2 /tmp/all_class_c.$$ > /tmp/all_class_c.$$.tmp");
#OLD unlink ("/tmp/all_class_c.$$");
#OLD system ("mv /tmp/all_class_c.$$.tmp /tmp/all_class_c.$$");
#OLD
#OLD if ($DEBUG >0){print "DEBUG done with sort command at:";$TMP_DATE=`date`; print $TMP_DATE;}
print "</TABLE>\n";
print "<BR><BR>\n";
print "<TABLE BORDER=3>\n";
print "<TR><TH colspan=3>All Class C IP ranges Sorted By Number Of Attacks</TD></TR>\n";
print "<TR> <TH>Class C Address</TH> <TH>Number Of Attacks</TH> <TH>Number Of Login Attempts</TH> </TR> \n";
#OLD# foreach my $name (sort { $my_array{$b} <=> $my_array{$a} } keys %my_array) {
#OLD open (INPUT, "/tmp/all_class_c.$$") ;
#OLD if ($DEBUG >0){print "DEBUG Starting reading /tmp/all_class_c.$$ at:";$TMP_DATE=`date`; print $TMP_DATE;}
#OLD while (<INPUT>){
#OLD chomp;
#OLD ($name, $number_of_attacks)=split (/ /,$_);
#OLD if ($DEBUG >0){ print "DEBUG name is $name\n";}
#OLD $note="";
#OLD if ( -e "/var/www/html/honey/notes/$name" ){
#OLD $note=`cat /var/www/html/honey/notes/$name`;
#OLD chomp $note;
#OLD }
#OLD #
#OLD # this was horribly slow too
#OLD # Can also fail with "bash: /usr/bin/wc: Argument list too long"
#OLD #
#OLD #$number_of_login_attempts=`cat $name* |wc -l`;
#OLD
#OLD #
#OLD # Damn, I'm reading this file a bazillion times
#OLD # If there are 11,000 Class C spaces, then I
#OLD # read this file 11,000 times. This sucks.
#OLD #
#OLD # I need to make this significantly faster
#OLD # 2015-12-30 ericw
#OLD #
#OLD # Here's a hint:
#OLD # sort sum2 by IP address, calculate totals, write to a
#OLD # temp file, then sort and print the temp file to the
#OLD # output file.
#OLD # sort -nr -t\> -k5 /var/www/html/honey//class_c_list.shtml
#OLD #
#OLD $number_of_login_attempts=0;
#OLD open (INPUT2, "/var/www/html/honey/attacks/sum2.data");
#OLD while (<INPUT2>){
#OLD if (/$name/){
#OLD ($dict,$stuff)=split(/ /,$_);
#OLD open (INPUT3, "/var/www/html/honey/attacks/dict-$dict.txt.wc");
#OLD while (<INPUT3>){
#OLD $number_of_login_attempts+= $_;
#OLD }
#OLD close (INPUT3);
#OLD }
#OLD }
#OLD close (INPUT2);
#OLD chomp $number_of_login_attempts;
#OLD printf "<TR><TD>%s $note</TD><TD>%d</TD>", $name, $number_of_attacks;
#OLD $number_of_login_attempts=&commify($number_of_login_attempts);
#OLD printf "<TD>$number_of_login_attempts</TD></TR>\n";
#OLD }
#OLD close (INPUT);
$count=1;
$t2=0;
$number_of_login_attempts=0;
$number_of_attacks=0;
`sed 's/ /\.\./' /var/www/html/honey/attacks/sum2.data | sort -n -t . -k 3,3n -k 4,4n -k 5,5n |sed \'s/\\.\\./ /\' > /var/www/html/honey/attacks/sum2.data.sorted`;
$prior_class_c="";
open (INPUT2, "/var/www/html/honey/attacks/sum2.data.sorted");
open (OUTPUT2, ">/var/www/html/honey/attacks/sum2.data.sorted.out");
while (<INPUT2>){
($dict,$ip1,$ip2,$ip3,$trash)=split(/\s+|\./,$_,5);
#print "debug $ip1,$ip2,$ip3\n";
$this_class_c="$ip1.$ip2.$ip3";
if ( $prior_class_c ne $this_class_c ) {
if ( $prior_class_c ne "" ) {
print (OUTPUT2 "$prior_class_c $number_of_attacks $number_of_login_attempts\n");
$number_of_login_attempts=0;
$number_of_attacks=0;
}
$prior_class_c = $this_class_c ;
}
open (INPUT3, "/var/www/html/honey/attacks/dict-$dict.txt.wc");
while (<INPUT3>){
$number_of_login_attempts+= $_;
$number_of_attacks++;
}
close (INPUT3);
}
close (INPUT2);
close (OUTPUT2);
`sort -nr -k2 /var/www/html/honey/attacks/sum2.data.sorted.out >/var/www/html/honey/attacks/sum2.data.sorted.out2`;
open (INPUT, "/var/www/html/honey/attacks/sum2.data.sorted.out2");
while (<INPUT>){
chomp;
($class_c,$number_of_attacks,$number_of_login_attempts)=split(/\s/,$_,3);
if ( -f "/var/www/html/honey/notes/$class_c" ){
$note=`cat /var/www/html/honey/notes/$class_c`;
chomp $note;
}
print "<TR><TD>$class_c $note</TD><TD>$number_of_attacks</TD>";
$number_of_login_attempts=&commify($number_of_login_attempts);
print "<TD>$number_of_login_attempts</TD></TR>\n";
$note="";
}
close (INPUT);
unlink ("/var/www/html/honey/attacks/sum2.data.sorted");
unlink ("/var/www/html/honey/attacks/sum2.data.sorted.out");
unlink ("/var/www/html/honey/attacks/sum2.data.sorted.out2");
if ($DEBUG >0){print "DEBUG Done reading /tmp/all_class_c.$$ at:";$TMP_DATE=`date`; print $TMP_DATE;}
unlink ("/tmp/all_class_c.$$") ;
}
#####################################################################
#
# Main line of code
#
$DEBUG=0;
$YEAR=`date +%Y`;
chomp $YEAR;
$MONTH=`date +%m`;
chomp $MONTH;
chdir ("/var/www/html/honey/attacks/");
if ( "$ARGV[0]" eq "ALL" ){
&all_class_c;
}
else {
&this_month_hall_of_shame;
&this_year_hall_of_shame;
}
unlink ("/tmp/LongTail.hall.of.shame.$$");