19.09.2017 Views

the-web-application-hackers-handbook

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

362 Chapter 10 n Attacking Back-End Components<br />

TRY IT!<br />

http://mdsec.net/admin/5/<br />

http://mdsec.net/admin/9/<br />

http://mdsec.net/admin/14/<br />

Injecting Through Dynamic Execution<br />

Many <strong>web</strong> scripting languages support <strong>the</strong> dynamic execution of code that is<br />

generated at runtime. This feature enables developers to create <strong>application</strong>s that<br />

dynamically modify <strong>the</strong>ir own code in response to various data and conditions.<br />

If user input is incorporated into code that is dynamically executed, an attacker<br />

may be able to supply crafted input that breaks out of <strong>the</strong> intended data context<br />

and specifies commands that are executed on <strong>the</strong> server in <strong>the</strong> same way as if<br />

<strong>the</strong>y had been written by <strong>the</strong> original developer. The first target of an attacker<br />

at this point typically is to inject an API that runs OS commands.<br />

The PHP function eval is used to dynamically execute code that is passed to<br />

<strong>the</strong> function at runtime. Consider a search function that enables users to create<br />

stored searches that are <strong>the</strong>n dynamically generated as links within <strong>the</strong>ir user<br />

interface. When users access <strong>the</strong> search function, <strong>the</strong>y use a URL like <strong>the</strong> following:<br />

/search.php?storedsearch=\$mysearch%3dwahh<br />

The server-side <strong>application</strong> implements this functionality by dynamically<br />

generating variables containing <strong>the</strong> name/value pairs specified in <strong>the</strong> storedsearch<br />

parameter, in this case creating a mysearch variable with <strong>the</strong> value wahh:<br />

$storedsearch = $_GET[‘storedsearch’];<br />

eval(“$storedsearch;”);<br />

In this situation, you can submit crafted input that is dynamically executed<br />

by <strong>the</strong> eval function, resulting in injection of arbitrary PHP commands into<br />

<strong>the</strong> server-side <strong>application</strong>. The semicolon character can be used to batch commands<br />

in a single parameter. For example, to retrieve <strong>the</strong> contents of <strong>the</strong> file<br />

/etc/password, you could use ei<strong>the</strong>r <strong>the</strong> file_get_contents or system command:<br />

/search.php?storedsearch=\$mysearch%3dwahh;%20echo%20file_get<br />

_contents(‘/etc/passwd’)<br />

/search.php?storedsearch=\$mysearch%3dwahh;%20system(‘cat%20/etc/<br />

passwd’)<br />

NOTE The Perl language also contains an eval function that can be<br />

exploited in <strong>the</strong> same way. Note that <strong>the</strong> semicolon character may need to<br />

be URL-encoded (as %3b) because some CGI script parsers interpret this as a<br />

parameter delimiter. In classic ASP, Execute() performs a similar role.

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

Saved successfully!

Ooh no, something went wrong!