Data normalization weaknesses

32
Data normalization weaknesses @d0znpp VolgaCTF, 03/09/2013

description

 

Transcript of Data normalization weaknesses

Page 1: Data normalization weaknesses

Data normalization weaknesses

@d0znppVolgaCTF, 03/09/2013

Page 2: Data normalization weaknesses

Intro

• Researcher, bug-hunter, CEO

• Web application security in depth

• @d0znpp personal twitter

• lab.onsec.ru our blog (@ONsec_lab)

Page 3: Data normalization weaknesses

What is normalization?

• Transferring and storing data are always accompanied by their formatting

• First normalization than formatting

• Encoding (different charsets)

• Truncation (limited sizes)

• Trims

• Canonizations

• ...

Page 4: Data normalization weaknesses

Data normalization or input validation weaknesses?

Page 5: Data normalization weaknesses

Web application basics

• Client-Server model

• Client is browser (Chrome, Safari, IE, FF)

• Server is web server software (Nginx, Apache)

• Application server (FastCGI, Tomcat)

• Database storage (SQL or noSQL)

Page 6: Data normalization weaknesses

Web application example. Depth #1

Browser WebServer

Database

AppServerHTTP FCGI SQL

Page 7: Data normalization weaknesses

Web application example. Depth #2

Browser WebServer

Database

AppServerHTTP FCGI SQL

Operation System

File System

FS driver

Page 8: Data normalization weaknesses

Web application example. Depth #3

Browser WebServer

Database

AppServerHTTP FCGI SQL

OS

File System

FS driverNetwork layer

Page 9: Data normalization weaknesses

Protocol level normalization

Browser WebServer

Database

AppServerHTTP FCGI SQL

OS

File System

FS driverNetwork layer

Page 10: Data normalization weaknesses

Protocol level normalization

• Urlencoding - what could be simpler?

• %22 to «

• %23 to #

• %25 to %

• Double url-encoding is basic bypass for many input validators, right?

Page 11: Data normalization weaknesses

2+ urlencodingWhy not?!

Browser Frontend BackendHTTP FCGI

OS

BalancerHTTP

%252527 %2527 %27

Inputvalidator

Page 12: Data normalization weaknesses

Protocol level normalization

Browser WebServer

Database

AppServerHTTP FCGI SQL

OS

File System

FS driverNetwork layer

Page 14: Data normalization weaknesses

Protocol level normalization

• Something new?

• Why only parameters?

• Let’s try to fuzz smth else! :)

• GET{F}/{F}HTTP.1.1

• {F} = 0x09, 0x0b, 0x0c, 0x0d, 0x32

• Apache/2.2.22 (Unix)

• GET / bla-bla bla bla bla ehohoh

Valid packet!

Page 15: Data normalization weaknesses

File paths normalization

Browser WebServer

Database

AppServerHTTP FCGI SQL

OS

File System

FS driverNetwork layer

Page 17: Data normalization weaknesses

Filesystem names canonization

• Normalization

• /etc/passwd//////////////////////////////////.php

• C:\boot.<<

• C:\boot’‘ini

• C:\boot.in>

Page 18: Data normalization weaknesses

Database storing normalization

Browser WebServer

Database

AppServerHTTP FCGI SQL

OS

File System

FS driverNetwork layer

Page 19: Data normalization weaknesses

Database storing normalization

• Encodings

• Client encoding

• Storing encoding

• Trim

• Size limited truncation

Page 20: Data normalization weaknesses

Database storing normalization

• VARCHAR or BLOB ?

• What size limit of CREATE TABLE t1 (login TEXT) ?

• INSERT INTO logins VALUES (:id, :login, :password)

• $login = « admin aa»

Page 21: Data normalization weaknesses

Application layer normalization

Browser WebServer

Database

AppServerHTTP FCGI SQL

OS

File System

FS driverNetwork layer

Page 23: Data normalization weaknesses

Application layer normalization

• Port overwriting, formatting

• localhost:81

• localhost:+81AAAAA

• localhost: 00081 AAA

Page 24: Data normalization weaknesses

IT IS ENCODING !!!

Page 25: Data normalization weaknesses

Multibyte encodings

• One byte for one char

• More bytes for one char !

• á

• 0xE1

• 0xC3A1 UTF-8 C-form

• 0x61CC81 UTF-8 D-form

Page 26: Data normalization weaknesses

addslashes() bypass

• http://shiflett.org/blog/2006/jan/addslashes-versus-mysql-real-escape-string

• ’ to \’

• Replace 0x27 byte to 0x5c27

• But what about multibyte?

• 0xbf5c - valid char for GBK encoding

• 0xbf5c27 -> 0xbf5c 0x27

Page 27: Data normalization weaknesses

addslashes() bypass

• http://kuza55.blogspot.ru/2007/06/mysql-injection-encoding-attacks.html

• Find all encodings where 0x5c is valid second byte at any char

• big5, [A1-F9]

• sjis, [81-9F], [E0-FC]

• gbk, [81-FE]

• cp932, [81-9F], [E0-FC]

Page 28: Data normalization weaknesses

Homework! escapeshellarg/cmd()

• Note that:

• PHP use SH by default at system(), not BASH

• SH have no multibyte encoding

• escapeshellarg cut bytes 0x80-0xFF

Page 29: Data normalization weaknesses

But... escapeshellarg()

• http://lab.onsec.ru/2013/03/breaking-escapeshellarg-news.html

• for shell no differences between

• ls -la

• ls ‘’-la’’

• ls ‘-la’

• unzip ‘-d/var/www’ - escaped, but arg!

Page 30: Data normalization weaknesses

PHP string encodinghttp://www.php.net/manual/language.types.string.php#language.types.string.details

• String will be encoded in whatever fashion it is encoded in the script file

• If Zend Multibyte is enabled, the script may be written in an arbitrary encoding (which is explicity declared or is detected) and then converted to a certain internal encoding, which is then the encoding that will be used for the string literals

• State-dependent encodings where the same byte values can be used in initial and non-initial shift states may be problematic

Page 31: Data normalization weaknesses

Multibyte problems• Lengths in chars or bytes?

• State-dependent encodings

• 0x0102 char

• 0x0203 char

• 0x01020203 two chars

• But what about case when 0x0202 is valid char also?

• Try to find 0x0202 in this string ;)

Page 32: Data normalization weaknesses

Thanks for attention!

[email protected]@d0znpp@ONsec_lablab.onsec.ru