|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
$ Q8 Z; M) V7 Q5 w
' U( q$ x# L% G, M一.准备工作
) S+ x9 E, H c+ S6 s7 h# U9 G) f: L. l+ b
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.00 ]1 d) c3 @+ f, v
2 G8 Y1 {0 T; m7 w2 T- Y3 y( ptengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
1 U# F- g1 F. ~5 ~; k3 T" f0 I4 j8 ~( g6 I6 C
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
. `; G) `( K4 d( z; M( F
8 L$ @2 I; H q* F0 e. fOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
7 k6 S4 Z( O) u
7 r$ _& Q3 |" [' ?3 }8 B依赖关系:
0 ]7 O1 T3 N' I: G! g' a$ otengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有: d2 o/ _ t; m. `
, l8 C) |( \9 v/ J0 \
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel& P# N- Y f9 q' u0 h7 y& J2 v* n
modsecurty依赖的包:pcre httpd-devel libxml2 apr$ h# M: m2 _: p% E+ ?
3 c9 u' g" {& `( Jyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
+ z3 W# `+ K( V二.启用standalone模块并编译
/ t5 H1 v v* I9 K0 p, s$ z! R: P* I
下载modsecurity for nginx 解压,进入解压后目录执行:# h0 _+ W; N' Q: L
N$ o* I3 k8 Q& W/ c6 t+ v./autogen.sh3 y1 u X' ^+ Y6 |
./configure --enable-standalone-module --disable-mlogc
' {( x5 F( C) o1 X2 ~6 }make 5 G8 g1 k# {( u4 r V
三.nginx添加modsecurity模块& T1 R! Z) P8 N$ x. h8 c
4 w# L: K+ e k# ~
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
8 [2 [' G) n1 j3 p* L0 E. w# l9 Y& a' _
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine! F! |" o: {9 ^5 U( ]7 e3 B, Y
make && make install
& l+ J* Y* G2 T' Z K* a, X四.添加规则" T) _0 l& m( A1 q6 ^+ s% G( r7 E
( v1 O- U E$ ], Z% g
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
2 C; y* V6 F- w: s) E5 ?6 R: E" F& O
) D( B& b% ?* K1 O9 @1.下载OWASP规则:7 g) u+ l4 m2 X4 _; r
( _+ e9 G: ^, v9 P
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
) h0 t2 }& O* |% P% @
9 F K, o) b Lmv owasp-modsecurity-crs /opt/tengine/conf/
; W \# u- i2 g! g+ v5 b- [5 r3 r+ ^( V d& Q$ o
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf5 ]8 A5 R* k( q; I) {1 Q: Z% r8 c
2.启用OWASP规则:
$ K g* d$ O8 f3 W% `* o) }' m+ q" {: v7 S6 V
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。 ~8 J" a, b0 N) X0 J; [$ ^5 L
; Y( u: [1 ]; ~5 ?1 ] p
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
, k! D W, w* {, h! p1 [% H, d K) o( q6 r' B
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。, i+ ^6 R" O; n. `5 j, g
# q2 c& J: ?* h6 B3 k- n2 WInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
! }+ Z8 m8 Q& D o- W. iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf, Y7 G T$ g# A, P2 j
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
: A$ `! [, R8 Y: {5 P" oInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf) y# R. F6 b2 R2 o. c
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf" s4 b5 H% I, a1 h+ O( j
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
) X# f. x, |! a* {5 V* u- tInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
1 Z; L S( d6 E, q五.配置nginx3 \3 m s7 Z2 V# |3 O a
/ S! v7 D+ e/ S# b) ?& i
在需要启用modsecurity的主机的location下面加入下面两行即可:
) B& | q4 X, z/ p' `. o) r9 J) n7 J" c( x. \# \' z
ModSecurityEnabled on; ) p9 j+ T2 ~, s/ _' t
ModSecurityConfig modsecurity.conf; a# s7 {1 x3 C' i
下面是两个示例配置,php虚拟主机:
6 K o- u# D0 n* \' n1 V! E6 |" F3 c& I6 U
server {
5 \& B. s2 X1 q- W! T( A0 ]6 l7 C listen 80;
, b8 u5 {+ M/ `6 e server_name 52os.net www.52os.net;
, X, ]: @: W. c7 ]- }
1 m c p0 _1 A, R6 y location ~ \.php$ {
( A" H: r! H4 u ModSecurityEnabled on; $ w6 l# G7 n% w) I( z; f; S, D
ModSecurityConfig modsecurity.conf;1 `6 M& t2 Q/ F7 A& `4 Q9 U( E
, A& W8 e! s' t8 [3 o root /web/wordpress;
* {$ \ _- U* V4 I7 H6 z: @ index index.php index.html index.htm;
3 e/ a+ ]6 S$ n % b6 O5 P! J2 C" I" [9 Y( `
fastcgi_pass 127.0.0.1:9000;
' u5 k# R3 Q5 B( K fastcgi_index index.php;
$ t4 Y1 o. Q* i fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;* j; e. g4 [- r2 s1 L( a
include fastcgi_params;
5 v3 Z1 Z' {/ ^- Y. d1 J }5 l% o4 D& d/ Q# E
}
- m0 H8 O: V8 a! J8 Iupstream负载均衡:' H' J* c! s* W% R, t
) [5 H, A, ]7 ^& |- h9 M. u4 W+ _8 kupstream 52os.net {
+ ^! L$ R# Z6 r6 Q' ] server 192.168.1.100:8080;3 v, f6 s4 G# ]* {8 q
server 192.168.1.101:8080 backup;
3 ?9 T+ P" u$ I( h}
! n. c' ?4 N. @( G+ H
6 a; j Q/ y9 L: }7 L1 F* ^- a/ U$ Jserver {' d6 o0 W/ F3 }5 |( F
listen 80;
1 L. `5 n& k( S. n: P6 gserver_name 52os.net www.52os.net;- F: B0 J, |, A, e1 X P, M0 l
2 [2 i$ i0 H' R, X; j6 o& y
location / {. q& g! {5 D9 {+ m
ModSecurityEnabled on;
0 g' C5 l& s: D! X ModSecurityConfig modsecurity.conf; , t2 a% M* S' ]' S- x2 B" s
3 g* N3 Z0 C. s7 Y
proxy_pass http://online;* J: o) c; Y( m N7 Y
proxy_redirect off;; Y3 T( D2 f: Y/ W
proxy_set_header Host $host;
" E/ [) D& [+ b6 E! X/ J proxy_set_header X-Real-IP $remote_addr;
5 n* X1 R5 D, a' d P" v7 G# [" a proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
2 }, f2 V6 I, j; Z4 u }& E0 v. A6 c5 {1 k% ?
}: K" A8 O" j9 |
六.测试
8 W# g! S8 |3 A$ \
+ y8 L5 Q( x4 Z2 c我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:* G/ P/ b/ ~2 u) T
* c" I% o& o" }2 n3 `, i. ~
<?php- \8 F. Y& O% ]; [( ]
phpinfo(); % y% q/ e8 E( `/ q. w, ~8 f+ I
?>
$ L& P, a7 y5 [9 D在浏览器中访问:: p( E) j, M6 o; N9 e3 A
q2 S! |9 M( d9 s; c& vhttp://www.52os.net/phpinfo.php?id=1 正常显示。
% d6 b9 O8 a, y1 {http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
; Y2 h2 Q4 `' P( M$ N% } |http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。& `8 W6 z/ n* e) f! A8 g
说明sql注入和xss已经被过滤了
( o$ e& H6 f: K; R/ {" B. O
2 t) d" h" b! j3 N: U, t" j七、安装过程中排错
" F( z( _+ z9 C9 z L
9 \4 @& w1 E3 B2 k' Y/ |& G B( x1.缺少APXS会报错
J6 a, a6 Z8 b* I* N) J! s) F; L% @% X4 y' M
configure: looking for Apache module support via DSO through APXS
9 y* Q5 W J9 w+ rconfigure: error: couldn't find APXS
4 [6 M2 s# E+ {) t2 eapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。* ]( P; x5 q& m
解决方法:) }2 k" K; {1 j$ R
0 A9 k4 p4 T N6 a, f
yum install httpd-devel- J' K5 t7 s% j7 f3 F& T4 \9 O
2.没有pcre
. S( O3 c( K; I/ ^( \# T. f' {
`; i9 J: }) s. ~9 `& [configure: *** pcre library not found.
4 ?7 _' |2 h! n4 ~; L/ C6 Econfigure: error: pcre library is required
5 r j' B6 Q0 ^- \* f解决方法:! }9 X5 h* v: m0 ~
+ i- R! n- N* T- k9 Z( F
yum install pcre pcre-devel- @5 Z$ w( L% }/ x! T& H2 E3 F7 W# n
3.没有libxml2
- S% ^9 h! d: F/ p8 a* N2 X3 ?: G* w3 x# |- r: i9 }' o7 x6 {
1 ^ t/ @7 W& A4 k
configure: *** xml library not found.0 s$ n& Y$ R S% P: R- n
configure: error: libxml2 is required! a, e! u: k6 X1 W+ |* b W
解决方法:
7 a/ P# M( w: q" w. \
' V1 T8 s: ?( H j9 S6 ]( nyum install libxml2 libxml2-devel Z8 O2 X; Y' M
4.执行 /opt/tengine/sbin/nginx -m 时有警告
8 H, b, f4 I m, h f5 `' }3 c8 J5 U
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
9 d* M: P/ _! G/ v8 K \nginx: [warn] ModSecurity: Loaded APR do not match with compiled!# c: @' W% @. D$ V) m1 a
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log# }% o: C) y8 g8 q" `! F7 t
O* O' U1 Y+ n
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured., x: T6 @' w6 H
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"* f, z5 G; w+ [/ w' u; P) _/ t
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
6 ^/ J' P7 T$ o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
$ C5 o( V' Y" S7 P( O9 W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
2 H+ x" h' n4 y# G; c! ]2 }2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
: x D& `* p/ U0 Z+ f$ ^' F解决方法,移除低版本的APR (1.3.9)% |' j9 ?3 z1 j
6 K; P* I/ } s! G+ m5 x4 ?3 Qyum remove apr3 A1 a9 }; ^6 H, W( ]' [( p$ i
5.Error.log中有: Audit log: Failed to lock global mutex
8 @3 T+ Q" S- q$ Y8 v4 P2 e( y6 s! L* H
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock * Y1 x9 e, u, ~$ H, J- l4 @
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
. O; a% T; t" ~" t解决方法:
0 d; ~8 o' c( i( c5 l6 N编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:( c% ~7 P# t$ c9 _4 x$ O
: D* q9 u) n6 _ x; c& D1 q6 Y: u( W
SecAuditLogDirMode 0777
& L7 o o- X; x' e- u" dSecAuditLogFileMode 0550
; [& n; g, o, _- ySecAuditLogStorageDir /var/log/modsecurity
2 h1 N, b; R+ ~$ B8 rSecAuditLogType Concurrent3 ]2 M( b) V5 ?4 K" L
参考文章:0 P% u4 {* I8 T" t
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX6 N9 _2 ]4 e- R* m- W7 _
http://drops.wooyun.org/tips/2614 |
|