Test XSS with inconsistent encoding functions in HTML context
This lab demonstrates a critical security vulnerability where different encoding functions are used in the wrong context. The 'fname' parameter uses htmlspecialchars() (correct for HTML context), while 'lname' uses urlencode() (incorrect for HTML context).
Input: <script>alert(1)</script>
Output: <script>alert(1)</script>
Input: <script>alert(1)</script>
Output: %3Cscript%3Ealert%281%29%3C%2Fscript%3E
When the browser receives %3Cscript%3Ealert%281%29%3C%2Fscript%3E in HTML context, it decodes it back to:
<script>alert(1)</script>
This happens because URL encoding is automatically decoded by browsers when rendering HTML content.
Objective: Exploit the incorrect use of urlencode() in HTML context to execute XSS payloads through the 'lname' parameter.
if(isset($_GET["fname"]) && isset($_GET["lname"])){
echo htmlspecialchars($_GET["fname"], ENT_QUOTES);
echo urlencode($_GET["lname"]);
}
<script>alert(1)</script><script>alert(document.domain)</script><script>alert(document.cookie)</script><img src=x onerror=alert(1)><body onload=alert(1)><svg onload=alert(1)><a href="javascript:alert(1)">click</a><iframe src="javascript:alert(1)"></iframe><object data="javascript:alert(1)"></object><embed src="javascript:alert(1)"></embed>When you submit <script>alert(1)</script> as the last name:
%3Cscript%3Ealert%281%29%3C%2Fscript%3E<script>alert(1)</script>Using wrong encoding functions demonstrates:
Use the right encoding for each context: