forked from wedaa/LongTail-Log-Analysis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LongTail_add_country_to_ip.pl
executable file
·160 lines (150 loc) · 4.02 KB
/
LongTail_add_country_to_ip.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
#!/usr/bin/perl
# SAMPLE INPUT
# 213 117.21.174.111
# 2801 119.147.84.181
sub init {
# if ($0) {
# print "DEBUG found an arg --> $0\n";
# }
# else {
# print "DEBUG no arg found\n";
# }
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);
}
}
# <A HREF="/honey/ip_attacks.shtml#109.161.137.122">109.161.137.122</A>
&init;
while (<>){
chomp;
($trash,$count,$ip)=split (/\s+/,$_,3);
if ( ($count eq "") && ($ip eq "") ) {
#print "DEBUG Only one field, must be ip\n";
$ip = $trash;
}
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})$/){
$tmp_country_code=`/usr/local/etc/whois.pl $ip`;
chomp $tmp_country_code;
($trash,$country)=split(/ /,$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";
}
}