65 static inline int StateIsValid(uint16_t alproto,
void *alstate)
67 if (alstate != NULL) {
70 if (htp_state->
conn != NULL) {
89 #ifdef DEBUG_VALIDATION
97 for (; tx_store != NULL; tx_store = tx_store->
next) {
101 store_cnt++, state_cnt++)
104 if (item->
sid == num) {
105 SCLogDebug(
"sid %u already in state: %p %p %p %u %u, direction %s",
106 num, state, dir_state, tx_store, state_cnt,
107 store_cnt, direction & STREAM_TOSERVER ?
"toserver" :
"toclient");
117 const Signature *s, uint32_t inspect_flags, uint8_t direction)
122 &state->
dir_state[(direction & STREAM_TOSERVER) ? 0 : 1];
124 #ifdef DEBUG_VALIDATION
125 BUG_ON(DeStateSearchState(state, direction, s->
iid));
129 store = DeStateStoreAlloc();
130 dir_state->
head = store;
131 dir_state->
cur = store;
132 dir_state->
tail = store;
133 }
else if (dir_state->
cur) {
134 store = dir_state->
cur;
136 store = DeStateStoreAlloc();
139 dir_state->
tail = store;
140 dir_state->
cur = store;
173 for (i = 0; i < 2; i++) {
175 while (store != NULL) {
176 store_next = store->
next;
184 static void StoreFileNoMatchCnt(
DetectEngineState *de_state, uint16_t file_no_match, uint8_t direction)
186 de_state->
dir_state[(direction & STREAM_TOSERVER) ? 0 : 1].filestore_cnt += file_no_match;
199 const uint8_t flow_flags,
void *tx,
const uint64_t tx_id,
const uint16_t file_no_match)
201 SCLogDebug(
"tx %"PRIu64
", file_no_match %u", tx_id, file_no_match);
202 StoreFileNoMatchCnt(destate, file_no_match, flow_flags);
203 if (StoreFilestoreSigsCantMatch(sgh, destate, flow_flags)) {
206 f, flow_flags & (STREAM_TOCLIENT | STREAM_TOSERVER), tx, tx_id);
214 Flow *
f,
void *tx, uint64_t tx_id,
216 uint32_t inspect_flags, uint8_t flow_flags,
217 const uint16_t file_no_match)
224 SCLogDebug(
"destate created for %"PRIu64, tx_id);
226 DeStateSignatureAppend(tx_data->
de_state, s, inspect_flags, flow_flags);
229 DeStateSignatureAppend(tx_data->
de_state, s, inspect_flags,
230 flow_flags ^ (STREAM_TOSERVER | STREAM_TOCLIENT));
232 StoreStateTxHandleFiles(sgh,
f, tx_data->
de_state, flow_flags, tx, tx_id, file_no_match);
260 void *alstate = FlowGetAppState(
f);
261 if (!StateIsValid(
f->
alproto, alstate)) {
268 uint64_t inspect_tx_id =
MIN(inspect_ts, inspect_tc);
272 for ( ; inspect_tx_id < total_txs; inspect_tx_id++) {
274 if (inspect_tx != NULL) {
286 static int DeStateTest01(
void)
288 SCLogDebug(
"sizeof(DetectEngineState)\t\t%"PRIuMAX,
290 SCLogDebug(
"sizeof(DeStateStore)\t\t\t%"PRIuMAX,
292 SCLogDebug(
"sizeof(DeStateStoreItem)\t\t%"PRIuMAX
"",
298 static int DeStateTest02(
void)
300 uint8_t direction = STREAM_TOSERVER;
306 memset(&s, 0x00,
sizeof(s));
309 DeStateSignatureAppend(state, &s, 0, direction);
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 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
340 DeStateSignatureAppend(state, &s, 0, direction);
344 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
348 DeStateSignatureAppend(state, &s, 0, direction);
351 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
354 DeStateSignatureAppend(state, &s, 0, direction);
367 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
370 DeStateSignatureAppend(state, &s, 0, direction);
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 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
400 DeStateSignatureAppend(state, &s, 0, direction);
403 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
405 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
407 DeStateSignatureAppend(state, &s, 0, direction);
409 DeStateSignatureAppend(state, &s, 0, direction);
423 static int DeStateTest03(
void)
429 memset(&s, 0x00,
sizeof(s));
431 uint8_t direction = STREAM_TOSERVER;
434 DeStateSignatureAppend(state, &s, 0, direction);
448 static int DeStateSigTest01(
void)
455 uint8_t httpbuf1[] =
"POST / HTTP/1.0\r\n";
456 uint8_t httpbuf2[] =
"User-Agent: Mozilla/1.0\r\n";
457 uint8_t httpbuf3[] =
"Cookie: dummy\r\nContent-Length: 10\r\n\r\n";
458 uint8_t httpbuf4[] =
"Http Body!";
459 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
460 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
461 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
462 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
469 memset(&
f, 0,
sizeof(
f));
470 memset(&
ssn, 0,
sizeof(
ssn));
531 static int DeStateSigTest02(
void)
538 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n";
539 uint8_t httpbuf2[] =
"User-Agent: Mozilla/1.0\r\nContent-Length: 10\r\n";
540 uint8_t httpbuf3[] =
"Cookie: dummy\r\n\r\n";
541 uint8_t httpbuf4[] =
"Http Body!";
542 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
543 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
544 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
545 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
546 uint8_t httpbuf5[] =
"GET /?var=val HTTP/1.1\r\n";
547 uint8_t httpbuf6[] =
"User-Agent: Firefox/1.0\r\n";
548 uint8_t httpbuf7[] =
"Cookie: dummy2\r\nContent-Length: 10\r\n\r\nHttp Body!";
549 uint32_t httplen5 =
sizeof(httpbuf5) - 1;
550 uint32_t httplen6 =
sizeof(httpbuf6) - 1;
551 uint32_t httplen7 =
sizeof(httpbuf7) - 1;
557 memset(&
f, 0,
sizeof(
f));
558 memset(&
ssn, 0,
sizeof(
ssn));
580 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;)");
582 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;)");
647 static int DeStateSigTest03(
void)
649 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
650 "Host: www.server.lan\r\n"
651 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
652 "Content-Length: 215\r\n"
654 "-----------------------------277531038314945\r\n"
655 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
656 "Content-Type: image/jpeg\r\n"
659 "-----------------------------277531038314945--";
660 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
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;)");
701 STREAM_TOSERVER | STREAM_START | STREAM_EOF, httpbuf1, httplen1);
734 static int DeStateSigTest04(
void)
736 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
737 "Host: www.server.lan\r\n"
738 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
739 "Content-Length: 215\r\n"
741 "-----------------------------277531038314945\r\n"
742 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
743 "Content-Type: image/jpeg\r\n"
746 "-----------------------------277531038314945--";
747 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
755 memset(&
ssn, 0,
sizeof(
ssn));
785 STREAM_TOSERVER | STREAM_START | STREAM_EOF, httpbuf1, httplen1);
815 static int DeStateSigTest05(
void)
817 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
818 "Host: www.server.lan\r\n"
819 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
820 "Content-Length: 215\r\n"
822 "-----------------------------277531038314945\r\n"
823 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
824 "Content-Type: image/jpeg\r\n"
827 "-----------------------------277531038314945--";
828 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
837 memset(&
ssn, 0,
sizeof(
ssn));
844 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;)");
866 STREAM_TOSERVER | STREAM_START | STREAM_EOF, httpbuf1, httplen1);
900 static int DeStateSigTest06(
void)
902 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
903 "Host: www.server.lan\r\n"
904 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
905 "Content-Length: 215\r\n"
907 "-----------------------------277531038314945\r\n"
908 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
909 "Content-Type: image/jpeg\r\n"
912 "-----------------------------277531038314945--";
913 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
922 memset(&
ssn, 0,
sizeof(
ssn));
929 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;)");
952 STREAM_TOSERVER | STREAM_START | STREAM_EOF, httpbuf1, httplen1);
984 static int DeStateSigTest07(
void)
986 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
987 "Host: www.server.lan\r\n"
988 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
989 "Content-Length: 215\r\n"
991 "-----------------------------277531038314945\r\n"
992 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
993 "Content-Type: image/jpeg\r\n"
996 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
997 uint8_t httpbuf2[] =
"filecontent\r\n"
998 "-----------------------------277531038314945--";
999 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1008 memset(&
ssn, 0,
sizeof(
ssn));
1075 static int DeStateSigTest08(
void)
1077 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
1078 "Host: www.server.lan\r\n"
1079 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
1080 "Content-Length: 440\r\n"
1082 "-----------------------------277531038314945\r\n"
1083 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"AAAApicture1.jpg\"\r\n"
1084 "Content-Type: image/jpeg\r\n"
1087 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1088 uint8_t httpbuf2[] =
"file";
1089 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1090 uint8_t httpbuf3[] =
"content\r\n"
1091 "-----------------------------277531038314945\r\n";
1092 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
1094 uint8_t httpbuf4[] =
"Content-Disposition: form-data; name=\"uploadfile_1\"; filename=\"BBBBpicture2.jpg\"\r\n"
1095 "Content-Type: image/jpeg\r\n"
1098 "-----------------------------277531038314945--";
1099 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
1109 memset(&
ssn, 0,
sizeof(
ssn));
1116 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;)");
1181 tx_ud = htp_tx_get_user_data(tx);
1206 static int DeStateSigTest09(
void)
1208 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
1209 "Host: www.server.lan\r\n"
1210 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
1211 "Content-Length: 440\r\n"
1213 "-----------------------------277531038314945\r\n"
1214 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
1215 "Content-Type: image/jpeg\r\n"
1218 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1219 uint8_t httpbuf2[] =
"file";
1220 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1221 uint8_t httpbuf3[] =
"content\r\n"
1222 "-----------------------------277531038314945\r\n";
1223 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
1225 uint8_t httpbuf4[] =
"Content-Disposition: form-data; name=\"uploadfile_1\"; filename=\"somepicture2.jpg\"\r\n"
1226 "Content-Type: image/jpeg\r\n"
1229 "-----------------------------277531038314945--";
1230 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
1240 memset(&
ssn, 0,
sizeof(
ssn));
1247 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;)");
1312 tx_ud = htp_tx_get_user_data(tx);
1335 static int DeStateSigTest10(
void)
1337 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
1338 "Host: www.server.lan\r\n"
1339 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
1340 "Content-Length: 440\r\n"
1342 "-----------------------------277531038314945\r\n"
1343 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
1344 "Content-Type: image/jpeg\r\n"
1347 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1348 uint8_t httpbuf2[] =
"file";
1349 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1350 uint8_t httpbuf3[] =
"content\r\n"
1351 "-----------------------------277531038314945\r\n";
1352 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
1354 uint8_t httpbuf4[] =
"Content-Disposition: form-data; name=\"uploadfile_1\"; filename=\"somepicture2.jpg\"\r\n"
1355 "Content-Type: image/jpeg\r\n"
1358 "-----------------------------277531038314945--";
1359 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
1369 memset(&
ssn, 0,
sizeof(
ssn));
1441 tx_ud = htp_tx_get_user_data(tx);