Security 在lua中执行外部程序,而不使用userinput作为lua中的参数

Security 在lua中执行外部程序,而不使用userinput作为lua中的参数,security,lua,user-input,Security,Lua,User Input,我想在lua中执行一个外部程序。通常这可以通过 os.execute("run '"..arg0.."' 'arg1' arg2") 这种方法的问题是,如果我想将用户输入作为字符串传递给外部程序,那么用户输入可以是;邪恶的“h4ck-teh系统”,上面的脚本将如下执行: /bin/bash -c "run ''; evil 'h4ck teh system' '' 'arg1' arg2" 另一个问题发生在我将'$var'作为参数,并且shell将其替换为其环境变量时。在我的特殊情况下,我有

我想在lua中执行一个外部程序。通常这可以通过

os.execute("run '"..arg0.."' 'arg1' arg2")
这种方法的问题是,如果我想将用户输入作为字符串传递给外部程序,那么用户输入可以是
;邪恶的“h4ck-teh系统”
,上面的脚本将如下执行:

/bin/bash -c "run ''; evil 'h4ck teh system' '' 'arg1' arg2"
另一个问题发生在我将
'$var'
作为参数,并且shell将其替换为其环境变量时。在我的特殊情况下,我有一些类似于
[[program'set title“$my title$”]
——因此嵌套字符串,
程序
解析
“$my title$”
(带转义序列)与
“$my title$”
(按原样)不同。因为我想将标题设置为它,所以最好的方法是使用如下参数:
“我的标题”
。但现在命令必须是:

os.execute([[run "set title '$My Title$'"]])
但是现在,正如我所说,
$My
将被替换为一个空字符串,因为环境不知道任何名为
$My
的变量,而且我从来都不希望它被替换

因此,我正在寻找与

execv("run", {"set title '"..arg0.."'", arg1, arg2})
用法示例:

-- Usage example #1:
execute_commands(
   {"your/program", "arg 1", "$arg2", "arg-3", "~/arg4.txt"},
   {expand=true, "echo", "Your program finished with exit code $?"},
   {"ls", "-l"}
)
-- The following command will be executed:
-- /bin/bash -c 'your/program '\''arg 1'\'' '\''$arg2'\'' arg-3 ~/arg4.txt;echo "Your program finished with exit code $?";ls -l'
$arg2
将不会按您的要求展开为值,因为它周围有单引号。
不幸的是,
“您的程序用退出代码$?”完成。
也不会展开(除非您显式设置
expand=true


e、 g.
execute\u命令({“echo”,[[set my'$program$';cd“finished”;]])
将执行
/bin/bash'echo'\\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\''\'''\''''\'''\'''''\'''\'''''''\'''''''''';cd“完成”;“\\”“”-c
导致此错误:
设置sh:1:cd:cannotcd为finished sh:1:\\:notfound
;它从未打算执行cd@sivizius-如何在
/bin/bash
-c
之间插入文本?我的代码不能产生这样的输出。只要复制粘贴失败,
-c
确实在
/bin/bash
之后。这是旧版本的代码。这个解决方案很有效,但我不喜欢。我习惯于有一个真正的论点清单。此解决方案添加了一些层,以便稍后由bash再次删除。我对此仍有一些安全顾虑。但是,至少谢谢你,这在某种程度上有所帮助。@sivizius-你能举一个关于这个解决方案的“安全问题”的例子吗?
-- Usage example #1:
execute_commands(
   {"your/program", "arg 1", "$arg2", "arg-3", "~/arg4.txt"},
   {expand=true, "echo", "Your program finished with exit code $?"},
   {"ls", "-l"}
)
-- The following command will be executed:
-- /bin/bash -c 'your/program '\''arg 1'\'' '\''$arg2'\'' arg-3 ~/arg4.txt;echo "Your program finished with exit code $?";ls -l'
-- Usage example #2:
execute_commands{"run", "set title '$My Title$'", "arg1", "arg2"}
-- the generated command is not trivial, but it does exactly what you need :-)
-- /bin/bash -c 'run '\''set title '\''\'\'\''$My Title$'\''\'\'' arg1 arg2'