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->
iid));
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)
222 if (tx_data->de_state == NULL) {
224 if (tx_data->de_state == NULL)
226 SCLogDebug(
"destate created for %"PRIu64, tx_id);
228 DeStateSignatureAppend(tx_data->de_state, s, inspect_flags, flow_flags);
231 DeStateSignatureAppend(tx_data->de_state, s, inspect_flags,
232 flow_flags ^ (STREAM_TOSERVER | STREAM_TOCLIENT));
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) {
278 ResetTxState(txd->de_state);
288 static int DeStateTest01(
void)
290 SCLogDebug(
"sizeof(DetectEngineState)\t\t%"PRIuMAX,
292 SCLogDebug(
"sizeof(DeStateStore)\t\t\t%"PRIuMAX,
294 SCLogDebug(
"sizeof(DeStateStoreItem)\t\t%"PRIuMAX
"",
300 static int DeStateTest02(
void)
302 uint8_t direction = STREAM_TOSERVER;
308 memset(&s, 0x00,
sizeof(s));
311 DeStateSignatureAppend(state, &s, 0, direction);
313 DeStateSignatureAppend(state, &s, 0, direction);
315 DeStateSignatureAppend(state, &s, 0, direction);
317 DeStateSignatureAppend(state, &s, 0, direction);
319 DeStateSignatureAppend(state, &s, 0, direction);
321 DeStateSignatureAppend(state, &s, 0, direction);
323 DeStateSignatureAppend(state, &s, 0, direction);
325 DeStateSignatureAppend(state, &s, 0, direction);
327 DeStateSignatureAppend(state, &s, 0, direction);
329 DeStateSignatureAppend(state, &s, 0, direction);
331 DeStateSignatureAppend(state, &s, 0, direction);
333 DeStateSignatureAppend(state, &s, 0, direction);
335 DeStateSignatureAppend(state, &s, 0, direction);
337 DeStateSignatureAppend(state, &s, 0, direction);
339 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
342 DeStateSignatureAppend(state, &s, 0, direction);
346 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
350 DeStateSignatureAppend(state, &s, 0, direction);
353 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
356 DeStateSignatureAppend(state, &s, 0, direction);
369 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
372 DeStateSignatureAppend(state, &s, 0, direction);
374 DeStateSignatureAppend(state, &s, 0, direction);
376 DeStateSignatureAppend(state, &s, 0, direction);
378 DeStateSignatureAppend(state, &s, 0, direction);
380 DeStateSignatureAppend(state, &s, 0, direction);
382 DeStateSignatureAppend(state, &s, 0, direction);
384 DeStateSignatureAppend(state, &s, 0, direction);
386 DeStateSignatureAppend(state, &s, 0, direction);
388 DeStateSignatureAppend(state, &s, 0, direction);
390 DeStateSignatureAppend(state, &s, 0, direction);
392 DeStateSignatureAppend(state, &s, 0, direction);
394 DeStateSignatureAppend(state, &s, 0, direction);
396 DeStateSignatureAppend(state, &s, 0, direction);
398 DeStateSignatureAppend(state, &s, 0, direction);
400 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
402 DeStateSignatureAppend(state, &s, 0, direction);
405 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
407 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
409 DeStateSignatureAppend(state, &s, 0, direction);
411 DeStateSignatureAppend(state, &s, 0, direction);
425 static int DeStateTest03(
void)
431 memset(&s, 0x00,
sizeof(s));
433 uint8_t direction = STREAM_TOSERVER;
436 DeStateSignatureAppend(state, &s, 0, direction);
450 static int DeStateSigTest01(
void)
457 uint8_t httpbuf1[] =
"POST / HTTP/1.0\r\n";
458 uint8_t httpbuf2[] =
"User-Agent: Mozilla/1.0\r\n";
459 uint8_t httpbuf3[] =
"Cookie: dummy\r\nContent-Length: 10\r\n\r\n";
460 uint8_t httpbuf4[] =
"Http Body!";
461 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
462 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
463 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
464 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
469 memset(&th_v, 0,
sizeof(th_v));
471 memset(&f, 0,
sizeof(f));
472 memset(&ssn, 0,
sizeof(ssn));
479 f.
proto = IPPROTO_TCP;
533 static int DeStateSigTest02(
void)
540 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n";
541 uint8_t httpbuf2[] =
"User-Agent: Mozilla/1.0\r\nContent-Length: 10\r\n";
542 uint8_t httpbuf3[] =
"Cookie: dummy\r\n\r\n";
543 uint8_t httpbuf4[] =
"Http Body!";
544 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
545 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
546 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
547 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
548 uint8_t httpbuf5[] =
"GET /?var=val HTTP/1.1\r\n";
549 uint8_t httpbuf6[] =
"User-Agent: Firefox/1.0\r\n";
550 uint8_t httpbuf7[] =
"Cookie: dummy2\r\nContent-Length: 10\r\n\r\nHttp Body!";
551 uint32_t httplen5 =
sizeof(httpbuf5) - 1;
552 uint32_t httplen6 =
sizeof(httpbuf6) - 1;
553 uint32_t httplen7 =
sizeof(httpbuf7) - 1;
557 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;)");
649 static int DeStateSigTest03(
void)
651 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
652 "Host: www.server.lan\r\n"
653 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
654 "Content-Length: 215\r\n"
656 "-----------------------------277531038314945\r\n"
657 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
658 "Content-Type: image/jpeg\r\n"
661 "-----------------------------277531038314945--";
662 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
670 memset(&th_v, 0,
sizeof(th_v));
672 memset(&ssn, 0,
sizeof(ssn));
680 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;)");
686 f =
UTHBuildFlow(AF_INET,
"1.2.3.4",
"1.2.3.5", 1024, 80);
689 f->
proto = IPPROTO_TCP;
703 STREAM_TOSERVER | STREAM_START | STREAM_EOF, httpbuf1, httplen1);
736 static int DeStateSigTest04(
void)
738 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
739 "Host: www.server.lan\r\n"
740 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
741 "Content-Length: 215\r\n"
743 "-----------------------------277531038314945\r\n"
744 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
745 "Content-Type: image/jpeg\r\n"
748 "-----------------------------277531038314945--";
749 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
755 memset(&th_v, 0,
sizeof(th_v));
757 memset(&ssn, 0,
sizeof(ssn));
774 f->
proto = IPPROTO_TCP;
787 STREAM_TOSERVER | STREAM_START | STREAM_EOF, httpbuf1, httplen1);
817 static int DeStateSigTest05(
void)
819 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
820 "Host: www.server.lan\r\n"
821 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
822 "Content-Length: 215\r\n"
824 "-----------------------------277531038314945\r\n"
825 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
826 "Content-Type: image/jpeg\r\n"
829 "-----------------------------277531038314945--";
830 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
837 memset(&th_v, 0,
sizeof(th_v));
839 memset(&ssn, 0,
sizeof(ssn));
846 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;)");
855 f->
proto = IPPROTO_TCP;
868 STREAM_TOSERVER | STREAM_START | STREAM_EOF, httpbuf1, httplen1);
902 static int DeStateSigTest06(
void)
904 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
905 "Host: www.server.lan\r\n"
906 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
907 "Content-Length: 215\r\n"
909 "-----------------------------277531038314945\r\n"
910 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
911 "Content-Type: image/jpeg\r\n"
914 "-----------------------------277531038314945--";
915 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
922 memset(&th_v, 0,
sizeof(th_v));
924 memset(&ssn, 0,
sizeof(ssn));
931 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;)");
941 f->
proto = IPPROTO_TCP;
954 STREAM_TOSERVER | STREAM_START | STREAM_EOF, httpbuf1, httplen1);
986 static int DeStateSigTest07(
void)
988 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
989 "Host: www.server.lan\r\n"
990 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
991 "Content-Length: 215\r\n"
993 "-----------------------------277531038314945\r\n"
994 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
995 "Content-Type: image/jpeg\r\n"
998 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
999 uint8_t httpbuf2[] =
"filecontent\r\n"
1000 "-----------------------------277531038314945--";
1001 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1008 memset(&th_v, 0,
sizeof(th_v));
1010 memset(&ssn, 0,
sizeof(ssn));
1026 f->
proto = IPPROTO_TCP;
1077 static int DeStateSigTest08(
void)
1079 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
1080 "Host: www.server.lan\r\n"
1081 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
1082 "Content-Length: 440\r\n"
1084 "-----------------------------277531038314945\r\n"
1085 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"AAAApicture1.jpg\"\r\n"
1086 "Content-Type: image/jpeg\r\n"
1089 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1090 uint8_t httpbuf2[] =
"file";
1091 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1092 uint8_t httpbuf3[] =
"content\r\n"
1093 "-----------------------------277531038314945\r\n";
1094 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
1096 uint8_t httpbuf4[] =
"Content-Disposition: form-data; name=\"uploadfile_1\"; filename=\"BBBBpicture2.jpg\"\r\n"
1097 "Content-Type: image/jpeg\r\n"
1100 "-----------------------------277531038314945--";
1101 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
1109 memset(&th_v, 0,
sizeof(th_v));
1111 memset(&ssn, 0,
sizeof(ssn));
1118 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;)");
1127 f->
proto = IPPROTO_TCP;
1183 tx_ud = htp_tx_get_user_data(tx);
1208 static int DeStateSigTest09(
void)
1210 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
1211 "Host: www.server.lan\r\n"
1212 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
1213 "Content-Length: 440\r\n"
1215 "-----------------------------277531038314945\r\n"
1216 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
1217 "Content-Type: image/jpeg\r\n"
1220 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1221 uint8_t httpbuf2[] =
"file";
1222 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1223 uint8_t httpbuf3[] =
"content\r\n"
1224 "-----------------------------277531038314945\r\n";
1225 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
1227 uint8_t httpbuf4[] =
"Content-Disposition: form-data; name=\"uploadfile_1\"; filename=\"somepicture2.jpg\"\r\n"
1228 "Content-Type: image/jpeg\r\n"
1231 "-----------------------------277531038314945--";
1232 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
1240 memset(&th_v, 0,
sizeof(th_v));
1242 memset(&ssn, 0,
sizeof(ssn));
1249 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;)");
1258 f->
proto = IPPROTO_TCP;
1314 tx_ud = htp_tx_get_user_data(tx);
1337 static int DeStateSigTest10(
void)
1339 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
1340 "Host: www.server.lan\r\n"
1341 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
1342 "Content-Length: 440\r\n"
1344 "-----------------------------277531038314945\r\n"
1345 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
1346 "Content-Type: image/jpeg\r\n"
1349 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1350 uint8_t httpbuf2[] =
"file";
1351 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1352 uint8_t httpbuf3[] =
"content\r\n"
1353 "-----------------------------277531038314945\r\n";
1354 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
1356 uint8_t httpbuf4[] =
"Content-Disposition: form-data; name=\"uploadfile_1\"; filename=\"somepicture2.jpg\"\r\n"
1357 "Content-Type: image/jpeg\r\n"
1360 "-----------------------------277531038314945--";
1361 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
1369 memset(&th_v, 0,
sizeof(th_v));
1371 memset(&ssn, 0,
sizeof(ssn));
1387 f->
proto = IPPROTO_TCP;
1443 tx_ud = htp_tx_get_user_data(tx);