CGI Scripting and Vulnerabilities COEN 351: E-commerce Security.

Slides:



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

1 Configuring Internet- related services (April 22, 2015) © Abdou Illia, Spring 2015.
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.
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.
How Clients and Servers Work Together. Objectives Learn about the interaction of clients and servers Explore the features and functions of Web servers.
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 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 Using Apache. Concepts Browser prepares parameter list List is attached to name of program to run on server "submit" button sends string.
CGI Programming Languages Web Based Software Development July 21, 2005 Song, JaeHa.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
Linux Operations and Administration
Common Gateway Interface
Web server and web browser It’s a take and give policy in between client and server through HTTP(Hyper Text Transport Protocol) Server takes a request.
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.
CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.
Web Server Configuration Alokes Chattopadhyay Computer & Informatics Centre IIT Kharagpur.
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.
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
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
FTP (File Transfer Protocol) & Telnet
JavaScript, Fourth Edition
Copyright 2000 eMation SECURITY - Controlling Data Access with
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.
Introduction to Programming the WWW I CMSC Summer 2004 Lecture 6.
USING PERL FOR CGI PROGRAMMING
Database-Driven Web Sites, Second Edition1 Chapter 5 WEB SERVERS.
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.
Building PERL Scripts on a Windows system* *and running those scripts on an Apache server!
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.
Website Development with PHP and MySQL Saving Data.
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 Chapter 9 – Cookies, Sessions, FTP, and More spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
1 CGI with perl. 2 References Perl tutorials and references:
CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.
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.
Form Data Encoding GET – URL encoded POST – URL encoded
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.
Intermediate CGI & CGI.pm Webmaster II - Fort Collins, CO Copyright © XTR Systems, LLC CGI Programming & The CGI.pm Perl Module Instructor: Joseph DiVerdi,
ASP. What is ASP? ASP stands for Active Server Pages ASP is a Microsoft Technology ASP is a program that runs inside IIS IIS stands for Internet Information.
Configuring and Troubleshooting Identity and Access Solutions with Windows Server® 2008 Active Directory®
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 1 Intermediate Perl Programming Class Two Instructor:
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.
CITA 310 Section 4 Apache Configuration (Selected Topics from Textbook Chapter 6)
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
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.
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)
CGI I: Basics Web Programming.
Configuring Internet-related services
The HTTP Protocol COSC 2206 Internet Tools The HTTP Protocol
Environment Variables
CGI I: Basics Web Programming.
Presentation transcript:

CGI Scripting and Vulnerabilities COEN 351: E-commerce Security

Setting up a test website under Windows IIS Built into Windows Apache Easy download Requires installation and configuration

Test Website under Windows Install Apache on Windows XP Install ActivePerl on Windows XP Configure Apache Use the instructions at

Test Website under Windows Start the Apache web server for a quick test. You might need to disable IIS. Go to Control Panel  Administrative Tools  Services and find IIS and stop it. (You can always restart it.) Open up a command prompt and run the Apache.exe. After you are done, kill the process with Ctr+C. Then use your browser to go to localhost. If it works, you see a page.

Test Website under Windows Configuring Apache First create a directory that will contain your web pages. Then edit the configuration file.

Test Website under Windows Since we are creating unsafe websites, remember to stop the Apache web- server when you are connected to the internet.

Test Website under Windows IIS Install and activate service Set up directory for cgi pages. Executables need execute permission, scripts need script or execute permission. Need application mapping between the file name extension and the script. IIS Manager (Administrative Tools  IIS Manager)

Test Website under Windows Notice: If you are actually using your machine as a web-server, disable all unused extensions to lower your “attack surface”.

Test Website under Windows Do not start Perl scripts with the shebang: #!perl/bin/perl Instead print "HTTP/ OK\n"; print "Content-type: text/html\n\n"; If you run a web-site, you need to take a number of precautions: Use NTFS security attributes / user accounts to restrict access. Place web-site on a different partition. Do not use default sites. Remove examples. Patch automatically. …

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

Only simple header. Notice the double lines. Print statement

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 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 with Perl Fundamentals 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 with Perl Fundamentals CGI.pm module print start_html(“hello”) Prints out: hello end_html Prints out:

CGI with Perl Fundamentals 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 with Perl Fundamentals Forms Allow browser to post data to server. Uses GET or POST message

CGI with Perl Fundamentals

HTTP request 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

CGI with Perl Fundamentals HTTP request with GET would be GET /f1.cgi?name=Thomas+Schwarz& =tschwarz%40scu.edu

CGI with Perl Fundamentals To read a form: Read the query string from $ENV{QUERY_STRING} If the $ENV{REQUEST_METHOD} is POST, determine the size of the request. Split the result on the “&” character. Split each name – value pair Decode the URL decoded characters in name and value. Associate each name with its values.

CGI with Perl Fundamentals Using the CGI.pm module makes things much easier.

CGI with Perl Fundamentals

CGI Security Security holes are exploited by user input. We need to check user input against Buffer overflows etc. that cause a program to misbehave. Input that is interpreted differently than the designer expects it.

CGI Security Interpretation example: Assume that we call a program within a script and pass user-provided parameters to the program.

CGI Security Interpretation Example #!usr/bin/perl –w use CGI my $App = /temp/app.exe’; my $q = new CGI; my $string = $q->param( “string” ); unless ( $string ) { error( $q, “Need string parameter”);} local *PIPE; open PIPE, “$App \”$string\” |” or die “Cannot open pipe: $!”; print q->header(“text/plain” ); print while ; close PIPE;

