19.09.2017 Views

the-web-application-hackers-handbook

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 19 n Finding Vulnerabilities in Source Code 739<br />

Sockets<br />

After a socket is created using socket, it is connected to a remote host via a call<br />

to connect, which takes a sockaddr_in structure composed of <strong>the</strong> target’s host<br />

and port details. If this host information is user-controllable in any way, <strong>the</strong><br />

<strong>application</strong> may be exploitable to cause network connections to arbitrary hosts,<br />

ei<strong>the</strong>r on <strong>the</strong> Internet or on <strong>the</strong> private DMZ or internal network on which <strong>the</strong><br />

<strong>application</strong> is hosted.<br />

Configuring <strong>the</strong> Perl Environment<br />

Perl provides a taint mode that helps prevent user-supplied input from being<br />

passed to potentially dangerous functions. You can execute Perl programs in<br />

taint mode by passing <strong>the</strong> -T flag to <strong>the</strong> Perl interpreter as follows:<br />

#!/usr/bin/perl -T<br />

When a program is running in taint mode, <strong>the</strong> interpreter tracks each item<br />

of input received from outside <strong>the</strong> program and treats it as tainted. If ano<strong>the</strong>r<br />

variable has its value assigned on <strong>the</strong> basis of a tainted item, it too is treated as<br />

tainted. For example:<br />

$path = “/home/pubs”<br />

# $path is not tainted<br />

$filename = param(“file”); # $filename is from request parameter and<br />

# is tainted<br />

$full_path = $path.$filename; # $full_path now tainted<br />

Tainted variables cannot be passed to a range of powerful commands, including<br />

eval, system, exec, and open. To use tainted data in sensitive operations,<br />

<strong>the</strong> data must be “cleaned” by performing a pattern-matching operation and<br />

extracting <strong>the</strong> matched substrings. For example:<br />

$full_path =~ m/^([a-zA-Z1-9]+)$/; # match alphanumeric submatch<br />

# in $full_path<br />

$clean_full_path = $1;<br />

# set $clean_full_path to <strong>the</strong><br />

# first submatch<br />

# $clean_full_path is untainted<br />

Although <strong>the</strong> taint mode mechanism is designed to help protect against many<br />

kinds of vulnerabilities, it is effective only if developers use appropriate regular<br />

expressions when extracting clean data from tainted input. If an expression is<br />

too liberal and extracts data that may cause problems in <strong>the</strong> context in which it

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!