Cross Site Request Forgery - Computerphile

Computerphile · Intermediate ·🔐 Cybersecurity ·12y ago

Key Takeaways

The video discusses Cross-Site Request Forgery (CSRF) attacks, where an attacker tricks a user into performing unintended actions on a web application, and explains how to prevent such attacks using tokens and one-time keys.

Full Transcript

Cross-site request forgery uh CSRF is the third big web attack. I've talked about cross-sight scripting in the past. I've talked about SQL injection. This is number three and it's the lesserk known one. Web browsers are fairly trusting things. Um I've said this before, if you give them some code to run, uh they they don't cast a value judgment on it. They can't tell if it's malicious. They will just run it. Now this was this was kind of okay in the in the early days of the web when there wasn't online banking and things like that. You know the worst you could do is is put a comment somewhere. Um nowadays bit more complicated. The web runs on data being sent back and forth and that data can be encoded in a couple of ways. You can have what's called a get request and that's like if you if you look up if you're using this on desktop and you look up at the the you know browser address bar you'll see youtube.com/watch. it's the name of the page and then a question mark and then v equals a string of characters. That means that you're going to the watch page and then the rest of that gets stripped off and sent to the logic at YouTube which says oh we want this video to pull these details out the database. There's another way of doing that called post which is the kind of thing where you have a form with a button at the bottom of it and when you hit go then all that data gets bundled up and sent along with the request but not in the address bar. And traditionally that if you put it in the address bar, it's designed for fetching data. It's called a get. So that means that you can take this YouTube URL and copy and paste it to somewhere else. And just bundling the data up like when you click go on a form is called a post. And that's meant for writing things for what things that happen once. So that when you hit refresh or go back to the same page, it doesn't do the same thing twice. So, if you enter a comment, um it doesn't put all your comments details in the URL bar and you can't copy and paste that to a friend and they'll post the same comment. And that's that's reasonably good for the early days of the web, but since then it's got a bit more complicated. Let's say you have a form uh on your website uh for an online bank. So, let's design a really bad online bank form. Let's have send some money from your account to this account number and send this much. And then there's a go button. And let's say that as long as you're logged in, you know, as long as you've been through all the authentication checks and, you know, use that little chip and pin device that gives you a password to type in, you've got access to that form. You type in the account number. You type in how much you're transmitting. You select go. First of all, this won't use a get request. It won't appear up top like YouTube does. Uh it'll bundle it up as a post. It'll send the request and your bank will send back that it's done. But the trouble is that form is well almost public. You know how it's designed. So let's imagine you set up a fake web page somewhere else that has that form on it. And maybe, just maybe, you hide some of those details. So, they're already typed in. So, it already has your account number in it and say £1,000. And then when someone comes along and they want to uh put a comment on your website, so let's design uh a really malicious blog. Let's let's call it my awesome blog. And let's write some really inflammatory content that that people will love to comment on. And here is a comments box. But off here, off off screen where they can't see it or even completely invisible is a box that says account number pre-filled with your account number amount a,000. I've written a hundred £100 because apparently my my hand has lower ambitions than my brain does. And when you click that go button, it's not going to my awesome blog. It goes to the really badly designed online bank, which promptly says, "Oh, we've got a request here. don't know what all this blog nonsense is about, but look, we've got an account number. We've got some money. Transfer it because we've already been authorized because you're logged into your online bank in that other tab while you were bored. And well, great, the money is gone. And it gets worse than that because uh if you've noticed when you type a comment on YouTube and you click post, it doesn't load the whole page back like it used to in the early days of the web. Now it does it all in the background silently and just sends a thing back saying, "Yeah, that's fine. No worries." Well, that would work with this, too. You wouldn't even need to click the go button. It could just be that when I load up my site, my malicious awesome blog, it just creates that form in the background and sends off that request. There's a few things in browsers designed to stop you doing that, but there are ways around them. Now, that's obviously quite a big problem. Online banks are generally not designed that way unless they're designed incompetently. But a lot of web forums, a lot of smaller sites have problems like that. If you've ever designed something with a delete your account button, then frequently it just goes to something like slashdelete with maybe, you know, confirm true on it. And you assume that the only time that form will be visible is when the user is logged in and has clicked no, I really want to delete my account. I could copy that delete your account form and just put it in the background of my completely irrelevant web page somewhere else and you wouldn't even see that it had fired because it's all happened in the background using modern web technologies and you wouldn't notice. It would delete your account quietly in the background because that form that delete your account form or that transfer the money form hasn't checked where the request is coming from. There was something in in what's called HTTP, the hypertext transfer protocol, the the very basics of the web, designed to stop this. And it's called the referral header. And it means that when you click a link or submit a form, it includes a thing that says referer this site. So you know if the form request is coming from the actual online bank or the actual delete your request form and not some malicious site elsewhere. The trouble is, if you start checking for that, a lot of users start complaining because that referral header isn't always sent like it should. Maybe you've got an advert blocker or maybe you've got some kind of privacy tool that's blocking that as well. The way to get around that is a one-time key. Uh what the Americans called a nons and which the British definitely do not call a nons. Um the onetime key works by the form on your website generating a unique code. can be anything just a random string of characters each time you create the form and then storing that character and saying right anything that comes back anything that I see needs to have this token with it and this this code this token you've generated is in the form as well completely hidden invisible to the user is something that says token random string of characters I think I just wrote a number plate Um, this random string uh is meaningless, but what it represents is that this form that I just made and I just sent out the user is the one that's coming back. So, meanwhile, over on on my malicious awesome blog, I don't know what this token is. I can't possibly know what this token is because it changes per user and it changes every time the form is requested or at least every few minutes. And if you copy that form, if you take that token and try and use it five minutes later or 10 minutes later or on a form that's already been submitted or for a different person or for any one of these things, they'll look at that token and they'll go, "That's wrong. We're not having that." And suddenly cross- sight request forgery doesn't work anymore. And there are still theoretical attacks and and lucky flukes that could get past this, but in general, that solves it. So, if you're designing web forms for anything which which is permanent, anything like deleting an account, posting a comment, authorizing someone, or even transferring money, if you're not using this, this is a pretty big security hole. And if the sites you're using aren't using it, well, you never know what might happen when you go to some malicious blog somewhere out there. So, that's cross-sight request forgery. And it is it's the third attack. And everyone knows about XSS. If if you're a web developer, cross- sight scripting, XSS is the one you know about. SQL injection, database attacks are the one you know about. But this this one went under the radar for a long time, and there's still a lot of developers that don't know about it. So, if you're designing a site or using a site that has anything kind of permanent to it, have a look and see if a token's being sent because if it isn't, might be a bit of a security hole there. You start with a script tag and then everything stops. You've got a closing script tag down here. Nothing in this section will actually appear on the user screen. What you now let's say I type in Tom with a quote mark in it.

