Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-13-2007, 04:51 AM   PM User | #1
Digicoder
New Coder

 
Join Date: Sep 2007
Location: US
Posts: 88
Thanks: 4
Thanked 4 Times in 4 Posts
Digicoder is an unknown quantity at this point
Image safety.

Okay, so I've got a script to write but a concern that i can't figure out.

I'm writing an image upload script but want to make sure that the images uploaded don't have a Trojan in them. There has been word of jpeg images especially that are the cause of this.

So, is there some kind of image scrubbing php module or script that I need to ensure that the pictures are bug free?

FYI, this system will use the imagemagic extension of php.

Thanks for the help all.
__________________
I need to find a book about all this stuff. God, thats gonna be one big book!

http://www.gamezftw.com
Play On!
Digicoder is offline   Reply With Quote
Old 11-13-2007, 05:04 AM   PM User | #2
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
You can use getimagesize() to start with, or the Imagick identifyimage function.
Inigoesdr is offline   Reply With Quote
Old 11-13-2007, 10:54 PM   PM User | #3
Digicoder
New Coder

 
Join Date: Sep 2007
Location: US
Posts: 88
Thanks: 4
Thanked 4 Times in 4 Posts
Digicoder is an unknown quantity at this point
I'm sorry, I still really don't under stand it all. Those functions will verify that the jpeg image is just a jpeg?
Sorry for the bother, and thanks for the help.
__________________
I need to find a book about all this stuff. God, thats gonna be one big book!

http://www.gamezftw.com
Play On!
Digicoder is offline   Reply With Quote
Old 11-14-2007, 03:33 AM   PM User | #4
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,904
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
you will need to do a scan with whatever antivirus is available on your server, many linux hosts will have clamav or similar which can be exec()'d
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 11-14-2007, 03:40 AM   PM User | #5
Digicoder
New Coder

 
Join Date: Sep 2007
Location: US
Posts: 88
Thanks: 4
Thanked 4 Times in 4 Posts
Digicoder is an unknown quantity at this point
Damn, I don't think my go daddy host has an anti virus scanner.
Thus fare they've told me that they have none of the many things i'd consider key for truly robust websites.
__________________
I need to find a book about all this stuff. God, thats gonna be one big book!

http://www.gamezftw.com
Play On!
Digicoder is offline   Reply With Quote
Old 11-14-2007, 04:42 AM   PM User | #6
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,713
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
You might want to check out the link in post #2 in this thread - http://www.codingforums.com/showthread.php?t=127318

It is possible for a file to contain a valid image AND contain php code. The image content prior to the <?php tag is simply content that php would output, then the php code would be parsed and executed. There are some conditions that must be true for this exploit to be possible, such as allowing an upload file name to be completely specified from the upload form, so that such an image/php code file could be placed on the server with a file name that could be browsed to and be parsed as php code.

The various image functions (getimagesize and imagecreatefromjpeg...) will find and happily return the image portion of such an image/php code file.

Since an image would not normally contain data that looks like php code, this type of exploit could be discovered by scanning the file for php only keywords that would be used by malicious code, such as <? exec shell echo print print_r...
__________________
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.
CFMaBiSmAd is online now   Reply With Quote
Old 11-14-2007, 04:46 AM   PM User | #7
dniwebdesign
Regular Coder

 
dniwebdesign's Avatar
 
Join Date: Dec 2003
Location: Carrot River, Saskatchewan
Posts: 838
Thanks: 15
Thanked 9 Times in 9 Posts
dniwebdesign is an unknown quantity at this point
If you use the getimagesize(), check the file type of the file being uploaded it should narrow down the field of what files are uploaded.

Also if you expect the images to be a certain size you can also limit the size of the uploads.

Otherwise, the virus scanner is your best bet, but if you don't have that, you have to do a detailed check on every part of the file to make sure it is a jpeg file.
__________________
Dawson Irvine
CEO - DNI Web Design
http://www.dniwebdesign.com
dniwebdesign is offline   Reply With Quote
Old 11-14-2007, 04:57 AM   PM User | #8
Digicoder
New Coder

 
Join Date: Sep 2007
Location: US
Posts: 88
Thanks: 4
Thanked 4 Times in 4 Posts
Digicoder is an unknown quantity at this point
Wow, thanks guys, I've learned a bit in the past few minutes.
I still have one question, now that i know that the image can be more than just binary or hex, how exactly do i extract the file into a readable format?

Or am i just asking a pointless question on this one.
Thus i would only have to use some thing like this:
PHP Code:
<?php
if($_FILE['imagefile']['name']=='<?php' || $_FILE['imagefile']['name']== '<?')
{
// Either parse with imagecreatefromjpeg or throw out and ask for another file.
}
?>
__________________
I need to find a book about all this stuff. God, thats gonna be one big book!

http://www.gamezftw.com
Play On!
Digicoder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:36 PM.


Advertisement
Log in to turn off these ads.