-
Notifications
You must be signed in to change notification settings - Fork 0
/
banSaslSpam.pl
162 lines (153 loc) · 5.04 KB
/
banSaslSpam.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
#!/usr/bin/perl
use strict;
use warnings;
# More than this will trigger the banning
# 65,536 IPs in a /16, 65,536 /16s total.
my $threshold16 = 6;
# 255 IPs in a /24
my $threshold24 = 1;
my $flag = $ARGV[0];
my $notice = "no";
$notice = "yes" if (defined $flag && $flag eq "y");
print "Ban SASL spammers (auto: $notice)\n";
print " \033[0;32m".`date +"%H:%M - %m/%d"`."\033[0m";
my @bannedips = get_bans();
if (defined $flag && $flag eq "b") {
print "Current ban list:\n";
foreach my $bip (@bannedips) {
print "$bip\n";
}
exit;
}
my $count = 0;
my @branges;
my @cranges;
my ($p1, $p2, $p3, $p4)=(0)x4;
my ($c1, $c2, $c3, $c4)=(0)x4;
my @ips = get_log_entries();
print "Getting ranges...\n";
my @sorted_ips = sort { $a cmp $b } @ips;
foreach my $ip (@sorted_ips) {
my ($o1, $o2, $o3, $o4) = split(/\./, $ip);
if ($o1 == $p1) { $c1++ } else { $c1 = 0 };
if ($c1 > 0 && $o2 == $p2) { $c2++ }
else {
if ($c2 > $threshold16) {
my $range = "$p1.$p2.0.0/16";
my $matched = grep { /$range/ } @bannedips;
if (!defined $matched || $matched == 0) {
push(@branges, $range);
$count++;
}
}
$c2 = 0;
};
if ($c2 > 0 && $o3 == $p3) { $c3++ }
else {
if ($c3 > $threshold24) {
my $range = "$p1.$p2.$p3.0/24";
my $matched = grep { /$range/ } @bannedips;
my $prange = "$p1.$p2.0.0/16";
my $parent = grep { /$prange/ } @bannedips;
if ((!defined $matched || $matched == 0) && (!defined $parent || $parent == 0)) {
push(@cranges, $range);
$count++;
}
}
$c3 = 0;
};
($p1, $p2, $p3, $p4) = ($o1, $o2, $o3, $o4);
}
my $display_count = "0";
$display_count = "\033[0;31m$count\033[0m" if ($count gt 0);
print " $display_count IP ranges found \n";
foreach my $banrange (@branges) { banit($banrange) }
foreach my $banrange (@cranges) { banit($banrange) }
print "Done.\n";
sub get_bans {
my $banned_count = 0;
my @bannedips;
print "Getting banned list... \n";
my @rawbanned = `/bin/firewall-cmd --list-all | grep 'port="587" protocol="tcp" drop' | sort -V`;
foreach my $banline (@rawbanned) {
if ($banline =~ /source address="(.*?)" /) {
my $bannedip = $1;
push @bannedips, ($bannedip);
$banned_count++;
}
}
print "\033[0;33m $banned_count\033[0m bans\n";
return @bannedips;
}
sub get_log_entries {
my @pfldata;
print "Getting log entries... \n";
@pfldata = `/sbin/pflogsumm /var/log/maillog /var/log/maillog-* --verbose`;
my $logged_count = 0;
my @ips;
foreach my $line (@pfldata) {
if ($line =~ /\[(\d+.\d+.\d+.\d+)\]: SASL (PLAIN|LOGIN) authentication failed:/) {
push @ips, ($1);
$logged_count++;
}
if ($line =~ /does not resolve to address (\d+.\d+.\d+.\d+)/) {
push @ips, ($1);
$logged_count++;
}
}
print "\033[0;33m $logged_count\033[0m entries logged\n";
return @ips;
}
sub banit {
my @ranges = @_;
my $banrange = $ranges[0];
my $input;
if (defined $flag && $flag eq "y") {
$input = $flag
} else {
print " IPs found in range $banrange. Ban it? y/n ";
$input = <>; chomp($input);
}
if ($input eq "y") {
ban_range($banrange);
}
}
sub ban_range {
my @ranges = @_;
my $banrange = $ranges[0];
if (defined $banrange && $banrange =~ /(\d+).(\d+).\d+.0\/(16|24)/) {
my $o1 = $1;
my $o2 = $2;
my $parent;
if ($banrange =~ /\/24/) {
my $prange = "$o1.$o2.0.0/16";
$parent = grep { /^$prange/ } @bannedips;
}
if ($banrange =~ /\/16/) {
my $crange = "$o1.$o2.\\d+.0/24";
my @children = grep { /^$crange$/ } @bannedips;
if (@children) {print " Sub range of \033[0;33m$banrange\033[0m already banned...\n";}
foreach my $child (@children) {
print " - Unbanning $child from port 25...";
`firewall-cmd --remove-rich-rule='rule family=ipv4 port port="25" protocol="tcp" source address=$child drop'`;
`firewall-cmd --remove-rich-rule='rule family=ipv4 port port="25" protocol="tcp" source address=$child drop' --permanent`;
print " and port 587.\n";
`firewall-cmd --remove-rich-rule='rule family=ipv4 port port="587" protocol="tcp" source address=$child drop'`;
`firewall-cmd --remove-rich-rule='rule family=ipv4 port port="587" protocol="tcp" source address=$child drop' --permanent`;
}
}
if (defined $parent && $parent > 0) {
print " Parent of $banrange was banned, skipping\n";
} else {
print " + Banning $banrange from port 25...";
`firewall-cmd --add-rich-rule='rule family=ipv4 port port="25" protocol="tcp" source address=$banrange drop'`;
`firewall-cmd --add-rich-rule='rule family=ipv4 port port="25" protocol="tcp" source address=$banrange drop' --permanent`;
print " and port 587.\n";
`firewall-cmd --add-rich-rule='rule family=ipv4 port port="587" protocol="tcp" source address=$banrange drop'`;
`firewall-cmd --add-rich-rule='rule family=ipv4 port port="587" protocol="tcp" source address=$banrange drop' --permanent`;
push @bannedips, ($banrange);
}
} else {
print "\033[0;33m Invalid range \033[0m\n";
}
}