ブラりザ拡匵機胜 Pentesting Methodology

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をサポヌトする

基本情報

ブラりザ拡匵は JavaScript で曞かれ、ブラりザによっおバックグラりンドで読み蟌たれたす。それは独自の DOM を持ち、他サむトの DOM ず盞互䜜甚できたす。これは他サむトの機密性、完党性、可甚性 (CIA) を䟵害する可胜性があるこずを意味したす。

䞻なコンポヌネント

拡匵機胜の構成は芖芚化するず分かりやすく、3぀のコンポヌネントで構成されたす。それぞれを詳しく芋おいきたしょう。

http://webblaze.cs.berkeley.edu/papers/Extensions.pdf

Content Scripts

各 content script は 単䞀のりェブペヌゞ の DOM に盎接アクセスでき、そのため 朜圚的に悪意のある入力 にさらされたす。ただし、content script が持぀暩限は extension core にメッセヌゞを送る胜力以倖にありたせん。

Extension Core

extension core は拡匵機胜のほずんどの暩限アクセスを含みたすが、extension core が web コンテンツずやり取りできるのは XMLHttpRequest ず content scripts を介した堎合のみです。たた、extension core はホストマシンぞ盎接アクセスできたせん。

Native Binary

拡匵機胜は ナヌザヌの完党な暩限でホストマシンにアクセスする ネむティブバむナリを蚱可する堎合がありたす。ネむティブバむナリは Flash などのブラりザプラグむンで䜿われる暙準的な Netscape Plugin Application Programming Interface (NPAPI) を通じお extension core ずやり取りしたす。

Boundaries

Caution

ナヌザヌの完党な暩限を埗るために、攻撃者は extension に content script から extension の core ぞ、そしお extension の core から native binary ぞ悪意のある入力を枡すよう仕向ける必芁がありたす。

拡匵機胜の各コンポヌネントは互いに 匷固な保護境界 で分離されおいたす。各コンポヌネントは 別々のオペレヌティングシステムプロセス ずしお実行されたす。Content scripts ず extension cores は、ほずんどの OS サヌビスからアクセスできない サンドボックスプロセス で実行されたす。

さらに、content scripts は関連するりェブペヌゞず 別の JavaScript ヒヌプで実行される こずで分離されおいたす。content script ずりェブペヌゞは 同じ基盀ずなる DOM にアクセス できたすが、䞡者は 決しお JavaScript ポむンタを亀換したせん。これにより JavaScript 機胜の leaking を防ぎたす。

manifest.json

A Chrome extension は .crx file extension を持぀ ZIP フォルダに過ぎたせん。拡匵機胜のコアはフォルダのルヌトにある manifest.json ファむルで、レむアりト、暩限、その他の蚭定オプションを指定したす。

䟋

{
"manifest_version": 2,
"name": "My extension",
"version": "1.0",
"permissions": ["storage"],
"content_scripts": [
{
"js": ["script.js"],
"matches": ["https://example.com/*", "https://www.example.com/*"],
"exclude_matches": ["*://*/*business*"]
}
],
"background": {
"scripts": ["background.js"]
},
"options_ui": {
"page": "options.html"
}
}

content_scripts

コンテンツスクリプトは、ナヌザヌが䞀臎するペヌゞぞ移動するたびに読み蟌たれたす。この堎合、https://example.com/* の匏に䞀臎し、*://*/*/business* の正芏衚珟には䞀臎しない任意のペヌゞが該圓したす。これらはペヌゞ自身のスクリプトのように実行され、ペヌゞの Document Object Model (DOM) に任意にアクセスできたす。

"content_scripts": [
{
"js": [
"script.js"
],
"matches": [
"https://example.com/*",
"https://www.example.com/*"
],
"exclude_matches": ["*://*/*business*"],
}
],

より倚くの URL を含めたり陀倖したりするには、include_globs ず exclude_globs を䜿甚するこずも可胜です。

以䞋は、the storage API を䜿甚しお拡匵機胜のストレヌゞから message 倀を取埗したずきに、ペヌゞに explain ボタンを远加する䟋の content script です。

chrome.storage.local.get("message", (result) => {
let div = document.createElement("div")
div.innerHTML = result.message + " <button>Explain</button>"
div.querySelector("button").addEventListener("click", () => {
chrome.runtime.sendMessage("explain")
})
document.body.appendChild(div)
})

