Imagine an e-banking web application at banking.example.com
with the following form to submit a transaction:
<form action="/transaction" method="post">
<input type="text" name="beneficiary"/>
<input type="text" name="amount"/>
<input type="submit" value="Pay"/>
</form>
An attacker could now set up a website at hacker.net
with the following:
<form action="https://banking.example.com/transaction" method="post" style="visibility:hidden">
<input type="text" name="beneficiary" value="John Doe, Account No. 34-236326-1"/>
<input type="text" name="amount" value="1000000"/>
<input type="submit" value="Pay"/>
</form>
<script>
document.forms[0].submit();
</script>
The attacker would then trick victims into visiting hacker.net
, which will cause the victims' browsers to send a POST request to the e-banking application, making a large transaction to the hacker. This works because the victim's browser happily sends the session cookie along with the forged POST request to the e-banking application. If the form would have been protected by a CSRF token, then the attacker could not have caused the victim's browser to send a valid POST request and thus the attack would not be possible.
This type of attack is called a Cross-Site Request Forgery (CSRF) attack.
Incidently, CSRF attacks are also the reason why people give the advice of never ever visiting other websites while being logged into an e-banking or other critical web application.
CSRF tokens do not protect a web form being automatically submitted by regular authorized users as themselves. To protect from that, you'd use a CAPTCHA.