]>
Raphaƫl G. Git Repositories - tools/blob - blacklist
6 use IPC
::System
::Simple
qw(capturex);
7 use Data
::Validate
::IP
qw(is_ipv4 is_ipv6);
29 '2a01:4f8:191:1405::/64'
32 my @userlist = ('rapsys');
34 # Extract sshd.service scan
36 # # Extract user and ip
37 # if (/Failed password for (?:invalid user )?(.+) from (.+) port [0-9]+ ssh2/ && grep($_ ne $1, @userlist)) {
40 # # Check if v4 ip and not in whitelist
41 # if (is_ipv4($ip) && not scalar map { my $network = NetAddr::IP->new($_); my $netip = NetAddr::IP->new($ip); unless ($network->contains($netip)) { (); } } @{$whitelist{ipv4}}) {
42 # # Add ip in v4 blacklist
45 # } elsif (is_ipv6($ip) && not scalar map { my $network = NetAddr::IP->new($_); my $netip = NetAddr::IP->new($ip); unless ($network->contains($netip)) { (); } } @{$whitelist{ipv6}}) {
49 #} capturex('journalctl', '-u', 'sshd.service');
51 # Extract kernel port scan
53 if (/kernel: net-fw DROP .* SRC=([^\s]+) DST=.*/) {
56 # Check if v4 ip and not in whitelist
57 if (is_ipv4
($ip) && not scalar map { my $network = NetAddr
::IP-
>new($_); my $netip = NetAddr
::IP-
>new($ip); unless ($network->contains($netip)) { (); } } @{$whitelist{ipv4
}}) {
59 } elsif (is_ipv6
($ip) && not scalar map { my $network = NetAddr
::IP-
>new($_); my $netip = NetAddr
::IP-
>new($ip); unless ($network->contains($netip)) { (); } } @{$whitelist{ipv6
}}) {
62 } elsif (/op=PAM:authentication grantors=\? acct="(.+)" exe="\/usr\
/(?:libexec\/dovecot\
/auth|sbin\/sshd
)" hostname=.+ addr=(.+) terminal=(?:dovecot|ssh) res=failed/ && grep($_ ne $1, @userlist)) {
65 # Check if v4 ip and not in whitelist
66 if (is_ipv4($ip) && not scalar map { my $network = NetAddr::IP->new($_); my $netip = NetAddr::IP->new($ip); unless ($network->contains($netip)) { (); } } @{$whitelist{ipv4}}) {
67 # Add ip in v4 blacklist
70 } elsif (is_ipv6($ip) && not scalar map { my $network = NetAddr::IP->new($_); my $netip = NetAddr::IP->new($ip); unless ($network->contains($netip)) { (); } } @{$whitelist{ipv6}}) {
74 } capturex('journalctl', '-m', '-t', 'kernel');
76 # Open blrule4s file for reading
77 open (my $fh, '<', '/etc/shorewall/blrules') or die "Can
't open < /etc/shorewall/blrules: $!";
79 # Populate with comments
80 @blrule4s = map { chomp($_); if (/^#/) { $_; } else { (); } } <$fh>;
82 # Prepend each specific ip from whitelist
83 map { push @blrule4s, "WHITELIST\tnet:$1\tall" if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/32$/); } @{$whitelist{ipv4}};
86 map { push @blrule4s, "DROP\t\tnet:".$_.(length lt 12?"\t":'')."\tfw"; } sort keys %ip4s;
89 close $fh or die "Can't
close fh
: $!";
91 # Open blrule4s file for writing
92 open ($fh, '>', '/etc/shorewall/blrules') or die "Can
't open > /etc/shorewall/blrules: $!";
94 # Inject content of blacklist
95 map { print $fh $_."\n"; } @blrule4s;
98 close $fh or die "Can't
close fh
: $!";
100 # Print ipv6 to update hash
101 #XXX; right now it don't seems scanned at all...
102 for (sort keys %ip6s) {
106 # Restart shorewall service
107 capturex('systemctl', 'restart', 'shorewall.service');