| 1 |
#BSD Copyright (c) 2009, Jeff Forristal (wiretrip.net) |
| 2 |
#BSD All rights reserved. |
| 3 |
#BSD |
| 4 |
#BSD Redistribution and use in source and binary forms, with or without |
| 5 |
#BSD modification, are permitted provided that the following conditions |
| 6 |
#BSD are met: |
| 7 |
#BSD |
| 8 |
#BSD - Redistributions of source code must retain the above copyright |
| 9 |
#BSD notice, this list of conditions and the following disclaimer. |
| 10 |
#BSD |
| 11 |
#BSD - Redistributions in binary form must reproduce the above copyright |
| 12 |
#BSD notice, this list of conditions and the following disclaimer in the |
| 13 |
#BSD documentation and/or other materials provided with the distribution. |
| 14 |
#BSD |
| 15 |
#BSD THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 |
#BSD "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 |
#BSD LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 |
#BSD FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 |
#BSD COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 |
#BSD INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 |
#BSD BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 |
#BSD LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 |
#BSD CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 24 |
#BSD LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 25 |
#BSD ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
#BSD POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
######################################################################## |
| 29 |
|
| 30 |
{ # package variables |
| 31 |
my $MIMEBASE64_TRYLOADING = 1; |
| 32 |
|
| 33 |
######################################################################## |
| 34 |
|
| 35 |
=item B<encode_base64> |
| 36 |
|
| 37 |
Params: $data [, $eol] |
| 38 |
|
| 39 |
Return: $b64_encoded_data |
| 40 |
|
| 41 |
This function does Base64 encoding. If the binary MIME::Base64 module |
| 42 |
is available, it will use that; otherwise, it falls back to an internal |
| 43 |
perl version. The perl version carries the following copyright: |
| 44 |
|
| 45 |
Copyright 1995-1999 Gisle Aas <[email protected]> |
| 46 |
|
| 47 |
NOTE: the $eol parameter will be inserted every 76 characters. This is |
| 48 |
used to format the data for output on a 80 character wide terminal. |
| 49 |
|
| 50 |
=cut |
| 51 |
|
| 52 |
sub encode_base64 { |
| 53 |
if ($MIMEBASE64_TRYLOADING) { |
| 54 |
eval "require MIME::Base64"; |
| 55 |
$MIMEBASE64_TRYLOADING = 0; |
| 56 |
} |
| 57 |
goto &MIME::Base64::encode_base64 if ($MIME::Base64::VERSION); |
| 58 |
my $res = ""; |
| 59 |
my $eol = $_[1]; |
| 60 |
$eol = "\n" unless defined $eol; |
| 61 |
pos( $_[0] ) = 0; |
| 62 |
while ( $_[0] =~ /(.{1,45})/gs ) { |
| 63 |
$res .= substr( pack( 'u', $1 ), 1 ); |
| 64 |
chop($res); |
| 65 |
} |
| 66 |
$res =~ tr|` -_|AA-Za-z0-9+/|; |
| 67 |
my $padding = ( 3 - length( $_[0] ) % 3 ) % 3; |
| 68 |
$res =~ s/.{$padding}$/'=' x $padding/e if $padding; |
| 69 |
if ( length $eol ) { |
| 70 |
$res =~ s/(.{1,76})/$1$eol/g; |
| 71 |
} |
| 72 |
$res; |
| 73 |
} |
| 74 |
|
| 75 |
######################################################################## |
| 76 |
|
| 77 |
=item B<decode_base64> |
| 78 |
|
| 79 |
Params: $data |
| 80 |
|
| 81 |
Return: $b64_decoded_data |
| 82 |
|
| 83 |
A perl implementation of base64 decoding. The perl code for this function |
| 84 |
was actually taken from an older MIME::Base64 perl module, and bears the |
| 85 |
following copyright: |
| 86 |
|
| 87 |
Copyright 1995-1999 Gisle Aas <[email protected]> |
| 88 |
|
| 89 |
=cut |
| 90 |
|
| 91 |
sub decode_base64 { |
| 92 |
if ($MIMEBASE64_TRYLOADING) { |
| 93 |
eval "require MIME::Base64"; |
| 94 |
$MIMEBASE64_TRYLOADING = 0; |
| 95 |
} |
| 96 |
goto &MIME::Base64::decode_base64 if ($MIME::Base64::VERSION); |
| 97 |
my $str = shift; |
| 98 |
my $res = ""; |
| 99 |
$str =~ tr|A-Za-z0-9+=/||cd; |
| 100 |
$str =~ s/=+$//; # remove padding |
| 101 |
$str =~ tr|A-Za-z0-9+/| -_|; # convert to uuencoded format |
| 102 |
while ( $str =~ /(.{1,60})/gs ) { |
| 103 |
my $len = chr( 32 + length($1) * 3 / 4 ); # compute length byte |
| 104 |
$res .= unpack( "u", $len . $1 ); # uudecode |
| 105 |
} |
| 106 |
$res; |
| 107 |
} |
| 108 |
|
| 109 |
######################################################################## |
| 110 |
|
| 111 |
} # end package variables |
| 112 |
|
| 113 |
######################################################################## |
| 114 |
|
| 115 |
=item B<encode_uri_hex> |
| 116 |
|
| 117 |
Params: $data |
| 118 |
|
| 119 |
Return: $result |
| 120 |
|
| 121 |
This function encodes every character (except the / character) with normal |
| 122 |
URL hex encoding. |
| 123 |
|
| 124 |
=cut |
| 125 |
|
| 126 |
sub encode_uri_hex { # normal hex encoding |
| 127 |
my $str = shift; |
| 128 |
$str =~ s/([^\/])/sprintf("%%%02x",ord($1))/ge; |
| 129 |
return $str; |
| 130 |
} |
| 131 |
|
| 132 |
######################################################################### |
| 133 |
|
| 134 |
=item B<encode_uri_randomhex> |
| 135 |
|
| 136 |
Params: $data |
| 137 |
|
| 138 |
Return: $result |
| 139 |
|
| 140 |
This function randomly encodes characters (except the / character) with |
| 141 |
normal URL hex encoding. |
| 142 |
|
| 143 |
=cut |
| 144 |
|
| 145 |
sub encode_uri_randomhex { # random normal hex encoding |
| 146 |
my @T = split( //, shift ); |
| 147 |
my $s; |
| 148 |
foreach (@T) { |
| 149 |
if (m#[;=:&@\?]#) { |
| 150 |
$s .= $_; |
| 151 |
next; |
| 152 |
} |
| 153 |
if ( ( rand() * 2 ) % 2 == 1 ) { $s .= sprintf( "%%%02x", ord($_) ); } |
| 154 |
else { $s .= $_; } |
| 155 |
} |
| 156 |
return $s; |
| 157 |
} |
| 158 |
|
| 159 |
######################################################################### |
| 160 |
|
| 161 |
=item B<encode_uri_randomcase> |
| 162 |
|
| 163 |
Params: $data |
| 164 |
|
| 165 |
Return: $result |
| 166 |
|
| 167 |
This function randomly changes the case of characters in the string. |
| 168 |
|
| 169 |
=cut |
| 170 |
|
| 171 |
sub encode_uri_randomcase { |
| 172 |
my ( $x, $uri ) = ( '', shift ); |
| 173 |
return $uri if ( $uri !~ tr/a-zA-Z// ); # fast-path |
| 174 |
my @T = split( //, $uri ); |
| 175 |
for ( $x = 0 ; $x < ( scalar @T ) ; $x++ ) { |
| 176 |
if ( ( rand() * 2 ) % 2 == 1 ) { |
| 177 |
$T[$x] =~ tr/A-Za-z/a-zA-Z/; |
| 178 |
} |
| 179 |
} |
| 180 |
return join( '', @T ); |
| 181 |
} |
| 182 |
|
| 183 |
######################################################################### |
| 184 |
|
| 185 |
=item B<encode_unicode> |
| 186 |
|
| 187 |
Params: $data |
| 188 |
|
| 189 |
Return: $result |
| 190 |
|
| 191 |
This function converts a normal string into Windows unicode format |
| 192 |
(non-overlong or anything fancy). |
| 193 |
|
| 194 |
=cut |
| 195 |
|
| 196 |
sub encode_unicode { |
| 197 |
my ( $c, $r ) = ( '', '' ); |
| 198 |
foreach $c ( split( //, shift ) ) { |
| 199 |
$r .= pack( "v", ord($c) ); |
| 200 |
} |
| 201 |
return $r; |
| 202 |
} |
| 203 |
|
| 204 |
######################################################################### |
| 205 |
|
| 206 |
=item B<decode_unicode> |
| 207 |
|
| 208 |
Params: $unicode_string |
| 209 |
|
| 210 |
Return: $decoded_string |
| 211 |
|
| 212 |
This function attempts to decode a unicode (UTF-8) string by |
| 213 |
converting it into a single-byte-character string. Overlong |
| 214 |
characters are converted to their standard characters in place; |
| 215 |
non-overlong (aka multi-byte) characters are substituted with the |
| 216 |
0xff; invalid encoding characters are left as-is. |
| 217 |
|
| 218 |
Note: this function is useful for dealing with the various unicode |
| 219 |
exploits/vulnerabilities found in web servers; it is *not* good for |
| 220 |
doing actual UTF-8 parsing, since characters over a single byte are |
| 221 |
basically dropped/replaced with a placeholder. |
| 222 |
|
| 223 |
=cut |
| 224 |
|
| 225 |
sub decode_unicode { |
| 226 |
my $str = $_[0]; |
| 227 |
return $str if ( $str !~ tr/!-~//c ); # fastpath |
| 228 |
my ( $lead, $count, $idx ); |
| 229 |
my $out = ''; |
| 230 |
my $len = length($str); |
| 231 |
my ( $ptr, $no, $nu ) = ( 0, 0, 0 ); |
| 232 |
|
| 233 |
while ( $ptr < $len ) { |
| 234 |
my $c = substr( $str, $ptr, 1 ); |
| 235 |
if ( ord($c) >= 0xc0 && ord($c) <= 0xfd ) { |
| 236 |
$count = 0; |
| 237 |
$c = ord($c) << 1; |
| 238 |
while ( ( $c & 0x80 ) == 0x80 ) { |
| 239 |
$c <<= 1; |
| 240 |
last if ( $count++ == 4 ); |
| 241 |
} |
| 242 |
$c = ( $c & 0xff ); |
| 243 |
for ( $idx = 1 ; $idx < $count ; $idx++ ) { |
| 244 |
my $o = ord( substr( $str, $ptr + $idx, 1 ) ); |
| 245 |
$no = 1 if ( $o != 0x80 ); |
| 246 |
$nu = 1 if ( $o < 0x80 || $o > 0xbf ); |
| 247 |
} |
| 248 |
my $o = ord( substr( $str, $ptr + $idx, 1 ) ); |
| 249 |
$nu = 1 if ( $o < 0x80 || $o > 0xbf ); |
| 250 |
if ($nu) { |
| 251 |
$out .= substr( $str, $ptr++, 1 ); |
| 252 |
} |
| 253 |
else { |
| 254 |
if ($no) { |
| 255 |
$out .= "\xff"; # generic replacement char |
| 256 |
} |
| 257 |
else { |
| 258 |
my $prior = |
| 259 |
ord( substr( $str, $ptr + $count - 1, 1 ) ) << 6; |
| 260 |
$out .= pack( "C", |
| 261 |
(( ord( substr( $str, $ptr + $count, 1 ) ) & 0x7f ) + |
| 262 |
$prior ) & 255 ); |
| 263 |
} |
| 264 |
$ptr += $count + 1; |
| 265 |
} |
| 266 |
$no = $nu = 0; |
| 267 |
} |
| 268 |
else { |
| 269 |
$out .= $c; |
| 270 |
$ptr++; |
| 271 |
} |
| 272 |
} |
| 273 |
return $out; |
| 274 |
} |
| 275 |
|
| 276 |
######################################################################## |
| 277 |
|
| 278 |
=item B<encode_anti_ids> |
| 279 |
|
| 280 |
Params: \%request, $modes |
| 281 |
|
| 282 |
Return: nothing |
| 283 |
|
| 284 |
encode_anti_ids computes the proper anti-ids encoding/tricks |
| 285 |
specified by $modes, and sets up %hin in order to use those tricks. |
| 286 |
Valid modes are (the mode numbers are the same as those found in whisker |
| 287 |
1.4): |
| 288 |
|
| 289 |
=over 4 |
| 290 |
|
| 291 |
=item 1 Encode some of the characters via normal URL encoding |
| 292 |
|
| 293 |
=item 2 Insert directory self-references (/./) |
| 294 |
|
| 295 |
=item 3 Premature URL ending (make it appear the request line is done) |
| 296 |
|
| 297 |
=item 4 Prepend a long random string in the form of "/string/../URL" |
| 298 |
|
| 299 |
=item 5 Add a fake URL parameter |
| 300 |
|
| 301 |
=item 6 Use a tab instead of a space as a request spacer |
| 302 |
|
| 303 |
=item 7 Change the case of the URL (works against Windows and Novell) |
| 304 |
|
| 305 |
=item 8 Change normal seperators ('/') to Windows version ('\') |
| 306 |
|
| 307 |
=item 9 Session splicing [NOTE: not currently available] |
| 308 |
|
| 309 |
=item A Use a carriage return (0x0d) as a request spacer |
| 310 |
|
| 311 |
=item B Use binary value 0x0b as a request spacer |
| 312 |
|
| 313 |
=back |
| 314 |
|
| 315 |
You can set multiple modes by setting the string to contain all the modes |
| 316 |
desired; i.e. $modes="146" will use modes 1, 4, and 6. |
| 317 |
|
| 318 |
=cut |
| 319 |
|
| 320 |
sub encode_anti_ids { |
| 321 |
my ( $rhin, $modes ) = ( shift, shift ); |
| 322 |
my ( @T, $x, $c, $s, $y ); |
| 323 |
my $ENCODED = 0; |
| 324 |
my $W = $$rhin{'whisker'}; |
| 325 |
|
| 326 |
return if ( !( defined $rhin && ref($rhin) ) ); |
| 327 |
|
| 328 |
# in case they didn't do it already |
| 329 |
$$rhin{'whisker'}->{'uri_orig'} = $$rhin{'whisker'}->{'uri'}; |
| 330 |
|
| 331 |
# note: order is important! |
| 332 |
|
| 333 |
# mode 9 - session splicing |
| 334 |
#if($modes=~/9/){ |
| 335 |
# $$rhin{'whisker'}->{'ids_session_splice'}=1; |
| 336 |
#} |
| 337 |
|
| 338 |
# mode 4 - prepend long random string |
| 339 |
if ( $modes =~ /4/ ) { |
| 340 |
$s = ''; |
| 341 |
if ( $$W{'uri'} =~ m#^/# ) { |
| 342 |
$y = &utils_randstr; |
| 343 |
$s .= $y while ( length($s) < 512 ); |
| 344 |
$$W{'uri'} = "/$s/.." . $$W{'uri'}; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
# mode 7 - (windows) random case sensitivity |
| 349 |
if ( $modes =~ /7/ ) { |
| 350 |
$$W{'uri'} = encode_uri_randomcase( $$W{'uri'} ); |
| 351 |
} |
| 352 |
|
| 353 |
# mode 2 - directory self-reference (/./) |
| 354 |
if ( $modes =~ /2/ ) { |
| 355 |
$$W{'uri'} =~ s#/#/./#g; |
| 356 |
} |
| 357 |
|
| 358 |
# mode 8 - windows directory separator (\) |
| 359 |
if ( $modes =~ /8/ ) { |
| 360 |
$$W{'uri'} =~ s#/#\\#g; |
| 361 |
$$W{'uri'} =~ s#^\\#/#; |
| 362 |
$$W{'uri'} =~ s#^([a-zA-Z0-9_]+):\\#$1://#; |
| 363 |
$$W{'uri'} =~ s#\\$#/#; |
| 364 |
} |
| 365 |
|
| 366 |
# mode 1 - random URI (non-UTF8) encoding |
| 367 |
if ( $modes =~ /1/ ) { |
| 368 |
if ( $ENCODED == 0 ) { |
| 369 |
$$W{'uri'} = encode_uri_randomhex( $$W{'uri'} ); |
| 370 |
$ENCODED = 1; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
# mode 5 - fake parameter |
| 375 |
if ( $modes =~ /5/ ) { |
| 376 |
( $s, $y ) = ( &utils_randstr, &utils_randstr ); |
| 377 |
$$W{'uri'} = "/$s.html%3F$y=/../$$W{'uri'}"; |
| 378 |
} |
| 379 |
|
| 380 |
# mode 3 - premature URL ending |
| 381 |
if ( $modes =~ /3/ ) { |
| 382 |
$s = &utils_randstr; |
| 383 |
$$W{'uri'} = "/%20HTTP/1.1%0d%0aAccept%3a%20$s/../..$$W{'uri'}"; |
| 384 |
} |
| 385 |
|
| 386 |
# mode 6 - TAB as request spacer |
| 387 |
if ( $modes =~ /6/ ) { |
| 388 |
$$W{'http_space1'} = "\t"; |
| 389 |
} |
| 390 |
|
| 391 |
# mode A - CR as request spacer |
| 392 |
if ( $modes =~ /A/i ) { |
| 393 |
$$W{'http_space1'} = $$W{'http_space2'} = "\x0d"; |
| 394 |
} |
| 395 |
|
| 396 |
# mode B - 0x0b as request spacer |
| 397 |
if ( $modes =~ /B/i ) { |
| 398 |
$$W{'http_space1'} = $$W{'http_space2'} = "\x0b"; |
| 399 |
} |
| 400 |
|
| 401 |
} |
| 402 |
|