PageRenderTime 63ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/detect-engine-uri.c

https://github.com/decanio/suricata-tilera
C | 3897 lines | 3009 code | 744 blank | 144 comment | 656 complexity | 33c718194613c083cfdf1a14767e0850 MD5 | raw file
Possible License(s): GPL-2.0, Unlicense, AGPL-1.0
  1. /* Copyright (C) 2007-2010 Open Information Security Foundation
  2. *
  3. * You can copy, redistribute or modify this Program under the terms of
  4. * the GNU General Public License version 2 as published by the Free
  5. * Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * version 2 along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. * 02110-1301, USA.
  16. */
  17. /** \file
  18. *
  19. * \author Victor Julien <victor@inliniac.net>
  20. * \author Pablo Rincon Crespo <pablo.rincon.crespo@gmail.com>
  21. *
  22. * Based on detect-engine-uri.c
  23. */
  24. #include "suricata-common.h"
  25. #include "suricata.h"
  26. #include "decode.h"
  27. #include "detect.h"
  28. #include "detect-engine.h"
  29. #include "detect-parse.h"
  30. #include "detect-engine-state.h"
  31. #include "detect-engine-content-inspection.h"
  32. #include "flow-util.h"
  33. #include "util-debug.h"
  34. #include "util-print.h"
  35. #include "flow.h"
  36. #include "app-layer-parser.h"
  37. #include "util-unittest.h"
  38. #include "util-unittest-helper.h"
  39. #include "app-layer.h"
  40. #include "app-layer-htp.h"
  41. #include "app-layer-protos.h"
  42. /** \brief Do the content inspection & validation for a signature
  43. *
  44. * \param de_ctx Detection engine context
  45. * \param det_ctx Detection engine thread context
  46. * \param s Signature to inspect
  47. * \param sm SigMatch to inspect
  48. * \param f Flow
  49. * \param flags app layer flags
  50. * \param state App layer state
  51. *
  52. * \retval 0 no match
  53. * \retval 1 match
  54. */
  55. int DetectEngineInspectPacketUris(ThreadVars *tv,
  56. DetectEngineCtx *de_ctx,
  57. DetectEngineThreadCtx *det_ctx,
  58. Signature *s, Flow *f, uint8_t flags,
  59. void *alstate, int tx_id)
  60. {
  61. HtpState *htp_state = (HtpState *)alstate;
  62. htp_tx_t *tx = list_get(htp_state->connp->conn->transactions, tx_id);
  63. if (tx == NULL || tx->request_uri_normalized == NULL)
  64. return 0;
  65. det_ctx->discontinue_matching = 0;
  66. det_ctx->buffer_offset = 0;
  67. det_ctx->inspection_recursion_counter = 0;
  68. //PrintRawDataFp(stdout, (uint8_t *)bstr_ptr(tx->request_uri_normalized),
  69. // bstr_len(tx->request_uri_normalized));
  70. /* Inspect all the uricontents fetched on each
  71. * transaction at the app layer */
  72. int r = DetectEngineContentInspection(de_ctx, det_ctx, s, s->sm_lists[DETECT_SM_LIST_UMATCH],
  73. f,
  74. (uint8_t *)bstr_ptr(tx->request_uri_normalized),
  75. bstr_len(tx->request_uri_normalized),
  76. DETECT_ENGINE_CONTENT_INSPECTION_MODE_URI, NULL);
  77. if (r == 1) {
  78. return 1;
  79. }
  80. return 0;
  81. }
  82. /***********************************Unittests**********************************/
  83. #ifdef UNITTESTS
  84. /** \test Test a simple uricontent option */
  85. static int UriTestSig01(void)
  86. {
  87. int result = 0;
  88. Flow f;
  89. HtpState *http_state = NULL;
  90. uint8_t http_buf1[] = "POST /one HTTP/1.0\r\n"
  91. "User-Agent: Mozilla/1.0\r\n"
  92. "Cookie: hellocatch\r\n\r\n";
  93. uint32_t http_buf1_len = sizeof(http_buf1) - 1;
  94. uint8_t http_buf2[] = "POST /oneself HTTP/1.0\r\n"
  95. "User-Agent: Mozilla/1.0\r\n"
  96. "Cookie: hellocatch\r\n\r\n";
  97. uint32_t http_buf2_len = sizeof(http_buf2) - 1;
  98. TcpSession ssn;
  99. Packet *p = NULL;
  100. Signature *s = NULL;
  101. ThreadVars tv;
  102. DetectEngineThreadCtx *det_ctx = NULL;
  103. memset(&tv, 0, sizeof(ThreadVars));
  104. memset(&f, 0, sizeof(Flow));
  105. memset(&ssn, 0, sizeof(TcpSession));
  106. p = UTHBuildPacket(http_buf1, http_buf1_len, IPPROTO_TCP);
  107. FLOW_INITIALIZE(&f);
  108. f.protoctx = (void *)&ssn;
  109. f.flags |= FLOW_IPV4;
  110. p->flow = &f;
  111. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  112. p->flowflags |= FLOW_PKT_TOSERVER;
  113. p->flowflags |= FLOW_PKT_ESTABLISHED;
  114. f.alproto = ALPROTO_HTTP;
  115. StreamTcpInitConfig(TRUE);
  116. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  117. if (de_ctx == NULL) {
  118. goto end;
  119. }
  120. de_ctx->mpm_matcher = MPM_B2G;
  121. de_ctx->flags |= DE_QUIET;
  122. s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  123. "(msg:\"Test uricontent option\"; "
  124. "uricontent:\"one\"; sid:1;)");
  125. if (s == NULL) {
  126. goto end;
  127. }
  128. SigGroupBuild(de_ctx);
  129. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  130. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf1, http_buf1_len);
  131. if (r != 0) {
  132. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  133. goto end;
  134. }
  135. http_state = f.alstate;
  136. if (http_state == NULL) {
  137. printf("no http state: ");
  138. goto end;
  139. }
  140. /* do detect */
  141. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  142. if (!PacketAlertCheck(p, 1)) {
  143. printf("sig 1 alerted, but it should not: ");
  144. goto end;
  145. }
  146. DetectEngineStateReset(f.de_state);
  147. r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf2, http_buf2_len);
  148. if (r != 0) {
  149. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  150. goto end;
  151. }
  152. http_state = f.alstate;
  153. if (http_state == NULL) {
  154. printf("no http state: ");
  155. goto end;
  156. }
  157. if (!PacketAlertCheck(p, 1)) {
  158. printf("sig 1 alerted, but it should not: ");
  159. goto end;
  160. }
  161. /* do detect */
  162. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  163. result = 1;
  164. end:
  165. if (det_ctx != NULL)
  166. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  167. if (de_ctx != NULL)
  168. SigGroupCleanup(de_ctx);
  169. if (de_ctx != NULL)
  170. DetectEngineCtxFree(de_ctx);
  171. StreamTcpFreeConfig(TRUE);
  172. FLOW_DESTROY(&f);
  173. UTHFreePacket(p);
  174. return result;
  175. }
  176. /** \test Test the pcre /U option */
  177. static int UriTestSig02(void)
  178. {
  179. int result = 0;
  180. Flow f;
  181. HtpState *http_state = NULL;
  182. uint8_t http_buf1[] = "POST /on HTTP/1.0\r\n"
  183. "User-Agent: Mozilla/1.0\r\n"
  184. "Cookie: hellocatch\r\n\r\n";
  185. uint32_t http_buf1_len = sizeof(http_buf1) - 1;
  186. uint8_t http_buf2[] = "POST /one HTTP/1.0\r\n"
  187. "User-Agent: Mozilla/1.0\r\n"
  188. "Cookie: hellocatch\r\n\r\n";
  189. uint32_t http_buf2_len = sizeof(http_buf2) - 1;
  190. TcpSession ssn;
  191. Packet *p = NULL;
  192. Signature *s = NULL;
  193. ThreadVars tv;
  194. DetectEngineThreadCtx *det_ctx = NULL;
  195. memset(&tv, 0, sizeof(ThreadVars));
  196. memset(&f, 0, sizeof(Flow));
  197. memset(&ssn, 0, sizeof(TcpSession));
  198. p = UTHBuildPacket(http_buf1, http_buf1_len, IPPROTO_TCP);
  199. FLOW_INITIALIZE(&f);
  200. f.protoctx = (void *)&ssn;
  201. f.flags |= FLOW_IPV4;
  202. p->flow = &f;
  203. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  204. p->flowflags |= FLOW_PKT_TOSERVER;
  205. p->flowflags |= FLOW_PKT_ESTABLISHED;
  206. f.alproto = ALPROTO_HTTP;
  207. StreamTcpInitConfig(TRUE);
  208. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  209. if (de_ctx == NULL) {
  210. goto end;
  211. }
  212. de_ctx->mpm_matcher = MPM_B2G;
  213. de_ctx->flags |= DE_QUIET;
  214. s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  215. "(msg:\"Test pcre /U option\"; "
  216. "pcre:/one/U; sid:1;)");
  217. if (s == NULL) {
  218. goto end;
  219. }
  220. SigGroupBuild(de_ctx);
  221. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  222. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf1, http_buf1_len);
  223. if (r != 0) {
  224. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  225. goto end;
  226. }
  227. http_state = f.alstate;
  228. if (http_state == NULL) {
  229. printf("no http state: ");
  230. goto end;
  231. }
  232. /* do detect */
  233. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  234. if (PacketAlertCheck(p, 1)) {
  235. printf("sig 1 alerted with payload2, but it should not: ");
  236. goto end;
  237. }
  238. DetectEngineStateReset(f.de_state);
  239. r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf2, http_buf2_len);
  240. if (r != 0) {
  241. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  242. goto end;
  243. }
  244. http_state = f.alstate;
  245. if (http_state == NULL) {
  246. printf("no http state: ");
  247. goto end;
  248. }
  249. /* do detect */
  250. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  251. if (!PacketAlertCheck(p, 1)) {
  252. printf("sig 1 didnt alert, but it should: ");
  253. goto end;
  254. }
  255. result = 1;
  256. end:
  257. if (det_ctx != NULL)
  258. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  259. if (de_ctx != NULL)
  260. SigGroupCleanup(de_ctx);
  261. if (de_ctx != NULL)
  262. DetectEngineCtxFree(de_ctx);
  263. StreamTcpFreeConfig(TRUE);
  264. FLOW_DESTROY(&f);
  265. UTHFreePacket(p);
  266. return result;
  267. }
  268. /** \test Test the pcre /U option */
  269. static int UriTestSig03(void)
  270. {
  271. int result = 0;
  272. Flow f;
  273. HtpState *http_state = NULL;
  274. uint8_t http_buf1[] = "POST /one HTTP/1.0\r\n"
  275. "User-Agent: Mozilla/1.0\r\n"
  276. "Cookie: hellocatch\r\n\r\n";
  277. uint32_t http_buf1_len = sizeof(http_buf1) - 1;
  278. uint8_t http_buf2[] = "POST /oneself HTTP/1.0\r\n"
  279. "User-Agent: Mozilla/1.0\r\n"
  280. "Cookie: hellocatch\r\n\r\n";
  281. uint32_t http_buf2_len = sizeof(http_buf2) - 1;
  282. TcpSession ssn;
  283. Packet *p = NULL;
  284. Signature *s = NULL;
  285. ThreadVars tv;
  286. DetectEngineThreadCtx *det_ctx = NULL;
  287. memset(&tv, 0, sizeof(ThreadVars));
  288. memset(&f, 0, sizeof(Flow));
  289. memset(&ssn, 0, sizeof(TcpSession));
  290. p = UTHBuildPacket(http_buf1, http_buf1_len, IPPROTO_TCP);
  291. FLOW_INITIALIZE(&f);
  292. f.protoctx = (void *)&ssn;
  293. f.flags |= FLOW_IPV4;
  294. p->flow = &f;
  295. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  296. p->flowflags |= FLOW_PKT_TOSERVER;
  297. p->flowflags |= FLOW_PKT_ESTABLISHED;
  298. f.alproto = ALPROTO_HTTP;
  299. StreamTcpInitConfig(TRUE);
  300. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  301. if (de_ctx == NULL) {
  302. goto end;
  303. }
  304. de_ctx->mpm_matcher = MPM_B2G;
  305. de_ctx->flags |= DE_QUIET;
  306. s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  307. "(msg:\"Test pcre /U option\"; "
  308. "pcre:/blah/U; sid:1;)");
  309. if (s == NULL) {
  310. goto end;
  311. }
  312. SigGroupBuild(de_ctx);
  313. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  314. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf1, http_buf1_len);
  315. if (r != 0) {
  316. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  317. goto end;
  318. }
  319. http_state = f.alstate;
  320. if (http_state == NULL) {
  321. printf("no http state: ");
  322. goto end;
  323. }
  324. /* do detect */
  325. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  326. if (PacketAlertCheck(p, 1)) {
  327. printf("sig 1 alerted, but it should not: ");
  328. goto end;
  329. }
  330. DetectEngineStateReset(f.de_state);
  331. r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf2, http_buf2_len);
  332. if (r != 0) {
  333. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  334. goto end;
  335. }
  336. http_state = f.alstate;
  337. if (http_state == NULL) {
  338. printf("no http state: ");
  339. goto end;
  340. }
  341. /* do detect */
  342. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  343. if (PacketAlertCheck(p, 1)) {
  344. printf("sig 1 alerted, but it should not: ");
  345. goto end;
  346. }
  347. result = 1;
  348. end:
  349. if (det_ctx != NULL)
  350. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  351. if (de_ctx != NULL)
  352. SigGroupCleanup(de_ctx);
  353. if (de_ctx != NULL)
  354. DetectEngineCtxFree(de_ctx);
  355. StreamTcpFreeConfig(TRUE);
  356. FLOW_DESTROY(&f);
  357. UTHFreePacket(p);
  358. return result;
  359. }
  360. /** \test Test the urilen option */
  361. static int UriTestSig04(void)
  362. {
  363. int result = 0;
  364. Flow f;
  365. HtpState *http_state = NULL;
  366. uint8_t http_buf1[] = "POST /one HTTP/1.0\r\n"
  367. "User-Agent: Mozilla/1.0\r\n"
  368. "Cookie: hellocatch\r\n\r\n";
  369. uint32_t http_buf1_len = sizeof(http_buf1) - 1;
  370. uint8_t http_buf2[] = "POST /oneself HTTP/1.0\r\n"
  371. "User-Agent: Mozilla/1.0\r\n"
  372. "Cookie: hellocatch\r\n\r\n";
  373. uint32_t http_buf2_len = sizeof(http_buf2) - 1;
  374. TcpSession ssn;
  375. Packet *p = NULL;
  376. Signature *s = NULL;
  377. ThreadVars tv;
  378. DetectEngineThreadCtx *det_ctx = NULL;
  379. memset(&tv, 0, sizeof(ThreadVars));
  380. memset(&f, 0, sizeof(Flow));
  381. memset(&ssn, 0, sizeof(TcpSession));
  382. p = UTHBuildPacket(http_buf1, http_buf1_len, IPPROTO_TCP);
  383. FLOW_INITIALIZE(&f);
  384. f.protoctx = (void *)&ssn;
  385. f.flags |= FLOW_IPV4;
  386. p->flow = &f;
  387. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  388. p->flowflags |= FLOW_PKT_TOSERVER;
  389. p->flowflags |= FLOW_PKT_ESTABLISHED;
  390. f.alproto = ALPROTO_HTTP;
  391. StreamTcpInitConfig(TRUE);
  392. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  393. if (de_ctx == NULL) {
  394. goto end;
  395. }
  396. de_ctx->mpm_matcher = MPM_B2G;
  397. de_ctx->flags |= DE_QUIET;
  398. s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  399. "(msg:\"Test urilen option\"; "
  400. "urilen:>20; sid:1;)");
  401. if (s == NULL) {
  402. goto end;
  403. }
  404. SigGroupBuild(de_ctx);
  405. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  406. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf1, http_buf1_len);
  407. if (r != 0) {
  408. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  409. goto end;
  410. }
  411. http_state = f.alstate;
  412. if (http_state == NULL) {
  413. printf("no http state: ");
  414. goto end;
  415. }
  416. /* do detect */
  417. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  418. if (PacketAlertCheck(p, 1)) {
  419. printf("sig 1 alerted, but it should not: ");
  420. goto end;
  421. }
  422. DetectEngineStateReset(f.de_state);
  423. r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf2, http_buf2_len);
  424. if (r != 0) {
  425. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  426. goto end;
  427. }
  428. http_state = f.alstate;
  429. if (http_state == NULL) {
  430. printf("no http state: ");
  431. goto end;
  432. }
  433. /* do detect */
  434. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  435. if (PacketAlertCheck(p, 1)) {
  436. printf("sig 1 alerted, but it should not: ");
  437. goto end;
  438. }
  439. result = 1;
  440. end:
  441. if (det_ctx != NULL)
  442. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  443. if (de_ctx != NULL)
  444. SigGroupCleanup(de_ctx);
  445. if (de_ctx != NULL)
  446. DetectEngineCtxFree(de_ctx);
  447. StreamTcpFreeConfig(TRUE);
  448. FLOW_DESTROY(&f);
  449. UTHFreePacket(p);
  450. return result;
  451. }
  452. /** \test Test the urilen option */
  453. static int UriTestSig05(void)
  454. {
  455. int result = 0;
  456. Flow f;
  457. HtpState *http_state = NULL;
  458. uint8_t http_buf1[] = "POST /one HTTP/1.0\r\n"
  459. "User-Agent: Mozilla/1.0\r\n"
  460. "Cookie: hellocatch\r\n\r\n";
  461. uint32_t http_buf1_len = sizeof(http_buf1) - 1;
  462. uint8_t http_buf2[] = "POST /oneself HTTP/1.0\r\n"
  463. "User-Agent: Mozilla/1.0\r\n"
  464. "Cookie: hellocatch\r\n\r\n";
  465. uint32_t http_buf2_len = sizeof(http_buf2) - 1;
  466. TcpSession ssn;
  467. Packet *p = NULL;
  468. Signature *s = NULL;
  469. ThreadVars tv;
  470. DetectEngineThreadCtx *det_ctx = NULL;
  471. memset(&tv, 0, sizeof(ThreadVars));
  472. memset(&f, 0, sizeof(Flow));
  473. memset(&ssn, 0, sizeof(TcpSession));
  474. p = UTHBuildPacket(http_buf1, http_buf1_len, IPPROTO_TCP);
  475. FLOW_INITIALIZE(&f);
  476. f.protoctx = (void *)&ssn;
  477. f.flags |= FLOW_IPV4;
  478. p->flow = &f;
  479. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  480. p->flowflags |= FLOW_PKT_TOSERVER;
  481. p->flowflags |= FLOW_PKT_ESTABLISHED;
  482. f.alproto = ALPROTO_HTTP;
  483. StreamTcpInitConfig(TRUE);
  484. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  485. if (de_ctx == NULL) {
  486. goto end;
  487. }
  488. de_ctx->mpm_matcher = MPM_B2G;
  489. de_ctx->flags |= DE_QUIET;
  490. s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  491. "(msg:\"Test urilen option\"; "
  492. "urilen:>4; sid:1;)");
  493. if (s == NULL) {
  494. goto end;
  495. }
  496. SigGroupBuild(de_ctx);
  497. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  498. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf1, http_buf1_len);
  499. if (r != 0) {
  500. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  501. goto end;
  502. }
  503. http_state = f.alstate;
  504. if (http_state == NULL) {
  505. printf("no http state: ");
  506. goto end;
  507. }
  508. /* do detect */
  509. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  510. if (PacketAlertCheck(p, 1)) {
  511. printf("sig 1 alerted, but it should not: ");
  512. goto end;
  513. }
  514. DetectEngineStateReset(f.de_state);
  515. r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf2, http_buf2_len);
  516. if (r != 0) {
  517. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  518. goto end;
  519. }
  520. http_state = f.alstate;
  521. if (http_state == NULL) {
  522. printf("no http state: ");
  523. goto end;
  524. }
  525. /* do detect */
  526. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  527. if (!PacketAlertCheck(p, 1)) {
  528. printf("sig 1 didnt alert with payload2, but it should: ");
  529. goto end;
  530. }
  531. result = 1;
  532. end:
  533. if (det_ctx != NULL)
  534. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  535. if (de_ctx != NULL)
  536. SigGroupCleanup(de_ctx);
  537. if (de_ctx != NULL)
  538. DetectEngineCtxFree(de_ctx);
  539. StreamTcpFreeConfig(TRUE);
  540. FLOW_DESTROY(&f);
  541. UTHFreePacket(p);
  542. return result;
  543. }
  544. /** \test Test the pcre /U option */
  545. static int UriTestSig06(void)
  546. {
  547. int result = 0;
  548. Flow f;
  549. HtpState *http_state = NULL;
  550. uint8_t http_buf1[] = "POST /oneoneoneone HTTP/1.0\r\n"
  551. "User-Agent: Mozilla/1.0\r\n"
  552. "Cookie: hellocatch\r\n\r\n";
  553. uint32_t http_buf1_len = sizeof(http_buf1) - 1;
  554. uint8_t http_buf2[] = "POST /oneself HTTP/1.0\r\n"
  555. "User-Agent: Mozilla/1.0\r\n"
  556. "Cookie: hellocatch\r\n\r\n";
  557. uint32_t http_buf2_len = sizeof(http_buf2) - 1;
  558. TcpSession ssn;
  559. Packet *p = NULL;
  560. Signature *s = NULL;
  561. ThreadVars tv;
  562. DetectEngineThreadCtx *det_ctx = NULL;
  563. memset(&tv, 0, sizeof(ThreadVars));
  564. memset(&f, 0, sizeof(Flow));
  565. memset(&ssn, 0, sizeof(TcpSession));
  566. p = UTHBuildPacket(http_buf1, http_buf1_len, IPPROTO_TCP);
  567. FLOW_INITIALIZE(&f);
  568. f.protoctx = (void *)&ssn;
  569. f.flags |= FLOW_IPV4;
  570. p->flow = &f;
  571. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  572. p->flowflags |= FLOW_PKT_TOSERVER;
  573. p->flowflags |= FLOW_PKT_ESTABLISHED;
  574. f.alproto = ALPROTO_HTTP;
  575. StreamTcpInitConfig(TRUE);
  576. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  577. if (de_ctx == NULL) {
  578. goto end;
  579. }
  580. de_ctx->mpm_matcher = MPM_B2G;
  581. de_ctx->flags |= DE_QUIET;
  582. s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  583. "(msg:\"Test pcre /U option\"; "
  584. "pcre:/(oneself)+/U; sid:1;)");
  585. if (s == NULL) {
  586. goto end;
  587. }
  588. SigGroupBuild(de_ctx);
  589. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  590. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf1, http_buf1_len);
  591. if (r != 0) {
  592. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  593. goto end;
  594. }
  595. http_state = f.alstate;
  596. if (http_state == NULL) {
  597. printf("no http state: ");
  598. goto end;
  599. }
  600. /* do detect */
  601. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  602. if (PacketAlertCheck(p, 1)) {
  603. printf("sig 1 alerted, but it should not: ");
  604. goto end;
  605. }
  606. DetectEngineStateReset(f.de_state);
  607. r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf2, http_buf2_len);
  608. if (r != 0) {
  609. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  610. goto end;
  611. }
  612. http_state = f.alstate;
  613. if (http_state == NULL) {
  614. printf("no http state: ");
  615. goto end;
  616. }
  617. /* do detect */
  618. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  619. if (!PacketAlertCheck(p, 1)) {
  620. printf("sig 1 didnt alert on payload2, but it should: ");
  621. goto end;
  622. }
  623. result = 1;
  624. end:
  625. if (det_ctx != NULL)
  626. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  627. if (de_ctx != NULL)
  628. SigGroupCleanup(de_ctx);
  629. if (de_ctx != NULL)
  630. DetectEngineCtxFree(de_ctx);
  631. StreamTcpFreeConfig(TRUE);
  632. FLOW_DESTROY(&f);
  633. UTHFreePacket(p);
  634. return result;
  635. }
  636. /** \test Test the pcre /U option in combination with urilen */
  637. static int UriTestSig07(void)
  638. {
  639. int result = 0;
  640. Flow f;
  641. HtpState *http_state = NULL;
  642. uint8_t http_buf1[] = "POST /oneoneoneone HTTP/1.0\r\n"
  643. "User-Agent: Mozilla/1.0\r\n"
  644. "Cookie: hellocatch\r\n\r\n";
  645. uint32_t http_buf1_len = sizeof(http_buf1) - 1;
  646. uint8_t http_buf2[] = "POST /oneoneself HTTP/1.0\r\n"
  647. "User-Agent: Mozilla/1.0\r\n"
  648. "Cookie: hellocatch\r\n\r\n";
  649. uint32_t http_buf2_len = sizeof(http_buf2) - 1;
  650. TcpSession ssn;
  651. Packet *p = NULL;
  652. Signature *s = NULL;
  653. ThreadVars tv;
  654. DetectEngineThreadCtx *det_ctx = NULL;
  655. memset(&tv, 0, sizeof(ThreadVars));
  656. memset(&f, 0, sizeof(Flow));
  657. memset(&ssn, 0, sizeof(TcpSession));
  658. p = UTHBuildPacket(http_buf1, http_buf1_len, IPPROTO_TCP);
  659. FLOW_INITIALIZE(&f);
  660. f.protoctx = (void *)&ssn;
  661. f.flags |= FLOW_IPV4;
  662. p->flow = &f;
  663. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  664. p->flowflags |= FLOW_PKT_TOSERVER;
  665. p->flowflags |= FLOW_PKT_ESTABLISHED;
  666. f.alproto = ALPROTO_HTTP;
  667. StreamTcpInitConfig(TRUE);
  668. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  669. if (de_ctx == NULL) {
  670. goto end;
  671. }
  672. de_ctx->mpm_matcher = MPM_B2G;
  673. de_ctx->flags |= DE_QUIET;
  674. s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  675. "(msg:\"Test pcre /U option with urilen \"; "
  676. "pcre:/(one){2,}(self)?/U; urilen:3<>20; sid:1;)");
  677. if (s == NULL) {
  678. goto end;
  679. }
  680. SigGroupBuild(de_ctx);
  681. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  682. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf1, http_buf1_len);
  683. if (r != 0) {
  684. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  685. goto end;
  686. }
  687. http_state = f.alstate;
  688. if (http_state == NULL) {
  689. printf("no http state: ");
  690. goto end;
  691. }
  692. /* do detect */
  693. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  694. if (!PacketAlertCheck(p, 1)) {
  695. printf("sig 1 didnt alert, but it should: ");
  696. goto end;
  697. }
  698. DetectEngineStateReset(f.de_state);
  699. r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf2, http_buf2_len);
  700. if (r != 0) {
  701. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  702. goto end;
  703. }
  704. http_state = f.alstate;
  705. if (http_state == NULL) {
  706. printf("no http state: ");
  707. goto end;
  708. }
  709. /* do detect */
  710. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  711. if (!PacketAlertCheck(p, 1)) {
  712. printf("sig 1 didnt alert with payload2, but it should: ");
  713. goto end;
  714. }
  715. result = 1;
  716. end:
  717. if (det_ctx != NULL)
  718. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  719. if (de_ctx != NULL)
  720. SigGroupCleanup(de_ctx);
  721. if (de_ctx != NULL)
  722. DetectEngineCtxFree(de_ctx);
  723. StreamTcpFreeConfig(TRUE);
  724. FLOW_DESTROY(&f);
  725. UTHFreePacket(p);
  726. return result;
  727. }
  728. /** \test Test the pcre /U option in combination with urilen */
  729. static int UriTestSig08(void)
  730. {
  731. int result = 0;
  732. Flow f;
  733. HtpState *http_state = NULL;
  734. uint8_t http_buf1[] = "POST /oneoneoneone HTTP/1.0\r\n"
  735. "User-Agent: Mozilla/1.0\r\n"
  736. "Cookie: hellocatch\r\n\r\n";
  737. uint32_t http_buf1_len = sizeof(http_buf1) - 1;
  738. uint8_t http_buf2[] = "POST /oneoneself HTTP/1.0\r\n"
  739. "User-Agent: Mozilla/1.0\r\n"
  740. "Cookie: hellocatch\r\n\r\n";
  741. uint32_t http_buf2_len = sizeof(http_buf2) - 1;
  742. TcpSession ssn;
  743. Packet *p = NULL;
  744. Signature *s = NULL;
  745. ThreadVars tv;
  746. DetectEngineThreadCtx *det_ctx = NULL;
  747. memset(&tv, 0, sizeof(ThreadVars));
  748. memset(&f, 0, sizeof(Flow));
  749. memset(&ssn, 0, sizeof(TcpSession));
  750. p = UTHBuildPacket(http_buf1, http_buf1_len, IPPROTO_TCP);
  751. FLOW_INITIALIZE(&f);
  752. f.protoctx = (void *)&ssn;
  753. f.flags |= FLOW_IPV4;
  754. p->flow = &f;
  755. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  756. p->flowflags |= FLOW_PKT_TOSERVER;
  757. p->flowflags |= FLOW_PKT_ESTABLISHED;
  758. f.alproto = ALPROTO_HTTP;
  759. StreamTcpInitConfig(TRUE);
  760. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  761. if (de_ctx == NULL) {
  762. goto end;
  763. }
  764. de_ctx->mpm_matcher = MPM_B2G;
  765. de_ctx->flags |= DE_QUIET;
  766. s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  767. "(msg:\"Test pcre /U option with urilen\"; "
  768. "pcre:/(blabla){2,}(self)?/U; urilen:3<>20; sid:1;)");
  769. if (s == NULL) {
  770. goto end;
  771. }
  772. SigGroupBuild(de_ctx);
  773. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  774. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf1, http_buf1_len);
  775. if (r != 0) {
  776. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  777. goto end;
  778. }
  779. http_state = f.alstate;
  780. if (http_state == NULL) {
  781. printf("no http state: ");
  782. goto end;
  783. }
  784. /* do detect */
  785. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  786. if (PacketAlertCheck(p, 1)) {
  787. printf("sig 1 alerted, but it should not: ");
  788. goto end;
  789. }
  790. DetectEngineStateReset(f.de_state);
  791. r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf2, http_buf2_len);
  792. if (r != 0) {
  793. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  794. goto end;
  795. }
  796. http_state = f.alstate;
  797. if (http_state == NULL) {
  798. printf("no http state: ");
  799. goto end;
  800. }
  801. /* do detect */
  802. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  803. if (PacketAlertCheck(p, 1)) {
  804. printf("sig 1 alerted, but it should not: ");
  805. goto end;
  806. }
  807. result = 1;
  808. end:
  809. if (det_ctx != NULL)
  810. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  811. if (de_ctx != NULL)
  812. SigGroupCleanup(de_ctx);
  813. if (de_ctx != NULL)
  814. DetectEngineCtxFree(de_ctx);
  815. StreamTcpFreeConfig(TRUE);
  816. FLOW_DESTROY(&f);
  817. UTHFreePacket(p);
  818. return result;
  819. }
  820. /** \test Test the pcre /U option in combination with urilen */
  821. static int UriTestSig09(void)
  822. {
  823. int result = 0;
  824. Flow f;
  825. HtpState *http_state = NULL;
  826. uint8_t http_buf1[] = "POST /oneoneoneone HTTP/1.0\r\n"
  827. "User-Agent: Mozilla/1.0\r\n"
  828. "Cookie: hellocatch\r\n\r\n";
  829. uint32_t http_buf1_len = sizeof(http_buf1) - 1;
  830. uint8_t http_buf2[] = "POST /oneoneself HTTP/1.0\r\n"
  831. "User-Agent: Mozilla/1.0\r\n"
  832. "Cookie: hellocatch\r\n\r\n";
  833. uint32_t http_buf2_len = sizeof(http_buf2) - 1;
  834. TcpSession ssn;
  835. Packet *p = NULL;
  836. Signature *s = NULL;
  837. ThreadVars tv;
  838. DetectEngineThreadCtx *det_ctx = NULL;
  839. memset(&tv, 0, sizeof(ThreadVars));
  840. memset(&f, 0, sizeof(Flow));
  841. memset(&ssn, 0, sizeof(TcpSession));
  842. p = UTHBuildPacket(http_buf1, http_buf1_len, IPPROTO_TCP);
  843. FLOW_INITIALIZE(&f);
  844. f.protoctx = (void *)&ssn;
  845. f.flags |= FLOW_IPV4;
  846. p->flow = &f;
  847. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  848. p->flowflags |= FLOW_PKT_TOSERVER;
  849. p->flowflags |= FLOW_PKT_ESTABLISHED;
  850. f.alproto = ALPROTO_HTTP;
  851. StreamTcpInitConfig(TRUE);
  852. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  853. if (de_ctx == NULL) {
  854. goto end;
  855. }
  856. de_ctx->mpm_matcher = MPM_B2G;
  857. de_ctx->flags |= DE_QUIET;
  858. s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  859. "(msg:\"Test pcre /U option with urilen \"; "
  860. "pcre:/(one){2,}(self)?/U; urilen:<2; sid:1;)");
  861. if (s == NULL) {
  862. goto end;
  863. }
  864. SigGroupBuild(de_ctx);
  865. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  866. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf1, http_buf1_len);
  867. if (r != 0) {
  868. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  869. goto end;
  870. }
  871. http_state = f.alstate;
  872. if (http_state == NULL) {
  873. printf("no http state: ");
  874. goto end;
  875. }
  876. /* do detect */
  877. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  878. if (PacketAlertCheck(p, 1)) {
  879. printf("sig 1 alerted, but it should not: ");
  880. goto end;
  881. }
  882. DetectEngineStateReset(f.de_state);
  883. r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf2, http_buf2_len);
  884. if (r != 0) {
  885. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  886. goto end;
  887. }
  888. http_state = f.alstate;
  889. if (http_state == NULL) {
  890. printf("no http state: ");
  891. goto end;
  892. }
  893. /* do detect */
  894. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  895. if (PacketAlertCheck(p, 1)) {
  896. printf("sig 1 alerted, but it should not: ");
  897. goto end;
  898. }
  899. result = 1;
  900. end:
  901. if (det_ctx != NULL)
  902. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  903. if (de_ctx != NULL)
  904. SigGroupCleanup(de_ctx);
  905. if (de_ctx != NULL)
  906. DetectEngineCtxFree(de_ctx);
  907. StreamTcpFreeConfig(TRUE);
  908. FLOW_DESTROY(&f);
  909. UTHFreePacket(p);
  910. return result;
  911. }
  912. /** \test Test the uricontent option in combination with urilen */
  913. static int UriTestSig10(void)
  914. {
  915. int result = 0;
  916. Flow f;
  917. HtpState *http_state = NULL;
  918. uint8_t http_buf1[] = "POST /oneoneoneone HTTP/1.0\r\n"
  919. "User-Agent: Mozilla/1.0\r\n"
  920. "Cookie: hellocatch\r\n\r\n";
  921. uint32_t http_buf1_len = sizeof(http_buf1) - 1;
  922. uint8_t http_buf2[] = "POST /oneoneself HTTP/1.0\r\n"
  923. "User-Agent: Mozilla/1.0\r\n"
  924. "Cookie: hellocatch\r\n\r\n";
  925. uint32_t http_buf2_len = sizeof(http_buf2) - 1;
  926. TcpSession ssn;
  927. Packet *p = NULL;
  928. Signature *s = NULL;
  929. ThreadVars tv;
  930. DetectEngineThreadCtx *det_ctx = NULL;
  931. memset(&tv, 0, sizeof(ThreadVars));
  932. memset(&f, 0, sizeof(Flow));
  933. memset(&ssn, 0, sizeof(TcpSession));
  934. p = UTHBuildPacket(http_buf1, http_buf1_len, IPPROTO_TCP);
  935. FLOW_INITIALIZE(&f);
  936. f.protoctx = (void *)&ssn;
  937. f.flags |= FLOW_IPV4;
  938. p->flow = &f;
  939. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  940. p->flowflags |= FLOW_PKT_TOSERVER;
  941. p->flowflags |= FLOW_PKT_ESTABLISHED;
  942. f.alproto = ALPROTO_HTTP;
  943. StreamTcpInitConfig(TRUE);
  944. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  945. if (de_ctx == NULL) {
  946. goto end;
  947. }
  948. de_ctx->mpm_matcher = MPM_B2G;
  949. de_ctx->flags |= DE_QUIET;
  950. s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  951. "(msg:\"Test uricontent with urilen option\"; "
  952. "uricontent:\"one\"; urilen:<2; sid:1;)");
  953. if (s == NULL) {
  954. goto end;
  955. }
  956. SigGroupBuild(de_ctx);
  957. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  958. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf1, http_buf1_len);
  959. if (r != 0) {
  960. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  961. goto end;
  962. }
  963. http_state = f.alstate;
  964. if (http_state == NULL) {
  965. printf("no http state: ");
  966. goto end;
  967. }
  968. /* do detect */
  969. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  970. if (PacketAlertCheck(p, 1)) {
  971. printf("sig 1 alerted, but it should not: ");
  972. goto end;
  973. }
  974. DetectEngineStateReset(f.de_state);
  975. r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf2, http_buf2_len);
  976. if (r != 0) {
  977. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  978. goto end;
  979. }
  980. http_state = f.alstate;
  981. if (http_state == NULL) {
  982. printf("no http state: ");
  983. goto end;
  984. }
  985. /* do detect */
  986. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  987. if (PacketAlertCheck(p, 1)) {
  988. printf("sig 1 alerted, but it should not: ");
  989. goto end;
  990. }
  991. result = 1;
  992. end:
  993. if (det_ctx != NULL)
  994. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  995. if (de_ctx != NULL)
  996. SigGroupCleanup(de_ctx);
  997. if (de_ctx != NULL)
  998. DetectEngineCtxFree(de_ctx);
  999. StreamTcpFreeConfig(TRUE);
  1000. FLOW_DESTROY(&f);
  1001. UTHFreePacket(p);
  1002. return result;
  1003. }
  1004. /** \test Test content, uricontent, urilen, pcre /U options */
  1005. static int UriTestSig11(void)
  1006. {
  1007. int result = 0;
  1008. Flow f;
  1009. HtpState *http_state = NULL;
  1010. uint8_t http_buf1[] = "POST /oneoneoneone HTTP/1.0\r\n"
  1011. "User-Agent: Mozilla/1.0\r\n"
  1012. "Cookie: hellocatch\r\n\r\n";
  1013. uint32_t http_buf1_len = sizeof(http_buf1) - 1;
  1014. uint8_t http_buf2[] = "POST /oneoneself HTTP/1.0\r\n"
  1015. "User-Agent: Mozilla/1.0\r\n"
  1016. "Cookie: hellocatch\r\n\r\n";
  1017. uint32_t http_buf2_len = sizeof(http_buf2) - 1;
  1018. TcpSession ssn;
  1019. Packet *p = NULL;
  1020. Signature *s = NULL;
  1021. ThreadVars tv;
  1022. DetectEngineThreadCtx *det_ctx = NULL;
  1023. memset(&tv, 0, sizeof(ThreadVars));
  1024. memset(&f, 0, sizeof(Flow));
  1025. memset(&ssn, 0, sizeof(TcpSession));
  1026. p = UTHBuildPacket(http_buf1, http_buf1_len, IPPROTO_TCP);
  1027. FLOW_INITIALIZE(&f);
  1028. f.protoctx = (void *)&ssn;
  1029. f.flags |= FLOW_IPV4;
  1030. p->flow = &f;
  1031. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  1032. p->flowflags |= FLOW_PKT_TOSERVER;
  1033. p->flowflags |= FLOW_PKT_ESTABLISHED;
  1034. f.alproto = ALPROTO_HTTP;
  1035. StreamTcpInitConfig(TRUE);
  1036. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  1037. if (de_ctx == NULL) {
  1038. goto end;
  1039. }
  1040. de_ctx->mpm_matcher = MPM_B2G;
  1041. de_ctx->flags |= DE_QUIET;
  1042. s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  1043. "(msg:\"Test content, uricontent, pcre /U and urilen options\"; "
  1044. "content:\"one\"; uricontent:\"one\"; pcre:/(one){2,}(self)?/U;"
  1045. "urilen:<2; sid:1;)");
  1046. if (s == NULL) {
  1047. goto end;
  1048. }
  1049. SigGroupBuild(de_ctx);
  1050. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  1051. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf1, http_buf1_len);
  1052. if (r != 0) {
  1053. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  1054. goto end;
  1055. }
  1056. http_state = f.alstate;
  1057. if (http_state == NULL) {
  1058. printf("no http state: ");
  1059. goto end;
  1060. }
  1061. /* do detect */
  1062. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  1063. if (PacketAlertCheck(p, 1)) {
  1064. printf("sig 1 alerted, but it should not: ");
  1065. goto end;
  1066. }
  1067. DetectEngineStateReset(f.de_state);
  1068. r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf2, http_buf2_len);
  1069. if (r != 0) {
  1070. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  1071. goto end;
  1072. }
  1073. http_state = f.alstate;
  1074. if (http_state == NULL) {
  1075. printf("no http state: ");
  1076. goto end;
  1077. }
  1078. /* do detect */
  1079. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  1080. if (PacketAlertCheck(p, 1)) {
  1081. printf("sig 1 alerted, but it should not: ");
  1082. goto end;
  1083. }
  1084. result = 1;
  1085. end:
  1086. if (det_ctx != NULL)
  1087. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  1088. if (de_ctx != NULL)
  1089. SigGroupCleanup(de_ctx);
  1090. if (de_ctx != NULL)
  1091. DetectEngineCtxFree(de_ctx);
  1092. StreamTcpFreeConfig(TRUE);
  1093. FLOW_DESTROY(&f);
  1094. UTHFreePacket(p);
  1095. return result;
  1096. }
  1097. /** \test Test uricontent, urilen, pcre /U options */
  1098. static int UriTestSig12(void)
  1099. {
  1100. int result = 0;
  1101. Flow f;
  1102. HtpState *http_state = NULL;
  1103. uint8_t http_buf1[] = "POST /oneoneoneone HTTP/1.0\r\n"
  1104. "User-Agent: Mozilla/1.0\r\n"
  1105. "Cookie: hellocatch\r\n\r\n";
  1106. uint32_t http_buf1_len = sizeof(http_buf1) - 1;
  1107. uint8_t http_buf2[] = "POST /oneoneself HTTP/1.0\r\n"
  1108. "User-Agent: Mozilla/1.0\r\n"
  1109. "Cookie: hellocatch\r\n\r\n";
  1110. uint32_t http_buf2_len = sizeof(http_buf2) - 1;
  1111. TcpSession ssn;
  1112. Packet *p = NULL;
  1113. Signature *s = NULL;
  1114. ThreadVars tv;
  1115. DetectEngineThreadCtx *det_ctx = NULL;
  1116. memset(&tv, 0, sizeof(ThreadVars));
  1117. memset(&f, 0, sizeof(Flow));
  1118. memset(&ssn, 0, sizeof(TcpSession));
  1119. p = UTHBuildPacket(http_buf1, http_buf1_len, IPPROTO_TCP);
  1120. FLOW_INITIALIZE(&f);
  1121. f.protoctx = (void *)&ssn;
  1122. f.flags |= FLOW_IPV4;
  1123. p->flow = &f;
  1124. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  1125. p->flowflags |= FLOW_PKT_TOSERVER;
  1126. p->flowflags |= FLOW_PKT_ESTABLISHED;
  1127. f.alproto = ALPROTO_HTTP;
  1128. StreamTcpInitConfig(TRUE);
  1129. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  1130. if (de_ctx == NULL) {
  1131. goto end;
  1132. }
  1133. de_ctx->mpm_matcher = MPM_B2G;
  1134. de_ctx->flags |= DE_QUIET;
  1135. s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  1136. "(msg:\"Test pcre /U, uricontent and urilen option\"; "
  1137. "uricontent:\"one\"; "
  1138. "pcre:/(one)+self/U; urilen:>2; sid:1;)");
  1139. if (s == NULL) {
  1140. goto end;
  1141. }
  1142. SigGroupBuild(de_ctx);
  1143. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  1144. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf1, http_buf1_len);
  1145. if (r != 0) {
  1146. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  1147. goto end;
  1148. }
  1149. http_state = f.alstate;
  1150. if (http_state == NULL) {
  1151. printf("no http state: ");
  1152. goto end;
  1153. }
  1154. /* do detect */
  1155. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  1156. if (PacketAlertCheck(p, 1)) {
  1157. printf("sig 1 alerted, but it should not: ");
  1158. goto end;
  1159. }
  1160. DetectEngineStateReset(f.de_state);
  1161. r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf2, http_buf2_len);
  1162. if (r != 0) {
  1163. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  1164. goto end;
  1165. }
  1166. http_state = f.alstate;
  1167. if (http_state == NULL) {
  1168. printf("no http state: ");
  1169. goto end;
  1170. }
  1171. /* do detect */
  1172. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  1173. if (!PacketAlertCheck(p, 1)) {
  1174. printf("sig 1 didnt alert with payload2, but it should: ");
  1175. goto end;
  1176. }
  1177. result = 1;
  1178. end:
  1179. if (det_ctx != NULL)
  1180. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  1181. if (de_ctx != NULL)
  1182. SigGroupCleanup(de_ctx);
  1183. if (de_ctx != NULL)
  1184. DetectEngineCtxFree(de_ctx);
  1185. StreamTcpFreeConfig(TRUE);
  1186. FLOW_DESTROY(&f);
  1187. UTHFreePacket(p);
  1188. return result;
  1189. }
  1190. /** \test Test uricontent, urilen */
  1191. static int UriTestSig13(void)
  1192. {
  1193. int result = 0;
  1194. Flow f;
  1195. HtpState *http_state = NULL;
  1196. uint8_t http_buf1[] = "POST /one HTTP/1.0\r\n"
  1197. "User-Agent: Mozilla/1.0\r\n"
  1198. "Cookie: hellocatch\r\n\r\n";
  1199. uint32_t http_buf1_len = sizeof(http_buf1) - 1;
  1200. uint8_t http_buf2[] = "POST /oneself HTTP/1.0\r\n"
  1201. "User-Agent: Mozilla/1.0\r\n"
  1202. "Cookie: hellocatch\r\n\r\n";
  1203. uint32_t http_buf2_len = sizeof(http_buf2) - 1;
  1204. TcpSession ssn;
  1205. Packet *p = NULL;
  1206. Signature *s = NULL;
  1207. ThreadVars tv;
  1208. DetectEngineThreadCtx *det_ctx = NULL;
  1209. memset(&tv, 0, sizeof(ThreadVars));
  1210. memset(&f, 0, sizeof(Flow));
  1211. memset(&ssn, 0, sizeof(TcpSession));
  1212. p = UTHBuildPacket(http_buf1, http_buf1_len, IPPROTO_TCP);
  1213. FLOW_INITIALIZE(&f);
  1214. f.protoctx = (void *)&ssn;
  1215. f.flags |= FLOW_IPV4;
  1216. p->flow = &f;
  1217. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  1218. p->flowflags |= FLOW_PKT_TOSERVER;
  1219. p->flowflags |= FLOW_PKT_ESTABLISHED;
  1220. f.alproto = ALPROTO_HTTP;
  1221. StreamTcpInitConfig(TRUE);
  1222. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  1223. if (de_ctx == NULL) {
  1224. goto end;
  1225. }
  1226. de_ctx->mpm_matcher = MPM_B2G;
  1227. de_ctx->flags |= DE_QUIET;
  1228. s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  1229. "(msg:\"Test urilen option\"; "
  1230. "urilen:>2; uricontent:\"one\"; sid:1;)");
  1231. if (s == NULL) {
  1232. goto end;
  1233. }
  1234. SigGroupBuild(de_ctx);
  1235. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  1236. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf1, http_buf1_len);
  1237. if (r != 0) {
  1238. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  1239. goto end;
  1240. }
  1241. http_state = f.alstate;
  1242. if (http_state == NULL) {
  1243. printf("no http state: ");
  1244. goto end;
  1245. }
  1246. /* do detect */
  1247. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  1248. if (!PacketAlertCheck(p, 1)) {
  1249. printf("sig 1 didnt alert with pkt, but it should: ");
  1250. goto end;
  1251. }
  1252. DetectEngineStateReset(f.de_state);
  1253. r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf2, http_buf2_len);
  1254. if (r != 0) {
  1255. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  1256. goto end;
  1257. }
  1258. http_state = f.alstate;
  1259. if (http_state == NULL) {
  1260. printf("no http state: ");
  1261. goto end;
  1262. }
  1263. /* do detect */
  1264. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  1265. if (!PacketAlertCheck(p, 1)) {
  1266. printf("sig 1 didnt alert with payload2, but it should: ");
  1267. goto end;
  1268. }
  1269. result = 1;
  1270. end:
  1271. if (det_ctx != NULL)
  1272. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  1273. if (de_ctx != NULL)
  1274. SigGroupCleanup(de_ctx);
  1275. if (de_ctx != NULL)
  1276. DetectEngineCtxFree(de_ctx);
  1277. StreamTcpFreeConfig(TRUE);
  1278. FLOW_DESTROY(&f);
  1279. UTHFreePacket(p);
  1280. return result;
  1281. }
  1282. /** \test Test uricontent, pcre /U */
  1283. static int UriTestSig14(void)
  1284. {
  1285. int result = 0;
  1286. Flow f;
  1287. HtpState *http_state = NULL;
  1288. uint8_t http_buf1[] = "POST /one HTTP/1.0\r\n"
  1289. "User-Agent: Mozilla/1.0\r\n"
  1290. "Cookie: hellocatch\r\n\r\n";
  1291. uint32_t http_buf1_len = sizeof(http_buf1) - 1;
  1292. uint8_t http_buf2[] = "POST /oneself HTTP/1.0\r\n"
  1293. "User-Agent: Mozilla/1.0\r\n"
  1294. "Cookie: hellocatch\r\n\r\n";
  1295. uint32_t http_buf2_len = sizeof(http_buf2) - 1;
  1296. TcpSession ssn;
  1297. Packet *p = NULL;
  1298. Signature *s = NULL;
  1299. ThreadVars tv;
  1300. DetectEngineThreadCtx *det_ctx = NULL;
  1301. memset(&tv, 0, sizeof(ThreadVars));
  1302. memset(&f, 0, sizeof(Flow));
  1303. memset(&ssn, 0, sizeof(TcpSession));
  1304. p = UTHBuildPacket(http_buf1, http_buf1_len, IPPROTO_TCP);
  1305. FLOW_INITIALIZE(&f);
  1306. f.protoctx = (void *)&ssn;
  1307. f.flags |= FLOW_IPV4;
  1308. p->flow = &f;
  1309. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  1310. p->flowflags |= FLOW_PKT_TOSERVER;
  1311. p->flowflags |= FLOW_PKT_ESTABLISHED;
  1312. f.alproto = ALPROTO_HTTP;
  1313. StreamTcpInitConfig(TRUE);
  1314. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  1315. if (de_ctx == NULL) {
  1316. goto end;
  1317. }
  1318. de_ctx->mpm_matcher = MPM_B2G;
  1319. de_ctx->flags |= DE_QUIET;
  1320. s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  1321. "(msg:\"Test uricontent option\"; "
  1322. "uricontent:\"one\"; pcre:/one(self)?/U;sid:1;)");
  1323. if (s == NULL) {
  1324. goto end;
  1325. }
  1326. SigGroupBuild(de_ctx);
  1327. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  1328. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf1, http_buf1_len);
  1329. if (r != 0) {
  1330. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  1331. goto end;
  1332. }
  1333. http_state = f.alstate;
  1334. if (http_state == NULL) {
  1335. printf("no http state: ");
  1336. goto end;
  1337. }
  1338. /* do detect */
  1339. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  1340. if (!PacketAlertCheck(p, 1)) {
  1341. printf("sig 1 didnt alert with pkt, but it should: ");
  1342. goto end;
  1343. }
  1344. DetectEngineStateReset(f.de_state);
  1345. r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf2, http_buf2_len);
  1346. if (r != 0) {
  1347. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  1348. goto end;
  1349. }
  1350. http_state = f.alstate;
  1351. if (http_state == NULL) {
  1352. printf("no http state: ");
  1353. goto end;
  1354. }
  1355. /* do detect */
  1356. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  1357. if (!PacketAlertCheck(p, 1)) {
  1358. printf("sig 1 didnt alert with payload2, but it should: ");
  1359. goto end;
  1360. }
  1361. result = 1;
  1362. end:
  1363. if (det_ctx != NULL)
  1364. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  1365. if (de_ctx != NULL)
  1366. SigGroupCleanup(de_ctx);
  1367. if (de_ctx != NULL)
  1368. DetectEngineCtxFree(de_ctx);
  1369. StreamTcpFreeConfig(TRUE);
  1370. FLOW_DESTROY(&f);
  1371. UTHFreePacket(p);
  1372. return result;
  1373. }
  1374. /** \test Test pcre /U with anchored regex (bug 155) */
  1375. static int UriTestSig15(void)
  1376. {
  1377. int result = 0;
  1378. Flow f;
  1379. HtpState *http_state = NULL;
  1380. uint8_t http_buf1[] = "POST /one HTTP/1.0\r\n"
  1381. "User-Agent: Mozilla/1.0\r\n"
  1382. "Cookie: hellocatch\r\n\r\n";
  1383. uint32_t http_buf1_len = sizeof(http_buf1) - 1;
  1384. uint8_t http_buf2[] = "POST /oneself HTTP/1.0\r\n"
  1385. "User-Agent: Mozilla/1.0\r\n"
  1386. "Cookie: hellocatch\r\n\r\n";
  1387. uint32_t http_buf2_len = sizeof(http_buf2) - 1;
  1388. TcpSession ssn;
  1389. Packet *p = NULL;
  1390. Signature *s = NULL;
  1391. ThreadVars tv;
  1392. DetectEngineThreadCtx *det_ctx = NULL;
  1393. memset(&tv, 0, sizeof(ThreadVars));
  1394. memset(&f, 0, sizeof(Flow));
  1395. memset(&ssn, 0, sizeof(TcpSession));
  1396. p = UTHBuildPacket(http_buf1, http_buf1_len, IPPROTO_TCP);
  1397. FLOW_INITIALIZE(&f);
  1398. f.protoctx = (void *)&ssn;
  1399. f.flags |= FLOW_IPV4;
  1400. p->flow = &f;
  1401. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  1402. p->flowflags |= FLOW_PKT_TOSERVER;
  1403. p->flowflags |= FLOW_PKT_ESTABLISHED;
  1404. f.alproto = ALPROTO_HTTP;
  1405. StreamTcpInitConfig(TRUE);
  1406. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  1407. if (de_ctx == NULL) {
  1408. goto end;
  1409. }
  1410. de_ctx->mpm_matcher = MPM_B2G;
  1411. de_ctx->flags |= DE_QUIET;
  1412. s = de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  1413. "(msg:\"Test uricontent option\"; "
  1414. "uricontent:\"one\"; pcre:/^\\/one(self)?$/U;sid:1;)");
  1415. if (s == NULL) {
  1416. goto end;
  1417. }
  1418. SigGroupBuild(de_ctx);
  1419. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  1420. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf1, http_buf1_len);
  1421. if (r != 0) {
  1422. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  1423. goto end;
  1424. }
  1425. http_state = f.alstate;
  1426. if (http_state == NULL) {
  1427. printf("no http state: ");
  1428. goto end;
  1429. }
  1430. /* do detect */
  1431. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  1432. if (!PacketAlertCheck(p, 1)) {
  1433. printf("sig 1 didnt alert with pkt, but it should: ");
  1434. goto end;
  1435. }
  1436. DetectEngineStateReset(f.de_state);
  1437. r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf2, http_buf2_len);
  1438. if (r != 0) {
  1439. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  1440. goto end;
  1441. }
  1442. http_state = f.alstate;
  1443. if (http_state == NULL) {
  1444. printf("no http state: ");
  1445. goto end;
  1446. }
  1447. /* do detect */
  1448. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  1449. if (!PacketAlertCheck(p, 1)) {
  1450. printf("sig 1 didnt alert with payload2, but it should: ");
  1451. goto end;
  1452. }
  1453. result = 1;
  1454. end:
  1455. if (det_ctx != NULL)
  1456. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  1457. if (de_ctx != NULL)
  1458. SigGroupCleanup(de_ctx);
  1459. if (de_ctx != NULL)
  1460. DetectEngineCtxFree(de_ctx);
  1461. StreamTcpFreeConfig(TRUE);
  1462. FLOW_DESTROY(&f);
  1463. UTHFreePacket(p);
  1464. return result;
  1465. }
  1466. /** \test Test pcre /U with anchored regex (bug 155) */
  1467. static int UriTestSig16(void)
  1468. {
  1469. int result = 0;
  1470. Flow f;
  1471. HtpState *http_state = NULL;
  1472. uint8_t http_buf1[] = "POST /search?q=123&aq=7123abcee HTTP/1.0\r\n"
  1473. "User-Agent: Mozilla/1.0/\r\n"
  1474. "Host: 1.2.3.4\r\n\r\n";
  1475. uint32_t http_buf1_len = sizeof(http_buf1) - 1;
  1476. uint8_t http_buf2[] = "POST /search?q=123&aq=7123abcee HTTP/1.0\r\n"
  1477. "User-Agent: Mozilla/1.0\r\n"
  1478. "Cookie: hellocatch\r\n\r\n";
  1479. uint32_t http_buf2_len = sizeof(http_buf2) - 1;
  1480. TcpSession ssn;
  1481. Packet *p = NULL;
  1482. Signature *s = NULL;
  1483. ThreadVars tv;
  1484. DetectEngineThreadCtx *det_ctx = NULL;
  1485. memset(&tv, 0, sizeof(ThreadVars));
  1486. memset(&f, 0, sizeof(Flow));
  1487. memset(&ssn, 0, sizeof(TcpSession));
  1488. p = UTHBuildPacket(http_buf1, http_buf1_len, IPPROTO_TCP);
  1489. FLOW_INITIALIZE(&f);
  1490. f.protoctx = (void *)&ssn;
  1491. f.flags |= FLOW_IPV4;
  1492. p->flow = &f;
  1493. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  1494. p->flowflags |= FLOW_PKT_TOSERVER;
  1495. p->flowflags |= FLOW_PKT_ESTABLISHED;
  1496. f.alproto = ALPROTO_HTTP;
  1497. StreamTcpInitConfig(TRUE);
  1498. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  1499. if (de_ctx == NULL) {
  1500. goto end;
  1501. }
  1502. de_ctx->mpm_matcher = MPM_B2G;
  1503. de_ctx->flags |= DE_QUIET;
  1504. s = de_ctx->sig_list = SigInit(de_ctx, "drop tcp any any -> any any (msg:\"ET TROJAN Downadup/Conficker A or B Worm reporting\"; flow:to_server,established; uricontent:\"/search?q=\"; pcre:\"/^\\/search\\?q=[0-9]{1,3}(&aq=7(\\?[0-9a-f]{8})?)?/U\"; pcre:\"/\\x0d\\x0aHost\\: \\d+\\.\\d+\\.\\d+\\.\\d+\\x0d\\x0a/\"; sid:2009024; rev:9;)");
  1505. if (s == NULL) {
  1506. goto end;
  1507. }
  1508. SigGroupBuild(de_ctx);
  1509. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  1510. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf1, http_buf1_len);
  1511. if (r != 0) {
  1512. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  1513. goto end;
  1514. }
  1515. http_state = f.alstate;
  1516. if (http_state == NULL) {
  1517. printf("no http state: ");
  1518. goto end;
  1519. }
  1520. /* do detect */
  1521. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  1522. if (!PacketAlertCheck(p, 2009024)) {
  1523. printf("sig 1 didnt alert with pkt, but it should: ");
  1524. goto end;
  1525. }
  1526. p->alerts.cnt = 0;
  1527. DetectEngineStateReset(f.de_state);
  1528. p->payload = http_buf2;
  1529. p->payload_len = http_buf2_len;
  1530. r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf2, http_buf2_len);
  1531. if (r != 0) {
  1532. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  1533. goto end;
  1534. }
  1535. http_state = f.alstate;
  1536. if (http_state == NULL) {
  1537. printf("no http state: ");
  1538. goto end;
  1539. }
  1540. /* do detect */
  1541. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  1542. if (PacketAlertCheck(p, 2009024)) {
  1543. printf("sig 1 alerted, but it should not (host should not match): ");
  1544. goto end;
  1545. }
  1546. result = 1;
  1547. end:
  1548. if (det_ctx != NULL)
  1549. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  1550. if (de_ctx != NULL)
  1551. SigGroupCleanup(de_ctx);
  1552. if (de_ctx != NULL)
  1553. DetectEngineCtxFree(de_ctx);
  1554. StreamTcpFreeConfig(TRUE);
  1555. FLOW_DESTROY(&f);
  1556. UTHFreePacket(p);
  1557. return result;
  1558. }
  1559. /**
  1560. * \test Test multiple relative contents
  1561. */
  1562. static int UriTestSig17(void)
  1563. {
  1564. int result = 0;
  1565. uint8_t *http_buf = (uint8_t *)"POST /now_this_is_is_big_big_string_now HTTP/1.0\r\n"
  1566. "User-Agent: Mozilla/1.0\r\n";
  1567. uint32_t http_buf_len = strlen((char *)http_buf);
  1568. Flow f;
  1569. TcpSession ssn;
  1570. HtpState *http_state = NULL;
  1571. Packet *p = NULL;
  1572. ThreadVars tv;
  1573. DetectEngineThreadCtx *det_ctx = NULL;
  1574. memset(&tv, 0, sizeof(ThreadVars));
  1575. memset(&f, 0, sizeof(Flow));
  1576. memset(&ssn, 0, sizeof(TcpSession));
  1577. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  1578. FLOW_INITIALIZE(&f);
  1579. f.protoctx = (void *)&ssn;
  1580. f.flags |= FLOW_IPV4;
  1581. p->flow = &f;
  1582. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  1583. p->flowflags |= FLOW_PKT_TOSERVER;
  1584. p->flowflags |= FLOW_PKT_ESTABLISHED;
  1585. f.alproto = ALPROTO_HTTP;
  1586. StreamTcpInitConfig(TRUE);
  1587. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  1588. if (de_ctx == NULL) {
  1589. goto end;
  1590. }
  1591. de_ctx->mpm_matcher = MPM_B2G;
  1592. de_ctx->flags |= DE_QUIET;
  1593. de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  1594. "(msg:\"test multiple relative uricontents\"; "
  1595. "uricontent:\"this\"; uricontent:\"is\"; within:6; "
  1596. "uricontent:\"big\"; within:8; "
  1597. "uricontent:\"string\"; within:8; sid:1;)");
  1598. if (de_ctx->sig_list == NULL) {
  1599. goto end;
  1600. }
  1601. SigGroupBuild(de_ctx);
  1602. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  1603. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  1604. if (r != 0) {
  1605. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  1606. goto end;
  1607. }
  1608. http_state = f.alstate;
  1609. if (http_state == NULL) {
  1610. printf("no http state: ");
  1611. goto end;
  1612. }
  1613. /* do detect */
  1614. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  1615. if (!PacketAlertCheck(p, 1)) {
  1616. printf("sig 1 alerted, but it should not: ");
  1617. goto end;
  1618. }
  1619. result = 1;
  1620. end:
  1621. if (det_ctx != NULL)
  1622. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  1623. if (de_ctx != NULL)
  1624. SigGroupCleanup(de_ctx);
  1625. if (de_ctx != NULL)
  1626. DetectEngineCtxFree(de_ctx);
  1627. StreamTcpFreeConfig(TRUE);
  1628. FLOW_DESTROY(&f);
  1629. UTHFreePacket(p);
  1630. return result;
  1631. }
  1632. /**
  1633. * \test Test multiple relative contents
  1634. */
  1635. static int UriTestSig18(void)
  1636. {
  1637. int result = 0;
  1638. uint8_t *http_buf = (uint8_t *)"POST /now_this_is_is_is_big_big_big_string_now HTTP/1.0\r\n"
  1639. "User-Agent: Mozilla/1.0\r\n";
  1640. uint32_t http_buf_len = strlen((char *)http_buf);
  1641. Flow f;
  1642. TcpSession ssn;
  1643. HtpState *http_state = NULL;
  1644. Packet *p = NULL;
  1645. ThreadVars tv;
  1646. DetectEngineThreadCtx *det_ctx = NULL;
  1647. memset(&tv, 0, sizeof(ThreadVars));
  1648. memset(&f, 0, sizeof(Flow));
  1649. memset(&ssn, 0, sizeof(TcpSession));
  1650. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  1651. FLOW_INITIALIZE(&f);
  1652. f.protoctx = (void *)&ssn;
  1653. f.flags |= FLOW_IPV4;
  1654. p->flow = &f;
  1655. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  1656. p->flowflags |= FLOW_PKT_TOSERVER;
  1657. p->flowflags |= FLOW_PKT_ESTABLISHED;
  1658. f.alproto = ALPROTO_HTTP;
  1659. StreamTcpInitConfig(TRUE);
  1660. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  1661. if (de_ctx == NULL) {
  1662. goto end;
  1663. }
  1664. de_ctx->mpm_matcher = MPM_B2G;
  1665. de_ctx->flags |= DE_QUIET;
  1666. de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  1667. "(msg:\"test multiple relative uricontents\"; "
  1668. "uricontent:\"this\"; uricontent:\"is\"; within:9; "
  1669. "uricontent:\"big\"; within:12; "
  1670. "uricontent:\"string\"; within:8; sid:1;)");
  1671. if (de_ctx->sig_list == NULL) {
  1672. goto end;
  1673. }
  1674. SigGroupBuild(de_ctx);
  1675. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  1676. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  1677. if (r != 0) {
  1678. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  1679. goto end;
  1680. }
  1681. http_state = f.alstate;
  1682. if (http_state == NULL) {
  1683. printf("no http state: ");
  1684. goto end;
  1685. }
  1686. /* do detect */
  1687. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  1688. if (!PacketAlertCheck(p, 1)) {
  1689. printf("sig 1 alerted, but it should not: ");
  1690. goto end;
  1691. }
  1692. result = 1;
  1693. end:
  1694. if (det_ctx != NULL)
  1695. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  1696. if (de_ctx != NULL)
  1697. SigGroupCleanup(de_ctx);
  1698. if (de_ctx != NULL)
  1699. DetectEngineCtxFree(de_ctx);
  1700. StreamTcpFreeConfig(TRUE);
  1701. FLOW_DESTROY(&f);
  1702. UTHFreePacket(p);
  1703. return result;
  1704. }
  1705. /**
  1706. * \test Test multiple relative contents
  1707. */
  1708. static int UriTestSig19(void)
  1709. {
  1710. int result = 0;
  1711. uint8_t *http_buf = (uint8_t *)"POST /this_this_now_is_is_____big_string_now HTTP/1.0\r\n"
  1712. "User-Agent: Mozilla/1.0\r\n";
  1713. uint32_t http_buf_len = strlen((char *)http_buf);
  1714. Flow f;
  1715. TcpSession ssn;
  1716. HtpState *http_state = NULL;
  1717. Packet *p = NULL;
  1718. ThreadVars tv;
  1719. DetectEngineThreadCtx *det_ctx = NULL;
  1720. memset(&tv, 0, sizeof(ThreadVars));
  1721. memset(&f, 0, sizeof(Flow));
  1722. memset(&ssn, 0, sizeof(TcpSession));
  1723. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  1724. FLOW_INITIALIZE(&f);
  1725. f.protoctx = (void *)&ssn;
  1726. f.flags |= FLOW_IPV4;
  1727. p->flow = &f;
  1728. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  1729. p->flowflags |= FLOW_PKT_TOSERVER;
  1730. p->flowflags |= FLOW_PKT_ESTABLISHED;
  1731. f.alproto = ALPROTO_HTTP;
  1732. StreamTcpInitConfig(TRUE);
  1733. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  1734. if (de_ctx == NULL) {
  1735. goto end;
  1736. }
  1737. de_ctx->mpm_matcher = MPM_B2G;
  1738. de_ctx->flags |= DE_QUIET;
  1739. de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  1740. "(msg:\"test multiple relative uricontents\"; "
  1741. "uricontent:\"now\"; uricontent:\"this\"; "
  1742. "uricontent:\"is\"; within:12; "
  1743. "uricontent:\"big\"; within:8; "
  1744. "uricontent:\"string\"; within:8; sid:1;)");
  1745. if (de_ctx->sig_list == NULL) {
  1746. goto end;
  1747. }
  1748. SigGroupBuild(de_ctx);
  1749. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  1750. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  1751. if (r != 0) {
  1752. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  1753. goto end;
  1754. }
  1755. http_state = f.alstate;
  1756. if (http_state == NULL) {
  1757. printf("no http state: ");
  1758. goto end;
  1759. }
  1760. /* do detect */
  1761. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  1762. if (!PacketAlertCheck(p, 1)) {
  1763. printf("sig 1 alerted, but it should not: ");
  1764. goto end;
  1765. }
  1766. result = 1;
  1767. end:
  1768. if (det_ctx != NULL)
  1769. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  1770. if (de_ctx != NULL)
  1771. SigGroupCleanup(de_ctx);
  1772. if (de_ctx != NULL)
  1773. DetectEngineCtxFree(de_ctx);
  1774. StreamTcpFreeConfig(TRUE);
  1775. FLOW_DESTROY(&f);
  1776. UTHFreePacket(p);
  1777. return result;
  1778. }
  1779. /**
  1780. * \test Test multiple relative contents with offset
  1781. */
  1782. static int UriTestSig20(void)
  1783. {
  1784. int result = 0;
  1785. uint8_t *http_buf = (uint8_t *)"POST /_________thus_thus_is_a_big HTTP/1.0\r\n"
  1786. "User-Agent: Mozilla/1.0\r\n";
  1787. uint32_t http_buf_len = strlen((char *)http_buf);
  1788. Flow f;
  1789. TcpSession ssn;
  1790. HtpState *http_state = NULL;
  1791. Packet *p = NULL;
  1792. ThreadVars tv;
  1793. DetectEngineThreadCtx *det_ctx = NULL;
  1794. memset(&tv, 0, sizeof(ThreadVars));
  1795. memset(&f, 0, sizeof(Flow));
  1796. memset(&ssn, 0, sizeof(TcpSession));
  1797. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  1798. FLOW_INITIALIZE(&f);
  1799. f.protoctx = (void *)&ssn;
  1800. f.flags |= FLOW_IPV4;
  1801. p->flow = &f;
  1802. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  1803. p->flowflags |= FLOW_PKT_TOSERVER;
  1804. p->flowflags |= FLOW_PKT_ESTABLISHED;
  1805. f.alproto = ALPROTO_HTTP;
  1806. StreamTcpInitConfig(TRUE);
  1807. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  1808. if (de_ctx == NULL) {
  1809. goto end;
  1810. }
  1811. de_ctx->mpm_matcher = MPM_B2G;
  1812. de_ctx->flags |= DE_QUIET;
  1813. de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  1814. "(msg:\"test multiple relative uricontents\"; "
  1815. "uricontent:\"thus\"; offset:8; "
  1816. "uricontent:\"is\"; within:6; "
  1817. "uricontent:\"big\"; within:8; sid:1;)");
  1818. if (de_ctx->sig_list == NULL) {
  1819. goto end;
  1820. }
  1821. SigGroupBuild(de_ctx);
  1822. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  1823. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  1824. if (r != 0) {
  1825. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  1826. goto end;
  1827. }
  1828. http_state = f.alstate;
  1829. if (http_state == NULL) {
  1830. printf("no http state: ");
  1831. goto end;
  1832. }
  1833. /* do detect */
  1834. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  1835. if (!PacketAlertCheck(p, 1)) {
  1836. printf("sig 1 alerted, but it should not: ");
  1837. goto end;
  1838. }
  1839. result = 1;
  1840. end:
  1841. if (det_ctx != NULL)
  1842. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  1843. if (de_ctx != NULL)
  1844. SigGroupCleanup(de_ctx);
  1845. if (de_ctx != NULL)
  1846. DetectEngineCtxFree(de_ctx);
  1847. StreamTcpFreeConfig(TRUE);
  1848. FLOW_DESTROY(&f);
  1849. UTHFreePacket(p);
  1850. return result;
  1851. }
  1852. /**
  1853. * \test Test multiple relative contents with a negated content.
  1854. */
  1855. static int UriTestSig21(void)
  1856. {
  1857. int result = 0;
  1858. uint8_t *http_buf = (uint8_t *)"POST /we_need_to_fix_this_and_yes_fix_this_now HTTP/1.0\r\n"
  1859. "User-Agent: Mozilla/1.0\r\n";
  1860. uint32_t http_buf_len = strlen((char *)http_buf);
  1861. Flow f;
  1862. TcpSession ssn;
  1863. HtpState *http_state = NULL;
  1864. Packet *p = NULL;
  1865. ThreadVars tv;
  1866. DetectEngineThreadCtx *det_ctx = NULL;
  1867. memset(&tv, 0, sizeof(ThreadVars));
  1868. memset(&f, 0, sizeof(Flow));
  1869. memset(&ssn, 0, sizeof(TcpSession));
  1870. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  1871. FLOW_INITIALIZE(&f);
  1872. f.protoctx = (void *)&ssn;
  1873. f.flags |= FLOW_IPV4;
  1874. p->flow = &f;
  1875. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  1876. p->flowflags |= FLOW_PKT_TOSERVER;
  1877. p->flowflags |= FLOW_PKT_ESTABLISHED;
  1878. f.alproto = ALPROTO_HTTP;
  1879. StreamTcpInitConfig(TRUE);
  1880. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  1881. if (de_ctx == NULL) {
  1882. goto end;
  1883. }
  1884. de_ctx->mpm_matcher = MPM_B2G;
  1885. de_ctx->flags |= DE_QUIET;
  1886. de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  1887. "(msg:\"test multiple relative uricontents\"; "
  1888. "uricontent:\"fix\"; uricontent:\"this\"; within:6; "
  1889. "uricontent:!\"and\"; distance:0; sid:1;)");
  1890. if (de_ctx->sig_list == NULL) {
  1891. goto end;
  1892. }
  1893. SigGroupBuild(de_ctx);
  1894. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  1895. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  1896. if (r != 0) {
  1897. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  1898. goto end;
  1899. }
  1900. http_state = f.alstate;
  1901. if (http_state == NULL) {
  1902. printf("no http state: ");
  1903. goto end;
  1904. }
  1905. /* do detect */
  1906. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  1907. if (PacketAlertCheck(p, 1)) {
  1908. printf("sig 1 alerted, but it should not: ");
  1909. goto end;
  1910. }
  1911. result = 1;
  1912. end:
  1913. if (det_ctx != NULL)
  1914. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  1915. if (de_ctx != NULL)
  1916. SigGroupCleanup(de_ctx);
  1917. if (de_ctx != NULL)
  1918. DetectEngineCtxFree(de_ctx);
  1919. StreamTcpFreeConfig(TRUE);
  1920. FLOW_DESTROY(&f);
  1921. UTHFreePacket(p);
  1922. return result;
  1923. }
  1924. /**
  1925. * \test Test relative pcre.
  1926. */
  1927. static int UriTestSig22(void)
  1928. {
  1929. int result = 0;
  1930. uint8_t *http_buf = (uint8_t *)"POST /this_is_a_super_duper_"
  1931. "nova_in_super_nova_now HTTP/1.0\r\n"
  1932. "User-Agent: Mozilla/1.0\r\n";
  1933. uint32_t http_buf_len = strlen((char *)http_buf);
  1934. Flow f;
  1935. TcpSession ssn;
  1936. HtpState *http_state = NULL;
  1937. Packet *p = NULL;
  1938. ThreadVars tv;
  1939. DetectEngineThreadCtx *det_ctx = NULL;
  1940. memset(&tv, 0, sizeof(ThreadVars));
  1941. memset(&f, 0, sizeof(Flow));
  1942. memset(&ssn, 0, sizeof(TcpSession));
  1943. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  1944. FLOW_INITIALIZE(&f);
  1945. f.protoctx = (void *)&ssn;
  1946. f.flags |= FLOW_IPV4;
  1947. p->flow = &f;
  1948. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  1949. p->flowflags |= FLOW_PKT_TOSERVER;
  1950. p->flowflags |= FLOW_PKT_ESTABLISHED;
  1951. f.alproto = ALPROTO_HTTP;
  1952. StreamTcpInitConfig(TRUE);
  1953. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  1954. if (de_ctx == NULL) {
  1955. goto end;
  1956. }
  1957. de_ctx->mpm_matcher = MPM_B2G;
  1958. de_ctx->flags |= DE_QUIET;
  1959. de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  1960. "(msg:\"test multiple relative uricontents\"; "
  1961. "pcre:/super/U; uricontent:\"nova\"; within:7; sid:1;)");
  1962. if (de_ctx->sig_list == NULL) {
  1963. goto end;
  1964. }
  1965. SigGroupBuild(de_ctx);
  1966. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  1967. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  1968. if (r != 0) {
  1969. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  1970. goto end;
  1971. }
  1972. http_state = f.alstate;
  1973. if (http_state == NULL) {
  1974. printf("no http state: ");
  1975. goto end;
  1976. }
  1977. /* do detect */
  1978. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  1979. if (!PacketAlertCheck(p, 1)) {
  1980. printf("sig 1 didn't alert, but it should have: ");
  1981. goto end;
  1982. }
  1983. result = 1;
  1984. end:
  1985. if (det_ctx != NULL)
  1986. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  1987. if (de_ctx != NULL)
  1988. SigGroupCleanup(de_ctx);
  1989. if (de_ctx != NULL)
  1990. DetectEngineCtxFree(de_ctx);
  1991. StreamTcpFreeConfig(TRUE);
  1992. FLOW_DESTROY(&f);
  1993. UTHFreePacket(p);
  1994. return result;
  1995. }
  1996. /**
  1997. * \test Test multiple relative contents with a negated content.
  1998. */
  1999. static int UriTestSig23(void)
  2000. {
  2001. int result = 0;
  2002. uint8_t *http_buf = (uint8_t *)"POST /we_need_to_fix_this_and_yes_fix_this_now HTTP/1.0\r\n"
  2003. "User-Agent: Mozilla/1.0\r\n";
  2004. uint32_t http_buf_len = strlen((char *)http_buf);
  2005. Flow f;
  2006. TcpSession ssn;
  2007. HtpState *http_state = NULL;
  2008. Packet *p = NULL;
  2009. ThreadVars tv;
  2010. DetectEngineThreadCtx *det_ctx = NULL;
  2011. memset(&tv, 0, sizeof(ThreadVars));
  2012. memset(&f, 0, sizeof(Flow));
  2013. memset(&ssn, 0, sizeof(TcpSession));
  2014. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  2015. FLOW_INITIALIZE(&f);
  2016. f.protoctx = (void *)&ssn;
  2017. f.flags |= FLOW_IPV4;
  2018. p->flow = &f;
  2019. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  2020. p->flowflags |= FLOW_PKT_TOSERVER;
  2021. p->flowflags |= FLOW_PKT_ESTABLISHED;
  2022. f.alproto = ALPROTO_HTTP;
  2023. StreamTcpInitConfig(TRUE);
  2024. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  2025. if (de_ctx == NULL) {
  2026. goto end;
  2027. }
  2028. de_ctx->mpm_matcher = MPM_B2G;
  2029. de_ctx->flags |= DE_QUIET;
  2030. de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  2031. "(msg:\"test multiple relative uricontents\"; "
  2032. "uricontent:!\"fix_this_now\"; sid:1;)");
  2033. if (de_ctx->sig_list == NULL) {
  2034. goto end;
  2035. }
  2036. SigGroupBuild(de_ctx);
  2037. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  2038. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  2039. if (r != 0) {
  2040. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  2041. goto end;
  2042. }
  2043. http_state = f.alstate;
  2044. if (http_state == NULL) {
  2045. printf("no http state: ");
  2046. goto end;
  2047. }
  2048. /* do detect */
  2049. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  2050. if (PacketAlertCheck(p, 1)) {
  2051. printf("sig 1 alerted, but it should not: ");
  2052. goto end;
  2053. }
  2054. result = 1;
  2055. end:
  2056. if (det_ctx != NULL)
  2057. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  2058. if (de_ctx != NULL)
  2059. SigGroupCleanup(de_ctx);
  2060. if (de_ctx != NULL)
  2061. DetectEngineCtxFree(de_ctx);
  2062. StreamTcpFreeConfig(TRUE);
  2063. FLOW_DESTROY(&f);
  2064. UTHFreePacket(p);
  2065. return result;
  2066. }
  2067. /**
  2068. * \test Test multiple relative contents with a negated content.
  2069. */
  2070. static int UriTestSig24(void)
  2071. {
  2072. int result = 0;
  2073. uint8_t *http_buf = (uint8_t *)"POST /we_need_to_fix_this_and_yes_fix_this_now HTTP/1.0\r\n"
  2074. "User-Agent: Mozilla/1.0\r\n";
  2075. uint32_t http_buf_len = strlen((char *)http_buf);
  2076. Flow f;
  2077. TcpSession ssn;
  2078. HtpState *http_state = NULL;
  2079. Packet *p = NULL;
  2080. ThreadVars tv;
  2081. DetectEngineThreadCtx *det_ctx = NULL;
  2082. memset(&tv, 0, sizeof(ThreadVars));
  2083. memset(&f, 0, sizeof(Flow));
  2084. memset(&ssn, 0, sizeof(TcpSession));
  2085. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  2086. FLOW_INITIALIZE(&f);
  2087. f.protoctx = (void *)&ssn;
  2088. f.flags |= FLOW_IPV4;
  2089. p->flow = &f;
  2090. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  2091. p->flowflags |= FLOW_PKT_TOSERVER;
  2092. p->flowflags |= FLOW_PKT_ESTABLISHED;
  2093. f.alproto = ALPROTO_HTTP;
  2094. StreamTcpInitConfig(TRUE);
  2095. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  2096. if (de_ctx == NULL) {
  2097. goto end;
  2098. }
  2099. de_ctx->mpm_matcher = MPM_B2G;
  2100. de_ctx->flags |= DE_QUIET;
  2101. de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  2102. "(msg:\"test multiple relative uricontents\"; "
  2103. "uricontent:\"we_need_to\"; uricontent:!\"fix_this_now\"; sid:1;)");
  2104. if (de_ctx->sig_list == NULL) {
  2105. goto end;
  2106. }
  2107. SigGroupBuild(de_ctx);
  2108. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  2109. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  2110. if (r != 0) {
  2111. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  2112. goto end;
  2113. }
  2114. http_state = f.alstate;
  2115. if (http_state == NULL) {
  2116. printf("no http state: ");
  2117. goto end;
  2118. }
  2119. /* do detect */
  2120. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  2121. if (PacketAlertCheck(p, 1)) {
  2122. printf("sig 1 alerted, but it should not: ");
  2123. goto end;
  2124. }
  2125. result = 1;
  2126. end:
  2127. if (det_ctx != NULL)
  2128. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  2129. if (de_ctx != NULL)
  2130. SigGroupCleanup(de_ctx);
  2131. if (de_ctx != NULL)
  2132. DetectEngineCtxFree(de_ctx);
  2133. StreamTcpFreeConfig(TRUE);
  2134. FLOW_DESTROY(&f);
  2135. UTHFreePacket(p);
  2136. return result;
  2137. }
  2138. /**
  2139. * \test Test normalized uricontents.
  2140. */
  2141. static int UriTestSig25(void)
  2142. {
  2143. int result = 0;
  2144. uint8_t *http_buf = (uint8_t *)"POST /normalized%20uri "
  2145. "HTTP/1.0\r\nUser-Agent: Mozilla/1.0\r\n";
  2146. uint32_t http_buf_len = strlen((char *)http_buf);
  2147. Flow f;
  2148. TcpSession ssn;
  2149. HtpState *http_state = NULL;
  2150. Packet *p = NULL;
  2151. ThreadVars tv;
  2152. DetectEngineThreadCtx *det_ctx = NULL;
  2153. memset(&tv, 0, sizeof(ThreadVars));
  2154. memset(&f, 0, sizeof(Flow));
  2155. memset(&ssn, 0, sizeof(TcpSession));
  2156. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  2157. FLOW_INITIALIZE(&f);
  2158. f.protoctx = (void *)&ssn;
  2159. f.flags |= FLOW_IPV4;
  2160. p->flow = &f;
  2161. p->flowflags |= FLOW_PKT_TOSERVER;
  2162. p->flowflags |= FLOW_PKT_ESTABLISHED;
  2163. f.alproto = ALPROTO_HTTP;
  2164. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  2165. StreamTcpInitConfig(TRUE);
  2166. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  2167. if (de_ctx == NULL) {
  2168. goto end;
  2169. }
  2170. de_ctx->mpm_matcher = MPM_B2G;
  2171. de_ctx->flags |= DE_QUIET;
  2172. de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  2173. "(msg:\"test multiple relative uricontents\"; "
  2174. "pcre:/normalized/U; uricontent:\"normalized uri\"; sid:1;)");
  2175. if (de_ctx->sig_list == NULL) {
  2176. goto end;
  2177. }
  2178. SigGroupBuild(de_ctx);
  2179. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  2180. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  2181. if (r != 0) {
  2182. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  2183. goto end;
  2184. }
  2185. http_state = f.alstate;
  2186. if (http_state == NULL) {
  2187. printf("no http state: ");
  2188. goto end;
  2189. }
  2190. /* do detect */
  2191. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  2192. if (!PacketAlertCheck(p, 1)) {
  2193. printf("sig 1 didn't alert, but it should have: ");
  2194. goto end;
  2195. }
  2196. result = 1;
  2197. end:
  2198. if (det_ctx != NULL)
  2199. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  2200. if (de_ctx != NULL)
  2201. SigGroupCleanup(de_ctx);
  2202. if (de_ctx != NULL)
  2203. DetectEngineCtxFree(de_ctx);
  2204. StreamTcpFreeConfig(TRUE);
  2205. FLOW_DESTROY(&f);
  2206. UTHFreePacket(p);
  2207. return result;
  2208. }
  2209. /**
  2210. * \test Test multiple relative contents with a negated content.
  2211. */
  2212. static int UriTestSig26(void)
  2213. {
  2214. int result = 0;
  2215. uint8_t *http_buf = (uint8_t *)"POST /we_need_to_fix_this_and_yes_fix_this_now HTTP/1.0\r\n"
  2216. "User-Agent: Mozilla/1.0\r\n";
  2217. uint32_t http_buf_len = strlen((char *)http_buf);
  2218. Flow f;
  2219. TcpSession ssn;
  2220. HtpState *http_state = NULL;
  2221. Packet *p = NULL;
  2222. ThreadVars tv;
  2223. DetectEngineThreadCtx *det_ctx = NULL;
  2224. memset(&tv, 0, sizeof(ThreadVars));
  2225. memset(&f, 0, sizeof(Flow));
  2226. memset(&ssn, 0, sizeof(TcpSession));
  2227. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  2228. FLOW_INITIALIZE(&f);
  2229. f.protoctx = (void *)&ssn;
  2230. f.flags |= FLOW_IPV4;
  2231. p->flow = &f;
  2232. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  2233. p->flowflags |= FLOW_PKT_TOSERVER;
  2234. p->flowflags |= FLOW_PKT_ESTABLISHED;
  2235. f.alproto = ALPROTO_HTTP;
  2236. StreamTcpInitConfig(TRUE);
  2237. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  2238. if (de_ctx == NULL) {
  2239. goto end;
  2240. }
  2241. de_ctx->mpm_matcher = MPM_B2G;
  2242. de_ctx->flags |= DE_QUIET;
  2243. de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  2244. "(msg:\"test multiple relative uricontents\"; "
  2245. "uricontent:\"fix_this\"; isdataat:4,relative; sid:1;)");
  2246. if (de_ctx->sig_list == NULL) {
  2247. goto end;
  2248. }
  2249. SigGroupBuild(de_ctx);
  2250. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  2251. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  2252. if (r != 0) {
  2253. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  2254. goto end;
  2255. }
  2256. http_state = f.alstate;
  2257. if (http_state == NULL) {
  2258. printf("no http state: ");
  2259. goto end;
  2260. }
  2261. /* do detect */
  2262. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  2263. if (!PacketAlertCheck(p, 1)) {
  2264. printf("sig 1 didn't alert, but it should have: ");
  2265. goto end;
  2266. }
  2267. result = 1;
  2268. end:
  2269. if (det_ctx != NULL)
  2270. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  2271. if (de_ctx != NULL)
  2272. SigGroupCleanup(de_ctx);
  2273. if (de_ctx != NULL)
  2274. DetectEngineCtxFree(de_ctx);
  2275. StreamTcpFreeConfig(TRUE);
  2276. FLOW_DESTROY(&f);
  2277. UTHFreePacket(p);
  2278. return result;
  2279. }
  2280. /**
  2281. * \test Test multiple relative contents with a negated content.
  2282. */
  2283. static int UriTestSig27(void)
  2284. {
  2285. int result = 0;
  2286. uint8_t *http_buf = (uint8_t *)"POST /we_need_to_fix_this_and_yes_fix_this_now HTTP/1.0\r\n"
  2287. "User-Agent: Mozilla/1.0\r\n";
  2288. uint32_t http_buf_len = strlen((char *)http_buf);
  2289. Flow f;
  2290. TcpSession ssn;
  2291. HtpState *http_state = NULL;
  2292. Packet *p = NULL;
  2293. ThreadVars tv;
  2294. DetectEngineThreadCtx *det_ctx = NULL;
  2295. memset(&tv, 0, sizeof(ThreadVars));
  2296. memset(&f, 0, sizeof(Flow));
  2297. memset(&ssn, 0, sizeof(TcpSession));
  2298. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  2299. FLOW_INITIALIZE(&f);
  2300. f.protoctx = (void *)&ssn;
  2301. f.flags |= FLOW_IPV4;
  2302. p->flow = &f;
  2303. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  2304. p->flowflags |= FLOW_PKT_TOSERVER;
  2305. p->flowflags |= FLOW_PKT_ESTABLISHED;
  2306. f.alproto = ALPROTO_HTTP;
  2307. StreamTcpInitConfig(TRUE);
  2308. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  2309. if (de_ctx == NULL) {
  2310. goto end;
  2311. }
  2312. de_ctx->mpm_matcher = MPM_B2G;
  2313. de_ctx->flags |= DE_QUIET;
  2314. de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  2315. "(msg:\"test multiple relative uricontents\"; "
  2316. "uricontent:\"fix_this\"; isdataat:!10,relative; sid:1;)");
  2317. if (de_ctx->sig_list == NULL) {
  2318. goto end;
  2319. }
  2320. SigGroupBuild(de_ctx);
  2321. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  2322. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  2323. if (r != 0) {
  2324. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  2325. goto end;
  2326. }
  2327. http_state = f.alstate;
  2328. if (http_state == NULL) {
  2329. printf("no http state: ");
  2330. goto end;
  2331. }
  2332. /* do detect */
  2333. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  2334. if (!PacketAlertCheck(p, 1)) {
  2335. printf("sig 1 didn't alert, but it should have: ");
  2336. goto end;
  2337. }
  2338. result = 1;
  2339. end:
  2340. if (det_ctx != NULL)
  2341. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  2342. if (de_ctx != NULL)
  2343. SigGroupCleanup(de_ctx);
  2344. if (de_ctx != NULL)
  2345. DetectEngineCtxFree(de_ctx);
  2346. StreamTcpFreeConfig(TRUE);
  2347. FLOW_DESTROY(&f);
  2348. UTHFreePacket(p);
  2349. return result;
  2350. }
  2351. static int UriTestSig28(void)
  2352. {
  2353. int result = 0;
  2354. uint8_t *http_buf = (uint8_t *)"POST /this_b5ig_string_now_in_http HTTP/1.0\r\n"
  2355. "User-Agent: Mozilla/1.0\r\n";
  2356. uint32_t http_buf_len = strlen((char *)http_buf);
  2357. Flow f;
  2358. TcpSession ssn;
  2359. HtpState *http_state = NULL;
  2360. Packet *p = NULL;
  2361. ThreadVars tv;
  2362. DetectEngineThreadCtx *det_ctx = NULL;
  2363. memset(&tv, 0, sizeof(ThreadVars));
  2364. memset(&f, 0, sizeof(Flow));
  2365. memset(&ssn, 0, sizeof(TcpSession));
  2366. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  2367. FLOW_INITIALIZE(&f);
  2368. f.protoctx = (void *)&ssn;
  2369. f.flags |= FLOW_IPV4;
  2370. p->flow = &f;
  2371. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  2372. p->flowflags |= FLOW_PKT_TOSERVER;
  2373. p->flowflags |= FLOW_PKT_ESTABLISHED;
  2374. f.alproto = ALPROTO_HTTP;
  2375. StreamTcpInitConfig(TRUE);
  2376. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  2377. if (de_ctx == NULL) {
  2378. goto end;
  2379. }
  2380. de_ctx->mpm_matcher = MPM_B2G;
  2381. de_ctx->flags |= DE_QUIET;
  2382. de_ctx->sig_list = SigInit(de_ctx,
  2383. "alert tcp any any -> any any (msg:\"dummy\"; "
  2384. "uricontent:\"this\"; "
  2385. "byte_extract:1,2,one,string,dec,relative; "
  2386. "uricontent:\"ring\"; distance:one; sid:1;)");
  2387. if (de_ctx->sig_list == NULL) {
  2388. goto end;
  2389. }
  2390. SigGroupBuild(de_ctx);
  2391. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  2392. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  2393. if (r != 0) {
  2394. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  2395. goto end;
  2396. }
  2397. http_state = f.alstate;
  2398. if (http_state == NULL) {
  2399. printf("no http state: ");
  2400. goto end;
  2401. }
  2402. /* do detect */
  2403. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  2404. if (!PacketAlertCheck(p, 1)) {
  2405. printf("sig 1 didn't alert, but should have: ");
  2406. goto end;
  2407. }
  2408. result = 1;
  2409. end:
  2410. if (det_ctx != NULL)
  2411. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  2412. if (de_ctx != NULL)
  2413. SigGroupCleanup(de_ctx);
  2414. if (de_ctx != NULL)
  2415. DetectEngineCtxFree(de_ctx);
  2416. StreamTcpFreeConfig(TRUE);
  2417. FLOW_DESTROY(&f);
  2418. UTHFreePacket(p);
  2419. return result;
  2420. }
  2421. static int UriTestSig29(void)
  2422. {
  2423. int result = 0;
  2424. uint8_t *http_buf = (uint8_t *)"POST /this_b5ig_string_now_in_http HTTP/1.0\r\n"
  2425. "User-Agent: Mozilla/1.0\r\n";
  2426. uint32_t http_buf_len = strlen((char *)http_buf);
  2427. Flow f;
  2428. TcpSession ssn;
  2429. HtpState *http_state = NULL;
  2430. Packet *p = NULL;
  2431. ThreadVars tv;
  2432. DetectEngineThreadCtx *det_ctx = NULL;
  2433. memset(&tv, 0, sizeof(ThreadVars));
  2434. memset(&f, 0, sizeof(Flow));
  2435. memset(&ssn, 0, sizeof(TcpSession));
  2436. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  2437. FLOW_INITIALIZE(&f);
  2438. f.protoctx = (void *)&ssn;
  2439. f.flags |= FLOW_IPV4;
  2440. p->flow = &f;
  2441. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  2442. p->flowflags |= FLOW_PKT_TOSERVER;
  2443. p->flowflags |= FLOW_PKT_ESTABLISHED;
  2444. f.alproto = ALPROTO_HTTP;
  2445. StreamTcpInitConfig(TRUE);
  2446. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  2447. if (de_ctx == NULL) {
  2448. goto end;
  2449. }
  2450. de_ctx->mpm_matcher = MPM_B2G;
  2451. de_ctx->flags |= DE_QUIET;
  2452. de_ctx->sig_list = SigInit(de_ctx,
  2453. "alert tcp any any -> any any (msg:\"dummy\"; "
  2454. "uricontent:\"this\"; "
  2455. "byte_extract:1,2,one,string,dec,relative; "
  2456. "uricontent:\"ring\"; distance:one; sid:1;)");
  2457. if (de_ctx->sig_list == NULL) {
  2458. goto end;
  2459. }
  2460. SigGroupBuild(de_ctx);
  2461. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  2462. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  2463. if (r != 0) {
  2464. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  2465. goto end;
  2466. }
  2467. http_state = f.alstate;
  2468. if (http_state == NULL) {
  2469. printf("no http state: ");
  2470. goto end;
  2471. }
  2472. /* do detect */
  2473. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  2474. if (!PacketAlertCheck(p, 1)) {
  2475. printf("sig 1 didn't alert, but should have: ");
  2476. goto end;
  2477. }
  2478. result = 1;
  2479. end:
  2480. if (det_ctx != NULL)
  2481. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  2482. if (de_ctx != NULL)
  2483. SigGroupCleanup(de_ctx);
  2484. if (de_ctx != NULL)
  2485. DetectEngineCtxFree(de_ctx);
  2486. StreamTcpFreeConfig(TRUE);
  2487. FLOW_DESTROY(&f);
  2488. UTHFreePacket(p);
  2489. return result;
  2490. }
  2491. static int UriTestSig30(void)
  2492. {
  2493. int result = 0;
  2494. uint8_t *http_buf = (uint8_t *)"POST /this_b5ig_string_now_in_http HTTP/1.0\r\n"
  2495. "User-Agent: Mozilla/1.0\r\n";
  2496. uint32_t http_buf_len = strlen((char *)http_buf);
  2497. Flow f;
  2498. TcpSession ssn;
  2499. HtpState *http_state = NULL;
  2500. Packet *p = NULL;
  2501. ThreadVars tv;
  2502. DetectEngineThreadCtx *det_ctx = NULL;
  2503. memset(&tv, 0, sizeof(ThreadVars));
  2504. memset(&f, 0, sizeof(Flow));
  2505. memset(&ssn, 0, sizeof(TcpSession));
  2506. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  2507. FLOW_INITIALIZE(&f);
  2508. f.protoctx = (void *)&ssn;
  2509. f.flags |= FLOW_IPV4;
  2510. p->flow = &f;
  2511. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  2512. p->flowflags |= FLOW_PKT_TOSERVER;
  2513. p->flowflags |= FLOW_PKT_ESTABLISHED;
  2514. f.alproto = ALPROTO_HTTP;
  2515. StreamTcpInitConfig(TRUE);
  2516. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  2517. if (de_ctx == NULL) {
  2518. goto end;
  2519. }
  2520. de_ctx->mpm_matcher = MPM_B2G;
  2521. de_ctx->flags |= DE_QUIET;
  2522. de_ctx->sig_list = SigInit(de_ctx,
  2523. "alert tcp any any -> any any (msg:\"dummy\"; "
  2524. "uricontent:\"this\"; "
  2525. "byte_extract:1,2,one,string,dec,relative; "
  2526. "uricontent:\"_b5ig\"; offset:one; sid:1;)");
  2527. if (de_ctx->sig_list == NULL) {
  2528. goto end;
  2529. }
  2530. SigGroupBuild(de_ctx);
  2531. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  2532. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  2533. if (r != 0) {
  2534. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  2535. goto end;
  2536. }
  2537. http_state = f.alstate;
  2538. if (http_state == NULL) {
  2539. printf("no http state: ");
  2540. goto end;
  2541. }
  2542. /* do detect */
  2543. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  2544. if (!PacketAlertCheck(p, 1)) {
  2545. printf("sig 1 didn't alert, but should have: ");
  2546. goto end;
  2547. }
  2548. result = 1;
  2549. end:
  2550. if (det_ctx != NULL)
  2551. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  2552. if (de_ctx != NULL)
  2553. SigGroupCleanup(de_ctx);
  2554. if (de_ctx != NULL)
  2555. DetectEngineCtxFree(de_ctx);
  2556. StreamTcpFreeConfig(TRUE);
  2557. FLOW_DESTROY(&f);
  2558. UTHFreePacket(p);
  2559. return result;
  2560. }
  2561. static int UriTestSig31(void)
  2562. {
  2563. int result = 0;
  2564. uint8_t *http_buf = (uint8_t *)"POST /this_b5ig_string_now_in_http HTTP/1.0\r\n"
  2565. "User-Agent: Mozilla/1.0\r\n";
  2566. uint32_t http_buf_len = strlen((char *)http_buf);
  2567. Flow f;
  2568. TcpSession ssn;
  2569. HtpState *http_state = NULL;
  2570. Packet *p = NULL;
  2571. ThreadVars tv;
  2572. DetectEngineThreadCtx *det_ctx = NULL;
  2573. memset(&tv, 0, sizeof(ThreadVars));
  2574. memset(&f, 0, sizeof(Flow));
  2575. memset(&ssn, 0, sizeof(TcpSession));
  2576. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  2577. FLOW_INITIALIZE(&f);
  2578. f.protoctx = (void *)&ssn;
  2579. f.flags |= FLOW_IPV4;
  2580. p->flow = &f;
  2581. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  2582. p->flowflags |= FLOW_PKT_TOSERVER;
  2583. p->flowflags |= FLOW_PKT_ESTABLISHED;
  2584. f.alproto = ALPROTO_HTTP;
  2585. StreamTcpInitConfig(TRUE);
  2586. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  2587. if (de_ctx == NULL) {
  2588. goto end;
  2589. }
  2590. de_ctx->mpm_matcher = MPM_B2G;
  2591. de_ctx->flags |= DE_QUIET;
  2592. de_ctx->sig_list = SigInit(de_ctx,
  2593. "alert tcp any any -> any any (msg:\"dummy\"; "
  2594. "uricontent:\"this\"; "
  2595. "byte_extract:1,2,one,string,dec,relative; "
  2596. "uricontent:\"his\"; depth:one; sid:1;)");
  2597. if (de_ctx->sig_list == NULL) {
  2598. goto end;
  2599. }
  2600. SigGroupBuild(de_ctx);
  2601. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  2602. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  2603. if (r != 0) {
  2604. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  2605. goto end;
  2606. }
  2607. http_state = f.alstate;
  2608. if (http_state == NULL) {
  2609. printf("no http state: ");
  2610. goto end;
  2611. }
  2612. /* do detect */
  2613. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  2614. if (!PacketAlertCheck(p, 1)) {
  2615. printf("sig 1 didn't alert, but should have: ");
  2616. goto end;
  2617. }
  2618. result = 1;
  2619. end:
  2620. if (det_ctx != NULL)
  2621. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  2622. if (de_ctx != NULL)
  2623. SigGroupCleanup(de_ctx);
  2624. if (de_ctx != NULL)
  2625. DetectEngineCtxFree(de_ctx);
  2626. StreamTcpFreeConfig(TRUE);
  2627. FLOW_DESTROY(&f);
  2628. UTHFreePacket(p);
  2629. return result;
  2630. }
  2631. static int UriTestSig32(void)
  2632. {
  2633. int result = 0;
  2634. uint8_t *http_buf = (uint8_t *)"POST /this_b5ig_string_now_in_http HTTP/1.0\r\n"
  2635. "User-Agent: Mozilla/1.0\r\n";
  2636. uint32_t http_buf_len = strlen((char *)http_buf);
  2637. Flow f;
  2638. TcpSession ssn;
  2639. HtpState *http_state = NULL;
  2640. Packet *p = NULL;
  2641. ThreadVars tv;
  2642. DetectEngineThreadCtx *det_ctx = NULL;
  2643. memset(&tv, 0, sizeof(ThreadVars));
  2644. memset(&f, 0, sizeof(Flow));
  2645. memset(&ssn, 0, sizeof(TcpSession));
  2646. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  2647. FLOW_INITIALIZE(&f);
  2648. f.protoctx = (void *)&ssn;
  2649. f.flags |= FLOW_IPV4;
  2650. p->flow = &f;
  2651. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  2652. p->flowflags |= FLOW_PKT_TOSERVER;
  2653. p->flowflags |= FLOW_PKT_ESTABLISHED;
  2654. f.alproto = ALPROTO_HTTP;
  2655. StreamTcpInitConfig(TRUE);
  2656. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  2657. if (de_ctx == NULL) {
  2658. goto end;
  2659. }
  2660. de_ctx->mpm_matcher = MPM_B2G;
  2661. de_ctx->flags |= DE_QUIET;
  2662. de_ctx->sig_list = SigInit(de_ctx,
  2663. "alert tcp any any -> any any (msg:\"dummy\"; "
  2664. "uricontent:\"this\"; "
  2665. "byte_extract:1,2,one,string,dec,relative; "
  2666. "uricontent:\"g_st\"; within:one; sid:1;)");
  2667. if (de_ctx->sig_list == NULL) {
  2668. goto end;
  2669. }
  2670. SigGroupBuild(de_ctx);
  2671. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  2672. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  2673. if (r != 0) {
  2674. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  2675. goto end;
  2676. }
  2677. http_state = f.alstate;
  2678. if (http_state == NULL) {
  2679. printf("no http state: ");
  2680. goto end;
  2681. }
  2682. /* do detect */
  2683. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  2684. if (!PacketAlertCheck(p, 1)) {
  2685. printf("sig 1 didn't alert, but should have: ");
  2686. goto end;
  2687. }
  2688. result = 1;
  2689. end:
  2690. if (det_ctx != NULL)
  2691. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  2692. if (de_ctx != NULL)
  2693. SigGroupCleanup(de_ctx);
  2694. if (de_ctx != NULL)
  2695. DetectEngineCtxFree(de_ctx);
  2696. StreamTcpFreeConfig(TRUE);
  2697. FLOW_DESTROY(&f);
  2698. UTHFreePacket(p);
  2699. return result;
  2700. }
  2701. static int UriTestSig33(void)
  2702. {
  2703. int result = 0;
  2704. uint8_t *http_buf = (uint8_t *)"POST /normalized%20uri "
  2705. "HTTP/1.0\r\nUser-Agent: Mozilla/1.0\r\n";
  2706. uint32_t http_buf_len = strlen((char *)http_buf);
  2707. Flow f;
  2708. TcpSession ssn;
  2709. HtpState *http_state = NULL;
  2710. Packet *p = NULL;
  2711. ThreadVars tv;
  2712. DetectEngineThreadCtx *det_ctx = NULL;
  2713. memset(&tv, 0, sizeof(ThreadVars));
  2714. memset(&f, 0, sizeof(Flow));
  2715. memset(&ssn, 0, sizeof(TcpSession));
  2716. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  2717. FLOW_INITIALIZE(&f);
  2718. f.protoctx = (void *)&ssn;
  2719. f.flags |= FLOW_IPV4;
  2720. p->flow = &f;
  2721. p->flowflags |= FLOW_PKT_TOSERVER;
  2722. p->flowflags |= FLOW_PKT_ESTABLISHED;
  2723. f.alproto = ALPROTO_HTTP;
  2724. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  2725. StreamTcpInitConfig(TRUE);
  2726. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  2727. if (de_ctx == NULL) {
  2728. goto end;
  2729. }
  2730. de_ctx->mpm_matcher = MPM_B2G;
  2731. de_ctx->flags |= DE_QUIET;
  2732. de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  2733. "(msg:\"test multiple relative uricontents\"; "
  2734. "urilen:15; sid:1;)");
  2735. if (de_ctx->sig_list == NULL) {
  2736. goto end;
  2737. }
  2738. SigGroupBuild(de_ctx);
  2739. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  2740. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  2741. if (r != 0) {
  2742. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  2743. goto end;
  2744. }
  2745. http_state = f.alstate;
  2746. if (http_state == NULL) {
  2747. printf("no http state: ");
  2748. goto end;
  2749. }
  2750. /* do detect */
  2751. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  2752. if (!PacketAlertCheck(p, 1)) {
  2753. printf("sig 1 didn't alert, but it should have: ");
  2754. goto end;
  2755. }
  2756. result = 1;
  2757. end:
  2758. if (det_ctx != NULL)
  2759. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  2760. if (de_ctx != NULL)
  2761. SigGroupCleanup(de_ctx);
  2762. if (de_ctx != NULL)
  2763. DetectEngineCtxFree(de_ctx);
  2764. StreamTcpFreeConfig(TRUE);
  2765. FLOW_DESTROY(&f);
  2766. UTHFreePacket(p);
  2767. return result;
  2768. }
  2769. static int UriTestSig34(void)
  2770. {
  2771. int result = 0;
  2772. uint8_t *http_buf = (uint8_t *)"POST /normalized%20uri "
  2773. "HTTP/1.0\r\nUser-Agent: Mozilla/1.0\r\n";
  2774. uint32_t http_buf_len = strlen((char *)http_buf);
  2775. Flow f;
  2776. TcpSession ssn;
  2777. HtpState *http_state = NULL;
  2778. Packet *p = NULL;
  2779. ThreadVars tv;
  2780. DetectEngineThreadCtx *det_ctx = NULL;
  2781. memset(&tv, 0, sizeof(ThreadVars));
  2782. memset(&f, 0, sizeof(Flow));
  2783. memset(&ssn, 0, sizeof(TcpSession));
  2784. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  2785. FLOW_INITIALIZE(&f);
  2786. f.protoctx = (void *)&ssn;
  2787. f.flags |= FLOW_IPV4;
  2788. p->flow = &f;
  2789. p->flowflags |= FLOW_PKT_TOSERVER;
  2790. p->flowflags |= FLOW_PKT_ESTABLISHED;
  2791. f.alproto = ALPROTO_HTTP;
  2792. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  2793. StreamTcpInitConfig(TRUE);
  2794. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  2795. if (de_ctx == NULL) {
  2796. goto end;
  2797. }
  2798. de_ctx->mpm_matcher = MPM_B2G;
  2799. de_ctx->flags |= DE_QUIET;
  2800. de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  2801. "(msg:\"test multiple relative uricontents\"; "
  2802. "urilen:15, norm; sid:1;)");
  2803. if (de_ctx->sig_list == NULL) {
  2804. goto end;
  2805. }
  2806. SigGroupBuild(de_ctx);
  2807. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  2808. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  2809. if (r != 0) {
  2810. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  2811. goto end;
  2812. }
  2813. http_state = f.alstate;
  2814. if (http_state == NULL) {
  2815. printf("no http state: ");
  2816. goto end;
  2817. }
  2818. /* do detect */
  2819. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  2820. if (!PacketAlertCheck(p, 1)) {
  2821. printf("sig 1 didn't alert, but it should have: ");
  2822. goto end;
  2823. }
  2824. result = 1;
  2825. end:
  2826. if (det_ctx != NULL)
  2827. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  2828. if (de_ctx != NULL)
  2829. SigGroupCleanup(de_ctx);
  2830. if (de_ctx != NULL)
  2831. DetectEngineCtxFree(de_ctx);
  2832. StreamTcpFreeConfig(TRUE);
  2833. FLOW_DESTROY(&f);
  2834. UTHFreePacket(p);
  2835. return result;
  2836. }
  2837. static int UriTestSig35(void)
  2838. {
  2839. int result = 0;
  2840. uint8_t *http_buf = (uint8_t *)"POST /normalized%20uri "
  2841. "HTTP/1.0\r\nUser-Agent: Mozilla/1.0\r\n";
  2842. uint32_t http_buf_len = strlen((char *)http_buf);
  2843. Flow f;
  2844. TcpSession ssn;
  2845. HtpState *http_state = NULL;
  2846. Packet *p = NULL;
  2847. ThreadVars tv;
  2848. DetectEngineThreadCtx *det_ctx = NULL;
  2849. memset(&tv, 0, sizeof(ThreadVars));
  2850. memset(&f, 0, sizeof(Flow));
  2851. memset(&ssn, 0, sizeof(TcpSession));
  2852. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  2853. FLOW_INITIALIZE(&f);
  2854. f.protoctx = (void *)&ssn;
  2855. f.flags |= FLOW_IPV4;
  2856. p->flow = &f;
  2857. p->flowflags |= FLOW_PKT_TOSERVER;
  2858. p->flowflags |= FLOW_PKT_ESTABLISHED;
  2859. f.alproto = ALPROTO_HTTP;
  2860. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  2861. StreamTcpInitConfig(TRUE);
  2862. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  2863. if (de_ctx == NULL) {
  2864. goto end;
  2865. }
  2866. de_ctx->mpm_matcher = MPM_B2G;
  2867. de_ctx->flags |= DE_QUIET;
  2868. de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  2869. "(msg:\"test multiple relative uricontents\"; "
  2870. "urilen:16; sid:1;)");
  2871. if (de_ctx->sig_list == NULL) {
  2872. goto end;
  2873. }
  2874. SigGroupBuild(de_ctx);
  2875. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  2876. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  2877. if (r != 0) {
  2878. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  2879. goto end;
  2880. }
  2881. http_state = f.alstate;
  2882. if (http_state == NULL) {
  2883. printf("no http state: ");
  2884. goto end;
  2885. }
  2886. /* do detect */
  2887. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  2888. if (PacketAlertCheck(p, 1)) {
  2889. printf("sig 1 alerted, but it shouldn't have: ");
  2890. goto end;
  2891. }
  2892. result = 1;
  2893. end:
  2894. if (det_ctx != NULL)
  2895. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  2896. if (de_ctx != NULL)
  2897. SigGroupCleanup(de_ctx);
  2898. if (de_ctx != NULL)
  2899. DetectEngineCtxFree(de_ctx);
  2900. StreamTcpFreeConfig(TRUE);
  2901. FLOW_DESTROY(&f);
  2902. UTHFreePacket(p);
  2903. return result;
  2904. }
  2905. static int UriTestSig36(void)
  2906. {
  2907. int result = 0;
  2908. uint8_t *http_buf = (uint8_t *)"POST /normalized%20uri "
  2909. "HTTP/1.0\r\nUser-Agent: Mozilla/1.0\r\n";
  2910. uint32_t http_buf_len = strlen((char *)http_buf);
  2911. Flow f;
  2912. TcpSession ssn;
  2913. HtpState *http_state = NULL;
  2914. Packet *p = NULL;
  2915. ThreadVars tv;
  2916. DetectEngineThreadCtx *det_ctx = NULL;
  2917. memset(&tv, 0, sizeof(ThreadVars));
  2918. memset(&f, 0, sizeof(Flow));
  2919. memset(&ssn, 0, sizeof(TcpSession));
  2920. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  2921. FLOW_INITIALIZE(&f);
  2922. f.protoctx = (void *)&ssn;
  2923. f.flags |= FLOW_IPV4;
  2924. p->flow = &f;
  2925. p->flowflags |= FLOW_PKT_TOSERVER;
  2926. p->flowflags |= FLOW_PKT_ESTABLISHED;
  2927. f.alproto = ALPROTO_HTTP;
  2928. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  2929. StreamTcpInitConfig(TRUE);
  2930. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  2931. if (de_ctx == NULL) {
  2932. goto end;
  2933. }
  2934. de_ctx->mpm_matcher = MPM_B2G;
  2935. de_ctx->flags |= DE_QUIET;
  2936. de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  2937. "(msg:\"test multiple relative uricontents\"; "
  2938. "urilen:16, norm; sid:1;)");
  2939. if (de_ctx->sig_list == NULL) {
  2940. goto end;
  2941. }
  2942. SigGroupBuild(de_ctx);
  2943. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  2944. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  2945. if (r != 0) {
  2946. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  2947. goto end;
  2948. }
  2949. http_state = f.alstate;
  2950. if (http_state == NULL) {
  2951. printf("no http state: ");
  2952. goto end;
  2953. }
  2954. /* do detect */
  2955. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  2956. if (PacketAlertCheck(p, 1)) {
  2957. printf("sig 1 alerted, but it shouldn't have: ");
  2958. goto end;
  2959. }
  2960. result = 1;
  2961. end:
  2962. if (det_ctx != NULL)
  2963. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  2964. if (de_ctx != NULL)
  2965. SigGroupCleanup(de_ctx);
  2966. if (de_ctx != NULL)
  2967. DetectEngineCtxFree(de_ctx);
  2968. StreamTcpFreeConfig(TRUE);
  2969. FLOW_DESTROY(&f);
  2970. UTHFreePacket(p);
  2971. return result;
  2972. }
  2973. static int UriTestSig37(void)
  2974. {
  2975. int result = 0;
  2976. uint8_t *http_buf = (uint8_t *)"POST /normalized%20uri "
  2977. "HTTP/1.0\r\nUser-Agent: Mozilla/1.0\r\n";
  2978. uint32_t http_buf_len = strlen((char *)http_buf);
  2979. Flow f;
  2980. TcpSession ssn;
  2981. HtpState *http_state = NULL;
  2982. Packet *p = NULL;
  2983. ThreadVars tv;
  2984. DetectEngineThreadCtx *det_ctx = NULL;
  2985. memset(&tv, 0, sizeof(ThreadVars));
  2986. memset(&f, 0, sizeof(Flow));
  2987. memset(&ssn, 0, sizeof(TcpSession));
  2988. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  2989. FLOW_INITIALIZE(&f);
  2990. f.protoctx = (void *)&ssn;
  2991. f.flags |= FLOW_IPV4;
  2992. p->flow = &f;
  2993. p->flowflags |= FLOW_PKT_TOSERVER;
  2994. p->flowflags |= FLOW_PKT_ESTABLISHED;
  2995. f.alproto = ALPROTO_HTTP;
  2996. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  2997. StreamTcpInitConfig(TRUE);
  2998. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  2999. if (de_ctx == NULL) {
  3000. goto end;
  3001. }
  3002. de_ctx->mpm_matcher = MPM_B2G;
  3003. de_ctx->flags |= DE_QUIET;
  3004. de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  3005. "(msg:\"test multiple relative uricontents\"; "
  3006. "urilen:17, raw; sid:1;)");
  3007. if (de_ctx->sig_list == NULL) {
  3008. goto end;
  3009. }
  3010. SigGroupBuild(de_ctx);
  3011. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  3012. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  3013. if (r != 0) {
  3014. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  3015. goto end;
  3016. }
  3017. http_state = f.alstate;
  3018. if (http_state == NULL) {
  3019. printf("no http state: ");
  3020. goto end;
  3021. }
  3022. /* do detect */
  3023. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  3024. if (!PacketAlertCheck(p, 1)) {
  3025. printf("sig 1 didn't alert, but it should have: ");
  3026. goto end;
  3027. }
  3028. result = 1;
  3029. end:
  3030. if (det_ctx != NULL)
  3031. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  3032. if (de_ctx != NULL)
  3033. SigGroupCleanup(de_ctx);
  3034. if (de_ctx != NULL)
  3035. DetectEngineCtxFree(de_ctx);
  3036. StreamTcpFreeConfig(TRUE);
  3037. FLOW_DESTROY(&f);
  3038. UTHFreePacket(p);
  3039. return result;
  3040. }
  3041. static int UriTestSig38(void)
  3042. {
  3043. int result = 0;
  3044. uint8_t *http_buf = (uint8_t *)"POST /normalized%20uri "
  3045. "HTTP/1.0\r\nUser-Agent: Mozilla/1.0\r\n";
  3046. uint32_t http_buf_len = strlen((char *)http_buf);
  3047. Flow f;
  3048. TcpSession ssn;
  3049. HtpState *http_state = NULL;
  3050. Packet *p = NULL;
  3051. ThreadVars tv;
  3052. DetectEngineThreadCtx *det_ctx = NULL;
  3053. memset(&tv, 0, sizeof(ThreadVars));
  3054. memset(&f, 0, sizeof(Flow));
  3055. memset(&ssn, 0, sizeof(TcpSession));
  3056. p = UTHBuildPacket(http_buf, http_buf_len, IPPROTO_TCP);
  3057. FLOW_INITIALIZE(&f);
  3058. f.protoctx = (void *)&ssn;
  3059. f.flags |= FLOW_IPV4;
  3060. p->flow = &f;
  3061. p->flowflags |= FLOW_PKT_TOSERVER;
  3062. p->flowflags |= FLOW_PKT_ESTABLISHED;
  3063. f.alproto = ALPROTO_HTTP;
  3064. p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
  3065. StreamTcpInitConfig(TRUE);
  3066. DetectEngineCtx *de_ctx = DetectEngineCtxInit();
  3067. if (de_ctx == NULL) {
  3068. goto end;
  3069. }
  3070. de_ctx->mpm_matcher = MPM_B2G;
  3071. de_ctx->flags |= DE_QUIET;
  3072. de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
  3073. "(msg:\"test multiple relative uricontents\"; "
  3074. "urilen:18, raw; sid:1;)");
  3075. if (de_ctx->sig_list == NULL) {
  3076. goto end;
  3077. }
  3078. SigGroupBuild(de_ctx);
  3079. DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
  3080. int r = AppLayerParse(NULL, &f, ALPROTO_HTTP, STREAM_TOSERVER, http_buf, http_buf_len);
  3081. if (r != 0) {
  3082. printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
  3083. goto end;
  3084. }
  3085. http_state = f.alstate;
  3086. if (http_state == NULL) {
  3087. printf("no http state: ");
  3088. goto end;
  3089. }
  3090. /* do detect */
  3091. SigMatchSignatures(&tv, de_ctx, det_ctx, p);
  3092. if (PacketAlertCheck(p, 1)) {
  3093. printf("sig 1 alerted, but it shouldn't have: ");
  3094. goto end;
  3095. }
  3096. result = 1;
  3097. end:
  3098. if (det_ctx != NULL)
  3099. DetectEngineThreadCtxDeinit(&tv, det_ctx);
  3100. if (de_ctx != NULL)
  3101. SigGroupCleanup(de_ctx);
  3102. if (de_ctx != NULL)
  3103. DetectEngineCtxFree(de_ctx);
  3104. StreamTcpFreeConfig(TRUE);
  3105. FLOW_DESTROY(&f);
  3106. UTHFreePacket(p);
  3107. return result;
  3108. }
  3109. #endif /* UNITTESTS */
  3110. void UriRegisterTests(void)
  3111. {
  3112. #ifdef UNITTESTS
  3113. UtRegisterTest("UriTestSig01", UriTestSig01, 1);
  3114. UtRegisterTest("UriTestSig02", UriTestSig02, 1);
  3115. UtRegisterTest("UriTestSig03", UriTestSig03, 1);
  3116. UtRegisterTest("UriTestSig04", UriTestSig04, 1);
  3117. UtRegisterTest("UriTestSig05", UriTestSig05, 1);
  3118. UtRegisterTest("UriTestSig06", UriTestSig06, 1);
  3119. UtRegisterTest("UriTestSig07", UriTestSig07, 1);
  3120. UtRegisterTest("UriTestSig08", UriTestSig08, 1);
  3121. UtRegisterTest("UriTestSig09", UriTestSig09, 1);
  3122. UtRegisterTest("UriTestSig10", UriTestSig10, 1);
  3123. UtRegisterTest("UriTestSig11", UriTestSig11, 1);
  3124. UtRegisterTest("UriTestSig12", UriTestSig12, 1);
  3125. UtRegisterTest("UriTestSig13", UriTestSig13, 1);
  3126. UtRegisterTest("UriTestSig14", UriTestSig14, 1);
  3127. UtRegisterTest("UriTestSig15", UriTestSig15, 1);
  3128. UtRegisterTest("UriTestSig16", UriTestSig16, 1);
  3129. UtRegisterTest("UriTestSig17", UriTestSig17, 1);
  3130. UtRegisterTest("UriTestSig18", UriTestSig18, 1);
  3131. UtRegisterTest("UriTestSig19", UriTestSig19, 1);
  3132. UtRegisterTest("UriTestSig20", UriTestSig20, 1);
  3133. UtRegisterTest("UriTestSig21", UriTestSig21, 1);
  3134. UtRegisterTest("UriTestSig22", UriTestSig22, 1);
  3135. UtRegisterTest("UriTestSig23", UriTestSig23, 1);
  3136. UtRegisterTest("UriTestSig24", UriTestSig24, 1);
  3137. UtRegisterTest("UriTestSig25", UriTestSig25, 1);
  3138. UtRegisterTest("UriTestSig26", UriTestSig26, 1);
  3139. UtRegisterTest("UriTestSig27", UriTestSig27, 1);
  3140. UtRegisterTest("UriTestSig28", UriTestSig28, 1);
  3141. UtRegisterTest("UriTestSig29", UriTestSig29, 1);
  3142. UtRegisterTest("UriTestSig30", UriTestSig30, 1);
  3143. UtRegisterTest("UriTestSig31", UriTestSig31, 1);
  3144. UtRegisterTest("UriTestSig32", UriTestSig32, 1);
  3145. UtRegisterTest("UriTestSig33", UriTestSig33, 1);
  3146. UtRegisterTest("UriTestSig34", UriTestSig34, 1);
  3147. UtRegisterTest("UriTestSig35", UriTestSig35, 1);
  3148. UtRegisterTest("UriTestSig36", UriTestSig36, 1);
  3149. UtRegisterTest("UriTestSig37", UriTestSig37, 1);
  3150. UtRegisterTest("UriTestSig38", UriTestSig38, 1);
  3151. #endif /* UNITTESTS */
  3152. return;
  3153. }