TL;DR
Web application input validation is often a first line of defence against attacks. However, it’s frequently flawed. This guide shows common bypass techniques and how to protect your applications.
Understanding the Problem
Input validation checks what data users enter into forms or URLs. If not done correctly, attackers can send malicious input that breaks the application logic, steals data, or executes harmful code. Common vulnerabilities include:
- Client-Side Validation: Only checking data in the browser. Easily bypassed by disabling JavaScript or modifying requests.
- Insufficient Server-Side Validation: Not thoroughly checking all possible malicious inputs on the server.
- Blacklisting instead of Whitelisting: Trying to block bad characters/patterns instead of allowing only known good ones.
Bypass Techniques
- Basic Encoding: Try URL encoding, HTML entity encoding, or Base64 encoding to disguise malicious input.
- URL Encoding: Replace spaces with
%20, special characters with their encoded equivalents (e.g.,<becomes%3C). - HTML Entity Encoding: Replace characters like
<with<.
- URL Encoding: Replace spaces with
- Case Manipulation: Some validations are case-sensitive. Try changing the case of input (e.g.,
SCRIPTinstead ofscript). - Double Encoding: Encode a character twice. The server might decode it once, leaving the malicious character intact.
Example: <script> encoded as %3Cscript%3E - Whitespace and Comments: Insert whitespace or HTML/SQL comments to break validation patterns.
Example: <scriptalert(1)</script> - Using Alternative Syntax: For languages like JavaScript, try different ways of writing the same command.
Example: Instead of alert('XSS'), use eval("alert('XSS')") - Bypassing Blacklists with Similar Characters: Use characters that look similar to blacklisted ones (e.g.,
ainstead ofa). - Exploiting Regular Expression Flaws: If the validation uses a regular expression, find ways to craft input that matches the regex but isn’t valid.
- Look for overly permissive patterns or missing anchors (
^and$) in the regex.
- Look for overly permissive patterns or missing anchors (
- HTTP Parameter Pollution: Send multiple parameters with the same name. The server might process only one, potentially bypassing validation on others.
Example: ?param=safe¶m=<script>alert(1)</script>
Protecting Your Applications
- Server-Side Validation is Essential: Never rely solely on client-side validation. Always validate all input on the server.
- Whitelisting over Blacklisting: Define what is allowed, not what isn’t. This is much more secure.
Example: Allow only alphanumeric characters and spaces in a username field. - Input Sanitization: Remove or encode potentially harmful characters before processing the input.
- Use appropriate encoding functions for the context (e.g., HTML entity encoding for displaying data, SQL escaping for database queries).
- Regular Expression Best Practices: Use well-tested and secure regular expressions with proper anchors (
^and$) to match the entire input string. - Content Security Policy (CSP): Implement CSP to control which resources the browser is allowed to load, mitigating XSS attacks.
- Regular Security Audits: Regularly test your application for vulnerabilities using automated tools and manual penetration testing.