CGI Security Interpretation Example We first verify that the user enters a string. We use a pipe in order to stream the output of app to the page. The “print while ;” statement takes the output one line at a time and prints it out.

CGI Security Interpretation Example #!usr/bin/perl –w use CGI my $App = /temp/app.exe’; my $q = new CGI; my $string = $q->param( “string” ); unless ( $string ) { error( $q, “Need string parameter”);} local *PIPE; open PIPE, “$App \”$string\” |” or die “Cannot open pipe: $!”; print q->header(“text/plain” ); print while ; close PIPE;

CGI Security Interpretation Example When Perl opens up a pipe, then user input is passed through a shell Assume users types in ‘rm -rf /’ on a Unix machine. The command would execute as if the following command would have been entered into a shell: $ /temp/app.exe “ \rm –rf /’ “

CGI Security Interpretation Example When Perl opens up a pipe, then user input is passed through a shell Assume users types in “; mail < /etc/passwd” on a Unix machine. The command would execute as if the following command would have been entered into a shell: $ /temp/app.exe “”; mail < /etc/passwd

CGI Security Interpretation Example A simplistic countermeasure checks the input for bad characters, before we pass user input to the pipe. This is a bad strategy because it only excludes possible attacks. Much better to positively identify good input. Before 9/11, visa to US was granted unless there was a positive reason to exclude some-one. (Bad list.) After 9/11, visa to US demands proof of good attitudes. Bad policy maybe for the US, but good policy for web- servers (unless you eliminate legitimate traffic).

CGI Security Interpretation Example #!usr/bin/perl –w use CGI my $App = /temp/app.exe’; my $q = new CGI; my $string = $q->param( “string” ); unless ( $string ) { error( $q, “Need string parameter”);} if ($string =~ /[‘\$\\” ‘ ;& … ] ) {error($q, “Bad input”);} local *PIPE; open PIPE, “$App \”$string\” |” or die “Cannot open pipe: $!”; print q->header(“text/plain” ); print while ; close PIPE;

CGI Security Interpretation Example We will only allow strings that are alpha- numerical, have underscores, hyphens, periods, question marks, and exclamation points.

CGI Security Interpretation Example #!usr/bin/perl –w use CGI my $App = /temp/app.exe’; my $q = new CGI; my $string = $q->param( “string” ); unless ( $string ) { error( $q, “Need string parameter”);} if ($string =~ /^[\w.!?-]+$/ ) {error($q, “Bad input”);} local *PIPE; open PIPE, “$App \”$string\” |” or die “Cannot open pipe: $!”; print q->header(“text/plain” ); print while ; close PIPE;

CGI Security Interpretation Example This is much better. But do we positively know that one could not write an attack string that way? More importantly, a minor change can destroy the security. Better not use this idea.

CGI Security Interpretation Example Prevent the root problem: Do not pass arguments through the shell. First fork. Then let child process call exec.

CGI Security Interpretation Example #!usr/bin/perl –w use CGI my $App = /temp/app.exe’; my $q = new CGI; my $string = $q->param( “string” ); unless ( $string ) { error( $q, “Need string parameter”);} local *PIPE; my $pid = open PIPE, “-|”; die “Cannot fork $!” unless defined $pid; unless ( $pid ) { exec app, $ string or die “Cannot open pipe: $!”; print q->header(“text/plain” ); print while ; close PIPE;

CGI Security DO NOT TRUST INPUT Data in hidden fields can be changed by the user. Data in cookies can be changed.

Perl Taint Mode Perl offers some protection against user input. In taint mode, Perl will not allow any data from outside the application to affect anything outside the application. Tainted variables can not be passed to eval shell calls on the file system

Perl Taint Mode Tainted variables taint variables calculated from them. However, to make things work, you usually need to untaint variables: If a variable matches with a regular expression using () groups, then they become untainted. if ($ =~ { $ = } else { warn ("TAINTED DATA SENT BY $ENV{'REMOTE_ADDR'}: $ $!"); $ = ""; # successful match did not occur }

CGI Security Data Storage Issues Danger: Opening files when the filename is dynamically generated based on user input. Move data files out of web server tree. Set file permissions. Principle of minimal permission. Files that only need to be read should be owned by nobody and should be write protected.

CGI Security Learn the Odds and Ends User should not be able to send to anyone but a single entity. Otherwise, it is trivial to fake coming from your organization.

Maintaining State Query strings and extra path information State information is lost when user leaves the website. Need to parse the query string Hidden fields Only with form submission. Cookies Persistent. Need user enabling cookies.

Maintaining State With query strings: Have a cgi script that handles every request for an html page. If the URL does not contain an identifier, then create a new identifier and give it to the user. Script parses all links in the HTML page and resets them to contain a query string.

Maintaining State With query strings: Running a cgi script with every browser request eats up resources. mod_perl, Fastcgi embed the Perl interpreter into the web server and perform better. Otherwise, preprocess the document. I.e. every document is now dynamic.

Maintaining State Hidden Fields Form submission goes directly to cgi script.

Maintaining State Client-side cookies Cookies are set in the http header.

Maintaining State Client-side cookies Setting cookies within CGI module my $cookie = $q->cookie( -name => “thomas”, -value => , -domain => “scu.edu” -expires => “+1y”, -path => “/cig”, -secure => 1 ); print $q->header( -type => “text/html”, -cookie => $cookie );

Maintaining State Client-side cookies

Maintaining State Client-side cookies