Get a Pentest and security assessment of your IT network.

Cyber Security

Using X-Forwarded-For to Identify Clients

TL;DR

Identifying clients using HTTP headers like X-Forwarded-For, alongside their direct IP address, improves accuracy when they’re behind proxies or load balancers. This helps with logging, security, and analytics.

Why Use X-Forwarded-For?

When a user connects through a proxy server or load balancer, your web server sees the proxy’s IP address, not the client’s original IP. This makes it hard to know who is actually visiting your site.

  • Accurate Logging: Log the real visitor IP for better analytics and debugging.
  • Security Measures: Implement geo-blocking or rate limiting based on the client’s true location.
  • Personalisation: Tailor content based on user location (if appropriate).

How it Works

The X-Forwarded-For header is added by proxies and load balancers to include a comma-separated list of IP addresses. The first address is the original client’s IP, and subsequent addresses are the IPs of each proxy in the chain.

Steps to Identify Clients

  1. Check for the Header: Your web server or application needs to check if the X-Forwarded-For header exists.
  2. Extract the Client IP: If present, extract the first IP address from the comma-separated list. This is usually the client’s original IP.
  3. Validate the Header (Important!): Never trust the X-Forwarded-For header without validation. Attackers can spoof this header to hide their true IP.

Example Code (Python/Flask)

from flask import Flask, request

app = Flask(__name__)

@app.route('/')
def index():
    client_ip = request.remote_addr  # Get the direct IP address
    x_forwarded_for = request.headers.get('X-Forwarded-For')

    if x_forwarded_for:
        ips = x_forwarded_for.split(',')
        client_ip = ips[0].strip() # Get the first IP (client's original)

    return f'Your IP address is: {client_ip}'

if __name__ == '__main__':
    app.run(debug=True)

Validation Techniques

  • Known Proxy IPs: Maintain a list of your known proxy server IP addresses. If the X-Forwarded-For header includes an IP not on this list, it’s likely spoofed.
  • Trust Proxies Only: Configure your web server to only accept X-Forwarded-For headers from trusted proxies.
  • Header Order: Be aware that the order of IPs in the header is important. The first IP is considered the client, and subsequent IPs are proxies.

Web Server Configuration Examples

How you configure your web server depends on which one you’re using.

  • Apache: Use the mod_remoteip module to handle X-Forwarded-For.
  • Nginx: Use the realip_module and configure trusted proxies in your configuration file.
  • Load Balancer Configuration: Ensure your load balancer is correctly adding the X-Forwarded-For header with the client’s IP address.

Security Considerations

Always treat the X-Forwarded-For header as potentially untrustworthy. Proper validation is crucial to prevent attackers from manipulating it for malicious purposes in your cyber security measures.

Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation