SQLI attacks PHP Source Code Automatic Creation of


![PHP Web applications $_GET[] <HTML> … <script> … PHP application $_POST[] http: //www. example. PHP Web applications $_GET[] <HTML> … <script> … PHP application $_POST[] http: //www. example.](https://slidetodoc.com/presentation_image_h/ca5777f2ee9fd52f5b26530453bdde23/image-3.jpg)
![Example: Message board (add mode) if ($_GET['mode'] == "add") add. Message. For. Topic(); else Example: Message board (add mode) if ($_GET['mode'] == "add") add. Message. For. Topic(); else](https://slidetodoc.com/presentation_image_h/ca5777f2ee9fd52f5b26530453bdde23/image-4.jpg)
![Example: Message board (display mode) if ($_GET['mode'] == "add") add. Message. For. Topic(); else Example: Message board (display mode) if ($_GET['mode'] == "add") add. Message. For. Topic(); else](https://slidetodoc.com/presentation_image_h/ca5777f2ee9fd52f5b26530453bdde23/image-5.jpg)
![SQL injection attack $_GET[]: if ($_GET['mode'] == mode = “display” "add") topic. ID = SQL injection attack $_GET[]: if ($_GET['mode'] == mode = “display” "add") topic. ID =](https://slidetodoc.com/presentation_image_h/ca5777f2ee9fd52f5b26530453bdde23/image-6.jpg)
![First-order XSS attack if ($_GET['mode'] == "add") add. Message. For. Topic(); function add. Message. First-order XSS attack if ($_GET['mode'] == "add") add. Message. For. Topic(); function add. Message.](https://slidetodoc.com/presentation_image_h/ca5777f2ee9fd52f5b26530453bdde23/image-7.jpg)
![Second-order XSS attack $_GET[]: mode = “add” msg = MALICIOUS topic. ID = 42 Second-order XSS attack $_GET[]: mode = “add” msg = MALICIOUS topic. ID = 42](https://slidetodoc.com/presentation_image_h/ca5777f2ee9fd52f5b26530453bdde23/image-8.jpg)
![Second-order XSS attack $_GET[]: mode = “add” msg = MALICIOUS topic. ID = 42 Second-order XSS attack $_GET[]: mode = “add” msg = MALICIOUS topic. ID = 42](https://slidetodoc.com/presentation_image_h/ca5777f2ee9fd52f5b26530453bdde23/image-9.jpg)


![Input generation: concolic execution if ($_GET['mode'] == "add") add. Message. For. Topic(); else if Input generation: concolic execution if ($_GET['mode'] == "add") add. Message. For. Topic(); else if](https://slidetodoc.com/presentation_image_h/ca5777f2ee9fd52f5b26530453bdde23/image-12.jpg)













- Slides: 25

SQLI attacks PHP Source Code Automatic Creation of SQL Injection and Cross-Site Scripting Attacks 1 st-order XSS attacks 2 nd-order XSS attacks Adam Kiezun, Philip J. Guo, Karthick Jayaraman, Michael D. Ernst International Conference on Software Engineering May 20, 2009

Overview Problem: Finding security vulnerabilities (SQLI and XSS) in Web applications Approach: 1. Automatically generate inputs 2. Dynamically track taint 3. Mutate inputs to produce exploits Results: 60 unique new vulnerabilities in 5 PHP applications, first to create 2 nd-order XSS, no false positives
![PHP Web applications GET HTML script PHP application POST http www example PHP Web applications $_GET[] <HTML> … <script> … PHP application $_POST[] http: //www. example.](https://slidetodoc.com/presentation_image_h/ca5777f2ee9fd52f5b26530453bdde23/image-3.jpg)
PHP Web applications $_GET[] <HTML> … <script> … PHP application $_POST[] http: //www. example. com/register. php? name=Bob&age=25 Database Data HTML SQL URL PHP on webserver Web browser
![Example Message board add mode if GETmode add add Message For Topic else Example: Message board (add mode) if ($_GET['mode'] == "add") add. Message. For. Topic(); else](https://slidetodoc.com/presentation_image_h/ca5777f2ee9fd52f5b26530453bdde23/image-4.jpg)
Example: Message board (add mode) if ($_GET['mode'] == "add") add. Message. For. Topic(); else if ($_GET[‘mode’] == “display”) display. All. Messages. For. Top ic(); else die(“Error: invalid mode”); $_GET[]: mode = “add” msg = “hi there” topic. ID = 42 poster = “Bob” Thanks for posting, Bob function add. Message. For. Topic() { $my_msg = $_GET['msg']; $my_topic. ID = $_GET['topic. ID']; $my_poster = $_GET['poster']; $sqlstmt = ” INSERT INTO messages VALUES('$my_msg’ , '$my_topic. ID') "; $result = mysql_query($sqlstmt);
![Example Message board display mode if GETmode add add Message For Topic else Example: Message board (display mode) if ($_GET['mode'] == "add") add. Message. For. Topic(); else](https://slidetodoc.com/presentation_image_h/ca5777f2ee9fd52f5b26530453bdde23/image-5.jpg)
Example: Message board (display mode) if ($_GET['mode'] == "add") add. Message. For. Topic(); else if ($_GET[‘mode’] == “display”) function display. All. Messages. For. Topic() { ic(); $my_topic. ID = $_GET['topic. ID']; else $sqlstmt = ” SELECT msg FROM die(“Error: invalid messages WHERE mode”); topic. ID='$my_topic. ID’ "; $_GET[]: $result = mysql_query($sqlstmt); mode = “display” topic. ID = 42 Message: hi there while($row = mysql_fetch_assoc($result)) { echo "Message: ". $row['msg'];
![SQL injection attack GET if GETmode mode display add topic ID SQL injection attack $_GET[]: if ($_GET['mode'] == mode = “display” "add") topic. ID =](https://slidetodoc.com/presentation_image_h/ca5777f2ee9fd52f5b26530453bdde23/image-6.jpg)
SQL injection attack $_GET[]: if ($_GET['mode'] == mode = “display” "add") topic. ID = 1' OR add. Message. For. Topic(); '1'='1 else if ($_GET[‘mode’] == “display”) function display. All. Messages. For. Topic() { ic(); $my_topic. ID = $_GET['topic. ID']; else $sqlstmt = ” SELECT msg FROM die(“Error: invalid messages WHERE mode”); topic. ID='$my_topic. ID’ "; $result = mysql_query($sqlstmt); while($row = mysql_fetch_assoc($result)) { "Message: . SELECT msg FROM messages WHERE echo topic. ID='1' OR "'1'='1' $row['msg'];
![Firstorder XSS attack if GETmode add add Message For Topic function add Message First-order XSS attack if ($_GET['mode'] == "add") add. Message. For. Topic(); function add. Message.](https://slidetodoc.com/presentation_image_h/ca5777f2ee9fd52f5b26530453bdde23/image-7.jpg)
First-order XSS attack if ($_GET['mode'] == "add") add. Message. For. Topic(); function add. Message. For. Topic() { $my_poster = $_GET['poster']; […] echo "Thanks for posting, $my_poster"; } $_GET[]: mode = “add” msg = “hi there” topic. ID = 42 poster = MALICIOUS Example MALICIOUS input: “uh oh<script>alert(‘XSS’)</script>” Thanks for posting, uh oh
![Secondorder XSS attack GET mode add msg MALICIOUS topic ID 42 Second-order XSS attack $_GET[]: mode = “add” msg = MALICIOUS topic. ID = 42](https://slidetodoc.com/presentation_image_h/ca5777f2ee9fd52f5b26530453bdde23/image-8.jpg)
Second-order XSS attack $_GET[]: mode = “add” msg = MALICIOUS topic. ID = 42 poster = “Villain” Attacker’s input Example MALICIOUS input: “uh oh<script>alert(‘XSS’)</script>” add. Message. For. Topic() PHP application Database
![Secondorder XSS attack GET mode add msg MALICIOUS topic ID 42 Second-order XSS attack $_GET[]: mode = “add” msg = MALICIOUS topic. ID = 42](https://slidetodoc.com/presentation_image_h/ca5777f2ee9fd52f5b26530453bdde23/image-9.jpg)
Second-order XSS attack $_GET[]: mode = “add” msg = MALICIOUS topic. ID = 42 poster = “Villain” Attacker’s input Example MALICIOUS input: “uh oh<script>alert(‘XSS’)</script>” add. Message. For. Topic() PHP application Database $_GET[]: mode = “display” topic. ID = 42 Victim’s input display. All. Messages. For. Topic() echo() Message: uh oh

Architecture Input Generator inputs PHP Source Code Taint Propagator Concrete + Symbolic Database taint sets Attack Generator/Checker Ardilla Malicious inputs

Input generation Input Generator inputs PHP Source Code Goal: Create a set of concrete inputs (_$GET[] & _$POST[]) We use Apollo generator (Artzi et al. ’ 08), based on concolic execution
![Input generation concolic execution if GETmode add add Message For Topic else if Input generation: concolic execution if ($_GET['mode'] == "add") add. Message. For. Topic(); else if](https://slidetodoc.com/presentation_image_h/ca5777f2ee9fd52f5b26530453bdde23/image-12.jpg)
Input generation: concolic execution if ($_GET['mode'] == "add") add. Message. For. Topic(); else if ($_GET[‘mode’] == “display”) display. All. Messages. For. Top ic(); else die(“Error: invalid mode”); $_GET[]: mode = “ 1” msg = “ 1” topic. ID = 1 poster = “ 1” $_GET[]: mode = “add” msg = “ 1” topic. ID = 1 poster = “ 1” PHP Source Code Input Generator inputs $_GET[]: mode = “display” msg = “ 1” topic. ID = 1 poster = “ 1”

Example: SQL injection attack 1. Generate inputs until program reaches an SQL statement SELECT msg FROM messages WHERE topic. ID='$my_topic. ID‘ function display. All. Messages. For. Topic() { $my_topic. ID = $_GET['topic. ID']; $sqlstmt = ” SELECT msg FROM messages WHERE topic. ID='$my_topic. ID’ "; $result = mysql_query($sqlstmt); $_GET[]: mode = “display” msg = “ 1” topic. ID = 1 poster = “ 1”

Taint propagation inputs PHP Source Code Taint Propagator taint sets Goal: Determine which input variables affect each potentially dangerous value Concrete + Symbolic Database inputs Technique: Execute and track data-flow from input variables to sensitive sinks Sensitive sinks: mysql_query(), echo(), print()

Taint propagation: data-flow Each value has a taint set, which contains input variables whose values flow into it inputs PHP Source Code Taint Propagator Concrete + Symbolic Database taint sets function display. All. Messages. For. Topic() { $my_topic. ID = $_GET['topic. ID']; $sqlstmt = ” SELECT msg FROM messages WHERE topic. ID='$my_topic. ID’ "; $result = mysql_query($sqlstmt); Taint propagation /* {‘topic. ID’} */ • Assignments: $my_poster Taint set Sensitive sink = $_GET[“poster”] • String concatenation: $full_n = $first_n. $last_n • PHP built-in functions: $z = foo($x, $y) • Database operations (for 2 nd-order XSS)

Example: SQL injection attack 1. Generate inputs until program reaches an SQL statement SELECT msg FROM messages WHERE topic. ID='$my_topic. ID‘ 2. Collect taint sets for values in sensitive sinks: { ’topic. ID’ } function display. All. Messages. For. Topic() { $my_topic. ID = $_GET['topic. ID']; $sqlstmt = ” SELECT msg FROM messages WHERE topic. ID='$my_topic. ID’ "; $result = mysql_query($sqlstmt); /* {‘topic. ID’} */ Taint set Sensitive sink

Attack generation and checking PHP Source Code Goal: Generate attacks for each sensitive sink inputs taint sets Attack Generator/C hecker Malicious inputs Technique: Mutate inputs into candidate attacks • Replace tainted input variables with shady strings developed by security professionals: • e. g. , “ 1’ or ‘ 1’=‘ 1”, “<script>code</script>” Alternative: String constraint solver (Kiezun et al. ’ 09)

Attack generation and checking inputs taint sets PHP Source Code Attack Generator/C hecker Malicious inputs Given a program, an input i, and taint sets for each var that reaches any sensitive sink: res = exec(program, i) Attack generation for shady in shady_strings: mutated_input = i. replace(var, shady) mutated_res = exec(program, mutated_input) if mutated_res DIFFERS FROM res: report mutated_input as attack Attack checking

Attack generation: mutating inputs res = exec(program, i) for shady in shady_strings: mutated_input = i. replace(var, shady) mutated_res = exec(program, mutated_input) if mutated_res DIFFERS FROM res: report mutated_input as attack $_GET[]: mode = “display” topic. ID = 1' OR '1'='1

Example: SQL injection attack 1. Generate inputs until program reaches an SQL statement SELECT msg FROM messages WHERE topic. ID='$my_topic. ID‘ 2. Collect taint sets for values in sensitive sinks: { ’topic. ID’ } 3. Generate attack candidate by picking a shady string $_GET[]: mode = “display” topic. ID = 1' OR '1'='1

Attack checking: diffing outputs res = exec(program, i) for shady in shady_strings: mutated_input = i. replace(var, shady) mutated_res = exec(program, mutated_input) if mutated_res DIFFERS FROM res: report mutated_input as attack What is a significant difference? • For SQLI: compare SQL parse tree structure • For XSS: compare HTML for additional scriptinducing elements (<script></script>) Avoids false positives from input sanitizing and filtering

Example: SQL injection attack 1. Generate inputs until program reaches an SQL statement SELECT msg FROM messages WHERE topic. ID='$my_topic. ID‘ 2. Collect taint sets for values in sensitive sinks: { ’topic. ID’ } 3. Generate attack candidate by picking a shady string 4. Check by mutating input and comparing SQL parse trees: innocuous: SELECT msg FROM messages WHERE topic. ID=‘ 1’ mutated: SELECT msg FROM messages WHERE topic. ID=‘ 1’ OR ‘ 1’=‘ 1’ 5. Report an attack since SQL parse tree structure differs

Experimental results Name Type LOC School. Mate School administration 8, 181 6, 765 Web. Chess Online chess 4, 722 38, 457 Faq. Forge Document creator 1, 712 15, 355 Game player tracker 915 1, 143 Bulletin board 326 366 EVE activity tracker gecc. BBlite Vulnerability Kind Source. Forge Downloads Sensitive sinks Reached sensitive sinks Unique attacks SQLI 366 91 23 1 st-order XSS 274 97 29 2 nd-order XSS 274 66 8 Main limitation: input generator Total: 60

Comparison with previous work Defensive coding: + : can completely solve problem if done properly - : must re-write existing code Static analysis: + : can potentially prove absence of errors - : false positives, does not produce concrete attacks Dynamic monitoring: + : can prevent all attacks - : runtime overhead, false positives affect app. behavior Random fuzzing: + : easy to use, produces concrete attacks - : creates mostly invalid inputs

Automatic Creation of SQL Injection and Cross-Site Scripting Attacks • Contributions – Automatically create SQLI and XSS attacks – First technique for 2 nd-order XSS • Technique – Dynamically track taint through both program and database – Input mutation and output comparison • Implementation and evaluation – Found 60 new vulnerabilities, no false positives