67 static inline int StateIsValid(uint16_t alproto,
void *alstate)
69 if (alstate != NULL) {
72 if (htp_state->
conn != NULL) {
91 #ifdef DEBUG_VALIDATION
99 for (; tx_store != NULL; tx_store = tx_store->
next) {
103 store_cnt++, state_cnt++)
106 if (item->
sid == num) {
107 SCLogDebug(
"sid %u already in state: %p %p %p %u %u, direction %s",
108 num, state, dir_state, tx_store, state_cnt,
109 store_cnt, direction & STREAM_TOSERVER ?
"toserver" :
"toclient");
119 const Signature *s, uint32_t inspect_flags, uint8_t direction)
124 &state->
dir_state[(direction & STREAM_TOSERVER) ? 0 : 1];
126 #ifdef DEBUG_VALIDATION
127 BUG_ON(DeStateSearchState(state, direction, s->
num));
131 store = DeStateStoreAlloc();
132 dir_state->
head = store;
133 dir_state->
cur = store;
134 dir_state->
tail = store;
135 }
else if (dir_state->
cur) {
136 store = dir_state->
cur;
138 store = DeStateStoreAlloc();
141 dir_state->
tail = store;
142 dir_state->
cur = store;
175 for (i = 0; i < 2; i++) {
177 while (store != NULL) {
178 store_next = store->
next;
186 static void StoreFileNoMatchCnt(
DetectEngineState *de_state, uint16_t file_no_match, uint8_t direction)
188 de_state->
dir_state[(direction & STREAM_TOSERVER) ? 0 : 1].filestore_cnt += file_no_match;
201 const uint8_t flow_flags,
void *tx,
const uint64_t tx_id,
const uint16_t file_no_match)
203 SCLogDebug(
"tx %"PRIu64
", file_no_match %u", tx_id, file_no_match);
204 StoreFileNoMatchCnt(destate, file_no_match, flow_flags);
205 if (StoreFilestoreSigsCantMatch(sgh, destate, flow_flags)) {
208 f, flow_flags & (STREAM_TOCLIENT | STREAM_TOSERVER), tx, tx_id);
216 Flow *f,
void *tx, uint64_t tx_id,
218 uint32_t inspect_flags, uint8_t flow_flags,
219 const uint16_t file_no_match)
223 if (tx_data == NULL) {
227 if (tx_data->de_state == NULL) {
229 if (tx_data->de_state == NULL)
231 SCLogDebug(
"destate created for %"PRIu64, tx_id);
233 DeStateSignatureAppend(tx_data->de_state, s, inspect_flags, flow_flags);
234 StoreStateTxHandleFiles(sgh, f, tx_data->de_state, flow_flags, tx, tx_id, file_no_match);
262 void *alstate = FlowGetAppState(f);
263 if (!StateIsValid(f->
alproto, alstate)) {
270 uint64_t inspect_tx_id =
MIN(inspect_ts, inspect_tc);
274 for ( ; inspect_tx_id < total_txs; inspect_tx_id++) {
276 if (inspect_tx != NULL) {
280 ResetTxState(txd->de_state);
291 static int DeStateTest01(
void)
293 SCLogDebug(
"sizeof(DetectEngineState)\t\t%"PRIuMAX,
295 SCLogDebug(
"sizeof(DeStateStore)\t\t\t%"PRIuMAX,
297 SCLogDebug(
"sizeof(DeStateStoreItem)\t\t%"PRIuMAX
"",
303 static int DeStateTest02(
void)
305 uint8_t direction = STREAM_TOSERVER;
311 memset(&s, 0x00,
sizeof(s));
314 DeStateSignatureAppend(state, &s, 0, direction);
316 DeStateSignatureAppend(state, &s, 0, direction);
318 DeStateSignatureAppend(state, &s, 0, direction);
320 DeStateSignatureAppend(state, &s, 0, direction);
322 DeStateSignatureAppend(state, &s, 0, direction);
324 DeStateSignatureAppend(state, &s, 0, direction);
326 DeStateSignatureAppend(state, &s, 0, direction);
328 DeStateSignatureAppend(state, &s, 0, direction);
330 DeStateSignatureAppend(state, &s, 0, direction);
332 DeStateSignatureAppend(state, &s, 0, direction);
334 DeStateSignatureAppend(state, &s, 0, direction);
336 DeStateSignatureAppend(state, &s, 0, direction);
338 DeStateSignatureAppend(state, &s, 0, direction);
340 DeStateSignatureAppend(state, &s, 0, direction);
342 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
345 DeStateSignatureAppend(state, &s, 0, direction);
349 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
353 DeStateSignatureAppend(state, &s, 0, direction);
356 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
359 DeStateSignatureAppend(state, &s, 0, direction);
372 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
375 DeStateSignatureAppend(state, &s, 0, direction);
377 DeStateSignatureAppend(state, &s, 0, direction);
379 DeStateSignatureAppend(state, &s, 0, direction);
381 DeStateSignatureAppend(state, &s, 0, direction);
383 DeStateSignatureAppend(state, &s, 0, direction);
385 DeStateSignatureAppend(state, &s, 0, direction);
387 DeStateSignatureAppend(state, &s, 0, direction);
389 DeStateSignatureAppend(state, &s, 0, direction);
391 DeStateSignatureAppend(state, &s, 0, direction);
393 DeStateSignatureAppend(state, &s, 0, direction);
395 DeStateSignatureAppend(state, &s, 0, direction);
397 DeStateSignatureAppend(state, &s, 0, direction);
399 DeStateSignatureAppend(state, &s, 0, direction);
401 DeStateSignatureAppend(state, &s, 0, direction);
403 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
405 DeStateSignatureAppend(state, &s, 0, direction);
408 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
410 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
412 DeStateSignatureAppend(state, &s, 0, direction);
414 DeStateSignatureAppend(state, &s, 0, direction);
428 static int DeStateTest03(
void)
434 memset(&s, 0x00,
sizeof(s));
436 uint8_t direction = STREAM_TOSERVER;
439 DeStateSignatureAppend(state, &s, 0, direction);
453 static int DeStateSigTest01(
void)
460 uint8_t httpbuf1[] =
"POST / HTTP/1.0\r\n";
461 uint8_t httpbuf2[] =
"User-Agent: Mozilla/1.0\r\n";
462 uint8_t httpbuf3[] =
"Cookie: dummy\r\nContent-Length: 10\r\n\r\n";
463 uint8_t httpbuf4[] =
"Http Body!";
464 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
465 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
466 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
467 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
472 memset(&th_v, 0,
sizeof(th_v));
473 memset(&f, 0,
sizeof(f));
474 memset(&ssn, 0,
sizeof(ssn));
481 f.
proto = IPPROTO_TCP;
534 static int DeStateSigTest02(
void)
541 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n";
542 uint8_t httpbuf2[] =
"User-Agent: Mozilla/1.0\r\nContent-Length: 10\r\n";
543 uint8_t httpbuf3[] =
"Cookie: dummy\r\n\r\n";
544 uint8_t httpbuf4[] =
"Http Body!";
545 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
546 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
547 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
548 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
549 uint8_t httpbuf5[] =
"GET /?var=val HTTP/1.1\r\n";
550 uint8_t httpbuf6[] =
"User-Agent: Firefox/1.0\r\n";
551 uint8_t httpbuf7[] =
"Cookie: dummy2\r\nContent-Length: 10\r\n\r\nHttp Body!";
552 uint32_t httplen5 =
sizeof(httpbuf5) - 1;
553 uint32_t httplen6 =
sizeof(httpbuf6) - 1;
554 uint32_t httplen7 =
sizeof(httpbuf7) - 1;
558 memset(&th_v, 0,
sizeof(th_v));
559 memset(&f, 0,
sizeof(f));
560 memset(&ssn, 0,
sizeof(ssn));
566 f.
proto = IPPROTO_TCP;
582 Signature *s =
DetectEngineAppendSig(
de_ctx,
"alert tcp any any -> any any (flow:to_server; content:\"POST\"; http_method; content:\"/\"; http_uri; content:\"Mozilla\"; http_header; content:\"dummy\"; http_cookie; content:\"body\"; nocase; http_client_body; sid:1; rev:1;)");
584 s =
DetectEngineAppendSig(
de_ctx,
"alert tcp any any -> any any (flow:to_server; content:\"GET\"; http_method; content:\"Firefox\"; http_header; content:\"dummy2\"; http_cookie; sid:2; rev:1;)");
648 static int DeStateSigTest03(
void)
650 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
651 "Host: www.server.lan\r\n"
652 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
653 "Content-Length: 215\r\n"
655 "-----------------------------277531038314945\r\n"
656 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
657 "Content-Type: image/jpeg\r\n"
660 "-----------------------------277531038314945--";
661 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
669 memset(&th_v, 0,
sizeof(th_v));
670 memset(&ssn, 0,
sizeof(ssn));
678 Signature *s =
DetectEngineAppendSig(
de_ctx,
"alert http any any -> any any (flow:to_server; content:\"POST\"; http_method; content:\"upload.cgi\"; http_uri; filestore; sid:1; rev:1;)");
684 f =
UTHBuildFlow(AF_INET,
"1.2.3.4",
"1.2.3.5", 1024, 80);
687 f->
proto = IPPROTO_TCP;
701 STREAM_TOSERVER | STREAM_START | STREAM_EOF, httpbuf1, httplen1);
732 static int DeStateSigTest04(
void)
734 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
735 "Host: www.server.lan\r\n"
736 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
737 "Content-Length: 215\r\n"
739 "-----------------------------277531038314945\r\n"
740 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
741 "Content-Type: image/jpeg\r\n"
744 "-----------------------------277531038314945--";
745 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
751 memset(&th_v, 0,
sizeof(th_v));
752 memset(&ssn, 0,
sizeof(ssn));
769 f->
proto = IPPROTO_TCP;
782 STREAM_TOSERVER | STREAM_START | STREAM_EOF, httpbuf1, httplen1);
810 static int DeStateSigTest05(
void)
812 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
813 "Host: www.server.lan\r\n"
814 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
815 "Content-Length: 215\r\n"
817 "-----------------------------277531038314945\r\n"
818 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
819 "Content-Type: image/jpeg\r\n"
822 "-----------------------------277531038314945--";
823 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
830 memset(&th_v, 0,
sizeof(th_v));
831 memset(&ssn, 0,
sizeof(ssn));
838 Signature *s =
DetectEngineAppendSig(
de_ctx,
"alert http any any -> any any (content:\"GET\"; http_method; content:\"upload.cgi\"; http_uri; filename:\"nomatch\"; sid:1; rev:1;)");
847 f->
proto = IPPROTO_TCP;
860 STREAM_TOSERVER | STREAM_START | STREAM_EOF, httpbuf1, httplen1);
892 static int DeStateSigTest06(
void)
894 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
895 "Host: www.server.lan\r\n"
896 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
897 "Content-Length: 215\r\n"
899 "-----------------------------277531038314945\r\n"
900 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
901 "Content-Type: image/jpeg\r\n"
904 "-----------------------------277531038314945--";
905 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
912 memset(&th_v, 0,
sizeof(th_v));
913 memset(&ssn, 0,
sizeof(ssn));
920 Signature *s =
DetectEngineAppendSig(
de_ctx,
"alert http any any -> any any (content:\"POST\"; http_method; content:\"upload.cgi\"; http_uri; filename:\"nomatch\"; filestore; sid:1; rev:1;)");
930 f->
proto = IPPROTO_TCP;
943 STREAM_TOSERVER | STREAM_START | STREAM_EOF, httpbuf1, httplen1);
973 static int DeStateSigTest07(
void)
975 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
976 "Host: www.server.lan\r\n"
977 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
978 "Content-Length: 215\r\n"
980 "-----------------------------277531038314945\r\n"
981 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
982 "Content-Type: image/jpeg\r\n"
985 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
986 uint8_t httpbuf2[] =
"filecontent\r\n"
987 "-----------------------------277531038314945--";
988 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
995 memset(&th_v, 0,
sizeof(th_v));
996 memset(&ssn, 0,
sizeof(ssn));
1012 f->
proto = IPPROTO_TCP;
1061 static int DeStateSigTest08(
void)
1063 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
1064 "Host: www.server.lan\r\n"
1065 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
1066 "Content-Length: 440\r\n"
1068 "-----------------------------277531038314945\r\n"
1069 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"AAAApicture1.jpg\"\r\n"
1070 "Content-Type: image/jpeg\r\n"
1073 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1074 uint8_t httpbuf2[] =
"file";
1075 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1076 uint8_t httpbuf3[] =
"content\r\n"
1077 "-----------------------------277531038314945\r\n";
1078 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
1080 uint8_t httpbuf4[] =
"Content-Disposition: form-data; name=\"uploadfile_1\"; filename=\"BBBBpicture2.jpg\"\r\n"
1081 "Content-Type: image/jpeg\r\n"
1084 "-----------------------------277531038314945--";
1085 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
1093 memset(&th_v, 0,
sizeof(th_v));
1094 memset(&ssn, 0,
sizeof(ssn));
1101 Signature *s =
DetectEngineAppendSig(
de_ctx,
"alert http any any -> any any (content:\"POST\"; http_method; content:\"upload.cgi\"; http_uri; filename:\"BBBBpicture\"; filestore; sid:1; rev:1;)");
1110 f->
proto = IPPROTO_TCP;
1166 tx_ud = htp_tx_get_user_data(tx);
1189 static int DeStateSigTest09(
void)
1191 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
1192 "Host: www.server.lan\r\n"
1193 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
1194 "Content-Length: 440\r\n"
1196 "-----------------------------277531038314945\r\n"
1197 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
1198 "Content-Type: image/jpeg\r\n"
1201 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1202 uint8_t httpbuf2[] =
"file";
1203 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1204 uint8_t httpbuf3[] =
"content\r\n"
1205 "-----------------------------277531038314945\r\n";
1206 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
1208 uint8_t httpbuf4[] =
"Content-Disposition: form-data; name=\"uploadfile_1\"; filename=\"somepicture2.jpg\"\r\n"
1209 "Content-Type: image/jpeg\r\n"
1212 "-----------------------------277531038314945--";
1213 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
1221 memset(&th_v, 0,
sizeof(th_v));
1222 memset(&ssn, 0,
sizeof(ssn));
1229 Signature *s =
DetectEngineAppendSig(
de_ctx,
"alert http any any -> any any (content:\"POST\"; http_method; content:\"upload.cgi\"; http_uri; filename:\"somepicture\"; filestore; sid:1; rev:1;)");
1238 f->
proto = IPPROTO_TCP;
1294 tx_ud = htp_tx_get_user_data(tx);
1315 static int DeStateSigTest10(
void)
1317 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
1318 "Host: www.server.lan\r\n"
1319 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
1320 "Content-Length: 440\r\n"
1322 "-----------------------------277531038314945\r\n"
1323 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
1324 "Content-Type: image/jpeg\r\n"
1327 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1328 uint8_t httpbuf2[] =
"file";
1329 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1330 uint8_t httpbuf3[] =
"content\r\n"
1331 "-----------------------------277531038314945\r\n";
1332 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
1334 uint8_t httpbuf4[] =
"Content-Disposition: form-data; name=\"uploadfile_1\"; filename=\"somepicture2.jpg\"\r\n"
1335 "Content-Type: image/jpeg\r\n"
1338 "-----------------------------277531038314945--";
1339 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
1347 memset(&th_v, 0,
sizeof(th_v));
1348 memset(&ssn, 0,
sizeof(ssn));
1364 f->
proto = IPPROTO_TCP;
1420 tx_ud = htp_tx_get_user_data(tx);