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.

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

Finding OS Command Injection Flaws<br />

In your <strong>application</strong> mapping exercises (see Chapter 4), you should have identified<br />

any instances where <strong>the</strong> <strong>web</strong> <strong>application</strong> appears to be interacting with<br />

<strong>the</strong> underlying operating system by calling external processes or accessing <strong>the</strong><br />

filesystem. You should probe all <strong>the</strong>se functions, looking for command injection<br />

flaws. In fact, however, <strong>the</strong> <strong>application</strong> may issue operating system commands<br />

containing absolutely any item of user-supplied data, including every URL and<br />

body parameter and every cookie. To perform a thorough test of <strong>the</strong> <strong>application</strong>,<br />

you <strong>the</strong>refore need to target all <strong>the</strong>se items within every <strong>application</strong> function.<br />

Different command interpreters handle shell metacharacters in different ways.<br />

In principle, any type of <strong>application</strong> development platform or <strong>web</strong> server may<br />

call out to any kind of shell interpreter, running ei<strong>the</strong>r on its own operating system<br />

or that of any o<strong>the</strong>r host. Therefore, you should not make any assumptions<br />

about <strong>the</strong> <strong>application</strong>’s handling of metacharacters based on any knowledge of<br />

<strong>the</strong> <strong>web</strong> server’s operating system.<br />

Two broad types of metacharacters may be used to inject a separate command<br />

into an existing preset command:<br />

n The characters ;|& and newline may be used to batch multiple commands,<br />

one after <strong>the</strong> o<strong>the</strong>r. In some cases, <strong>the</strong>se characters may be doubled with<br />

different effects. For example, in <strong>the</strong> Windows command interpreter,<br />

using && causes <strong>the</strong> second command to run only if <strong>the</strong> first is successful.<br />

Using || causes <strong>the</strong> second command to always run, regardless of <strong>the</strong><br />

success of <strong>the</strong> first.<br />

n The backtick character (`) can be used to encapsulate a separate command<br />

within a data item being processed by <strong>the</strong> original command. Placing an<br />

injected command within backticks causes <strong>the</strong> shell interpreter to execute<br />

<strong>the</strong> command and replace <strong>the</strong> encapsulated text with <strong>the</strong> results of this<br />

command before continuing to execute <strong>the</strong> resulting command string.<br />

In <strong>the</strong> previous examples, it was straightforward to verify that command injection<br />

was possible and to retrieve <strong>the</strong> results of <strong>the</strong> injected command, because<br />

those results were returned immediately within <strong>the</strong> <strong>application</strong>’s response.<br />

In many cases, however, this may not be possible. You may be injecting into a<br />

command that returns no results and which does not affect <strong>the</strong> <strong>application</strong>’s<br />

subsequent processing in any identifiable way. Or <strong>the</strong> method you have used<br />

to inject your chosen command may be such that its results are lost as multiple<br />

commands are batched toge<strong>the</strong>r.<br />

In general, <strong>the</strong> most reliable way to detect whe<strong>the</strong>r command injection is<br />

possible is to use time-delay inference in a similar way as was described for<br />

exploiting blind SQL injection. If a potential vulnerability appears to exist, you<br />

can <strong>the</strong>n use o<strong>the</strong>r methods to confirm this and to retrieve <strong>the</strong> results of your<br />

injected commands.

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

Saved successfully!

Ooh no, something went wrong!