Original Description

If you don't secure your web forms, one mistaken click could be all it takes for your users to delete their own accounts. Tom Scott explains. http://www.facebook.com/computerphile https://twitter.com/computer_phile More from Tom Scott: http://www.youtube.com/user/enyay and https://twitter.com/tomscott Hacking Websites with SQL Injection: http://www.youtube.com/watch?v=_jKylhJtPmI Cracking Websites with Cross Site Scripting: http://www.youtube.com/watch?v=L5l9lSnNMxg This video was filmed and edited by Sean Riley. Computerphile is a sister project to Brady Haran's Numberphile. See the full list of Brady's video projects at: http://bit.ly/bradychannels
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Computerphile · Computerphile · 35 of 60

1 Follow the Cookie Trail - Computerphile
Follow the Cookie Trail - Computerphile
Computerphile
2 EXTRA BITS - Follow the Cookie Trail - Computerphile
EXTRA BITS - Follow the Cookie Trail - Computerphile
Computerphile
3 Musical Floppy Drives - Computerphile
Musical Floppy Drives - Computerphile
Computerphile
4 The Hair Algorithm - Computerphile
The Hair Algorithm - Computerphile
Computerphile
5 Getting Sorted & Big O Notation - Computerphile
Getting Sorted & Big O Notation - Computerphile
Computerphile
6 Quick Sort - Computerphile
Quick Sort - Computerphile
Computerphile
7 Hyper History and Cyber War - Computerphile
Hyper History and Cyber War - Computerphile
Computerphile
8 Entropy in Compression - Computerphile
Entropy in Compression - Computerphile
Computerphile
9 Original Elite on the BBC B - Computerphile
Original Elite on the BBC B - Computerphile
Computerphile
10 IP Addresses and the Internet - Computerphile
IP Addresses and the Internet - Computerphile
Computerphile
11 A Career in Video Games - Computerphile
A Career in Video Games - Computerphile
Computerphile
12 Error Detection and Flipping the Bits - Computerphile
Error Detection and Flipping the Bits - Computerphile
Computerphile
13 Programming BASIC and Sorting - Computerphile
Programming BASIC and Sorting - Computerphile
Computerphile
14 Birthplace of the World Wide Web - Computerphile
Birthplace of the World Wide Web - Computerphile
Computerphile
15 Punch Card Programming - Computerphile
Punch Card Programming - Computerphile
Computerphile
16 Programming Paradigms - Computerphile
Programming Paradigms - Computerphile
Computerphile
17 CERN Computing Centre (and mouse farm) - Computerphile
CERN Computing Centre (and mouse farm) - Computerphile
Computerphile
18 Error Correction - Computerphile
Error Correction - Computerphile
Computerphile
19 Home-Made Code - Computerphile
Home-Made Code - Computerphile
Computerphile
20 Security of Data on Disk - Computerphile
Security of Data on Disk - Computerphile
Computerphile
21 Gesture Controls - Computerphile
Gesture Controls - Computerphile
Computerphile
22 How Intelligent is Artificial Intelligence? - Computerphile
How Intelligent is Artificial Intelligence? - Computerphile
Computerphile
23 Encryption and Security Agencies - Computerphile
Encryption and Security Agencies - Computerphile
Computerphile
24 Virtual Machines Power the Cloud - Computerphile
Virtual Machines Power the Cloud - Computerphile
Computerphile
25 Hacking Websites with SQL Injection - Computerphile
Hacking Websites with SQL Injection - Computerphile
Computerphile
26 How Huffman Trees Work - Computerphile
How Huffman Trees Work - Computerphile
Computerphile
27 Cracking Websites with Cross Site Scripting - Computerphile
Cracking Websites with Cross Site Scripting - Computerphile
Computerphile
28 Cloud Computing (Cloudy with a Chance of Pizza) - Computerphile
Cloud Computing (Cloudy with a Chance of Pizza) - Computerphile
Computerphile
29 Texting Cabbage with a Recorder - Computerphile
Texting Cabbage with a Recorder - Computerphile
Computerphile
30 Hashing Algorithms and Security - Computerphile
Hashing Algorithms and Security - Computerphile
Computerphile
31 How YouTube Works - Computerphile
How YouTube Works - Computerphile
Computerphile
32 How NOT to Store Passwords! - Computerphile
How NOT to Store Passwords! - Computerphile
Computerphile
33 A New Golden Age of Video Games - Computerphile
A New Golden Age of Video Games - Computerphile
Computerphile
34 A Universe of Triangles - Computerphile
A Universe of Triangles - Computerphile
Computerphile
Cross Site Request Forgery - Computerphile
Cross Site Request Forgery - Computerphile
Computerphile
36 The True Power of the Matrix (Transformations in Graphics) - Computerphile
The True Power of the Matrix (Transformations in Graphics) - Computerphile
Computerphile
37 The Great 202 Jailbreak - Computerphile
The Great 202 Jailbreak - Computerphile
Computerphile
38 EXTRA BITS - Printing and Typesetting History - Computerphile
EXTRA BITS - Printing and Typesetting History - Computerphile
Computerphile
39 Triangles to Pixels - Computerphile
Triangles to Pixels - Computerphile
Computerphile
40 The Problem with Time & Timezones - Computerphile
The Problem with Time & Timezones - Computerphile
Computerphile
41 The Visibility Problem - Computerphile
The Visibility Problem - Computerphile
Computerphile
42 Lights and Shadows in Graphics - Computerphile
Lights and Shadows in Graphics - Computerphile
Computerphile
43 The Penguin Barcode - Computerphile
The Penguin Barcode - Computerphile
Computerphile
44 Typesetters in the '80s - Computerphile
Typesetters in the '80s - Computerphile
Computerphile
45 The Font Magicians - Computerphile
The Font Magicians - Computerphile
Computerphile
46 The Little Mac with the Big Bite - Computerphile
The Little Mac with the Big Bite - Computerphile
Computerphile
47 EXTRA BITS - More on the Original Mac at 30 - Computerphile
EXTRA BITS - More on the Original Mac at 30 - Computerphile
Computerphile
48 XP to Ubuntu with an 8yr old Hacktop - Computerphile
XP to Ubuntu with an 8yr old Hacktop - Computerphile
Computerphile
49 EXTRA BITS - Hacktop Real-Time Boot Comparison - Computerphile
EXTRA BITS - Hacktop Real-Time Boot Comparison - Computerphile
Computerphile
50 EXTRA BITS - Making a Bootable USB in Linux - Computerphile
EXTRA BITS - Making a Bootable USB in Linux - Computerphile
Computerphile
51 EXTRA BITS - Installing Ubuntu Permanently - Computerphile
EXTRA BITS - Installing Ubuntu Permanently - Computerphile
Computerphile
52 The Dawn of Desktop Publishing - Computerphile
The Dawn of Desktop Publishing - Computerphile
Computerphile
53 What is Bootstrapping? - Computerphile
What is Bootstrapping? - Computerphile
Computerphile
54 Reverse Polish Notation and The Stack - Computerphile
Reverse Polish Notation and The Stack - Computerphile
Computerphile
55 Home-Made Z80 Retro Computer - Computerphile
Home-Made Z80 Retro Computer - Computerphile
Computerphile
56 Should Everybody Learn to Code? - Computerphile
Should Everybody Learn to Code? - Computerphile
Computerphile
57 Programming in PostScript - Computerphile
Programming in PostScript - Computerphile
Computerphile
58 Heartbleed, Running the Code - Computerphile
Heartbleed, Running the Code - Computerphile
Computerphile
59 YouTube's Secret Algorithm - Computerphile
YouTube's Secret Algorithm - Computerphile
Computerphile
60 YouTube Search & Discovery - Computerphile
YouTube Search & Discovery - Computerphile
Computerphile

