-
Notifications
You must be signed in to change notification settings - Fork 48
/
nodefuzz.js
315 lines (294 loc) · 9.68 KB
/
nodefuzz.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env node
if (
process.argv.indexOf("help") !== -1 ||
process.argv.indexOf("-help") !== -1 ||
process.argv.indexOf("--help") !== -1
) {
console.log("NodeFuzz v0.1.6");
console.log("Check config.js for about everything.");
console.log(
'-m | --module - "ModulePath" where to load modules. (Note: can be file or folder)',
);
console.log(
"-c | --config - Configuration-file path. (Note: Configuration-file must export config-object)",
);
console.log("-i | --instrumentation - Instrumentation-module path.");
console.log("NodeFuzz is tested to work on nodejs 0.10.8");
process.exit(1);
}
//
// instrumentationEvents is global EventEmitter for events from NodeFuzz-core to instrumentation modules.
//
const events = require("events");
// eslint-disable-next-line no-multi-assign
const instrumentationEvents = (global.instrumentationEvents =
new events.EventEmitter());
const fs = require("fs");
//
// Require for config and its init. (Note: config is required into global object so it can be used in
// communication between modules without separate requires.)
//
if (
process.argv.indexOf("-c") !== -1 ||
process.argv.indexOf("--config") !== -1
) {
try {
console.log("Loading config-file: ");
if (process.argv.indexOf("-c") !== -1) {
console.log(process.argv[process.argv.indexOf("-c") + 1]);
config = require(process.argv[process.argv.indexOf("-c") + 1]);
} else {
console.log(process.argv[process.argv.indexOf("--config") + 1]);
config = require(process.argv[process.argv.indexOf("--config") + 1]);
}
} catch (e) {
console.log(`Error while loading given configuration-file.\n${e}`);
process.exit(1);
}
} else {
console.log("Loading default configuration-file:\n./config.js");
try {
config = require("./config.js");
} catch (e) {
console.log(`Error: ${e}\nI can't survive without configuration...`);
process.exit(1);
}
}
if (config.init) config.init();
else console.log("config.js had no property init.");
if (
process.argv.indexOf("-i") !== -1 ||
process.argv.indexOf("--instrumentation") !== -1
) {
try {
console.log("Loading instrumentation-module: ");
if (process.argv.indexOf("-i") !== -1) {
console.log(process.argv[process.argv.indexOf("-i") + 1]);
config.instrumentation = require(process.argv[
process.argv.indexOf("-i") + 1
]);
} else {
console.log(process.argv[process.argv.indexOf("--instrumentation") + 1]);
config.instrumentation = require(process.argv[
process.argv.indexOf("--instrumentation") + 1
]);
}
} catch (e) {
console.log(`Error while loading given instrumentation-file.\n${e}`);
// process.exit(1)
}
} else {
console.log("Loading default instrumentation-module:");
if (config.defaultInstrumentationFile === undefined) {
console.log(
"No default instrumentation-module defined. Check configuration for config.defaultInstrumentationFile",
);
// process.exit(1)
}
console.log(config.defaultInstrumentationFile);
try {
config.instrumentation = require(config.defaultInstrumentationFile);
} catch (e) {
console.log(`Error: ${e}\n`);
console.log("No instrumentation-module is loaded.");
}
}
//
// ModuleLoader for modules used as testcase generators.
//
const http = require("http");
const WebSocketServer = require("websocket").server;
const moduleLoader = require("./moduleLoader.js");
const fuzzModules = moduleLoader.loadModules();
config.testCaseCounter = 0;
const testCaseBuffer = [];
config.previousTestCasesBuffer = [];
const { previousTestCasesBuffer } = config;
let httpRootDirSet;
//
// HTTP-server.
//
// HTTP-server listens to port specified by config.port
//
// When requested HTTP-server will respond to client with the content specified in config.clientFile
//
// TODO: Allow HTTP-server to respond with other files also.(NOTE: poor implementation done.)
//
let httpRootDirFiles;
if (config.httpRootDir) {
try {
if (fs.statSync(config.httpRootDir).isDirectory()) {
httpRootDirFiles = fs.readdirSync(config.httpRootDir);
httpRootDirSet = true;
}
} catch (e) {
console.log(`Loading http rootdir failed ${e}`);
}
}
let websocketConnected = false;
const server = http.createServer((request, response) => {
if (!websocketConnected) {
if (request.url !== "/favicon.ico") {
response.writeHead(200);
response.write(config.clientFile);
response.end();
} else {
response.writeHead(404);
response.end();
}
} else if (
httpRootDirSet !== undefined &&
httpRootDirFiles.indexOf(request.url.trim().slice(1)) !== -1
) {
console.log(request.url);
response.writeHead(200);
const responseFile = fs.readFileSync(
config.httpRootDir + request.url.trim().slice(1),
);
response.end(responseFile);
} else {
response.writeHead(404);
response.end();
}
});
server.listen(config.port, (err) => {
if (err) throw err;
console.log(`Server listening port ${config.port}`);
});
// WebSocket inits.
const wsServer = new WebSocketServer({
httpServer: server,
autoAcceptConnections: false,
});
if (config.addCustomWebSocketHandler)
config.addCustomWebSocketHandler(wsServer);
//
// WebSocket server connection handler.
//
// Variables
// testCasesWithoutRestartCounter: Counts testcases done without client restart.
// sending: Tracks if testcase sending is already ongoing. Used to prevent errors because of multiple simultaneous requests.
//
// Events
// testCasesWithoutRestartLimit: Triggered if amount of testcases without restart reaches config.testCasesWithoutRestart value.
// websocketTimeout: Triggered if client fails to request new testcase within time defined by config.timeout.
// websocketDisconnected: Triggered when the WebSocket-connection is disconnected
//
let sending = false;
let timeoutGetNewTestcase; // TODO: is this the right scope for this?
let testCasesWithoutRestartCounter = 0;
if (config.disableDefaultWsOnRequest !== true) {
wsServer.on("request", (request) => {
websocketConnected = true;
if (
request.requestedProtocols !== null &&
request.requestedProtocols[0] === "fuzz-protocol"
) {
const connection = request.accept("fuzz-protocol", request.origin);
connection.on("message", () => {
if (!sending) {
sending = true;
testCasesWithoutRestartCounter++;
clearTimeout(timeoutGetNewTestcase);
if (testCasesWithoutRestartCounter > config.testCasesWithoutRestart) {
sending = false;
testCasesWithoutRestartCounter = 0;
instrumentationEvents.emit("testCasesWithoutRestartLimit");
} else {
timeoutGetNewTestcase = setTimeout(() => {
sending = false;
testCasesWithoutRestartCounter = 0;
instrumentationEvents.emit("websocketTimeout");
}, config.timeout);
sendNewTestCase(connection);
}
}
});
} else if (
request.requestedProtocols !== null &&
request.requestedProtocols[0] === "feedback-protocol"
) {
const feedbackConnection = request.accept(
"feedback-protocol",
request.origin,
);
feedbackConnection.on("message", (message) => {
instrumentationEvents.emit("feedbackMessage", message.utf8Data);
});
}
});
wsServer.on("close", () => {
websocketConnected = false;
testCasesWithoutRestartCounter = 0;
setTimeout(() => {
instrumentationEvents.emit("websocketDisconnected");
}, 500);
});
}
//
// Helper function to get random element from array.
//
function ra(a) {
return a[Math.floor(a.length * Math.random())];
}
//
// sendNewTestCase(connection)
// connection: WebSocket connection
//
// This function handles testcases and testcase sending to WebSocket.
//
// Function holds few testcases in the testCaseBuffer variable to minimize the latency from modules used as
// testcase generators.
//
// Modules used as testcase generators are taken randomly from modules loaded into variable fuzzModules by moduleLoader.js
//
// This funtion also updates previousTestCasesBuffer which holds n previous testcases where n is defined by variable config.buffer
//
// If generator module returns empty string then this function will call itself with 20ms setTimeout. This feature
// can be used to poll generator modules that need more time to do stuff.
//
function sendNewTestCase(connection) {
// update the variable to keep track of the current testcase count
config.testCaseCounter += 1;
let currentTestCase;
if (!config.disableTestCaseBuffer) {
if (testCaseBuffer.length === 0) {
process.nextTick(() => {
testCaseBuffer.push(ra(fuzzModules).fuzz());
});
process.nextTick(() => {
testCaseBuffer.push(ra(fuzzModules).fuzz());
});
currentTestCase = ra(fuzzModules).fuzz();
} else if (testCaseBuffer.length >= 4) {
currentTestCase = testCaseBuffer.pop();
} else {
process.nextTick(() => {
testCaseBuffer.push(ra(fuzzModules).fuzz());
});
process.nextTick(() => {
testCaseBuffer.push(ra(fuzzModules).fuzz());
});
currentTestCase = testCaseBuffer.pop();
}
} else {
currentTestCase = ra(fuzzModules).fuzz();
}
if (currentTestCase) {
if (previousTestCasesBuffer.unshift(currentTestCase) > config.bufferSize) {
previousTestCasesBuffer.pop();
}
if (currentTestCase instanceof Buffer) {
connection.sendBytes(currentTestCase);
} else {
const test = Buffer.from(currentTestCase);
connection.sendBytes(test);
}
sending = false;
} else {
setTimeout(() => {
sendNewTestCase(connection);
}, 50);
}
}
instrumentationEvents.emit("startClient");