我有一个openwrt路由器,我用作本地网络服务器,我创建了一个网页,在我的3G调制解调器上拨打USSD,脚本如下:
<html>
<title>CHECK USSD</title>
<body>
<?php
if($_POST['send']){
$ussd=$_POST['ussd'];
exec('ussd.py '.$ussd,$out);
echo "Result: ".$out[0];
}
?>
<form action="" method="post">
USSD :<input type="text" autofocus name="ussd" size="14" value="">
<input name="send" type="submit" value="Send Ussd">
</form>
</body>
</html>
ussd.py
是我用来检查USSD的python脚本。问题是,当用户尝试在输入框中输入某种类型的脚本时,例如:$(echo "hacked" > /www/index.html)
或$(rm -fr /root/*)
这些脚本也会被执行。所以人们可以很容易地破解我的路由器。我该如何防止这种情况发生?
答案 0 :(得分:0)
您要查找的命令是escapeshellarg()
,以便将您的字符串视为单个参数。
$pattern = '/Some regex that matches your potential inputs/';
if (preg_match($pattern, $ussd)) {
exec('ussd.py '.escapeshellarg($ussd),$out);
} else {
//throw error / response to user
}
一般来说,直接从用户输入执行不被认为是安全的。因此,找到与潜在输入命令中的模式相对应的正则表达式,并确保它在继续之前匹配(您已指定硬编码命令不是一个选项)。