-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.ts
356 lines (294 loc) · 10.6 KB
/
engine.ts
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
import { generate } from "escodegen";
import { parseScript } from "esprima";
import * as estree from "estree";
import Scope from "./scopes";
import highlight from "cli-highlight";
type Statement = estree.Statement | estree.Declaration | estree.ModuleDeclaration;
// A sink is a potentially dangerous JavaScript function that can cause
// undesirable effects if attacker controlled data is passed to it. Basically, if the
// function returns input back to the screen as output without security checks,
// it’s considered a sink. An example of this would be the “innerHTML”
// property as that changes the contents of the HTML page to
// whatever is given to it.
// The goal is to detect any sources being passed to sinks
const SINK_VARIABLE = /(?:document\.domain|innerHTML|outerHTML|insertAdjacentHTML|onevent)$/;
const isSinkVariable = (input: string) => {
return SINK_VARIABLE.test(input)
}
const SINK_FUNCTION = /document\.write(In)?/;
const isSinkFunction = (input: string) => {
return SINK_FUNCTION.test(input)
}
export class Analyzer {
program: Statement[];
scope: Scope;
constructor(source: string) {
this.program = parseScript(source).body;
this.scope = new Scope();
}
run() {
this.program.forEach(stmt => {
this.statement(stmt)
})
}
private statement(stmt: Statement) {
switch (stmt.type) {
case 'ExpressionStatement':
this.expression(stmt.expression);
break;
case 'BlockStatement':
this.blockStatement(stmt)
break;
case 'FunctionDeclaration':
this.functionDeclaration(stmt);
break;
case 'DoWhileStatement':
this.doWhileStatement(stmt);
break;
case 'ForStatement':
this.forStatement(stmt);
break;
case 'ForOfStatement':
this.statement(stmt.body);
break;
case 'ForInStatement':
this.statement(stmt.body);
break;
case 'IfStatement':
this.ifStatement(stmt);
break;
case 'LabeledStatement':
this.statement(stmt.body);
break;
case 'SwitchStatement':
this.switchStatement(stmt);
break;
case 'TryStatement':
this.tryStatement(stmt);
break;
case 'VariableDeclaration':
this.variableDeclaration(stmt);
break;
}
}
private variableDeclaration(stmt: estree.VariableDeclaration) {
stmt.declarations.forEach(declaration => {
if (declaration.id.type === 'Identifier') {
const id = declaration.id.name;
let sources: string[] = []
if (declaration.init) {
sources = this.expression(declaration.init)
}
this.scope.createVariable(id, false);
if (sources.length !== 0) {
this.scope.markSource(id, sources, stmt, false);
}
}
})
}
private tryStatement(stmt: estree.TryStatement) {
this.blockStatement(stmt.block);
stmt.handler && this.statement(stmt.handler?.body);
stmt.finalizer && this.statement(stmt.finalizer);
}
private switchStatement(stmt: estree.SwitchStatement) {
stmt.cases.forEach(c => {
c.consequent.forEach(cons => {
this.statement(cons);
})
})
}
private ifStatement(stmt: estree.IfStatement) {
stmt.consequent && this.statement(stmt.consequent);
stmt.alternate && this.statement(stmt.alternate);
}
private forStatement(stmt: estree.ForStatement) {
if (stmt.init) {
if (stmt.init.type === 'VariableDeclaration') {
this.variableDeclaration(stmt.init)
} else {
this.expression(stmt.init)
}
}
if (stmt.test) {
this.expression(stmt.test);
}
if (stmt.update) {
this.expression(stmt.update);
}
this.statement(stmt.body);
}
private doWhileStatement(stmt: estree.DoWhileStatement) {
this.expression(stmt.test);
this.statement(stmt.body);
}
private functionDeclaration(stmt: estree.FunctionDeclaration) {
const id = stmt.id?.name;
const args = stmt.params.map(param => generate(param))
const sinked = this.blockStatement(stmt.body, stmt, args);
if (id) {
const argList = args.map(arg => {
return sinked.includes(arg)
})
this.scope.markSink(id, argList);
}
}
private blockStatement(stmt: estree.BlockStatement, node?: estree.Expression | Statement, args?: string[]): string[] {
const prevScope = this.scope;
this.scope = new Scope(this.scope);
if (args && node) {
args?.forEach(arg => {
this.scope.createVariable(arg, true)
this.scope.markSource(arg, [], node, true)
})
}
stmt.body.forEach(stmt => {
this.statement(stmt);
})
const sinked = this.scope.sinked;
this.scope = prevScope;
return sinked;
}
// returns controlled sources in an expression
private expression(expr: estree.Expression): string[] {
switch (expr.type) {
case 'Identifier':
return this.identifier(expr);
case 'AssignmentExpression':
return this.assignment(expr);
case 'CallExpression':
return this.callExpression(expr);
case 'NewExpression':
return this.newExpression(expr);
case 'TemplateLiteral':
return this.templateLiteral(expr);
case 'MemberExpression':
return this.memberExpression(expr);
case 'UpdateExpression':
return this.expression(expr.argument);
case 'AwaitExpression':
return this.expression(expr.argument);
case 'UnaryExpression':
return this.expression(expr.argument);
case 'BinaryExpression':
case 'LogicalExpression':
return this.binaryExpression(expr);
case 'ConditionalExpression':
return this.conditionalExpression(expr);
case 'SequenceExpression':
return this.sequenceExpression(expr);
default:
return [];
}
}
private sequenceExpression(expr: estree.SequenceExpression): string[] {
return expr.expressions
.map(seq => this.expression(seq))
.flat();
}
private conditionalExpression(expr: estree.ConditionalExpression) {
const consequent = this.expression(expr.consequent);
const alternate = this.expression(expr.alternate);
return [...consequent, ...alternate];
}
private binaryExpression(expr: estree.BinaryExpression | estree.LogicalExpression): string[] {
const left = this.expression(expr.left);
const right = this.expression(expr.right);
return [...left, ...right];
}
private newExpression(expr: estree.NewExpression): string[] {
const sources = [];
for (const arg of expr.arguments) {
if (arg.type != 'SpreadElement') {
sources.push(...this.expression(arg))
}
}
return sources;
}
private memberExpression(expr: estree.MemberExpression): string[] {
let sources: string[] = [];
if (expr.object.type !== 'Super') {
sources.push(...this.expression(expr.object));
}
if (expr.property.type !== 'PrivateIdentifier') {
sources.push(...this.expression(expr.property));
}
const str = generate(expr);
if(this.isSource(str)) {
sources.push(str);
}
return sources;
}
private templateLiteral(expr: estree.TemplateLiteral): string[] {
return expr.expressions
.map(e => this.expression(e))
.flat()
}
private callExpression(expr: estree.CallExpression): string[] {
const sources = expr.arguments.map(arg => {
if(arg.type !== 'SpreadElement') {
const sourcesInArg = this.expression(arg)
return sourcesInArg
}
return []
}).flat()
if(expr.callee.type !== 'Identifier' && expr.callee.type !== 'Super') {
sources.push(...this.expression(expr.callee))
}
const callee = generate(expr.callee);
if (isSinkFunction(callee)) {
this.report(sources, callee, expr);
return sources;
}
const markedArgs = this.scope.findSink(callee);
if (markedArgs) {
expr.arguments.forEach((arg, i) => {
if(arg.type === 'SpreadElement') return;
const sourcesInArg = this.expression(arg)
if (markedArgs[i] === true && sourcesInArg.length > 0 ) {
this.report(sourcesInArg, callee, expr);
}
})
}
return sources;
}
private assignment(expr: estree.AssignmentExpression): string[] {
const left = generate(expr.left)
const sources = this.expression(expr.right);
if (sources.length === 0) return [];
if (isSinkVariable(left)) {
this.report(sources, left, expr);
return [];
}
this.scope.markSource(left, sources, expr, false);
return [left]
}
private identifier(expr: estree.Identifier): string[] {
const id = expr.name;
if (this.isSource(id)) {
return [id];
}
return [];
}
private isSource(id: string): boolean {
return this.scope.isSource(id);
}
private report(sources: string[], sink: string, node: Statement | estree.Expression) {
sources.forEach(source => {
this.scope.markSinked(source);
})
console.log('--------------------------------')
logNode(node)
console.log('Controlled sources: ',sources.join(','));
console.log('Sink: ', sink);
}
}
function logNode(node: Statement | estree.Expression) {
const code = generate(node);
const config = {
language: 'javascript',
ignoreIllegals: true
}
const highlighted = highlight(code,config)
console.log(highlighted)
}