Dev.to Security 🔐 Cybersecurity 👁 0 📖 5 min read

Understanding Host Headers: How One Server Hosts Multiple Websites and How Attackers Exploit It

Understanding Host Headers: How One Server Hosts Multiple Websites and How Attackers Exploit It Have you ever wondered how a single VPS can host multiple websites using just one public IP address? For example: examp

Understanding Host Headers: How One Server Hosts Multiple Websites and How Attackers Exploit It

Have you ever wondered how a single VPS can host multiple websites using just one public IP address?

For example:

  • example.com
  • shop.example.com
  • admin.example.com

All three websites could run on the same VPS.

So, how does the server know which application a visitor wants to access?

The answer is the HTTP Host header.

Understanding this concept is also important when learning about Host Header Injection, one of the vulnerabilities encountered during web application security testing.

Let's break it down.

1. What Is the HTTP Host Header?

Whenever a browser sends an HTTP request, it includes information about the requested hostname.

For example:

GET / HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0
Accept: text/html

The important part is:

Host: example.com

The Host header tells the web server which hostname the client wants to access.

But why do we need it?

Because multiple domains can point to the same IP address.

2. How Can One VPS Host Multiple Websites?

Imagine we have one VPS with the following public IP:

192.168.1.100

We configure two domains:

Domain IP Address
example.com 192.168.1.100
shop.com 192.168.1.100

Both domains point to the same server.

Inside the VPS, we have two applications:

  • Application 1: Go backend running on port 3000.
  • Application 2: Node.js backend running on port 4000.

Both applications run simultaneously.

But how do incoming requests reach the correct application?

This is where Apache or Nginx comes in.

3. Apache and Nginx: The Receptionist

Think of Apache or Nginx as a receptionist in a building.

The building has multiple offices, and the receptionist directs visitors to the correct office.

Similarly, Apache and Nginx receive incoming HTTP requests and select the appropriate website configuration.

This is called name-based virtual hosting.

Apache Configuration

Apache uses VirtualHost configurations to handle multiple domains.

Example:

<VirtualHost *:80>
    ServerName example.com

    ProxyPass / http://127.0.0.1:3000/
    ProxyPassReverse / http://127.0.0.1:3000/
</VirtualHost>

<VirtualHost *:80>
    ServerName shop.com

    ProxyPass / http://127.0.0.1:4000/
    ProxyPassReverse / http://127.0.0.1:4000/
</VirtualHost>

What happens?

  1. Apache receives an HTTP request.
  2. It checks the requested hostname.
  3. It matches the hostname against ServerName and ServerAlias.
  4. It selects the appropriate VirtualHost.
  5. It forwards the request to the configured backend application.

For example:

Host header Backend
example.com 127.0.0.1:3000
shop.com 127.0.0.1:4000

Nginx Configuration

Nginx achieves something similar using the server and server_name directives.

Example:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
    }
}

server {
    listen 80;
    server_name shop.com;

    location / {
        proxy_pass http://127.0.0.1:4000;
    }
}

Nginx receives the request, checks the hostname, and selects the matching server block.

Both Apache and Nginx can act as reverse proxies.

4. What Happens When an Attacker Changes the Host Header?

Now comes the interesting part from a web security perspective.

Normally, a browser sends:

GET / HTTP/1.1
Host: example.com

But an attacker can modify this header using tools such as Burp Suite.

For example:

GET / HTTP/1.1
Host: attacker.example

The application or web server might handle this unexpected hostname in different ways.

The important security question is:

Does the application trust the Host header without validating it?

If the application uses an untrusted hostname to generate URLs, redirects, or other security-sensitive content, vulnerabilities may occur.

Let's look at some examples.

5. Common Host Header Attacks

A. Password Reset Poisoning

Imagine a website generates password reset links using the incoming Host header.

Normally, the application generates:

https://example.com/reset?token=12345

But if an attacker can manipulate the Host header, the application might generate:

https://attacker.example/reset?token=12345

If this poisoned link is sent to a victim, the victim may visit the attacker's domain with their reset token.

An attacker could potentially capture the token.

The root cause is trusting an unvalidated Host header when generating password reset URLs.

B. Web Cache Poisoning

Some applications generate HTML content using the Host header.

For example:

<script src="https://example.com/app.js"></script>

If the application uses an attacker-controlled hostname instead, it might generate:

<script src="https://attacker.example/app.js"></script>

If a shared cache stores and serves this manipulated response to other users, it could result in cache poisoning.

C. Virtual Host Discovery

A single server may host multiple applications.

For example:

  • www.example.com
  • admin.example.com
  • dev.example.com

An attacker may investigate whether different Host values cause the server to return different applications.

However, discovering an internal virtual host does not automatically mean authentication has been bypassed.

6. What About X-Forwarded-Host?

There is another HTTP header worth understanding:

X-Forwarded-Host: attacker.example

Consider this architecture:

Browser
   |
   v
Nginx Reverse Proxy
   |
   v
Backend Application

A reverse proxy can pass hostname information to the backend using forwarded headers.

Some applications use X-Forwarded-Host when generating absolute URLs.

If the proxy blindly forwards attacker-controlled values and the application trusts them, this can introduce security issues.

Therefore, validating forwarded headers is just as important as validating the original Host header.

7. How to Test Host Header Injection Using Burp Suite

When testing an authorized application, start with a normal request.

GET / HTTP/1.1
Host: target.example

Then modify the Host header:

GET / HTTP/1.1
Host: attacker.example

Observe:

  • Does the response change?
  • Does the application redirect to another domain?
  • Does the application generate URLs using the supplied hostname?
  • Does a password reset email contain a manipulated URL?
  • Does the response get stored and reused by a shared cache?

You can also investigate forwarded headers:

X-Forwarded-Host: attacker.example

Remember: changing a header and observing a different response is not sufficient to prove a vulnerability. You need to demonstrate a meaningful security impact.

8. How Can Developers Prevent These Attacks?

Some important defensive practices include:

  1. Maintain an explicit allowlist of valid hostnames.
  2. Use a configured canonical domain when generating password reset links.
  3. Configure Apache and Nginx with intentional default virtual hosts.
  4. Do not blindly trust X-Forwarded-Host.
  5. Configure reverse proxies to overwrite or sanitize forwarded headers.
  6. Validate redirects and URLs generated by the application.

Conclusion

The HTTP Host header might look like a simple part of an HTTP request, but it plays an important role in how modern web applications and web servers work.

Understanding how one VPS can host multiple applications, how Apache and Nginx select virtual hosts, and how applications consume hostname information provides a strong foundation for learning Host Header Attacks.

For anyone learning web application security, understanding the underlying architecture is just as important as learning payloads.

Keep learning, keep testing, and understand the logic behind every vulnerability.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.