web security sql injection, xss, csrf, parameter tampering, dos attacks, session hijacking softuni...

36
Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University http:// softuni.bg Web Development Basics

Upload: eric-park

Post on 04-Jan-2016

236 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

Web SecuritySQL Injecti on, XSS, CSRF, Parameter

Tampering, DoS Att acks, Session Hijacking

SoftUni TeamTechnical TrainersSoftware Universityhttp://softuni.bg

Web Development Basics

Page 2: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

Table of Contents

1. Web Security Main Concepts

2. SQL Injection

3. Cross-Site Scripting (XSS)

4. Cross-Site Request Forgery (CSRF/XSRF)

5. Parameter Tempering

6. Session Hijacking

7. DoS/DDoS Attacks

2

Page 3: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

Web Security Main Concepts

Page 4: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

4

Is Software Security a Feature? Most people consider software security as a necessary feature of a

product

Is Security Vulnerability a Bug? If the software "failed" and allowed a hacker to see personal info,

most users would consider that a software bug

Feature or Bug

Page 5: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

Reasons for Failures

Software failures usually happen spontaneously Without intentional mischief

Failures can be result of malicious attacks For the Challenge/Prestige Curiosity driven Aiming to use resources Vandalizing Stealing

5

Page 6: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

6

Maximum Simplicity More complicated – greater chance for mistakes

Secure the Weakest Link Hackers attack where the weakest link is

Limit the Publicly Available Resources Incorrect Until Proven Correct

Consider each user input as incorrect The Principle of the "Weakest Privilege" Security in Errors (Remain stable) Provide Constant Defense (also use backups)

Golden Rules!

Page 7: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

SQL InjectionWhat is SQL Injection and How to Prevent It?

Page 8: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

8

Try the following queries: ' crashes ' or ''=' Login with any user '; INSERT INTO Messages(MessageText, MessageDate) VALUES ('Hacked!!!', '1.1.1980')-- injects a message

What is SQL Injection?

$loginQuery = "SELECT * FROM users WHERE username='{$_POST['user']}' AND password='{$_POST['pass']}'";$result = mysql_query($loginQuery);

Page 9: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

9

The following SQL commands are executed: Usual search (no SQL injection):

SQL-injected search (matches all records):

SQL-injected INSERT command:

How Does SQL Injection Work?

SELECT * FROM Messages WHERE MessageText LIKE '%nakov%'"

SELECT * FROM Messages WHERE MessageText LIKE '%%%%'"

SELECT * FROM Messages WHERE MessageTextLIKE '%'; INSERT INTO Messages(MessageText, MessageDate) VALUES ('Hacked!!!', '1.1.1980') --%'"

SELECT * FROM Messages WHERE MessageText LIKE '%' or 1=1 --%'"

Page 10: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

10

Original SQL Query:

Setting username to John & password to ' OR '1'= '1 produces

Result: If a user Admin exists – he is logged in without password

Another SQL Injection Example

String sqlQuery = SELECT * FROM user WHERE name = 'Admin' AND pass='' OR '1'='1'

String sqlQuery = "SELECT * FROM user WHERE name = '" + username + "' AND pass='" + password + "'"

Page 11: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

11

Ways to prevent the SQL injection: SQL-escape all data coming from the user:

Not recommended: use as last resort only! Preferred approach:

Use ORM Use parameterized queries

Preventing SQL Injection

Page 12: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

SQL Injection and PreventionLive Demo

Page 13: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

Cross Site Scripting (XSS)What is XSS and How to Prevent It?

<script

>…

<script>…

Page 14: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

14

Cross-Site Scripting (XSS) is a common security vulnerability in Web applications Web application is let to display a JavaScript code that is executed

at the client's browser Crackers could take control over sessions, cookies, passwords, and

other private data

How to prevent from XSS? Validate the user input (built-in in ASP.NET) Perform HTML escaping when displaying text data in a Web control

XSS Attack

Page 15: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

XSS

Cross-site scripting attack Cookie theft Account hijacking Modify content Modify user settings Download malware Submit CRSF attack Password prompt

15

Submits sc

ript o

n an

unsafe form

Execute the script

on visiting the page

Page 16: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

16

HTML escaping is the act of replacing special characters with their HTML entities Escaped characters are interpreted as character data instead of

