Caused when raw, unfiltered data entered by the user is displayed in the browser.
If that sounds similar to an SQL Injection attack, it is. XSS is Scipt Injection.
Non-persistent attacks are "one-offs" - a script is injected on a page as a user view it. The scipt is not stored on the site, and must be injected at the point of use.
Here's how it works:
test<script>alert('Attacked!');</script>
See if you can inject a script on this page to do something else interesting.
Hint: all scripts are contained within a pair of <script> </script> tags.
Non-persistent XSS attacks often exploit a vulnerable URL parameter whose value is displayed on the page. The attacker creates a carefully crafted URL that injects a script onto the page, as we did above using a form.
Here's how it works:
<script>document.write('Watch your cookies: ' + document.cookie);</script>
Can you craft a URL that injects a script on this page? Send the link to a classmate and ask them to click on it.
Notice that the script injections above affect only a single user and are temporary in nature.
A persistent XSS attack is created when the script is saved in the site's DB and injected every time that DB record is displayed.
Here's how it works:
<script> fetch(new Request("/app/hacked-sessions?c="+escape(document.cookie))); </script>
Add another persistent XSS of your own design. Doesn't have to do anything terrible - just take some action when the comments are viewed. Because the comments are displayed in reverse-chronological order, your script will execute first, before the hack we made above.