Web頁面執行shell命令


本文以apache為web服務器為例

  1. 安裝apache服務
    yum -y install httpd
  2. 啟動apache
    systemctl restart httpd
  3. 創建shell腳本
    cd /var/www/cgi-bin/
    vim shell
    #!/bin/sh  
    alias urldecode='sed "s@+@ @g;s@%@\\\\x@g" | xargs -0 printf "%b"'  
    echo -e "Content-type: text/plain\n"  
    decoded_str=`echo $QUERY_STRING | urldecode`  
    echo -e "`$decoded_str` \n"
    shell
    測試:在瀏覽器中輸入http://127.0.0.1/cgi-bin/shell?pwd,即可列出目錄
  4. 提供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?"+ 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或域名

  5. 效果圖如圖所示
  6. cgi-bin目錄執行shell腳本格式
    #!/bin/sh
    printf "Content-Type: text/plain\n\n"
    your_commands_here
  7. 安全性優化
    限制用戶訪問cgi-bin目錄,修改/etc/httpd/conf/httpd.conf
    <Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
    Deny From all
    Allow From 127.0.0.1 your-ip-address
    </Directory>
    配置http頁面賬號密碼訪問,也可實現安全性
  8. 弊端
    無法執行復雜的腳本命令,如帶有" |等特殊符號的命令無法執行,如yum、top命令執行結果不完整、僅適用於簡單帶輸出腳本命令,腳本運行賬號為apache


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM