CGI Scripting and Vulnerabilities COEN 351: E-commerce Security  Thomas Schwarz, S.J. 2006.

Slides:



Advertisements
Similar presentations
CGI & HTML forms CGI Common Gateway Interface  A web server is only a pipe between user-agents  and content – it does not generate content.
Advertisements

Browsers and Servers CGI Processing Model ( Common Gateway Interface ) © Norman White, 2013.
CGI Programming.
CGI Programming Part 2. Input Tags Many different ways of getting data from the user. The tag is used most often. has a type attribute –Specifies the.
Common Gateway Interface (CGI). CGI is a protocol: CGI is not a programming language CGI is a protocol for the exchange of information between between.
Browsers and Servers CGI Processing Model ( Common Gateway Interface ) © Norman White, 2013.
How does the server format the information it gives to the appln program? As environment variables and in standard input.
CGI Programming in Perl Software Tools. Lecture 22 / Slide 2 CGI Programming l Last time we looked at designing a static web page. Today we will see how.
CGI programming in Perl Learning Objectives: 1. To understand how a CGI program works in Perl and how to make it runnable in web browsers 2. To learn how.
1 CGI The Common Gateway Interface E-Commerce Prof. Sheizaf Rafaeli.
Outcomes Know what are CGI Environment Variables Know how to use environment variables How to process A simple Query Form Able to use URL Encoding rules.
CGI. CGI Programming What is "CGI"? –Common Gateway Interface A means of running an executable program via the Web. CGI is not a Perl-specific concept.
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
CGI Programming Languages Web Based Software Development July 21, 2005 Song, JaeHa.
Common Gateway Interface
1 ‘Dynamic’ Web Pages So far, we have developed ‘static’ web-pages, e.g., cv.html, repair.html and order.html. There is often a requirement to produce.
Chapter 9 Using Perl for CGI Programming. Computation is required to support sophisticated web applications Computation can be done by the server or the.
1 Homework / Exam Exam 3 –Solutions Posted –Questions? HW8 due next class Final Exam –See posted schedule Websites on UNIX systems Course Evaluations.
Overview A plain HTML document is static A CGI program is executed in real-time, so that it can output dynamic information. CGI (Common Gateway Interface)
CP3024 Lecture 3 Server Side Facilities. Lecture contents  Server side includes  Common gateway interface (CGI)  PHP Hypertext Preprocessor (PHP) pages.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
1 HTML and CGI Scripting CSC8304 – Computing Environments for Bioinformatics - Lecture 10.
Comp2513 Forms and CGI Server Applications Daniel L. Silver, Ph.D.
Chapter 33 CGI Technology for Dynamic Web Documents There are two alternative forms of retrieving web documents. Instead of retrieving static HTML documents,
CGI Scripting and Vulnerabilities COEN 351: E-commerce Security  Thomas Schwarz, S.J
2 1 Sending Data Using a Hyperlink CGI/Perl Programming By Diane Zak.
Web Server Design Week 14 Old Dominion University Department of Computer Science CS 495/595 Spring 2010 Martin Klein 4/14/10.
USING PERL FOR CGI PROGRAMMING
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: HTTP and CGI Fundamentals of Web Programming.
CGI Security COEN 351. CGI Security Security holes are exploited by user input. We need to check user input against Buffer overflows etc. that cause a.
CSCE Systems Programming Lecture 21 Web Server: CGI -Dynamic Pages CSCE March 25, 2013.
10/13/2015 ©2006 Scott Miller, University of Victoria 1 Content Serving Static vs. Dynamic Content Web Servers Server Flow Control Rev. 2.0.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Using Perl for CGI Programming.
Website Development with PHP and MySQL Saving Data.
CGI Programming. What is "CGI"? Common Gateway Interface A means of running an executable program via the Web. CGI is not a Perl-specific concept. Almost.
CGI Programming. What is it? CGI –Common Gateway Interface Standard way to pass information back to the Web Server –GET Query String –POST Standard Input.
1 Basic Perl CGI Programming. 2 Issues How and when your program is invoked. Generating Response –HTTP Headers –HTML (or whatever document type you want)
CGI programming Peter Verhás January What this tutorial is about Introduction to CGI programming Using ScriptBasic –Simple to program –Simple to.
Chapter 9: Perl and CGI Programming CGI Programming Acknowledgement: Some materials are taken from Teach Yourself CGI Programming with PERL 5 in a Week.
1 © Netskills Quality Internet Training, University of Newcastle HTML Forms © Netskills, Quality Internet Training, University of Newcastle Netskills is.
1 CGI with perl. 2 References Perl tutorials and references:
Perl CGI What is "CGI"? Common Gateway Interface A means of running an executable program via the Web. Perl have a *very* nice interface to create CGI.
CGI Scripting and Vulnerabilities COEN 351: E-commerce Security.
Form Data Encoding GET – URL encoded POST – URL encoded
Introduction to Programming the WWW I CMSC Winter 2003.
WWW: an Internet application Bill Chu. © Bei-Tseng Chu Aug 2000 WWW Web and HTTP WWW web is an interconnected information servers each server maintains.
Netprog 2002 CGI Programming1 CGI Programming CLIENT HTTP SERVER CGI Program http request http response setenv(), dup(), fork(), exec(),...
Intermediate CGI & CGI.pm Webmaster II - Fort Collins, CO Copyright © XTR Systems, LLC CGI Programming & The CGI.pm Perl Module Instructor: Joseph DiVerdi,
Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets.
Operating Systems Lesson 12. HTTP vs HTML HTML: hypertext markup language ◦ Definitions of tags that are added to Web documents to control their appearance.
Krerk Piromsopa. 1 Department of Computer Engineering. Chulalongkorn University. Web Application Generic Issues.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
Form Processing Week Four. Form Processing Concepts The principal tool used to process Web forms stored on UNIX servers is a CGI (Common Gateway Interface)
CS 330 Class 8 Homework A pattern that contains a word with an optional period A pattern that contains Fred with a space (not Freddy) See regexp.txt guest4.htm.
Introduction to CGI PROG. CGI stands for Common Gateway Interface. CGI is a standard programming interface to Web servers that gives us a way to make.
PHP: Further Skills 02 By Trevor Adams. Topics covered Persistence What is it? Why do we need it? Basic Persistence Hidden form fields Query strings Cookies.
Web Server Design Week 13 Old Dominion University Department of Computer Science CS 495/595 Spring 2012 Michael L. Nelson 04/03/12.
Web Server Design Week 15 Old Dominion University Department of Computer Science CS 495/595 Spring 2009 Michael L. Nelson 4/20/09.
Lesson 11. CGI CGI is the interface between a Web page or browser and a Web server that is running a certain program/script. The CGI (Common Gateway Interface)
University of Kansas Department of Electrical Engineering and Computer Science Dr. Susan Gauch April 21, 2005 I T T C Introduction to Web Technologies.
The Common Gateway Interface (CGI) Pat Morin COMP2405.
Chapter 7 - Introduction to Common Gateway Interface (CGI)
WWW and HTTP King Fahd University of Petroleum & Minerals
CGI I: Basics Web Programming.
CGI Programming Part II UNIX Security
The HTTP Protocol COSC 2206 Internet Tools The HTTP Protocol
Environment Variables
An Example of a TCP/IP Application: the World Wide Web
Web Server Design Week 16 Old Dominion University
CGI I: Basics Web Programming.
Presentation transcript:

CGI Scripting and Vulnerabilities COEN 351: E-commerce Security  Thomas Schwarz, S.J. 2006

CGI with Perl Fundamentals Webserver passes information to the CGI script via environmental variables. %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 Perl has three standard file handles. STDIN 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. STDOUT Perl writes HTTP header and body through STDOUT. Different webservers have different buffering policies. STDERR Perl can send error messages to STDERR. However, webservers differ in how they treat the output. Apache puts STDERR output into the log. iPlanet puts STDERR into the HTTP, but probably out of order, because STDERR traffic is not buffered.

CGI with Perl Fundamentals You are now ready to create a webpage in your home directory index.html Next step is to try a cgi script. #!/perl/bin/perl -wT print "Content-type: text/html\n\n"; print " Hi \n"; Path to the perl executable. Different from UNIX!

CGI with Perl Fundamentals Creating dynamic web-pages with PERL 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

This script uses only a simple header. Notice the double lines in the first print statement. This generates a basic HTTP message. HTTP requests:

CGI with Perl Fundamentals The minimum requirement for a static website are: The “Content-Type” line. The document itself. Need to include the she-bang line. 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 Header Types Content-type header Redirection Status Message

CGI with Perl Fundamentals #! /perl/bin/perl -wT use CGI::Carp qw(warningsToBrowser fatalsToBrowser); print <<EHTML; Content-type: text/html Environmental Variables Hi Server $ENV{SERVER_NAME} Listening port $ENV{SERVER_PORT} Server software $ENV{SERVER_SOFTWARE} Server protocol $ENV{SERVER_PROTOCOL} CGI version $ENV{GATEWAY_INTERFACE} EHTML Shebang with path to Perl Sends diagnostic messages to the browser. Remove before posting it. 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. Environmental variables

