A Common CGI Security Hole in PERL Scripts

CS4234 -  - WWW: The Underlying Technology

Problem:


Example (insecure) PERL script for "finger" on the WWW:

Enter one or more user names.  This form will look the user names up on the server and give you information about the users.  (Done via the "finger" utility.)


 
 



Above form invokes this perl code:

#!/usr/local/bin/perl
require "cgi-lib.pl";
&ReadParse(*input); #query is sent w/ FORM action
# $query = $ENV{'QUERY_STRING'}; #query is sent w/ GET action
print "Context-type:text/html\n\n"; # http header...REQUIRED
print "<HTML><HEAD><TITLE>Finger Script</TITLE>";
print "</HEAD>\n<BODY>\n<PRE>\n";
$fin_out = `finger $input{usernames}`;
print "$fin_out\n</PRE>\n</BODY></HTML>;

Notes:



Why is the form above a potential security hole?

This code uses "tainted" variables in system calls:
 

How to Solve Problem

#!/usr/local/bin/perl -T
require "cgi-lib.pl";
&ReadParse(*input); #query is sent w/ FORM action
# $query = $ENV{'QUERY_STRING'}; #query is sent w/ GET action
print "Context-type:text/html\n\n"; # http header...REQUIRED
print "<HTML><HEAD><TITLE>Finger Script</TITLE>";
print "</HEAD>\n<BODY>\n<PRE>\n";
$input{usernames} =~ /^([\w.]*)$/; #untaint
$fin_out = $1;
$out_line = `finger $fin_out`;
print "$out_line\n</PRE>\n</BODY></HTML>;

Use of "taintperl" disallows certain actions.


Hints for Secure CGI Programming


References:

  1. Wall,Larry and Randal L. Schwartz, "Programming PERL", O'Reilly & Associates, © 1991, pp. 258,374-375 ...(the Camel Book).
  2. Stein, Lincoln "How To Set Up and Maintain a World Wide Web Site", Addison Wesley © 1995, pp 385-389.
  3. Stein, Lincoln "The WWW Security FAQ"....and the Chapter on Safe PERL scripts.

Last updated on 25 October 2000 by abrams@vt.edu.  Original material by Stephen Williams.