CSRF stands for Cross-Site Request Forgery. It is a type of attack where a malicious website tricks a user's browser into performing unwanted actions on a trusted site where the user is logged in.
Click to reveal answer
beginner
How does a CSRF token help protect against CSRF attacks?
A CSRF token is a unique, secret value generated by the server and included in forms. When the form is submitted, the server checks the token. If it matches, the request is valid. This prevents attackers from forging requests because they don't know the token.
Click to reveal answer
intermediate
Show a simple PHP code snippet to generate and store a CSRF token in a session.
<?php
session_start();
if (empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
?>
Click to reveal answer
beginner
How do you include a CSRF token in an HTML form?
<form method="post" action="submit.php">
<input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($_SESSION['csrf_token']); ?>">
<!-- other form fields -->
<input type="submit" value="Submit">
</form>
Click to reveal answer
beginner
What is the server-side check for a CSRF token in PHP?
On form submission, the server compares the token sent in the form with the token stored in the session. If they match, the request is accepted; otherwise, it is rejected to prevent CSRF attacks.
Click to reveal answer
What is the main purpose of a CSRF token?
ATo speed up form submission
BTo verify the user is logged in
CTo encrypt user data
DTo prevent unauthorized form submissions from other sites
✗ Incorrect
CSRF tokens prevent unauthorized form submissions by ensuring the request comes from the legitimate user.
Where is a CSRF token usually stored on the server side?
AIn the user's session data
BIn a cookie accessible by JavaScript
CIn the URL query string
DIn a database table
✗ Incorrect
CSRF tokens are stored in the user's session to keep them secret and tied to the user.
Which PHP function is commonly used to generate a secure CSRF token?
Arand()
Bmd5()
Cbin2hex(random_bytes())
Duniqid()
✗ Incorrect
bin2hex(random_bytes()) generates a cryptographically secure random token.
What happens if the CSRF token in the form does not match the session token?
AThe request is rejected to prevent CSRF
BThe server redirects to the login page
CThe token is automatically updated
DThe request is accepted anyway
✗ Incorrect
Mismatch means the request might be forged, so it is rejected.
Why can't attackers easily guess the CSRF token?
ABecause it is stored in a cookie
BBecause it is a long, random string generated securely
CBecause it is encrypted with a password
DBecause it changes every second
✗ Incorrect
CSRF tokens are long random strings generated securely, making guessing practically impossible.
Explain what a CSRF attack is and how a CSRF token helps prevent it.
Think about how a malicious site tricks a user and how a secret token stops that.
You got /3 concepts.
Describe the steps to implement CSRF token protection in a PHP web form.
Consider what happens before showing the form and after the form is submitted.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a CSRF token in a PHP web application?
easy
A. To speed up the loading time of the website
B. To prevent unauthorized commands from being transmitted from a user the website trusts
C. To encrypt user passwords before storing them
D. To validate user input format on forms
Solution
Step 1: Understand CSRF attack basics
CSRF attacks trick a user into submitting unwanted actions on a trusted site.
Step 2: Role of CSRF token
A CSRF token is a unique value that verifies the request is from the legitimate user, preventing unauthorized commands.
Final Answer:
To prevent unauthorized commands from being transmitted from a user the website trusts -> Option B
A. session_start() should be called after token check
B. Using == instead of === causes token mismatch
C. Tokens should be compared with strtolower()
D. Missing check if $_POST['csrf_token'] is set before comparing
Solution
Step 1: Analyze token existence check
The code compares $_POST['csrf_token'] without verifying if it exists, risking undefined index error.
Step 2: Importance of isset()
Using isset() prevents errors by ensuring the token is present before comparison.
Final Answer:
Missing check if $_POST['csrf_token'] is set before comparing -> Option D
Quick Check:
Check token existence before compare = Missing isset() [OK]
Hint: Always check if POST token exists before comparing [OK]
Common Mistakes:
Ignoring isset() causing runtime errors
Thinking == vs === affects token validation here
Calling session_start() after token check
5. You want to protect a PHP form from CSRF attacks. Which sequence correctly implements token generation, embedding, and validation?
A) Generate token on form display, store in session, embed in hidden input, then check token on form submission.
B) Generate token on form submission, embed in hidden input, then store in session.
C) Store token in session after form submission, then generate token and embed in form.
D) Embed a static token in the form and check it on submission without session.
hard
A. Generate token on form display, store in session, embed in hidden input, then check token on form submission.
B. Generate token on form submission, embed in hidden input, then store in session.
C. Store token in session after form submission, then generate token and embed in form.
D. Embed a static token in the form and check it on submission without session.
Solution
Step 1: Token generation and storage
Token must be generated before showing the form and stored in session to verify later.
Step 2: Embedding and validation
Embed the token in a hidden form field and check it against the session token on submission.
Final Answer:
Generate token on form display, store in session, embed in hidden input, then check token on form submission. -> Option A