Basic Perl Web Upload Script

Reply

Join Date: Jan 2007
Posts: 2
Reputation: JayT is an unknown quantity at this point 
Solved Threads: 0
JayT JayT is offline Offline
Newbie Poster

Basic Perl Web Upload Script

 
0
  #1
Feb 5th, 2007
Hi,

I've been reading through some tutorials on creating a basic web upload script with perl and cgi. The problem is that my server keeps throwing an Internal Server Error 500 without giving any feedback. I am hoping that someone has run into a similar problem or there is an obvious problem with my script. Here is the form script on the html document (/var/www/apache2-default/projects/music/music.html):

<FORM ACTION="upload.cgi" METHOD="POST" ENCTYPE="multipart/form-data">
Song to Upload: <INPUT TYPE="file" NAME="song">
<br>
<INPUT TYPE="submit" NAME="Submit" VALUE="Submit Form">
</FORM>


My httpd.conf for Apache2:

<Directory /var/www/apache2-default/projects/music/>
Options FollowSymLinks +ExecCGI
AddHandler cgi-script .cgi
</Directory>


And my actual upload.cgi file:

#!/usr/bin/perl -w

use CGI;
$upload_dir = "/apache2-default/projects/music/upload";

$query = new CGI;

$filename = $query->param("song");
$filename =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle = $query->upload("song");

open(UPLOADFILE, ">$upload_dir/$filename") or die "Can't open '$upload_dir/$filename': $!";
binmode UPLOADFILE;
while ( <$upload_filehandle> )
{
print UPLOADFILE;
}
close UPLOADFILE;


All folders and files have been chmoded to 755 for all user execution. Even so, it seems like the httpd.conf points to the correct directory to allow cgi execution, but there has to be something wrong with the upload.cgi script.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 263
Reputation: Mushy-pea is an unknown quantity at this point 
Solved Threads: 1
Mushy-pea's Avatar
Mushy-pea Mushy-pea is offline Offline
Posting Whiz in Training

Re: Basic Perl Web Upload Script

 
0
  #2
Feb 7th, 2007
First, it would be highly advisable to start your Perl script like this:
  1. #!/usr/bin/perl -T
  2.  
  3. use strict;
  4. use warnings;

I know you used warnings with the -w flag already, but adding strict will help with debugging as well. Also, you "MUST" use the -T flag as shown to enable taint mode, or mistakes in your code could turn into gaping security holes. DO NOT leave all your folders and files at the 755 permission setting. Only a CGI script you want to be executable by a HTTP request should have these permissions. All other files should not allow anything else but read permission to "other" or "world" users (i.e. 4 as the last permission digit).

If there is a file called something like "cgierror.log" in the "logs" directory on the server, compile and run time errors may be collected here. You have not validated the user input sufficiently before passing it to open. This

  1. $filename =~ s/.*[\/\\](.*)/$1/;

means, match some stuff then capture anything any number of times and set $filename to this. This means someone could pass virtually anything into open(FILEHANDLE,....). Including of course ">my_file", which will delete the contents of any file a hacker chooses. Instead try,

  1. my $filename =~ /([^<>]*)/;
  2. $filename = $1;
  3. open(FILEHANDLE, "<", $filename);

The regex will remove any shell meta characters (<>) from the name supplied. I think it's best to use the three parameter form of open shown, as it's safer by not allowing user data to set the open mode. Finally, if it's just a text field you want to gather with your form, put:

  1. <input type="text" name="song">

I don't think type="file" is valid HTML.

Steven.
The one question you should not ask when teaching a new language structure is "Do you understand?". Do you understand?
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 2
Reputation: bildja is an unknown quantity at this point 
Solved Threads: 0
bildja bildja is offline Offline
Newbie Poster

Re: Basic Perl Web Upload Script

 
0
  #3
Aug 4th, 2009
i'm sure 'file' is absolutely valid)
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Basic Perl Web Upload Script

 
0
  #4
Aug 4th, 2009
Originally Posted by bildja View Post
i'm sure 'file' is absolutely valid)
cool, for the last 2 and a half years I've been wondering about that.....
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 2
Reputation: bildja is an unknown quantity at this point 
Solved Threads: 0
bildja bildja is offline Offline
Newbie Poster

Re: Basic Perl Web Upload Script

 
0
  #5
Aug 5th, 2009
lol)) i didn't pay attention for the post date)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC