59
File Uploaders Vulnerabilities HackPra November 2012 Soroush Dalili SecProject.com

File in the Hole!

Embed Size (px)

Citation preview

Page 1: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 1/59

File Uploaders Vulnerabilities

HackPraNovember 2012

Soroush DaliliSecProject.com

Page 2: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 2/59

Web Application Security Researcher since2006

Finding vulnerabilities in my spare time: IIS Semi-Colon Problem, IIS ShortFile Scanner, ...

My blog: http://soroush.secproject.com/blog/  Twitter: @IRSDL Email: IRSDL at Yahoo dot com

Page 3: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 3/59

Introduction to Form-based File Upload File Upload Vulnerabilities

Protections and Bypass Methods

Bonus zero days in examples!

Page 4: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 4/59

 Based on: http://osvdb.org , Keyword: File UploadMore info: http://goo.gl/NmxpM 

Page 5: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 5/59

Page 6: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 6/59

Easy way to put the files on the server Increase business efficiency

Uses a simple web browser Sharing photos, videos, files, and so on Being used in most of the modern websites:

Social Networks, Mail Systems, Shops,Content Management Systems, Forums,… 

Page 7: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 7/59

The most common:

Form-based File Upload in HTML (RFC 1867)

Post Method Content-Type (enctype) = multipart/form-data

Others:

PUT HTTP Method ActiveX Java Applets … 

Page 8: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 8/59

<form method="post" enctype="multipart/form-data"action="upload.aspx">

File Name: <input type="file" name="myfile" />

<input type="submit" value="Upload" /></form>

Page 9: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 9/59

POST /samples/upload.aspx HTTP/1.1Host: www.example.com

Content-Type: multipart/form-data; boundary=AB12Content-Length: 1337

--AB12Content-Disposition: form-data; name="myfile"; filename="test.txt"

Content-Type: text/plain

File Contents ...--AB12--

RFC 1867

Page 10: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 10/59

Specific Issues: Improper or no access control

Arbitrary (Unrestricted) File Upload Overwriting critical files Path disclosure Directory Traversal Insecure Temporary File+ Other web application vulnerabilities

Page 11: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 11/59

Group A: Admin level access needed (Specific users which

have been authorised by admin)

Authentication bypass vulnerabilities… 

Client Side Attacks… CSRF , XSS 

Group B:

No authentication needed Normal user can have access

“ All the options are on the table!” 

Page 12: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 12/59

External module/library, it is safe: Wrong! It is just an editor: Wrong!

Bunch of images are harmless: Wrong! In-house applications are more vulnerable. Published vulns. in public apps:

Year OSVDB.org Records

2012 194

2011 123

2010 123

2009 107

2008 145

Page 13: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 13/59

Restricted File Upload:

Validation or other protections

▪  Can be bypassed? Unrestricted/Unprotected file upload:

You can upload whatever you want!

▪ And NO access control?

▪Piece of cake!

Page 14: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 14/59

Based on: http://exploit-db.com – total: 74 itemsMore info: http://goo.gl/NmxpM 

Page 15: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 15/59

Changing the functionality Bypassing the protections

Make the website vulnerable Denial of Service! Lame but possible

Famous sensitive files:.htaccess, web.config, crossdomain.xml,clientaccesspolicy.xml, global.asa, golbal.asax

Page 16: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 16/59

Exploit-DB ID: 17644FCKeditor (Old Version) Protection bypass by

uploading a .htaccess fileEven x_test.gif could run as a php file! Better Exploitation:Running a shell inside the .htaccess fileBy “Eldar Marcussen” -http://www.justanotherhacker.com

Page 17: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 17/59

Included libraries are not always safe File system and webserver are important Different method for path disclosure:

File/Directory/Symlink already exists Filename is too long

▪ NTFS: 255 characters

Forbidden characters or reserved words▪ WinOS: “<>?|:*"” + Control Characters 

