nginx %00空字节可执行任意代码漏洞(PHP)

After publishing my previous blog post on PHP, nginx configuration, and potential arbitrary code execution, I came across a separate null-byte injection vulnerability in older versions of nginx (0.5.*, 0.6.*, 0.7 <= 0.7.65, 0.8 <= 0.8.37). By taking advantage of this vulnerability, an attacker can cause a server that uses PHP-FastCGI to execute any publicly accessible file on the server as PHP.

In vulnerable versions of nginx, null bytes are allowed in URIs by default (their presence is indicated via a variable named zero_in_uri defined in ngx_http_request.h). Individual modules have the ability to opt-out of handling URIs with null bytes. However, not all of them do; in particular, the FastCGI module does not.

The attack itself is simple: a malicious user who makes a request to http://example.com/file.ext%00.php causes file.ext to be parsed as PHP. If an attacker can control the contents of a file served up by nginx (ie: using an avatar upload form) the result is arbitrary code execution. This vulnerability can not be mitigated by nginx configuration settings like try_files or PHP configuration settings like cgi.fix_pathinfo: the only defense is to upgrade to a newer version of nginx or to explicitly block potentially malicious requests to directories containing user-controlled content.

 

XML/HTML代码
  1. # This location block will prevent an attacker from exploiting    
  2. # this vulnerability using files in the ‘uploads’ or ‘other_uploads’ directory    
  3. location ~ ^/(uploads|other_uploads)/.*.php$   
  4.  {      
  5.                deny all;   
  6.  }   

Although the affected versions of nginx are relatively old (0.7.66 was released June 7th, 2010, 0.8.38 was released May 24th 2010), no mention of the change appears in the release notes. As a result, administrators may be running vulnerable servers without realizing their risk. I discovered a couple places where vulnerable packages were being distributed:

  1. Ubuntu Lucid Lynx (Ubuntu’s current LTS offering) and Hardy Heron (via both the hardy and hardy-backports repositories) provided vulnerable versions of nginx via apt-get. The lucid and hardy packages have been updated: hardy-backports is awaiting approval. [1] [2]
  2. Fedora provides a vulnerable version in its EPEL-4 repository. At this time, an updated package has not been released.

I sent several emails to igor@sysoev.ru regarding the vulnerability. I sent the first on June 24th and I sent followups on July 4th, July 20th, and August 2nd. I received the following reply to my August 2nd email:

Thank you for report.

I do not consider this as nginx security issue since every application
should validate its input data, so nginx passed the data to application.
Also this is PHP installation issue where scripts and user uploaded
data are not separated. This issue was discussed several times on mailing
list.

At some point I’ve decided that zero byte in URI should not appear
in any encoding, operating system, etc., and just makes more problems
than helps. So I have remove zero byte test.

For anyone who’s curious, the changes can be found at r3528 from svn://svn.nginx.org. At that time, it appears trunk corresponded to nginx 0.8: r3599 merged r3528 into the nginx 0.7 branch. The corresponding commit message is "remove r->zero_in_uri." I’ve reproduced the output of svn diff below:

 