This video teaches about Cross-Site Request Forgery (CSRF) attacks and how to prevent them using tokens and one-time keys, which is crucial for web application security.

Key Takeaways
  1. Design a malicious website with a form that is not visible to the user
  2. Pre-fill the form with the user's account number and amount
  3. Trick the user into clicking the 'go' button on the malicious website
  4. Send the request to the web application without the user's knowledge or consent
  5. Use a token that changes per user and per request to prevent forgery
  6. Check the token on the server-side to prevent forgery
💡 CSRF attacks can be prevented by using a token that changes per user and per request, which is sent with the form and checked on the server-side.

Related AI Lessons

Security Belongs on the Blueprint
Integrate security into building design to mitigate physical and cyber risks
Medium · Cybersecurity
# A 4-Line HTML File Stole the Admin’s Secret — Intigriti LeakyJar CTF Writeup
Learn how a 4-line HTML file exploited a CSRF vulnerability to steal an admin's secret in the Intigriti LeakyJar CTF challenge
Medium · Cybersecurity
The Digital Gateway to Arabic Cybersecurity
Learn about the importance of language-specific cybersecurity solutions, particularly for Arabic-speaking regions, and how they can enhance digital security
Medium · Cybersecurity
Cybersecurity vs Cloud Computing – Which Career Will Dominate 2026? ☁️
Learn which IT career, cybersecurity or cloud computing, will dominate in 2026 and why it matters for your career choices
Medium · Cybersecurity
Up next
You Think Your Card Declined by Mistake? It Might Be a 2026 Scam
Tolulope Michael
Watch →