|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。0 V4 G# @2 m! [/ Z" Y; g U
' X7 n1 b) @# d- {' c, s8 {
一.准备工作
! Z; f+ z4 }7 u1 |% [9 ?* u6 `
$ C2 ^( D. O+ [: q3 v系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
2 L2 w( m$ `1 R6 l2 s0 }- p; t0 @+ A8 _4 o# \7 [7 \
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
7 N# s) A) V4 H2 f* i) N ^* ?* J5 x
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
5 Y/ h6 N$ d' N0 I4 b
0 t4 I! ^# H4 K" g1 K- aOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
* q7 J% f1 q$ K+ s, }( E" \0 J1 M/ r5 Y0 a' m1 s
依赖关系:7 \" B2 z5 M) E2 ]: a+ Q
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:6 ^& O- f% E- \; ?
* {. G: Q8 J* |
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
$ H( e; h, `2 Mmodsecurty依赖的包:pcre httpd-devel libxml2 apr
6 u! F, M% k N' x5 Q& c# U) v$ O% C7 w, r
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel D( d$ t( A. j) B* h, ?
二.启用standalone模块并编译
w6 E3 V% Z! K/ C7 V" O
9 K3 ?' R1 n; d) r h+ w6 r下载modsecurity for nginx 解压,进入解压后目录执行:
# H% w3 N6 F7 C+ ^5 i2 C% H# A
9 ?0 W2 O1 R- x, g./autogen.sh
6 `. b7 F' Y2 w' a./configure --enable-standalone-module --disable-mlogc0 u/ i4 i/ r# i4 i5 _7 b2 Q
make " P1 ?- p- F4 d$ Y
三.nginx添加modsecurity模块- O3 p5 n8 l( z$ L* j
' w# m* Z; X6 w; N; [$ w6 p
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
2 R/ R4 K# ?4 b) b$ I& `6 ~0 C1 o& \2 h' K, }# d3 h
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine( r% l& |+ u( S1 u# o, J/ e' X+ u
make && make install0 m+ A7 P) K5 m7 n ^5 o) j
四.添加规则0 M0 d8 W. }% ]' t5 p' e u
, Z0 h0 Z$ d0 z7 ]modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。& p* Y$ K$ K5 x1 m6 V
5 j4 n# r7 Z0 ]. A3 R
1.下载OWASP规则:5 t0 i" u; w( B/ k
- E- S6 D; t+ L! R% r2 pgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs9 X7 n. o; I6 R2 x
% u( U/ {. K/ i, b( t& U7 p% M8 {' Wmv owasp-modsecurity-crs /opt/tengine/conf/5 j2 v+ a2 @) \
' S1 j$ J, U9 J6 x; r/ f
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
& c" q8 ]5 @3 m O/ E2.启用OWASP规则:" d( W+ n! k' V' g0 Q- o& L
" v$ L' {9 e2 S0 w2 a* l4 J复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
" W' ^$ W" I0 E* d8 o8 Y: d1 Q4 y5 |0 q/ a) Z+ x
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
2 _! V x! e; {8 R8 @5 R! v
6 u' U3 Z7 u! [% ?0 x: Mowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。# X5 Q: J D" _6 H$ w5 q2 y6 m- `) N
' Y( K" |' y2 G/ dInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
2 n/ p; M" l6 C3 h0 Z* VInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf$ g# G2 z$ [5 H6 i$ e9 g
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
. k# G- I& I. H' X) X& xInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
; `- Q, N2 F- |Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf7 s. V) N! k: _+ K' ?* v1 N
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf3 e9 }: a& _! ^
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
" s5 X$ R" C" l E$ A- M* e2 e五.配置nginx$ {, ?/ F% t- R
9 _" C9 }. ^% l9 V$ L" c, _( S在需要启用modsecurity的主机的location下面加入下面两行即可:% W, R# i6 P( a; n$ e- k, ?. W
6 [, B; C3 G. O" F u! h
ModSecurityEnabled on;
" L0 d1 j8 ~6 x, \9 PModSecurityConfig modsecurity.conf;
5 ~4 a, [& y* g* v下面是两个示例配置,php虚拟主机:
* }0 |, k6 `3 O2 x/ Z
7 N) Q u9 ^. q2 S% ]server {( I+ k: ]% Z' f3 s6 ?) B" K
listen 80;
k8 j& ] H. e/ N" f server_name 52os.net www.52os.net;
z6 j8 O7 t P6 h) n: t * G) J5 u8 j/ n/ N, Z% g
location ~ \.php$ {
3 t. m; j, X3 s# a, G7 s ModSecurityEnabled on;
' v; U) u$ o+ f$ m' k ModSecurityConfig modsecurity.conf;
" r1 f0 y) D: ^" D4 a) {) |5 l- o8 X
root /web/wordpress;
4 ~* w2 P! Q% C8 O. T index index.php index.html index.htm;6 ?3 \# k. }1 u( S& _
( l" P) U3 }$ t; O& f9 @ fastcgi_pass 127.0.0.1:9000;
; H' F5 v0 z$ J X+ q fastcgi_index index.php;3 p" o x% P2 Y
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;/ s f2 R A( X, h2 o9 K3 ]. H& k. _
include fastcgi_params;. |9 `& j- K) R. r1 q/ t
}% M4 M% a7 c" s0 e: C% i
}
; ~5 i8 U; s& Jupstream负载均衡:
6 p, e' M' \! S) H1 {7 A* i1 c- m
" B5 J, Z( j# O9 |5 T7 G- Bupstream 52os.net {
, T: Q$ y0 ]' @" \* A% e server 192.168.1.100:8080;; l$ {6 V& `1 s/ i% g5 }8 p
server 192.168.1.101:8080 backup;
& G# Z: `6 l) R6 t1 U+ z}
! t5 F9 K! \# z6 k& d
) ?) |# P. I. t) R- n" z& gserver {
4 m$ F: d/ I3 _1 Elisten 80;
, L k" {$ {. U. e( [2 N9 sserver_name 52os.net www.52os.net;& Z! D$ ]5 c' @6 a% \% G4 k& r
0 o1 b$ g7 M& w- ^5 u
location / {
% h: l* k3 S9 v* H ModSecurityEnabled on; 2 ]% W8 h) Q# S' Z$ ?
ModSecurityConfig modsecurity.conf; 3 R% n2 F# Q3 j2 p; W. x
2 S+ b; F, k8 R- H proxy_pass http://online;
+ \$ v" V7 f: `* K, m proxy_redirect off;
* a# |) x- J% t proxy_set_header Host $host;2 \9 w; [0 K9 M
proxy_set_header X-Real-IP $remote_addr;) O# X/ u' D8 ^. I' p( ?; h
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
% E# j4 i3 z* v. a. E, u }$ R* }3 `- H3 R$ `/ B
}4 a" ^; ?4 T$ u: R. T, X
六.测试6 b- t+ U4 j- N1 s4 [
$ M1 z1 I* \8 z0 {" r我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
0 f1 ]* W' y3 i) l1 M
/ D/ }% b" L8 P6 K. X4 n' F; P5 E4 K<?php' Z: c2 t5 z8 N- ?0 f: n4 b
phpinfo(); , K. L0 R) s! B, \0 ^" |
?>8 l. i) C4 }0 C6 ^
在浏览器中访问:
4 L D; `0 P% [3 [9 s" M
+ o: ~1 m; K7 q4 J9 m. h2 X! k: chttp://www.52os.net/phpinfo.php?id=1 正常显示。
3 z$ p0 v4 E% H! G. B: Ehttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
7 J7 \. @( h& y# e xhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。$ Q* I9 L [" `/ D! x
说明sql注入和xss已经被过滤了( A4 [5 }. G2 i- c `
) P; z9 }2 W3 p七、安装过程中排错9 n5 C# x5 U- c" U" g+ M, ^
9 q8 m: V# S1 Z1.缺少APXS会报错
$ A6 B! \5 v' C; B
& P" c R+ n5 n) w4 b" w" O' U5 |configure: looking for Apache module support via DSO through APXS2 |" R9 C |! H: W- i2 H) X) U# j
configure: error: couldn't find APXS, i% k8 o6 Y) q$ D% e8 A+ M
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。- F' e; s7 U, l7 e
解决方法:8 e* N& }9 F, V1 v# M) R
1 I) t6 r6 x% z- a+ }8 Z0 T6 syum install httpd-devel
6 M, R- u# \& P' v9 E2.没有pcre
H+ ?: N0 S6 ]* b! ~' _3 X& m2 Y9 N- e z5 W
configure: *** pcre library not found.
% |# X3 X: x7 T4 j R2 W' Bconfigure: error: pcre library is required7 H7 u: J: \* I2 ~
解决方法:* L3 h( a5 y& b. g
; c( x0 i( p: Z. v% K9 eyum install pcre pcre-devel
; S# @4 ~+ ]/ C: k; Z/ O" |6 q7 V3.没有libxml2
( M( q, @+ v# o; Z- v4 W0 W. I& p. {0 [3 N- [
. m) N) k" r. ~
configure: *** xml library not found.. n) ]; q# O- W% j
configure: error: libxml2 is required4 q, o5 a" k5 r5 q/ x
解决方法:
5 k1 w3 O3 V6 q8 V
- l6 Y. c, f( P, m# Fyum install libxml2 libxml2-devel
- S$ m4 U4 }0 k4.执行 /opt/tengine/sbin/nginx -m 时有警告
! U( S, C( y$ F4 c3 w/ X5 e8 ~: k3 @0 h" a
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
" I I$ ~) [6 B- R I9 Q7 qnginx: [warn] ModSecurity: Loaded APR do not match with compiled!' Q) d0 @* B# K5 C
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log/ t& j2 T; L$ ?' m
7 c( W4 Z7 G5 j' l/ i1 O% {6 d
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
+ B2 ]4 f, M: z: q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
/ A5 s. S8 u0 X9 l: E" l. s8 f9 r2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!1 s4 j' m! J8 H0 J
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"1 u( F* K5 [9 u% |/ L
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
* @' _) }: F) l2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.9 G1 I( C& K: Z* H& M
解决方法,移除低版本的APR (1.3.9)! _5 \8 c6 @) `" H/ b1 O" R
; S/ R8 ]; ]4 a+ g2 xyum remove apr2 E$ u; T5 @0 I; ]
5.Error.log中有: Audit log: Failed to lock global mutex4 ^+ g. q. u4 [
1 G4 y! |; c9 w2 x2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
1 E& L# _: E+ N" s4 ~6 l$ _8 gglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
/ ]3 k" J- {) k解决方法:
' W2 Z2 Q8 {( p7 F编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
! Q9 G j- h3 @9 v( ?. z. h0 `1 r6 Z6 z0 [6 E
SecAuditLogDirMode 0777
( g" |& d. j& I% B* dSecAuditLogFileMode 0550
) \- y+ Q. ^7 }# sSecAuditLogStorageDir /var/log/modsecurity
* J5 t. E3 j2 @7 Y7 kSecAuditLogType Concurrent* F5 G! Z- H; K% {* w5 X4 V8 B1 e
参考文章:
+ i/ c+ N. i7 f0 L3 m. u0 m& B: {https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
! k# A% Z' h5 Q' j1 j" W0 Phttp://drops.wooyun.org/tips/2614 |
|