Learning Library

← Back to Library

Cross‑Site Scripting: Old Yet Dangerous

Key Points

  • XSS (cross‑site scripting) is a decades‑old injection attack that remains the top‑impact threat in recent IBM X‑Force Cloud reports and ranks among OWASP’s top web‑application vulnerabilities.
  • Attackers embed malicious JavaScript into benign sites (e.g., comment fields), which then executes in a victim’s browser under the trusted site’s context.
  • Because the malicious code runs as if it originated from the legitimate website, attackers can deface pages, inject misleading UI elements, or manipulate user actions.
  • The injected script can also steal session cookies or other credentials, effectively hijacking the user’s authenticated session.
  • Mitigation strategies focus on proper input sanitization, output encoding, and security best practices to eliminate XSS vectors and reduce its prevalence.

Full Transcript

# Cross‑Site Scripting: Old Yet Dangerous **Source:** [https://www.youtube.com/watch?v=z4LhLJnmoZ0](https://www.youtube.com/watch?v=z4LhLJnmoZ0) **Duration:** 00:09:17 ## Summary - XSS (cross‑site scripting) is a decades‑old injection attack that remains the top‑impact threat in recent IBM X‑Force Cloud reports and ranks among OWASP’s top web‑application vulnerabilities. - Attackers embed malicious JavaScript into benign sites (e.g., comment fields), which then executes in a victim’s browser under the trusted site’s context. - Because the malicious code runs as if it originated from the legitimate website, attackers can deface pages, inject misleading UI elements, or manipulate user actions. - The injected script can also steal session cookies or other credentials, effectively hijacking the user’s authenticated session. - Mitigation strategies focus on proper input sanitization, output encoding, and security best practices to eliminate XSS vectors and reduce its prevalence. ## Sections - [00:00:00](https://www.youtube.com/watch?v=z4LhLJnmoZ0&t=0s) **Cross‑Site Scripting Still Threatening Web** - The speaker outlines why the decades‑old XSS attack remains a top web vulnerability, breaks down its architecture with a coding example, and proposes mitigation strategies. - [00:03:06](https://www.youtube.com/watch?v=z4LhLJnmoZ0&t=186s) **Session Hijacking and Malicious Injection Risks** - The speaker explains how an attacker injecting code into a trusted website can hijack user sessions, steal credentials, deliver malware, and redirect victims to phishing sites, causing severe damage. - [00:06:13](https://www.youtube.com/watch?v=z4LhLJnmoZ0&t=373s) **Understanding and Mitigating XSS** - The speaker explains how a cross‑site scripting payload executes in a user’s browser and advises validating and sanitizing all user input to prevent such attacks. ## Full Transcript
0:02X.S.S. three letters that the good guys don't want to see. 0:06It stands for cross-site scripting. 0:09It's a type of attack that has been around since 1999. 0:13That's a quarter of a century. 0:14So this is old news in one sense, 0:17but at the same time, it's not. 0:20The recent IBM X-Force Cloud security landscape report In fact, showed it to be the number one most impactful type of attack. 0:30And the well-respected industry group OWASP, 0:34the Open World Wide Application Security Project, 0:37listed it as number three on their list of top ten Web Application Vulnerabilities. 0:44So why is an attack that's been around this long still affecting us in a major way today? 0:50We're going to take a look at this attack and try to understand the architecture, 0:54the anatomy. We're going to peel it apart. 0:56We're going to look at a coding example. 0:57So you can see, in fact, how the attack works. 1:00And then take a look at some recommendations, 1:02some things that we can do that I hope will one day make it so that these numbers disappear when it comes to XSS cross-site scripting. 1:11Okay. Let's take a look at the architecture of a cross-site scripting attack. 1:15How does this thing lay out? 1:17Here's our attacker. Here's our victim. okay. 1:20The attacker finds a website, a regular website, trusted website. 1:24Good guys, they're not really part of the attack, 1:27but they're actually going to be unwitting part of the attack. 1:30The attacker then basically crafts some code. 1:34So he writes a JavaScript or something like that 1:37and puts that in, let's say on this website, there's a comment section 1:41where people can go in and add their own comments, free form text, 1:44and he adds in his script. 1:47Now this guy comes along, the good guy, 1:50unsuspecting, and he visits that site 1:53and either just hovers over it or he clicks on the link. 1:57That then causes this to execute. 1:59Now, the code that this guy has now injected, and that's the key word, this is an injection type of attack 2:05similar to a SQL injection, but this is with different code, 2:10and that is going to now run in this guy's browser, 2:14But here's the trick, 2:15It's running under the context of this website 2:18which is a trusted website, even though this is untrusted code. 2:22And that's where the bad guy is taking advantage. 2:25What could happen now? 2:26As a result, What could happen is the image that he gets back could be a defaced version of this website. 2:33So this guy could actually manipulate what he sees. 2:36That's one possibility is defacement. 2:39And if he deface is that it? Depends on what he puts in, 2:42but he could also add elements to the screen or things like that 2:45and really throw this person off. 2:47Make them do things that they shouldn't otherwise do. 2:50Another really damaging part of this attack is that now this code is running on this guy's system, 2:56and he could, in the script cause the cookies, 3:01the things that are causing the context for this user to be transferred over to the bad guy. 3:06The bad guy has essentially, at that point, hijacked his session. 3:12A hijacking attack now means that let's say, for instance, this is his is bank's website. 3:17Well, now this guy can essentially hijack that session, 3:22begin in the middle and go into his bank account, 3:25empty out his account, buy lots of stuff in his name. 3:28If it's an e-commerce site, something like that. 3:31So this guy now has complete control over the session that the good guy started. 3:36What's another type of thing that could happen? 3:38Well, he could inject through this. 3:41He could inject malware, and that malware 3:44now might escape the browser and run on his system natively. 3:48That also would be a really big problem. 3:50Another possibility, you can see there are a lot of them, 3:53is in the code that he is injected, 3:57maybe there is a redirection to another site, 4:01maybe this is essentially a phishing campaign. 4:05And now when this guy logs in or he's going to go to this site, it's going to ask him to log in. 4:10He's going to put in information about himself, his credentials, and things like that. 4:13And all of that stuff is of course, going to go to this guy. 4:16So there's a lot of different types of scenarios that can happen here. 4:20All very damaging and all doing it under the context of a trusted site. 4:25Even though the code that's been injected is very untrustworthy. 4:29Now, this guy, it's initiated by him inserting, injecting the code into the website, and then someone comes along and views it. 4:38But this guy can be more proactive if he wants. 4:41In fact, one of the things he could do is he could send an email over to this user and in that email, or it could be a text message, something like that, 4:51and in that is the link that includes this code. 4:55And then when he does that it's going to run under the context of that website. 4:59And again give him all of these kinds of attack scenarios. 5:03So you can see this guy takes control. 5:06This guy now is a victim and really doesn't even know in many cases that, in fact, what he's doing is interacting with the bad guy. 5:13Okay. Now we've taken a look at the architecture of a cross-site scripting attack. 5:19Now let's take a look at a coding example. 5:21Super simple. 5:22This one would not do any harm, but you'll get the general idea. 5:26So here is an example of the URL 5:29that the attacker might insert into the comment section. 5:33As an example, in my previous diagram. 5:36Or that they could send in an email. 5:38And so here we see this is the domain name. 5:41So that should look familiar. 5:43That's basically telling it what web server to go to in order to run this particular command, this particular script. 5:52And in this particular script that's what it's going to run. 5:57Now all of that's fine. Really. No damage. 6:00Here's where all the damage comes. 6:02This is the payload. 6:04This is where the bad guy now has basically inserted into, 6:09what would be an otherwise benign link, has inserted his own code. 6:13And in case you don't know how to read what this does, 6:16basically what it's going to do in this case is on the user's browser. 6:20It's going to pop up a box that says that 6:26cross-site scripting. 6:27So that's all this one's going to do. 6:29But you can see the fact is the attacker could take anything that they wanted to 6:35and put that in this section and that now is going to run on the, 6:40unsuspecting victims system 6:43under the context of that website. 6:47That's how they're able to do the magic. 6:49This is a very simple coding example. 6:51It could be worse. 6:53Now we've taken a look at the coding example. 6:56What can you do about this. 6:57We understand the problem, what's the solution? 7:00Well there are a number of things. 7:01It begins with don't trust user input. 7:05Whatever a user puts on your site is something you need to first of all validate. 7:11You need to make sure that they have not inserted anything that you don't want on that page. 7:17So if you've got a comment section, for instance, 7:20the only thing that should be in there would be plain text. If they're putting anything else in there, then that would be a problem. 7:26For instance, you want to look at everything, inspect and reject, inspect all of the input that comes in. 7:33And that's text. That's fine. 7:35That's text. That's fine. 7:36Know, this is code. I'm not going to allow that. 7:40I'm not going to allow, even though we're not talking about SQL injection, look for those commands as well. 7:45And if I see that stuff in thereI'm going to throw that out. 7:48That way I make sure the stuff that goes on the page is in fact plain text. 7:53So inspect and reject. 7:56Don't trust user generated input that comes to your site. 8:00The next thing you can do to help with this is to encode 8:06the output. 8:07Encode any of the stuff that the user has put on your page. 8:11And by that I mean, for instance, if we've got special characters 8:14like these, then convert them into their plain text counterparts. 8:20Now these are the things that the HTML, the browser, is actually going to take and turn into plain text. 8:26If you allow these versions to go through. 8:29Those are the kinds of things that could be inserted and allow a script to be in. 8:34So you're going to validate inputs, encode outputs. Cover both the ends and the outs. 8:41And those are just a couple of things you can do. 8:42Those are the ones that will make the biggest difference on this. 8:45There are actually a number of other things that you can do. 8:47And so you have those recommendations. 8:50One more set of recommendations I would refer you to, 8:53I mentioned OWASP at the beginning of the video. 8:55They have a really nice cheat sheet 8:58And in that cheat sheet they will go through in detail 9:01coding examples and give you even more ideas. 9:03Other way you can avoid what has been a quarter of a century of attacks 9:09using this very common mechanism that still is at the top of the charts. 9:14It's called cross-site scripting. Let's get rid of it.