▪ WinOS: CON, NUL, COM1, … 

Sensitive file system patterns▪ NTFS ADS: “:$I30:$INDEX_ALLOCATION” or “::$BITMAP” 

Permission Denied

Page 18: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 18/59

GleamTech FileVista v4.6:Uploading “test” as a file when we have a

directory with the same name:

Page 20: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 20/59

Modern browsers hide the local path:

Note: Anything before the last “/”

or “ \” in filename is usually ignored

by the web application

but it needs to be tested!

Page 21: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 21/59

Usually File Uploaders have destinationparameter(s)! Can accept an absolute path? Try these:

▪ “C: \”, “ \\127.0.0.1\c$\”, “file:///c: \\”,“ \\.\GLOBALROOT\Device\HarddiskVolume1\”,“ \\?\localhost\c$\” 

Is it a relative path?▪

“../”, “.. \”, with URL encoding, double URL encoding, orUnicode encoding

▪ Other tricks (Code/FS dependent)▪ Dot or Space after filename in windows

▪ Incorrect protections: Example: replacing “../” with nothing 

Page 22: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 22/59

GleamTech FileVista v4.6:

Bypassing protections for “../” & “.. \” by using: 

“.. /” (“..%20/”) & “.. \” (“..%20 \”) 

Short Demo: File - YouTube 

Page 23: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 23/59

Accessible via web directly? Examples:

Mail attachments: Upload, Download Data Processing : e.g. resizing an image

PHP temp files on File Upload

Page 24: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 24/59

Old but still effective for some systems…  SmarterMail File Upload Vulnerability:

Temp Uploaded ASP fileLink: http://securitytracker.com/id/1013021 

MailSite Express File Upload Vulnerability:

Temp Uploaded and then viewed file

Link: http://securitytracker.com/id?1015063 

Page 25: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 25/59

Similar to other Web Apps Vulns… Impacts can be highly critical though! e.g.:

Cross Site Request Forgery▪ Upload a file on behalf of authorities

Cross Site Scripting▪ Can make a website vulnerable OR can be vulnerable itself!

SQL Injection▪ When the website uses a database system

Denial of Service▪ Consuming server’s hard drive? Processing a large image?

And so on… 

Page 26: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 26/59

Client Side Protections: Name and Extension

It only makes the website more user friendly.

It is not for security! Data can be manipulated by a web proxy as usual

Server Side Protections – Proper ones!

Inside the code (Internal) Outside the code (External)

Page 27: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 27/59

Internal ExternalContent-Type (mime-type) Firewall: Request Header Detection

File Name and Extension Firewall: Request Body Detection

File Header (File Type Detector) Web Server Configurations

Content Format Permissions on File system

Compression (Image) Antivirus Application

Name Randomization Storing data in another domain

Storing files out of accessible webdirectory

Storing files in the database 

Page 28: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 28/59

Good to have it. But, no matter how good itis, it can be bypassed:

Different implementations of RFCs in web servers. e.g.:▪ Using white space characters and CrLf in HTTP Header

▪ Using Multiple fake “Boundary” items 

▪ Using “Transfer-Encoding: chunked” and obfuscating the body 

▪ Different File Systems/Operating Systems features. e.g.:

▪ “test.aspx” = “test~1.asp” in Windows which supports 8.3 

▪ Different web technologies features. e.g.:

▪ PHP converts “.” to “_” in the parameter name 

▪ ASP converts certain UTF-8 characters to ASCII

Page 29: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 29/59

.htaccess, web.config, and so on:

Overwrite their contents

Create a new one in a new folder Use Windows 8.3 file names

Other webserver configurations

Use extensions that are not being blocked▪ asa, cer, php3, php4, ashx, pl, cgi, shtml, phtml, … 

Try path traversal to move the uploaded file

Page 30: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 30/59

We don’t need bypass for file upload  Write access in Upload directory is needed

Webserver needs to be configured not FS

