Recently Viewed...
SnowCovered Top Sellers

Version 5 of the perennial best-selling tool for creating data-based solutions in DNN without custom programming. This version focuses on greater flexibility, expandability, and ease-of-use.

Live Content uses Web 2.0 approach to provide a Rich User Interface and streamlines content presentation by overlaying content on current page. Overlay images, videos, audio, text/html content, flash, dotnetnuke modules, and external content. Experience the demo...

Ultra Video Gallery is a brother product of Ultra Media Gallery, UVG allows you to add videos in various format and automatically convert them to flv format, you also can add videos from embed code and play them in our integrated flash video player.

Powerful, Ajax Enabled, Easy to Use. Document Management has never been better. Open-DocumentLibrary allows DotNetNuke users to share and manage documents in a flexible, intelligent way, offering granular control over Folder and Document access.

'Relationship Building' and 'Communication' are two essential nuts and bolts for a business to prosper. This module allows you to bridge both of these and easily generate continuous awareness of your web site, products and services. Your prospects and customers will greatly appreciate this featur

In this day and age, knowing as much detailed information as possible about your customer, prospect or web site user is essential. Thankfully, the new 'Dynamics Forms' module from Data Springs, makes it easier than ever to segment your data collection efforts.

Capture your users attention, enrich your site with multimedia flash, and create and opt in distribution list for your DNN site. These are just a few of the many features the Data Springs Module Collection can provide you.

Ultra Media Gallery is the most popular photo gallery and media gallery solution for DotNetNuke, UMG offers 10 different flash player to browse your gallery with completely different user interface experience.

The Catalyst skins are professionally designed, coded and packaged by a team of DotNetNuke experts. The skins are available in 12 great colours. This skin is easily customisable with our unique DrNuke EasyMod technology. Try our demo!

ALL NEW ! - Minimalist includes skin packs in 12 great colors. Each color has Flat, Gradient and Glass versions. Feature rich XML Flash header, perfect for just about any purpose. 9 Different menu options in each skin pack; 3 horizontal menus, 3 vertical menus and 3 all-new Twin level menus . . .

    |   Register   |   Thursday, November 20, 2008   
You are here:Resources  Articles & Information  Handing File Uploads  


Secure Programming Tips - Handling File Uploads

Week 6: Handling File Uploads


Online photo albums, document repositories, content management systems, all of these applications have one thing in common, they generally allow a user to upload files to a remote server. A server, that in most cases, hosts numerous other applications. A server that may store extremely sensitive customer information. A server, that if compromised, could result in the theft of customer records, confidential corporate information, or potentially compromise the entire corporate network. It's true, even the simplest thing of letting a user upload an image to your server, could result in the complete compromise of your entire corporate network. When's the last time you performed proper security testing on your upload functionality?

When allowing user's to upload files to your server, there are several areas we must pay particular attention to. These areas include proper input validation of valid file types, the location the uploaded files will be stored and the various permissions associated with the file storage. A simple mistake in anyone or all of these areas can result in a catastrophic compromise of your server. So it is absolutely essential when allowing a user to upload files to your server, you put your best security foot forward and enforce strict rules associated with this functionality.

Generally, when we allow a user to upload a file to our server we put a restriction on the acceptable file types we will allow them to upload. In some cases we may allow image files, such as .jpg, .gif, .png, etc. Whereas in other cases, such as that of a document repository, we may allow files such as .doc, .pdf, .xls, etc. In either case we want to restrict the types of files to a predefined list and disallow all other types of files. The only guaranteed manner we can enforce proper file type restrictions is through proper input validation. This validation must occur on both the client, as well as the server (input validation should always be done in both locations). The validation must check several things, including the file extension, the content-type and the file name itself.

Proper input validation must be used to ensure the file type the user is attempting to upload is an allowable file type. The easiest way to perform the first part of the file type validation is to check the extension of the file the user is attempting to upload. Figure 1 shows a sample JavaScript function that can be used to perform client-side validation of a file.

