I want my users to be able to upload a photo. Currently I am not checking the uploaded photo for problems of any kind, although I do limit the size to 32k. Is there any way for me to check uploaded image files for malware? For instance, is there a server out there running ClamAV that I can access from PHP? Thanks.
|
|
This has been answered before on this site, in extensive detail. See these questions:
For general advice relating to web security, OWASP is always a good resource, too. |
|||
|
|
|
Best I can think for PHP is to re-create the image, for example:
Then delete the originally uploaded image and keep your resized copy. |
|||
|
|
|
As Christian says, ithe best solution is to whitelist and verify the file type - however any file which has uncompressed metadata can potentially be an avenue for attacking a webserver. Strip the metadata. Invoking the virus checker synchronously from the web request is a very bad idea in terms of performance, stability and availability. While the default upload handler in PHP sensibly puts files out of the way and forces you to move them before the request completes - this means that you'll have to copy the file once more after you virus scan it. |
|||
|
|
|
I think that the best approach would be to use a combined solution:
By using all of these checks, the possibility of a successful attack would a lot smaller. |
|||
|
|
|
A good and popular practice is to check if the uploaded image is really an image and not something else. Validating an image is more secure than scanning with an antivirus. The getimagesize() function will determine the size of any given image file and return the dimensions along with the file type and a height/width text string to be used inside a normal HTML IMG tag and the correspondent HTTP content type. The function supports many image formats. You can use
Note that MPEG videos are detected as |
|||||||||
|
|
Not a complete answer* but one way to filter out unwanted content would be to use PHP's image manipulation functions to transpose the uploaded image into a new image and then save that, rather than the uploaded data. A bit like taking a screenshot of a picture rather than just saving the picture directly. *= Or the best solution, or easiest... |
|||||||
|
