基本上我想要实现的是通过php在java控制台中运行命令。我想这样做的方式可能太过分了,所以如果有更简单的方法,请告诉我。
所以我想到的是在php中使用exec()
或shell_exec()
来通过tmux会话输入命令。问题是apache在www-data上运行,并且该用户无法创建tmux会话(出于某种原因)。
在互联网上搜索太久后,我发现this。一种以root身份执行应用程序的方法。即使它是从另一个用户执行的。我试过这个,这显然有效,但现在我想通过参数从php运行命令。但由于注射,我不确定这是不安全的。毕竟它确实需要用户输入。或者,只要我在php中使用escapeshellarg()
或escapeshellcmd()
,我就不必担心这个问题吗?
提前感谢您的帮助:)
答案 0 :(得分:1)
在root(thru sudo)执行的脚本中,我将以下内容检查传递的参数。我怀疑你必须以相同的方式检查每行输入,然后检查每一行上的命令以确定是否要执行它。
如果您的服务器不是以root用户身份运行,而是以其他“常规”用户身份运行,那么会更安全。所以我同意这是一个坏主意,但如果我必须这样做,那么我会用它:
# Check to make sure the parameters do not have special characters
security_check () {
# echo does not execute the contents of "$@" provided it is inside
# double quotes.
SC_PARMS1="`echo \"$@\" | tr \"\\\`<>|\" \"xxxx\"`"
SC_PARMS2="`echo \"$@\" | tr \"\\\`<>|\" \"yyyy\"`"
# Can't use "$@" in if test as it executes the contents, hence we
# must compare 2 different converted strings.
if [ "$SC_PARMS1" != "$SC_PARMS2" ]; then
echo "`uname -n`: Security abort in: $0 $@"
return 1
fi
unset SC_PARMS1 SC_PARMS2
return 0
}
while read line ; do
security_check "$line"
if [ $? = 0 ]; then
echo "We could execute $line"
# Now we need to check the commands we allow
if [ "$line" = "ls" ]; then
# This test is very rudimentary, you might need to do a set -- $line and
# examine more than just the command ($1 at that point)
eval $line
fi
fi
done
以root身份运行任何类型的服务器都不是一个好主意。尝试查看root是否可以保留用于实际的超级用户要求,并使用服务帐户(即非root帐户)运行您的应用程序。
答案 1 :(得分:1)
是强>
(括号中的这一行只是为了满足最少30个字符的要求。)
答案 2 :(得分:0)
安全
如果它被正确转义。这使得声音比正确地逃避输入更容易,并确保其100%安全。
虽然您确定需要所有访问权限吗?如果攻击者以某种方式进行攻击,您应该限制攻击者可以执行的访问。拥有root权限比仅仅是用户帐户更糟糕