PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 16th, 2009, 08:15 AM
benno32's Avatar
benno32 benno32 is offline
/*
Dev Shed Novice (500 - 999 posts) Click here for more information
 
Join Date: Mar 2007
Location: Sydney, Australia
Posts: 729 benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 23 h 17 m 15 sec
Reputation Power: 619
PHP-Security - Uploade File Handling

Hi folks,

I was wondering if any devsheder's can reccomend a library or solution for handling file uploads in php.

In my case I am receiving user uploaded files using multipart/form-data post data from flash and accessing the file via the $_FILES super global.

I want to be able to virus check and validate the file types.

thanks
Ben
__________________
*/

Reply With Quote
  #2  
Old April 16th, 2009, 08:47 AM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,701 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 5 h 23 m 48 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Any decent virus scanner will have already scanned the files as they were being uploaded, or at least when PHP tried to access them. Otherwise it's just a matter of using the right exec/system function to call the right command-line scanner with the right arguments.

Validate file types? Most people are content to check extension, but if you want to look inside the file then it's trivial to write one yourself. Really. It's simple.
PHP Code:
 $h fopen($_FILES["file"]["tmp_name"], "rb");
$data fread($h10);

if (
strncmp($data"\x25\x50\x44\x46\x2D\x31\x2E"7) == 0/* is a PDF */;
if (
strncmp($data"\xFF\xD8\xFF"3) == 0/* is a JPEG */;
// next line isn't supposed to wrap - blame devshed for that
if (strncmp($data"\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1\x00"9) == 0/* is a DOC */;
if (
strncmp($data"\x4D\x5A"2) == 0/* is a DLL */;
// ... 

I get those character sequences from http://filext.com.
Comments on this post
benno32 agrees: Nice one!
holodoc agrees!

Last edited by requinix : April 16th, 2009 at 08:49 AM.

Reply With Quote
  #3  
Old April 17th, 2009, 08:40 AM
benno32's Avatar
benno32 benno32 is offline
/*
Dev Shed Novice (500 - 999 posts) Click here for more information
 
Join Date: Mar 2007
Location: Sydney, Australia
Posts: 729 benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 23 h 17 m 15 sec
Reputation Power: 619
Thanks champion,

I didn't want to just check the file extension or mime type so I used your method. In my case I only want gif's,jpg's and png's so I added the extra couple of binary checks using the database you posted.

All three image types made it through the filter, next I tried putting through a text file called text.txt which I had renamed it to text.gif. It did not get through.

I would have spent a long time trying to figure this out! I owe you a favour.

Thanks again

P.S I like the idea of passing the buck for checking viruses to anti virus software on the server.

Reply With Quote
  #4  
Old April 17th, 2009, 08:59 AM
simshaun's Avatar
simshaun simshaun is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2008
Location: North Carolina
Posts: 2,674 simshaun User rank is General 25th Grade (Above 100000 Reputation Level)simshaun User rank is General 25th Grade (Above 100000 Reputation Level)simshaun User rank is General 25th Grade (Above 100000 Reputation Level)simshaun User rank is General 25th Grade (Above 100000 Reputation Level)simshaun User rank is General 25th Grade (Above 100000 Reputation Level)simshaun User rank is General 25th Grade (Above 100000 Reputation Level)simshaun User rank is General 25th Grade (Above 100000 Reputation Level)simshaun User rank is General 25th Grade (Above 100000 Reputation Level)simshaun User rank is General 25th Grade (Above 100000 Reputation Level)simshaun User rank is General 25th Grade (Above 100000 Reputation Level)simshaun User rank is General 25th Grade (Above 100000 Reputation Level)simshaun User rank is General 25th Grade (Above 100000 Reputation Level)simshaun User rank is General 25th Grade (Above 100000 Reputation Level)simshaun User rank is General 25th Grade (Above 100000 Reputation Level)simshaun User rank is General 25th Grade (Above 100000 Reputation Level)simshaun User rank is General 25th Grade (Above 100000 Reputation Level)  Folding Points: 59700 Folding Title: Beginner FolderFolding Points: 59700 Folding Title: Beginner FolderFolding Points: 59700 Folding Title: Beginner Folder
Time spent in forums: 4 Weeks 1 Day 20 h 35 m 37 sec
Reputation Power: 2673
While requinix gave what I think is the best solution, there are a couple things in PHP that may help you.

mime_content_type()
fileinfo functions
Comments on this post
holodoc agrees!
benno32 agrees!

Reply With Quote
  #5  
Old April 17th, 2009, 10:03 AM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,931 E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 7 h 48 m 54 sec
Reputation Power: 7053
Quote:
I didn't want to just check the file extension

You need to check the file extension as well. There is nothing stopping me from putting a gif header at the start of a PHP file, uploading it to your server and then being able to execute arbitrary PHP code on your website. PHP won't care if there is a random gif header at the start of the file, it will still execute PHP code inside of the file if the file has a .php extension.
Comments on this post
simshaun agrees!
ryon420 agrees!
benno32 agrees: Yes true. Fair enough.

Reply With Quote
  #6  
Old April 17th, 2009, 08:57 PM
benno32's Avatar
benno32 benno32 is offline
/*
Dev Shed Novice (500 - 999 posts) Click here for more information
 
Join Date: Mar 2007
Location: Sydney, Australia
Posts: 729 benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level)benno32 User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 23 h 17 m 15 sec
Reputation Power: 619
Quote:
Originally Posted by simshaun
While requinix gave what I think is the best solution, there are a couple things in PHP that may help you.

mime_content_type()
fileinfo functions


Thanks simshaun,

In my case I am using Flash to upload files and it always sends the mime type application/octet-stream no matter what type of file is being used unfortunately.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-Security - Uploade File Handling

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap