]>
Raphaƫl G. Git Repositories - tools/blob - blacklist
e950ba087832414a2920a6ceed8200c5eae933b1
6 use IPC
::System
::Simple
qw(capturex);
7 use Data
::Validate
::IP
qw(is_ipv4 is_ipv6);
8 use NetAddr
::IP
::Util
qw(shiftleft inet_4map6 ipv4to6);
9 use NetAddr
::IP
qw(:nofqdn Ones);
21 #my $iplist = qr/^(?:127\.|::1|2a01:4f8:190:22a6:|5\.9\.143\.173|85\.68\.|81\.67\.|89\.157\.|82\.241\.255\.46)/;
37 '2a01:4f8:191:1405::/64'
41 #Create a new NetAddr::IP object without calling slow gethostbyname (load /etc/resolv.conf)
44 my ($ip, $mask) = split('/', shift);
48 mask
=> !defined($mask)||$mask==32?Ones
:shiftleft
(Ones
, 32 - $mask),
51 #Generate fake address
52 #XXX: NetAddr::IP expect a faked Socket6 gethostbyname struct
53 #XXX: see /usr/lib64/perl5/vendor_perl/NetAddr/IP/Util.pm +235
54 #addr => scalar ($ip, '', AF_INET, 16, NetAddr::IP::Util::inet_4map6(NetAddr::IP::Util::ipv4to6(pack('C4', split('\.', $ip)))))
55 addr
=> inet_4map6
(ipv4to6
(pack('C4', split('\.', $ip))))
57 #Return fake NetAddr::IP object
58 return bless $self, 'NetAddr::IP';
62 my @userlist = ('rapsys');
64 #Extract sshd.service scan
66 # # Extract user and ip
67 # if (/Failed password for (?:invalid user )?(.+) from (.+) port [0-9]+ ssh2/ && grep($_ ne $1, @userlist)) {
70 # # Check if v4 ip and not in whitelist
71 # if (is_ipv4($ip) && not scalar map { my $network = NetAddr::IP->new($_); my $netip = NetAddr::IP->new($ip); unless ($network->contains($netip)) { (); } } @{$iplist{ipv4}}) {
72 # # Add ip in v4 blacklist
75 # } elsif (is_ipv6($ip) && not scalar map { my $network = NetAddr::IP->new($_); my $netip = NetAddr::IP->new($ip); unless ($network->contains($netip)) { (); } } @{$iplist{ipv6}}) {
79 #} capturex('journalctl', '-u', 'sshd.service');
81 #Extract kernel port scan
83 #oct. 04 19:10:30 aurae.aoihime.eu kernel: net-fw DROP IN=enp3s0 OUT= MAC=50:46:5d:a1:a1:85:0c:86:10:f5:c6:4b:08:00 SRC=61.227.52.153 DST=144.76.27.210 LEN=52 TOS=0x00 PREC=0x00 TTL=116 ID=29123 DF PROTO=TCP SPT=64349 DPT=445 WINDOW=8192 RES=0x00 SYN URGP=0
84 if (/kernel: net-fw DROP .* SRC=([^\s]+) .* PROTO=([^\s]+) .* DPT=([^\s]+)/) {
91 # Check if v4 ip and not in whitelist
92 if (is_ipv4
($ip) && not scalar map { my $network = new_ipv4
($_); my $netip = new_ipv4
($ip); unless ($network->contains($netip)) { (); } } @{$iplist{ipv4
}}) {
93 if (!defined $ip4s{$ip}) {
94 %{$ip4s{$ip}} = ('tcp' => {}, 'udp' => {});
96 $ip4s{$ip}{$proto}{$dpt}=1;
97 } elsif (is_ipv6
($ip) && not scalar map { my $network = NetAddr
::IP-
>new($_); my $netip = NetAddr
::IP-
>new($ip); unless ($network->contains($netip)) { (); } } @{$iplist{ipv6
}}) {
98 if (!defined $ip6s{$ip}) {
99 %{$ip6s{$ip}} = ('tcp' => {}, 'udp' => {});
101 $ip6s{$ip}{$proto}{$dpt}=1;
104 #oct. 04 19:17:10 aurae.aoihime.eu kernel: audit: type=1100 audit(1570209430.543:17321294): pid=5890 uid=0 auid=4294967295 ses=4294967295 msg='op=PAM:authentication grantors=? acct="root" exe="/usr/sbin/sshd" hostname=195.154.112.70 addr=195.154.112.70 terminal=ssh res=failed'
105 } elsif (/op=PAM:authentication grantors=\? acct="(.+)" exe="\/usr\
/(?:libexec\/dovecot\
/auth|sbin\/sshd
)" hostname=.+ addr=(.+) terminal=(dovecot|ssh) res=failed/ && grep($_ ne $1, @userlist)) {
111 my $dpt = $3 eq 'ssh' ? 22 : 445;
112 # Check if v4 ip and not in whitelist
113 if (is_ipv4($ip) && not scalar map { my $network = new_ipv4($_); my $netip = new_ipv4($ip); unless ($network->contains($netip)) { (); } } @{$iplist{ipv4}}) {
114 if (!defined $ip4s{$ip}) {
115 %{$ip4s{$ip}} = ('tcp' => {}, 'udp' => {});
117 # Add ip in v4 blacklist
118 $ip4s{$ip}{$proto}{$dpt}=1;
120 } elsif (is_ipv6($ip) && not scalar map { my $network = NetAddr::IP->new($_); my $netip = NetAddr::IP->new($ip); unless ($network->contains($netip)) { (); } } @{$iplist{ipv6}}) {
121 if (!defined $ip6s{$ip}) {
122 %{$ip6s{$ip}} = ('tcp' => {}, 'udp' => {});
124 $ip6s{$ip}{$proto}{$dpt}=1;
126 #nov. 30 15:30:07 aurae.aoihime.eu kernel: audit: type=1100 audit(1575124207.371:38129): pid=685 uid=985 auid=4294967295 ses=4294967295 msg='op=PAM:authentication grantors=? acct="toto
" exe="/usr/bin
/pwauth
" hostname=? addr=? terminal=? res=failed'
127 #XXX: Until mod_authnz pass to pwauth the (SERVER_NAME|SERVER_ADDR) + REMOTE_ADDR+REMOTE_PORT in env it's impossible to know who did a failed auth
128 #XXX: see https://github.com/phokz/mod-auth-external/blob/master/mod_authnz_external/TODO
129 #} elsif (/op=PAM:authentication grantors=\? acct="(.+)" exe="\
/usr\
/bin\/pwauth
" hostname=.+ addr=(.+) terminal=\? res=failed/ && grep($_ ne $1, @userlist)) {
132 } capturex('journalctl', '-m', '-t', 'kernel', '-o', 'cat', '--no-hostname');
134 #Process each ipv4s keys
136 #Set proto as either tcp or udp
137 for my $proto (('tcp', 'udp')) {
138 #Check if branch is empty
139 if (!scalar keys %{$ip4s{$_}{$proto}}) {
141 delete $ip4s{$_}{$proto};
146 #Process each ipv6s keys
148 #Set proto as either tcp or udp
149 for my $proto (('tcp', 'udp')) {
150 #Check if branch is empty
151 if (!scalar keys %{$ip6s{$_}{$proto}}) {
153 delete $ip6s{$_}{$proto};
158 #Open blrule4s file for reading
159 open (my $fh, '<', '/etc/shorewall/blrules') or die "Can
't open < /etc/shorewall/blrules: $!";
161 #Populate with comments
162 @blrule4s = map { chomp($_); if (/^#/) { $_; } else { (); } } <$fh>;
164 #Prepend each specific ip from whitelist
165 map { push @blrule4s, "WHITELIST\tnet:$1\tall" if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/32$/); } @{$iplist{ipv4}};
171 for my $proto (keys %{$ip4s{$_}}) {
173 push @blrule4s, "DROP\t\tnet:".$_.(length($_)<12?"\t":'')."\tfw\t$proto\t".(scalar keys %{$ip4s{$_}{$proto}}>5||defined $ip4s{$_}{$proto}{0}?'#':'').join(",", keys %{$ip4s{$_}{$proto}});
178 close $fh or die "Can't close fh: $!";
180 #Open blrule4s file for writing
181 open ($fh, '>', '/etc/shorewall/blrules') or die "Can't open > /etc/shorewall/blrules: $!";
183 #Inject content of blacklist
184 map { print $fh $_."\n"; } @blrule4s;
187 close $fh or die "Can't close fh: $!";
189 #Print ipv6 to update hash
190 #XXX; right now it don't seems scanned at all...
191 for (sort keys %ip6s) {
193 for my $proto (keys %{$ip6s{$_}}) {
194 #Print the ipv6 scanner
195 print $_."\t$proto\t".join(",", keys %{$ip6s{$_}{$proto}})."\n";
199 # Restart shorewall service
200 capturex
('systemctl', 'restart', 'shorewall.service');