-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfirewall-whitelisting-local-cgi
executable file
·134 lines (105 loc) · 2.86 KB
/
firewall-whitelisting-local-cgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env suidperl -T
=for comment
firewall whitelisting CGI
by Jon Jensen <[email protected]>
2005-03-04
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <https://www.gnu.org/licenses/>.
Add these rules somewhere in your iptables configuration file,
before your drop rules:
-N dynamic_ip
-A INPUT -j dynamic_ip
=cut
# Changelog :
#
# 20140707 - Emanuele 'Lele' Calo' - Added username in iptables comments feature
# 20050304 - Jon Jensen - Initial version
use warnings;
no warnings 'qw';
use strict;
use Sys::Syslog;
my $table = 'dynamic_ip';
my $iptables = '/sbin/iptables';
#my $out = join "\n", map { "$_=$ENV{$_}" } sort keys %ENV;
# Save important things before clearing tainted environment
my $ip = $ENV{REMOTE_ADDR};
my $user = $ENV{REMOTE_USER};
my $secure = $ENV{HTTPS};
my @fullhost = split(':', $ENV{HTTP_HOST});
my $host = $fullhost[0];
%ENV = ();
openlog 'firewall CGI', 'pid nowait', 'LOG_AUTHPRIV';
announce("Connection from $ip by $user");
perish("This application may only be accessed with SSL encryption.\n")
unless $secure;
$ip =~ /^(\d+\.\d+\.\d+\.\d+)$/;
@_ = split /\./, $1 || '';
my @ip;
for (@_) {
push @ip, $_ if $_ < 256;
}
perish("Invalid IP address\n")
unless @ip == 4;
$ip = join '.', @ip;
perish("Devious IP address: $ip\n")
if $ip eq '0.0.0.0' or $ip eq '255.255.255.255';
my $msg;
if (`$iptables -L $table -n` =~ /\Q$ip/) {
$msg = <<END;
Your IP address, $ip, was already whitelisted in the firewall.
Go ahead and
END
announce("IP was already whitelisted");
}
else {
my @base = ($iptables, '-A', $table, '-s', $ip, '-m', 'comment', '--comment', $user . ' whitelisted ' . time());
for (
[qw( -p tcp -m multiport --dports 21,22 -j ACCEPT )],
) {
system(@base, @$_) == 0 or perish("Error: $!\n");
}
$msg = <<END;
Your IP address, $ip, has been added to the firewall whitelist.
You should now be able to
END
announce("IP has been added to whitelist");
}
print <<END;
Content-type: text/html\r
\r
<html>
<head>
<meta http-equiv="cache-control" content="no-cache">
<title>End Point Firewall</title>
</head>
<body>
<h1>End Point Firewall</h1>
<p>
$msg
access the firewalled ports on the server.
</p>
<p>
Please email support if you run into any problems.
</p>
</body>
</html>
END
closelog;
exit 0;
sub announce {
syslog 'LOG_NOTICE', '%s', @_;
return;
}
sub perish {
syslog 'LOG_ERR', '%s', @_;
closelog;
die @_;
}