-
Notifications
You must be signed in to change notification settings - Fork 1
/
newt.js
executable file
·230 lines (216 loc) · 12.3 KB
/
newt.js
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env node
/*
* newt.js, a simple node-powered fuzzer by @chaseahiggins
*****************************************************************************
* newt provides a simple, extensible method to perform file format fuzzing
* by exposing various function hooks to set the monitoring and fuzz methods
* newt makes it easy to add custom process monitoring/exit handling and
* can easily be modified to accept more fuzzing methods.
*****************************************************************************
* BUGS/TODO:
* the new standalone procmon reveals how tangled a mess things are, needs to be fixed fast <---||||f
* unite arg parsers
* eliminate dependence on exploitable module in gdb mode (or coopt gdb.js into this codebase)
* move crash logging deal to logging class
* add support for fuzzing cli args
* add support for fuzzing via stdin
* that master result obj needs to be consistent
* port newt to windows including windbg monitor mode
* autofuzz mode should should switch to exit code + message with next()
* cache ngen files in memory to reduce tight file reads
* netfuzzing mode is not complete
* add crash minimizer
* some tests would be nice yeah?
* add ability to use multiple mutators on each file
*****************************************************************************
*/
var fs = require('fs');
var newt = newt || {};
// our shit
newt.procmon = require('./lib/procmon');
newt.fuzzing = require('./lib/fuzzing');
newt.fuzzer = require('./lib/fuzz_director');
// options
newt.opts = {
debug: 1
};
// cli interpretron time
if (require.main === module) {
// we will keep cli stuff real simple, check first arg, exec accordingly
switch (process.argv[2]) {
case "randbuff": // random buffer
// print a random buffer of argv[3] len
if (process.argv.length < 5) {
console.log("Generate a random string of a provided length:");
console.log("\tuse: newt.js randbuff -l <str len>");
console.log("\tex: newt.js randbuff -l 32");
process.exit();
}
var args = process.argv.slice(3);
for (var i = 0; i < args.length; i++) {
var arg = args[i];
switch (arg) {
case "-l": // str length
args.str_len = args[++i];
break;
}
}
var b = newt.fuzzing.randBuff(args.str_len, {
printable_only: 1
});
process.stdout.write(b);
break;
case "mutate": // mangle buffer
if (process.argv.length < 5) {
console.log("Take buffer from stdin, mangle every fuzz factor bytes and print to stdout:");
console.log("\tuse: newt.js mutate -f <fuzz factor>");
console.log("\tex: cat seed.jpg | ./newt.js mutate -f 32 -x ripple > case.jpg");
console.log("\tavailable mutators are: buffmangler, bitflip, byteflip, bytearith, chunkspew, bitrotate, ripple");
process.exit();
}
var args = newt.fuzzer.utils.parseArgs(process.argv.slice(3));
// take a stdinput, mangle it to stdout
if (!args.mutators) {
console.log("[!] mutatation mode requires you to set a mutator with flag -x (e.g. -x ripple");
console.log("[i] try \"newt.js mutate\" for more information");
process.exit();
}
process.stdin.resume();
process.stdin.setEncoding('binary');
process.stdout.setDefaultEncoding('binary');
process.stdin.on('data', function (buff) {
var b = newt.fuzzing[args.mutators[0].name](buff, {
fuzz_factor: args.fuzz_factor
});
process.stdout.write(b);
});
break;
case "autofuzz": // go through and fuzz a subject automagically
if (process.argv.length < 9) {
console.log("Perform an unattended fuzz run on a subject binary");
console.log("\tuse: newt.js autofuzz -s <subject> -i <seeds_sir> -o <output_dir> [-k <kill_time> -m <monitor_mode> -f <fuzz_factor> -x <mutators>]");
console.log("\tex: newt.js autofuzz -f 32 -s okular -m gdb -i seeds -o out -k 2");
console.log("\tavailable monitoring modes are: gdb, asan");
console.log("\tavailable mutators are: buffmangler, bitflip, byteflip, bytearith, chunkspew, bitrotate, ripple");
console.log("\tfuzz factor roughly translates to fuzzing 1/-f bytes");
process.exit();
}
var opts = newt.fuzzer.utils.parseArgs(process.argv.slice(3));
newt.fuzzer.autoFuzz(opts);
break;
case "netfuzz": // much like autofuzz, but assuming the local instance unable to procmon
/*
* for now, we will just use sockets I guess, and assume that the ngen file
* has taken care of the hard work of describing the correct protocol
*/
if (process.argv.length < 9) {
console.log("Fuzz a remote network service");
console.log("\tuse newt.js netfuzz -i <ngen_seeds_dir> -o <logging_dir> -h <host:port>");
console.log("\tex: newt.js netfuzz -i ngen_seeds -o out -h localhost:1337");
console.log("\tnote: ngen seed exmaples come with newt, will be further documented in the future");
process.exit();
}
var args = process.argv.slice(3);
for (var i = 0; i < args.length; i++) {
var arg = args[i];
switch (arg) {
case "-h":
// set the host:port
args.host = args[++i];
break;
case "-o":
// out dir
args.out_dir = args[++i];
break;
case "-i":
// seeds dir
args.seeds_dir = args[++i];
break;
}
} // end options iteration
newt.fuzzer.netFuzz(args);
break; // end netfuzz
case "procmon": // simply spawn a proc to mon, fuzzing will happen elsewhere
// parse args from stdin
if (process.argv.length < 7) {
console.log("Spawn a process with arguments and monitor for crashes");
console.log("\tuse: ./newt.js procmon -s <subject bin + args> -m <monitor mode>");
console.log("\tex: ./newt.js procmon -s firefox case.html -m asan");
console.log("\tavailable monitoring modes are gdb, asan, none");
process.exit();
}
var args = process.argv.slice(3);
for (var i = 0; i < args.length; i++) {
var arg = args[i];
switch (arg) {
case "-r":
// respawn proc if crashes
args.respawn = true;
continue;
case "-o":
// output directory
args.out_dir = args[++i];
continue;
case "-s":
// the fuzz subject
// special case where we may need to collect multiple
var cmd = [];
while (args[++i] && args[i].indexOf("-") !== 0 && i < args.length) // find all opts
cmd.push(args[i]); // append to the cmd arr
// now set it all
args.subject = cmd;
i -= 1;
continue;
case "-m":
// monitor mode, default gdb
args.monitoring = {
mode: args[++i],
launcher: null
};
continue;
default:
console.log("[-] Unrecognized option: " + arg);
} // end switch
} // end for
newt.procmon.standaloneMonitor(args, null);
break;
default:
// help case
console.log("[~] newt.js " + require('./package.json').version + " - a simple node-powered fuzzer");
console.log("Usage: newt command [opts]");
console.log("Specify any command with no options for more usage information\n");
console.log("Commands:");
// autofuzz
console.log(" autofuzz Automatically generate cases and fuzz the subject");
console.log(" | -i Required, directory where file format or ngen seeds can be found");
console.log(" | -o Required, output directory for crashes, logs, cases");
console.log(" | -s Required, the subject binary to fuzz");
console.log(" | -k Optional, kill subject after -k seconds, useful for GUI bins");
console.log(" | -f Optional, int value that translates to fuzzing 1/-f byte in most mutators");
console.log(" | -m Optional, monitor mode. Default is gdb, asan instrumented bins also supported");
console.log(" | -x Optional, comma-separated list of mutators(e.g. ripple,chunkspew) default is all");
console.log(" | -p Optional, preserve non-crashing cases so they can be examined or used elsewhere")
console.log(); // newline
// procmon
console.log(" procmon Launch and monitor a process");
console.log(" | -s Required, the subject binary [with args]");
console.log(" | -m Required, monitor mode [asan|gdb]");
console.log(" | -r Optional, respawn process on exit(flag takes no args)");
console.log(" | -o Optional, output dir. Results printed to console if none specified");
console.log();
// netfuzz
console.log(" netfuzz Fuzz a remote network service");
console.log(" | -i Required, directory where ngen seeds can be found");
console.log(" | -o Required, output directory for crashes, logs, cases");
console.log(" | -h Required, the host to send the fuzz case as host:port");
console.log();
// randbuff
console.log(" randbuff Generate a random string for use elsewhere");
console.log(" | -l Required, length of string to generate");
console.log();
// mutate
console.log(" mutate Mutate an input buffer from stdin, write it to stdout");
console.log(" | -f Required, fuzz factor for the input buffer");
console.log(" | -x Required, the mutator to use(e.g. ripple)");
} // end comm switch
} // end arg handling