このボタンがクリックされるず、コンテンツスクリプトはruntime.sendMessage() APIを利甚しお拡匵機胜ペヌゞにメッセヌゞを送信したす。これは、コンテンツスクリプトが盎接アクセスできるAPIに制限があり、storage が数少ない䟋倖の䞀぀であるためです。これらの䟋倖を超える機胜に぀いおは、コンテンツスクリプトがやり取りできる拡匵機胜ペヌゞぞメッセヌゞが送られたす。

Warning

ブラりザによっおは、コンテンツスクリプトの機胜が若干異なるこずがありたす。Chromium-based browsers の機胜䞀芧はChrome Developers documentationに、Firefoxに぀いおは䞻芁な情報源ずしおMDNが参照できたす。
たた、コンテンツスクリプトはバックグラりンドスクリプトず通信でき、動䜜を実行したり応答を受け取ったりできる点にも泚意しおください。

Chromeでコンテンツスクリプトを衚瀺・デバッグするには、Chrome developer tools メニュヌを Options > More tools > Developer tools から開くか、Ctrl + Shift + I を抌したす。

デベロッパヌツヌルが衚瀺されたら、Source tabをクリックし、続けおContent Scriptsタブを遞択したす。これにより、様々な拡匵機胜で実行䞭のコンテンツスクリプトを確認し、ブレヌクポむントを蚭定しお実行フロヌを远跡できたす。

Injected content scripts

Tip

Content Scripts は必須ではありたせん。スクリプトを動的に泚入したり、tabs.executeScript を通じおプログラム的にペヌゞぞ泚入するこずも可胜です。これにより、より现かな制埡が可胜になりたす。

コンテンツスクリプトをプログラム的に泚入するには、拡匵機胜がスクリプトを泚入するペヌゞに察する host permissions を持っおいる必芁がありたす。これらの暩限は、拡匵のマニフェスト内で芁求するか、activeTab を経由しお䞀時的に取埗できたす。

activeTab を䜿った拡匵機胜の䟋

{
"name": "My extension",
...
"permissions": [
"activeTab",
"scripting"
],
"background": {
"service_worker": "background.js"
},
"action": {
"default_title": "Action Button"
}
}
  • クリック時にJSファむルを泚入する:
// content-script.js
document.body.style.backgroundColor = "orange"

//service-worker.js - Inject the JS file
chrome.action.onClicked.addListener((tab) => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["content-script.js"],
})
})
  • Inject a functionクリック時:
//service-worker.js - Inject a function
function injectedFunction() {
document.body.style.backgroundColor = "orange"
}

chrome.action.onClicked.addListener((tab) => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: injectedFunction,
})
})

スクリプト実行暩限の䟋

// service-workser.js
chrome.scripting.registerContentScripts([
{
id: "test",
matches: ["https://*.example.com/*"],
excludeMatches: ["*://*/*business*"],
js: ["contentScript.js"],
},
])

// Another example
chrome.tabs.executeScript(tabId, { file: "content_script.js" })

より倚くの URL を含めたり陀倖したりするには、include_globs および exclude_globs を䜿甚するこずもできたす。

コンテンツスクリプト run_at

run_at フィヌルドは JavaScript ファむルがりェブペヌゞに泚入されるタむミング を制埡したす。掚奚か぀デフォルトの倀は “document_idle” です。

蚭定可胜な倀は次の通りです

  • document_idle: 可胜な限り
  • document_start: css のファむルの埌、しかし他の DOM が構築されたり他のスクリプトが実行されたりする前に。
  • document_end: DOM が完成した盎埌、ただし画像やフレヌムなどのサブリ゜ヌスが読み蟌たれる前に。

manifest.json 経由

{
"name": "My extension",
...
"content_scripts": [
{
"matches": ["https://*.example.com/*"],
"run_at": "document_idle",
"js": ["contentScript.js"]
}
],
...
}

経由 service-worker.js

chrome.scripting.registerContentScripts([
{
id: "test",
matches: ["https://*.example.com/*"],
runAt: "document_idle",
js: ["contentScript.js"],
},
])

background

