| 1 | package Shadowd::Connector::Mojolicious; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use base 'Shadowd::Connector'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Shadowd::Connector::Mojolicious - Shadow Daemon Mojolicious Connector | ||
| 11 | |||
| 12 | =head1 VERSION | ||
| 13 | |||
| 14 | Version 2.0.0 | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | our $VERSION = '2.0.0'; | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | B<Shadow Daemon> is a collection of tools to I<detect>, I<record> and I<prevent> I<attacks> on I<web applications>. | ||
| 23 | Technically speaking, Shadow Daemon is a B<web application firewall> that intercepts requests and filters out malicious parameters. | ||
| 24 | It is a modular system that separates web application, analysis and interface to increase security, flexibility and expandability. | ||
| 25 | |||
| 26 | I<Shadowd::Connector::Mojolicious> is the Shadow Daemon connector for Perl Mojolicious applications. To use this module you | ||
| 27 | have to create a hook that is executed on every request and pass the Mojolicious controller object to the constructor. | ||
| 28 | |||
| 29 | =head2 Mojolicious | ||
| 30 | |||
| 31 | use Shadowd::Connector::Mojolicious; | ||
| 32 | |||
| 33 | sub startup { | ||
| 34 | my $app = shift; | ||
| 35 | |||
| 36 | $app->hook(before_dispatch => sub { | ||
| 37 | my $self = shift; | ||
| 38 | return Shadowd::Connector::Mojolicious->new($self)->start(); | ||
| 39 | }); | ||
| 40 | |||
| 41 | # ... | ||
| 42 | } | ||
| 43 | |||
| 44 | =head2 Mojolicious::Lite | ||
| 45 | |||
| 46 | use Shadowd::Connector::Mojolicious; | ||
| 47 | |||
| 48 | under sub { | ||
| 49 | my $self = shift; | ||
| 50 | return Shadowd::Connector::Mojolicious->new($self)->start(); | ||
| 51 | }; | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | =head1 METHODS | ||
| 56 | |||
| 57 | =head2 new($query) | ||
| 58 | |||
| 59 | This method is a simple constructor for an object oriented interface. It requires a Mojolicious controller object as parameter. | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | sub new { | ||
| 64 | my ($class, $query) = @_; | ||
| 65 | |||
| 66 | my $self = $class->SUPER::new; | ||
| 67 | $self->{'_query'} = $query; | ||
| 68 | |||
| 69 | # Mojolicious supports cookies with shared names, so first we have to get all unique names. | ||
| 70 | foreach my $cookie (@{$self->{'_query'}->req->cookies}) { | ||
| 71 | $self->{'_cookies'}->{$cookie->name} = 1; | ||
| 72 | } | ||
| 73 | |||
| 74 | return $self; | ||
| 75 | } | ||
| 76 | |||
| 77 | =head2 get_client_ip() | ||
| 78 | |||
| 79 | This method returns the IP address of the client with the help of the controller. If Mojolicious is configured correctly this is the correct IP | ||
| 80 | address even if a reverse proxy is used. | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | sub get_client_ip { | ||
| 85 | my ($self) = @_; | ||
| 86 | |||
| 87 | return $self->{'_query'}->tx->remote_address; | ||
| 88 | } | ||
| 89 | |||
| 90 | =head2 get_caller() | ||
| 91 | |||
| 92 | This method returns the caller with the help of the controller. Since everything is routed through a front controller the selected route is the caller. | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | sub get_caller { | ||
| 97 | my ($self) = @_; | ||
| 98 | |||
| 99 | return $self->{'_query'}->req->url->path->to_string; | ||
| 100 | } | ||
| 101 | |||
| 102 | =head2 get_resource() | ||
| 103 | |||
| 104 | This method returns the request resource. | ||
| 105 | |||
| 106 | =cut | ||
| 107 | |||
| 108 | sub get_resource { | ||
| 109 | my ($self) = @_; | ||
| 110 | |||
| 111 | return $self->{'_query'}->req->url->to_string; | ||
| 112 | } | ||
| 113 | |||
| 114 | =head2 gather_input() | ||
| 115 | |||
| 116 | This method gathers the user input with the help of the controller. | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | sub gather_input { | ||
| 121 | my ($self) = @_; | ||
| 122 | |||
| 123 | $self->{'_input'} = {}; | ||
| 124 | |||
| 125 | foreach my $key ($self->{'_query'}->param) { | ||
| 126 | my $path = $self->{'_query'}->req->method . '|' . $self->escape_key($key); | ||
| 127 | my @values; | ||
| 128 | |||
| 129 | # Mojolicious 5 has separate methods to get input with the same name. | ||
| 130 | if ($self->{'_query'}->can('every_param')) { | ||
| 131 | @values = @{$self->{'_query'}->every_param($key)}; | ||
| 132 | } else { | ||
| 133 | @values = $self->{'_query'}->param($key); | ||
| 134 | } | ||
| 135 | |||
| 136 | if ($#values > 0){ | ||
| 137 | for my $index (0 .. $#values) { | ||
| 138 | if (!$values[$index]->isa('Mojo::Upload')) { | ||
| 139 | $self->{'_input'}->{$path . '|' . $index} = $values[$index]; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } else { | ||
| 143 | if (!$values[0]->isa('Mojo::Upload')) { | ||
| 144 | $self->{'_input'}->{$path} = $values[0]; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | foreach my $key (keys %{$self->{'_cookies'}}) { | ||
| 150 | my @values; | ||
| 151 | |||
| 152 | if ($self->{'_query'}->can('every_cookie')) { | ||
| 153 | @values = @{$self->{'_query'}->every_cookie($key)}; | ||
| 154 | } else { | ||
| 155 | @values = $self->{'_query'}->cookie($key); | ||
| 156 | } | ||
| 157 | |||
| 158 | if ($#values > 0){ | ||
| 159 | for my $index (0 .. $#values) { | ||
| 160 | $self->{'_input'}->{'COOKIE|' . $self->escape_key($key) . '|' . $index} = $values[$index]; | ||
| 161 | } | ||
| 162 | } else { | ||
| 163 | $self->{'_input'}->{'COOKIE|' . $self->escape_key($key)} = $values[0]; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | my $headers = $self->{'_query'}->req->headers->to_hash; | ||
| 168 | |||
| 169 | foreach my $key (keys %$headers) { | ||
| 170 | $self->{'_input'}->{'SERVER|' . $self->escape_key($key)} = $headers->{$key}; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | =head2 defuse_input($threats) | ||
| 175 | |||
| 176 | This method defuses dangerous input with the help of the controller. | ||
| 177 | |||
| 178 | =cut | ||
| 179 | |||
| 180 | sub defuse_input { | ||
| 181 | my ($self, $threats) = @_; | ||
| 182 | |||
| 183 | my %cookies; | ||
| 184 | |||
| 185 | foreach my $key (keys %{$self->{'_cookies'}}) { | ||
| 186 | my @values; | ||
| 187 | |||
| 188 | if ($self->{'_query'}->can('every_cookie')) { | ||
| 189 | @values = @{$self->{'_query'}->every_cookie($key)}; | ||
| 190 | } else { | ||
| 191 | @values = $self->{'_query'}->cookie($key); | ||
| 192 | } | ||
| 193 | |||
| 194 | $cookies{$key} = \@values; | ||
| 195 | } | ||
| 196 | |||
| 197 | foreach my $path (@{$threats}) { | ||
| 198 | my @path_split = $self->split_path($path); | ||
| 199 | |||
| 200 | if ($#path_split < 1) { | ||
| 201 | next; | ||
| 202 | } | ||
| 203 | |||
| 204 | my $key = $self->unescape_key($path_split[1]); | ||
| 205 | |||
| 206 | if ($path_split[0] eq 'SERVER') { | ||
| 207 | $self->{'_query'}->req->headers->header($key, ''); | ||
| 208 | } elsif ($path_split[0] eq 'COOKIE') { | ||
| 209 | if ($#path_split == 1) { | ||
| 210 | $cookies{$key} = ['']; | ||
| 211 | } else { | ||
| 212 | my $array = $cookies{$key}; | ||
| 213 | $array->[$path_split[2]] = ''; | ||
| 214 | $cookies{$key} = $array; | ||
| 215 | } | ||
| 216 | } else { | ||
| 217 | if ($#path_split == 1) { | ||
| 218 | $self->{'_query'}->req->param($key, ''); | ||
| 219 | } else { | ||
| 220 | my @values; | ||
| 221 | |||
| 222 | if ($self->{'_query'}->can('every_param')) { | ||
| 223 | @values = @{$self->{'_query'}->every_param($key)}; | ||
| 224 | } else { | ||
| 225 | @values = $self->{'_query'}->param($key); | ||
| 226 | } | ||
| 227 | |||
| 228 | $values[$path_split[2]] = ''; | ||
| 229 | $self->{'_query'}->req->param($key, @values); | ||
| 230 | } | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | if ($self->{'_query'}->req->headers->cookie) { | ||
| 235 | my $cookie_string = ''; | ||
| 236 | |||
| 237 | # No encoding on purpose. That's how Mojolicious roles. | ||
| 238 | foreach my $key (keys %cookies) { | ||
| 239 | $self->{'_query'}->cookie(@{$cookies{$key}}); | ||
| 240 | |||
| 241 | foreach my $value (@{$cookies{$key}}) { | ||
| 242 | $cookie_string .= $key . '=' . $value . ';'; | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | # Remove last semicolon. | ||
| 247 | chop($cookie_string); | ||
| 248 | |||
| 249 | # Overwrite the cookie string. | ||
| 250 | $self->{'_query'}->req->headers->cookie($cookie_string); | ||
| 251 | } | ||
| 252 | |||
| 253 | # Don't stop the complete request. | ||
| 254 | return 1; | ||
| 255 | } | ||
| 256 | |||
| 257 | =head2 gather_hashes() | ||
| 258 | |||
| 259 | This module does not support the integrity check, because everything is routed through one file. | ||
| 260 | |||
| 261 | =cut | ||
| 262 | |||
| 263 | sub gather_hashes { | ||
| 264 | my ($self) = @_; | ||
| 265 | |||
| 266 | $self->{'_hashes'} = {}; | ||
| 267 | } | ||
| 268 | |||
| 269 | =head2 error() | ||
| 270 | |||
| 271 | This method renders an error message with the help of the controller. | ||
| 272 | |||
| 273 | =cut | ||
| 274 | |||
| 275 | sub error { | ||
| 276 | my ($self) = @_; | ||
| 277 | |||
| 278 | $self->{'_query'}->render(data => '<h1>500 Internal Server Error</h1>', status => 500); | ||
| 279 | } | ||
| 280 | |||
| 281 | =head1 AUTHOR | ||
| 282 | |||
| 283 | Hendrik Buchwald, C<< <hb@zecure.org> >> | ||
| 284 | |||
| 285 | =head1 BUGS | ||
| 286 | |||
| 287 | Please report any bugs or feature requests to C<bug-shadowd-connector@rt.cpan.org>, or through the web interface at | ||
| 288 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Shadowd-Connector>. I will be notified, and then you'll automatically | ||
| 289 | be notified of progress on your bug as I make changes. | ||
| 290 | |||
| 291 | It is also possible to report bugs via Github at L<https://github.com/zecure/shadowd_perl/issues>. | ||
| 292 | |||
| 293 | =head1 SUPPORT | ||
| 294 | |||
| 295 | You can find documentation for this module with the perldoc command. | ||
| 296 | |||
| 297 | perldoc Shadowd::Connector | ||
| 298 | |||
| 299 | |||
| 300 | You can also look for information at: | ||
| 301 | |||
| 302 | =over 4 | ||
| 303 | |||
| 304 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 305 | |||
| 306 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Shadowd-Connector> | ||
| 307 | |||
| 308 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 309 | |||
| 310 | L<http://annocpan.org/dist/Shadowd-Connector> | ||
| 311 | |||
| 312 | =item * CPAN Ratings | ||
| 313 | |||
| 314 | L<http://cpanratings.perl.org/d/Shadowd-Connector> | ||
| 315 | |||
| 316 | =item * Search CPAN | ||
| 317 | |||
| 318 | L<http://search.cpan.org/dist/Shadowd-Connector/> | ||
| 319 | |||
| 320 | =back | ||
| 321 | |||
| 322 | =head1 LICENSE AND COPYRIGHT | ||
| 323 | |||
| 324 | Shadow Daemon -- Web Application Firewall | ||
| 325 | |||
| 326 | Copyright (C) 2014-2016 Hendrik Buchwald C<< <hb@zecure.org> >> | ||
| 327 | |||
| 328 | This file is part of Shadow Daemon. Shadow Daemon is free software: you can | ||
| 329 | redistribute it and/or modify it under the terms of the GNU General Public | ||
| 330 | License as published by the Free Software Foundation, version 2. | ||
| 331 | |||
| 332 | This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 333 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 334 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 335 | details. | ||
| 336 | |||
| 337 | You should have received a copy of the GNU General Public License | ||
| 338 | along with this program. If not, see L<http://www.gnu.org/licenses/>. | ||
| 339 | |||
| 340 | =cut | ||
| 341 | |||
| 342 | 1; # End of Shadowd::Connector::Mojolicious |