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.

536 Chapter 13 n Attacking Users: O<strong>the</strong>r Techniques<br />

Preventing Header Injection Vulnerabilities<br />

The most effective way to prevent HTTP header injection vulnerabilities is to<br />

not insert user-controllable input into <strong>the</strong> HTTP headers that <strong>the</strong> <strong>application</strong><br />

returns. As you saw with arbitrary redirection vulnerabilities, safer alternatives<br />

to this behavior usually are available.<br />

If it is considered unavoidable to insert user-controllable data into HTTP<br />

headers, <strong>the</strong> <strong>application</strong> should employ a twofold defense-in-depth approach<br />

to prevent any vulnerabilities from arising:<br />

n Input validation—The <strong>application</strong> should perform context-dependent<br />

validation of <strong>the</strong> data being inserted in as strict a manner as possible.<br />

For example, if a cookie value is being set based on user input, it may be<br />

appropriate to restrict this to alphabetical characters only and a maximum<br />

length of 6 bytes.<br />

n Output validation—Every piece of data being inserted into headers should<br />

be filtered to detect potentially malicious characters. In practice, any character<br />

with an ASCII code below 0x20 should be regarded as suspicious,<br />

and <strong>the</strong> request should be rejected.<br />

Applications can prevent any remaining header injection vulnerabilities from<br />

being used to poison proxy server caches by using HTTPS for all <strong>application</strong><br />

content, provided that <strong>the</strong> <strong>application</strong> does not employ a caching reverse-proxy<br />

server behind its SSL terminator.<br />

Cookie Injection<br />

In cookie injection attacks, <strong>the</strong> attacker leverages some feature of an <strong>application</strong>’s<br />

functionality, or browser behavior, to set or modify a cookie within <strong>the</strong><br />

browser of a victim user.<br />

An attacker may be able to deliver a cookie injection attack in various ways:<br />

n Some <strong>application</strong>s contain functionality that takes a name and value in<br />

request parameters and sets <strong>the</strong>se within a cookie in <strong>the</strong> response. A<br />

common example where this occurs is in functions for persisting user<br />

preferences.<br />

n As already described, if an HTTP header injection vulnerability exists,<br />

this can be exploited to inject arbitrary Set-Cookie headers.<br />

n XSS vulnerabilities in related domains can be leveraged to set a cookie on<br />

a targeted domain. Any subdomains of <strong>the</strong> targeted domain itself, and<br />

of its parent domains and <strong>the</strong>ir subdomains, can all be used in this way.<br />

n An active man-in-<strong>the</strong>-middle attack (for example, against users on a public<br />

wireless network) can be used to set cookies for arbitrary domains, even

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

Saved successfully!

Ooh no, something went wrong!