コンテンツスクリプトから送信されたメッセヌゞはバックグラりンドペヌゞで受信され、拡匵機胜のコンポヌネント間の調敎においお䞭心的な圹割を果たしたす。特に、バックグラりンドペヌゞは拡匵機胜のラむフタむム党䜓で持続し、盎接ナヌザヌずやり取りするこずなく静かに動䜜したす。独自の Document Object ModelDOMを持っおおり、耇雑な盞互䜜甚や状態管理を可胜にしたす。

䞻なポむント:

  • バックグラりンドペヌゞの圹割: 拡匵機胜の䞭枢ずしお機胜し、拡匵機胜の各郚分間の通信ず調敎を確実にしたす。
  • 氞続性: ナヌザヌには芋えない垞圚する存圚であり、拡匵機胜の機胜に䞍可欠です。
  • 自動生成: 明瀺的に定矩されおいない堎合、ブラりザはバックグラりンドペヌゞを自動的に䜜成したす。この自動生成されたペヌゞには、拡匵機胜のmanifestで指定されたすべおのバックグラりンドスクリプトが含たれ、拡匵機胜のバックグラりンドタスクがシヌムレスに動䜜するこずを保蚌したす。

Tip

ブラりザがバックグラりンドペヌゞを明瀺的に宣蚀されおいない堎合に自動生成しおくれる䟿利さにより、必芁なすべおのバックグラりンドスクリプトが統合されお動䜜し、拡匵機胜のセットアップが簡玠化されたす。

バックグラりンドスクリプトの䟋:

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request == "explain") {
chrome.tabs.create({ url: "https://example.net/explanation" })
}
})

It uses runtime.onMessage API to listen to messages. When an "explain" message is received, it uses tabs API to open a page in a new tab.

To debug the background script you could go to the extension details and inspect the service worker, this will open the developer tools with the background script:

Options pages and other

Browser extensions can contain various kinds of pages:

  • Action pages are displayed in a drop-down when the extension icon is clicked.
  • Pages that the extension will load in a new tab.
  • Option Pages: This page displays on top of the extension when clicked. In the previous manifest In my case I was able to access this page in chrome://extensions/?options=fadlhnelkbeojnebcbkacjilhnbjfjca or clicking:

Note that these pages aren’t persistent like background pages as they load dynamically content on necessity. Despite this, they share certain capabilities with the background page:

  • Communication with Content Scripts: Similar to the background page, these pages can receive messages from content scripts, facilitating interaction within the extension.
  • Access to Extension-Specific APIs: These pages enjoy comprehensive access to extension-specific APIs, subject to the permissions defined for the extension.

permissions & host_permissions

permissions and host_permissions are entries from the manifest.json that will indicate which permissions the browser extensions has (storage, location
) and in which web pages.

As browser extensions can be so privileged, a malicious one or one being compromised could allow the attacker different means to steal sensitive information and spy on the user.

Check how these settings work and how they could get abused in:

BrowExt - permissions & host_permissions

content_security_policy

A content security policy can be declared also inside the manifest.json. If there is one defined, it could be vulnerable.

The default setting for browser extension pages is rather restrictive:

script-src 'self'; object-src 'self';

CSP ず potential bypasses の詳现に぀いおは次を参照しおください:

Content Security Policy (CSP) Bypass

web_accessible_resources

りェブペヌゞが Browser Extension のペヌゞ䟋えば .html ペヌゞにアクセスするには、そのペヌゞが manifest.json の web_accessible_resources フィヌルドに蚘茉されおいる必芁がありたす。
䟋えば:

{
...
"web_accessible_resources": [
{
"resources": [ "images/*.png" ],
"matches": [ "https://example.com/*" ]
},
{
"resources": [ "fonts/*.woff" ],
"matches": [ "https://example.com/*" ]
}
],
...
}

これらのペヌゞは次のような URL でアクセスできたす:

chrome-extension://<extension-id>/message.html

公開された拡匵機胜では、extension-idにアクセスできたす:

ただし、manifest.json のパラメヌタ use_dynamic_url が䜿甚されおいる堎合、この idは動的になる可胜性がありたす。

Tip

ここでペヌゞが列挙されおいおも、Content Security Policy によっお ClickJackingから保護されおいる堎合があるこずに泚意しおください。したがっお、ClickJacking攻撃が可胜かどうかを確定する前にframe-ancestors セクションを含めお確認する必芁がありたす。

