|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
1 w- g2 n$ l; B. f, a
! h" j/ N( v5 V9 ^5 V: j2 g9 |一.准备工作
' Q- } Y& K7 \3 m1 W4 A! W" Y. G g/ W. g# o
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
" e# m {8 T+ W6 p; d6 K d5 ~$ {2 ~
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
3 N. U# \+ U7 H( h. g! d9 X2 `- q+ l- C2 i7 p
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
, a( x: e: K7 t6 X. ?+ v1 v2 J# t& G/ c/ L9 d$ R
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
8 l) I& m! W/ L) m" V' Z0 L3 o% l( [7 P" C4 W4 s
依赖关系:4 U) x% G" W- x7 ~" O% w9 i
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
- y2 r0 j3 D; T' L! X# S% ~4 N( W h0 E( h* i
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel- |2 U6 k* z3 w# S- d( a
modsecurty依赖的包:pcre httpd-devel libxml2 apr
# m/ k# G. w t6 t! V# F. L! C7 d$ M* u+ [! O- M
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
q8 ?( C* n* Z0 M5 @0 K' O$ [二.启用standalone模块并编译
/ c5 ?8 V8 u2 D" K8 s9 i6 K1 {3 N, a# L1 b
下载modsecurity for nginx 解压,进入解压后目录执行:
" V1 [* w- Y' L. ~/ p3 D4 h7 V( y! {( v+ z- _% @6 k! j* X8 K
./autogen.sh' [3 v7 q- S3 K+ d. U7 B
./configure --enable-standalone-module --disable-mlogc/ {) p8 Y) V) W z& ~( m* {' Y( L
make
9 [6 O' n0 x; P% M' \6 c: }( ]7 h三.nginx添加modsecurity模块- m1 l0 E& x2 A3 L2 S+ \
( R% a8 y) o! T) \/ O. Q在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:: ]1 E$ X7 h! z/ Z. L
- O$ l! }$ Y5 U4 P$ U./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine' ]. g0 I4 Z; R4 Q9 H( s. l
make && make install
. e- y( W% z ~四.添加规则3 a( q8 x( z' Z4 V7 Q: g
% V4 R u& b( B8 J* r+ e' ~
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。$ N/ V) H$ ]: i$ f- X5 j, X" T% c
4 `+ V. ]1 }: V2 a0 `
1.下载OWASP规则:
& a5 `' ^. T2 x+ a2 T! N* i) z; m$ @9 @* t, \/ b
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
, k* J- O5 z% J; d% N% ?% D x8 H
A, i! H; w& X+ hmv owasp-modsecurity-crs /opt/tengine/conf/
! T" U9 @8 e5 d/ \
, V8 T& r1 d8 mcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
3 Q. F0 E$ A9 C9 e* T2.启用OWASP规则:
7 F2 z( P9 U s3 r% g; X( q, q' l& n
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
! N3 U! U/ T/ w6 i$ f
/ F2 R, w t6 z( ^编辑modsecurity.conf 文件,将SecRuleEngine设置为 on% h" S k/ n, z, z1 w3 [; D% j6 q2 y2 s1 k
0 d2 m. g# Y2 U. o1 }5 n( [ ~
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。; F5 G' _; w+ Y8 ~
2 V5 R* G- x7 U2 c/ V h; l% HInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
" X' s) D o+ `" B! L# UInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf: j5 D+ y k% _) I( ]% E7 `, z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf, _, B& g8 I7 ^4 l
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
3 q* K6 u3 X" a) Y( N% s7 VInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf1 l, R4 L! Z% s P* Z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
8 \% E" g1 b q' B0 B$ B' `Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
$ i0 }8 ?, x: x; k! i五.配置nginx) p+ ~; C" A! j* L: K# s4 b2 S
5 \0 s' W0 J* u {7 t6 w V
在需要启用modsecurity的主机的location下面加入下面两行即可:3 K" F L3 ]7 ~, L
4 o) x/ k1 s: K% @: ]
ModSecurityEnabled on; I" h9 _7 a, V0 G
ModSecurityConfig modsecurity.conf;
) Q! `3 \9 K4 N! C& F+ `1 b下面是两个示例配置,php虚拟主机:
9 X" h% J# |/ w
9 C$ a' C! |6 _6 I6 sserver {
$ O& u& K- g+ o listen 80;
( }, u) y% j1 m/ \0 `+ ^% s2 l( \ server_name 52os.net www.52os.net;
" E9 c* ? c I& y3 Q: U# Y
8 Z6 A, ^+ a+ Y1 E" S location ~ \.php$ {9 {' J5 O6 ~1 z+ ?4 k3 H' y
ModSecurityEnabled on; ' D: K! W& A+ |6 i
ModSecurityConfig modsecurity.conf;8 @1 a' ^, K" n
" z/ T# ~2 B+ M4 W$ D" i4 \
root /web/wordpress;
$ Q) l/ h7 R' E7 H. f6 K* G2 u index index.php index.html index.htm;5 T, x7 R8 J ^. S4 g7 `
3 F0 L4 J/ l# ^8 w5 a9 J
fastcgi_pass 127.0.0.1:9000;
- \4 N7 b- J! B fastcgi_index index.php;
9 D: e7 X' g/ h7 n) ]0 G6 r fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
% ~8 w2 g. n' O. F2 \: V include fastcgi_params;
- U2 }7 u) ]$ \: I4 y/ C# Z }' M: o* M. Z, a: A( H
}
7 ~. c4 ^5 j+ n! y8 O5 qupstream负载均衡:, l% N4 k. {: [9 F& n9 g/ D
' [1 x' `' L3 d mupstream 52os.net {
+ J1 r+ w8 W7 K% ] server 192.168.1.100:8080;" q& Z* D1 Y( U; f2 g( b
server 192.168.1.101:8080 backup;
8 n. S( U2 ~% K( u& w7 x( l. i}+ Q5 V3 d5 e( c% D% @
4 F% @0 R2 c4 t8 Y) |; ~
server {
4 v: \( p @% q! _( j, |% C. xlisten 80;% i4 J$ p2 b4 O2 Q. e+ C
server_name 52os.net www.52os.net;
! C* T0 ?2 M5 @& I8 G% e
* T# r; c9 ]) t. R( o* Y( ?0 Glocation / {2 X1 z/ J) d% L0 V8 u% C. M5 c
ModSecurityEnabled on; - \5 G* y* h2 B ?
ModSecurityConfig modsecurity.conf; [' q# z6 h" o/ }+ i
: e4 @- x) d4 x6 _, B. E8 m1 d
proxy_pass http://online;% Q: v: I3 x! q$ x3 u8 u
proxy_redirect off;- j# e _8 {, m
proxy_set_header Host $host;# r% l" U2 R) p5 i% Q6 @% P+ O
proxy_set_header X-Real-IP $remote_addr;
# H, o; v. r- r* c proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;, [/ Z, }! m/ `/ C9 ]! }, x
}! q5 Y2 D( z9 z! I1 _* D- A
}
/ a; u B* W6 W& U六.测试4 J" l0 E2 M2 |$ Z# i+ a5 M8 p
$ B* V5 \1 _" l' k. n& ~我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
" b$ v2 q$ g$ g% s. O* T3 V& z: ]# m& `9 {
<?php- u1 r+ o/ ~9 T% O) v2 i
phpinfo();
6 A0 l6 I4 n7 I+ Q$ |3 e+ z& M?>
W2 y* U. n4 C# e% _" G0 s在浏览器中访问:# z& U: f0 B7 f9 S
! I4 `6 L5 m2 P4 T
http://www.52os.net/phpinfo.php?id=1 正常显示。
" ^9 P ~. D( e( ^http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。- z) X6 Y7 X* F5 V
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
0 z* c% G; A8 y$ o" o* f说明sql注入和xss已经被过滤了3 L; l/ Y+ |0 J' D; F8 y, q3 ]* I& F
4 l- q# e. J4 v7 U/ Z6 J
七、安装过程中排错
& x q# L5 Q4 T2 j6 U: S8 ?7 ?4 P: a6 z5 r! G! I
1.缺少APXS会报错) b! J4 P7 g+ \8 O: w
# y# \" J a, _7 X5 f# V% g
configure: looking for Apache module support via DSO through APXS' P, T' j/ A7 v; q& z& g
configure: error: couldn't find APXS) N; d+ m& P( M9 a
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
$ N* q6 ? q# V) k& b( X/ ]解决方法:
/ ~" p, c( I( J8 V1 G% S$ X2 B" M2 C! _8 ~
yum install httpd-devel2 |8 P" x$ w# h8 ]
2.没有pcre
- G8 c8 ]4 c- F- i$ O* {/ y
! h& Y; Y" [) w3 m, |configure: *** pcre library not found.
0 B+ w# c$ W7 I# S4 ?0 Q( B4 vconfigure: error: pcre library is required {% a8 q, ^7 Z
解决方法:
3 r" I' J$ j+ \2 R
! _# [. \# q1 S$ e! myum install pcre pcre-devel) h9 k# }9 b% e5 F: S
3.没有libxml2
9 {4 R/ J# Q8 h
, F; T) w; p- z) z. n8 M& z% d
0 V% E7 X5 e7 |# B' c8 t9 i; Fconfigure: *** xml library not found.
' X7 e2 ?$ Y! v1 r5 rconfigure: error: libxml2 is required
! e9 r+ q% W5 c: u U5 s& F3 f7 X解决方法:( B: }8 {( {" s
" M7 w5 I q* R$ S7 k! O6 S
yum install libxml2 libxml2-devel
# E: ~9 u# a3 g6 l4.执行 /opt/tengine/sbin/nginx -m 时有警告9 L4 g1 s' a0 {
( E, W- \* Q9 ~1 `2 B! u) R CTengine version: Tengine/2.1.0 (nginx/1.6.2)1 t' r. ?) T4 z& _( A }5 C
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
0 h) P1 F$ X( \, b0 Q原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
% w4 s6 }! m$ `* M: n
$ F1 j" v9 e3 ^: S& N- A+ n2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.' K2 _% Z5 F n, p1 `9 H8 C
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"5 M) y9 h' s9 `& ]
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!; a' a$ K6 X3 q! K# ^
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
( S- B4 Y8 S! _# V# k$ H; @7 I) j2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"! @; e: x; b& x' A8 @3 J
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.% q M3 x. @& C' Z/ s- G$ k
解决方法,移除低版本的APR (1.3.9)
- }& M' q" S3 v, p" t# U) w! a1 ]. n7 j5 A
yum remove apr4 h4 ]5 g) q* i, ]" |3 C v$ G
5.Error.log中有: Audit log: Failed to lock global mutex" V% N* I% y6 h/ d
" ?) g7 z% L3 l8 l
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock . L2 q) {* _! o% d7 Y3 T4 P
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]' v2 j$ Q ^4 q5 E0 `5 v
解决方法:4 S j5 {; C9 u% V- z" {5 B
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:* p- _' D) J! ^% j2 i4 |
: k: |. z9 l. S( WSecAuditLogDirMode 0777
l9 p8 T( v/ W& M X! p3 n. N0 \: ISecAuditLogFileMode 0550
! H. p2 [8 e, z: iSecAuditLogStorageDir /var/log/modsecurity0 g0 u( s. _, n! j; w
SecAuditLogType Concurrent0 j/ B) M( T; [9 C( N
参考文章:
& E/ V! I0 |3 ?/ m; b) q/ R' Chttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
, F! E. T/ g( c3 v' f/ g4 N/ Hhttp://drops.wooyun.org/tips/2614 |
|