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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
|
/*
** Copyright (C) 1998,1999,2000,2001 Martin Roesch <roesch@clark.net>
**
** 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 2 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, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/****************************************************
* spp_unidecode.h
*
* decodes UTF-8 encoded requests.
*
* Notes:
* Arguments are the same as the http_decode plugin
* Checks for NULL CGI and Unicode Directory Trans
*****************************************************/
/* $Id: spp_unidecode.c,v 1.8 2001/07/12 16:23:28 fygrave Exp $ */
#include "spp_unidecode.h"
#include "codes.h"
#include "decode.h"
#include <ctype.h>
#define MODNAME "spp_unidecode"
#define NOCGINULL "-cginull"
#define NOUNICODE "-unicode"
int Ucheck_cgi_null = 1;
int Ucheck_iis_unicode = 1;
int Uparse_arg;
extern char *file_name;
extern int file_line;
extern int do_detect;
/* Instantiate the list of ports we're going to watch */
UPortList UnidecodePorts;
/*local function declares*/
int TranslateUnicode(char* InBuff, int InLength, char* OutBuff, int OutLength, Packet* p);
int GetNextChar(char* Buff, char* NextChar, Packet* p);
void CheckNull(unsigned long c, Packet* p);
void CheckDirTrans(unsigned long c, Packet* p);
void LogUnknown(Packet* p);
void LogInvalid(Packet* p);
/*
* Function: SetupHttpDecode()
*
* Purpose: Registers the preprocessor keyword and initialization
* function into the preprocessor list.
*
* Arguments: None.
*
* Returns: void function
*
*/
void SetupUnidecode()
{
/* link the preprocessor keyword to the init function in
the preproc list */
RegisterPreprocessor("unidecode", UnidecodeInit);
#ifdef DEBUG
printf("Preprocessor: Unidecode in setup...\n");
#endif
}
/*
* Function: HttpDecodeInit(u_char *)
*
* Purpose: Processes the args sent to the preprocessor, sets up the
* port list, links the processing function into the preproc
* function list
*
* Arguments: args => ptr to argument string
*
* Returns: void function
*
*/
void UnidecodeInit(u_char *args)
{
#ifdef DEBUG
printf("Preprocessor: HttpDecode Initialized\n");
#endif
/* parse the argument list into a list of ports to normalize */
USetPorts(args);
init_codes();
/* Set the preprocessor function into the function list */
AddFuncToPreprocList(UPreprocUrlDecode);
}
/*
* Function: SetPorts(char *)
*
* Purpose: Reads the list of port numbers from the argument string and
* parses them into the port list data struct
*
* Arguments: portlist => argument list
*
* Returns: void function
*
*/
void USetPorts(char *portlist)
{
char **toks;
int num_toks;
int num_ports = 0;
int num;
if(portlist == NULL)
{
FatalError("ERROR %s (%d)=> No arguments to http_decode preprocessor!\n", file_name, file_line);
}
/* tokenize the argument list */
toks = mSplit(portlist, " ", 31, &num_toks, '\\');
/* convert the tokens and place them into the port list */
for(num = 0; num < num_toks; num++)
{
if(!strncmp(NOUNICODE, toks[num], sizeof NOUNICODE))
{
Ucheck_iis_unicode = 0;
}
else if(!strncmp(NOCGINULL, toks[num], sizeof NOCGINULL))
{
Ucheck_cgi_null = 0;
}
else
{
UnidecodePorts.ports[num_ports++] = atoi(toks[num]);
}
}
UnidecodePorts.num_entries = num_ports;
#ifdef DEBUG
printf("Decoding HTTP on %d ports: ", UnidecodePorts.num_entries);
for(num_ports = 0; num_ports < UnidecodePorts.num_entries; num_ports++)
{
printf("%d ", UnidecodePorts.ports[num_ports]);
}
printf("\n");
#endif
}
/*
* Function: PreprocUrlDecode(Packet *)
*
* Purpose: Inspects the packet's payload for "Escaped" characters and
* converts them back to their ASCII values. This function
* is based on the NCSA code and was contributed by Michael Henry!
*
* Arguments: p => pointer to the current packet data struct
*
* Returns: void function
*
*/
void UPreprocUrlDecode(Packet *p)
{
char *url; /* this is where the converted data will be written */
char *index; /* this is the index pointer to walk thru the data */
char *end; /* points to the end of the payload, for loop control */
u_int16_t psize; /* payload size */
int i; /* loop counter */
#ifdef DEBUG
printf("http decoder init on %d bytes\n", p->dsize);
#endif
/* check to make sure we're talking TCP and that the TWH has already
completed before processing anything */
if(!PacketIsTCP(p))
{
#ifdef DEBUG
printf("It isn't TCP session traffic\n");
#endif
return;
}
if(!IsTcpSessionTraffic(p))
{
#ifdef DEBUG
printf("It isn't TCP session traffic\n");
#endif
return;
}
/* check the port against the decode port list */
for(i = 0; i < UnidecodePorts.num_entries; i++)
{
if(UnidecodePorts.ports[i] == p->dp)
{
/* on match, normalize the data */
#ifdef DEBUG
printf("Got HTTP traffic (%d bytes)!\n", p->dsize);
printf("%s\n", p->data);
#endif
/* setup the pointers */
url = (char *) p->data;
index = (char *) p->data;
end = (char *) p->data + p->dsize;
psize = (u_int16_t) (p->dsize);
Uparse_arg = 0; /* nullify cgi arguments flag */
/* walk thru each char in the payload */
p->dsize = TranslateUnicode(url, p->dsize, index, p->dsize, p);
/* set the payload size to reflect the new size */
//p->dsize = psize;
#ifdef DEBUG
printf("New size: %d\n", p->dsize);
printf("converted data:\n");
PrintNetData(stdout, p->data, p->dsize);
#endif
return;
}
}
}
int TranslateUnicode(char* InBuff, int InLength, char* OutBuff,
int OutLength, Packet* p)
{
int i;
int out_pos;
int num_this_char;
char new_char;
int first_space_found = 0;
int uri_set = 0;
int uri_finished = 0;
char *cur;
DebugMessage(DEBUG_DECODE, "Decoding\n");
out_pos=0;
i=0;
while((num_this_char=GetNextChar(&InBuff[i], &new_char, p)) &&
!uri_finished)
{
cur = OutBuff+out_pos;
if (out_pos!=OutLength){
OutBuff[out_pos++]=new_char;
}
else
{
OutBuff[out_pos]=0;
return out_pos;
}
if(*cur == ' ' && !first_space_found)
{
first_space_found = 1;
DebugMessage(DEBUG_DECODE, "found URI first space\n");
}
if(first_space_found && *cur != ' ' && !uri_set)
{
p->URI.uri = cur;
uri_set = 1;
DebugMessage(DEBUG_DECODE, "set URI at %p\n", p->URI.uri);
}
if(!uri_finished && uri_set &&
(*cur == ' ' || *cur == '\n'))
{
char *foo = p->URI.uri;
p->URI.length = (cur - foo);
uri_finished = 1;
DebugMessage(DEBUG_DECODE, "URI length: %d\n", p->URI.length);
DebugMessage(DEBUG_DECODE, "URI: %s\n", p->URI.uri);
}
i=i+num_this_char;
if (i>InLength){
return out_pos;
}
}
/* after the URI has been decoded, shift any remaining data left and return
* the new size
*/
while(i < InLength)
{
OutBuff[out_pos++] = InBuff[i++];
}
return out_pos;
}
/*******************************************
* returns the number of bytes used in the
* IN buff to make the character
*******************************************/
int GetNextChar(char* Buff, char* NextChar, Packet* p)
{
char Code[3];
unsigned long Hex;
unsigned long Hex2;
unsigned long Hex3;
NextChar[0]=Buff[0];
if (Buff[0]==0){
return 0;
}
if (Buff[0]==0x25)
{
Code[0]=Buff[1];
Code[1]=Buff[2];
Code[2]=0;
Hex = strtoul(Code, NULL, 16);
if ((Hex & 128)==0)
{
NextChar[0] = codes[Hex];
CheckNull(Hex, p);
CheckDirTrans(Hex, p);
return 3;
}
else if ((Hex & 224) == 192)
{
if (Buff[3]!=0x25)
{
DebugMessage(DEBUG_DECODE, "Invalid UTF-8 Code\n");
LogInvalid(p);
NextChar[0]=Buff[0];
return 1;
}
Code[0]=Buff[4];
Code[1]=Buff[5];
Code[2]=0;
Hex2 = strtoul(Code, NULL, 16);
if ((Hex2 & 192)!=128)
{
DebugMessage(DEBUG_DECODE, "Second Byte Invalid\n");
LogInvalid(p);
NextChar[0]=Buff[0];
return 1;
}
Hex = Hex & 31;
Hex2 = Hex2 & 63;
CheckNull((Hex * 64) | Hex2, p);
CheckDirTrans((Hex * 64) | Hex2, p);
if (codes[(Hex * 64) | Hex2]==0)
{
DebugMessage(DEBUG_DECODE, "Unknown mapping\n");
LogUnknown(p);
NextChar[0]=Buff[0];
return 1;
}
else
{
NextChar[0] = codes[(Hex * 64) | Hex2];
return 6;
}
}
else if ((Hex & 240) == 224)
{
if (Buff[3]!=0x25)
{
DebugMessage(DEBUG_DECODE, "Invalid UTF-8 Code\n");
LogInvalid(p);
NextChar[0]=Buff[0];
return 1;
}
if (Buff[6]!=0x25)
{
DebugMessage(DEBUG_DECODE, "Invalid UTF-8 Code\n");
LogInvalid(p);
NextChar[0]=Buff[0];
return 1;
}
Code[0]=Buff[4];
Code[1]=Buff[5];
Code[2]=0;
Hex2 = strtoul(Code, NULL, 16);
if ((Hex2 & 192)!=128)
{
DebugMessage(DEBUG_DECODE, "Second Byte Invalid\n");
LogInvalid(p);
NextChar[0]=Buff[0];
return 1;
}
Code[0]=Buff[7];
Code[1]=Buff[8];
Code[2]=0;
Hex3 = strtoul(Code, NULL, 16);
if ((Hex3 & 192)!=128)
{
DebugMessage(DEBUG_DECODE, "Third Byte Invalid\n");
LogInvalid(p);
NextChar[0]=Buff[0];
return 1;
}
Hex = Hex & 15;
Hex2 = Hex2 & 63;
Hex3 = Hex3 & 63;
if ( codes[(Hex * 4096) | (Hex2 * 64) | Hex3] ==0)
{
DebugMessage(DEBUG_DECODE, "Unknown Mapping\n");
LogUnknown(p);
NextChar[0]=Buff[0];
return 1;
}
else
{
NextChar[0] = codes[(Hex * 4096) | (Hex2 * 64) | Hex3];
CheckNull((Hex * 4096) | (Hex2 * 64) | Hex3, p);
CheckDirTrans((Hex * 4096) | (Hex2 * 64) | Hex3, p);
return 9;
}
}
}
else
{
if (Buff[0] == '?') Uparse_arg = 1; /* tail is presumably cgi params. */
NextChar[0] = Buff[0];
}
return 1;
}
/*************************************************
* Check for Null cgi attack
*************************************************/
void CheckNull(unsigned long c, Packet* p){
char logMessage[180];
Event event;
if (!Ucheck_cgi_null || Uparse_arg) return;
if (c==0 && do_detect)
{
snprintf(logMessage, sizeof(logMessage),
MODNAME ": CGI Null Byte attack detected");
SetEvent(&event, GENERATOR_SPP_UNIDECODE,
UNIDECODE_CGINULL_ATTACK, 1, 0, 0, 0);
CallAlertFuncs(p, logMessage, NULL, &event);
CallLogFuncs(p, logMessage, NULL, &event);
do_detect = 0;
}
}
/***************************************************
* Check for the unicode directory transveral attack
***************************************************/
void CheckDirTrans(unsigned long c, Packet* p){
char logMessage[180];
Event event;
if (!Ucheck_iis_unicode || Uparse_arg) return;
if (((c==0x5c) || (c==0x2f) || (c==0x2e)) && do_detect)
{
snprintf(logMessage, sizeof(logMessage),
MODNAME ": Unicode Directory Transversal attack detected");
SetEvent(&event, GENERATOR_SPP_UNIDECODE,
UNIDECODE_DIRECTORY_TRAVERSAL, 1, 0, 0, 0);
CallAlertFuncs(p, logMessage, NULL, &event);
CallLogFuncs(p, logMessage, NULL, &event);
do_detect = 0;
}
}
/****************************************************
* We don't know what this is
****************************************************/
void LogUnknown(Packet* p)
{
char logMessage[180];
Event event;
if(!do_detect)
return;
snprintf(logMessage, sizeof(logMessage),
MODNAME ": Unknown Unicode Mapping. If you know what this maps "
"to contact anonpoet@inconnu.isu.edu.");
SetEvent(&event, GENERATOR_SPP_UNIDECODE,
UNIDECODE_UNKNOWN_MAPPING, 1, 0, 0, 0);
CallAlertFuncs(p, logMessage, NULL, &event);
CallLogFuncs(p, logMessage, NULL, &event);
do_detect = 0;
}
/***************************************************
* This combination can't exist
***************************************************/
void LogInvalid(Packet* p)
{
char logMessage[180];
Event event;
if(!do_detect)
return;
snprintf(logMessage, sizeof(logMessage),
MODNAME ": Invalid Unicode String detected");
SetEvent(&event, GENERATOR_SPP_UNIDECODE,
UNIDECODE_INVALID_MAPPING, 1, 0, 0, 0);
CallAlertFuncs(p, logMessage, NULL, &event);
CallLogFuncs(p, logMessage, NULL, &event);
do_detect = 0;
}
|