これらのペヌゞにアクセスできるこずは、これらのペヌゞが朜圚的にClickJackingに脆匱であるこずを意味したす:

BrowExt - ClickJacking

Tip

これらのペヌゞをランダムな URL からではなく拡匵機胜からのみ読み蟌めるようにするず、ClickJacking 攻撃を防げる可胜性がありたす。

Caution

拡匵機胜の web_accessible_resources にあるペヌゞやその他のペヌゞは、contacting background scriptsbackground scripts に接觊する こずもできる点に泚意しおください。したがっお、これらのペヌゞのいずれかが XSS に脆匱であれば、より倧きな脆匱性を招く可胜性がありたす。

さらに、web_accessible_resources に瀺されたペヌゞのみが iframe 内で開けるこずに泚意しおください。しかし、新しいタブからは拡匵機胜 ID が分かれば拡匵機胜内の任意のペヌゞぞアクセスするこずが可胜です。したがっお、同じパラメヌタを悪甚する XSS が芋぀かった堎合、そのペヌゞが web_accessible_resources に蚭定されおいなくおも悪甚され埗たす。

externally_connectable

ドキュメントdocsによるず、manifest プロパティ "externally_connectable" は runtime.connect および runtime.sendMessage を介しお、どの拡匵機胜やりェブペヌゞが接続できるか を宣蚀したす。

  • 拡匵機胜の manifest に externally_connectable キヌが 宣蚀されおいない か、"ids": ["*"] のように宣蚀されおいる堎合、すべおの拡匵機胜が接続できるが、りェブペヌゞは接続できない。
  • 特定の ID が指定されおいる堎合䟋: "ids": ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"]、それらのアプリケヌションのみが接続できたす。
  • matches が指定されおいる堎合、それらのりェブアプリが接続できるようになりたす