XML/HTML代码
  1. Index: src/http/ngx_http_request.h    
  2. ===================================================================    
  3. — src/http/ngx_http_request.h (revision 3527)    
  4. +++ src/http/ngx_http_request.h (revision 3528)    
  5. @@ -56,7 +56,7 @@     
  6. #define NGX_HTTP_PARSE_INVALID_HEADER      13      
  7. -#define NGX_HTTP_ZERO_IN_URI               1    
  8. +/* unused                                  1 */     
  9. #define NGX_HTTP_SUBREQUEST_IN_MEMORY      2     
  10. #define NGX_HTTP_SUBREQUEST_WAITED         4     
  11. #define NGX_HTTP_LOG_UNSAFE                8    
  12. @@ -435,9 +435,6 @@         
  13. /* URI with "+" */         
  14. unsigned                          plus_in_uri:1;      
  15. –    /* URI with "\0" or "%00" */    
  16. –    unsigned                          zero_in_uri:1;   
  17. –         
  18. unsigned                          invalid_header:1;           
  19. unsigned                          valid_location:1;   
  20.  Index: src/http/ngx_http_core_module.c    
  21. ===================================================================    
  22. — src/http/ngx_http_core_module.c (revision 3527)    
  23. +++ src/http/ngx_http_core_module.c (revision 3528)    
  24. @@ -1341,7 +1341,7 @@           
  25. /* no content handler was found */      
  26. –    if (r->uri.data[r->uri.len – 1] == ‘/’ && !r->zero_in_uri) {    
  27. +    if (r->uri.data[r->uri.len – 1] == ‘/’) {              
  28.  if (ngx_http_map_uri_to_path(r, &path, &root, 0) != NULL) {                 
  29. ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,    
  30. @@ -2104,7 +2104,6 @@         
  31. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,                        
  32. "http subrequest \"%V?%V\"", uri, &sr->args);     
  33.  –    sr->zero_in_uri = (flags & NGX_HTTP_ZERO_IN_URI) != 0;        
  34.  sr->subrequest_in_memory = (flags & NGX_HTTP_SUBREQUEST_IN_MEMORY) != 0;      sr->waited = (flags & NGX_HTTP_SUBREQUEST_WAITED) != 0;     
  35.  Index: src/http/ngx_http_special_response.c    
  36. ===================================================================   
  37.  — src/http/ngx_http_special_response.c    (revision 3527)    
  38. +++ src/http/ngx_http_special_response.c    (revision 3528)    
  39. @@ -517,8 +517,6 @@           
  40. r->err_status = overwrite;      
  41. –    r->zero_in_uri = 0;   
  42.  –      if (ngx_http_complex_value(r, &err_page->value, &uri) != NGX_OK) {             
  43. return NGX_ERROR;         
  44. }   
  45.  Index: src/http/ngx_http_upstream.c    
  46. ===================================================================    
  47. — src/http/ngx_http_upstream.c    (revision 3527)   
  48.  +++ src/http/ngx_http_upstream.c    (revision 3528)    
  49. @@ -1815,10 +1815,6 @@                
  50.  return NGX_DONE;          
  51.    }    
  52.   –        if (flags & NGX_HTTP_ZERO_IN_URI) {    
  53. –            r->zero_in_uri = 1; –        }   
  54.  –   
  55.           if (r->method != NGX_HTTP_HEAD) {               
  56.   r->method = NGX_HTTP_GET;         
  57.     }    
  58. Index: src/http/ngx_http_parse.c    
  59. ===================================================================    
  60. — src/http/ngx_http_parse.c   (revision 3527)    
  61. +++ src/http/ngx_http_parse.c   (revision 3528)    
  62. @@ -438,8 +438,7 @@                     
  63. r->plus_in_uri = 1;                    
  64.  break;            
  65.      case ‘\0’: –        
  66.            r->zero_in_uri = 1; –            
  67.        break; +            
  68.        return NGX_HTTP_PARSE_INVALID_REQUEST;        
  69.          default:   
  70.                   state = sw_check_uri;   
  71.                   break;   
  72.  @@ -496,8 +495,7 @@   
  73.                   r->plus_in_uri = 1;   
  74.                   break;   
  75.               case ‘\0’:    
  76. –                r->zero_in_uri = 1;    
  77. –                break;   
  78.  +                return NGX_HTTP_PARSE_INVALID_REQUEST;    
  79.              }   
  80.               break;   
  81.    @@ -526,8 +524,7 @@   
  82.                   r->complex_uri = 1;   
  83.                   break;   
  84.               case ‘\0’:   
  85.  –                r->zero_in_uri = 1;   
  86.  –                break;    
  87. +                return NGX_HTTP_PARSE_INVALID_REQUEST;      
  88.            }   
  89.               break;   
  90.    @@ -1202,7 +1199,7 @@   
  91.                       ch = *p++;    
  92.                    } else if (ch == ‘\0’) {   
  93. –                    r->zero_in_uri = 1;    
  94. +                    return NGX_HTTP_PARSE_INVALID_REQUEST;     
  95.                 }   
  96.                     state = quoted_state;    
  97. @@ -1304,8 +1301,7 @@   
  98.           }   
  99.             if (ch == ‘\0’) {   
  100. –            *flags |= NGX_HTTP_ZERO_IN_URI;   
  101.  –            continue;    
  102. +            goto unsafe;   
  103.           }   
  104.             if (ngx_path_separator(ch) && len > 2) {    
  105. @@ -1449,34 +1445,19 @@   
  106.   void     
  107. ngx_http_split_args(ngx_http_request_t *r, ngx_str_t *uri, ngx_str_t *args)    
  108.  {   
  109.  –    u_char  ch, *p, *last;    
  110. +    u_char  *p, *last;     
  111.  –    p = uri>data;   
  112.  +    last = uri>data + uri->len;   
  113.    –    last = p + uri->len;   
  114.  +    p = ngx_strlchr(uri->data, last, ‘?’);    
  115.   –    args->len = 0;   
  116.  +    if (p) {    
  117. +        uri->len = p – uri->data;    
  118. +        p++;   
  119.  +        args->len = last – p;   
  120.  +        args->data = p;    
  121.   –    while (p < last) { – –           
  122. ch = *p++;   
  123.  –   
  124.  –        if (ch == ‘?’) { –            args->len = last – p;   
  125.  –            args->data = p;    
  126. –   
  127.  –            uri->len = p – 1 – uri->data;    
  128. –    
  129. –            if (ngx_strlchr(p, last, ‘\0’) != NULL) {    
  130. –                r->zero_in_uri = 1; –               
  131. }    
  132. –    
  133. –            return;    
  134. –        }    
  135. –    
  136. –        if (ch == ‘\0’) {    
  137. –            r->zero_in_uri = 1;    
  138. –            continue; –        }    
  139. +    } else {    
  140. +        args->len = 0;   
  141.       }    
  142.  }    
  143. Index: src/http/modules/ngx_http_gzip_static_module.c    
  144. ===================================================================    
  145. — src/http/modules/ngx_http_gzip_static_module.c  (revision 3527)    
  146. +++ src/http/modules/ngx_http_gzip_static_module.c  (revision 3528)    
  147. @@ -89,10 +89,6 @@   
  148.           return NGX_DECLINED;   
  149.       }   
  150.    –    if (r->zero_in_uri) {   
  151.  –        return NGX_DECLINED;    
  152. –    }   
  153.  –         
  154. gzcf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_static_module);    
  155.        if (!gzcf->enable) {    
  156. Index: src/http/modules/ngx_http_index_module.c    
  157. ===================================================================    
  158. — src/http/modules/ngx_http_index_module.c    (revision 3527)    
  159. +++ src/http/modules/ngx_http_index_module.c    (revision 3528)    
  160. @@ -116,10 +116,6 @@   
  161.           return NGX_DECLINED;   
  162.       }   
  163.    –    if (r->zero_in_uri) {    
  164. –        return NGX_DECLINED;    
  165. –    }    
  166. –      ilcf = ngx_http_get_module_loc_conf(r, ngx_http_index_module);   
  167.       clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);   
  168.    Index: src/http/modules/ngx_http_random_index_module.c    
  169. ===================================================================    
  170. — src/http/modules/ngx_http_random_index_module.c (revision 3527)    
  171. +++ src/http/modules/ngx_http_random_index_module.c (revision 3528)    
  172. @@ -86,10 +86,6 @@   
  173.           return NGX_DECLINED;   
  174.       }      
  175. –    if (r->zero_in_uri) {   
  176.  –        return NGX_DECLINED;    
  177. –    }    
  178. –      if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {          return NGX_DECLINED;   
  179.       }   
  180.  Index: src/http/modules/ngx_http_dav_module.c    
  181. ===================================================================    
  182. — src/http/modules/ngx_http_dav_module.c  (revision 3527)    
  183. +++ src/http/modules/ngx_http_dav_module.c  (revision 3528)    
  184. @@ -146,10 +146,6 @@         
  185. ngx_int_t   
  186.                  rc;   
  187.       ngx_http_dav_loc_conf_t  *dlcf;   
  188.    –    if (r->zero_in_uri) {   
  189.  –        return NGX_DECLINED;   
  190.  –       
  191. }    
  192. –      dlcf = ngx_http_get_module_loc_conf(r, ngx_http_dav_module);   
  193.         if (!(r->method & dlcf->methods))    
  194. {    
  195. Index: src/http/modules/ngx_http_flv_module.c    
  196. ===================================================================    
  197. — src/http/modules/ngx_http_flv_module.c  (revision 3527)    
  198. +++ src/http/modules/ngx_http_flv_module.c  (revision 3528)   
  199.  @@ -80,10 +80,6 @@   
  200.           return NGX_DECLINED;   
  201.       }    
  202.   –    if (r->zero_in_uri) {    
  203. –        return NGX_DECLINED;   
  204.  –    }    
  205. –      rc = ngx_http_discard_request_body(r);    
  206.        if (rc != NGX_OK) {    
  207. Index: src/http/modules/ngx_http_static_module.c    
  208. ===================================================================    
  209. — src/http/modules/ngx_http_static_module.c   (revision 3527)   
  210.  +++ src/http/modules/ngx_http_static_module.c   (revision 3528)    
  211. @@ -66,10 +66,6 @@   
  212.           return NGX_DECLINED;    
  213.      }   
  214.    –    if (r->zero_in_uri) {   
  215.  –        return NGX_DECLINED;   
  216.  –    } –   
  217.       log = r>connection->log;   
  218.         /*   
  219.  Index: src/http/modules/ngx_http_autoindex_module.c    
  220. ===================================================================    
  221. — src/http/modules/ngx_http_autoindex_module.c    (revision 3527)    
  222. +++ src/http/modules/ngx_http_autoindex_module.c    (revision 3528)   
  223.  @@ -160,10 +160,6 @@   
  224.           return NGX_DECLINED;    
  225.      }     
  226.  –    if (r->zero_in_uri) {    
  227. –        return NGX_DECLINED;    
  228. –    }    
  229. –      if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {   
  230.           return NGX_DECLINED;    
  231.      }   
  232.  Index: src/http/modules/perl/ngx_http_perl_module.c    
  233. ===================================================================    
  234. — src/http/modules/perl/ngx_http_perl_module.c    (revision 3527)    
  235. +++ src/http/modules/perl/ngx_http_perl_module.c    (revision 3528)    
  236. @@ -168,10 +168,6 @@    
  237.  static ngx_int_t     
  238. ngx_http_perl_handler(ngx_http_request_t *r)     
  239. {    
  240. –    if (r->zero_in_uri) { –        return NGX_HTTP_NOT_FOUND; –    } –      r->main->count++;        
  241.    ngx_http_perl_handle_request(r);    
  242.   
发表评论?

0 条评论。

发表评论