| 1 | package Shadowd::Connector::CGI; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use base 'Shadowd::Connector'; | ||
| 7 | |||
| 8 | use CGI; | ||
| 9 | use URI::Encode qw(uri_encode); | ||
| 10 | use Crypt::Digest::SHA256 qw(sha256_file_hex); | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Shadowd::Connector::CGI - Shadow Daemon CGI Connector | ||
| 15 | |||
| 16 | =head1 VERSION | ||
| 17 | |||
| 18 | Version 2.0.0 | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | our $VERSION = '2.0.0'; | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | B<Shadow Daemon> is a collection of tools to I<detect>, I<record> and I<prevent> I<attacks> on I<web applications>. | ||
| 27 | Technically speaking, Shadow Daemon is a B<web application firewall> that intercepts requests and filters out malicious parameters. | ||
| 28 | It is a modular system that separates web application, analysis and interface to increase security, flexibility and expandability. | ||
| 29 | |||
| 30 | I<Shadowd::Connector::CGI> is the Shadow Daemon connector for Perl CGI applications. The module operates fully automatic and only has | ||
| 31 | to be loaded/used to start its task. | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | =head1 METHODS | ||
| 36 | |||
| 37 | =head2 new($query) | ||
| 38 | |||
| 39 | This method is a simple constructor for an object oriented interface. It requires a CGI object as parameter. | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | sub new { | ||
| 44 | my ($class, $query) = @_; | ||
| 45 | |||
| 46 | my $self = $class->SUPER::new; | ||
| 47 | $self->{'_query'} = $query; | ||
| 48 | |||
| 49 | return $self; | ||
| 50 | } | ||
| 51 | |||
| 52 | =head2 get_client_ip() | ||
| 53 | |||
| 54 | This method returns the IP address of the client from the environment. The default key is I<REMOTE_ADDR>, but if you are using a | ||
| 55 | reverse proxy you have to change the key via the configuration file. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | sub get_client_ip { | ||
| 60 | my ($self) = @_; | ||
| 61 | |||
| 62 | return $ENV{$self->get_config('client_ip', 0, 'REMOTE_ADDR')}; | ||
| 63 | } | ||
| 64 | |||
| 65 | =head2 get_caller() | ||
| 66 | |||
| 67 | This method returns the caller from the environment. The default key is I<SCRIPT_FILENAME>, i.e. the executed Perl script. | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | sub get_caller { | ||
| 72 | my ($self) = @_; | ||
| 73 | |||
| 74 | return $ENV{$self->get_config('caller', 0, 'SCRIPT_FILENAME')}; | ||
| 75 | } | ||
| 76 | |||
| 77 | =head2 get_resource() | ||
| 78 | |||
| 79 | This method returns the request resource. | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | sub get_resource { | ||
| 84 | my ($self) = @_; | ||
| 85 | |||
| 86 | return $ENV{'REQUEST_URI'}; | ||
| 87 | } | ||
| 88 | |||
| 89 | =head2 gather_input() | ||
| 90 | |||
| 91 | This method gathers the user input with the help of the CGI module. The CGI module gets the user input from the environment. | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub gather_input { | ||
| 96 | my ($self) = @_; | ||
| 97 | |||
| 98 | $self->{'_input'} = {}; | ||
| 99 | |||
| 100 | foreach my $key ($self->{'_query'}->param) { | ||
| 101 | my $path = $self->{'_query'}->request_method . '|' . $self->escape_key($key); | ||
| 102 | my @values = $self->{'_query'}->param($key); | ||
| 103 | |||
| 104 | if ($#values > 0){ | ||
| 105 | for my $index (0 .. $#values) { | ||
| 106 | $self->{'_input'}->{$path . '|' . $index} = $values[$index]; | ||
| 107 | } | ||
| 108 | } else { | ||
| 109 | $self->{'_input'}->{$path} = $values[0]; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | foreach my $key ($self->{'_query'}->cookie) { | ||
| 114 | $self->{'_input'}->{'COOKIE|' . $self->escape_key($key)} = $self->{'_query'}->cookie($key); | ||
| 115 | } | ||
| 116 | |||
| 117 | foreach my $key ($self->{'_query'}->http) { | ||
| 118 | $self->{'_input'}->{'SERVER|' . $self->escape_key($key)} = $self->{'_query'}->http($key); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | =head2 defuse_input($threats) | ||
| 123 | |||
| 124 | This method defuses dangerous input by overwriting the environment of the script. | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | sub defuse_input { | ||
| 129 | my ($self, $threats) = @_; | ||
| 130 | |||
| 131 | my %cookies; | ||
| 132 | |||
| 133 | foreach my $cookie ($self->{'_query'}->cookie) { | ||
| 134 | $cookies{$cookie} = $self->{'_query'}->cookie($cookie); | ||
| 135 | } | ||
| 136 | |||
| 137 | foreach my $path (@{$threats}) { | ||
| 138 | my @path_split = $self->split_path($path); | ||
| 139 | |||
| 140 | if ($#path_split < 1) { | ||
| 141 | next; | ||
| 142 | } | ||
| 143 | |||
| 144 | my $key = $self->unescape_key($path_split[1]); | ||
| 145 | |||
| 146 | if ($path_split[0] eq 'SERVER') { | ||
| 147 | $ENV{$key} = ''; | ||
| 148 | } elsif ($path_split[0] eq 'COOKIE') { | ||
| 149 | $cookies{$key} = ''; | ||
| 150 | } else { | ||
| 151 | if ($#path_split == 1) { | ||
| 152 | $self->{'_query'}->param($key, ''); | ||
| 153 | } else { | ||
| 154 | my @values = $self->{'_query'}->param($key); | ||
| 155 | $values[$path_split[2]] = ''; | ||
| 156 | $self->{'_query'}->param($key, @values); | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | # Save the changes for the CGI module. | ||
| 162 | $self->{'_query'}->save_request; | ||
| 163 | |||
| 164 | # Overwrite the query string in the env in case that the target does not use CGI. | ||
| 165 | $ENV{'QUERY_STRING'} = $self->{'_query'}->query_string; | ||
| 166 | |||
| 167 | if (defined $ENV{'HTTP_COOKIE'}) { | ||
| 168 | my $cookie_string = ''; | ||
| 169 | |||
| 170 | foreach my $key (keys %cookies) { | ||
| 171 | $cookie_string .= uri_encode($key) . '=' . uri_encode($cookies{$key}) . ';'; | ||
| 172 | } | ||
| 173 | |||
| 174 | # Remove last semicolon. | ||
| 175 | chop($cookie_string); | ||
| 176 | |||
| 177 | # Overwrite the cookie string. | ||
| 178 | $ENV{'HTTP_COOKIE'} = $cookie_string; | ||
| 179 | } | ||
| 180 | |||
| 181 | # Don't stop the complete request. | ||
| 182 | return 1; | ||
| 183 | } | ||
| 184 | |||
| 185 | =head2 gather_hashes() | ||
| 186 | |||
| 187 | This method gathers cryptographically secure checksums of the executed script. | ||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | sub gather_hashes { | ||
| 192 | my ($self) = @_; | ||
| 193 | |||
| 194 | $self->{'_hashes'} = {}; | ||
| 195 | |||
| 196 | my $file = $ENV{'SCRIPT_FILENAME'}; | ||
| 197 | $self->{'_hashes'}->{'sha256'} = sha256_file_hex($file); | ||
| 198 | } | ||
| 199 | |||
| 200 | =head2 error() | ||
| 201 | |||
| 202 | This method simply prints an error message. | ||
| 203 | |||
| 204 | =cut | ||
| 205 | |||
| 206 | sub error { | ||
| 207 | my ($self) = @_; | ||
| 208 | |||
| 209 | print $self->{'_query'}->header(-status => '500 Internal Server Error'); | ||
| 210 | print '<h1>500 Internal Server Error</h1>'; | ||
| 211 | } | ||
| 212 | |||
| 213 | BEGIN { | ||
| 214 | my $connector = Shadowd::Connector::CGI->new(CGI->new); | ||
| 215 | |||
| 216 | if (!$connector->start) { | ||
| 217 | exit; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | =head1 AUTHOR | ||
| 222 | |||
| 223 | Hendrik Buchwald, C<< <hb@zecure.org> >> | ||
| 224 | |||
| 225 | =head1 BUGS | ||
| 226 | |||
| 227 | Please report any bugs or feature requests to C<bug-shadowd-connector@rt.cpan.org>, or through the web interface at | ||
| 228 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Shadowd-Connector>. I will be notified, and then you'll automatically | ||
| 229 | be notified of progress on your bug as I make changes. | ||
| 230 | |||
| 231 | It is also possible to report bugs via Github at L<https://github.com/zecure/shadowd_perl/issues>. | ||
| 232 | |||
| 233 | =head1 SUPPORT | ||
| 234 | |||
| 235 | You can find documentation for this module with the perldoc command. | ||
| 236 | |||
| 237 | perldoc Shadowd::Connector | ||
| 238 | |||
| 239 | |||
| 240 | You can also look for information at: | ||
| 241 | |||
| 242 | =over 4 | ||
| 243 | |||
| 244 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 245 | |||
| 246 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Shadowd-Connector> | ||
| 247 | |||
| 248 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 249 | |||
| 250 | L<http://annocpan.org/dist/Shadowd-Connector> | ||
| 251 | |||
| 252 | =item * CPAN Ratings | ||
| 253 | |||
| 254 | L<http://cpanratings.perl.org/d/Shadowd-Connector> | ||
| 255 | |||
| 256 | =item * Search CPAN | ||
| 257 | |||
| 258 | L<http://search.cpan.org/dist/Shadowd-Connector/> | ||
| 259 | |||
| 260 | =back | ||
| 261 | |||
| 262 | =head1 LICENSE AND COPYRIGHT | ||
| 263 | |||
| 264 | Shadow Daemon -- Web Application Firewall | ||
| 265 | |||
| 266 | Copyright (C) 2014-2016 Hendrik Buchwald C<< <hb@zecure.org> >> | ||
| 267 | |||
| 268 | This file is part of Shadow Daemon. Shadow Daemon is free software: you can | ||
| 269 | redistribute it and/or modify it under the terms of the GNU General Public | ||
| 270 | License as published by the Free Software Foundation, version 2. | ||
| 271 | |||
| 272 | This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 273 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 274 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 275 | details. | ||
| 276 | |||
| 277 | You should have received a copy of the GNU General Public License | ||
| 278 | along with this program. If not, see L<http://www.gnu.org/licenses/>. | ||
| 279 | |||
| 280 | =cut | ||
| 281 | |||
| 282 | 1; # End of Shadowd::Connector::CGI |