-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfuzz_2.js
More file actions
420 lines (311 loc) · 17.8 KB
/
Copy pathfuzz_2.js
File metadata and controls
420 lines (311 loc) · 17.8 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
const deploy = require('./deploy-solc-0.4')
const Web3 = require('web3')
const glob = require("glob")
const fs = require('fs')
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
let { exec } = require("child_process");
let web3 = new Web3(Web3.givenProvider || "ws://localhost:33333");
let seriesInfo = JSON.parse(fs.readFileSync("params/seriesInfo.json"))
let runFuzzer = JSON.parse(fs.readFileSync("params/runFuzzer.json"))
let tx_fuzz = JSON.parse(fs.readFileSync("params/tx_fuzz.json"))
let subscriptionHolder = Object()
let txTimeCounter = 0
const csvWriter = createCsvWriter({
path: 'data/out.csv',
header: [
{id: 'from', title: 'from'},
{id: 'to', title: 'to'},
{id: 'value', title: 'value'},
{id: 'gas', title: 'gas'},
{id: 'input', title: 'input'},
{id: 'tx_hash', title: 'tx_hash'},
{id: 'fuzz_string', title: 'fuzz_string'},
]
});
let data = []
let prev_victim_addr = null
let prev_attacker_addr = null
let prev_tx_hash = null
let prev_tx_string_index = null
let current_tx_hash = null
let after_of_prev_tx_obj = null
let before_of_curr_tx_obj = null
let txBalanceInfo = Object()
Object.entries(tx_fuzz).forEach(([key, tx_params]) => {
/*
fuzzArray = fuzzString.split(",")
var safety = fuzzArray[fuzzArray.length-1]
let fileNameOne = fuzzArray[0]
let fileNameTwo = fuzzArray[1]
let contractOneSerieInfo = seriesInfo[serieStringIndex][fileNameOne]
let contractTwoSerieInfo = seriesInfo[serieStringIndex][fileNameTwo]
*/
//TODO: change the following to something universal
//let contractOneAddress = contractOneSerieInfo["contracts"]["main"]["address"]
//let contractTwoAddress = contractTwoSerieInfo["contracts"]["main"]["address"]
let serieStringIndex = key
console.log(serieStringIndex)
let contractOneAddress = tx_fuzz[serieStringIndex]["victim"]["address"]
let contractTwoAddress = tx_fuzz[serieStringIndex]["attacker"]["address"]
let abi = tx_fuzz[serieStringIndex]["attacker"]["abi"]
let fuzzString = tx_fuzz[serieStringIndex]["files"] + "," + tx_fuzz[serieStringIndex]["label"] + "," + serieStringIndex
web3.eth.getAccounts((error,accounts) => {
if (error) {
console.log(error)
return 0
}
return accounts
}).then((accounts) => {
let randAcountIndex = Math.floor(Math.random() * 5)
//let randGasPrice = '12345' + Math.floor(Math.random() * 9).toString() + Math.floor(Math.random() * 9).toString() + Math.floor(Math.random() * 9).toString()
let randGasPrice = '12345678'
console.log('rand act index:')
console.log(randAcountIndex)
console.log('rand gas price:')
console.log(randGasPrice)
subscriptionHolder[fuzzString] = web3.eth.subscribe('pendingTransactions', function(error, result){
})
.on("connected", function(res){
/*
console.log("=>>>>>>>>>>>>>>>>>>>>>>>>> connected:")
console.log(res);
console.log("=====================================")
*/
})
.on("data", (res) => {
console.log("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n")
console.log(">>>>> res: ")
console.log(res);
console.log("=====================================\n")
web3.eth.getTransaction(res).then(tx => {
//console.log(tx)
if (tx.from == accounts[randAcountIndex] && tx.to == contractTwoAddress) {
data.push({
"from": contractTwoAddress,
"to": contractOneAddress,
"value": tx.value,
"gas": tx.gas,
"input": tx.input,
"tx_hash": tx.hash,
"fuzz_string": fuzzString
})
subscriptionHolder[fuzzString].unsubscribe((error) => {
console.log("Unsubscribing from " + fuzzString + ", Error is " + error)
})
current_tx_hash = tx.hash
} else {
console.log("\ntx is not what we want...: ")
//console.log(tx)
console.log("tx.from: " + tx.from + "\ntx.to: " + tx.to + "\nwe want from: " + contractTwoAddress + "\nto: " + contractOneAddress)
}
})
})
let newAttakcerInstance = new web3.eth.Contract(abi, contractTwoAddress, {
from: accounts[randAcountIndex]
})
setTimeout(() => {
// first let's get the balance of previous pair of contracts before we issue any other tx
console.log("Entering for :::: " + fuzzString)
if (prev_tx_hash != null) {
web3.eth.getBalance(prev_victim_addr)
.then(async (victim_bal) => {
return {
attacker_bal: await web3.eth.getBalance(prev_attacker_addr),
victim_bal: victim_bal
}
})
.then((bal_obj) => {
after_of_prev_tx_obj = bal_obj
})
.then(async () => {
return await web3.eth.getBalance(contractTwoAddress)
})
.then(async (attacker_bal) => {
return {
attacker_bal: attacker_bal,
victim_bal: await web3.eth.getBalance(contractOneAddress)
}
})
.then((bal_obj) => {
before_of_curr_tx_obj = bal_obj
if (txBalanceInfo[current_tx_hash]) {
txBalanceInfo[current_tx_hash]['before_tx'] = before_of_curr_tx_obj
} else {
txBalanceInfo[current_tx_hash] = Object()
txBalanceInfo[current_tx_hash]['before_tx'] = before_of_curr_tx_obj
}
if (txBalanceInfo[prev_tx_hash]) {
txBalanceInfo[prev_tx_hash]['after_tx'] = after_of_prev_tx_obj
} else {
txBalanceInfo[prev_tx_hash] = Object()
txBalanceInfo[prev_tx_hash]['after_tx'] = after_of_prev_tx_obj
}
})
.then(() => {
// now let's execute this transaction
console.log("doing: " + fuzzString)
// let's use prev_tx_hash here to get the debug_traceTransaction:
cmd = `curl localhost:8545 -X POST --header 'Content-type: application/json' --data '{"jsonrpc":"2.0", "method":"debug_traceTransaction", "params":["${prev_tx_hash}", {}], "id":1}' > data/trace_${prev_tx_string_index}.json`
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
newAttakcerInstance.methods.startAttack(contractOneAddress).send({from:accounts[randAcountIndex],
gas: 3000000,
handleRevert:true
}).on("error", (error) => {
console.log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \n ===============================================")
console.log(error)
console.log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \n ===============================================")
})
.then((current_tx_obj) => {
current_tx_hash = current_tx_obj["transactionHash"];
// let's set these for the next iteration
prev_attacker_addr = contractTwoAddress
prev_victim_addr = contractOneAddress
prev_tx_hash = current_tx_hash
})
prev_tx_string_index = serieStringIndex
})
} else {
console.log("this is the first transaction?!")
console.log(prev_tx_hash)
// now let's execute this transaction
console.log("\n*******************-----*********************\n*********************************************\n*********************************************\n")
console.log("doing: " + fuzzString)
newAttakcerInstance.methods.startAttack(contractOneAddress).send({
from:accounts[randAcountIndex],
gas: 3000000,
handleRevert:true
}).on("error", (error) => {
console.log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \n ===============================================")
console.log(error)
console.log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \n ===============================================")
})
.then((current_tx_obj) => {
current_tx_hash = current_tx_obj["transactionHash"];
prev_attacker_addr = contractTwoAddress
prev_victim_addr = contractOneAddress
prev_tx_hash = current_tx_hash
console.log(" ------------------------ We are inside tx callback -------------------")
console.log("current tx hash is : " + current_tx_hash)
prev_tx_string_index = serieStringIndex
})
}
}, txTimeCounter)
txTimeCounter = txTimeCounter + 30000
console.log(contractOneAddress)
console.log(contractTwoAddress)
console.log("--------------\n\n")
})
})
let timeCounter = 0;
setInterval(() => {
timeCounter++;
console.log("Elapsed time: " + timeCounter.toString())
}, 1000)
setTimeout(() => {
web3.eth.getPendingTransactions().then((pendingTXs) => {
if (pendingTXs.length == 0){
// no pending tx, assuming that everything is over
console.log("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n")
console.log(data)
console.log("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n")
csvWriter
.writeRecords(data)
.then(() => {
console.log("\n====================================================\n====================================================\n====================================================\n")
console.log("just wrote collected data to file")
console.log(txBalanceInfo)
// Let's do the following (as the after_tx is not recorded for the last tx)
web3.eth.getBalance(prev_victim_addr)
.then(async (victim_bal) => {
return {
attacker_bal: await web3.eth.getBalance(prev_attacker_addr),
victim_bal: victim_bal
}
})
.then((bal_obj) => {
console.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")
console.log(bal_obj)
console.log("and current tx hash is:")
console.log(current_tx_hash)
console.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")
after_of_prev_tx_obj = bal_obj
if (txBalanceInfo[current_tx_hash]) {
txBalanceInfo[current_tx_hash]['after_tx'] = after_of_prev_tx_obj
} else {
txBalanceInfo[current_tx_hash] = Object()
txBalanceInfo[current_tx_hash]['after_tx'] = after_of_prev_tx_obj
}
})
.then(() => {
// Let's write out the file:
const writableTXData = JSON.stringify(txBalanceInfo)
fs.writeFileSync('data/txBalanceInfo.json', writableTXData, (err) => {
if (err) {
console.log("Something bad happened:")
console.log(err)
} else {
console.log("JSON data saved successfully to txBalanceInfo.json file.")
}
})
})
console.log("\n====================================================\n====================================================\n====================================================\n")
});
} else {
// We're in trouble, and there are more tx's. Let's wait a bit more
setTimeout(() => {
csvWriter
.writeRecords(data)
.then(() => {
console.log("\n====================================================\n====================================================\n====================================================\n")
console.log("just wrote collected data to file")
console.log(txBalanceInfo)
// Let's do the following (as the after_tx is not recorded for the last tx)
web3.eth.getBalance(prev_victim_addr)
.then(async (victim_bal) => {
return {
attacker_bal: await web3.eth.getBalance(prev_attacker_addr),
victim_bal: victim_bal
}
})
.then((bal_obj) => {
console.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")
console.log(bal_obj)
console.log("and current tx hash is:")
console.log(current_tx_hash)
console.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")
after_of_prev_tx_obj = bal_obj
if (txBalanceInfo[current_tx_hash]) {
txBalanceInfo[current_tx_hash]['after_tx'] = after_of_prev_tx_obj
} else {
txBalanceInfo[current_tx_hash] = Object()
txBalanceInfo[current_tx_hash]['after_tx'] = after_of_prev_tx_obj
}
})
.then(() => {
// Let's write out the file:
const writableTXData = JSON.stringify(txBalanceInfo)
fs.writeFileSync('data/txBalanceInfo.json', writableTXData, (err) => {
if (err) {
console.log("Something bad happened:")
console.log(err)
} else {
console.log("JSON data saved successfully to txBalanceInfo.json file.")
}
})
})
console.log("\n====================================================\n====================================================\n====================================================\n")
});
}, 25000*pendingTXs.length)
}
})
}, 12300000)