annotate maltfilter @ 58:a780a23e19a8

Change parsing status log messages.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 16 Aug 2009 22:17:48 +0300
parents a70493b6c916
children 69c39b5c6277
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 #!/usr/bin/perl -w
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 #############################################################################
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 #
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 # Malicious Attack Livid Termination Filter daemon (maltfilter)
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 # Programmed by Matti 'ccr' Hämäläinen <ccr@tnsp.org>
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 # (C) Copyright 2009 Tecnic Software productions (TNSP)
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 #
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #############################################################################
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 use strict;
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 use Date::Parse;
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 use Net::IP;
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12
57
a70493b6c916 Clear %statlist and %ignorelist when re-initializing (due to HUP), so we don't count stats twice.
Matti Hamalainen <ccr@tnsp.org>
parents: 55
diff changeset
13 my $progversion = "0.13.1";
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 my $progbanner =
11
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
15 "Malicious Attack Livid Termination Filter daemon (maltfilter) v$progversion\n".
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 "Programmed by Matti 'ccr' Hamalainen <ccr\@tnsp.org>\n".
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 "(C) Copyright 2009 Tecnic Software productions (TNSP)\n";
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 #############################################################################
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
20 ### Default settings and configuration
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 #############################################################################
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 my %settings = (
11
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
23 "VERBOSITY" => 3,
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 "DRY_RUN" => 1,
26
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
25 "WEED_BLOCK" => 168,
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
26 "WEED_GLOBAL" => 336,
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 "TRESHOLD" => 3,
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 "ACTION" => "DROP",
11
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
29 "LOGFILE" => "",
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 "IPTABLES" => "/sbin/iptables",
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31
11
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
32 "STATUS_FILE_PLAIN" => "",
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
33 "STATUS_FILE_HTML" => "",
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
34 "STATUS_FILE_CSS" => "",
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
35
17
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
36 "WHOIS_URL" => "http://whois.domaintools.com/",
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
37
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 "CHK_SSHD" => 1,
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 "CHK_KNOWN_CGI" => 1,
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 "CHK_PHP_XSS" => 1,
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 "CHK_PROXY_SCAN" => 1,
4
b2c7c76b3529 Added scanning feature for SSH root login attempts with failed passwords.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
42 "CHK_ROOT_SSH_PWD" => 0,
39
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
43 "CHK_SYSACCT_SSH_PWD" => 0,
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 "CHK_GOOD_HOSTS" => "",
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
45
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
46 "SYSACCT_MIN_UID" => 1,
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
47 "SYSACCT_MAX_UID" => 100,
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
48
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
49 "FULL_TIME" => 1,
44
471731c79bb3 Add configuration setting for PASSWD file.
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
50
471731c79bb3 Add configuration setting for PASSWD file.
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
51 "PASSWD" => "/etc/passwd",
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 );
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53
7
ee5f7b8dcdea Features, yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
54 my @noblock_ips_def = (
ee5f7b8dcdea Features, yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
55 "127.0.0.1",
ee5f7b8dcdea Features, yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
56 );
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
58 my %systemacct = ();
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
59
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 #############################################################################
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
61 ### Check given logfile line for matches
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
62 #############################################################################
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
63 sub check_log_line($)
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
64 {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
65 # (1) SSHD scans
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
66 if (/^(\S+\s+\d+\s+\d\d:\d\d:\d\d)\s+\S+\s+sshd\S*?: (.*)/) {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
67 my $mdate = $1;
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
68 my $merr = $2;
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
69
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
70 # (1.1) Generic login scan attempts
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
71 if ($merr =~ /^Failed password for invalid user (\S+) from (\d+\.\d+\.\d+\.\d+)/) {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
72 check_add_hit($2, $mdate, "SSH login scan", "", $settings{"CHK_SSHD"});
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
73 }
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
74 # (1.2) Root account SSH login password bruteforcing attempts.
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
75 elsif (/^Failed password for root from (\d+\.\d+\.\d+\.\d+)/) {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
76 check_add_hit($1, $mdate, "Root SSH password bruteforce", "", $settings{"CHK_ROOT_SSH_PWD"});
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
77 }
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
78 # (1.3) System account SSH login password bruteforcing attempts.
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
79 if ($merr =~ /^Failed password for (\S+) from (\d+\.\d+\.\d+\.\d+)/) {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
80 my $mip = $2; my $macct = $1;
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
81 if (defined($systemacct{$macct})) {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
82 check_add_hit($mip, $mdate, "SSH system account bruteforce", $macct, $settings{"CHK_SYSACCT_SSH_PWD"});
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
83 }
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
84 }
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
85 }
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
86 # (2) Common/known vulnerable CGI/PHP software scans (like phpMyAdmin)
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
87 elsif (/^\[(.+?)\]\s+\[error\]\s+\[client\s+(\d+\.\d+\.\d+\.\d+)\]\s+(.+)$/) {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
88 my $mdate = $1;
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
89 my $mip = $2;
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
90 my $merr = $3;
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
91 if ($merr =~ /^File does not exist: (.+)$/) {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
92 my $tmp = $1;
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
93 if ($tmp =~ /\/mss2|\/pma|admin|sql|\/roundcube|\/webmail|\/bin|\/mail|xampp|zen|mailto:|appserv|cube|round|_vti_bin|wiki/i) {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
94 check_add_hit($mip, $mdate, "CGI vuln scan", $tmp, $settings{"CHK_KNOWN_CGI"});
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
95 }
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
96 }
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
97 }
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
98 # (3) Apache common logging format checks
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
99 elsif (/(\d+\.\d+\.\d+\.\d+)\s+-\s+-\s+\[(.+?)\]\s+\"GET (\S*?) HTTP\//) {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
100 my $mdate = $2;
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
101 my $mip = $1;
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
102 my $merr = $3;
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
103
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
104 # (3.1) Simple match for generic PHP XSS vulnerability scans
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
105 if ($merr =~ /\.php\?\S*?=http:\/\/([^\/]+)/) {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
106 if (!check_hosts($settings{"CHK_GOOD_HOSTS"}, $1)) {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
107 check_add_hit($mip, $mdate, "PHP XSS", $merr, $settings{"CHK_PHP_XSS"});
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
108 }
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
109 }
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
110 # (3.2) Try to match proxy scanning attempts
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
111 elsif ($merr =~ /^http:\/\/([^\/]+)/) {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
112 if (!check_hosts($settings{"CHK_GOOD_HOSTS"}, $1)) {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
113 check_add_hit($mip, $mdate, "Proxy scan", $merr, $settings{"CHK_PROXY_SCAN"});
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
114 }
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
115 }
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
116 }
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
117 }
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
118
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
119
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
120 #############################################################################
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
121 ### Global variables
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 #############################################################################
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
123 my $reportmode = 0; # Full report mode
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
124 my @scanfiles = (); # Files to scan
54
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
125 my @scanfiles_once = (); # Files to scan only once during startup or HUP (e.g. not continuously followed)
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
126 my @noblock_ips = (); # IPs not to block
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
127 my %filehandles = (); # Global hash holding opened scanned log filehandles
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
128 my $pid_file = ""; # Name of Maltfilter daemon pid file
39
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
129 my @configfiles = (); # Array of configuration file names
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
130 my $LOGFILE; # Maltfilter logfile handle
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
131
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
132 # IPs currently blocked in Netfilter $blocklist{$ip} = date
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
133 my %blocklist = ();
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
134
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
135 # Gathered information about hosts
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
136 # $statlist{$ip}->
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
137 # "date1" = timestamp of first hit
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
138 # "date2" = timestamp of latest hit
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
139 # "hits" = number of hits to this IP
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
140 # $statlist{$ip}{"reason"}{$class}->
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
141 # "msg" = reason message (array if $reportmode)
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
142 # "hits" = hits to this class
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
143 # "date1" = timestamp of first hit
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
144 # "date2" = timestamp of latest hit
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
145 my %statlist = ();
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
146
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
147 # Gathered information about ignored hits (e.g. hits for tests that are not enabled)
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
148 # Same fields as in %statlist
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
149 my %ignorelist = ();
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
150
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151
2
3da95f3082d9 Misc. variable name cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
152 #############################################################################
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
153 ### Status output functionality
2
3da95f3082d9 Misc. variable name cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
154 #############################################################################
17
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
155 sub urlencode($)
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
156 {
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
157 my $value = $_[0];
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
158 $value =~ s/([^a-zA-Z_0-9 ])/"%" . uc(sprintf "%lx" , unpack("C", $1))/eg;
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
159 $value =~ tr/ /+/;
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
160 return $value;
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
161 }
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
162
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
163 my %entities = (
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
164 "<" => "lt",
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
165 ">" => "gt",
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
166 "&" => "amp",
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
167 );
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
168
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
169 sub htmlentities($)
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
170 {
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
171 my $value = $_[0];
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
172 # $value =~ s/([keys %entities])/"&".$entities{$1}.";"/eg;
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
173 foreach my $val (keys %entities) {
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
174 $value =~ s/$val/\&$entities{$val}\;/g;
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
175 }
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
176 return $value;
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
177 }
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
178
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
179 sub get_time_str($)
34
e4ffe2ce29a3 Generalize timestamp conversion to strings; Re-enable setting of timestamps in
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
180 {
e4ffe2ce29a3 Generalize timestamp conversion to strings; Re-enable setting of timestamps in
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
181 if ($_[0] >= 0) {
e4ffe2ce29a3 Generalize timestamp conversion to strings; Re-enable setting of timestamps in
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
182 return scalar localtime($_[0]);
e4ffe2ce29a3 Generalize timestamp conversion to strings; Re-enable setting of timestamps in
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
183 } else {
e4ffe2ce29a3 Generalize timestamp conversion to strings; Re-enable setting of timestamps in
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
184 return "?";
e4ffe2ce29a3 Generalize timestamp conversion to strings; Re-enable setting of timestamps in
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
185 }
e4ffe2ce29a3 Generalize timestamp conversion to strings; Re-enable setting of timestamps in
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
186 }
e4ffe2ce29a3 Generalize timestamp conversion to strings; Re-enable setting of timestamps in
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
187
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
188 my @paskat = (30*24*60*60, 7*24*60*60, 24*60*60, 60*60, 60);
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
189 my @opaskat = ("months", "weeks", "days", "hours", "minutes");
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
190 my @upaskat = ("month", "week", "day", "hour", "minute");
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
191
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
192 sub get_ago_str($)
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
193 {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
194 return get_time_str($_[0]) if ($settings{"FULL_TIME"});
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
195 if ($_[0] >= 0) {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
196 my $str = "";
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
197 my $cur = time() - $_[0];
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
198 my ($r, $k, $p, $n);
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
199 $n = 0;
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
200 foreach my $div (@paskat) {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
201 $r = int($cur / $div);
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
202 $k = ($cur % $div);
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
203 if ($r > 0) {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
204 $p = ($r > 1) ? $opaskat[$n] : $upaskat[$n];
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
205 $str .= ", " if ($str ne "");
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
206 $str .= sprintf("%d %s", $r, $p);
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
207 }
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
208 $cur = $k;
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
209 $n++;
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
210 }
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
211 return $str." ago";
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
212 } else {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
213 return "?";
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
214 }
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
215 }
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
216
11
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
217 sub printH($$$$)
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
218 {
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
219 my $fh = $_[1];
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
220 if ($_[0]) {
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
221 print $fh "<h".$_[2].">".$_[3]."</h".$_[2].">\n";
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
222 } else {
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
223 my $c = ($_[2] <= 1) ? "=" : "-";
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
224 print $fh $_[3]."\n". $c x length($_[3]) ."\n";
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
225 }
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
226 }
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
227
32
e7e484c89dbc Added highlighting of blocked entries in summary tables.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
228 sub printTD
11
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
229 {
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
230 my $fh = $_[1];
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
231 if ($_[0]) {
52
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
232 my $s = defined($_[3]) ? " ".$_[3]." " : "";
32
e7e484c89dbc Added highlighting of blocked entries in summary tables.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
233 print $fh "<td".$s.">".$_[2]."</td>";
11
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
234 } else {
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
235 print $fh $_[2];
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
236 }
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
237 }
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
238
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
239 sub printP($$$)
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
240 {
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
241 my $fh = $_[1];
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
242 if ($_[0]) {
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
243 print $fh "<p>\n".$_[2]."</p>\n";
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
244 } else {
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
245 print $fh $_[2]."\n";
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
246 }
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
247 }
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
248
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
249 sub printElem
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
250 {
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
251 my $fh = $_[1];
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
252 if ($_[0]) {
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
253 print $fh $_[2];
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
254 } elsif (defined($_[3])) {
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
255 print $fh $_[3];
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
256 }
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
257 }
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
258
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
259 sub bb($)
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
260 {
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
261 return $_[0] ? "<b>" : "";
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
262 }
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
263
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
264 sub eb($)
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
265 {
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
266 return $_[0] ? "</b>" : "";
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
267 }
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
268
13
fc053b001027 Improved reporting and documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
269 sub pe($$)
fc053b001027 Improved reporting and documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
270 {
fc053b001027 Improved reporting and documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
271 return $_[0] ? "<$_[1]>" : "";
fc053b001027 Improved reporting and documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
272 }
fc053b001027 Improved reporting and documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
273
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
274 sub get_link($$)
13
fc053b001027 Improved reporting and documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
275 {
17
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
276 if ($settings{"WHOIS_URL"} ne "") {
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
277 return $_[0] ? "<a href=\"".$settings{"WHOIS_URL"}.$_[1].
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
278 "\">".htmlentities($_[1])."</a>" : $_[1];
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
279 } else {
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
280 return $_[0];
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
281 }
13
fc053b001027 Improved reporting and documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
282 }
fc053b001027 Improved reporting and documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
283
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
284 sub print_table1($$$$$$)
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
285 {
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
286 my ($m, $f, $table, $keys, $func, $class) = @_;
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
287 my $ntotal = 0;
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
288
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
289 printElem($m, $f,
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
290 "<table class=\"".$class."\">\n".
17
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
291 "<tr><th>Hits</th><th>IP-address</th><th>First hit</th><th>Latest hit</th><th>Reason(s)</th></tr>\n",
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
292
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
293 "Hits | IP-address | First hit | Latest hit | Reason(s)\n"
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
294 );
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
295
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
296 foreach my $mip (sort { $func->($table, $a, $b) } keys %{$keys}) {
32
e7e484c89dbc Added highlighting of blocked entries in summary tables.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
297 my $blocked = defined($blocklist{$mip}) ? "blocked" : "unblocked";
e7e484c89dbc Added highlighting of blocked entries in summary tables.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
298 printElem($m, $f, " <tr class=\"$blocked\">");
17
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
299 printTD($m, $f, sprintf(bb($m)."%-10d".eb($m), $table->{$mip}{"hits"}));
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
300 printElem(!$m, $f, " | ");
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
301 printTD($m, $f, sprintf("%-15s", get_link($m, $mip)));
17
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
302 printElem(!$m, $f, " | ");
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
303 printTD($m, $f, get_ago_str($table->{$mip}{"date1"}));
17
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
304 printElem(!$m, $f, " | ");
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
305 printTD($m, $f, get_ago_str($table->{$mip}{"date2"}));
17
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
306 printElem(!$m, $f, " | ");
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
307 my @reasons = ();
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
308 foreach my $class (sort keys %{$table->{$mip}{"reason"}}) {
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
309 my $msgs;
18
b0017a324040 Cleanups; Disable weeding in report mode again; Don't display redundant IPTABLES reasons in blocklist report.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
310 if ($class ne "IPTABLES") {
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
311 if ($reportmode) {
18
b0017a324040 Cleanups; Disable weeding in report mode again; Don't display redundant IPTABLES reasons in blocklist report.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
312 my @tmp = reverse(@{$table->{$mip}{"reason"}{$class}{"msg"}});
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
313 if ($#tmp > 5) { $#tmp = 5; }
17
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
314 foreach (@tmp) { $_ = htmlentities($_); }
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
315 $msgs = join(" ".bb($m)."|".eb($m)." ", @tmp);
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
316 } else {
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
317 $msgs = $table->{$mip}{"reason"}{$class}{"msg"};
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
318 }
17
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
319 push(@reasons, bb($m).$class.eb($m)." #".$table->{$mip}{"reason"}{$class}{"hits"}.
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
320 " ( ".$msgs." )");
18
b0017a324040 Cleanups; Disable weeding in report mode again; Don't display redundant IPTABLES reasons in blocklist report.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
321 }
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
322 }
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
323 printTD($m, $f, join(", ", @reasons));
17
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
324 printElem($m, $f, "</tr>\n", "\n");
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
325 $ntotal++;
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
326 }
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
327 printElem($m, $f, "</table>\n");
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
328 printP($m, $f, bb($m).$ntotal.eb($m)." entries total.\n");
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
329 }
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
330
52
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
331 sub cmp_ips($$$)
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
332 {
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
333 my @ipa = split(/\./, $_[1]);
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
334 my @ipb = split(/\./, $_[2]);
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
335 for (my $i = 0; $i < 4; $i++) {
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
336 return -1 if ($ipa[$i] > $ipb[$i]);
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
337 return 1 if ($ipa[$i] < $ipb[$i]);
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
338 }
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
339 return 0;
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
340 }
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
341
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
342 sub test_ips($$)
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
343 {
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
344 my @ipa = split(/\./, $_[0]);
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
345 my @ipb = split(/\./, $_[1]);
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
346 for (my $i = 0; $i < 3; $i++) {
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
347 return $i if ($ipa[$i] != $ipb[$i]);
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
348 }
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
349 return 4;
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
350 }
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
351
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
352 my @ipcolors = (
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
353 "#666",
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
354 "#777",
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
355 );
17
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
356
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
357 sub print_table2($$$$$$)
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
358 {
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
359 my ($m, $f, $table, $keys, $func, $class) = @_;
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
360 my $nhits = 0;
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
361 my $str = "<th>IP-address</th><th>Hits</th><th>First hit</th><th>Latest hit</th><th>Class</th>";
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
362 my $str2 = "IP-address | Hits | First hit | Latest hit | Class ";
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
363
17
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
364 printElem($m, $f,
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
365 "<table class=\"".$class."\">\n<tr>". $str."<th> </th>".$str ."</tr>\n",
17
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
366 $str2." || ".$str2."\n");
52
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
367
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
368 my @previp = ("0.0.0.0", "0.0.0.0");
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
369 my @ncolor = (0, 0);
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
370
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
371 my $printEntry = sub {
52
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
372 my $blocked = "class=\"".(defined($blocklist{$_[0]}) ? "blocked" : "unblocked")."\"";
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
373 if (test_ips($previp[$_[1]], $_[0]) < 3) {
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
374 $ncolor[$_[1]]++;
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
375 }
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
376 $previp[$_[1]] = $_[0];
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
377 my $str = "style=\"background: ".$ipcolors[$ncolor[$_[1]] % scalar @ipcolors].";\"";
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
378
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
379 printTD($m, $f, sprintf("%-15s", get_link($m, $_[0])), $str);
17
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
380 printElem(!$m, $f, " | ");
32
e7e484c89dbc Added highlighting of blocked entries in summary tables.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
381 printTD($m, $f, sprintf("%-8d ", $table->{$_[0]}{"hits"}), $blocked);
17
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
382 printElem(!$m, $f, " | ");
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
383 printTD($m, $f, get_ago_str($table->{$_[0]}{"date1"}), $blocked);
17
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
384 printElem(!$m, $f, " | ");
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
385 printTD($m, $f, get_ago_str($table->{$_[0]}{"date2"}), $blocked);
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
386 printElem(!$m, $f, " | ");
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
387 my $tmp = join(", ", sort keys %{$table->{$_[0]}{"reason"}});
32
e7e484c89dbc Added highlighting of blocked entries in summary tables.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
388 printTD($m, $f, sprintf("%-30s", $tmp), $blocked);
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
389 $nhits += $table->{$_[0]}{"hits"};
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
390 };
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
391
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
392 my @mkeys = sort { $func->($table, $a, $b) } keys %{$keys};
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
393 my $nkeys = scalar @mkeys;
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
394 my $kmax = $nkeys / 2;
52
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
395
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
396 for (my $i = 0; $i <= $kmax; $i++) {
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
397 printElem($m, $f, " <tr>");
18
b0017a324040 Cleanups; Disable weeding in report mode again; Don't display redundant IPTABLES reasons in blocklist report.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
398 if ($i < $kmax) {
52
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
399 $printEntry->($mkeys[$i], 0);
18
b0017a324040 Cleanups; Disable weeding in report mode again; Don't display redundant IPTABLES reasons in blocklist report.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
400 printElem($m, $f, "<th> </th>", " || ");
b0017a324040 Cleanups; Disable weeding in report mode again; Don't display redundant IPTABLES reasons in blocklist report.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
401 }
52
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
402 if ($i + $kmax + 1 < $nkeys) { $printEntry->($mkeys[$i + $kmax + 1], 1); }
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
403 printElem($m, $f, "</tr>\n", "\n");
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
404 }
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
405
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
406 printElem($m, $f, "</table>\n");
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
407 printP($m, $f, bb($m).$nkeys.eb($m)." entries total, ".bb($m).$nhits.eb($m)." hits total.\n");
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
408 }
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
409
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
410 sub cmp_hits($$$)
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
411 {
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
412 return $_[0]->{$_[2]}{"hits"} <=> $_[0]->{$_[1]}{"hits"};
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
413 }
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
414
26
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
415 sub get_period($)
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
416 {
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
417 my ($str, $r, $k);
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
418 if ($_[0] > 30 * 24) {
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
419 $r = $_[0] / (30 * 24);
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
420 $k = $_[0] % (30 * 24);
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
421 $str = sprintf("%d months", $r);
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
422 $str .= sprintf(", %d days", $k) if ($k > 0);
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
423 } elsif ($_[0] > 24 * 7) {
29
6d3e33e9ee9b Oops, fix printing of weeks.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
424 $str = sprintf("%1.1f weeks", $_[0] / (24.0 * 7.0));
26
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
425 } elsif ($_[0] > 24) {
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
426 $r = $_[0] / 24;
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
427 $k = $_[0] % 24;
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
428 $str = sprintf("%d days", $r);
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
429 $str .= sprintf(", %d hours", $k) if ($k > 0);
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
430 } else {
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
431 $str = sprintf("%d hours", $_[0]);
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
432 }
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
433 return $str;
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
434 }
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
435
11
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
436 sub generate_status($$)
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
437 {
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
438 my $filename = shift;
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
439 my $m = shift;
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
440
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
441 return unless ($filename ne "");
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
442
39
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
443 open(STATUS, ">", $filename) or mdie("Could not open '".$filename."'!\n");
11
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
444 my $f = \*STATUS;
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
445
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
446 printElem($m, $f, "
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
447 <html>
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
448 <head>
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
449 <title>Maltfilter status report</title>
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
450 ");
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
451
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
452 printElem($m, $f, "<link href=\"".$settings{"STATUS_FILE_CSS"}."\" rel=\"stylesheet\" type=\"text/css\" />")
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
453 if ($settings{"STATUS_FILE_CSS"});
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
454
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
455 printElem($m, $f, "
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
456 </head>
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
457 <body>
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
458 ");
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
459
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
460 printH($m, $f, 1, "Maltfilter v$progversion status report");
26
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
461 my $period = get_period($settings{"WEED_GLOBAL"});
11
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
462
13
fc053b001027 Improved reporting and documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
463 printP($m, $f,
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
464 "Generated ".bb($m).get_time_str(time()).eb($m).". Data computed from ".
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
465 ($reportmode ? "complete logfile scan" : "a period of last $period").".\n");
26
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
466
18
b0017a324040 Cleanups; Disable weeding in report mode again; Don't display redundant IPTABLES reasons in blocklist report.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
467 printP($m, $f, "The hit classes marked as 'IPTABLES' are a pseudo-class meaning an\n".
b0017a324040 Cleanups; Disable weeding in report mode again; Don't display redundant IPTABLES reasons in blocklist report.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
468 "blocked IP that was in Netfilter before Maltfilter was started.\n");
13
fc053b001027 Improved reporting and documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
469
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
470 printH($m, $f, 2, "Currently blocked entries");
26
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
471 $period = get_period($settings{"WEED_BLOCK"});
18
b0017a324040 Cleanups; Disable weeding in report mode again; Don't display redundant IPTABLES reasons in blocklist report.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
472 printP($m, $f, "List of IPs that are currently blocked (or would be, if this is\n".
26
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
473 "a report-only mode). Data from period of $period.\n");
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
474 print_table1($m, $f, \%statlist, \%blocklist, \&cmp_hits, "blocked");
11
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
475
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
476 printH($m, $f, 2, "Summary of non-ignored entries");
18
b0017a324040 Cleanups; Disable weeding in report mode again; Don't display redundant IPTABLES reasons in blocklist report.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
477 printP($m, $f, "List of 'hits' of suspicious activity noticed by Maltfilter, but not\n".
b0017a324040 Cleanups; Disable weeding in report mode again; Don't display redundant IPTABLES reasons in blocklist report.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
478 "necessarily acted upon. Sorted by descending IP address.\n");
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
479 print_table2($m, $f, \%statlist, \%statlist, \&cmp_ips, "global");
11
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
480
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
481 printH($m, $f, 2, "Ignored entries");
52
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
482 printP($m, $f, "List of hits that were ignored (not acted upon), because the test was disabled.\n".
8cfb71b296da Added colour-coded grouping of IP addresses in summary table.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
483 "Notice that the entry may be blocked due to other checks, however.\n");
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
484 print_table1($m, $f, \%ignorelist, \%ignorelist, \&cmp_hits, "ignored");
11
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
485
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
486 printElem($m, $f, "</body>\n</html>\n");
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
487 close(STATUS);
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
488 }
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
489
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
490
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
491 #############################################################################
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
492 ### Entry management / handling functions
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
493 #############################################################################
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
494 ### Check if given IP or host exists in array
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
495 sub check_hosts_array($$)
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
496 {
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
497 my $chk_host = $_[1];
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
498 my $chk_ip = new Net::IP($chk_host);
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
499 foreach my $host (@{$_[0]}) {
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
500 if ($chk_host eq $host) {
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
501 return 1;
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
502 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
503 my $ip = new Net::IP($host);
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
504 if (defined($chk_ip) && defined($ip)) {
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
505 if ($chk_ip->binip() eq $ip->binip()) {
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
506 return 1;
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
507 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
508 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
509 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
510 return 0;
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
511 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
512
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
513 ### Check IP/host against | separated list of IPs/hosts
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
514 sub check_hosts($$)
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
515 {
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
516 my @tmp = split(/\s*\|\s*/, $_[0]);
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
517 return check_hosts_array(\@tmp, $_[1]);
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
518 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
519
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
520 ### Execute iptables
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
521 sub exec_iptables(@)
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
522 {
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
523 my @args = ($settings{"IPTABLES"}, @_);
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
524 if ($settings{"DRY_RUN"}) {
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
525 mlog(3, ":: ".join(" ", @args)."\n");
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
526 } else {
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
527 system(@args) == 0 or print join(" ", @args)." failed: $?\n";
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
528 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
529 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
530
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
531 ### Get current Netfilter INPUT table entries that match
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
532 ### entry types we manage, e.g. blocklist
54
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
533 sub update_blocklist($)
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
534 {
54
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
535 # NOTICE: argument not used now
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
536
17
fe220b5a975a Cleanups, add configuration for WHOIS linking.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
537 $ENV{"PATH"} = "";
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
538 open(STATUS, $settings{"IPTABLES"}." -v -n -L INPUT |") or
39
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
539 mdie("Could not execute ".$settings{"IPTABLES"}."\n");
23
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
540 my %newlist = ();
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
541 undef(%newlist);
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
542 while (<STATUS>) {
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
543 chomp;
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
544 if (/^\s*(\d+)\s+\d+\s+$settings{"ACTION"}\s+all\s+--\s+\*\s+\*\s+(\d+\.\d+\.\d+\.\d+)\s+0\.0\.0\.0\/0\s*$/) {
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
545 my $mip = $2;
20
429b42047d04 Fix blocklist entry updating.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
546 my $mdate = time();
429b42047d04 Fix blocklist entry updating.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
547 if (!defined($blocklist{$mip})) {
18
b0017a324040 Cleanups; Disable weeding in report mode again; Don't display redundant IPTABLES reasons in blocklist report.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
548 mlog(2, "* $mip appeared in iptables.\n");
20
429b42047d04 Fix blocklist entry updating.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
549 $blocklist{$2} = $mdate;
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
550 }
23
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
551 $newlist{$2} = $mdate;
53
dc072a56f343 Don't add hits when updating entries from netfilter.
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
552 update_entry(\%statlist, $mip, -1, "IPTABLES", "", 0);
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
553 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
554 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
555 close(STATUS);
23
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
556
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
557 foreach my $mip (keys %blocklist) {
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
558 if (!defined($newlist{$mip})) {
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
559 mlog(2, "* $mip removed from iptables.\n");
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
560 delete($blocklist{$mip});
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
561 }
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
562 }
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
563 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
564
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
565 ### Check if given timestamp is _newer_ than weedperiod threshold.
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
566 ### Returns false if timestamp is over weed period, e.g. needs weeding.
26
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
567 sub check_time1($)
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
568 {
26
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
569 return ($_[0] >= time() - ($settings{"WEED_BLOCK"} * 60 * 60));
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
570 }
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
571
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
572 sub check_time2($)
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
573 {
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
574 return ($_[0] >= time() - ($settings{"WEED_GLOBAL"} * 60 * 60));
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
575 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
576
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
577 ### Weed out old entries
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
578 sub weed_do($)
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
579 {
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
580 my $mtime = $blocklist{$_[0]};
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
581 mlog(2, "* Weeding $_[0] (".get_time_str($mtime)."\n");
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
582 exec_iptables("-D", "INPUT", "-s", $_[0], "-d", "0.0.0.0/0", "-j", $settings{"ACTION"});
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
583 delete($blocklist{$_[0]});
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
584 delete($statlist{$_[0]});
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
585 delete($ignorelist{$_[0]});
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
586 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
587
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
588 sub weed_entries()
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
589 {
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
590 # Don't weed in report mode.
18
b0017a324040 Cleanups; Disable weeding in report mode again; Don't display redundant IPTABLES reasons in blocklist report.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
591 return if ($reportmode);
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
592
25
34dcb7462043 Sanitize weeding of entries, separating blocklist weeding from global lists.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
593 # Weed blocked entries.
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
594 my @mips = keys %blocklist;
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
595 foreach my $mip (@mips) {
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
596 if (defined($blocklist{$mip})) {
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
597 if ($blocklist{$mip} >= 0) {
26
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
598 weed_do($mip) unless check_time1($blocklist{$mip});
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
599 } else {
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
600 weed_do($mip);
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
601 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
602 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
603 }
25
34dcb7462043 Sanitize weeding of entries, separating blocklist weeding from global lists.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
604
34dcb7462043 Sanitize weeding of entries, separating blocklist weeding from global lists.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
605 # Clean up old entries from other lists
34dcb7462043 Sanitize weeding of entries, separating blocklist weeding from global lists.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
606 foreach my $mip (keys %statlist) {
34dcb7462043 Sanitize weeding of entries, separating blocklist weeding from global lists.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
607 if (defined($statlist{$mip})) {
34dcb7462043 Sanitize weeding of entries, separating blocklist weeding from global lists.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
608 my $mtime = $statlist{$mip}{"date2"};
26
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
609 if (!check_time2($mtime) && !defined($blocklist{$mip})) {
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
610 mlog(3, "* Deleting stale $mip (".get_time_str($mtime).")\n");
25
34dcb7462043 Sanitize weeding of entries, separating blocklist weeding from global lists.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
611 delete($statlist{$mip});
34dcb7462043 Sanitize weeding of entries, separating blocklist weeding from global lists.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
612 }
34dcb7462043 Sanitize weeding of entries, separating blocklist weeding from global lists.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
613 }
34dcb7462043 Sanitize weeding of entries, separating blocklist weeding from global lists.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
614 }
34dcb7462043 Sanitize weeding of entries, separating blocklist weeding from global lists.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
615
34dcb7462043 Sanitize weeding of entries, separating blocklist weeding from global lists.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
616 foreach my $mip (keys %ignorelist) {
34dcb7462043 Sanitize weeding of entries, separating blocklist weeding from global lists.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
617 if (defined($ignorelist{$mip})) {
34dcb7462043 Sanitize weeding of entries, separating blocklist weeding from global lists.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
618 my $mtime = $ignorelist{$mip}{"date2"};
26
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
619 if (!check_time2($mtime)) {
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
620 mlog(3, "* Deleting stale ignored $mip (".get_time_str($mtime).")\n");
25
34dcb7462043 Sanitize weeding of entries, separating blocklist weeding from global lists.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
621 delete($ignorelist{$mip});
34dcb7462043 Sanitize weeding of entries, separating blocklist weeding from global lists.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
622 }
34dcb7462043 Sanitize weeding of entries, separating blocklist weeding from global lists.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
623 }
34dcb7462043 Sanitize weeding of entries, separating blocklist weeding from global lists.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
624 }
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
625 }
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
626
54
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
627 ### Update one entry data
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
628 sub update_date($$)
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
629 {
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
630 if (!defined($_[0]->{"date1"}) || ($_[1] > 0 && $_[0]->{"date1"} < 0)) {
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
631 $_[0]->{"date1"} = $_[1];
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
632 }
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
633 if (!defined($_[0]->{"date2"}) || $_[1] > $_[0]->{"date2"}) {
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
634 $_[0]->{"date2"} = $_[1];
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
635 }
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
636 }
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
637
53
dc072a56f343 Don't add hits when updating entries from netfilter.
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
638 sub update_entry($$$$$$)
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
639 {
53
dc072a56f343 Don't add hits when updating entries from netfilter.
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
640 my ($struct, $mip, $mdate, $mclass, $mreason, $addhits) = @_;
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
641
54
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
642 $struct->{$mip} = {} unless defined($struct->{$mip});
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
643 my $entry = $struct->{$mip};
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
644 $struct->{$mip}{"reason"}{$mclass} = {};
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
645 my $reason = $struct->{$mip}{"reason"}{$mclass};
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
646
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
647 # Add hits only when requested
53
dc072a56f343 Don't add hits when updating entries from netfilter.
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
648 if ($addhits) {
54
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
649 $entry->{"hits"}++;
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
650 $reason->{"hits"}++;
53
dc072a56f343 Don't add hits when updating entries from netfilter.
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
651 } else {
54
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
652 $entry->{"hits"} = 1 unless defined($entry->{"hits"});
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
653 $reason->{"hits"} = 1 unless defined($reason->{"hits"});
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
654 }
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
655
54
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
656 # Messages is an array in reportmode
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
657 if ($reportmode) {
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
658 push(@{$reason->{"msg"}}, $mreason);
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
659 } else {
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
660 $reason->{"msg"} = $mreason;
39
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
661 }
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
662
54
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
663 # Update timestamps (generic and reason)
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
664 update_date($entry, $mdate);
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
665 update_date($reason, $mdate);
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
666
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
667 return $entry->{"hits"};
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
668 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
669
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
670 ### Check if given "try count" exceeds treshold and if entry
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
671 ### is NOT in Netfilter already, then add it if so.
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
672 sub check_add_hit($$$$$)
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
673 {
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
674 my $mip = $_[0];
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
675 my $mdate = str2time($_[1]);
11
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
676 my $mclass = $_[2];
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
677 my $mreason = $_[3];
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
678 my $mcond = $_[4];
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
679 my $cnt;
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
680
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
681 if (check_hosts_array(\@noblock_ips, $mip)) {
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
682 mlog(3, "Hit to NOBLOCK_IPS($mip): [$mclass] $mreason\n");
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
683 return;
13
fc053b001027 Improved reporting and documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
684 }
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
685
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
686 # If condition is true, we add to regular statlist
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
687 if ($mcond) {
53
dc072a56f343 Don't add hits when updating entries from netfilter.
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
688 $cnt = update_entry(\%statlist, $mip, $mdate, $mclass, $mreason, 1);
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
689 } else {
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
690 # This is an ignored hit (for disabled test), add to ignorelist
53
dc072a56f343 Don't add hits when updating entries from netfilter.
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
691 update_entry(\%ignorelist, $mip, $mdate, $mclass, $mreason, 1);
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
692 return;
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
693 }
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
694
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
695 # Check if we have exceeded treshold etc.
26
61b6d742c49c Separate WEEDPERIOD into WEED_BLOCK and WEED_GLOBAL settings.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
696 if ($cnt >= $settings{"TRESHOLD"} && check_time1($mdate)) {
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
697 # Add to blocklist, unless already there.
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
698 if (!defined($blocklist{$mip})) {
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
699 mlog(1, "* Adding $mip ($mdate): [$mclass] $mreason\n");
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
700 exec_iptables("-I", "INPUT", "1", "-s", $mip, "-j", $settings{"ACTION"});
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
701 }
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
702 # Update date of last hit
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
703 $blocklist{$mip} = $mdate;
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
704 }
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
705 }
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
706
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
707
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
708 #############################################################################
11
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
709 ### Main helper functions
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
710 #############################################################################
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
711 ### Print log entry
39
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
712 sub mlog($$)
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
713 {
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
714 my $level = shift;
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
715 my $msg = shift;
23
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
716 if ($LOGFILE) {
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
717 print $LOGFILE "[".get_time_str(time())."] ".$msg if ($settings{"VERBOSITY"} > $level);
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
718 } elsif ($settings{"DRY_RUN"}) {
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
719 print STDERR $msg if ($settings{"VERBOSITY"} > $level);
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
720 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
721 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
722
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
723 ### Like Perl's die(), but also print a logfile entry.
39
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
724 sub mdie($)
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
725 {
54
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
726 mlog(-1, $_[0]) if ($LOGFILE);
39
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
727 die($_[0]);
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
728 }
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
729
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
730 ### Initialize
23
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
731 sub malt_init
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
732 {
57
a70493b6c916 Clear %statlist and %ignorelist when re-initializing (due to HUP), so we don't count stats twice.
Matti Hamalainen <ccr@tnsp.org>
parents: 55
diff changeset
733 %statlist = ();
a70493b6c916 Clear %statlist and %ignorelist when re-initializing (due to HUP), so we don't count stats twice.
Matti Hamalainen <ccr@tnsp.org>
parents: 55
diff changeset
734 undef(%statlist);
a70493b6c916 Clear %statlist and %ignorelist when re-initializing (due to HUP), so we don't count stats twice.
Matti Hamalainen <ccr@tnsp.org>
parents: 55
diff changeset
735 %ignorelist = ();
a70493b6c916 Clear %statlist and %ignorelist when re-initializing (due to HUP), so we don't count stats twice.
Matti Hamalainen <ccr@tnsp.org>
parents: 55
diff changeset
736 undef(%ignorelist);
a70493b6c916 Clear %statlist and %ignorelist when re-initializing (due to HUP), so we don't count stats twice.
Matti Hamalainen <ccr@tnsp.org>
parents: 55
diff changeset
737 mlog(0, "Updating initial blocklist from netfilter.\n");
a70493b6c916 Clear %statlist and %ignorelist when re-initializing (due to HUP), so we don't count stats twice.
Matti Hamalainen <ccr@tnsp.org>
parents: 55
diff changeset
738 update_blocklist(-1);
a70493b6c916 Clear %statlist and %ignorelist when re-initializing (due to HUP), so we don't count stats twice.
Matti Hamalainen <ccr@tnsp.org>
parents: 55
diff changeset
739
54
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
740 foreach my $filename (@scanfiles_once) {
58
a780a23e19a8 Change parsing status log messages.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
741 mlog(0, "Parsing [ONCE] ".$filename." ...\n");
54
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
742 if (open(INFILE, "<", $filename)) {
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
743 while (<INFILE>) {
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
744 chomp;
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
745 check_log_line($_);
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
746 }
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
747 } else {
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
748 mlog(-1, "Could not open '".$filename."', skipping now.\n");
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
749 }
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
750 close(INFILE);
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
751 }
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
752
3
368182409eac More variable cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
753 foreach my $filename (@scanfiles) {
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
754 local *INFILE;
58
a780a23e19a8 Change parsing status log messages.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
755 mlog(0, "Initial parsing ".$filename." ...\n");
54
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
756 if (open(INFILE, "<", $filename)) {
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
757 $filehandles{$filename} = *INFILE;
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
758 while (<INFILE>) {
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
759 chomp;
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
760 check_log_line($_);
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
761 }
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
762 } else {
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
763 mlog(-1, "Could not open '".$filename."', skipping now.\n");
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
764 }
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
765 }
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
766 }
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
767
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
768 ### Quick cleanup (not complete shutdown)
23
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
769 sub malt_cleanup
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
770 {
3
368182409eac More variable cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
771 foreach my $filename (keys %filehandles) {
368182409eac More variable cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
772 close($filehandles{$filename});
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
773 }
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
774 }
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
775
23
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
776 sub malt_finish
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
777 {
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
778 # Unlink pid-file
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
779 if ($pid_file ne "" && -e $pid_file) {
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
780 unlink $pid_file;
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
781 }
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
782 # Close logfile
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
783 close($LOGFILE) if (defined($LOGFILE));
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
784 undef($LOGFILE);
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
785 }
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
786
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
787 ### Signal handlers
23
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
788 sub malt_int
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
789 {
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
790 mlog(-1, "\nCaught Interrupt (^C), aborting.\n");
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
791 malt_cleanup();
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
792 malt_finish();
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
793 exit(1);
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
794 }
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
795
23
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
796 sub malt_term
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
797 {
11
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
798 mlog(-1, "Received TERM, quitting.\n");
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
799 malt_cleanup();
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
800 malt_finish();
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
801 exit(1);
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
802 }
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
803
23
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
804 sub malt_hup
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
805 {
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
806 mlog(-1, "Received HUP, reinitializing.\n");
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
807 malt_cleanup();
39
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
808 malt_configure();
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
809 malt_init();
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
810 mlog(-1, "Reinitialization finished, resuming scanning.\n");
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
811 }
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
812
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
813 ### Main scanning function
23
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
814 sub malt_scan
cb0a4b747cf0 Handle importing of current netfilter entries differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
815 {
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
816 mlog(1, "Entering main scanning loop.\n");
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
817 my $counter = -1;
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
818 while (1) {
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
819 my %filepos = ();
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
820 foreach my $filename (keys %filehandles) {
39
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
821 for ($filepos{$filename} = tell($filehandles{$filename});
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
822 $_ = <$filehandles{$filename}>;
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
823 $filepos{$filename} = tell($filehandles{$filename})) {
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
824 chomp;
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
825 check_log_line($_);
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
826 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
827 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
828 if ($counter < 0 || $counter++ >= 30) {
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
829 # Every once in a while, update known IP list from iptables
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
830 # (in case entries have appeared there from "outside")
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
831 # and perform weeding of old entries.
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
832 $counter = 0;
54
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
833 update_blocklist(time());
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
834 weed_entries();
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
835 generate_status($settings{"STATUS_FILE_PLAIN"}, 0);
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
836 generate_status($settings{"STATUS_FILE_HTML"}, 1);
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
837 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
838 sleep(5);
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
839 foreach my $filename (keys %filehandles) {
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
840 seek($filehandles{$filename}, $filepos{$filename}, 0);
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
841 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
842 }
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
843 }
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
844
16
87c0cdc048f5 Many changes and cleanups. Works again.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
845 ### Read one configuration file
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
846 sub malt_read_config($)
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
847 {
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
848 my $filename = $_[0];
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
849 my $errors = 0;
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
850 my $line = 0;
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
851
39
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
852 open(CONFFILE, "<", $filename) or mdie("Could not open configuration '".$filename."'!\n");
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
853 while (<CONFFILE>) {
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
854 $line++;
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
855 chomp;
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
856 if (/(^\s*#|^\s*$)/) {
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
857 # Ignore comments and empty lines
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
858 } elsif (/^\s*\"?([a-zA-Z0-9_]+)\"?\s*=>?\s*(\d+),?\s*$/) {
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
859 my $key = uc($1);
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
860 my $value = $2;
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
861 if (defined($settings{$key})) {
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
862 $settings{$key} = $value;
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
863 } else {
39
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
864 mlog(-1, "[$filename:$line] Unknown setting '$key' = $value\n");
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
865 $errors = 1;
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
866 }
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
867 } elsif (/^\s*\"?([a-zA-Z0-9_]+)\"?\s*=>?\s*\"(.*?)\",?\s*$/) {
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
868 my $key = uc($1);
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
869 my $value = $2;
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
870 if ($key eq "SCANFILE") {
54
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
871 push(@scanfiles, $value);
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
872 } elsif ($key eq "SCANFILE_ONCE") {
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
873 push(@scanfiles_once, $value);
8
29ddb6b9b521 Moar changes!
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
874 } elsif ($key eq "NOBLOCK_IPS") {
7
ee5f7b8dcdea Features, yay.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
875 push(@noblock_ips_def, $value);
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
876 } elsif (defined($settings{$key})) {
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
877 $settings{$key} = $value;
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
878 } else {
39
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
879 mlog(-1, "[$filename:$line] Unknown setting '$key' = '$value'\n");
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
880 $errors = 1;
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
881 }
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
882 # Force dry run mode if we are reporting only
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
883 if ($reportmode) {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
884 $settings{"DRY_RUN"} = 1;
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
885 }
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
886 } else {
39
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
887 mlog(-1, "[$filename:$line] Syntax error: $_\n");
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
888 $errors = 1;
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
889 }
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
890 }
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
891 close(CONFFILE);
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
892 return $errors;
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
893 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
894
39
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
895 ### Read all configuration files
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
896 sub malt_configure
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
897 {
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
898 # Let user define his/her own logfiles to scan
54
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
899 @scanfiles = ();
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
900 undef(@scanfiles);
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
901
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
902 @scanfiles_once = ();
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
903 undef(@scanfiles_once);
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
904
39
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
905 foreach my $filename (@configfiles) {
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
906 mdie("Errors in configuration file '$filename', bailing out.\n")
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
907 unless (malt_read_config($filename) == 0);
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
908 }
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
909
54
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
910 # Clean up certain arrays duplicate entries
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
911 my %saw = ();
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
912 @scanfiles = grep(!$saw{$_}++, @scanfiles);
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
913
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
914 %saw = ();
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
915 @scanfiles_once = grep(!$saw{$_}++, @scanfiles_once);
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
916
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
917 %saw = ();
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
918 @noblock_ips = grep(!$saw{$_}++, @noblock_ips_def);
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
919 undef(%saw);
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
920
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
921 mlog(-1, "Not blocking following IPs: ".join(", ", @noblock_ips)."\n");
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
922
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
923 # Check if we have anything to do
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
924 if ($reportmode) {
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
925 mdie("Nothing to do, no SCANFILE(s) or SCANFILE_ONCE(s) defined in configuration.\n") unless ($#scanfiles > 0 || $#scanfiles_once > 0);
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
926 } else {
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
927 mdie("Nothing to do, no SCANFILE(s) defined in configuration.\n") unless ($#scanfiles > 0);
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
928 }
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
929
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
930 # Test existence of iptables
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
931 if (! -e $settings{"IPTABLES"} || ! -x $settings{"IPTABLES"}) {
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
932 mdie("iptables binary does not exist or is not executable: ".$settings{"IPTABLES"}."\n");
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
933 }
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
934
19dace24ad46 Remove default scanfiles; Clean up update_entry() code; Add "SCANFILE_ONCE"
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
935 # Check settings
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
936 mdie("SYSACCT_MIN_UID must be >= 1.\n") unless ($settings{"SYSACCT_MIN_UID"} >= 1);
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
937 mdie("SYSACCT_MAX_UID must be >= SYSACCT_MIN_UID.\n") unless ($settings{"SYSACCT_MAX_UID"} >= $settings{"SYSACCT_MIN_UID"});
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
938
44
471731c79bb3 Add configuration setting for PASSWD file.
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
939 open(PASSWD, "<", $settings{"PASSWD"}) or mdie("Could not open '".$settings{"PASSWD"}."' for reading!\n");
40
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
940 while (<PASSWD>) {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
941 my @fields = split(/\s*:\s*/);
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
942 if ($fields[2] >= $settings{"SYSACCT_MIN_UID"} && $fields[2] <= $settings{"SYSACCT_MAX_UID"}) {
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
943 $systemacct{$fields[0]} = $fields[2];
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
944 }
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
945 }
24babaa1e331 Many cleanups and fixes; Example configuration updated.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
946 close(PASSWD);
39
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
947 }
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
948
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
949 #############################################################################
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
950 ###
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
951 ### Main program
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
952 ###
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
953 #############################################################################
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
954 # Setup signal handlers
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
955 $SIG{'INT'} = 'malt_int';
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
956 $SIG{'TERM'} = 'malt_term';
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
957 $SIG{'HUP'} = 'malt_hup';
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
958
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
959 # Print banner and help if no arguments
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
960 my $argc = $#ARGV + 1;
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
961 if ($argc < 1) {
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
962 print $progbanner.
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
963 "\n".
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
964 "Usage: maltfilter <pid filename> [config filename] [config filename...]\n".
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
965 " maltfilter -f [config filename] [config filename...]\n".
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
966 "-f turns on the full report mode.\n";
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
967 exit;
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
968 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
969
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
970 # Test pid file existence unless report mode
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
971 $pid_file = shift;
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
972 if ($pid_file eq "-f") {
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
973 $reportmode = 1;
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
974 } else {
39
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
975 mdie("'$pid_file' already exists, not starting.\n".
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
976 "If the daemon is NOT running, remove the pid-file and re-start.\n")
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
977 if (-e $pid_file);
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
978 }
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
979
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
980 # Read configuration files
39
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
981 while (defined(my $filename = shift)) {
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
982 push(@configfiles, $filename);
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
983 }
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
984
39
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
985 malt_configure();
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
986
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
987 # Open logfile
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
988 if ($settings{"DRY_RUN"}) {
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
989 print $progbanner.
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
990 "*********************************************\n".
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
991 "* NOTICE! DRY-RUN MODE ENABLED! No changes *\n".
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
992 "* will actually get committed to netfilter! *\n".
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
993 "*********************************************\n";
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
994 } elsif ($settings{"LOGFILE"} ne "") {
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
995 open($LOGFILE, ">>", $settings{"LOGFILE"}) or die("Could not open logfile '".$settings{"LOGFILE"}."' for writing!\n");
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
996 mlog(-1, "Log started\n");
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
997 }
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
998
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
999 # Initialize
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1000 malt_init();
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1001
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1002 # Fork to background, unless dry-running
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1003 if ($settings{"DRY_RUN"}) {
15
b05d0f0ff106 Cleanups in progress, does not work.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
1004 if ($reportmode) {
11
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
1005 mlog(-1, "Outputting report files.\n");
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
1006 generate_status($settings{"STATUS_FILE_PLAIN"}, 0);
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
1007 generate_status($settings{"STATUS_FILE_HTML"}, 1);
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
1008 malt_cleanup();
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
1009 } else {
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
1010 malt_scan();
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
1011 malt_cleanup();
26c2cc5077aa Added reporting functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
1012 }
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1013 } else {
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1014 if (my $pid = fork) {
39
d96229159abc v0.11.0: More fixes; Configuration files are now re-read when HUP signal is
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
1015 open(PIDFILE, ">", $pid_file) or mdie("Could not open pid file '".$pid_file."' for writing!\n");
0
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1016 print PIDFILE "$pid\n";
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1017 close(PIDFILE);
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1018 } else {
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1019 malt_scan();
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1020 malt_cleanup();
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1021 }
fec14263801d Initial import of maltfilter development version.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1022 }