CGI Scripting and Vulnerabilities COEN 351 Ecommerce Security
CGI Scripting and Vulnerabilities COEN 351: E-commerce Security Thomas Schwarz, S. J. 2006
CGI with Perl Fundamentals n Webserver passes information to the CGI script via environmental variables. n n n %ENV hash CGI scripts produce output by printing an HTTP message on STDOUT CGI scripts need to put out an HTTP header, but it does not have to be a full one.
CGI with Perl Fundamentals n Perl has three standard file handles. n STDIN n n STDOUT n n n Webservers passes request (with the header removed) to the cgi script. If there is post data, it will be available for reading from STDIN. There is no end-of-file marker, so read the content-length header to decide when you read the end-of-input, otherwise the script will hang. Perl writes HTTP header and body through STDOUT. Different webservers have different buffering policies. STDERR n n Perl can send error messages to STDERR. However, webservers differ in how they treat the output. n n Apache puts STDERR output into the log. i. Planet puts STDERR into the HTTP, but probably out of order, because STDERR traffic is not buffered.
CGI with Perl Fundamentals n You are now ready to create a webpage in your home directory n n index. html Next step is to try a cgi script. #!/perl/bin/perl -w. T print "Content-type: text/htmlnn"; print "<h 1>Hi</h 1>n"; Path to the perl executable. Different from UNIX!
CGI with Perl Fundamentals n Creating dynamic web-pages with PERL n n n Web server passes information to CGI scripts via environment variables. CGI scripts produce output by printing the HTTP message on STDOUT. CGI scripts do not need to printout full headers.
CGI with Perl Fundamentals
CGI with Perl Fundamentals n This script uses only a simple header. n Notice the double lines in the first print statement. n n This generates a basic HTTP message. HTTP requests:
CGI with Perl Fundamentals n The minimum requirement for a static website are: n n The “Content-Type” line. The document itself. n Need to include the she-bang line. n n n Use taint mode as a generic precaution. Use the CGI: : Carp Perl module Perl has a handy short-cut to print out many lines of text.
CGI with Perl Fundamentals n Header Types n n n Content-type header Redirection Status Message
CGI with Perl Fundamentals #! /perl/bin/perl -w. T use CGI: : Carp qw(warnings. To. Browser fatals. To. Browser); print <<EHTML; Content-type: text/html Shebang with path to Perl Sends diagnostic messages to the browser. Remove before posting it. <html> <head><title>Environmental Variables</title></head> <body> <h 1>Hi</h 1> <pre> Server $ENV{SERVER_NAME} Listening port $ENV{SERVER_PORT} Server software $ENV{SERVER_SOFTWARE} Server protocol $ENV{SERVER_PROTOCOL} CGI version $ENV{GATEWAY_INTERFACE} </pre> </body> </html> EHTML Environmental variables This allows you to just type in code instead of using individual print statements. The closing EHTML (or whatever token you choose) needs to be in the first position in the line and followed by an empty line.
CGI with Perl Fundamentals #! /perl/bin/perl -w. T use CGI: : Carp qw(warnings. To. Browser fatals. To. Browser); print <<EHTML; Content-type: text/html <html> <head><title>Environmental Variables</title></head> <body> <h 1>Hi</h 1> <pre> Server $ENV{SERVER_NAME} Listening port $ENV{SERVER_PORT} Server software $ENV{SERVER_SOFTWARE} Server protocol $ENV{SERVER_PROTOCOL} CGI version $ENV{GATEWAY_INTERFACE} </pre> </body> </html> EHTML
CGI with Perl Fundamentals n Environmental Variables n n n n AUTH_TYPE CONTENT_LENGTH CONTENT_TYPE DOCUMENT_ROOT GATEWAY_INTERFACE PATH_INFO PATH_TRANSLATED
CGI with Perl Fundamentals n Environmental Variables n n QUERY_STRING REMOTE_ADDR REMOTE_HOST REMOTE_IDENT n n n Ident daemon: UNIX and IRC clients only REMOTE_USER REQUEST_METHOD
CGI with Perl Fundamentals n Environmental Variables n n SCRIPT_NAME SERVER_PROTOCOL SERVER_SOFTWARE
CGI with Perl Fundamentals n Additional CGI Environment Variables: n n n n n HTTP_ACCEPT_CHARSET HTTP_ACCEPT_ENCODING HTTP_ACCEPT_LANGUAGE HTTP_COOKIE HTTP_FROM HTTP_HOST HTTP_REFERER HTTP_USER_AGENT
CGI with Perl Fundamentals n Environmental Variables n Secure server adds many more environmental variables. n n X. 509 server / browser certificates HTTPS n n Used as a flag to indicate whether the connection is secure. Values vary by server n “ON”, “on”, “Off”, “off”
CGI with Perl Fundamentals #!/perl/bin/perl -w. T use CGI qw(: standard); use CGI: : Carp qw(warnings. To. Browser fatals. To. Browser); my $email = "tjschwarz@scu. edu"; my $url = "http: //www. cse. scu. edu"; print header; print start_html("Scalars"); print <<End. HTML; <h 2>Hello</h 2> <p> My e-mail address is $email, and my web url is <a href="$url">$url</a>. </p> End. HTML print end_html;
CGI with Perl Fundamentals
CGI with Perl Fundamentals n CGI can output full or partial headers. n Partial headers: n n Content-type header Location header n n Specifies URL to redirect the client to. Status header n n One of E. g. “ 204 No response” Delimited by TWO new-lines
CGI with Perl Fundamentals
CGI with Perl Fundamentals n n When using a code, remember that the HTTP status message is not displayed. Therefore, you might want to formulate your own error page.
CGI with Perl Fundamentals n Complete Headers: n n n Need status line. Need Content-type line Need Server header. n n The last two are given to you as environmental variables. Called nph (non-parsed header) scripts
CGI: Forms COEN 351
CGI: Getting Data from Client n n HTML provides forms as a means to gather information and send them to the server. Use either POST or GET method.
CGI: Getting Data from Client n HTML form tags n <FORM ACTION = “register. cgi” METHOD = “POST”> n n METHOD: Either GET or POST ACTION: URL of the script that should receive the HTTP request. n n n Default is the same URL ENCTYPE: Specifies the media type used to encode the request. Default is usually adequate. on. Submit: Javascript handler.
Getting Data from Client
Getting Data from Client
Getting Data from Client n n Script register. cgi receives data. HTTP request looks like this: POST register. cgi HTTP/1. 1 Host: bobadilla. engr. scu. edu Content-Length: 11 Content-Type: application/x-www-form-urlencode name=thomas
Getting Data from Client
Getting Data from Client
Getting Data from Client n To read the data: n Read the data from the query string: n n Determine the method n n n $ENV{REQUEST_METHOD} If the method is POST, determine the size of the request n n $ENV{QUERY_STRING} $ENV{CONTENT_LENGTH} Read that amount of data from STDIN Parse the data and process it.
Getting Data from Client Determine the request method Read up to $ENV{CONTENT_LENGTH} from stdin
Getting Data from Client
Getting Data from Client
Getting Data from Client n n n In principle, you can write a perl parse function that will parse the input and give it to you in nice value-pair form. In reality, you want to use a perl module that prepares the input for you. See next week’s cgi lesson.
CGI: CGI. pm COEN 351
CGI. pm n Perl Modules n Pre-written code. n n n Standard library modules. Other modules e. g. at Comprehensive Perl Archive Network. CGI. pm module n n Load with “use CGI qw(: standard); ” Has various function names: n n n header start_html end_html
CGI. pm n CGI. pm handles n Input n n Replaces environment variables with environment methods HTML output n Easy handling of http headers n n start_html, end_html Error handling
CGI. pm n Comes with two small vulnerabilities of the DOS type n n Can be fixed by setting values in CGI. pm Allows uploading arbitrarily large files. n n Set $DISABLE_UPLOADS = 1. Allows arbitrarily large post messages n Set $POST_MAX = 102_400; #100 KB max
CGI. pm n CGI. pm module n print start_html(“hello”) n n Prints out: <html><head><title>hello</title></head><body> end_html n Prints out: </body></html>
CGI. pm n CGI. pm can be used in an object-oriented and in an imperative style. Imperative version use CGI qw(: standard); print header; print start_html("Hello World"); Object-Oriented Version use CGI; # don't need qw(: standard) $cgi = CGI->new; # ($cgi is now the object) print $cgi->header; # function call: $obj->function print $cgi->start_html("Hello World");
CGI. PM Output http: //perldoc. perl. org/CGI. html
CGI. PM Handling Output n Simple method calls to generate html output: n $q->header n q->header( -type => "text. html", -target => "main_frame", -expires => "+30 m", -status => "444 What's that");
CGI. PM Handling Output n n n q->start_html q->end_html $q->hr $q->h 1(. . . ) $q->h 2(. . . ) $q->p(…)
CGI. PM Handling Output n Form Elements such as: n n n n n start_form end_form textfield password_field filefield button submit reset hidden. . .
CGI. PM Handling Output #!/perl/bin/perl. exe use strict; use CGI; my $q = new CGI; print $q->header("text/html"), $q->start_html( -title => "Env Var", -bgcolor => "#f 0 f 0 f 0"), $q->h 3("HTTP Environmental Variables"); foreach( $q->http) { print $q->p($_. ": ", $q->http( $_ ), " "); } $q->end_html;
CGI. PM Handling Output n The example demonstrates output as well as access to the http environmental variables. n The latter are accessed through the http method.
CGI. PM Handling Output #!/perl/bin/perl. exe use strict; use CGI; my $q = new CGI; print $q->header("text/html"), $q->start_html( -title => "Env Var", -bgcolor => "#f 0 f 0 f 0"), $q->h 3("HTTP Environmental Variables"); foreach( $q->http) { print $q->p($_. ": ", $q->http( $_ ), " "); } $q->end_html;
CGI. pm n Alternatives for output n CGI methods n n Lots of print statements n n Compact, but limited expressionability Lots of typing, easy to control “here document” feature in Perl n Straight html text from perl
CGI. PM Handling Input http: //perldoc. perl. org/CGI. html
CGI. pm n Input with CGI. pm n Use Methods instead of Environmental Variables. content-type CONTENT_TYPE query_string QUERY_STRING remote_host REMOTE_HOST server_software SERVER_SOFTWARE url Not available CONTENT_LENGTH virtual_host HTTP_HOST
CGI. pm Input n Forms n n Allow browser to post data to server. Uses GET or POST message
CGI. pm Input Form using POST method
CGI. pm Input CGI. pm Form using GET method Notice query string
CGI. pm Input n HTTP request with POST is POST f 1. cgi HTTP/1. 1 Host: localhost Content-Length: 40 Content-Type: application/x-www-form-urlencode name=Thomas+Schwarz&email=tschwarz%40 scu. edu n HTTP request with GET is /f 1. cgi? name=Thomas+Schwarz&email=tschwarz%40 scu. edu
CGI. pm Input n We get input from both POST and GET methods with the param method. n n n param determines whether POST and GET is used. Under normal circumstances, param does not give you access to the query string if you are using POST. Work-around: n n Use url_param Change CGI. PM
CGI. pm n Using the CGI. pm module makes things much easier.
CGI. pm n Accessing environmental variables n http method n Without argument: n n Name of the environmental variable currently available. With argument: n The value of that environmental variable.
CGI. pm
CGI. pm We access parameters through the param method.
CGI. pm n Trapping Errors: n n Standard Perl construct “or die” sends output to stderr, which may or may not be sent to the client. Trapping die will work: eval { dangerous_stuff(); 1; } or do { error ($q, $@ || “Unknown Error” );
CGI. pm n Trapping Errors n n Trapping die will generate difficult to read code. Use CGI: : Carp n n Clean interface and code Quite powerful
CGI: Maintaining State COEN 351
CGI: Maintaining State n HTTP is a stateless protocol. n n TCP connection might be closed after each request! In order to maintain state, we can use: n n n Hidden Fields: Fat URLs Extra path information: Fat URLs Cookies
CGI: Maintaining State with Cookies n Cookie Mechanism n n Webserver sends a Set-Cookie HTTP header to the browser. Browser returns cookie in its cookie header.
CGI: Maintaining State with Cookies n Netscape Cookies Parameters: n -name n n -value -domain n n Name of cookie We can set several cookies Browsers will only return the cookies for URLs within this domain. -expires -path -secure n Browser will only return the cookie for secure URLs using https
CGI: Maintaining State with Cookies n Setting cookies: n CGI. pl has a cookie constructor: my $cookie = $q->cookie( -name => "student_id", -value => 11111, -domain => ". scu. edu", -expires => "+1 y", ); n CGI. pl allows you to construct headers easily: print $q->header( -type => "text/html", -cookie => $cookie );
CGI: Maintaining State with Cookies Capture of cookie slapping
CGI: Maintaining State with Cookies n Getting cookies n n Available in the HTTP_COOKIE environment. Can get value directly from CGI. pl: my $cookie = $q->cookie( "student_id"); print $q->header( -type => "text/plain" ), $cookie;
CGI: Maintaining State with Cookies n Security Issues with Cookies: n Cookies can be altered n n Sensitive cookie values need to be fully encrypted Cannot trust expiration date
CGI: Maintaining State: Query Strings Query strings are set by the GET http method n n To maintain state via query strings: 1. Handling all requests through cgi n 2. Change web-server settings Use regular expression to parse query string for fields
CGI: Maintaining State: Query Strings Performance suffers n n n Static webpages impossible Use mod-perl etc. to speed up cgi processing
CGI: Maintaining State: Hidden Fields n n n Hidden fields in forms are not displayed in browser, but are still sent to webserver. Hidden fields have no performance overhead and always work, BUT Hidden fields are easily altered and cannot be trusted
CGI: Maintaining State: Hidden Fields n Maintain state at web-server n n Use persistent files or database to maintain state. Performance suffers, but security is highest.
- Slides: 74