-
Notifications
You must be signed in to change notification settings - Fork 3
/
Monolith.js
359 lines (297 loc) · 14.3 KB
/
Monolith.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
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
const fs = require('fs');
const parser = require('@solidity-parser/parser');
const treeify = require('treeify');
const colors = require('colors');
const { performance } = require('perf_hooks');
function parse(file) {
var start = performance.now();
let node_kind;
let alerts = [];
let info = []
const content = fs.readFileSync(file).toString('utf-8');
const ast = (() => {
try {
return parser.parse(content);
} catch (err) {
console.log(`Error found while parsing the following file: ${file}`);
throw err;
}
})();
console.log(ast.children[1])
//console.log(treeify.asTree(ast, true));
noColorOutput = false;
let uses_totalSupply = false;
parser.visit(ast, {
ContractDefinition(node) {
const name = node.name;
let bases = node.baseContracts.map(spec => {
return spec.baseName.namePath;
}).join(', ');
bases = bases.length ?
noColorOutput ?
`(${bases})`
: `(${bases})`.gray
: '';
let specs = '';
if (node.kind === 'library') {
specs += noColorOutput ? '[Lib]' : '[Lib]'.yellow;
} else if (node.kind === 'interface') {
specs += noColorOutput ? '[Int]' : '[Int]'.blue;
}
console.log(` + ${specs} ${name} ${bases}`);
node_kind = node.kind;
},
'ContractDefinition:exit': function (node) {
console.log('');
},
StateVariableDeclaration(node){
//find _totalSupply declaration probably can be done better
if(node_kind == 'contract'){
try{
for (let v of node.variables) {
if (v.name == '_totalSupply') {
uses_totalSupply = true
console.log("using total supply")
console.log(`Uses uint256 _totalSupply: ${uses_totalSupply}`.green)
}else{
}
}
}catch(e){
console.log(e)
}
}
},
FunctionDefinition(node) {
let name;
if (node.isConstructor) {
name = noColorOutput ? '<Constructor>' : '<Constructor>'.gray;
} else if (node.isFallback) {
name = noColorOutput ? '<Fallback>' : '<Fallback>'.gray;
} else if (node.isReceiveEther) {
name = noColorOutput ? '<Receive Ether>' : '<Receive Ether>'.gray;
} else {
name = node.name;
}
let spec = '';
if (node.visibility === 'public' || node.visibility === 'default') {
spec += noColorOutput ? '[Pub]' : '[Pub]'.green;
} else if (node.visibility === 'external') {
spec += noColorOutput ? '[Ext]' : '[Ext]'.blue;
} else if (node.visibility === 'private') {
spec += noColorOutput ? '[Prv]' : '[Prv]'.red;
} else if (node.visibility === 'internal') {
spec += noColorOutput ? '[Int]' : '[Int]'.gray;
}
let payable = '';
if (node.stateMutability === 'payable') {
payable = noColorOutput ? ' ($)' : ' ($)'.yellow;
}
let mutating = '';
if (!node.stateMutability) {
mutating = noColorOutput ? ' #' : ' #'.red;
}
let modifiers = ''
for (let m of node.modifiers) {
if (!!modifiers) modifiers += ','
modifiers += m.name
}
try {
let test = node.body.statements;
} catch (e) {
if (e.name != 'TypeError') {
console.log(`[ERROR] at no statemetns detections at ${node.name}`.red)
}
console.log(`Using Contract as Interface ${node.name}`)
return
}
// search for totalSupply getter. to be removed, is useless
if (node.name == 'totalSupply' && node_kind== 'contract'){
for (let s of node.body.statements){
if (!(s.expression.name == '_totalSupply')){
alerts.push(`[Not using _totalSupply" possible hidden mint`.red)
}
else{
has_totalSupply = true
console.log(`Uses _totalSupply: ${has_totalSupply}`.green)
}
}
}
// check the statements on a function vs a whitelist if not in it then alert is sent
function checkExtraStatements(node_name, white_list, statements) {
for (let s of statements) {
if (!white_list.includes(s.type)) {
alerts.push(`[Extra Statement "${s.type}" on ${node_name}]`.red)
}
}
}
// Check number of statements on transferFrom
if (node.name == 'transferFrom' && node_kind != 'interface') {
//number_statements = Object.keys(node.body.statements).length;
let transferFrom_white_list = ["ExpressionStatement", "EmitStatement", "ReturnStatement"]
checkExtraStatements(node.name, transferFrom_white_list, node.body.statements)
}
// Check extra statements on _approve TODO: create modular function
if (node.name == "_approve" && node_kind != 'interface') {
//number_statements = Object.keys(node.body.statements).length;
let approve_white_list = ["ExpressionStatement", "EmitStatement", "ReturnStatement"]
checkExtraStatements(node.name, approve_white_list, node.body.statements)
}
// Check extra modifiers on _transfer TODO: create modular function
if (node.name == "_transfer" && node_kind != 'interface') {
for (let m of node.modifiers) {
if (!!modifiers)
alerts.push(`[Extra Modifier "${m.name}" on _transfer]`.red)
}
}
// find mint functions declarations
let mint_names = ["mint", "_mint", "Mint"]
if (mint_names.includes(node.name) && node_kind == 'contract') {
alerts.push(`[Mint Function] ${node.name}`.red)
}
// Search for Hidden Mints (+, and add()) on any function other that the whitelisted
if (node.type == 'FunctionDefinition' && node.name != null && node_kind == 'contract') {
let sums = 0;
let subs = 0;
console.log(node.name)
for (let s of node.body.statements) {
// Search statements of the node for a 'add' operator
let isIndexAccess = '';
let indexedBase = '';
let indexedindex = '';
try{
//Check left for IndexAccess
isIndexAccess = (s.expression.left.type == 'IndexAccess') ? true : false;
indexedBase = s.expression.left.base.name;
indexedindex = s.expression.left.index.name;
if (indexedindex == undefined) { indexedindex = s.expression.left.index.type}
sums += checkForAddition(isIndexAccess, indexedBase, indexedindex)
subs += checkForSubtraction(isIndexAccess, indexedBase, indexedindex)
} catch (e) {
if (e.name != 'TypeError') {
console.log(`[ERROR] Left IndexAccesCheck at ${node.name}`.red)
}
}
function checkForAddition(isIndexAccess, indexedBase, indexedindex) {
let additions = 0;
try {
//Check right for add
let isAdd = (s.expression.right.expression.memberName == 'add') ? true : false;
if (isIndexAccess && isAdd) {
info.push(`[Addition in "${node.name}"]: IndexAccess ${indexedBase}[${indexedindex}] and add()`.gray)
additions++
}
} catch (e) {
if (e.name != 'TypeError') {
console.log(`[ERROR] Add() check at ${node.name}`.red)
}
}
try {
//Check right for operator +
let isSum = (s.expression.right.operator == '+') ? true : false;
if (isIndexAccess && isSum) {
info.push(`[Addition in "${node.name}"]: IndexAccess ${indexedBase}[${indexedindex}] and '+'`.gray)
additions++
}
//let isSum = (s.expression.right.operator == '+')
} catch (e) {
if (e.name != 'TypeError') {
console.log(`[ERROR] '+' check at ${node.name}`.red)
}
}
try {
//Check sum and var update +=
let isSumUpdate = (s.expression.operator == '+=') ? true : false;
if (isIndexAccess && isSumUpdate) {
info.push(`[Addition in "${node.name}"]: IndexAccess ${indexedBase}[${indexedindex}] and '+='`.gray)
additions++
}
//let isSum = (s.expression.right.operator == '+')
} catch (e) {
if (e.name != 'TypeError') {
console.log(`[ERROR] '+=' check at ${node.name}`.red)
}
}
return additions
}
function checkForSubtraction(isIndexAccess, indexedBase, indexedindex) {
let substractions = 0;
try {
//Check right for sub
let isSub = (s.expression.right.expression.memberName == 'sub') ? true : false;
if (isIndexAccess && isSub) {
info.push(`[Subtraction in "${node.name}"]: IndexAccess ${indexedBase}[${indexedindex}] and sub()`.gray)
substractions++
}
} catch (e) {
if (e.name != 'TypeError') {
console.log(`[ERROR] sub() check at ${node.name}`.red)
}
}
try {
//Check right for operator +
let isMinus = (s.expression.right.operator == '-') ? true : false;
if (isIndexAccess && isMinus) {
info.push(`[Subtraction in "${node.name}"]: IndexAccess ${indexedBase}[${indexedindex}] and '-'`.gray)
substractions++
}
//let isSum = (s.expression.right.operator == '+')
} catch (e) {
if (e.name != 'TypeError') {
console.log(`[ERROR] '-' check at ${node.name}`.red)
}
}
try {
//Check sum and var update +=
let isMinusUpdate = (s.expression.operator == '-=') ? true : false;
if (isIndexAccess && isMinusUpdate) {
info.push(`[Subtraction in "${node.name}"]: IndexAccess ${indexedBase}[${indexedindex}] and '-='`.gray)
substractions++
}
//let isSum = (s.expression.right.operator == '+')
} catch (e) {
if (e.name != 'TypeError') {
console.log(`[ERROR] '-=' check at ${node.name}`.red)
}
}
return substractions
}
}
if(sums > subs){
alerts.push(`[Hidden Mint] at "${node.name}" {${sums} sums | ${subs} subtractions}`.red)
}
if (sums < subs) {
alerts.push(`[Hidden Burn] at "${node.name}" {${sums} sums | ${subs} subtractions}`.red)
}
//console.log(`${sums} sums | ${subs} subtractions on ${node.name}`)
}
console.log(` - ${spec} ${name}${payable}${mutating}`)
if (!!modifiers) {
console.log(` - modifiers: ${modifiers}`)
}
}
});
if (!uses_totalSupply) {
alerts.push(`[Possible Hidden Mint] Not using _totalSupply variable`.red)
}
// Print a legend for symbols being used
let mutationSymbol = noColorOutput ? ' #' : ' #'.red;
let payableSymbol = noColorOutput ? ' ($)' : ' ($)'.yellow;
let alertSymbol = noColorOutput ? ' (!!!)' : ' (!!!)'.brightRed;
let infoSymbol = noColorOutput ? ' (???)' : ' (???)'.gray;
console.log(`${alertSymbol} Findings${alertSymbol}\n`)
for (var a in alerts) {
console.log(' ', alerts[a])
}
console.log(`\n${infoSymbol} Info${infoSymbol}\n`)
for (var i in info) {
console.log(' ', info[i])
}
console.log(`
${payableSymbol} = payable function
${mutationSymbol} = non-constant function
`);
var end = performance.now();
console.log('Run Time ' + (end - start) + ' ms.');
}
parse('contracts/supply.sol');
//parse('contracts/ERC20.sol');