in reply to Re^4: How to avoid Null Byte Injection?
in thread How to avoid Null Byte Injection?

While your script is running, it will mark variables that come from outside the program as tainted. If you try to perform an unsafe operation with a tainted variable, the program will be killed instead of executing that operation.

However, it actually looks like Perl doesn't consider opening a file readonly to be an unsafe operation, which is surprising to me. So it wouldn't have solved your security problem, but it would help with something like this (which opens a file for append):

#!/usr/bin/perl -T use warnings; use strict; use Scalar::Util qw(tainted); use CGI qw(:standard); BEGIN { if ($ENV{REQUEST_METHOD}) { eval 'use CGI::Carp qw(fatalsToBrowser)'; } } use constant EOL => $ENV{REQUEST_METHOD} ? "<br>\n" : "\n"; if ($ENV{REQUEST_METHOD}) { start_html(); } my $file = param('file') or die "No filename given"; # The next 3 lines will untaint the variable. $file =~ /^(\w+)$/ or die "illegal filename\n"; $file = $1; printf "\$file is%s tainted.".EOL,tainted($file)?"":"n't"; open(F,">> /tmp/$file") or die "Couldn't open file"; print F "Wrote.\n"; close(F); print "OK".EOL; if ($ENV{REQUEST_METHOD}) { end_html(); }

Update: This version should work from the command-line or CGI.

Replies are listed 'Best First'.
Re^6: How to avoid Null Byte Injection?
by Nik (Initiate) on Oct 09, 2006 at 17:30 UTC
    Thanks but to me it doesnt run at all!
      Based on some earlier posts, I suspect you were running it as a CGI script, when it was originally designed to be run from the command-line. I've updated it so it should work in either environment.
        Thanks i will nwo try it.
        Could you pzl make me favour and try to see if you cann pass bogus input to http://nikos.no-ip.org so to see if a crafty user can still send unwanted code to my index.pl(or any other) and beak it?

        Plz its crucial to me to see if i made my script secure enough.
        I have changes a lot of things in all my scripts(perl code) and also added DigestAuth functionality to prevent certain unwated access.
        Plz give it a shot!