| 1 | package eris::log::context::attacks::url; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Inspects URL's for common attack patterns | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Const::Fast; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | with qw( | |||||||||||||||||||||||||||
| 10 | eris::role::context | ||||||||||||||||||||||||||||
| 11 | ); | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | our $VERSION = '0.008'; # VERSION | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | my %SUSPICIOUS = (); | |||||||||||||||||||||||||||
| 17 | # Not significant on their own | ||||||||||||||||||||||||||||
| 18 | 1 | const my %NeedsMore => map { $_ => 1 } qw(select union update table sleep alter alert drop delete rand > \\), '&#'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | sub BUILD { | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # Initialize things to prevent running code at compile time | ||||||||||||||||||||||||||||
| 24 | # | ||||||||||||||||||||||||||||
| 25 | # Generic Attack Strings | ||||||||||||||||||||||||||||
| 26 | 0 | ▶ | my @Generic = map { quotemeta } qw( | ||||||||||||||||||||||||||
|
Subroutine
BUILD: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | etc/passwd etc/shadow /* */ | ||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||
| 29 | 0 | push @Generic, q{\\\\(?!x)}, q{bin/[a-z]*sh}, q{\w+\.(?:exe|dll|bat|cgi)\b}; | |||||||||||||||||||||||||||
| 30 | 0 | unshift @Generic, q|\.\.(?:[\\\/]\.{0,2})+|; | |||||||||||||||||||||||||||
| 31 | 0 | $SUSPICIOUS{generic} = join '|', @Generic; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # SQL Injections | ||||||||||||||||||||||||||||
| 34 | 0 | my @SQLI = map { qr/(?<=[^a-z_\-=])$_(?![a-z_\-=])/ } map { quotemeta } qw( | |||||||||||||||||||||||||||
| 35 | insert update delete drop alter select union table sleep rand char chr | ||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | 0 | push @SQLI, qr/or\s+1=1\s*;\s*--/; | |||||||||||||||||||||||||||
| 38 | 0 | $SUSPICIOUS{sqli} = join '|', @SQLI; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # XSS Attempts | ||||||||||||||||||||||||||||
| 41 | 0 | my @XSS = map { qr/(?<=[^a-z_\-=])$_(?![a-z_\-=])/ } map { quotemeta } qw( | |||||||||||||||||||||||||||
| 42 | script alert onerror onload | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | 0 | push @XSS, map { quotemeta } qw( | |||||||||||||||||||||||||||
| 45 | --> > '; | ||||||||||||||||||||||||||||
| 46 | ), '&#'; | ||||||||||||||||||||||||||||
| 47 | 0 | $SUSPICIOUS{xss} = join('|', @XSS); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | const %SUSPICIOUS => %SUSPICIOUS; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | sub _build_priority { 100 } | ||||||||||||||||||||||||||
|
Subroutine
_build_priority: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub _build_field { '_exists_' } | ||||||||||||||||||||||||||
|
Subroutine
_build_field: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub _build_matcher { qr/(?:_ur[li]$)|(?:^resource$)/ } | ||||||||||||||||||||||||||
|
Subroutine
_build_matcher: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub sample_messages { | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | my @msgs = map { encode_json($_) } ( | ||||||||||||||||||||||||||
|
Subroutine
sample_messages: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 65 | { resource => "https://www.example.com/?t='%20OR%201=1;--" }, | ||||||||||||||||||||||||||||
| 66 | { resource => "https://www.example.com/../../../etc/passwd" }, | ||||||||||||||||||||||||||||
| 67 | { resource => "https://www.example.com/?q='><script>alert(1);</script>" }, | ||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||
| 69 | 0 | return @msgs; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | sub contextualize_message { | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | my ($self,$log) = @_; | ||||||||||||||||||||||||||
|
Subroutine
contextualize_message: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | my $ctxt = $log->context; | |||||||||||||||||||||||||||
| 77 | 0 | my $re = $self->matcher; | |||||||||||||||||||||||||||
| 78 | 0 | my %add = (); | |||||||||||||||||||||||||||
| 79 | 0 | my $score = 0; | |||||||||||||||||||||||||||
| 80 | 0 | my %tokens = (); | |||||||||||||||||||||||||||
| 81 | 0 | my %tags = (); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | foreach my $f ( keys %{ $ctxt } ) { | |||||||||||||||||||||||||||
| 84 | 0 | ▶ | next unless $f =~ /$re/o; # Optimize here, this will always be the same pattern | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # Normalize (Lower casing, Unescaping) | ||||||||||||||||||||||||||||
| 87 | 0 | my $url = lc $ctxt->{$f} =~ s/%([0-9a-f]{2})/chr(hex($1))/reg; | |||||||||||||||||||||||||||
| 88 | 0 | my %attack = (); | |||||||||||||||||||||||||||
| 89 | 0 | my @badness = (); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # We need to call each of these one at a time. Since our regexes live | ||||||||||||||||||||||||||||
| 92 | # in a hash, we can only optimize if they won't change. | ||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if( my @sqli = ($url =~ /$SUSPICIOUS{sqli}/go ) ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 94 | 0 | push @badness, @sqli; | |||||||||||||||||||||||||||
| 95 | 0 | $attack{tags} = 'sqli'; | |||||||||||||||||||||||||||
| 96 | 0 | $tags{sqli} = 1; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | elsif( my @xss = ($url =~ /$SUSPICIOUS{xss}/go ) ) { | ||||||||||||||||||||||||||||
| 99 | 0 | push @badness, @xss; | |||||||||||||||||||||||||||
| 100 | 0 | $attack{tags} = 'xss'; | |||||||||||||||||||||||||||
| 101 | 0 | $tags{xss} = 1; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | elsif( my @generic = ($url =~ /$SUSPICIOUS{generic}/go ) ) { | ||||||||||||||||||||||||||||
| 104 | 0 | push @badness, @generic; | |||||||||||||||||||||||||||
| 105 | 0 | $attack{tags} = 'generic'; | |||||||||||||||||||||||||||
| 106 | 0 | $tags{generic} = 1; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 0 | ▶ | next unless @badness; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # Extract the unique tokens for this field and globally | ||||||||||||||||||||||||||||
| 111 | 0 | my %uniq; | |||||||||||||||||||||||||||
| 112 | 0 | foreach my $token (@badness) { | |||||||||||||||||||||||||||
| 113 | 0 | $uniq{$token} = $tokens{$token} = 1; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | # Check that we're not squatting on a single english word | ||||||||||||||||||||||||||||
| 116 | 0 | my($t) = keys %uniq; | |||||||||||||||||||||||||||
| 117 | 0 | ▶ | if( keys(%uniq) == 1 && exists $NeedsMore{$t} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
keys %uniq == 1 and exists $NeedsMore{$t}
MC/DC0%0/2
keys %uniq == 1 and exists $NeedsMore{$t}
keys %uniq == 1
exists $NeedsMore{$t}
Truth table0%0/3
A and B
A= keys %uniq == 1B= exists $NeedsMore{$t}
| |||||||||||||||||||||||||||||
| 118 | 0 | next; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | # Store the Score and Tokens | ||||||||||||||||||||||||||||
| 121 | 0 | $score += $attack{score} = @badness; | |||||||||||||||||||||||||||
| 122 | 0 | $attack{tokens} = [ sort keys %uniq ]; | |||||||||||||||||||||||||||
| 123 | 0 | $add{$f} = \%attack; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if( keys %add ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | # Continue summing incase other things added scores. | ||||||||||||||||||||||||||||
| 128 | 0 | $log->add_context($self->name, { | |||||||||||||||||||||||||||
| 129 | attacks => \%add, | ||||||||||||||||||||||||||||
| 130 | attack_score => $score, | ||||||||||||||||||||||||||||
| 131 | attack_tokens => [ sort keys %tokens ], | ||||||||||||||||||||||||||||
| 132 | attack_type => [ sort keys %tags ], | ||||||||||||||||||||||||||||
| 133 | }); | ||||||||||||||||||||||||||||
| 134 | 0 | $tags{security} = 1; | |||||||||||||||||||||||||||
| 135 | 0 | $log->add_tags(keys %tags); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 1 | 1; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | __END__ | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =pod | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head1 NAME | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | eris::log::context::attacks::url - Inspects URL's for common attack patterns | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head1 VERSION | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | version 0.008 | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | This context matches any field ending in '_url' and inspects the URL for common | ||||||||||||||||||||||||||||
| 159 | attack patterns. This is not sophisticated, but leverages the reconnaissance | ||||||||||||||||||||||||||||
| 160 | stage of an attack in which attackers try unsophisticated things to look for | ||||||||||||||||||||||||||||
| 161 | weak spots in your infrastructure. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | It was built on the "least work for most reward" principle. This context is | ||||||||||||||||||||||||||||
| 164 | prone to false positives and false negatives, but works fast enough to be | ||||||||||||||||||||||||||||
| 165 | inlined into the log processing pipeline. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =head2 priority | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Defaults to 100, running after most other contexts so things can | ||||||||||||||||||||||||||||
| 172 | end up in the right fields. | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =head2 field | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | Defaults to '_exists_', meaning it's looking for the presence of certain | ||||||||||||||||||||||||||||
| 177 | keys in the L<eris::log> context. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head2 matcher | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | Defaults to matching the fields ending with '_url', '_uri', or fields exact matching 'resource'. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =head1 METHODS | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head2 contextualize_message | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | Takes an L<eris::log> instance, parses the fields 'resource' and 'referer' for | ||||||||||||||||||||||||||||
| 188 | attack patterns. | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | Provides 3 top level keys to the context: | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =over 2 | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =item B<attack_score> | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | The higher the number, the more likely an attack has been detected. Takes the | ||||||||||||||||||||||||||||
| 197 | HTTP response code into account if available. | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =item B<attack_triggers> | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | This is the count of distinct tokens detected in the URL leading us to believe this | ||||||||||||||||||||||||||||
| 202 | is an attack. | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =item B<attacks> | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | This is a HashRef containing all the tokens and attack signatures tripped. | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =back | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | Tags messages with 'security' if an attack string is detected. | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =for Pod::Coverage BUILD | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =for Pod::Coverage sample_messages | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | L<eris::log::contextualizer>, L<eris::role::context> | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | Brad Lhotsky <brad@divisionbyzero.net> | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | This software is Copyright (c) 2015 by Brad Lhotsky. | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | The (three-clause) BSD License | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =cut | ||||||||||||||||||||||||||||