CGI with Perl Fundamentals #! /perl/bin/perl -wT use CGI::Carp qw(warningsToBrowser fatalsToBrowser); print <<EHTML; Content-type: text/html Environmental Variables Hi Server $ENV{SERVER_NAME} Listening port $ENV{SERVER_PORT} Server software $ENV{SERVER_SOFTWARE} Server protocol $ENV{SERVER_PROTOCOL} CGI version $ENV{GATEWAY_INTERFACE} EHTML

CGI with Perl Fundamentals Environmental Variables AUTH_TYPE CONTENT_LENGTH CONTENT_TYPE DOCUMENT_ROOT GATEWAY_INTERFACE PATH_INFO PATH_TRANSLATED

CGI with Perl Fundamentals Environmental Variables QUERY_STRING REMOTE_ADDR REMOTE_HOST REMOTE_IDENT Ident daemon: UNIX and IRC clients only REMOTE_USER REQUEST_METHOD

CGI with Perl Fundamentals Environmental Variables SCRIPT_NAME SERVER_NAME SERVER_PROTOCOL SERVER_SOFTWARE

CGI with Perl Fundamentals Additional CGI Environment Variables: HTTP_ACCEPT HTTP_ACCEPT_CHARSET HTTP_ACCEPT_ENCODING HTTP_ACCEPT_LANGUAGE HTTP_COOKIE HTTP_FROM HTTP_HOST HTTP_REFERER HTTP_USER_AGENT

CGI with Perl Fundamentals Environmental Variables Secure server adds many more environmental variables. X.509 server / browser certificates HTTPS Used as a flag to indicate whether the connection is secure. Values vary by server “ON”, “on”, “Off”, “off”

CGI with Perl Fundamentals #!/perl/bin/perl -wT use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); my $ = my $url = " print header; print start_html("Scalars"); print <<EndHTML; Hello My address is $ , and my web url is $url. EndHTML print end_html;

CGI with Perl Fundamentals

CGI can output full or partial headers. Partial headers: One of Content-type header Location header Specifies URL to redirect the client to. Status header E.g. “204 No response” Delimited by TWO new-lines

CGI with Perl Fundamentals

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 Complete Headers: Need status line. Need Content-type line Need Server header. 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 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 HTML form tags METHOD: Either GET or POST ACTION: URL of the script that should receive the HTTP request. Default is the same URL ENCTYPE: Specifies the media type used to encode the request. Default is usually adequate. onSubmit: Javascript handler.

Getting Data from Client

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

To read the data: Read the data from the query string: $ENV{QUERY_STRING} Determine the method $ENV{REQUEST_METHOD} If the method is POST, determine the size of the request $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

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. Getting Data from Client

CGI: CGI.pm COEN 351

CGI.pm Perl Modules Pre-written code. Standard library modules. Other modules e.g. at Comprehensive Perl Archive Network. CGI.pm module Load with “use CGI qw(:standard);” Has various function names: header start_html end_html

CGI.pm CGI.pm handles Input Replaces environment variables with environment methods HTML output Easy handling of http headers start_html, end_html Error handling

CGI.pm Comes with two small vulnerabilities of the DOS type Can be fixed by setting values in CGI.pm Allows uploading arbitrarily large files. Set $DISABLE_UPLOADS = 1. Allows arbitrarily large post messages Set $POST_MAX = 102_400; #100KB max

