Keylogging and Form Hijacking
You'll learn how attackers use XSS-injected JavaScript to capture every keystroke and steal form data—including passwords—before it even reaches the server.
What This Attack Does
Once an attacker has achieved XSS (cross-site scripting), they can inject JavaScript that runs in the victim's browser with full access to the page. Two particularly dangerous capabilities this enables are keylogging (recording everything the user types) and form hijacking (intercepting form submissions).
Think of it like this: imagine someone installing a hidden camera over your shoulder that records every key you press on your keyboard, or copying your completed forms before you hand them in. That's exactly what malicious JavaScript can do in your browser.
How It Works
Keylogging: The attacker's injected script uses JavaScript event listeners to capture keydown, keypress, or keyup events. Every character typed—passwords, credit cards, messages—gets recorded and sent to the attacker's server.
Form Hijacking: The script locates form elements on the page and attaches event handlers to intercept the submit event. Before the legitimate form data reaches the real server, the attacker's code copies it and transmits it elsewhere.
Both attacks are invisible to the user. The page functions normally—forms still submit, typing still works—but the attacker silently collects everything in the background.
Why This Is Devastating
Unlike simple session theft (covered in cookie hijacking), these techniques capture raw credentials. Even if the user changes their password later, the attacker already has it. They can also capture two-factor codes typed into forms, security questions, and any other sensitive input.
Key Takeaway: XSS-injected JavaScript can install invisible keyloggers and form interceptors that capture sensitive data—including passwords—in real-time before it's encrypted or sent to legitimate servers, making it one of the most dangerous consequences of content injection vulnerabilities.