-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathfuzz.js
More file actions
84 lines (74 loc) · 2.47 KB
/
Copy pathfuzz.js
File metadata and controls
84 lines (74 loc) · 2.47 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
var can = require('./init.js');
var crypto = require('crypto');
if (can.argv.h || can.argv.help) {
console.log("This tool is a very simple Fuzzer for CANbus.");
console.log("Tool will choose randomly (between 0x00 and 0x800 by default) and send random data of random length");
console.log("Optionally provide a start or end range");
can.cmdOptions.push({
opt: "--min",
desc: "Minimum ID for range"
}, {
opt: "--max",
desc: "Maximum ID for range"
}, {
opt: "--sleep",
desc: "Time to sleep between "
}, {
opt: "--basebuffer",
desc: "Buffer to use as a base for the CAN messages. Mutation will happen to this buffer. If no buffer is provided, completely random data will be used."
}, {
opt: "--mutationRate",
desc: "Chance, for each byte of the data, that it will be changed randomly. "
}, {
opt: "--mutateIndexMin",
desc: "minimum position allowed to mutate. Default 0"
}, {
opt: "--mutateIndexMax",
desc: "maximum position allowed to mutate. Default is the end of the buffer"
});
can.printOptions();
process.exit();
}
var minID = can.argv.min || 0x00;
var maxID = can.argv.max || 0x800;
var sleep = can.argv.sleep || 100;
var mutationRate = can.argv.mutationRate || 0.6;
console.log("Starting fuzz for IDs", "0x" + can.decToHex(minID), "-", "0x" + can.decToHex(maxID));
var mutateIndexMin = can.argv.mutateIndexMin || 0;
var mutateIndexMax = can.argv.mutateIndexMax || 8;
var basebuffer;
if (can.argv.basebuffer) {
basebuffer = can.argv.basebuffer || "0000000000000000";
basebuffer = basebuffer.toString();
var buf = new Buffer(basebuffer, 'hex');
basebuffer = buf;
if (!can.argv.mutateIndexMax) {
mutateIndexMax = basebuffer.length;
}
console.log("Using a base buffer of " + can.argv.basebuffer.toString() + " and a mutation rate of " + mutationRate);
console.log("Mutating positions " + mutateIndexMin + " through " + mutateIndexMax);
}
function mutate(target, rate, minIndex, maxIndex) {
var buf = new Buffer(target);
// console.log("old buffer", buf);
for (var x = minIndex; x <= maxIndex; x++) {
if (Math.random() < rate) {
buf[x] = Math.floor(Math.random() * 256);
}
}
// console.log("new buffer", buf);
return buf;
}
function sendMsg() {
var id = Math.floor(Math.random() * (maxID - minID + 1)) + minID;
var canmsg = {
id: id
};
if (basebuffer) {
canmsg.data = mutate(basebuffer, mutationRate, mutateIndexMin, mutateIndexMax);
} else {
canmsg.data = new Buffer(crypto.randomBytes(8));
}
can.send(canmsg);
}
setInterval(sendMsg, sleep);