Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is php fileinfo sufficient to prevent upload of malicious files?

Tags:

security

php

I have searched around a bit, and have not really found a professional type response to how to have secure fileupload capability. So I wanted to get the opinion of some of the experts on this site. I am currently allowing upload of mp3s and images, and while I am pretty confident in preventing xss and injection attacks on my site, I am not really familiar with fileupload security. I basically just use php fileinfo and check an array of accepted filetypes against the filetype. For images, there is the getimagesize function and some additional checks. As far as storing them, I just have a folder within my directory, because I want the users to be able to use the files. If anyone could give me some tips I would really appreciate it.

like image 805
Scarface Avatar asked Apr 01 '10 15:04

Scarface


People also ask

What is FileInfo PHP?

FileInfo functions module can try to guess a content type and encoding of a file by looking for certain magic byte sequences at a specific position within a file. While it's not a bulletproof approach, the heuristics used to do a very good job. Before PHP 5.3.

What are the constraints to upload files in PHP?

$_FILES[“file”][“name”] – the name of the uploaded file. $_FILES[“file”][“type”] – the type of the uploaded file. $_FILES[“file”][“size”] – the size in bytes of the uploaded file. $_FILES[“file”][“tmp_name”] – the name of the temporary copy of the file stored on the server.

What is malicious file upload?

Malicious file uploading is a type of attack that involves placing files onto a server or computer in such a way that they contain some form of backdoor code that will allow the attacker to gain access afterward.

What is local file upload vulnerability?

A local file upload vulnerability is a vulnerability where an application allows a user to upload a malicious file directly which is then executed. A remote file upload vulnerability is a vulnerability where an application uses user input to fetch a remote file from a site on the Internet and store it locally.


2 Answers

I usually invoke ClamAV when accepting files that can be shared. With PHP, this is rather easily accomplished with php-clamav.

One of the last things you want to do is spread malware around the globe :)

If you can, do this in the background after a file is uploaded, but before making it public. A quirk with this class is that it can load the entire ClamAV virus definition database into memory, which will almost certainly stink if PHP is running under Apache conventionally (think on the order of +120 MB of memory per instance).

Using something like beanstalkd to scan uploads then update your DB to make them public is a very good way to work around this.


I mentioned this only because the other answers had not, in no way did I intend this to be a complete solution. See the other answers posted here, this is a step you should be finishing with. Always, always, always sanitize your input, make sure it's of the expected type, etc (did I mention that you should read the other answers too?)

like image 132
Tim Post Avatar answered Sep 22 '22 18:09

Tim Post


"malicious" files are not the only way to hurt your server (and if your site is down, it hurts your users).


For example, a possibility to hurt a server would be to upload a lot of very small files :

  • it would not use all the space on the disk,
  • but could use all available inodes...

...And when there is no free inode left, it's not possible to create any file anymore ; which, obviously, is bad.


After that, there is also the problems like :

  • copyright
  • content that is not OK to you or your users (nudity ? )

For that, there's not much you an do with technical solutions -- but an "alert the moderator" feature is oftne helpful ;-)

like image 39
Pascal MARTIN Avatar answered Sep 22 '22 18:09

Pascal MARTIN