ब्राउज़र एक्सटेंशन Pentesting पद्धति
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 का समर्थन करें
- सदस्यता योजनाओं की जांच करें!
- हमारे 💬 Discord समूह या टेलीग्राम समूह में शामिल हों या हमें Twitter 🐦 @hacktricks_live** पर फॉलो करें।**
- हैकिंग ट्रिक्स साझा करें और HackTricks और HackTricks Cloud गिटहब रिपोजिटरी में PRs सबमिट करें।
बुनियादी जानकारी
ब्राउज़र एक्सटेंशन JavaScript में लिखे जाते हैं और ब्राउज़र द्वारा पृष्ठभूमि में लोड किए जाते हैं। इसका अपना DOM होता है लेकिन यह अन्य साइटों के DOM के साथ इंटरैक्ट कर सकता है। इसका मतलब है कि यह अन्य साइटों की गोपनीयता, अखंडता, और उपलब्धता (CIA) को प्रभावित कर सकता है।
मुख्य घटक
एक्सटेंशन लेआउट्स को विज़ुअलाइज़ करने पर बेहतर समझा जा सकता है और ये तीन घटकों से मिलकर बनते हैं। आइए प्रत्येक घटक को गहराई से देखें।
 (1) (1).png)
Content Scripts
प्रत्येक content script का एक single web page के DOM तक सीधे पहुंच होती है और इसलिए यह potentially malicious input के संपर्क में आ सकता है। हालांकि, content script के पास extension core को संदेश भेजने की क्षमता के अलावा कोई अनुमतियाँ नहीं होतीं।
Extension Core
extension core में अधिकतर extension privileges/access होते हैं, लेकिन extension core केवल XMLHttpRequest और content scripts के माध्यम से वेब कंटेंट के साथ इंटरैक्ट कर सकता है। साथ ही, extension core को host machine तक सीधे पहुँच नहीं होती।
Native Binary
extension एक native binary की अनुमति देता है जो user के पूर्ण privileges के साथ host machine तक access कर सकता है। यह native binary extension core के साथ standard Netscape Plugin Application Programming Interface (NPAPI) के माध्यम से इंटरैक्ट करता है, जिसे Flash और अन्य browser plug-ins द्वारा उपयोग किया जाता है।
सीमाएँ
Caution
user के पूर्ण privileges प्राप्त करने के लिए, एक attacker को extension को इस तरह मनाना होगा कि वह malicious input को content script से extension’s core तक और extension’s core से native binary तक पास करे।
एक्सटेंशन के प्रत्येक घटक को एक-दूसरे से strong protective boundaries द्वारा अलग किया गया है। प्रत्येक घटक एक separate operating system process में चलता है। Content scripts और extension cores अधिकांश operating system services के लिए उपलब्ध न होने वाले sandbox processes में चलते हैं।
इसके अलावा, content scripts अपने संबंधित वेब पेजों से running in a separate JavaScript heap द्वारा अलग रहते हैं। content script और वेब पेज के पास access to the same underlying DOM होती है, लेकिन दोनों never exchange JavaScript pointers, जिससे JavaScript functionality का leaking रोका जाता है।
manifest.json
A Chrome extension is just a ZIP folder with a .crx file extension. The extension’s core is the manifest.json file at the root of the folder, which specifies layout, permissions, and other configuration options.
उदाहरण:
{
"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
Content scripts तब लोड होते हैं जब भी उपयोगकर्ता किसी मिलते‑जुलते पेज पर नेविगेट करता है, हमारे मामले में कोई भी पेज जो https://example.com/* expression से मेल खाता हो और *://*/*/business* regex से मेल नहीं खाता। वे पेज की अपनी स्क्रिप्ट्स की तरह चलते हैं और पेज के Document Object Model (DOM) तक मनमाना एक्सेस रखते हैं।
"content_scripts": [
{
"js": [
"script.js"
],
"matches": [
"https://example.com/*",
"https://www.example.com/*"
],
"exclude_matches": ["*://*/*business*"],
}
],
अधिक URLs को शामिल या बाहर करने के लिए include_globs और exclude_globs का उपयोग भी किया जा सकता है।
यह एक उदाहरण content script है जो पेज में एक explain button जोड़ देगा जब the storage API का उपयोग extension की storage से message वैल्यू प्राप्त करने के लिए किया जाएगा।
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)
})
.png)
जब इस बटन पर क्लिक किया जाता है, तो content script द्वारा extension पेजेस को एक संदेश भेजा जाता है, runtime.sendMessage() API के उपयोग के माध्यम से। यह इसलिए है क्योंकि content script को सीधे APIs तक पहुँचने में सीमाएँ होती हैं, और storage उन कुछ अपवादों में से एक है। इन अपवादों से परे की सुविधाओं के लिए, संदेश उन extension पेजेस को भेजे जाते हैं जिनसे content scripts संवाद कर सकते हैं।
Warning
ब्राउज़र के अनुसार, content script की क्षमताएँ थोड़ी भिन्न हो सकती हैं। Chromium-based ब्राउज़र्स के लिए, क्षमताओं की सूची Chrome Developers documentation में उपलब्ध है, और Firefox के लिए प्रमुख स्रोत MDN है।
यह भी ध्यान देने योग्य है कि content scripts background scripts के साथ संवाद करने में सक्षम होते हैं, जिससे वे कार्रवाई कर सकते हैं और प्रतिक्रियाएँ वापस भेज सकते हैं।
Chrome में content scripts को देखने और डिबग करने के लिए, Chrome developer tools मेन्यू को Options > More tools > Developer tools से एक्सेस किया जा सकता है या Ctrl + Shift + I दबाकर।
जब developer tools दिखाई दें, तो पहले Source tab पर क्लिक करें, उसके बाद Content Scripts tab। इससे विभिन्न extensions के चल रहे content scripts को देखा जा सकता है और execution flow को ट्रैक करने के लिए breakpoints सेट किए जा सकते हैं।
इंजेक्ट किए गए content scripts
Tip
ध्यान रखें कि Content Scripts अनिवार्य नहीं हैं, क्योंकि यह भी संभव है कि scripts को डायनामिक रूप से inject किया जाए और वेब पेजों में programatically inject किया जा सके via
tabs.executeScript। यह वास्तव में अधिक granular controls प्रदान करता है।
किसी content script को programmatic रूप से inject करने के लिए, extension को उस पेज के लिए host permissions की आवश्यकता होती है जिसमें scripts inject किए जाने हैं। ये permissions या तो extension के manifest में उन्हें request करके प्राप्त किए जा सकते हैं या अस्थायी रूप से activeTab के माध्यम से।
activeTab-आधारित एक्सटेंशन का उदाहरण
{
"name": "My extension",
...
"permissions": [
"activeTab",
"scripting"
],
"background": {
"service_worker": "background.js"
},
"action": {
"default_title": "Action Button"
}
}
- JS फ़ाइल को क्लिक पर Inject करें:
// 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" })
अधिक URLs को include या exclude करने के लिए include_globs और exclude_globs का उपयोग भी किया जा सकता है।
कंटेंट स्क्रिप्ट्स run_at
run_at फील्ड नियंत्रित करता है कब JavaScript फाइलें वेब पेज में इंजेक्ट की जाती हैं। पसंदीदा और डिफ़ॉल्ट मान "document_idle" है।
संभावित मान हैं:
document_idle: जब भी संभव होdocument_start:cssकी किसी भी फाइल के बाद, लेकिन किसी भी अन्य DOM के निर्माण या किसी अन्य स्क्रिप्ट के चलने से पहले।document_end: DOM पूरा होने के तुरंत बाद, लेकिन images और frames जैसे subresources लोड होने से पहले।
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
content scripts द्वारा भेजे गए संदेश background page द्वारा प्राप्त होते हैं, जो extension के घटकों के समन्वय में एक केंद्रीय भूमिका निभाता है। ध्यान देने योग्य है कि background page extension के lifetime में मौजूद रहता है और सीधे उपयोगकर्ता इंटरैक्शन के बिना काम करता है। इसका अपना Document Object Model (DOM) होता है, जो जटिल interactions और state management को सक्षम बनाता है।
मुख्य बिंदु:
- Background Page Role: extension के लिए नर्व सेंटर की तरह कार्य करता है, extension के विभिन्न भागों के बीच संचार और समन्वय सुनिश्चित करता है।
- Persistence: यह हमेशा मौजूद रहता है, उपयोगकर्ता के लिए अदृश्य होता है, पर extension की कार्यक्षमता के लिए अनिवार्य है।
- Automatic Generation: यदि स्पष्ट रूप से परिभाषित नहीं किया गया है, तो ब्राउज़र स्वतः एक background page बना देगा। यह auto-generated पेज उन सभी background scripts को शामिल करेगा जो extension के manifest में निर्दिष्ट हैं, जिससे extension के background tasks का निर्बाध संचालन सुनिश्चित होता है।
Tip
जब ब्राउज़र स्वतः एक background page बनाता है (जब स्पष्ट रूप से घोषित नहीं किया गया हो) तब यह सुविधा सुनिश्चित करती है कि सभी आवश्यक background scripts एकीकृत और सक्रिय हों, जिससे extension की setup प्रक्रिया सरल हो जाती है।
Example background script:
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request == "explain") {
chrome.tabs.create({ url: "https://example.net/explanation" })
}
})
यह runtime.onMessage API का उपयोग संदेश सुनने के लिए करता है। जब एक "explain" संदेश प्राप्त होता है, तो यह tabs API का उपयोग करके एक पेज को नए टैब में खोलता है।
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 में विभिन्न प्रकार के पेज हो सकते हैं:
- Action pages तब दिखाई देते हैं जब एक्सटेंशन आइकन पर क्लिक करने पर एक drop-down खुलता है।
- पेज जिन्हें एक्सटेंशन एक नए टैब में लोड करेगा।
- Option Pages: यह पेज क्लिक करने पर एक्सटेंशन के ऊपर प्रदर्शित होता है। पिछले manifest में, मेरे मामले में मैं इस पेज को
chrome://extensions/?options=fadlhnelkbeojnebcbkacjilhnbjfjcaमें एक्सेस कर पाया या क्लिक करके:
.png)
ध्यान दें कि ये पृष्ठ बैकग्राउंड पेज की तरह persistent नहीं होते क्योंकि ये आवश्यकतानुसार सामग्री को डायनामिकली लोड करते हैं। इसके बावजूद, ये बैकग्राउंड पेज के साथ कुछ क्षमताएँ साझा करते हैं:
- Communication with Content Scripts: बैकग्राउंड पेज की तरह, ये पृष्ठ content scripts से संदेश प्राप्त कर सकते हैं, जिससे एक्सटेंशन के भीतर इंटरैक्शन की सुविधा मिलती है।
- Access to Extension-Specific APIs: ये पेज एक्सटेंशन-विशिष्ट APIs तक व्यापक पहुँच रखते हैं, जो कि एक्सटेंशन के लिए परिभाषित permissions के अधीन होती है।
permissions & host_permissions
permissions और host_permissions manifest.json के एंट्री हैं जो यह संकेत देते हैं कि ब्राउज़र एक्सटेंशन के पास कौन-कौन सी permissions हैं (storage, location…) और किन वेब पेजों में।
चूंकि browser extensions बहुत privileged हो सकते हैं, एक malicious एक्सटेंशन या कोई compromised एक्सटेंशन attacker को संवेदनशील जानकारी चुराने और उपयोगकर्ता पर जासूसी करने के विभिन्न साधन प्रदान कर सकता है।
Check how these settings work and how they could get abused in:
BrowExt - permissions & host_permissions
content_security_policy
A content security policy को manifest.json के अंदर भी घोषित किया जा सकता है। यदि कोई परिभाषित है, तो यह कमज़ोर हो सकता है।
The default setting for browser extension pages is rather restrictive:
script-src 'self'; object-src 'self';
CSP और संभावित 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
In public extensions the extension-id is accesible:
.png)
हालाँकि, यदि manifest.json पैरामीटर use_dynamic_url उपयोग किया गया हो, तो यह id डायनेमिक हो सकता है।
Tip
ध्यान दें कि यहाँ कोई page का ज़िक्र होने पर भी वह ClickJacking के खिलाफ protected हो सकता है, जो Content Security Policy के कारण होता है। इसलिए ClickJacking attack संभव होने की पुष्टि करने से पहले आपको इसे (frame-ancestors section) भी चेक करना चाहिए।
इन pages तक पहुँच की अनुमति होने पर ये pages संभावित रूप से ClickJacking के लिए vulnerable हो सकते हैं:
Tip
इन pages को केवल extension द्वारा ही load होने की अनुमति देना और random URLs से रोकना ClickJacking attacks को रोक सकता है।
Caution
ध्यान दें कि
web_accessible_resourcesसे आने वाले पेज और extension के अन्य पेज भी contacting background scripts में सक्षम होते हैं। इसलिए यदि इन पेजों में से कोई XSS के लिए vulnerable है तो यह बड़ी vulnerability खोल सकता है।इसके अलावा, ध्यान दें कि आप केवल उन पेजों को जो
web_accessible_resourcesमें सूचित हैं ही iframes के अंदर खोल सकते हैं, लेकिन एक नए tab से extension ID जानकर extension का कोई भी पेज access किया जा सकता है। इसलिए यदि वही parameters abusing करके कोई XSS पाया जाता है, तो वह तब भी abused किया जा सकता है यदि पेजweb_accessible_resourcesमें configure नहीं किया गया हो।
externally_connectable
As per the docs, The "externally_connectable" manifest property declares which extensions and web pages can connect to your extension via runtime.connect and runtime.sendMessage.
- यदि
externally_connectablekey आपके extension के manifest में घोषित नहीं है या इसे"ids": ["*"]के रूप में घोषित किया गया है, तो all extensions connect कर सकते हैं, लेकिन कोई भी web pages connect नहीं कर पाएंगे। - यदि specific IDs निर्दिष्ट किए गए हैं, जैसे
"ids": ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"]में, तो केवल वे applications connect कर पाएँगे। - यदि matches निर्दिष्ट किए गए हैं, तो वह web apps connect करने में सक्षम होंगे:
"matches": [
"https://*.google.com/*",
"*://*.chromium.org/*",
- If it’s specified as empty:
"externally_connectable": {}, no app or web will be able to connect.
The less extensions and URLs indicated here, the smaller the attack surface will be.
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
WebRequestorDeclarativeNetRequestAPIs 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 और वेब पेज के बीच संचार के लिए आम तौर पर post messages का उपयोग होता है। इसलिए, वेब application में आप अक्सर function calls जैसे window.postMessage पाएँगे और content script में listeners जैसे window.addEventListener। ध्यान दें, कि extension भी web application के साथ communicate कर सकता है भेजकर एक Post Message (और इसलिए web को इसकी उम्मीद रखनी चाहिए) या बस वेब को एक नया script लोड करा सकता है।
Extension के अंदर
आमतौर पर extension के अंदर संदेश भेजने के लिए function chrome.runtime.sendMessage का उपयोग किया जाता है (आम तौर पर background script द्वारा handle किया जाता है) और इसे प्राप्त करके handle करने के लिए एक listener declare किया जाता है जो chrome.runtime.onMessage.addListener को कॉल करता है।
यह भी संभव है कि chrome.runtime.connect() का उपयोग करके single messages भेजने की बजाय एक persistent connection बनाई जाए; इसका उपयोग send और receive messages के लिए किया जा सकता है जैसा कि निम्न उदाहरण में:
chrome.runtime.connect() उदाहरण
```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>
यह भी संभव है कि background script से किसी विशिष्ट टैब में स्थित content script को संदेश भेजा जाए, **`chrome.tabs.sendMessage`** को कॉल करके जहाँ आपको संदेश भेजने के लिए उस टैब का **ID of the tab** निर्दिष्ट करना होगा।
### अनुमति प्राप्त `externally_connectable` से extension तक
**Web apps and external browser extensions allowed** को `externally_connectable` configuration में अनुमति होने पर requests भेजने के लिए निम्न का उपयोग कर सकते हैं :
```javascript
chrome.runtime.sendMessage(extensionId, ...
जहाँ आवश्यक हो, वहाँ extension ID का उल्लेख करें।
Native Messaging
यह संभव है कि background scripts सिस्टम के अंदर के binaries के साथ संवाद करें, जो यदि यह संचार ठीक तरह से सुरक्षित न किया गया हो तो RCEs जैसे critical vulnerabilities के लिए प्रवण हो सकते हैं। 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 ऑपरेट करते हैं और जहाँ host pages मौजूद हैं, वे एक-दूसरे से separated होते हैं, जिससे isolation सुनिश्चित होती है। इस isolation के बावजूद, दोनों के पास पेज के Document Object Model (DOM) के साथ इंटरैक्ट करने की क्षमता होती है, जो एक साझा संसाधन है। host page के लिए यह आवश्यक है कि वह content script के साथ, या content script के माध्यम से extension के साथ अप्रत्यक्ष रूप से संचार करने के लिए दोनों पक्षों द्वारा पहुँच योग्य 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 को प्राप्त संदेश की प्रामाणिकता की जाँच करनी चाहिए — यह निम्नलिखित की जाँच करके किया जा सकता है:
event.isTrusted: यह केवल तभी True होता है जब इवेंट किसी उपयोगकर्ता की क्रिया से ट्रिगर हुआ हो- Content Script संभवतः केवल तब संदेश की अपेक्षा करेगा जब उपयोगकर्ता कोई क्रिया करे
- origin domain: संदेश केवल allowlist किए गए domains से ही स्वीकार करने की उम्मीद हो सकती है
- यदि regex का उपयोग किया जा रहा है, तो बहुत सतर्क रहें
- Source:
received_message.source !== windowका उपयोग यह जांचने के लिए किया जा सकता है कि संदेश उसी window से था जहाँ Content Script सुन रहा है।
पिछली जाँचें, भले ही की गई हों, कमजोर हो सकती हैं, इसलिए निम्नलिखित पृष्ठ पर potential Post Message bypasses देखें:
Iframe
संचार का एक और संभव तरीका Iframe URLs के माध्यम से हो सकता है; इसका एक उदाहरण आप यहाँ पाएंगे:
DOM
यह बिल्कुल “संचार” का तरीका नहीं है, लेकिन web और Content Script दोनों को web DOM तक पहुँच होती है। इसलिए, यदि Content Script उससे कुछ जानकारी पढ़ रहा है और web DOM पर भरोसा कर रहा है, तो web उस डेटा को बदल सकता है (क्योंकि web पर भरोसा नहीं किया जाना चाहिए, या web XSS के लिए vulnerable है) और Content Script को compromise कर सकता है।
आप एक उदाहरण भी पा सकते हैं — एक DOM based XSS to compromise a browser extension — यहाँ:
Content Script ↔︎ Background Script Communication
A Content Script runtime.sendMessage() or tabs.sendMessage() फ़ंक्शन का उपयोग करके एक one-time JSON-serializable संदेश भेज सकता है।
रिस्पॉन्स को हैंडल करने के लिए लौटे हुए Promise का उपयोग करें। हालांकि, backward compatibility के लिए आप अभी भी अंतिम argument के रूप में callback पास कर सकते हैं।
Content Script से request भेजना इस तरह दिखता है:
;(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 event listener सेट अप करना होगा। यह 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() समकालिक (synchronous) तरीके से निष्पादित हुआ था। onMessage इवेंट हैंडलर को sendResponse() के असमकालिक (asynchronous) निष्पादन के लिए संशोधित करने हेतु return true; जोड़ना अनिवार्य है।
एक महत्वपूर्ण विचार यह है कि जब कई पृष्ठ onMessage इवेंट प्राप्त करने के लिए सेट हों, तो किसी विशेष इवेंट के लिए पहला पेज जो sendResponse() को execute करेगा ही प्रभावी रूप से प्रतिक्रिया प्रदान कर पाएगा। उसी इवेंट के लिए बाद की कोई भी प्रतिक्रियाएँ ध्यान में नहीं ली जाएँगी।
नए extensions बनाते समय callbacks की बजाय promises का उपयोग प्राथमिकता होनी चाहिए। callbacks के उपयोग के संदर्भ में, sendResponse() फ़ंक्शन केवल तभी वैध माना जाता है जब इसे सीधे समकालिक (synchronous) संदर्भ में निष्पादित किया गया हो, या यदि इवेंट हैंडलर true लौटाकर असमकालिक ऑपरेशन संकेत करता हो। यदि किसी भी हैंडलर ने true वापस नहीं किया या sendResponse() फ़ंक्शन मेमोरी से हटा दिया गया (garbage-collected) है, तो डिफ़ॉल्ट रूप से sendMessage() से जुड़ा callback ट्रिगर हो जाएगा।
Native Messaging
Browser extensions भी यह अनुमति देती हैं कि वे 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 वह string है जो browser extension के background scripts से application से संवाद करने के लिए runtime.connectNative() या runtime.sendNativeMessage() को पास किया जाता है। path binary का path है, केवल 1 मान्य type है जो कि stdio है (stdin और stdout का उपयोग करता है) और allowed_origins उन extensions को दर्शाते हैं जो इसे access कर सकते हैं (और इनमें wildcard नहीं हो सकता)।
Chrome/Chromium इस json को Windows registry और macOS तथा Linux के कुछ paths में खोजेगा (अधिक जानकारी docs में)।
Tip
Browser extension को यह communication उपयोग करने में सक्षम होने के लिए
nativeMessaingpermission घोषित करने की भी आवश्यकता होती है।
नीचे दिखाया गया है कि background script code native application को messages भेजते समय कैसा दिखता है:
chrome.runtime.sendNativeMessage(
"com.my_company.my_application",
{ text: "Hello" },
function (response) {
console.log("Received " + response)
}
)
In this blog post, एक vulnerable पैटर्न जो native messages का दुरुपयोग करता है प्रस्तावित किया गया है:
- Browser extension का content script के लिए एक wildcard pattern होता है।
- Content script
postMessagemessages को background script कोsendMessageके माध्यम से पास करता है। - Background script message को native application को
sendNativeMessageके माध्यम से पास करता है। - Native application message को खतरनाक तरीके से हैंडल करता है, जिससे code execution हो सकता है।
और इसका अंदर एक उदाहरण है कि किसी भी पेज से RCE तक कैसे पहुँचा जा सकता है abusing a browser extension।
Sensitive Information in Memory/Code/Clipboard
यदि एक Browser Extension अपनी memory के अंदर sensitive information स्टोर करता है, तो इसे dump किया जा सकता है (खासकर Windows मशीनों में) और उस जानकारी के लिए search किया जा सकता है।
इसलिए, Browser Extension की memory को secure नहीं माना जाना चाहिए और sensitive information जैसे credentials या mnemonic phrases store नहीं करने चाहिए।
बेशक, code में संवेदनशील जानकारी न रखें, क्योंकि वह public होगा।
Browser से memory dump करने के लिए आप process memory को dump कर सकते हैं या browser extension की settings में जाकर Inspect pop-up -> Memory सेक्शन -> Take a snaphost और CTRL+F करके snapshot के अंदर संवेदनशील जानकारी खोज सकते हैं।
इसके अलावा, बहुत ही संवेदनशील जानकारी जैसे mnemonic keys या passwords क्लिपबोर्ड में कॉपी करने की अनुमति नहीं दी जानी चाहिए (या कम से कम कुछ सेकंड में उसे क्लिपबोर्ड से हटा देना चाहिए), क्योंकि तब क्लिपबोर्ड की निगरानी करने वाली प्रक्रियाएँ उन्हें प्राप्त कर सकेंगी।
Loading an Extension in the Browser
- Browser Extension को Download करके अनज़िप करें
chrome://extensions/पर जाएँ औरDeveloper Modeenable करेंLoad unpackedबटन पर क्लिक करें
Firefox में आप about:debugging#/runtime/this-firefox पर जाएँ और Load Temporary Add-on बटन पर क्लिक करें।
Getting the source code from the store
Chrome extension का source code विभिन्न तरीकों से प्राप्त किया जा सकता है। नीचे हर विकल्प के लिए विस्तृत स्पष्टीकरण और निर्देश दिए गए हैं।
Download Extension as ZIP via Command Line
Chrome extension का source code command line का उपयोग करके ZIP फ़ाइल के रूप में डाउनलोड किया जा सकता है। इसमें curl का उपयोग करके किसी निश्चित URL से ZIP फ़ाइल को फ़ेच करना और फिर ZIP फ़ाइल की सामग्री को किसी डायरेक्टरी में extract करना शामिल है। यहाँ कदम दिए गए हैं:
"extension_id"को वास्तविक extension ID से बदलें।- निम्नलिखित commands को execute करें:
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"
Use the CRX Viewer website
Use the CRX Viewer extension
एक और सुविधाजनक तरीका Chrome Extension Source Viewer का उपयोग करना है, जो एक open-source प्रोजेक्ट है। इसे Chrome Web Store से इंस्टॉल किया जा सकता है: https://chrome.google.com/webstore/detail/chrome-extension-source-v/jifpbeccnghkjeaalbbjmodiffmgedin?hl=en। viewer का source code इसके GitHub repository पर उपलब्ध है: https://github.com/Rob–W/crxviewer।
View source of locally installed extension
Chrome में स्थानीय रूप से इंस्टॉल किए गए एक्सटेंशन को भी निरीक्षित किया जा सकता है। तरीका इस प्रकार है:
- अपने Chrome local profile directory तक पहुँचने के लिए
chrome://version/खोलें और “Profile Path” फ़ील्ड देखें। - प्रोफ़ाइल डायरेक्टरी के भीतर
Extensions/उपफ़ोल्डर पर नेविगेट करें। - यह फ़ोल्डर सभी इंस्टॉल किए गए एक्सटेंशनों को रखता है, आमतौर पर इनका source code पढ़ने योग्य फॉर्मेट में होता है।
एक्सटेंशनों की पहचान करने के लिए, आप उनके IDs को नामों से मैप कर सकते हैं:
- प्रत्येक एक्सटेंशन की IDs देखने के लिए
about:extensionsपेज पर Developer Mode सक्षम करें। - प्रत्येक एक्सटेंशन के फ़ोल्डर के भीतर,
manifest.jsonफ़ाइल में एक पढ़ने योग्यnameफ़ील्ड होती है, जो एक्सटेंशन की पहचान करने में मदद करती है।
Use a File Archiver or Unpacker
Chrome Web Store पर जाएँ और एक्सटेंशन डाउनलोड करें। फ़ाइल का एक्सटेंशन .crx होगा। फ़ाइल एक्सटेंशन को .crx से .zip में बदलें। किसी भी file archiver (जैसे WinRAR, 7-Zip, आदि) का उपयोग करके ZIP फ़ाइल की सामग्री को extract करें।
Use Developer Mode in Chrome
Chrome खोलें और chrome://extensions/ पर जाएँ। ऊपर दाईं ओर “Developer mode” सक्षम करें। “Load unpacked extension…” पर क्लिक करें। अपने एक्सटेंशन की डायरेक्टरी पर नेविगेट करें। यह source code डाउनलोड नहीं करता, पर पहले से डाउनलोड या विकसित किए गए एक्सटेंशन के कोड को देखने और संशोधित करने के लिए उपयोगी है।
Chrome extension manifest dataset
सभी संभावित vulnerable browser एक्सटेंशनों को पहचानने की कोशिश करने के लिए आप https://github.com/palant/chrome-extension-manifests-dataset का उपयोग कर सकते हैं और संभावित vulnerable संकेतों के लिए उनके manifest फ़ाइलों की जाँच कर सकते हैं। उदाहरण के लिए, अधिक से अधिक 25000 users वाले एक्सटेंशनों, content_scripts और permission 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)
Stealthy technique to backdoor Chromium by directly editing per-user Preferences and forging valid HMACs, causing the browser to accept and activate an arbitrary unpacked extension without prompts or flags.
Forced Extension Load Preferences Mac Forgery Windows
Detecting Malicious Extension Updates (Static Version Diffing)
Supply-chain compromises अक्सर पहले से benign extensions में किए गए malicious updates के रूप में आते हैं। एक व्यावहारिक, low-noise तरीका यह है कि नए extension package की तुलना last known-good version से की जाए, static analysis का उपयोग करके (उदाहरण के लिए, Assemblyline). उद्देश्य है किसी भी छोटे बदलाव पर अलर्ट करने की बजाय केवल उन high-signal deltas पर अलर्ट देना जो वास्तव में जोखिम सूचित करते हैं।
Workflow
- Submit both versions (old + new) को एक ही static-analysis profile में चलाएँ।
- Flag new or updated background/service worker scripts (persistence + privileged logic)।
- Flag new or updated content scripts (DOM access और data collection)।
- Flag new permissions/host_permissions जो
manifest.jsonमें जोड़े गए हों। - Flag new domains जो code से निकाले गए हों (संभावित C2/exfil endpoints)।
- Flag new static-analysis detections (उदा., base64 decode, cookie harvesting, network-request builders, obfuscation patterns)।
- Flag statistical anomalies जैसे कि बदले हुए scripts में तेज entropy उछाल या outlier z-scores।
Detecting script changes accurately
- New script added →
manifest.jsondiff के माध्यम से पता करें। - Existing script modified (manifest अपरिवर्तित) → extracted file tree से per-file hashes की तुलना करें (उदा., Assemblyline
Extractoutput). यह existing workers या content scripts में छिपे हुए stealthy updates पकड़ता है।
Pre-disclosure detections
पहले से ज्ञात IOCs पर आधारित “easy mode” detections से बचने के लिए, threat-intel-fed services को disable करें और intrinsic signals (domains, heuristic signatures, script deltas, entropy anomalies) पर निर्भर रहें। इससे सार्वजनिक रिपोर्टिंग से पहले malicious updates पकड़ने के मौके बढ़ते हैं।
Example high-confidence alert logic
- Low-noise combo: new domains + new static-analysis detections + updated background/service worker + updated or added content scripts।
- Broader catch: new domain + new or updated background/service worker (higher recall, higher noise)।
Key Assemblyline services for this workflow:
- Extract: extension को unpack करता है और per-file hashes देता है।
- Characterize: file characteristics (उदा., entropy) को compute करता है।
- JsJAWS / FrankenStrings / URLCreator: JS heuristics, strings, और domains को surface करता है ताकि versions में diff किया जा सके।
Security Audit Checklist
हालाँकि Browser Extensions का attack surface सीमित होता है, कुछ extensions में vulnerabilities या hardening सुधार की संभावनाएँ होती हैं। सबसे सामान्य चेकलिस्ट निम्नलिखित है:
- जितना संभव हो उतना requested
permissionsको Limit करें - जितना संभव हो उतना
host_permissionsको Limit करें - एक मजबूत
content_security_policyका उपयोग करें - जितना संभव हो उतना
externally_connectableको Limit करें; यदि आवश्यक नहीं है तो डिफ़ॉल्ट रूप से न रखें, specify करें{} - यदि कोई URL जो这里 XSS या takeover के लिए vulnerable है, यहां उल्लेखित है, तो attacker सीधे background scripts को संदेश भेज पाएगा। यह बहुत शक्तिशाली bypass हो सकता है।
- जितना संभव हो उतना
web_accessible_resourcesको Limit करें, यदि संभव हो तो खाली रखें। - यदि
web_accessible_resourcesnone नहीं है, तो ClickJacking के लिए जाँच करें - यदि extension और web page के बीच कोई communication हो रही है, तो उस communication में पैदा होने वाली संभावित XSS vulnerabilities की जाँच करें।
- यदि Post Messages उपयोग हो रहे हैं, तो Post Message vulnerabilities की जाँच करें।
- यदि Content Script DOM तक पहुँचता है, तो सुनिश्चित करें कि वे web द्वारा बदलने पर XSS introduce न करें
- विशेष ध्यान दें यदि यह communication Content Script -> Background script communication में भी शामिल है
- यदि background script native messaging के माध्यम से communicate कर रहा है तो सुनिश्चित करें कि communication secure और sanitized है
- Sensitive information को Browser Extension के code के अंदर नहीं स्टोर किया जाना चाहिए
- Sensitive information को Browser Extension की memory के अंदर नहीं रखा जाना चाहिए
- Sensitive information को file system unprotected में नहीं रखना चाहिए
Browser Extension Risks
- ऐप https://crxaminer.tech/ कुछ डेटा, जैसे कि extension द्वारा माँगी गई permissions, analyze करता है ताकि extension के उपयोग का risk level दिया जा सके।
Tools
Tarnish
- दिए गए Chrome webstore link से किसी भी Chrome extension को pull करता है।
- manifest.json viewer: extension के manifest का JSON-prettified संस्करण दिखाता है।
- Fingerprint Analysis: web_accessible_resources का पता लगाता है और Chrome extension fingerprinting JavaScript स्वतः जनरेट करता है।
- Potential Clickjacking Analysis: उन extension HTML पृष्ठों का पता लगाता है जिनमें web_accessible_resources directive सेट है। पृष्ठों के उद्देश्य के आधार पर ये Clickjacking के प्रति संवेदनशील हो सकते हैं।
- Permission Warning(s) viewer: यह उन सभी Chrome permission prompt warnings की सूची दिखाता है जो उपयोगकर्ता द्वारा extension install करने पर प्रदर्शित होंगे।
- Dangerous Function(s): संभावित खतरनाक functions (उदा., innerHTML, chrome.tabs.executeScript) के स्थान को दिखाता है जो attacker द्वारा exploit हो सकते हैं।
- Entry Point(s): दिखाता है कि extension कहाँ user/external input लेता है। यह extension के surface area को समझने और संभावित स्थानों की पहचान करने में उपयोगी है जहाँ maliciously-crafted data भेजा जा सकता है।
- Dangerous Function(s) और Entry Point(s) scanners द्वारा जनरेट किए गए alerts के लिए जानकारी:
- संबंधित code snippet और alert का कारण बनने वाली लाइन।
- समस्या का विवरण।
- “View File” बटन जिससे पूरा source file देखा जा सके।
- alerted file का path।
- alerted file का पूरा Chrome extension URI।
- फाइल का प्रकार, जैसे Background Page script, Content Script, Browser Action, आदि।
- यदि vulnerable line किसी JavaScript फाइल में है, तो उन सभी पृष्ठों के paths जहाँ यह शामिल है, साथ ही उन पृष्ठों का type और web_accessible_resource status।
- Content Security Policy (CSP) analyzer and bypass checker: यह आपके extension की CSP में कमजोरियों की ओर संकेत करेगा और उन संभावित bypass मार्गों को भी दिखाएगा जो whitelisted CDNs आदि के कारण हो सकते हैं।
- Known Vulnerable Libraries: यह Retire.js का उपयोग करके ज्ञात-vulnerable JavaScript libraries के उपयोग की जाँच करता है।
- Extension को डाउनलोड करने और formatted versions डाउनलोड करने की सुविधा।
- Original extension डाउनलोड करने का विकल्प।
- Beautified version (auto prettified HTML और JavaScript) डाउनलोड करने का विकल्प।
- स्कैन परिणामों का automatic caching; पहली बार स्कैन में समय लग सकता है, लेकिन बाद के स्कैन तेज़ होंगे यदि extension अपडेट नहीं हुआ है।
- Linkable Report URLs, जिससे generated extension report को आसानी से किसी और के साथ share किया जा सकता है।
Neto
Project Neto एक Python 3 package है जिसे Firefox और Chrome जैसे प्रसिद्ध ब्राउज़रों के browser plugins और extensions की hidden features को analyze और उजागर करने के लिए डिज़ाइन किया गया है। यह packaged files को unzip करके relevant resources (जैसे manifest.json, localization folders या Javascript और HTML source files) से इन features को extract करने की प्रक्रिया को automate करता है।
References
- Thanks to @naivenom for the help with this methodology
- https://www.cobalt.io/blog/introduction-to-chrome-browser-extension-security-testing
- https://palant.info/2022/08/10/anatomy-of-a-basic-extension/
- https://palant.info/2022/08/24/attack-surface-of-extension-pages/
- https://palant.info/2022/08/31/when-extension-pages-are-web-accessible/
- https://help.passbolt.com/assets/files/PBL-02-report.pdf
- https://developer.chrome.com/docs/extensions/develop/concepts/content-scripts
- https://developer.chrome.com/docs/extensions/mv2/background-pages
- https://thehackerblog.com/kicking-the-rims-a-guide-for-securely-writing-and-auditing-chrome-extensions/
- https://gist.github.com/LongJohnCoder/9ddf5735df3a4f2e9559665fb864eac0
- https://redcanary.com/blog/threat-detection/assemblyline-browser-extensions/
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 का समर्थन करें
- सदस्यता योजनाओं की जांच करें!
- हमारे 💬 Discord समूह या टेलीग्राम समूह में शामिल हों या हमें Twitter 🐦 @hacktricks_live** पर फॉलो करें।**
- हैकिंग ट्रिक्स साझा करें और HackTricks और HackTricks Cloud गिटहब रिपोजिटरी में PRs सबमिट करें।


