Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Is this safe??

by SilverB1rd (Scribe)
on Feb 07, 2001 at 00:07 UTC ( [id://56763] : perlquestion . print w/replies, xml ) Need Help??

SilverB1rd has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl print "Content-type:text/html\n\n"; $FORM{'fulltxtpath'} = stripmeta("../www$ENV{'QUERY_STRING'}"); read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; } foreach $key (keys(%FORM)) { if ($key =~ /required/i) {if ( ($FORM{$key} eq "") && ($FORM{$key} == +"") ) {failure();}} } open INF, $FORM{'fulltxtpath'} or dienice("Cant open $FORM{'fulltxtpat +h'}"); seek(INF,0,0); @fary = <INF>; close (INF); foreach $key (keys(%FORM)) { $rep = $FORM{$key}; foreach $i (@fary) {$i =~ s/\[$key\]/$rep/g;} } $mailprog = '/usr/lib/sendmail'; open (MAIL, "|$mailprog -t") or dienice("Can't access $mailprog!\n"); foreach $i (@fary) { print MAIL "$i"; } close (MAIL); $FORM{'success'} =~ s/https\:\/\/\/www/g; open INF, $FORM{'success'} or dienice("Cant open $FORM{'success'}"); seek(INF,0,0); @succtxt = <INF>; close (INF); foreach $i (@succtxt) { print "$i"; } exit; sub stripmeta { my($var1) = @_; @meta = ('&',';','`','\'','\\', '"','|','*','?','~','<','>', '^','(',')','[',']','{','}','$'); $var1 =~ s/\n/ /g; $var1 =~ s/\r/ /g; foreach $met (@meta){ $var1 =~ s/\Q$met\E/ /g; } return $var1 } sub failure { $FORM{'failure'} =~ s/https\:\/\/\/www/g; open INF, $FORM{'failure'} or dienice("Cant open $FORM{'failure'}"); seek(INF,0,0); @failtxt = <INF>; close (INF); foreach $i (@failtxt) { print "$i"; } exit; } sub dienice { my($errmsg) = @_; my($webmaster) = ''; print <<Eof; <html><head><title>Error!!</title></head><body>The error was $errmsg</ +body></html> Eof exit; }
Will me stripmeta sub protect me from attacks like ;/ rm -r*;/ in the 'QUERY_STRING'??

Replies are listed 'Best First'.
Sanitizing user-provided path/filenames
by Fastolfe (Vicar) on Feb 07, 2001 at 00:24 UTC

    If you really must rely on user-provided data that maps directly to path/filenames, and can't use a token system to represent the same thing, I would explicitely declare what your valid "root" directory is, and do a check like this:

    use CGI ':standard'; use File::Spec 'rel2abs'; my $ROOT = "/var/myapp/docroot/"; # wherever my $user_path = param('path'); # perhaps s/^\/+// also my $absolute = rel2abs($user_path, $ROOT); if ($absolute =~ /^\Q$ROOT/) { # $absolute is probably within $ROOT, so process it if (open(INF, "< $absolute")) { # it's here, do whatever } else { # "404 not found" } } else { # ERROR - They've tried to ../ their way out }

    Keep in mind, though, that this still lets them ../ their way anywhere they want under your declared $ROOT, so if you're expecting a filename to be in a certain place or under a certain hierarchy under your $ROOT, you need to do some additional checking/tokenizing to be sure that it actually does end up there. All this code does is keep the user sandboxed.

    I too highly recommend reading perlsec and using taint-checking (-T) to better prepare yourself for potentially unsafe user-provided data.

Re: Is this safe??
by chromatic (Archbishop) on Feb 07, 2001 at 00:27 UTC
    On first glance, probably. On the other hand, are you willing to take the chance? Who knows what shell metacharacters may be used in the future? I'd rather deny everything I don't explicitly allow than allow everything I don't explicitly deny.

    On the other hand, I would say it's fairly unlikely you'll have a query string AND posted data at the same time, for a normal operation. So it may be a moot point, as I don't expect this script to work very well.

    I would recommend you read perlsec and enable Taint mode. Also enable warnings with perl -w and use the strict module. You should also use CGI for your form handling (see my home node or Ovid's CGI course for details).

    If you want to be more paranoid, I would build up a hash of *allowed* files, then check the input against them:

    my %files = ( '/var/www/index.html' => 1, '/var/www/home/index.html' => 1; ); my $request = $query->param('fulltxtpath'); if (exists($files{$request})) { open (FILE, "> " . $request) or dienice("couldn't open $request: $ +!"); # open and print file } else { # error }
    Better yet, use opendir and readdir to build a list of allowed files. You might even use the keys of the hash as their values, and use $files{$request} in the open statement.

    Finally, I'd replace the looping substitutions with a transliteration:

    $var1 =~ tr/a-zA-Z0-9\/\.\-_//dc;

    That's reasonably strict.

      I was trying to copy the way this other cgi that my company was using, that stoped working for who knows why, this is way it uses file names passed in by the user.

      "I would say it's fairly unlikely you'll have a query string AND posted data at the same time"
      The mail program uses a template for the email it sends, this txt file is specified in the action line like so action="" The form data is sent via the post method.

      This script works fine, I just dont want to miss anything. What do I need to check to make sure I'm not letting anything slip by? is it the data being used to open files? or all the data being sent?
        What do I need to check to make sure I'm not letting anything slip by?

        As described above, use both taint mode and as follows:

        #!/usr/bin/perl -wT #warnings and taint mode now enabled use CGI; #you may now use the CGI methods.
        It would scare me greatly if you copied this code from a company web page since those pages are now vunerable to a variety of DoS and hack attacks. CGI has tested-true functions for nearly everything you do above. It looks like you could reduce that program to 10 lines or less OF SECURE CODE!

        On CGI security: If you're not using the inputted data in shell calls, then you're relatively safe. Taint mode specifically checks for this. Of course, some loser can still input some random schlop, so work your CGI with "valid entry or reject"/deny-allow in mind. As I said earlier, provides numerous functions for EXACTLY what you are doing (and doing incompletely), including character escaping, so click that link and read the docs on it.

        AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
Re: Is this safe??
by Hot Pastrami (Monk) on Feb 07, 2001 at 00:17 UTC
    Use for grabbing the parameters, and read up on Taint mode for protecting against such attacks. A search for 'Taint' on this site should yield lots of good results.

    Update: After I replied I looked at what this code actually does... you must be very cautious. You are allowing some very dangerous things to be specified by the user, such as paths to files. This approach invites grave danger to the security of your server.

    Hot Pastrami