-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcmd.js
More file actions
300 lines (279 loc) · 7.49 KB
/
Copy pathcmd.js
File metadata and controls
300 lines (279 loc) · 7.49 KB
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
function cmd() {
const path = require("path");
const fs = require("fs");
const self = this;
this.surkuConfig = {
maxMutations: 20,
minMutations: 1,
useOnly: undefined,
seed: undefined,
count: 1,
outputName: undefined,
inputPath: undefined,
verbose: undefined,
};
const debugPrint = function (message, level) {
if (
self.surkuConfig.hasOwnProperty("verbose") &&
self.surkuConfig.verbose >= level
) {
process.stderr.write(message);
}
};
if (
process.argv.indexOf("-v") !== -1 ||
process.argv.indexOf("--verbose") !== -1
)
this.surkuConfig.verbose = 5;
debugPrint("Args: \n", 1);
debugPrint(`${process.argv.toString().replace(/,/g, " ")}\n`, 1);
// These are sort of linked lists
//
/*
shortArgs=[
[argument1, argument2],
[argument1Function, argument2Function]
]
*/
const shortArgs = [
[
"-h",
"-a",
"-V",
"-o",
"-n",
"-s",
"-m",
"-p",
"-g",
"-M",
"-r",
"-l",
"-v",
"-Mm",
"-mm",
],
[
outputHelp,
outputAbout,
outputVersion,
parseOutputArg,
parseCount,
parseSeed,
notImplementedYet,
notImplementedYet,
notImplementedYet,
notImplementedYet,
notImplementedYet,
notImplementedYet,
setVerbose,
setMaxMutations,
setMinMutations,
],
];
const longArgs = [
[
"--help",
"--about",
"--version",
"--output",
"--count",
"--seed",
"--mutations",
"--patterns",
"--generators",
"--meta",
"--recursive",
"--list",
"--verbose",
"--maxmutations",
"--minmutations",
],
[
outputHelp,
outputAbout,
outputVersion,
parseOutputArg,
parseCount,
parseSeed,
notImplementedYet,
notImplementedYet,
notImplementedYet,
notImplementedYet,
notImplementedYet,
notImplementedYet,
setVerbose,
setMaxMutations,
setMinMutations,
],
];
const argDescriptions = [
[", show this thing"],
[", what is this thing"],
[", show version info"],
[
" <arg>, file name pattern for outputs, e.g. output/fuzz-%n.foo, required",
],
[" <arg>, how many files to generate, defaults to 1"],
[" <arg>, which seed to use, defaults to random"],
[
' <arg>, which mutators to use, for defaults use argument "-l" or "--list"',
],
[],
[],
[],
[],
[", list default mutators"],
[", verbosed console-outputs"],
[" <arg>, set max mutations per output, default 20"],
[" <arg>, set min mutations per output, default 2"],
];
function outputHelp() {
console.log("This is Surku v0.1.1 Help.");
console.log("Usage: node Surku.js [arguments] [file ...]");
for (let x = 0; x < shortArgs[0].length; x++) {
if (argDescriptions[x] !== "") {
console.log(
`${shortArgs[0][x]} | ${longArgs[0][x]}${argDescriptions[x]}`
);
}
}
process.exit(1);
}
if (process.argv[2] === undefined) {
outputHelp();
process.exit(1);
}
function outputAbout() {
debugPrint("Surku about:", 5);
console.log("Surku is a general purpose fuzzer");
console.log("Its purpose is to introduce malformations into sample files,");
console.log("in order to trigger errors in program under test.");
console.log("In default only general mutators are available,");
console.log("but new mutators can be added by user.");
console.log("");
console.log("For more info see doumentation in:");
console.log("http://code.google.com/p/ouspg/");
console.log("");
console.log("Surku as written by Atte Kettunen at OUSPG");
process.exit(1);
}
function outputVersion() {
console.log("Surku version 0.1");
process.exit(1);
}
function parseOutputArg(cmd, index) {
debugPrint("parseOutputArg\n", 5);
const outputExpression = process.argv[index + 1];
const resolvedPath = path.resolve(outputExpression);
const pathOnly = path.dirname(resolvedPath);
const baseName = path.basename(resolvedPath);
if (!fs.existsSync(pathOnly)) {
console.log("Output path does not exist.");
process.exit(2);
} else if (baseName.indexOf("%n") === -1) {
console.log("Must have output filename syntax. e.g. fuzz-%n.html");
process.exit(2);
} else {
cmd.surkuConfig.outputName = resolvedPath;
}
}
function parseCount(cmd, index) {
debugPrint("parseCount\n", 5);
const count = process.argv[index + 1];
if (!isNaN(parseInt(count))) {
cmd.surkuConfig.count = count;
} else {
console.log("Invalid output file count.");
process.exit(2);
}
}
function parseSeed(cmd, index) {
debugPrint("parseSeed\n", 5);
const seed = process.argv[index + 1];
if (!isNaN(parseInt(seed))) {
cmd.surkuConfig.seed = seed;
} else {
console.log("Invalid seed.");
process.exit(2);
}
}
function setMaxMutations(cmd, index) {
debugPrint(`setMaxMutations: ${process.argv[index + 1]}\n`, 5);
const mutations = process.argv[index + 1];
if (!isNaN(parseInt(mutations)) && parseInt(mutations) > 0) {
cmd.surkuConfig.maxMutations = mutations;
} else {
console.log("Invalid max mutations value.");
process.exit(2);
}
}
function setMinMutations(cmd, index) {
debugPrint(`setMinMutations: ${process.argv[index + 1]}\n`, 5);
const mutations = process.argv[index + 1];
if (!isNaN(parseInt(mutations)) && parseInt(mutations) > 0) {
cmd.surkuConfig.minMutations = mutations;
} else {
console.log("Invalid min mutations value.");
process.exit(2);
}
}
function setVerbose(cmd, index) {
debugPrint("setVerbose\n", 5);
cmd.surkuConfig.verbose = 5;
}
function notImplementedYet(cmd, index) {
debugPrint("Not implemented yet.\n");
console.log(`Argument ${process.argv[index]} is not implemented yet.`);
}
let lastIndex = 2;
process.argv.forEach((arg, index) => {
const indexShort = shortArgs[0].indexOf(arg);
const indexLong = longArgs[0].indexOf(arg);
if (indexShort !== -1) {
lastIndex = index;
shortArgs[1][indexShort](self, index);
}
if (indexLong !== -1) {
lastIndex = index;
longArgs[1][indexLong](self, index);
}
});
if (
parseInt(self.surkuConfig.maxMutations) <
parseInt(self.surkuConfig.minMutations)
) {
console.log("Set max mutations is smaller than set min mutations.");
process.exit(2);
}
if (
process.argv[process.argv.length - 2] !== "-o" &&
process.argv[process.argv.length - 2] !== "--output"
) {
const inputPath = path.resolve(process.argv[process.argv.length - 1]);
debugPrint(`inputPath: ${inputPath}\n`, 5);
if (fs.existsSync(inputPath)) {
if (fs.statSync(inputPath).isDirectory()) {
this.surkuConfig.inputPath = inputPath;
} else {
const newInputPath = [];
for (let x = lastIndex; x < process.argv.length; x++) {
if (
fs.existsSync(process.argv[x]) &&
!fs.statSync(process.argv[x]).isDirectory()
)
newInputPath.push(path.resolve(process.argv[x]));
}
this.surkuConfig.inputFile = newInputPath;
}
} else {
console.log(`Input path ${inputPath} does not exist.`);
process.exit(2);
}
} else {
console.log("No input path given.");
process.exit(2);
}
return this.surkuConfig;
}
module.exports = cmd();