`
haoningabc
  • 浏览: 1469710 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

通过nginx远程执行shell

阅读更多
saltstack远程执行shell,远程管理等返回json已经很成熟,扩展也很好用
原理其实就是通过网络协议,aes加密后,用python本地执行shell等系统调用

其实nginx也可以做成远程执行shell
利用nginx的request的args参数可以获取url
并自定义解析url的decode方法,比如%20变成空格
方法如下
void hao_urldecode(char *dest, const char *src)  
{  
    const char *p = src;  
    char code[3] = {0};  
    unsigned long ascii = 0;  
    char *end = NULL;  
  
    while(*p)  
    {     
        if(*p == '%')  
        {     
            memcpy(code, ++p, 2);   
            ascii = strtoul(code, &end, 16);  
            *dest++ = (char)ascii;  
            p += 2;  
        }     
        else  
            *dest++ = *p++;  
    }     
}  

nginx的args后面会跟着一个空格 HTTP/1.1
所以截取字符串把url还原成命令,使用system函数调用执行shell
    char urlcmd[255]= {"0"};                                                                                                                                 
    //    strcpy( urlcmd, r->args.data);
    printf("mycmd %s",urlcmd);
    char *mycmd=(char *)r->args.data;
    strcpy( urlcmd, mycmd);//snprintf
    char *abc;
    abc=strtok(urlcmd," ");
    char haoout[sizeof abc] = {0};
       hao_urldecode(haoout,abc);
    system(haoout)



完整代码的
简单例子如下:
ngx_http_echo_module.c
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <time.h>
#include "haolog.h"

#include <ngx_log.h>
/* Module config */
typedef struct {
        ngx_str_t  ed;
} ngx_http_echo_loc_conf_t;
static char *ngx_http_echo(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static void *ngx_http_echo_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_echo_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
/* Directives */
static ngx_command_t  ngx_http_echo_commands[] = {
        { 
                ngx_string("echo"),
                NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
                ngx_http_echo,
                NGX_HTTP_LOC_CONF_OFFSET,
                offsetof(ngx_http_echo_loc_conf_t, ed),
                NULL 
        },
        ngx_null_command
};
/* Http context of the module */
static ngx_http_module_t  ngx_http_echo_module_ctx = {
        NULL,                                  /* preconfiguration */
        NULL,                                  /* postconfiguration */
        NULL,                                  /* create main configuration */
        NULL,                                  /* init main configuration */
        NULL,                                  /* create server configuration */
        NULL,                                  /* merge server configuration */
        ngx_http_echo_create_loc_conf,         /* create location configration */
        ngx_http_echo_merge_loc_conf           /* merge location configration */
};
/* Module */
ngx_module_t  ngx_http_echo_module = {
        NGX_MODULE_V1,
        &ngx_http_echo_module_ctx,             /* module context */
        ngx_http_echo_commands,                /* module directives */
        NGX_HTTP_MODULE,                       /* module type */
        NULL,                                  /* init master */
        NULL,                                  /* init module */
        NULL,                                  /* init process */
        NULL,                                  /* init thread */
        NULL,                                  /* exit thread */
        NULL,                                  /* exit process */
        NULL,                                  /* exit master */
        NGX_MODULE_V1_PADDING
};
void hao_urldecode(char *dest, const char *src)
{
    const char *p = src;
    char code[3] = {0};
    unsigned long ascii = 0;
    char *end = NULL;

    while(*p)
    {   
        if(*p == '%')
        {   
            memcpy(code, ++p, 2); 
            ascii = strtoul(code, &end, 16);
            *dest++ = (char)ascii;
            p += 2;
        }   
        else
            *dest++ = *p++;
    }   
}
/* Handler function */
        static ngx_int_t
ngx_http_echo_handler(ngx_http_request_t *r)
{
        DEBUG_LOG("haoning haohao .........ngx_http_echo_handler\n");
        ngx_log_stderr(0,"haoning: ngx_http_hello_world_handler\"%s\"","haohao" );
//      ngx_log_error(0,"haoning: ngx_http_hello_world_handler\"%s\"","haohao" );
        fprintf(stderr, "haoning hahahah:%s\r\n","ningning");
        fprintf(stderr,"haoning haohao subrequest in memory: %d\n", (int) r->subrequest_in_memory);
        fprintf(stderr,"haoning haohao  r->method : %d\n",(int) r->method);
        fprintf(stderr,"haoning haohao r->http_version: %d\n",(int) r->http_version) ;
        fprintf(stderr,"haoning haohao r->request_line.data: %s\n",r->request_line.data) ;
        fprintf(stderr,"haoning haohao r->uri.data): %s\n",r->uri.data);
        fprintf(stderr,"haoning haohao r->args.data: %s\n",r->args.data);

    //u_char *mycmd=(u_char *)r->args.data;                                                                                                                              
    //char *urlcmd=(char *) malloc( sizeof(char)*255 );//=(u_char *)r->args.data;                                                                                                                              
    //memset(urlcmd,0,sizeof(char)*255);
    char urlcmd[255]= {"0"};                                                                                                                             
//    strcpy( urlcmd, r->args.data);
    printf("mycmd %s",urlcmd);
    char *mycmd=(char *)r->args.data;
    strcpy( urlcmd, mycmd);//snprintf
    char *abc;
    abc=strtok(urlcmd," ");
    char haoout[sizeof abc] = {0};
       hao_urldecode(haoout,abc);
   // u_char *mycmd=(u_char *) malloc( sizeof(char)*255 );;                                                                                                                            
    //u_char *thiscmd=(u_char *) malloc( sizeof(u_char)*255 );
    //ngx_unescape_uri(&thiscmd, &uu, 255, NGX_UNESCAPE_REDIRECT);
    fprintf(stderr,"haoning haohao urlcmd:%s\n",urlcmd);
    fprintf(stderr,"haoning haohao abc:%s\n",abc);
    fprintf(stderr,"haoning haohao out:%s\n",haoout);
    system(haoout);
    //fprintf(stderr,"haoning haohao thiscmd:%s\n",thiscmd);
    //free(urlcmd);
   // free(mycmd);

        fprintf(stderr,"haoning haohao r->unparsed_uri.data: %s\n",r->unparsed_uri.data);
        fprintf(stderr,"haoning haohao r->method_name.data: %s\n",r->method_name.data)  ;
        fprintf(stderr,"haoning haohao r->http_protocol.data: %s\n",r->http_protocol.data);
        fprintf(stderr,"haoning haohao r->exten.data: %s\n",r->exten.data);
        fprintf(stderr,"haoning haohao -----------------: %s\n","end");
        ngx_int_t rc;
        ngx_buf_t *b;
        ngx_chain_t out;
        ngx_http_echo_loc_conf_t *elcf;
        elcf = ngx_http_get_module_loc_conf(r, ngx_http_echo_module);
        if(!(r->method & (NGX_HTTP_HEAD|NGX_HTTP_GET|NGX_HTTP_POST)))
        {
                return NGX_HTTP_NOT_ALLOWED;
        }
        r->headers_out.content_type.len = sizeof("text/html") - 1;
        r->headers_out.content_type.data = (u_char *) "text/html";
        r->headers_out.status = NGX_HTTP_OK;
        r->headers_out.content_length_n = elcf->ed.len;
        if(r->method == NGX_HTTP_HEAD)
        {
                DEBUG_LOG("haoning......ngx_http_echo_handlerr---r->method == NGX_HTTP_HEAD");
                rc = ngx_http_send_header(r);
                if(rc != NGX_OK)
                {
                        return rc;
                }
        }
        b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
        if(b == NULL)
        {
                ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate response buffer.");
                return NGX_HTTP_INTERNAL_SERVER_ERROR;
        }
        out.buf = b;
        out.next = NULL;
        b->pos = elcf->ed.data;
        b->last = elcf->ed.data + (elcf->ed.len);
        b->memory = 1;
        b->last_buf = 1;
        rc = ngx_http_send_header(r);
        if(rc != NGX_OK)
        {
                return rc;
        }
        DEBUG_LOG("haoning......ngx_http_output_filter");
        return ngx_http_output_filter(r, &out);
}
        static char *
ngx_http_echo(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
        DEBUG_LOG("haoning --ngx_http_echo->>>>> init");
        ngx_http_core_loc_conf_t  *clcf;
        clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
        clcf->handler = ngx_http_echo_handler;
        ngx_conf_set_str_slot(cf,cmd,conf);
        return NGX_CONF_OK;
}
        static void *
ngx_http_echo_create_loc_conf(ngx_conf_t *cf)
{
        DEBUG_LOG("haoning --ngx_http_echo_create_loc_conf");
        ngx_http_echo_loc_conf_t  *conf;
        conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_echo_loc_conf_t));
        if (conf == NULL) {
                return NGX_CONF_ERROR;
        }
        conf->ed.len = 0;
        conf->ed.data = NULL;
        return conf;
}
        static char *
ngx_http_echo_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
        DEBUG_LOG("haoning --ngx_http_echo_merge_loc_conf");
        ngx_http_echo_loc_conf_t *prev = parent;
        ngx_http_echo_loc_conf_t *conf = child;
        ngx_conf_merge_str_value(conf->ed, prev->ed, "");
        return NGX_CONF_OK;
}

config
ngx_addon_name=ngx_http_echo_module
HTTP_MODULES="$HTTP_MODULES ngx_http_echo_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_echo_module.c $ngx_addon_dir/haolog.c"
NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/haolog.h"
CORE_LIBS="$CORE_LIBS -lpcre"


配置文件注意
user  nobody
改成
user  root;

#user  nobody;
user  root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        location /test {
            echo   "hello haohao";
        }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}


以上模块编译到nginx后
访问
http://10.11.11.11/test?touch%20a
tailf error.log


去/usr/local/nginx_echo/sbin
ls

已经在sbin/nginx的同级目录执行了
touch a的命令

另外:
这里使用nginx的root权限,其他安全性因素没有考虑,只做学习参考
  • 大小: 129.3 KB
  • 大小: 21.5 KB
分享到:
评论

相关推荐

    通过PHP来远程执行shell脚本工具 整个程序只有两个文件,一个PHP文件,一个shell安装脚本,易于使用和安装

    一个通过PHP来远程执行shell脚本工具。整个程序只有两个文件,一个PHP文件,一个shell安装脚本,易于使用和安装。支持PHP5.2+以上的版本。提供的功能包括: 1:用户登录注销。 2:记录每次使用的IP,方便审计。 3...

    boyurl 远程执行shell脚本工具

    在IT行业中,有时我们需要远程管理服务器或执行一些系统级的任务,而Boyurl就是这样一个工具,它允许我们通过PHP来远程执行shell脚本,极大地提高了运维效率。这个小巧的工具包含两个主要文件,一个PHP文件(boyurl....

    web应用执行shell操作服务器资源

    在IT领域,Web应用执行Shell操作服务器资源是一个关键的话题,特别是在系统管理和远程维护中。这样的功能使得管理员能够通过Web界面方便地控制服务器,而无需直接登录到服务器终端。以下是对这个主题的详细解释: 1...

    统计Nginx日志里前一个小时的IP数量以及IOS占比

    理解这个过程不仅有助于你执行类似的任务,还能让你深入理解shell脚本编程、日志分析以及Nginx日志格式。对于系统管理员和开发者来说,具备这些技能可以更好地监控和优化服务器性能,提高故障排查效率。

    基于Linux系统Nginx的动态网站的LNMP环境源码包

    了解shell命令行的基本用法,如ls、cd、mkdir、rm等,以及使用vi或nano编辑文本文件,是Linux运维的基本功。 接下来,我们关注Nginx。Nginx以其事件驱动的非阻塞I/O模型,能够在处理大量并发连接时保持低资源消耗。...

    常用shell 脚本,dos攻击防范,

    批量主机远程执行命令脚本.sh 批量创建100用户并设置密码脚本.sh 批量检测网站是否异常脚本.sh 找出占用CPU 内存过高的进程脚本.sh 更多精品教程.url 服务器系统配置初始化脚本.sh 本教程由我爱学it提供.url ...

    Shell 脚本编程实战100例.pdf

    首先,Shell脚本编程是Linux系统管理员必备的技能之一,它允许自动化地执行许多日常任务。Shell脚本可以通过组合常用的命令行工具,比如rsync、find、sed、awk等,来实现复杂的数据处理和自动化管理。本书通过100个...

    shell编程之从零到壹--大神成就之路

    简单脚本编写关注命令罗列、变量化、函数化和远程执行等方面,而大型脚本编写则包括功能框架搭建、命令填充、日志功能、锁文件功能、流程知识点填充以及输入参数安全优化等。 13. 运维自动化 运维自动化是通过自动...

    生产环境日常shell工作运维脚本工作直接复制不用修改直接运行

    9. **批量主机远程执行命令脚本.sh**:批量执行命令在多服务器环境中非常有用,可能是通过SSH并行执行相同命令,实现对所有服务器的统一管理。 这些Shell脚本集合不仅展示了Shell的强大功能,也体现了运维人员在...

    18个Linux Shell脚本经典案例视频.zip

    批量主机远程执行命令脚本 批量创建100用户并设置密码脚本 批量检测网站是否异常脚本 服务器系统配置初始化脚本 查看网卡实时流量脚本 监控100台服务器磁盘利用率脚本 监控MySQL主从同步状态是否异常脚本 目录文件...

    php7+nginx1.12+php7.1的环境搭建

    - 可以通过官方网站下载最新稳定版本的Nginx源码,或者直接在服务器上执行下载命令: ```shell cd /usr/local/src wget -c http://nginx.org/download/nginx-1.10.3.tar.gz ``` - **解压文件**: ```shell ...

    SSH Secure Shell Client

    总之,SSH Secure Shell Client是IT管理员和开发人员进行远程Linux服务器管理的重要工具,通过它,用户可以高效、安全地执行各种运维任务,无论是web应用的部署、系统的日常维护,还是故障排查,都能得心应手。...

    shell入门到进阶,脚本机动化

    例如,通过编写shell脚本,可以在多台服务器上统一执行`apt-get update`或`yum update`命令来更新系统,或者通过`dpkg -i`或`rpm -i`安装特定的软件包。 ### 自动化批量软件部署 对于LAMP(Linux + Apache + MySQL...

    GetShell的姿势总结1

    - **CMS或框架漏洞**:如ThinkPHP、Struts2等知名平台的远程执行漏洞。 - **中间件和架构漏洞**:Jboss、Weblogic等的应用层漏洞。 - **应用程序漏洞**:Redis、Zabbix等组件的远程执行漏洞。 - **命令注入**:在...

    使用shell脚本对Nginx日志进行切分的示例代码

    总结,通过使用shell脚本结合Nginx的日志管理功能,我们可以有效地对Nginx日志进行切分,确保日志数据的有序性和可维护性。定期处理日志文件有助于优化磁盘空间,同时方便对历史访问数据进行分析。对于大型网站或高...

    如何使用Go语言实现远程执行命令

    在Go语言中,远程执行命令通常涉及到SSH(Secure Shell)协议,这是一种用于安全网络服务,特别是远程登录的标准。通过SSH,我们可以将命令发送到远程服务器并获取执行结果,这对于管理和维护大量服务器集群非常有用...

    Ansible批量部署Nginx的示例代码

    3. Ansible的shell模块被用来执行目标主机上的Shell命令,即执行install_nginx.sh脚本。 4. 使用Ansible命令执行Playbook。首先进行语法检查(ansible-playbook nginx.yml --syntax-check),确保没有语法错误后,...

    shell100例.pdf

    4. **Apache访问日志分析**:通过读取Apache的日志文件,使用`awk`或者`grep`命令筛选出13:30到14:30间的访问记录,然后提取远程IP地址,进行统计。 5. **打印国际象棋棋盘**:利用`for`循环和嵌套结构,输出交替的...

    shell 常用脚本收集

    接着,脚本使用 kill 命令来 reload Nginx 服务,确保新的日志文件被使用。这个脚本可以帮助用户快速切割日志,提高日志管理的效率。 资源同步 第四个脚本是用来实现资源同步的。该脚本使用 rsync 命令来同步本地...

    解析php中如何直接执行SHELL

    在PHP中,可以使用一些内置的函数来执行操作系统命令,即所谓的"Shell命令...在某些情况下,如果服务器配置得当,还可以考虑使用SSH守护进程来安全地远程执行命令,这种方式通常比在PHP脚本中直接执行Shell命令更安全。

Global site tag (gtag.js) - Google Analytics