68 #define CASE_CODE(E) case E: return #E
70 static inline int StateIsValid(uint16_t alproto,
void *alstate)
72 if (alstate != NULL) {
75 if (htp_state->
conn != NULL) {
95 #ifdef DEBUG_VALIDATION
103 for (; tx_store != NULL; tx_store = tx_store->
next) {
107 store_cnt++, state_cnt++)
110 if (item->
sid == num) {
111 SCLogDebug(
"sid %u already in state: %p %p %p %u %u, direction %s",
112 num, state, dir_state, tx_store, state_cnt,
113 store_cnt, direction & STREAM_TOSERVER ?
"toserver" :
"toclient");
123 const Signature *s, uint32_t inspect_flags, uint8_t direction)
128 &state->
dir_state[(direction & STREAM_TOSERVER) ? 0 : 1];
130 #ifdef DEBUG_VALIDATION
131 BUG_ON(DeStateSearchState(state, direction, s->
num));
135 store = DeStateStoreAlloc();
136 dir_state->
head = store;
137 dir_state->
cur = store;
138 dir_state->
tail = store;
139 }
else if (dir_state->
cur) {
140 store = dir_state->
cur;
142 store = DeStateStoreAlloc();
145 dir_state->
tail = store;
146 dir_state->
cur = store;
180 for (i = 0; i < 2; i++) {
182 while (store != NULL) {
183 store_next = store->
next;
193 static void StoreFileNoMatchCnt(
DetectEngineState *de_state, uint16_t file_no_match, uint8_t direction)
195 de_state->
dir_state[(direction & STREAM_TOSERVER) ? 0 : 1].filestore_cnt += file_no_match;
210 const uint8_t flow_flags,
void *tx,
const uint64_t tx_id,
const uint16_t file_no_match)
212 SCLogDebug(
"tx %"PRIu64
", file_no_match %u", tx_id, file_no_match);
213 StoreFileNoMatchCnt(destate, file_no_match, flow_flags);
214 if (StoreFilestoreSigsCantMatch(sgh, destate, flow_flags)) {
217 f, flow_flags & (STREAM_TOCLIENT | STREAM_TOSERVER), tx, tx_id);
225 Flow *f,
void *tx, uint64_t tx_id,
227 uint32_t inspect_flags, uint8_t flow_flags,
228 const uint16_t file_no_match)
232 if (tx_data == NULL) {
236 if (tx_data->de_state == NULL) {
238 if (tx_data->de_state == NULL)
240 SCLogDebug(
"destate created for %"PRIu64, tx_id);
242 DeStateSignatureAppend(tx_data->de_state, s, inspect_flags, flow_flags);
243 StoreStateTxHandleFiles(sgh, f, tx_data->de_state, flow_flags, tx, tx_id, file_no_match);
271 void *alstate = FlowGetAppState(f);
272 if (!StateIsValid(f->
alproto, alstate)) {
279 uint64_t inspect_tx_id =
MIN(inspect_ts, inspect_tc);
283 for ( ; inspect_tx_id < total_txs; inspect_tx_id++) {
285 if (inspect_tx != NULL) {
289 ResetTxState(txd->de_state);
300 static int DeStateTest01(
void)
302 SCLogDebug(
"sizeof(DetectEngineState)\t\t%"PRIuMAX,
304 SCLogDebug(
"sizeof(DeStateStore)\t\t\t%"PRIuMAX,
306 SCLogDebug(
"sizeof(DeStateStoreItem)\t\t%"PRIuMAX
"",
312 static int DeStateTest02(
void)
314 uint8_t direction = STREAM_TOSERVER;
320 memset(&s, 0x00,
sizeof(s));
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 DeStateSignatureAppend(state, &s, 0, direction);
341 DeStateSignatureAppend(state, &s, 0, direction);
343 DeStateSignatureAppend(state, &s, 0, direction);
345 DeStateSignatureAppend(state, &s, 0, direction);
347 DeStateSignatureAppend(state, &s, 0, direction);
349 DeStateSignatureAppend(state, &s, 0, direction);
351 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
354 DeStateSignatureAppend(state, &s, 0, direction);
358 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
362 DeStateSignatureAppend(state, &s, 0, direction);
365 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
368 DeStateSignatureAppend(state, &s, 0, direction);
381 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
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 DeStateSignatureAppend(state, &s, 0, direction);
402 DeStateSignatureAppend(state, &s, 0, direction);
404 DeStateSignatureAppend(state, &s, 0, direction);
406 DeStateSignatureAppend(state, &s, 0, direction);
408 DeStateSignatureAppend(state, &s, 0, direction);
410 DeStateSignatureAppend(state, &s, 0, direction);
412 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
414 DeStateSignatureAppend(state, &s, 0, direction);
417 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
419 state->
dir_state[direction & STREAM_TOSERVER ? 0 : 1].
cur);
421 DeStateSignatureAppend(state, &s, 0, direction);
423 DeStateSignatureAppend(state, &s, 0, direction);
437 static int DeStateTest03(
void)
443 memset(&s, 0x00,
sizeof(s));
445 uint8_t direction = STREAM_TOSERVER;
448 DeStateSignatureAppend(state, &s, 0, direction);
462 static int DeStateSigTest01(
void)
469 uint8_t httpbuf1[] =
"POST / HTTP/1.0\r\n";
470 uint8_t httpbuf2[] =
"User-Agent: Mozilla/1.0\r\n";
471 uint8_t httpbuf3[] =
"Cookie: dummy\r\nContent-Length: 10\r\n\r\n";
472 uint8_t httpbuf4[] =
"Http Body!";
473 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
474 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
475 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
476 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
481 memset(&th_v, 0,
sizeof(th_v));
482 memset(&f, 0,
sizeof(f));
483 memset(&ssn, 0,
sizeof(ssn));
490 f.
proto = IPPROTO_TCP;
543 static int DeStateSigTest02(
void)
550 uint8_t httpbuf1[] =
"POST / HTTP/1.1\r\n";
551 uint8_t httpbuf2[] =
"User-Agent: Mozilla/1.0\r\nContent-Length: 10\r\n";
552 uint8_t httpbuf3[] =
"Cookie: dummy\r\n\r\n";
553 uint8_t httpbuf4[] =
"Http Body!";
554 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
555 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
556 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
557 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
558 uint8_t httpbuf5[] =
"GET /?var=val HTTP/1.1\r\n";
559 uint8_t httpbuf6[] =
"User-Agent: Firefox/1.0\r\n";
560 uint8_t httpbuf7[] =
"Cookie: dummy2\r\nContent-Length: 10\r\n\r\nHttp Body!";
561 uint32_t httplen5 =
sizeof(httpbuf5) - 1;
562 uint32_t httplen6 =
sizeof(httpbuf6) - 1;
563 uint32_t httplen7 =
sizeof(httpbuf7) - 1;
567 memset(&th_v, 0,
sizeof(th_v));
568 memset(&f, 0,
sizeof(f));
569 memset(&ssn, 0,
sizeof(ssn));
575 f.
proto = IPPROTO_TCP;
591 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;)");
593 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;)");
657 static int DeStateSigTest03(
void)
659 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
660 "Host: www.server.lan\r\n"
661 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
662 "Content-Length: 215\r\n"
664 "-----------------------------277531038314945\r\n"
665 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
666 "Content-Type: image/jpeg\r\n"
669 "-----------------------------277531038314945--";
670 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
678 memset(&th_v, 0,
sizeof(th_v));
679 memset(&ssn, 0,
sizeof(ssn));
687 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;)");
693 f =
UTHBuildFlow(AF_INET,
"1.2.3.4",
"1.2.3.5", 1024, 80);
696 f->
proto = IPPROTO_TCP;
710 STREAM_TOSERVER | STREAM_START | STREAM_EOF, httpbuf1, httplen1);
741 static int DeStateSigTest04(
void)
743 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
744 "Host: www.server.lan\r\n"
745 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
746 "Content-Length: 215\r\n"
748 "-----------------------------277531038314945\r\n"
749 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
750 "Content-Type: image/jpeg\r\n"
753 "-----------------------------277531038314945--";
754 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
760 memset(&th_v, 0,
sizeof(th_v));
761 memset(&ssn, 0,
sizeof(ssn));
778 f->
proto = IPPROTO_TCP;
791 STREAM_TOSERVER | STREAM_START | STREAM_EOF, httpbuf1, httplen1);
819 static int DeStateSigTest05(
void)
821 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
822 "Host: www.server.lan\r\n"
823 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
824 "Content-Length: 215\r\n"
826 "-----------------------------277531038314945\r\n"
827 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
828 "Content-Type: image/jpeg\r\n"
831 "-----------------------------277531038314945--";
832 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
839 memset(&th_v, 0,
sizeof(th_v));
840 memset(&ssn, 0,
sizeof(ssn));
847 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;)");
856 f->
proto = IPPROTO_TCP;
869 STREAM_TOSERVER | STREAM_START | STREAM_EOF, httpbuf1, httplen1);
901 static int DeStateSigTest06(
void)
903 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
904 "Host: www.server.lan\r\n"
905 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
906 "Content-Length: 215\r\n"
908 "-----------------------------277531038314945\r\n"
909 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
910 "Content-Type: image/jpeg\r\n"
913 "-----------------------------277531038314945--";
914 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
921 memset(&th_v, 0,
sizeof(th_v));
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;)");
939 f->
proto = IPPROTO_TCP;
952 STREAM_TOSERVER | STREAM_START | STREAM_EOF, httpbuf1, httplen1);
982 static int DeStateSigTest07(
void)
984 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
985 "Host: www.server.lan\r\n"
986 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
987 "Content-Length: 215\r\n"
989 "-----------------------------277531038314945\r\n"
990 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
991 "Content-Type: image/jpeg\r\n"
994 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
995 uint8_t httpbuf2[] =
"filecontent\r\n"
996 "-----------------------------277531038314945--";
997 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1004 memset(&th_v, 0,
sizeof(th_v));
1005 memset(&ssn, 0,
sizeof(ssn));
1021 f->
proto = IPPROTO_TCP;
1070 static int DeStateSigTest08(
void)
1072 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
1073 "Host: www.server.lan\r\n"
1074 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
1075 "Content-Length: 440\r\n"
1077 "-----------------------------277531038314945\r\n"
1078 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"AAAApicture1.jpg\"\r\n"
1079 "Content-Type: image/jpeg\r\n"
1082 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1083 uint8_t httpbuf2[] =
"file";
1084 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1085 uint8_t httpbuf3[] =
"content\r\n"
1086 "-----------------------------277531038314945\r\n";
1087 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
1089 uint8_t httpbuf4[] =
"Content-Disposition: form-data; name=\"uploadfile_1\"; filename=\"BBBBpicture2.jpg\"\r\n"
1090 "Content-Type: image/jpeg\r\n"
1093 "-----------------------------277531038314945--";
1094 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
1102 memset(&th_v, 0,
sizeof(th_v));
1103 memset(&ssn, 0,
sizeof(ssn));
1110 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;)");
1119 f->
proto = IPPROTO_TCP;
1175 tx_ud = htp_tx_get_user_data(tx);
1198 static int DeStateSigTest09(
void)
1200 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
1201 "Host: www.server.lan\r\n"
1202 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
1203 "Content-Length: 440\r\n"
1205 "-----------------------------277531038314945\r\n"
1206 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
1207 "Content-Type: image/jpeg\r\n"
1210 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1211 uint8_t httpbuf2[] =
"file";
1212 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1213 uint8_t httpbuf3[] =
"content\r\n"
1214 "-----------------------------277531038314945\r\n";
1215 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
1217 uint8_t httpbuf4[] =
"Content-Disposition: form-data; name=\"uploadfile_1\"; filename=\"somepicture2.jpg\"\r\n"
1218 "Content-Type: image/jpeg\r\n"
1221 "-----------------------------277531038314945--";
1222 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
1230 memset(&th_v, 0,
sizeof(th_v));
1231 memset(&ssn, 0,
sizeof(ssn));
1238 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;)");
1247 f->
proto = IPPROTO_TCP;
1303 tx_ud = htp_tx_get_user_data(tx);
1324 static int DeStateSigTest10(
void)
1326 uint8_t httpbuf1[] =
"POST /upload.cgi HTTP/1.1\r\n"
1327 "Host: www.server.lan\r\n"
1328 "Content-Type: multipart/form-data; boundary=---------------------------277531038314945\r\n"
1329 "Content-Length: 440\r\n"
1331 "-----------------------------277531038314945\r\n"
1332 "Content-Disposition: form-data; name=\"uploadfile_0\"; filename=\"somepicture1.jpg\"\r\n"
1333 "Content-Type: image/jpeg\r\n"
1336 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
1337 uint8_t httpbuf2[] =
"file";
1338 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
1339 uint8_t httpbuf3[] =
"content\r\n"
1340 "-----------------------------277531038314945\r\n";
1341 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
1343 uint8_t httpbuf4[] =
"Content-Disposition: form-data; name=\"uploadfile_1\"; filename=\"somepicture2.jpg\"\r\n"
1344 "Content-Type: image/jpeg\r\n"
1347 "-----------------------------277531038314945--";
1348 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
1356 memset(&th_v, 0,
sizeof(th_v));
1357 memset(&ssn, 0,
sizeof(ssn));
1373 f->
proto = IPPROTO_TCP;
1429 tx_ud = htp_tx_get_user_data(tx);