CGI.pm CGI.pm module print start_html(“hello”) Prints out: hello end_html Prints out:

CGI.pm 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

CGI.PM Handling Output Simple method calls to generate html output: $q->header q->header( -type => "text.html", -target => "main_frame", -expires => "+30m", -status => "444 What's that");

CGI.PM Handling Output q->start_html q->end_html $q->hr $q->h1(...) $q->h2(...) $q->p(…)

CGI.PM Handling Output Form Elements such as: 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 => "#f0f0f0"), $q->h3("HTTP Environmental Variables"); foreach( $q->http) { print $q->p($_.": ",$q->http( $_ ), " "); } $q->end_html;

CGI.PM Handling Output The example demonstrates output as well as access to the http environmental variables. 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 => "#f0f0f0"), $q->h3("HTTP Environmental Variables"); foreach( $q->http) { print $q->p($_.": ",$q->http( $_ ), " "); } $q->end_html;

CGI.pm Alternatives for output CGI methods Compact, but limited expressionability Lots of print statements Lots of typing, easy to control “here document” feature in Perl Straight html text from perl

CGI.PM Handling Input

CGI.pm Input with CGI.pm Use Methods instead of Environmental Variables. content-typeCONTENT_TYPE query_stringQUERY_STRING remote_hostREMOTE_HOST server_softwareSERVER_SOFTWARE urlNot available CONTENT_LENGTH virtual_hostHTTP_HOST

CGI.pm Input Forms 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 HTTP request with POST is POST f1.cgi HTTP/1.1 Host: localhost Content-Length: 40 Content-Type: application/x-www-form-urlencode name=Thomas+Schwarz& =tschwarz%40scu.edu HTTP request with GET is /f1.cgi?name=Thomas+Schwarz& =tschwarz%40scu.edu

CGI.pm Input We get input from both POST and GET methods with the param method. 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: Use url_param Change CGI.PM

CGI.pm Using the CGI.pm module makes things much easier.

CGI.pm Accessing environmental variables http method Without argument: Name of the environmental variable currently available. With argument: The value of that environmental variable.

CGI.pm

We access parameters through the param method.

CGI.pm Trapping Errors: 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 Trapping Errors Trapping die will generate difficult to read code. Use CGI::Carp Clean interface and code Quite powerful

CGI: Maintaining State COEN 351

CGI: Maintaining State HTTP is a stateless protocol. TCP connection might be closed after each request! In order to maintain state, we can use: Hidden Fields: Fat URLs Extra path information: Fat URLs Cookies

CGI: Maintaining State with Cookies Cookie Mechanism Webserver sends a Set-Cookie HTTP header to the browser. Browser returns cookie in its cookie header.

CGI: Maintaining State with Cookies Netscape Cookies Parameters: -name Name of cookie We can set several cookies -value -domain Browsers will only return the cookies for URLs within this domain. -expires -path -secure Browser will only return the cookie for secure URLs using https

CGI: Maintaining State with Cookies Setting cookies: CGI.pl has a cookie constructor: CGI.pl allows you to construct headers easily: my $cookie = $q->cookie( -name => "student_id", -value => 11111, -domain => ".scu.edu", -expires => "+1y", ); print $q->header( -type => "text/html", -cookie => $cookie );

CGI: Maintaining State with Cookies Capture of cookie slapping

CGI: Maintaining State with Cookies Getting cookies 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 Security Issues with Cookies: Cookies can be altered 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 To maintain state via query strings: 1. Handling all requests through cgi Change web-server settings 2. Use regular expression to parse query string for fields

CGI: Maintaining State: Query Strings Performance suffers Static webpages impossible Use mod-perl etc. to speed up cgi processing

CGI: Maintaining State: Hidden Fields Hidden fields in forms are not displayed in browser, but are still sent to web- server. Hidden fields have no performance overhead and always work, BUT Hidden fields are easily altered and cannot be trusted

CGI: Maintaining State: Hidden Fields Maintain state at web-server Use persistent files or database to maintain state. Performance suffers, but security is highest.