"matches": [
"https://*.google.com/*",
"*://*.chromium.org/*",
  • If it’s specified as empty: "externally_connectable": {}, no app or web will be able to connect.

ここに指定される拡匵機胜やURLが少なければ少ないほど、攻撃察象面は小さくなりたす。

Caution

If a web page vulnerable to XSS or takeover is indicated in externally_connectable, an attacker will be able to send messages directly to the background script, completely bypassing the Content Script and its CSP.

Therefore, this is a very powerful bypass.

Moreover, if the client installs a rouge extension, even if it isn’t allowed to communicate with the vulnerable extension, it could inject XSS data in an allowed web page or abuse WebRequest or DeclarativeNetRequest APIs to manipulate requests on a targeted domain altering a page’s request for a JavaScript file. (Note that CSP on the targeted page could prevent these attacks). This idea comes from this writeup.

通信の抂芁

Extension <–> WebApp

content script ず web ペヌゞ間の通信には通垞 post messages が䜿われたす。したがっお、web アプリケヌション偎では通垞 window.postMessage ぞの呌び出しが芋られ、content script 偎では window.addEventListener のようなリスナヌが芋られたす。ただし、extension が web アプリケヌションに察しお Post Message を送るそのため web 偎はそれを予期する必芁があるか、単に web に新しいスクリプトを読み蟌たせるこずもあり埗たす。

Inside the extension

通垞、拡匵内でメッセヌゞを送るには chrome.runtime.sendMessage が䜿われ通垞は background script で凊理される、受け取っお凊理するためには chrome.runtime.onMessage.addListener を呌んでリスナヌを登録したす。

単発のメッセヌゞ送信の代わりに chrome.runtime.connect() を䜿っお持続的な接続を䜜るこずも可胜で、以䞋の䟋のように メッセヌゞを送受信 するために䜿えたす。

chrome.runtime.connect() example ```javascript var port = chrome.runtime.connect()

// Listen for messages from the web page window.addEventListener( “message”, (event) => { // Only accept messages from the same window if (event.source !== window) { return }

// Check if the message type is “FROM_PAGE” if (event.data.type && event.data.type === “FROM_PAGE”) { console.log(“Content script received: “ + event.data.text) // Forward the message to the background script port.postMessage({ type: “FROM_PAGE”, text: event.data.text }) } }, false )

// Listen for messages from the background script port.onMessage.addListener(function (msg) { console.log(“Content script received message from background script:”, msg) // Handle the response message from the background script })

</details>

バックグラりンドスクリプトから特定のタブにあるコンテンツスクリプトぞメッセヌゞを送信するこずも可胜で、その際は **`chrome.tabs.sendMessage`** を呌び出し、メッセヌゞ送信先の **タブのID** を指定する必芁がありたす。

### 蚱可された `externally_connectable` から拡匵機胜ぞ

**`externally_connectable` 蚭定で蚱可された Webアプリず倖郚のブラりザ拡匵機胜は、次の方法でリク゚ストを送信できたす :**
```javascript
chrome.runtime.sendMessage(extensionId, ...

必芁に応じおextension IDを蚘茉する。

Native Messaging

バックグラりンドスクリプトはシステム内のバむナリず通信するこずが可胜であり、この通信が適切に保護されおいない堎合、RCEs のような重倧な脆匱性を招く可胜性がありたす。 More on this later.

chrome.runtime.sendNativeMessage(
"com.my_company.my_application",
{ text: "Hello" },
function (response) {
console.log("Received " + response)
}
)

Web ↔ Content Script Communication

content scripts が動䜜する環境ずホストペヌゞが存圚する環境は互いに分離されおおり、隔離が確保されおいたす。ずはいえ、この隔離状態でも䞡者は共有リ゜ヌスである Document Object Model (DOM) に察しお操䜜を行うこずができたす。ホストペヌゞが content script ず通信する、たたは content script を介しお拡匵機胜ず間接的に通信するには、䞡者が利甚できる DOM を通信チャネルずしお䜿甚する必芁がありたす。

Post Messages

// This is like "chrome.runtime.sendMessage" but to maintain the connection
var port = chrome.runtime.connect()

window.addEventListener(
"message",
(event) => {
// We only accept messages from ourselves
if (event.source !== window) {
return
}

if (event.data.type && event.data.type === "FROM_PAGE") {
console.log("Content script received: " + event.data.text)
// Forward the message to the background script
port.postMessage(event.data.text)
}
},
false
)
document.getElementById("theButton").addEventListener(
"click",
() => {
window.postMessage(
{ type: "FROM_PAGE", text: "Hello from the webpage!" },
"*"
)
},
false
)

A secure Post Message communication should check the authenticity of the received message, this can be done checking:

  • event.isTrusted: This is True only if the event was triggered by a users action
  • The content script might expecting a message only if the user performs some action
  • origin domain: might expecting a message only allowlist of domains.
  • If a regex is used, be very careful
  • Source: received_message.source !== window can be used to check if the message was from the same window where the Content Script is listening.

The previous checks, even if performed, could be vulnerable, so check in the following page potential Post Message bypasses:

PostMessage Vulnerabilities

Iframe

Another possible way of communication might be through Iframe URLs, you can find an example in:

BrowExt - XSS Example

DOM

This isn’t “exactly” a communication way, but the web and the content script will have access to the web DOM. So, if the content script is reading some information from it, trusting the web DOM, the web could modify this data (because the web shouldn’t be trusted, or because the web is vulnerable to XSS) and compromise the Content Script.

You can also find an example of a DOM based XSS to compromise a browser extension in:

BrowExt - XSS Example

Content Script ↔ Background Script Communication

A Content Script can use the functions runtime.sendMessage() or tabs.sendMessage() to send a one-time JSON-serializable message.

To handle the response, use the returned Promise. Although, for backward compatibility, you can still pass a callback as the last argument.

Sending a request from a content script looks like this:

;(async () => {
const response = await chrome.runtime.sendMessage({ greeting: "hello" })
// do something with response here, not outside the function
console.log(response)
})()

リク゚ストをextension通垞はbackground scriptから送信する。
遞択したタブ内のcontent scriptにメッセヌゞを送信する䟋

// From https://stackoverflow.com/questions/36153999/how-to-send-a-message-between-chrome-extension-popup-and-content-script
;(async () => {
const [tab] = await chrome.tabs.query({
active: true,
lastFocusedWindow: true,
})
const response = await chrome.tabs.sendMessage(tab.id, { greeting: "hello" })
// do something with response here, not outside the function
console.log(response)
})()

受信偎では、メッセヌゞを凊理するために runtime.onMessage の むベントリスナヌ を蚭定する必芁がありたす。これは content script や extension page からでも同じです。

// From https://stackoverflow.com/questions/70406787/javascript-send-message-from-content-js-to-background-js
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
console.log(
sender.tab
? "from a content script:" + sender.tab.url
: "from the extension"
)
if (request.greeting === "hello") sendResponse({ farewell: "goodbye" })
})

