Debug.setlocal() 安全问题

问题描述

debug.setlocal 是否有任何安全问题?如果可以,请告诉我它到底是如何工作的。

解决方法

Lua 线程运行在“栈”之上,栈是构成语言中大部分内存的项目列表。一切,从“全局”变量到表格,都在这个列表中。

当你创建一个局部变量并执行你的代码时,堆栈看起来像这样:

local a = "the letter a"
STACK
+---+----------------+
| 1 | "the letter a" |
+---+----------------+

现在,假设我们是恶意代码。我们知道下面的代码是用来运行我们的程序的:

local isAdministrator = false
local Code = "malicious code here!"
loadstring(Code)()

嗯,这意味着 loader 的堆栈看起来像这样:

+---+-------+
| 1 | false | isAdministrator
+---+-------+----------------+
| 2 | "malicious code here!" | Code
+---+---------------------+--+
| 3 | function loadstring |
+---+---------------------+--+
| 4 | function MaliciousCode |
+---+------------------------+

现在,假设更改 isAdministrator 对我们有一定的价值。让我们看看我们将如何做到这一点。

1 我们需要得到 lua 来修改我们函数上方的“堆栈”。想象 lua 是一棵树,就像这样:

otherScript
 + loader
    + maliciousCode

这棵树中的每一步都有自己的堆栈,多亏了 debug 库,它上面的堆栈中的每个变量都可以“访问”到它下面的堆栈。

如果我们当前所在的“堆栈”是堆栈 #1(始终为真) 然后调用当前函数的函数在栈 #2(这几乎总是正确的)

现在,查看我们要更改的函数的堆栈,isAdministrator 位于第一个插槽 1 中。所以,为了改变这一点,我们 juuuust:

debug.setlocal(2,1,true)

还有呸。 isAdministrator 现在是 true。我们可以随意破坏您的计算机!

不要担心 - 有一种方法可以阻止这种情况。拿走调试库真的很容易:

local func = loadstring("malicious code!")
setfenv(func,{
    --  Here we insert what functions our baby function can use. Let's give it the string,table,and math libraries- which are pretty safe
    math = math,table = table,string = string,--  Oh,and let's give it the `os.time` function as well.
    os = { time = os.time }
})
func()

现在,func 无法访问调试库,您可以放心地使用您愚蠢的 isAdministrator 本地人了。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...