Figure 1


function CheckFileExt(fileName, allowedFileTypes)
{
var dot;
var fileType;

dot = fileName.split(".");
fileType = "." dot[dot.length-1];

if(allowedFileTypes.join(".").indexOf(fileType) != -1)
{
return true;
}
else
{
alert("Invalid File Type!");
return false;
}
}

In order to call the validation script above we will use the OnChange event of the file input control, as seen in Figure 2.

Figure 2


OnChange="return CheckFileExt(this.value, ['gif', 'jpg', 'png', 'jpeg']);"

The OnChange event passes the file name the user is attempting to upload, as well as a comma separated list of allowed file extensions. The function seen in Figure 1, will take file name, strip the extension and compare it to the list of allowed extensions. If the extension is allowed the page will allow the user to continue. However, if the extension is not allowed a JavaScript alert box will pop-up informing the user the file type is invalid. There is one major issue with this though. If the user has turned off JavaScript in their browser it will render our validation function useless. This is why it is imperative to ensure the validation is performed not only on the client, but on the server as well.

The next area to consider for validation is the content-type and content itself. We can't solely trust a file by its extension alone, we must validate additional areas of it to ensure it is safe. It wouldn't be difficult for a malicious user to place the text seen in Figure 3 into a file and save it as a PDF document. This would allow the malicious user to bypass the file extension validation, assuming a PDF document was allowed, without the file actually being a valid PDF document.

Figure 3 The script tag below has been modified to render, rather than execute in your browser.


-%PDF < script>alert('Not A Valid PDF Document');< /script>

The text above placed into a file and saved as a PDF document, would not only bypass the file extension validation, but it would also bypass the content-type validation. Since the text contains a valid PDF signature ("-%PDF"), the content-type will assume it is a valid PDF document. The validation process must also validate the content of the file itself. By validating the actual content we would be able to make the determination that the file was not a valid PDF document.

The last area of input validation we need to look at is the actual file name. We would need to perform some validation to ensure the file name does not contain a characters that could be used maliciously. Characters such as < > /, etc. could be used maliciously to execute script or perform a directory traversal attack. Another cause for concern is a null byte(%00). Most servers interpret the null byte character as the end of a line or statement. Thus a file named "Test.asp%00.jpg", would bypass our file extension validation, and potentially our content-type validation. However, if the file were called directly in the browser by typing "http://mydomain.com/Test.asp%00.jpg". The server would interpret that everything after the should be dropped, thus the request would be changed to "http://mydomain.com/Test.asp". We'll see in the next section why this could be a major cause for concern.

Now that we have a better idea of how to perform proper validation on an uploaded file, we need determine where we are going to store the files a user has uploaded. Obviously a database is a bad place to store uploaded files. We would need to store the files in a server's file system. Most developers make the mistake of storing uploaded files directly with the web root. The primary issue with this is if a malicious user can determine the link to a file, they could possibly, depending on access permissions, directly request the file just by typing the URL into the address bar in their browser. This could potentially lead to the unnecessary disclosure of sensitive information. Another potential issue with storing uploaded files within the web root, is the possibility that the folder containing the files could allow the execution of scripts or executables.

If a malicious user were able to bypass our validation process and upload the fake PDF document, and if the file was stored within the web root, when a user loaded that document in their browser the script would execute. Now let's consider the larger issue. Let's say instead of JavaScript being placed in the PDF document we place executable ASP script code. Let's also assume we bypassed the validation by changing the file name to "Test.asp%00.pdf". If this file was stored in the web root and this folder allowed for script execution. A malicous user could directly request the file by changing the URL in their browser to "http://mydomain.com/Test.asp%00.pdf", which as mentioned earlier would be interpreted as "http://mydomain.com/Test.asp". The result would be the ASP code inside the document being executed and the result of whatever the code did, being rendered to the user's browser. The code in the file could allow a user to traverse each drive on the server, view or download documents, gain access to sensitive information, such as usernames, passwords, database connection strings, etc. The code in the file could also attempt to gain command shell access, that could allow a malicious user to execute commands on the server.