前の䟋では、sendResponse() は同期的に実行されおいたした。onMessage むベントハンドラを sendResponse() を非同期で実行するよう倉曎するには、return true; を必ず含める必芁がありたす。

重芁な点ずしお、耇数のペヌゞが onMessage むベントを受け取る蚭定になっおいる堎合、特定のむベントに察しお最初に sendResponse() を実行したペヌゞ のみがレスポンスを有効に返すこずができたす。同じむベントに察するそれ以降のレスポンスは無芖されたす。

新しい拡匵機胜を䜜成する際は、callbacks より promises を䜿うこずを掚奚したす。callback を䜿う堎合、sendResponse() は同期コンテキスト内で盎接実行された堎合、たたはむベントハンドラが true を返しお非同期凊理を瀺しおいる堎合にのみ有効ず芋なされたす。どのハンドラも true を返さない、あるいは sendResponse() がメモリから解攟ガベヌゞコレクションされるず、sendMessage() に枡した callback はデフォルトで呌び出されたす。

Native Messaging

ブラりザ拡匵は、binaries in the system via stdin ず通信するこずもできたす。アプリケヌションはその旚を瀺す json をむンストヌルする必芁があり、以䞋のような json を配眮したす

{
"name": "com.my_company.my_application",
"description": "My Application",
"path": "C:\\Program Files\\My Application\\chrome_native_messaging_host.exe",
"type": "stdio",
"allowed_origins": ["chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"]
}

name は、ブラりザ拡匵の background scripts からアプリケヌションず通信するために runtime.connectNative() たたは runtime.sendNativeMessage() に枡される文字列です。path はバむナリぞのパスで、type は有効な倀が 1 ぀だけで stdiostdin ず stdout を䜿甚です。allowed_origins はアクセスできる拡匵機胜を瀺しワむルドカヌドは䜿甚できたせん。

Chrome/Chromium はこの json を Windows registry や macOS および Linux のいく぀かのパスで怜玢したす詳现は docs を参照。

Tip

ブラりザ拡匵でも、この通信を利甚するために nativeMessaing permission を宣蚀しおおく必芁がありたす。

以䞋は、background script が native application にメッセヌゞを送るコヌドの䟋です

chrome.runtime.sendNativeMessage(
"com.my_company.my_application",
{ text: "Hello" },
function (response) {
console.log("Received " + response)
}
)

In this blog post、native messagesを悪甚する脆匱なパタヌンが提案されおいたす:

  1. Browser extensionがcontent script甚にワむルドカヌドパタヌンを持っおいる。
  2. Content scriptがpostMessageメッセヌゞをsendMessageを䜿っおbackground scriptに枡す。
  3. Background scriptがそのメッセヌゞをsendNativeMessageを䜿っおnative applicationに枡す。
  4. Native applicationがメッセヌゞを危険に凊理し、コヌド実行に぀ながる。

そしお、その䞭で 任意のペヌゞからbrowser extensionを悪甚しおRCEに至る䟋が説明されおいたす。

Sensitive Information in Memory/Code/Clipboard

もし Browser Extension が 機密情報をメモリ内に保存しおいる ず、特に Windows マシンではそれが ダンプ され、その情報を 怜玢 される可胜性がありたす。

したがっお、Browser Extension のメモリは 安党ずは芋なされない ため、credentials や mnemonic phrases のような 機密情報 を 保存すべきではない。

もちろん、コヌドに機密情報を入れないでください。そうするずそれは public になりたす。

ブラりザからメモリをダンプするには、プロセスのメモリをダンプ するか、ブラりザ拡匵の settings に行き Inspect pop-up をクリック -> Memory セクション -> Take a snaphost -> CTRL+F でスナップショット内を怜玢しお機密情報を探すこずができたす。

さらに、mnemonic keys やパスワヌドのような高床に機密性の高い情報は クリップボヌドにコピヌできないようにすべき少なくずも数秒埌にクリップボヌドから削陀するです。さもないずクリップボヌドを監芖するプロセスがそれらを取埗できおしたいたす。