Not having execute permission does not help!

Write permission can be prohibited outside

What about Temp/Real Time files/folders?

Still bad if you can upload arbitrary files

It is good to have this to reduce the risk

Page 31: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 31/59

AV only blocks malwares/viruses Web-shell can be obfuscated

AV vulnerabilities can be exploited: e.g.: Sophos Vulnerabilities by Tavis Ormandy:

▪ 7th Nov 2012: http://secunia.com/advisories/51156/ 

▪ Just upload a file to execute your code

▪ In PHP, you just need to send your file to any PHP file!

Page 32: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 32/59

Good solution, hard implementation

File Server must be isolated

File Server must be hardened Subdomain can still be dangerous

Reading/Setting cross subdomain cookies

▪ e.g.: “domain=.example.com” 

Phishing attacks

Page 33: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 33/59

Location in the request:

File extension will change the “Content-Type”  Can be easily manipulated by a web-proxy Mostly image uploaders are the victims

“Do Not Trust/Use Content-type!” 

Page 34: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 34/59

ManageEngine Support Center Plus:

Exploit-DB ID: 22040

Bypass = “Content-Type: image/gif ”  MobileCartly 1.0:

Exploit-DB ID: 20539

Bypass = “Content-Type: image/gif ”… 

Page 35: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 35/59

First Step: What is File Extension in“test.php.jpg”?  “.php.jpg”? 

“.jpg”  Next Step: Which part has validation? Filename or Extension or Both?

What does it do with existing files? Logical flaws

Denial of Service … 

Page 36: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 36/59

White-list or Black-list? Check executable extensions

“.php” is blocked, what about “.php3”,“.php4”, “.phtml”, etc? “.asp” is blocked, what about “.asa” or “.cer”?  What about client side extensions?

.htm, .html, .swf , .jar, …? 

Page 37: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 37/59

The most common bypass method in 2012! Webserver related (can be fixed in Apache) Apache common configuration: “file.php.jpg” served as PHP

“AddHandler application/x-httpd-php .php” ▪ Better solution:

IIS 6 useless feature: “file.asp;.jpg” run as an ASP file

“/folder.asp/file.txt”  run as an ASP file

Page 38: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 38/59

Normally when we have Regular Expressions Always try it!

Code Example: Blacklist RegEx: “  ̂\.php$” 

▪ “file.php” != “file.PhP” 

“file.php3.jpg” != “file.PHP3.JpG” 

Example: eFront

Exploit-DB ID: 18036

Page 39: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 39/59

Overwriting sensitive files is easy:

“web.config” == “WEB~1.con” 

“default.aspx” == “DEFAUL~1.asp”  Files without extensions are allowed?

“.htaccess” == “HTACCE~1” 

Page 40: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 40/59

End of filename - ignored characters: Trailing dot and space characters

▪ “test.asp … . .. .” == “test.asp” 

Sometimes when it saves a file:▪ “test.php<>” == “test.php” 

NTFS Alternate Data Streams:

“file.asp::$data” == “file.asp”  “/folder:$i30:$Index_allocation” == “/folder” 

“.htaccess:.jpg” make empty “.htaccess” ==“HTACCE~1” … 

Page 41: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 41/59

“file.php%00.jpg” 

It needs to be decoded

▪Web server (name is in URL or code hasURLDecode)

▪ From client in “filename” section 

▪ Depends on server side parser

Page 42: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 42/59

Height/Width of image files? Simple Example: Comments in a jpeg file:

Page 43: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 43/59

Detecting malicious code by using a pattern?

Too many vectors and obfuscation techniques

False/Positives

Binary files

Different encodings

Performance issue

“This protection method is vulnerable!” 

Page 44: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 44/59

Does it remove the meta-data? Always scrambles the input?

What about small data?

Malicious code can be produced by thecompression out of dust!

Source: http://www.idontplaydarts.com 

A compressed .png file can contain PHP code!

