28 #include "../stream-tcp.h"
29 #include "../detect.h"
30 #include "../detect-isdataat.h"
31 #include "../detect-engine-inspect-buffer.h"
32 #include "../util-file-decompression.h"
33 #include "../util-file-swf-decompression.h"
34 #include "../app-layer-events.h"
35 #include "../app-layer-htp.h"
39 static uint8_t *DetectFiledataSwfCreateCws(
const uint8_t *plain, uint32_t plain_len,
40 uint32_t file_len,
int compression_level, uint32_t *cws_len)
42 uLongf compressed_len = compressBound(plain_len);
51 cws[4] = (uint8_t)file_len;
52 cws[5] = (uint8_t)(file_len >> 8);
53 cws[6] = (uint8_t)(file_len >> 16);
54 cws[7] = (uint8_t)(file_len >> 24);
56 int r = compress2(cws +
SWF_HEADER_LEN, &compressed_len, plain, plain_len, compression_level);
66 static const uint8_t swf_lzma_fixture[] = {
67 0x5a, 0x57, 0x53, 0x17, 0x5c, 0x24, 0x00, 0x00, 0xb7, 0x21, 0x00, 0x00, 0x5d, 0x00, 0x00, 0x20,
68 0x00, 0x00, 0x3b, 0xff, 0xfc, 0x8e, 0x19, 0xfa, 0xdf, 0xe7, 0x66, 0x08, 0xa0, 0x3d, 0x3e, 0x85,
69 0xf5, 0x75, 0x6f, 0xd0, 0x7e, 0x61, 0x35, 0x1b, 0x1a, 0x8b, 0x16, 0x4d, 0xdf, 0x05, 0x32, 0xfe,
70 0xa4, 0x4c, 0x46, 0x49, 0xb7, 0x7b, 0x6b, 0x75, 0xf9, 0x2b, 0x5c, 0x37, 0x29, 0x0b, 0x91, 0x37,
71 0x01, 0x37, 0x0e, 0xe9, 0xf2, 0xe1, 0xfc, 0x9e, 0x64, 0xda, 0x6c, 0x11, 0x21, 0x33, 0xed, 0xa0,
72 0x0e, 0x76, 0x70, 0xa0, 0xcd, 0x98, 0x2e, 0x76, 0x80, 0xf0, 0xe0, 0x59, 0x56, 0x06, 0x08, 0xe9,
73 0xca, 0xeb, 0xa2, 0xc6, 0xdb, 0x5a, 0x86,
82 static int DetectFiledataSwfDecompressAllocTest01(
void)
84 uint8_t buffer[] = {
'C',
'W',
'S', 0x06, 0x80, 0xF0, 0xFA, 0x02, 0x00, 0x01, 0x02, 0x03, 0x04,
91 memset(&out_buffer, 0,
sizeof(out_buffer));
109 static int DetectFiledataSwfDecompressAllocTest02(
void)
111 const uint8_t plain[] =
"small compressed SWF body";
113 uint32_t cws_len = 0;
114 uint8_t *cws = DetectFiledataSwfCreateCws(
115 plain,
sizeof(plain), file_len, Z_BEST_COMPRESSION, &cws_len);
128 FAIL_IF(memcmp(out_buffer.
buf + 4, cws + 4, 4) != 0);
142 static int DetectFiledataSwfDecompressAllocTest03(
void)
144 const uint32_t plain_len = 48 * 1024;
145 uint8_t *plain =
SCMalloc(plain_len);
147 for (uint32_t i = 0; i < plain_len; i++)
148 plain[i] = (uint8_t)i;
150 uint32_t cws_len = 0;
151 uint8_t *cws = DetectFiledataSwfCreateCws(
152 plain, plain_len, plain_len +
SWF_HEADER_LEN, Z_NO_COMPRESSION, &cws_len);
179 static int DetectFiledataSwfDecompressAllocTest04(
void)
181 uint8_t buffer[] = {
'Z',
'W',
'S', 0x0D, 0x80, 0xF0, 0xFA, 0x02, 0x01, 0x00, 0x00, 0x00, 0xFF,
182 0xFF, 0xFF, 0xFF, 0xFF, 0x00 };
204 static int DetectFiledataSwfDecompressFileLengthTest01(
void)
206 const uint32_t plain_len = 16 * 1024;
207 uint8_t *plain =
SCMalloc(plain_len);
209 memset(plain,
'A', plain_len);
210 plain[plain_len - 1] =
'B';
212 uint32_t cws_len = 0;
213 uint8_t *cws = DetectFiledataSwfCreateCws(plain, plain_len, 1, Z_BEST_COMPRESSION, &cws_len);
226 FAIL_IF(memcmp(out_buffer.
buf + 4, cws + 4, 4) != 0);
239 static int DetectFiledataSwfDecompressFileLengthTest02(
void)
241 uint8_t zws[
sizeof(swf_lzma_fixture)];
242 memcpy(zws, swf_lzma_fixture,
sizeof(zws));
257 FAIL_IF(memcmp(out_buffer.
buf + 4, zws + 4, 4) != 0);
268 static int DetectFiledataSwfDecompressHeaderTest01(
void)
270 const uint8_t plain[] =
"small body";
272 uint32_t cws_len = 0;
273 uint8_t *cws = DetectFiledataSwfCreateCws(
274 plain,
sizeof(plain), file_len, Z_BEST_COMPRESSION, &cws_len);
285 FAIL_IF(memcmp(out_buffer.
buf + 4, cws + 4, 4) != 0);
297 static int DetectFiledataSwfDecompressDepthTest01(
void)
299 const uint32_t plain_len = 16 * 1024;
300 const uint32_t decompress_depth = 5000;
301 uint8_t *plain =
SCMalloc(plain_len);
303 memset(plain,
'A', plain_len);
304 plain[plain_len - 1] =
'B';
306 uint32_t cws_len = 0;
307 uint8_t *cws = DetectFiledataSwfCreateCws(
308 plain, plain_len, plain_len +
SWF_HEADER_LEN, Z_BEST_COMPRESSION, &cws_len);
333 static int DetectFiledataSwfDecompressDepthTest02(
void)
335 const uint32_t decompress_depth = 1;
355 static int DetectFiledataSwfDecompressDepthTest03(
void)
376 static int DetectEngineSMTPFiledataTest02(
void)
383 "(msg:\"file_data smtp test\"; "
384 "file_data; content:\"message\"; sid:1;)");
395 static int DetectFiledataParseTest04(
void)
401 "alert smtp any any -> any any "
402 "(msg:\"test\"; flow:to_client,established; file_data; content:\"abc\"; sid:1;)");
411 "DetectFiledataSwfDecompressAllocTest01", DetectFiledataSwfDecompressAllocTest01);
413 "DetectFiledataSwfDecompressAllocTest02", DetectFiledataSwfDecompressAllocTest02);
415 "DetectFiledataSwfDecompressAllocTest03", DetectFiledataSwfDecompressAllocTest03);
417 "DetectFiledataSwfDecompressAllocTest04", DetectFiledataSwfDecompressAllocTest04);
419 DetectFiledataSwfDecompressFileLengthTest01);
421 DetectFiledataSwfDecompressFileLengthTest02);
423 "DetectFiledataSwfDecompressHeaderTest01", DetectFiledataSwfDecompressHeaderTest01);
425 "DetectFiledataSwfDecompressDepthTest01", DetectFiledataSwfDecompressDepthTest01);
427 "DetectFiledataSwfDecompressDepthTest02", DetectFiledataSwfDecompressDepthTest02);
429 "DetectFiledataSwfDecompressDepthTest03", DetectFiledataSwfDecompressDepthTest03);
430 UtRegisterTest(
"DetectEngineSMTPFiledataTest02", DetectEngineSMTPFiledataTest02);
431 UtRegisterTest(
"DetectFiledataParseTest04", DetectFiledataParseTest04);