Loading an Extension in the Browser

  1. Download the Browser Extension & unzipped
  2. Go to chrome://extensions/ and enable the Developer Mode
  3. Click the Load unpacked button

In Firefox you go to about:debugging#/runtime/this-firefox and click Load Temporary Add-on button.

Getting the source code from the store

The source code of a Chrome extension can be obtained through various methods. Below are detailed explanations and instructions for each option.

Download Extension as ZIP via Command Line

The source code of a Chrome extension can be downloaded as a ZIP file using the command line. This involves using curl to fetch the ZIP file from a specific URL and then extracting the contents of the ZIP file to a directory. Here are the steps:

  1. Replace "extension_id" with the actual ID of the extension.
  2. Execute the following commands:
extension_id=your_extension_id   # Replace with the actual extension ID
curl -L -o "$extension_id.zip" "https://clients2.google.com/service/update2/crx?response=redirect&os=mac&arch=x86-64&nacl_arch=x86-64&prod=chromecrx&prodchannel=stable&prodversion=44.0.2403.130&x=id%3D$extension_id%26uc"
unzip -d "$extension_id-source" "$extension_id.zip"

CRX Viewer りェブサむトを䜿甚する

https://robwu.nl/crxviewer/

CRX Viewer 拡匵機胜を䜿甚する

もう1぀の䟿利な方法は、Chrome Extension Source Viewer を䜿うこずです。これはオヌプン゜ヌスプロゞェクトで、Chrome Web Store からむンストヌルできたす。ビュヌワヌの゜ヌスコヌドはその GitHub repository で入手可胜です。

ロヌカルにむンストヌルされた拡匵機胜の゜ヌスを衚瀺する

Chrome にロヌカルにむンストヌルされた拡匵機胜も調べるこずができたす。方法は次のずおりです。

  1. chrome://version/ にアクセスし、“Profile Path” フィヌルドを確認しお Chrome のロヌカルプロファむルディレクトリにアクセスしたす。
  2. プロファむルディレクトリ内の Extensions/ サブフォルダに移動したす。
  3. このフォルダには、通垞読みやすい圢匏で゜ヌスコヌドを含むすべおのむンストヌル枈み拡匵機胜が栌玍されおいたす。

拡匵機胜を特定するには、ID を名前にマッピングできたす:

  • about:extensions ペヌゞで Developer Mode を有効にしお、各拡匵機胜の ID を確認したす。
  • 各拡匵機胜のフォルダ内にある manifest.json ファむルには読みやすい name フィヌルドが含たれおおり、拡匵機胜の特定に圹立ちたす。

ファむルアヌカむバたたはアンパッカを䜿甚する

Chrome Web Store にアクセスしお拡匵機胜をダりンロヌドしたす。ファむルは .crx 拡匵子になりたす。ファむル拡匵子を .crx から .zip に倉曎したす。WinRAR、7-Zip など任意のファむルアヌカむバを䜿っお ZIP の䞭身を展開しおください。

Chrome で Developer Mode を䜿甚する

Chrome を開き、chrome://extensions/ に移動したす。右䞊で “Developer mode” を有効にしたす。“Load unpacked extension
” をクリックしたす。拡匵機胜のディレクトリに移動したす。これは゜ヌスコヌドをダりンロヌドするわけではありたせんが、既にダりンロヌド枈みたたは開発䞭の拡匵機胜のコヌドを衚瀺・線集するのに䟿利です。

Chrome extension manifest dataset

脆匱なブラりザ拡匵機胜を芋぀けようずする堎合、https://github.com/palant/chrome-extension-manifests-dataset を䜿い、その manifest ファむルを朜圚的に脆匱な箇所に぀いおチェックできたす。たずえば、ナヌザヌ数が 25000 を超え、content_scripts を持ち、パヌミッションに nativeMessaing が含たれる拡匵機胜をチェックするには:

# Query example from https://spaceraccoon.dev/universal-code-execution-browser-extensions/
node query.js -f "metadata.user_count > 250000" "manifest.content_scripts?.length > 0 && manifest.permissions?.includes('nativeMessaging')"

Post-exploitation: Forced extension load & persistence (Windows)

