From 5b11fcca49629e5ee2922d502645e55b987b2c4f Mon Sep 17 00:00:00 2001 From: Translator Date: Mon, 8 Sep 2025 02:43:14 +0000 Subject: [PATCH] Translated ['', 'src/network-services-pentesting/pentesting-web/electron --- .../electron-desktop-apps/README.md | 311 ++++++++++++------ 1 file changed, 218 insertions(+), 93 deletions(-) diff --git a/src/network-services-pentesting/pentesting-web/electron-desktop-apps/README.md b/src/network-services-pentesting/pentesting-web/electron-desktop-apps/README.md index 102649f84..1b18bd8ae 100644 --- a/src/network-services-pentesting/pentesting-web/electron-desktop-apps/README.md +++ b/src/network-services-pentesting/pentesting-web/electron-desktop-apps/README.md @@ -1,17 +1,17 @@ -# Electron Desktop Apps +# Electron 桌面应用 {{#include ../../../banners/hacktricks-training.md}} ## 介绍 -Electron结合了本地后端(使用**NodeJS**)和前端(**Chromium**),尽管它缺乏现代浏览器的一些安全机制。 +Electron 将本地后端(使用 **NodeJS**)和前端(使用 **Chromium**)结合在一起,尽管它缺少现代浏览器的一些安全机制。 -通常,您可能会在`.asar`应用程序中找到electron应用代码,获取代码需要提取它: +通常你可能会在 `.asar` 应用中找到 Electron 应用的代码,要获取这些代码你需要将其提取: ```bash npx asar extract app.asar destfolder #Extract everything npx asar extract-file app.asar main.js #Extract just a file ``` -在 Electron 应用的源代码中,在 `packet.json` 文件内,可以找到指定的 `main.js` 文件,其中设置了安全配置。 +在 Electron 应用的源代码中,在 `packet.json` 内,你可以看到指定了 `main.js` 文件,其中设置了安全配置。 ```json { "name": "standard-notes", @@ -19,8 +19,8 @@ npx asar extract-file app.asar main.js #Extract just a file ``` Electron 有 2 种进程类型: -- 主进程(完全访问 NodeJS) -- 渲染进程(出于安全原因,NodeJS 的访问应受到限制) +- 主进程(可完全访问 NodeJS) +- 渲染进程(出于安全原因,应该限制对 NodeJS 的访问) ![](<../../../images/image (182).png>) @@ -32,20 +32,20 @@ let win = new BrowserWindow() //Open Renderer Process win.loadURL(`file://path/to/index.html`) ``` -**renderer process** 的设置可以在 **main process** 中的 main.js 文件中进行 **配置**。一些配置将 **防止 Electron 应用程序获取 RCE** 或其他漏洞,如果 **设置正确配置**。 +Settings of the **渲染进程** can be **配置** in the **主进程** inside the main.js file. Some of the configurations will **阻止 Electron 应用程序 获取 RCE** or other vulnerabilities if the **设置正确配置**。 -Electron 应用程序 **可以通过 Node apis 访问设备**,尽管可以配置以防止它: +The Electron application **能够访问设备** via Node apis although it can be configure to prevent it: -- **`nodeIntegration`** - 默认情况下为 `off`。如果开启,允许从 renderer process 访问 node 功能。 -- **`contextIsolation`** - 默认情况下为 `on`。如果关闭,主进程和渲染进程不被隔离。 -- **`preload`** - 默认情况下为空。 -- [**`sandbox`**](https://docs.w3cub.com/electron/api/sandbox-option) - 默认情况下为 off。它将限制 NodeJS 可以执行的操作。 +- **`nodeIntegration`** - is `off` by default. If on, allows to access node features from the 渲染进程. +- **`contextIsolation`** - is `on` by default. If off, 主进程 and 渲染进程 aren't isolated. +- **`preload`** - empty by default. +- [**`sandbox`**](https://docs.w3cub.com/electron/api/sandbox-option) - is off by default. It will restrict the actions NodeJS can perform. - Node Integration in Workers -- **`nodeIntegrationInSubframes`** - 默认情况下为 `off`。 -- 如果 **`nodeIntegration`** 被 **启用**,这将允许在 Electron 应用程序中 **加载在 iframes 中的网页** 使用 **Node.js APIs**。 -- 如果 **`nodeIntegration`** 被 **禁用**,则预加载将在 iframe 中加载。 +- **`nodeIntegrationInSubframes`**- is `off` by default. +- If **`nodeIntegration`** is **enabled**, this would allow the use of **Node.js APIs** in web pages that are **加载在 iframes 中** within an Electron application. +- If **`nodeIntegration`** is **disabled**, then preload 会在 iframe 中加载 -配置示例: +Example of configuration: ```javascript const mainWindowOptions = { title: "Discord", @@ -71,7 +71,7 @@ spellcheck: true, }, } ``` -一些 **RCE payloads** 来自 [这里](https://7as.es/electron/nodeIntegration_rce.txt): +一些 **RCE payloads** 来自 [here](https://7as.es/electron/nodeIntegration_rce.txt): ```html Example Payloads (Windows): ``` ### 捕获流量 -修改 start-main 配置并添加使用代理,例如: +修改 start-main 配置并添加对 proxy 的使用,例如: ```javascript "start-main": "electron ./dist/main/main.js --proxy-server=127.0.0.1:8080 --ignore-certificateerrors", ``` -## Electron 本地代码注入 +## Electron Local Code Injection + +如果你能在本地执行一个 Electron App,可能会让它执行任意的 javascript 代码。详见: -如果您可以本地执行一个 Electron 应用程序,那么您可能可以使其执行任意的 JavaScript 代码。查看如何操作: {{#ref}} ../../../macos-hardening/macos-security-and-privilege-escalation/macos-proces-abuse/macos-electron-applications-injection.md @@ -111,7 +112,7 @@ onerror="alert(require('child_process').execSync('uname -a').toString());" /> ## RCE: XSS + nodeIntegration -如果 **nodeIntegration** 设置为 **on**,网页的 JavaScript 可以通过调用 `require()` 轻松使用 Node.js 功能。例如,在 Windows 上执行计算器应用程序的方法是: +如果 **nodeIntegration** 被设置为 **on**,网页的 JavaScript 可以通过调用 `require()` 轻松使用 Node.js 功能。例如,在 Windows 上执行 calc 应用的方式是: ```html ``` -> [!NOTE] > **如果 `contextIsolation` 开启,这将不起作用** +> [!NOTE] > **如果 `contextIsolation` 启用,则此方法无效** ## RCE: XSS + contextIsolation -_**contextIsolation**_ 引入了 **网页脚本与 JavaScript Electron 内部代码之间的分离上下文**,使得每段代码的 JavaScript 执行不会相互影响。这是消除 RCE 可能性的必要特性。 +The _**contextIsolation**_ introduces the **separated contexts between the web page scripts and the JavaScript Electron's internal code** so that the JavaScript execution of each code does not affect each. This is a necessary feature to eliminate the possibility of RCE. -如果上下文没有被隔离,攻击者可以: +If the contexts aren't isolated an attacker can: -1. 在渲染器中执行 **任意 JavaScript**(XSS 或导航到外部网站) -2. **覆盖内置方法**,该方法在预加载或 Electron 内部代码中被使用,替换为自己的函数 -3. **触发** 使用 **被覆盖的函数** -4. RCE? +1. Execute **在 renderer 中的任意 JavaScript** (XSS or navigation to external sites) +2. **覆盖内置方法**,该方法在 preload 或 Electron 内部代码中被使用以控制函数 +3. **触发** 对 **被覆盖函数** 的调用 +4. RCE? + +There are 2 places where built-int methods can be overwritten: In preload code or in Electron internal code: -内置方法可以被覆盖的地方有两个:在预加载代码中或在 Electron 内部代码中: {{#ref}} electron-contextisolation-rce-via-preload-code.md {{#endref}} + {{#ref}} electron-contextisolation-rce-via-electron-internal-code.md {{#endref}} + {{#ref}} electron-contextisolation-rce-via-ipc.md {{#endref}} ### 绕过点击事件 -如果在点击链接时应用了限制,你可能能够通过 **中间点击** 而不是常规的左键点击来绕过这些限制。 +如果在点击链接时存在限制,你可能可以通过 **使用中键点击** 而非常规的左键点击 来绕过它们 ```javascript window.addEventListener('click', (e) => { ``` -## RCE via shell.openExternal +## RCE 通过 shell.openExternal -有关此示例的更多信息,请查看 [https://shabarkin.medium.com/1-click-rce-in-electron-applications-79b52e1fe8b8](https://shabarkin.medium.com/1-click-rce-in-electron-applications-79b52e1fe8b8) 和 [https://benjamin-altpeter.de/shell-openexternal-dangers/](https://benjamin-altpeter.de/shell-openexternal-dangers/) +有关这些示例的更多信息,请参阅 [https://shabarkin.medium.com/1-click-rce-in-electron-applications-79b52e1fe8b8](https://shabarkin.medium.com/1-click-rce-in-electron-applications-79b52e1fe8b8) 和 [https://benjamin-altpeter.de/shell-openexternal-dangers/](https://benjamin-altpeter.de/shell-openexternal-dangers/) -在部署 Electron 桌面应用程序时,确保 `nodeIntegration` 和 `contextIsolation` 的正确设置至关重要。已确定,**客户端远程代码执行 (RCE)** 针对预加载脚本或 Electron 的主进程本地代码在这些设置到位时有效防止。 +在部署 Electron 桌面应用时,确保 `nodeIntegration` 和 `contextIsolation` 的设置正确至关重要。已经确定,当这些设置就位时,可以有效防止来自主进程、针对 preload 脚本或 Electron 原生代码的 **client-side remote code execution (RCE)**。 -当用户与链接交互或打开新窗口时,会触发特定的事件监听器,这些监听器对应用程序的安全性和功能至关重要: +当用户与链接交互或打开新窗口时,会触发特定的事件监听器,这些监听器对于应用的安全性和功能至关重要: ```javascript webContents.on("new-window", function (event, url, disposition, options) {} webContents.on("will-navigate", function (event, url) {} ``` -这些监听器被**桌面应用程序重写**以实现其自己的**业务逻辑**。该应用程序评估导航链接是否应在内部打开或在外部网页浏览器中打开。这个决定通常通过一个函数`openInternally`来做出。如果该函数返回`false`,则表示链接应在外部打开,利用`shell.openExternal`函数。 +这些监听器**被桌面应用程序重写**以实现其自身的**业务逻辑**。应用程序会评估导航到的链接是应在应用内打开还是在外部 web 浏览器中打开。这个决定通常通过一个函数,`openInternally`,来做出。如果该函数返回 `false`,则表示该链接应在外部打开,使用 `shell.openExternal` 函数。 -**以下是简化的伪代码:** +**Here is a simplified pseudocode:** ![https://miro.medium.com/max/1400/1*iqX26DMEr9RF7nMC1ANMAA.png](<../../../images/image (261).png>) ![https://miro.medium.com/max/1400/1*ZfgVwT3X1V_UfjcKaAccag.png](<../../../images/image (963).png>) -Electron JS安全最佳实践建议不要使用`openExternal`函数接受不受信任的内容,因为这可能通过各种协议导致RCE。操作系统支持不同的协议,这些协议可能触发RCE。有关此主题的详细示例和进一步解释,可以参考[这个资源](https://positive.security/blog/url-open-rce#windows-10-19042),其中包括能够利用此漏洞的Windows协议示例。 +Electron JS 安全最佳实践建议不要通过 `openExternal` 函数接受不受信任的内容,因为这可能通过各种协议导致 RCE。操作系统支持不同的协议,这些协议可能触发 RCE。有关此主题的详细示例和进一步说明,可参阅 [this resource](https://positive.security/blog/url-open-rce#windows-10-19042),其中包含能够利用此漏洞的 Windows 协议示例。 -在macos中,`openExternal`函数可以被利用来执行任意命令,例如`shell.openExternal('file:///System/Applications/Calculator.app')`。 +在 macos 中,`openExternal` 函数可以被利用来执行任意命令,例如 `shell.openExternal('file:///System/Applications/Calculator.app')`。 -**Windows协议漏洞的示例包括:** +**Examples of Windows protocol exploits include:** ```html ``` -## **RCE: XSS + 旧版 Chromium** +## **RCE: XSS + 过时的 chromium** -如果应用程序使用的 **chromium** 是 **旧版** 并且存在 **已知的** **漏洞**,那么可能可以通过 **XSS 利用它并获得 RCE**。\ -您可以在这个 **writeup** 中看到一个例子: [https://blog.electrovolt.io/posts/discord-rce/](https://blog.electrovolt.io/posts/discord-rce/) +如果应用使用的 **chromium** 版本**过旧**,且存在已知的 **漏洞**,则可能通过 **XSS** **利用它并获得 RCE**。\\ +你可以在这篇 **writeup** 中看到一个示例: [https://blog.electrovolt.io/posts/discord-rce/](https://blog.electrovolt.io/posts/discord-rce/) -## **通过内部 URL 正则绕过进行 XSS 钓鱼** +## **XSS Phishing(通过 Internal URL regex bypass)** -假设您发现了一个 XSS,但您 **无法触发 RCE 或窃取内部文件**,您可以尝试利用它来 **通过钓鱼窃取凭据**。 +假设你发现了一个 XSS,但你**无法触发 RCE 或窃取内部文件**,你可以尝试利用它**通过 phishing 窃取凭证**。 -首先,您需要了解当您尝试打开一个新 URL 时,前端的 JS 代码会发生什么: +首先,你需要了解当尝试打开一个新 URL 时会发生什么,可以通过检查前端的 JS 代码来确认: ```javascript webContents.on("new-window", function (event, url, disposition, options) {} // opens the custom openInternally function (it is declared below) webContents.on("will-navigate", function (event, url) {} // opens the custom openInternally function (it is declared below) ``` -对**`openInternally`**的调用将决定**链接**是作为平台的链接在**桌面窗口**中**打开**,还是作为**第三方资源**在**浏览器**中打开。 +The call to **`openInternally`** will decide if the **link** will be **opened** in the **desktop window** as it's a link belonging to the platform, **or** if will be opened in the **browser as a 3rd party resource**. -如果该函数使用的**regex**对绕过**漏洞**(例如**未转义子域的点**)是**脆弱的**,攻击者可以利用XSS**打开一个新窗口**,该窗口位于攻击者的基础设施中,**向用户请求凭据**: +在对 **`openInternally`** 的调用中,会决定该 **link** 是否作为属于平台的链接在 **desktop window** 中 **打开**,**or** 是否会在 **browser as a 3rd party resource** 中打开。 + +In the case the **regex** used by the function is **vulnerable to bypasses** (for example by **not escaping the dots of subdomains**) an attacker could abuse the XSS to **open a new window which** will be located in the attackers infrastructure **asking for credentials** to the user: + +如果该函数使用的 **regex** 存在 **易于被绕过**(例如 **未对子域名中的点进行转义**),攻击者可以滥用 XSS 去 **打开一个新窗口**,该窗口位于攻击者的基础设施中,**向用户索取凭证**: ```html