From: Victor Julien Date: Fri, 25 Mar 2016 13:25:22 +0000 (+0100) Subject: QA: expose Mime decoding API to commandline using --afl-mime= X-Git-Tag: suricata-3.1RC1~261 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bdaba1d8156a948934686f4f4aa25303a238b1f6;p=thirdparty%2Fsuricata.git QA: expose Mime decoding API to commandline using --afl-mime= --- diff --git a/configure.ac b/configure.ac index 47fe5e942d..e0e2efdbd6 100644 --- a/configure.ac +++ b/configure.ac @@ -268,6 +268,7 @@ AC_DEFINE([AFLFUZZ_PCAP_RUNMODE], [1], [Enable special AFL 'single' runmode]) AC_DEFINE([AFLFUZZ_CONF_TEST], [1], [Enable special --afl-parse-rules commandline option]) AC_DEFINE([AFLFUZZ_APPLAYER], [1], [Enable --afl-$proto-request commandline option]) + AC_DEFINE([AFLFUZZ_MIME], [1], [Enable --afl-mime commandline option]) ]) # disable TLS on user request diff --git a/src/suricata.c b/src/suricata.c index d3b069ef6a..e3edc0d493 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -1159,6 +1159,7 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri) {"afl-smb", required_argument, 0 , 0}, {"afl-modbus-request", required_argument, 0 , 0}, {"afl-modbus", required_argument, 0 , 0}, + {"afl-mime", required_argument, 0 , 0}, #ifdef BUILD_UNIX_SOCKET {"unix-socket", optional_argument, 0, 0}, #endif @@ -1432,6 +1433,11 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri) AppLayerParserSetup(); RegisterModbusParsers(); exit(AppLayerParserFromFile(ALPROTO_MODBUS, optarg)); +#endif +#ifdef AFLFUZZ_MIME + } else if(strcmp((long_opts[option_index]).name, "afl-mime") == 0) { + //printf("arg: //%s\n", optarg); + exit(MimeParserDataFromFile(optarg)); #endif } else if(strcmp((long_opts[option_index]).name, "simulate-ips") == 0) { SCLogInfo("Setting IPS mode"); diff --git a/src/util-decode-mime.c b/src/util-decode-mime.c index 8ba56b6f1c..7d8e888437 100644 --- a/src/util-decode-mime.c +++ b/src/util-decode-mime.c @@ -2614,6 +2614,52 @@ MimeDecEntity * MimeDecParseFullMsg(const uint8_t *buf, uint32_t blen, void *dat return msg; } +#ifdef AFLFUZZ_MIME +static int MimeParserDataFromFileCB(const uint8_t *chunk, uint32_t len, + MimeDecParseState *state) +{ + return MIME_DEC_OK; +} + +int MimeParserDataFromFile(char *filename) +{ + int result = 1; + FILE *fp = fopen(filename, "r"); + BUG_ON(fp == NULL); + uint8_t buffer[256]; + + uint32_t line_count = 0; + + MimeDecParseState *state = MimeDecInitParser(&line_count, + MimeParserDataFromFileCB); + + while (1) { + int done = 0; + size_t result = fread(&buffer, 1, sizeof(buffer), fp); + if (result < sizeof(buffer)) + done = 1; + + (void) MimeDecParseLine(buffer, result, 1, state); + + if (done) + break; + } + + /* Completed */ + (void)MimeDecParseComplete(state); + + if (state->msg) { + MimeDecFreeEntity(state->msg); + } + /* De Init parser */ + MimeDecDeInitParser(state); + + result = 0; + fclose(fp); + return result; +} +#endif + #ifdef UNITTESTS /* Helper body chunk callback function */ diff --git a/src/util-decode-mime.h b/src/util-decode-mime.h index 02b3bb13dd..0e5fb7cb05 100644 --- a/src/util-decode-mime.h +++ b/src/util-decode-mime.h @@ -239,6 +239,10 @@ MimeDecEntity * MimeDecParseFullMsg(const uint8_t *buf, uint32_t blen, void *dat int (*DataChunkProcessorFunc)(const uint8_t *chunk, uint32_t len, MimeDecParseState *state)); const char *MimeDecParseStateGetStatus(MimeDecParseState *state); +#ifdef AFLFUZZ_MIME +int MimeParserDataFromFile(char *filename); +#endif + /* Test functions */ void MimeDecRegisterTests(void);