forked from OISF/suricata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode-afl.c
167 lines (144 loc) · 4.8 KB
/
decode-afl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* Copyright (C) 2007-2017 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "suricata-common.h"
#include "suricata.h"
#include "conf.h"
#include "decode.h"
#include "util-debug.h"
#include "util-mem.h"
#include "app-layer-detect-proto.h"
#include "app-layer.h"
#include "tm-threads.h"
#include "util-error.h"
#include "util-print.h"
#include "tmqh-packetpool.h"
#include "util-profiling.h"
#include "pkt-var.h"
#include "util-mpm-ac.h"
#include "output.h"
#include "output-flow.h"
#include "defrag.h"
#include "flow.h"
#ifdef AFLFUZZ_DECODER
/* stateful processing of data as packets. Because AFL in case of a
* crash will only safe the last input, we dump all the inputs to a
* directory 'dump' with a unique timestamp for the serie and an
* incrementing 'id' so that we can 'replay' it in
* DecoderParseDataFromFileSerie().
*/
int DecoderParseDataFromFile(char *filename, DecoderFunc Decoder) {
uint8_t buffer[65536];
struct timeval ts;
memset(&ts, 0, sizeof(ts));
gettimeofday(&ts, NULL);
uint32_t cnt = 0;
DefragInit();
FlowInitConfig(FLOW_QUIET);
ThreadVars tv;
memset(&tv, 0, sizeof(tv));
DecodeThreadVars *dtv = DecodeThreadVarsAlloc(&tv);
DecodeRegisterPerfCounters(dtv, &tv);
StatsSetupPrivate(&tv);
PacketQueue pq;
memset(&pq, 0, sizeof(pq));
#ifdef AFLFUZZ_PERSISTANT_MODE
while (__AFL_LOOP(1000)) {
/* reset state */
memset(buffer, 0, sizeof(buffer));
#endif /* AFLFUZZ_PERSISTANT_MODE */
FILE *fp = fopen(filename, "r");
BUG_ON(fp == NULL);
size_t size = fread(&buffer, 1, sizeof(buffer), fp);
char outfilename[256];
snprintf(outfilename, sizeof(outfilename), "dump/%u-%u.%u", (uint)ts.tv_sec, (uint)ts.tv_usec, cnt);
FILE *out_fp = fopen(outfilename, "w");
BUG_ON(out_fp == NULL);
(void)fwrite(buffer, size, 1, out_fp);
fclose(out_fp);
Packet *p = PacketGetFromAlloc();
if (p != NULL) {
PacketSetData(p, buffer, size);
(void) Decoder (&tv, dtv, p, buffer, size, &pq);
while (1) {
Packet *extra_p = PacketDequeue(&pq);
if (unlikely(extra_p == NULL))
break;
PacketFree(extra_p);
}
PacketFree(p);
}
fclose(fp);
cnt++;
#ifdef AFLFUZZ_PERSISTANT_MODE
}
#endif /* AFLFUZZ_PERSISTANT_MODE */
/* if we get here there was no crash, so we can remove our files */
uint32_t x = 0;
for (x = 0; x < cnt; x++) {
char rmfilename[256];
snprintf(rmfilename, sizeof(rmfilename), "dump/%u-%u.%u", (uint)ts.tv_sec, (uint)ts.tv_usec, x);
unlink(rmfilename);
}
DecodeThreadVarsFree(&tv, dtv);
FlowShutdown();
DefragDestroy();
return 0;
}
/* load a serie of files generated by DecoderParseDataFromFile() in
* the same order as it was produced. */
int DecoderParseDataFromFileSerie(char *fileprefix, DecoderFunc Decoder)
{
uint8_t buffer[65536];
uint32_t cnt = 0;
DefragInit();
FlowInitConfig(FLOW_QUIET);
ThreadVars tv;
memset(&tv, 0, sizeof(tv));
DecodeThreadVars *dtv = DecodeThreadVarsAlloc(&tv);
DecodeRegisterPerfCounters(dtv, &tv);
StatsSetupPrivate(&tv);
PacketQueue pq;
memset(&pq, 0, sizeof(pq));
char filename[256];
snprintf(filename, sizeof(filename), "dump/%s.%u", fileprefix, cnt);
FILE *fp;
while ((fp = fopen(filename, "r")) != NULL)
{
memset(buffer, 0, sizeof(buffer));
size_t size = fread(&buffer, 1, sizeof(buffer), fp);
Packet *p = PacketGetFromAlloc();
if (p != NULL) {
PacketSetData(p, buffer, size);
(void) Decoder (&tv, dtv, p, buffer, size, &pq);
while (1) {
Packet *extra_p = PacketDequeue(&pq);
if (unlikely(extra_p == NULL))
break;
PacketFree(extra_p);
}
PacketFree(p);
}
fclose(fp);
cnt++;
snprintf(filename, sizeof(filename), "dump/%s.%u", fileprefix, cnt);
}
DecodeThreadVarsFree(&tv, dtv);
FlowShutdown();
DefragDestroy();
return 0;
}
#endif /* AFLFUZZ_DECODER */