From: Victor Julien Date: Thu, 5 Apr 2012 14:42:24 +0000 (+0200) Subject: http: improve multipart parsing, skip empty records. X-Git-Tag: suricata-1.3beta2~97 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=18837dce9247f8a18df8faf2560e902c0f22ef10;p=thirdparty%2Fsuricata.git http: improve multipart parsing, skip empty records. --- diff --git a/src/app-layer-htp-file.c b/src/app-layer-htp-file.c index cbac5a5b23..f14e17f1a0 100644 --- a/src/app-layer-htp-file.c +++ b/src/app-layer-htp-file.c @@ -1189,6 +1189,97 @@ end: return result; } +/** \test empty entries */ +static int HTPFileParserTest10(void) { + int result = 0; + Flow *f = NULL; + uint8_t httpbuf1[] = "POST /upload.cgi HTTP/1.1\r\n" + "Host: www.server.lan\r\n" + "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n" + "Content-Length: 337\r\n" + "\r\n"; + uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */ + + uint8_t httpbuf2[] = "-----------------------------277531038314945\r\n" + "\r\n"; + uint32_t httplen2 = sizeof(httpbuf2) - 1; /* minus the \0 */ + + uint8_t httpbuf3[] = "-----------------------------277531038314945\r\n" + "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n" + "Somereallylongheaderstr: with a good value\r\n" + "\r\n"; + uint32_t httplen3 = sizeof(httpbuf3) - 1; /* minus the \0 */ + + uint8_t httpbuf4[] = "filecontent\r\n" + "-----------------------------277531038314945--"; + uint32_t httplen4 = sizeof(httpbuf4) - 1; /* minus the \0 */ + + TcpSession ssn; + HtpState *http_state = NULL; + + memset(&ssn, 0, sizeof(ssn)); + + f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 80); + if (f == NULL) + goto end; + f->protoctx = &ssn; + + StreamTcpInitConfig(TRUE); + + SCLogDebug("\n>>>> processing chunk 1 <<<<\n"); + int r = AppLayerParse(NULL, f, ALPROTO_HTTP, STREAM_TOSERVER|STREAM_START, httpbuf1, httplen1); + if (r != 0) { + printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r); + result = 0; + goto end; + } + + SCLogDebug("\n>>>> processing chunk 2 size %u <<<<\n", httplen2); + r = AppLayerParse(NULL, f, ALPROTO_HTTP, STREAM_TOSERVER|STREAM_EOF, httpbuf2, httplen2); + if (r != 0) { + printf("toserver chunk 2 returned %" PRId32 ", expected 0: ", r); + result = 0; + goto end; + } + + SCLogDebug("\n>>>> processing chunk 3 size %u <<<<\n", httplen3); + r = AppLayerParse(NULL, f, ALPROTO_HTTP, STREAM_TOSERVER|STREAM_EOF, httpbuf3, httplen3); + if (r != 0) { + printf("toserver chunk 3 returned %" PRId32 ", expected 0: ", r); + result = 0; + goto end; + } + + SCLogDebug("\n>>>> processing chunk 4 size %u <<<<\n", httplen4); + r = AppLayerParse(NULL, f, ALPROTO_HTTP, STREAM_TOSERVER|STREAM_EOF, httpbuf4, httplen4); + if (r != 0) { + printf("toserver chunk 4 returned %" PRId32 ", expected 0: ", r); + result = 0; + goto end; + } + + http_state = f->alstate; + if (http_state == NULL) { + printf("no http state: "); + result = 0; + goto end; + } + + AppLayerDecoderEvents *decoder_events = AppLayerGetDecoderEventsForFlow(f); + if (decoder_events != NULL) { + printf("app events: "); + goto end; + } + + result = 1; +end: + StreamTcpFreeConfig(TRUE); + if (http_state != NULL) + HTPStateFree(http_state); + UTHFreeFlow(f); + return result; +} + #endif /* UNITTESTS */ void HTPFileParserRegisterTests(void) { @@ -1202,5 +1293,6 @@ void HTPFileParserRegisterTests(void) { UtRegisterTest("HTPFileParserTest07", HTPFileParserTest07, 1); UtRegisterTest("HTPFileParserTest08", HTPFileParserTest08, 1); UtRegisterTest("HTPFileParserTest09", HTPFileParserTest09, 1); + UtRegisterTest("HTPFileParserTest10", HTPFileParserTest10, 1); #endif /* UNITTESTS */ } diff --git a/src/app-layer-htp.c b/src/app-layer-htp.c index 694bfa5210..14e7692862 100644 --- a/src/app-layer-htp.c +++ b/src/app-layer-htp.c @@ -1368,7 +1368,10 @@ int HtpRequestBodyHandleMultipart(HtpState *hstate, HtpTxUserData *htud, SCLogDebug("header_len %u", header_len); uint8_t *header = header_start; - if ((uint32_t)(expected_boundary_len + 2) <= header_len) { + /* skip empty records */ + if (expected_boundary_len == header_len) { + goto next; + } else if ((uint32_t)(expected_boundary_len + 2) <= header_len) { header_len -= (expected_boundary_len + 2); header = header_start + (expected_boundary_len + 2); // + for 0d 0a } @@ -1504,7 +1507,7 @@ int HtpRequestBodyHandleMultipart(HtpState *hstate, HtpTxUserData *htud, } else { htud->request_body.body_parsed += (header_end - chunks_buffer); } - +next: SCLogDebug("header_start %p, header_end %p, form_end %p", header_start, header_end, form_end);