-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfuzz.js
More file actions
358 lines (270 loc) · 16 KB
/
Copy pathfuzz.js
File metadata and controls
358 lines (270 loc) · 16 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
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 web3 = new Web3(Web3.givenProvider || "ws://localhost:33333");
const numberOfContractsSeries = 4
let seriesInfo = JSON.parse(fs.readFileSync("params/seriesInfo.json"))
let runFuzzer = JSON.parse(fs.readFileSync("params/runFuzzer.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 current_tx_hash = null
let after_of_prev_tx_obj = null
let before_of_curr_tx_obj = null
let txBalanceInfo = Object()
Array(numberOfContractsSeries).fill().map(async (_, i) => {
let serieIndex = i + 1
let serieStringIndex = serieIndex.toString()
console.log(runFuzzer)
return await runFuzzer[serieStringIndex].forEach(async (fuzzString) => {
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 abi = contractTwoSerieInfo["contracts"]["main"]["abi"]
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()
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(">>>>> data: ")
//console.log(res);
//console.log("=====================================\n")
web3.eth.getTransaction(res).then(tx => {
//console.log(tx)
if (tx.from == accounts[randAcountIndex] && tx.to == contractTwoAddress && tx.gasPrice == randGasPrice) {
data.push({
"from": tx.from,
"to": tx.to,
"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, {
gasPrice: randGasPrice, 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_attacker_addr != 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) => {
console.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")
console.log(bal_obj)
console.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")
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) => {
console.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")
console.log(bal_obj)
console.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n")
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("\n*********************************************\n*********************************************\n*********************************************\n")
console.log("doing: " + fuzzString)
newAttakcerInstance.methods.startAttack(contractOneAddress).send({from:accounts[randAcountIndex]})
// let's set these for the next iteration
prev_attacker_addr = contractTwoAddress
prev_victim_addr = contractOneAddress
prev_tx_hash = current_tx_hash
})
} else {
// now let's execute this transaction
console.log("\n*******************-----*********************\n*********************************************\n*********************************************\n")
console.log("doing: " + fuzzString)
newAttakcerInstance.methods.startAttack(contractOneAddress).send({from:accounts[randAcountIndex]})
prev_attacker_addr = contractTwoAddress
prev_victim_addr = contractOneAddress
prev_tx_hash = current_tx_hash
}
}, txTimeCounter)
txTimeCounter = txTimeCounter + 60000
//Math.floor(Math.random() * 400000)
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
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")
});
}, 15000*pendingTXs.length)
}
})
}, 800000)