PNG Image CompressionPNG with PHP

code!

Page 45: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 45/59

Harmless Text… 

Gzip Compression:

Now, we have a PHP backdoor:

<?=$_GET[0]($_POST[1]);?>

Page 46: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 46/59

Page 47: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 47/59

Directory Traversal to make it accessible? Remember FileVista Issue?

Still can be used in LFI How will users see them? You need to proxy them

▪ Performance issue

Local File Disclosure by a Directory Traversal▪ Loading unauthorized contents/files

▪ Local/Remote file inclusion issue

▪ And so on

Page 48: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 48/59

SQL Injection Still can create temporary files

Performance In upload and download

Files in the database need more space

Can lead to DoS

What if you want to migrate to another app?

Page 49: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 49/59

File duplication issues Delay problems Good for LFI and DoS!

Special file formats Compressed files XML files

Bad programming

Using “include” function to show an image  Replacing bad characters/extensions with nothing:▪ “file.p.phphp” “file.php” 

And so on

Page 50: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 50/59

In Apache (if we are in “/www/uploads/” dir):

Tested in Windows:

▪ filename = “.” or “…” make “/www/uploads” file 

In NTFS, “…:.jpg” makes “…” file 

Removable only via command line: “del *.*” 

By misconfiguration, “file.jpg” can run as

PHP:

“/file.jpg/index.php”, check this too! 

Page 51: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 51/59

Many developers use it as a base! O_0 The issues are:

It uses “Content-Type” as well as the extension!  No protection for double extensions

Possible Cross Site Scripting

Cross Site Request Forgery

Lack of authentication

Other online examples have similar issues

Page 52: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 52/59

Page 53: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 53/59

“.asptxt” is a valid extension  Dangerous characters are converted to “_”  However, file duplication is an exception! Thanks to Mostafa Azizi (@0daynet) for this bug

“file.asp;txt” will be saved as “file.asp_txt” 

Again “file.asp;txt”will be saved as

“file(1).asp;txt”

IIS6 useless feature shines! But, we can even use Null Character here to have

“file(1).asp” on the server! 

Short Demo: File - YouTube 

Page 54: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 54/59

There is a logical flaw in CKFinder ASP version. Uploading “Con.pdf.txt” kills the server: 

CKFinder renames an existing file to “file(1).ext” 

▪ If “file(1).ext” was taken, it will try “file(2).ext”, and so on 

CkFinder is OK with multiple valid extensions

“CON” is a forbidden name in Windows 

CKFinder thinks “Con.pdf.txt” is taken, ▪ It tries “Con.pdf(1).txt” which is forbidden too! 

▪ “Con.pdf(2).txt” … “Con.pdf(MaxInt).txt” 

This can kill the server!

Page 55: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 55/59

Do you have enough internal and externalprotections? Are they bypassable? Try it yourself!

Can you host malwares, adult or illegalcontent for free?!

Do you have a monitoring system to report

newly uploaded/modified files? Are all the libraries and modules up to date? Upload a safe webshell and try to see how

much access it has!

Page 56: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 56/59

File Managers include different modules: Upload File Create File/Directory Edit File Rename File/Directory Delete File/Directory Move File/Directory Compress/Decompress Modules Image converting/resizing modules Download/View File Browse Files/Directories Change PermissionsMore functionality Possibility of more vulnerability

Page 57: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 57/59

Attack vectors cheat sheets Protection methods cheat sheets More OWASP ESAPI for file managers Payloads to make the testing automatic Better checklists for Web Developers &

Webmasters.

Page 58: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 58/59

 

Any Question?

Page 59: File in the Hole!

7/27/2019 File in the Hole!

http://slidepdf.com/reader/full/file-in-the-hole 59/59

A BIG thank you to everyone

And, to the people who helped me to prepare this

talk.

Here are my contact details:

Twitter: @IRSDL

Email: IRSDL at Yahoo dot com