命令注入可能在shell脚本中而不使用eval?

我想知道,现在最新版本的sh,bash,ksh等是否可以通过执行这个(非常简单的)脚本来获得命令注入?

#!/bin/sh echo "What is the name of the program you are looking for?" read program locate $program 

尽pipe我们已经可以执行代码,如果他们有一个shell,我只是想知道如果一个variables可以包含像PHP这样的恶意代码:

 parameter=parameter;ls 

在这个问题中,shellshock(envvariables)也可以被忽略。

对的,这是可能的。 但是这并不像你提到的那么简单。 看下面的一些例子。

它不会工作:

 $ read -p "Type some text:" var1 Type some text:Example;hostname $ echo $var1 Example;hostname $ $var1 Example;hostname: command not found 

但是,如果你这样使用,是的,它会工作:

 $ read -p "Type some text:" var1 Type some text:hostname $ echo $var1 hostname $ $var1 SSBLZMVM1 

如果这样写的话,你永远不知道是否没有可以被欺骗的外壳实现。 然而,通过将定位参数放在引号中可以保证安全。 然后扩展参数将被视为一个单词:

 #!/bin/sh echo "What is the name of the program you are looking for?" read program locate "${program}"