-
Notifications
You must be signed in to change notification settings - Fork 10
/
LongTail_list_accounts_by_ip.perl
executable file
·289 lines (268 loc) · 8.34 KB
/
LongTail_list_accounts_by_ip.perl
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
#!/usr/bin/perl
######################################################################
#
# Written by: Eric Wedaa
# Perl version started: 2016-10-16
# Last updated on: 2016-10-16
# Purpose: This script creates webpages that list the accounts
# attempted by each IP address. This initial version
# runs outside of the LongTail.sh Version 1 program, but will
# be folded inside of the Version 2 code (which is all Perl).
#
# Initial notes:
# cat /var/log/messages |grep sshd |grep IP: |awk '{print $5,$8}' |sort |uniq -c |sort -k2V
#
# Please see show_help routine (immediately below) for usage instructions
#
######################################################################
sub show_help {
print "mandatory arguments are -i <filename> -o <filename> -t <directoryName>\n";
print "where -i is input file, -o is output file, and -t is a directory to store temporary files and a sort location\n";
print "Minimal checking is done in this version, as it will eventually\n";
print "be folded into LongTail.pl\n\n";
print "\n";
print "Input file is assumed to be the temp file created by longtail, there is NO ERROR CHECKING DONE ON INPUT\n";
print "Filenames must be fully qualified, such as /tmp/LongTail.$$, not just LongTail.$$\n";
exit 0;
}
sub init_ip_to_country{
open ("FILE", "/usr/local/etc/translate_country_codes")|| die "Can not open /usr/local/etc/translate_country_codes\nExiting now\n";
while (<FILE>){
chomp;
$_ =~ s/ / /g;
($code,$country)=split (/ /,$_,2);
$country =~ s/ /_/g;
$country_code{$code}=$country;
}
close (FILE);
open ("FILE", "/usr/local/etc/ip-to-country")|| die "Can not open /usr/local/etc/ip-to-country\nExiting now\n";
while (<FILE>){
chomp;
$_ =~ s/ / /g;
($ip,$country)=split (/ /,$_,2);
$ip_to_country{$ip}=$country;
}
close (FILE);
open ("FILE", "/usr/local/etc/LongTail_sshPsycho_IP_addresses")|| die "Can not open /usr/local/etc/LongTail_sshPsycho_IP_addresses\nExiting now\n";
while (<FILE>){
chomp;
$ssh_psycho{$_}=$_;
($ip1,$ip2,$ip3,$ip4)=split(/\./,$_);
if ( $ip4 eq ""){
$counter=1;
while ($counter <= 255){
$tmp="$_.$counter";
$ssh_psycho{$tmp}="$_.$counter";
$counter++;
}
}
}
close (FILE);
open ("FILE", "/usr/local/etc/LongTail_sshPsycho_2_IP_addresses")|| die "Can not open /usr/local/etc/LongTail_sshPsycho_IP_addresses\nExiting now\n";
while (<FILE>){
chomp;
$ssh_psycho2{$_}=$_;
($ip1,$ip2,$ip3,$ip4)=split(/\./,$_);
if ( $ip4 eq ""){
$counter=1;
while ($counter <= 255){
$tmp="$_.$counter";
$ssh_psycho2{$tmp}="$_.$counter";
$counter++;
}
}
}
close (FILE);
open ("FILE", "/usr/local/etc/LongTail_friends_of_sshPsycho_IP_addresses")|| die "Can not open /usr/local/etc/LongTail_friends_of_sshPsycho_IP_addresses\nExiting now\n";
while (<FILE>){
chomp;
$ssh_psycho_friends{$_}=$_;
($ip1,$ip2,$ip3,$ip4)=split(/\./,$_);
if ( $ip4 eq ""){
$counter=1;
while ($counter <= 255){
$tmp="$_.$counter";
$ssh_psycho_friends{$tmp}="$_.$counter";
$counter++;
}
}
}
close (FILE);
open ("FILE", "/usr/local/etc/LongTail_associates_of_sshPsycho_IP_addresses")|| die "Can not open /usr/local/etc/LongTail_associates_of_sshPsycho_IP_addresses\nExiting now\n";
while (<FILE>){
chomp;
$ssh_psycho_associates{$_}=$_;
($ip1,$ip2,$ip3,$ip4)=split(/\./,$_);
if ( $ip4 eq ""){
$counter=1;
while ($counter <= 255){
$tmp="$_.$counter";
$ssh_psycho_associates{$tmp}="$_.$counter";
$counter++;
}
}
}
close (FILE);
if ( -d "/usr/local/etc/LongTail_botnets"){
open (FIND, "find /usr/local/etc/LongTail_botnets -type f -print|sort |") || die "Can not run find command\n";
while (<FIND>){
chomp;
if (/.sh$/){next;}
if (/.pl$/){next;}
if (/.html/){next;}
if (/.shtml/){next;}
if (/\.201\d\./){next;}
if (/\.202\d\./){next;}
if (/backups/){next;}
if (/sshPsycho/){next;}
$filename=$_;
$botnet_name=`basename $filename`;
chomp $botnet_name;
open (FILE, "$_");
while (<FILE>){
chomp;
$ip=$_;
if (/\.\.\./){next;}
#if ($ssh_psycho_friends{$ip} ){ $tag="sshPsycho_Friend"; }
$botnets{$ip}=$botnet_name;
}
close (FILE);
}
close (FIND);
}
}
######################################################################
sub process_args {
#
# My processing of arguments sucks and needs to be fixed
#
if ( @ARGV == 0 ){
print "No parameters passed, exiting now\n";
exit 1;
}
else{
while ( @ARGV > 0 ) {
if (( $ARGV[0] eq "-h" ) || ( $ARGV[0] eq "-help" )) {
&show_help;
exit 0;
}
if ( $ARGV[0] eq "-i" ) {
shift @ARGV;;
$INPUT_FILE=$ARGV[0];
if ( -e "$INPUT_FILE" ) {
}
else {
print "Input file you specified does not exist, exiting now\n";
exit 1;
}
shift @ARGV;;
next;
}
if ( $ARGV[0] eq "-o" ) {
shift @ARGV;;
$OUTPUT_FILE=$ARGV[0];
# I am assuming that I can write to the file passed to me :-)
shift @ARGV;;
next;
}
if ( $ARGV[0] eq "-t" ) {
shift @ARGV;;
$TMPDIR=$ARGV[0];
# I am assuming that I can write to the file passed to me :-)
shift @ARGV;;
if ( ! -d $TMPDIR ){
print "-t $TMPDIR is not a directory, exiting now\n";
exit 1;
}
next;
}
print "Bad option, exiting now";
exit 1;
} #end of while
} # end of else
# Make sure all the options were passed to me
if ( "X$INPUT_FILE" eq "X"){
print "You didn't pass an input file, exiting now\n";
exit;
}
if ( "X$OUTPUT_FILE" eq "X"){
print "You didn't pass an output file, exiting now\n";
exit;
}
if ( "X$TMPDIR" eq "X"){
print "You didn't pass an temporary directory for sorting and output files, exiting now\n";
exit;
}
}
######################################################################
sub list_accounts_by_ip {
#
# input line looks like this:
# 2016-10-16T03:45:04-04:00 syrtest sshd-22[1636]: IP: 116.31.116.40 PassLog: Username: root Password: metallica
#
#open (FILE, $INPUT_FILE)|| die "can not open $INPUT_FILE\n";
open (FILE, "/usr/local/etc/catall.sh $INPUT_FILE|")|| die "can not open $INPUT_FILE\n";
open (OUT, ">$TMPDIR/LongTail.$$")|| die "Can not write to $TMPDIR/LongTail.$$\n";
while (<FILE>){
chomp;
@words=split(/ /,$_,10);
print (OUT "$words[4] $words[7]\n");
}
close (FILE);
close (OUT);
`sort -V $TMPDIR/LongTail.$$ > $TMPDIR/LongTail.$$.sorted`;
unlink ("$TMPDIR/LongTail.$$");
`uniq -c $TMPDIR/LongTail.$$.sorted > $TMPDIR/LongTail.$$.uniqed`;
unlink ("$TMPDIR/LongTail.$$.sorted");
open (FILE, "$TMPDIR/LongTail.$$.uniqed") || die "This is wierd, can not open $TMPDIR/LongTail.$$.uniqed\n";
open (OUTFILE, ">>$OUTPUT_FILE") || die "This is wierd, can not open $OUTPUT_FILE\n";
while (<FILE>){
chomp;
@words=split(/\s+/,$_,4);
$ip=$words[2];
if (defined($ip_to_country{$ip})){
$tmp_country_code=$ip_to_country{$ip};
}
else {
if ($ip =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/){
if ($DEBUG >0){print "Calling /usr/local/etc/whois.pl $ip now\n";}
#print (STDERR "ip is $ip\n");
$tmp_country_code=`/usr/local/etc/whois.pl $ip`;
chomp $tmp_country_code;
if ($DEBUG >0){print "tmp_country_code is -->$tmp_country_code<--\n";}
#$ip_to_country{$ip}=1;
($trash,$tmp_country_code)=split(/ /,$tmp_country_code);
if ($DEBUG >0){print "tmp_country_code NOW is -->$tmp_country_code<--\n";}
$ip_to_country{$ip}=$tmp_country_code
}
}
$tmp_country_code=~ tr/A-Z/a-z/;
#$_ =~ s/$ip/$ip $country_code{$tmp_country_code}/;
$tag="";
if ($ssh_psycho{$ip} ){ $tag="sshPsycho"; }
if ($ssh_psycho2{$ip} ){ $tag="sshPsycho-2"; }
if ($ssh_psycho_associates{$ip} ){ $tag="sshPsycho_Associate"; }
if ($ssh_psycho_friends{$ip} ){ $tag="sshPsycho_Friend"; }
if ($botnets{$ip}) { $tag=$botnets{$ip};}
#if ($tag ne ""){
# print "$_($tag)\n";
#}
#else {
# print "$_\n";
#}
print (OUTFILE "<TR><TD>$words[2]</TD><TD>$country_code{$tmp_country_code} $tag</TD><TD>$words[1]</TD><TD>$words[3]</TD></TR>\n");
}
close (FILE);
close (OUTFILE);
unlink ("$TMPDIR/LongTail.$$.uniqued");
}
######################################################################
$DEBUG=0;
if ($DEBUG >0 ){print "calling process_args now\n";}
&process_args;
if ($DEBUG >0 ){print "calling init_ip_to_country now\n";}
&init_ip_to_country;
#print "Input file is $INPUT_FILE\n";
#print "output file is $OUTPUT_FILE\n";
if ($DEBUG >0 ){print "calling list_accounts_by_ip now\n";}
&list_accounts_by_ip;