In summary the primary concern is validation, validation, validation. We, as developers, need to ensure we validate every aspect of a file that is being uploaded. This includes the file extension, the file name, the content-type and lastly the content itself. Doing all of this still doesn't guarantee the file is 100% safe, but it will go a long way in preventing malicious files form being uploaded to our servers. We also need to ensure we aren't storing the files in the web root, because doing so could allow malicious users to directly request the files in their browser, thus potentially exposing sensitive information. Finally, if we must store the files in the web root we must ensure the folder permissions containing the files do not allow for the execution of script or executable code. Adding these preventive measures will go a long way in ensuring your user's and your server won't be compromised due to malicious file uploads.

Feedback Comments
Records per Page
Page 1 of 1First   Previous   Next   Last   
         2/29/2008 10:06:02 PM
Continuation from message below... Don''''t get me wrong, there are pros and cons to storing them in a database or on the file system. However, if performance is essential to your application the file system is definitely the way to store them.I am planning on writing another, more in-depth article showing the full scale of file uploads and how they can be compromised. Obviously, my intention is not to show people how to hack a file upload system, but I feel it is necessary to demonstrate the dangers associated with allow file uploads, especially when the functionality is implemented incorrectly. If you have specific practical examples you would like to see, let me know and I''''ll see what I can do.

         2/29/2008 10:00:54 PM
There''s been considerable debate on where upload files should be stored. The primary reasons it''s better to store them on the file system, rather then in a database are 1) the application could take a severe performance hit while attempting to access the files. Files generally need to be serialized and stored in a database and binary data, which means when they are retrieved they need to go through a de-serialization process to write the binary data out to a stream for access by the user. 2) There''s a lack of accessibility to the files. If the files are stored on the file system, they can be accessed and downloaded very quickly by a user. 3) When performing data backups of the databases, all files are processed whether they have been changed or not. If stored on the file system a quick checksum can determine if a file has changed or not, so making this determination is much quicker when the files are stored on the file system. Don''t get me wrong, there are pros and cons to storing

         2/28/2008 5:10:47 PM
Why is storing uploaded files in databases is a "bad" option Hi, You said: "Obviously a database is a bad place to store uploaded files". What is the reason? Also, could you provide any practical example? Thanks, Ricky.

Feedback





Enter the code shown above in the box below
Send

 

DNN Modules
SharePoint Web Parts
Flash Image Rotator for SharePoint 2007

Flash Image Rotator Web Part for SharePoint 2007 

 

Who would have thought? Flash with Sharepoint! The FIRST and ONLY flash rotation web part for Sharepoint. The Flash Image Rotator displays selected images and then rotates between the images. Several extended and optional features allow you to select the time to rotate each image, fade between i...more

Price: $129.99
 
Flash News Ticker for SharePoint 2007

Flash News Ticker Web Part for SharePoint 2007 

 

Provide current news items with a user-friendly news ticker for your Sharepoint Portal. With millions of web sites offering information you need a fun way to display information and the solution is Flash News Ticker....more

Price: $139.99
 
View Stock Quote Web Part

Stock Quote Web Part for SharePoint 2007

 

Giving your site visitors relevant information is critical. With the Data Springs Stock Web Part you can provide your users with up to date financial information....more

Price: $149.99
 
Random Image Web Part for SharePoint / MOSS 2007

Random Image Web Part for SharePoint 2007

With Random Image for Sharepoint 2007, you can select multiple images to display randomly when the web part loads...

Price: $139.99
 
SharePoint Charts Web Part

MOSS Charts Web Part for SharePoint 2007

The MOSS Chart Web Part is a web part built by Data Springs for the purpose of rendering several chart types based on data from a SharePoint list on a MOSS 2007 or WSS 3.0 Site ... more

Price: $269.99
 
Copyright 2005 - 2008 by Data Springs, Inc.
Contact Us | Terms Of Use | Privacy Statement