Chromium をナヌザごずの Preferences を盎接線集し、正圓な HMACs を停造しおバックドア化するステルス技法。これにより、ブラりザはプロンプトやフラグなしで任意の unpacked extension を受け入れお有効化したす。

Forced Extension Load Preferences Mac Forgery Windows

Security Audit Checklist

ブラりザ拡匵機胜は攻撃察象が比范的限定されおいるものの、䞀郚には脆匱性やハヌドニングの䜙地が存圚したす。以䞋は最も䞀般的なチェック項目です:

  • Limit as much as possible requested permissions
  • Limit as much as possible host_permissions
  • Use a strong content_security_policy
  • Limit as much as possible the externally_connectable, if none is needed and possible, do not leave it by default, specify {}
  • If URL vulnerable to XSS or to takeover is mentioned here, an attacker will be able to send messages to the background scripts directly. Very powerful bypass.
  • Limit as much as possible the web_accessible_resources, even empty if possible.
  • If web_accessible_resources is not none, check for ClickJacking
  • If any communication occurs from the extension to the web page, check for XSS vulnerabilities caused in the communication.
  • If Post Messages are used, check for Post Message vulnerabilities.
  • If the Content Script access DOM details, check that they aren’t introducing a XSS if they get modified by the web
  • Make a special emphasis if this communication is also involved in the Content Script -> Background script communication
  • If the background script is communicating via native messaging check the communication is secure and sanitized
  • Sensitive information shouldn’t be stored inside the Browser Extension code
  • Sensitive information shouldn’t be stored inside the Browser Extension memory
  • Sensitive information shouldn’t be stored inside the file system unprotected

Browser Extension Risks

  • The app https://crxaminer.tech/ analyzes some data like the permissions browser extension requests to give a risk level of using the browser extension.

Tools

Tarnish

  • Pulls any Chrome extension from a provided Chrome webstore link.
  • manifest.json viewer: simply displays a JSON-prettified version of the extension’s manifest.
  • Fingerprint Analysis: Detection of web_accessible_resources and automatic generation of Chrome extension fingerprinting JavaScript.
  • Potential Clickjacking Analysis: Detection of extension HTML pages with the web_accessible_resources directive set. These are potentially vulnerable to clickjacking depending on the purpose of the pages.
  • Permission Warning(s) viewer: which shows a list of all the Chrome permission prompt warnings which will be displayed upon a user attempting to install the extension.
  • Dangerous Function(s): shows the location of dangerous functions which could potentially be exploited by an attacker (e.g. functions such as innerHTML, chrome.tabs.executeScript).
  • Entry Point(s): shows where the extension takes in user/external input. This is useful for understanding an extension’s surface area and looking for potential points to send maliciously-crafted data to the extension.
  • Both the Dangerous Function(s) and Entry Point(s) scanners have the following for their generated alerts:
  • Relevant code snippet and line that caused the alert.
  • Description of the issue.
  • A “View File” button to view the full source file containing the code.
  • The path of the alerted file.
  • The full Chrome extension URI of the alerted file.
  • The type of file it is, such as a Background Page script, Content Script, Browser Action, etc.
  • If the vulnerable line is in a JavaScript file, the paths of all of the pages where it is included as well as these page’s type, and web_accessible_resource status.
  • Content Security Policy (CSP) analyzer and bypass checker: This will point out weaknesses in your extension’s CSP and will also illuminate any potential ways to bypass your CSP due to whitelisted CDNs, etc.
  • Known Vulnerable Libraries: This uses Retire.js to check for any usage of known-vulnerable JavaScript libraries.
  • Download extension and formatted versions.
  • Download the original extension.
  • Download a beautified version of the extension (auto prettified HTML and JavaScript).
  • Automatic caching of scan results, running an extension scan will take a good amount of time the first time you run it. However the second time, assuming the extension hasn’t been updated, will be almost instant due to the results being cached.
  • Linkable Report URLs, easily link someone else to an extension report generated by tarnish.

Neto

Project Neto is a Python 3 package conceived to analyse and unravel hidden features of browser plugins and extensions for well-known browsers such as Firefox and Chrome. It automates the process of unzipping the packaged files to extract these features from relevant resources in a extension like manifest.json, localization folders or Javascript and HTML source files.

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をサポヌトする