Repository navigation
Expand file tree
/
Copy patha8.html
More file actions
103 lines (99 loc) · 5.75 KB
/
Copy patha8.html
File metadata and controls
103 lines (99 loc) · 5.75 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
{% extends "./layout.html" %} {% block title %}A8-Cross-Site Request Forgery (CSRF) {% endblock %} {% block content %}
<div class="row">
<div class="col-lg-12">
<div class="bs-example" style="margin-bottom: 40px;">
<span class="label label-warning">Exploitability: AVERAGE</span>
<span class="label label-warning">Prevalence: COMMON</span>
<span class="label label-danger">Detectability: EASY</span>
<span class="label label-warning">Technical Impact: MODERATE</span>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Description</h3>
</div>
<div class="panel-body">
A CSRF attack forces a logged-on victim’s browser to send a forged HTTP request, including the victim’s session cookie and any other automatically included authentication information, to a vulnerable web application. This allows the attacker to force the victim’s browser to generate requests that the vulnerable application processes as legitimate requests from the victim.
</div>
</div>
<!--
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Real World Attack Incident Examples</h3>
</div>
<div class="panel-body">
Screencast here ...
</div>
</div>
-->
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Attack Mechanics</h3>
</div>
<div class="panel-body">
<p>
As browsers automatically send credentials like session cookies with HTTP requests to the server where cookies were received from, attackers can create malicious web pages which generate forged requests that are indistinguishable from legitimate ones.</p>
<p>For example, CSRF vulnerability can be exploited on profile form on the insecure demo application.</p>
<iframe width="560" height="315" src="//www.youtube.com/embed/vRDykS_2y3I?rel=0" frameborder="0" allowfullscreen></iframe>
<p>To exploit it:
<ol>
<li>An attacker would need to host a forged form like below on a malicious sever.
<pre>
<html lang="en">
<head></head>
<body>
<form method="POST" action="http://TARGET_APP_URL_HERE/profile">
<h1> You are about to win a brand new iPhone!</h1>
<h2> Click on the win button to claim it...</h2>
<input type="hidden" name="bankAcc" value="9999999"/>
<input type="hidden" name="bankRouting" value="88888888"/>
<input type="submit" value="Win !!!"/>
</form>
</body>
</html>
</pre> Note: A sample app containing form for CSRF attack on NodeGoat app is available <a target="_blank" href="https://github.com/ckarande/nodegoat-csrf-attack">here</a>.
</li>
<li>Next, attacker would need to manage opening the form on logged in victim's browser and attract user to submit it. When user submits this form, it results in victim user's browser sending a malicious request to vulnerable server, causing CSRF attack.
</li>
</ol>
</p>
</div>
</div>
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">How Do I Prevent It?</h3>
</div>
<div class="panel-body">
<p>Express csrf middleware provides a very effective way to deal with csrf attack. By default this middleware generates a token named "_csrf" which should be added to requests which mutate state (PUT, POST, DELETE), within a hidden form field, or query-string, or header fields.</p>
<p>If using method-override middleware, it is very important that it is used before any middleware that needs to know the method of the request, including CSRF middleware. Otherwise an attacker can use non-state mutating methods (such as GET) to bypass the CSRF middleware checks, and use method override header to convert request to desired method.</p>
<p>When form is submitted, the middleware checks for existence of token and validates it by matching to the generated token for the response-request pair. If tokens do not match, it rejects the request. Thus making it really hard for an attacker to exploit CSRF.
</p>
</div>
</div>
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Source Code Example</h3>
</div>
<div class="panel-body">
The
<code>server.js</code>includes the express CSRF middleware after session is initialized. Then creates a custom middleware to generate new token using
<code>req.csrfToken();</code>and exposes it to view by setting it in
<code>res.locals</code>
<pre>
//Enable Express csrf protection
app.use(express.csrf());
app.use(function(req, res, next) {
res.locals.csrftoken = req.csrfToken();
next();
}); </pre> Next, this token can be included in a hidden form field in
<code>views/profile.html</code>as below.
<pre>
<input type="hidden" name="_csrf" value="{{ csrftoken } }"></pre>
</div>
</div>
</div>
</div>
{% endblock %}