mark up

Typical characters to escape <, > – start / end of HTML tag & – start of character entity reference ', " – text in single / double quotes …

What is HTML Escaping?

Page 17: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

17

Each character could be presented as HTML entity escaping sequence Numeric character references:

'λ' is &#955;, &#x03BB; or &#X03bb; Named HTML entities:

'λ' is &lambda; '<' is &lt; '>' is &gt; '&' is &amp; " (double quote) is &quot;

HTML Character Escaping

Page 18: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

18

HTML encodes a string and returns the encoded (html-safe) string

Example (in PHP):

HTML Output:

Web browser renders the following:

How to Encode HTML Entities?

echo htmlentities("The image tag: <img>");

The image tag: &lt;img&gt;

The image tag: <img>

echo htmlspecialchars("The image tag: <img>");

Page 19: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

HTML EscapingLive Demo

Page 20: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

Cross-Site Request ForgeryWhat is CSRF and How to Prevent It?

Page 21: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

21

Cross-Site Request Forgery (CSRF / XSRF) is a web security attack over the HTTP protocol Allows executing unauthorized commands on behalf of some

authenticated user E.g. to transfer some money in a bank system

The user has valid permissions to execute the requested command The attacker uses these permissions to send a forged HTTP

request unbeknownst to the user Through a link / site / web form that the user is allured to open

What is CSRF?

Page 22: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

22

How does CSRF work?1. The user has a valid authentication cookie for the site victim.org

(remembered in the browser)

2. The attacker asks the user to visit some evil site, e.g. http://evilsite.com

3. The evil site sends HTTP GET / POST to victim.org and does something evil

Through a JavaScript AJAX request Using the browser's authentication cookie

4. The victim.org performs the unauthorized command on behalf of the authenticated user

CSRF Explained

Page 23: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

23

Cross-site request forgery attack

CSRF

Evil.com

MySite.com

User

Login

Authentication cookie

<form action=“mysite.com/ChangePassword”>

Submit data on behalf of User

Page 24: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

Cross-Site Request ForgeryLive Demo

Page 25: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

25

To prevent CSRF attacks in PHP apps use random generated tokens Put hidden field with random generated token in the HTML forms:

Verify anti-CSRF token in each controller action that should be protected:

Prevent CSRF in PHP

<form action="" method="POST"> <input type="text" name="message" /> <input type="hidden" name="formToken" value="$_SESSION['formToken']" /></form>

if (!isset($_POST['formToken']) || $_POST['formToken'] != $_SESSION['formToken']) { throw new Exception('Invalid request!'); exit; }

$_SESSION['formToken'] = uniqid(mt_rand(), true);

Page 26: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

Anti-CSRF in MVC AppsLive Demo

Page 27: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

Parameter TamperingWhat is Parameter Tampering and

How to Prevent It?

Page 28: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

What is Parameter Tampering?

What is Parameter Tampering? Malicious user alters the HTTP request parameters in unexpected

way Altered query string (in GET requests) Altered request body (form fields in POST requests) Altered cookies (e.g. authentication cookie) Skipped data validation at the client-side Injected parameter in MVC apps

28

Page 29: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

Parameter TamperingLive Demo

Page 30: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

Session Hijacking

Page 31: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

1. Capture a valid token session using a sniffer

2. Use the valid session token to gainunauthorized access to the server

Always use SSL when sending sensitive data!

You should use Man in the Middle attackto sniff the session token

Session Hijacking

Page 32: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

DoS (DDoS) AttacksWhat is Denial-of-Service attack?

32

Page 33: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

33

Semantic URL attacks URL Manipulation

Man in the Middle (MiTM) Brute force (use CAPTCHA!) Insufficient Access Control Error messages can reveal information Phishing Security flows in other software you are using Social Engineering

Other Threats

Page 35: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

License

This course (slides, examples, demos, videos, homework, etc.)is licensed under the "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International" license

35

Page 36: Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University

Free Trainings @ Software University Software University Foundation – softuni.org Software University – High-Quality Education,

Profession and Job for Software Developers softuni.bg

Software University @ Facebook facebook.com/SoftwareUniversity

Software University @ YouTube youtube.com/SoftwareUniversity

Software University Forums – forum.softuni.bg