0
点赞
收藏
分享

微信扫一扫

WEB页面执行shell命令


1.安装apache服务

apt-get install apache2

2.创建shell脚本

cd /var/www/cgi-bin/
vim shell


#!/bin/sh
alias urldecode='sed "s@+@ @g;s@%@\\\\x@g" | xargs -0 printf "%b"'
echo "Content-Type: text/html;";
decoded_str=`echo $QUERY_STRING | urldecode`
echo
echo `$decoded_str`



WEB页面执行shell命令_运维


记得给执行权限,chmod a+x *

需要配置一下apache2

cd /etc/apache2/

取消如下注释



WEB页面执行shell命令_html_02


增加一个模块



WEB页面执行shell命令_Powered by 金山文档_03


确保这四项存在



WEB页面执行shell命令_Powered by 金山文档_04


测试:在浏览器中输入​​http://127.0.0.1/cgi-bin/shell.sh?pwd​​,即可列出目录



WEB页面执行shell命令_linux_05


3.提供web接口

cd /var/www/html
vim index.html
<html>
<head>
<script>
function httpGet(url)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", url, false); // false: wait respond
xmlHttp.send(null);
return xmlHttp.responseText;
}
function f()
{
var url = "http://127.0.0.1/cgi-bin/shell.sh?"+ document.getElementById('in').value;
document.getElementById('out').innerHTML = httpGet(url);
}
</script>
</head>
<body>
<span>command: </span>
<input id='in'></input>
<button onclick='f()'>send</button>
<br/>
<pre id='out'></pre>
</body>
</html>

index.html

注意修改代码中ip,更改为服务器ip或域名

4.效果图如图所示



WEB页面执行shell命令_html_06


5.cgi-bin目录执行shell脚本格式

#!/bin/sh
printf "Content-Type: text/plain\n\n"
your_commands_here

6.安全性优化

限制用户访问cgi-bin目录,修改/etc/httpd/conf/httpd.conf

AllowOverride None

Options None

Require all granted

Deny From all

Allow From 127.0.0.1 your-ip-address

配置http页面账号密码访问,也可实现安全性

7.弊端

无法执行复杂的脚本命令,如带有" |等特殊符号的命令无法执行,如yum、top命令执行结果不完整、仅适用于简单带输出脚本命令,脚本运行账号为apache

举报

相关推荐

0 条评论