Skip to content

Security Cipher

Menu
  • Home
  • Blog
  • Services
  • About Us
  • Resources
    • Security Tools
    • Penetration Testing Tricks
    • Security Terminologies
    • Vulnerability Explain
    • Secure Code Explain
    • AWS Cloud Security Checklist
    • Web Application Security Quiz
  • My Resume
Contact Us

Penetration Testing Tricks

  • Subdomain Enumeration Tools
  • Graphql [Inprogress]
  • 2FA Bypass
  • Captcha Bypass

Vulnerability Explain

  • Cross-Site-Scripting (XSS)
  • SQL Injection
  • Server-Side Request Forgery (SSRF)

Security Resources

  • Search Engines for Hackers
  • Browser Extensions
  • Out-of-Band Exfiltration Tools
  • Wordlists
  • Input Sanitization Techniques for Secure Coding
  • HTTP Security Headers

Secure Code Explain

  • Insecure Password Storage
  • Host Header Injection
  • SQL Injection
  • Session Fixation
  • Home
  • Docs
  • Secure Code Explain
  • Host Header Injection

Host Header Injection

Here is an example of vulnerable code that is susceptible to a Host Header Injection Attack :

🥺 Vulnerable Code #

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PasswordResetServlet {
public void resetPassword(HttpServletRequest request, HttpServletResponse response) throws IOException {
String email = request.getParameter("email");
String resetLink = "https://" + request.getHeader("Host") + "/reset-password?email=" + email;

// Send password reset link to the user's email
// ...

response.sendRedirect(resetLink);
}
}

In the vulnerable code snippet above:

  • The resetPassword method takes an HTTP request and response as parameters and extracts the email parameter from the request.
  • It constructs a password reset link by directly using the Host header from the HTTP request. This allows an attacker to manipulate the Host header and potentially redirect the password reset link to a malicious site.

😎 Secure Code #

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PasswordResetServlet {
private static final String APP_DOMAIN = "example.com";

public void resetPassword(HttpServletRequest request, HttpServletResponse response) throws IOException {
String email = request.getParameter("email");
String resetLink = "https://" + APP_DOMAIN + "/reset-password?email=" + email;

// Send password reset link to the user's email
// ...

response.sendRedirect(resetLink);
}
}

In the secure code snippet above:

  • We’ve introduced a constant APP_DOMAIN, which represents the legitimate domain of the application. This domain is not derived from the Host header.
  • Instead of using request.getHeader("Host"), we use the constant APP_DOMAIN to construct the password reset link. This ensures that the link is always generated using the expected and trusted domain, mitigating the host header injection vulnerability.
Share This Article :
  • Facebook
  • Twitter
  • LinkedIn
  • Pinterest

This Post Has One Comment

  1. Uday Patel October 20, 2023 Reply

    In the secure code there is no validation over the email parameter

Leave a Reply Cancel reply

Table of Contents
  • 🥺 Vulnerable Code
  • 😎 Secure Code
logo name

Learn penetration Testing …

Instagram Twitter Youtube Telegram Linkedin

Useful Links

Blogs
About
Contact

Recent Post

  • Top Recon Tools for Bug Bounty Hunters
  • Mastering WordPress Penetration Testing: A Step-by-Step Guide
  • Enhance WordPress Security: Comprehensive Guide

Subscribe Now

Don’t miss our future updates! Get Subscribed Today!

Subscription Form

By entering your email, you agree to our terms & Conditions and Privacy policy.

©2023. Security Cipher. All Rights Reserved.

Privacy Policies
Terms & Conditions