|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
# g$ T; E# D) }# n% R
6 o# b, m, x, D+ u- H一.准备工作% D6 W! y4 `2 J( w# s
7 K9 e+ D. \5 r* c: I- e( i' A
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0; f5 M2 ?$ U- B+ W$ E6 S Y# k4 j
6 @* T: F! A8 x0 ]" z9 y ztengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
. x2 t8 |9 e" G g4 L( l) |3 B
2 z( f7 j2 B0 I9 f3 f! n8 ^$ k2 Smodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
* L0 g; S# F# I
3 c i# f) t$ Z4 r4 [ ~OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
7 O, i: c: e7 I
4 X u. d3 Z) o( C' E2 f0 G依赖关系:
: r9 Q8 r0 n4 x( Qtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
9 r7 |6 c# L/ q, x5 ]) Q
# h" F. h! L j8 S; Nyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel- N' `. t% g5 p' B% N; q3 L
modsecurty依赖的包:pcre httpd-devel libxml2 apr
% ?4 t0 \1 J: ^1 r' g( V3 m2 |8 J y! ?$ B
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
7 g/ e% I+ h/ K, `二.启用standalone模块并编译1 G% i2 \8 }& S- e; z
2 W2 ^) j9 m4 n8 |
下载modsecurity for nginx 解压,进入解压后目录执行:
% f$ i; W' t. g) U* L4 g
, F# v5 d( H4 s./autogen.sh
. x; H4 y1 @2 c0 C8 d./configure --enable-standalone-module --disable-mlogc
8 }% C6 P) [4 k1 Tmake
7 u# O# |* B! f4 ~三.nginx添加modsecurity模块
4 u4 ]$ i5 x p1 G8 X+ r
) Q3 w& V) C# Q4 k f& L3 t# P在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:5 |3 A* _+ |$ \) W% s) N; X% n! c
7 o; A$ V* g. `/ Y
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
1 J' ]) |4 @' x4 S; fmake && make install
3 _0 E1 V" R) n* ?6 J& K四.添加规则: N0 h, U9 A" [
: `7 h2 R" O5 {' r E# q
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
* T- ]6 e3 I2 }! ]) i" h; r( k3 o9 |7 j; E: C+ Q
1.下载OWASP规则:- `6 {- e* A5 L6 B8 f6 `$ p
: @4 z. R( e# H$ J B4 s
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs/ r) k- w# @8 k, g( |! p. @
# Z4 `" N6 w. P* ?
mv owasp-modsecurity-crs /opt/tengine/conf/% Q; G* a- q! C# A8 D. J
5 k( t, Q% ]/ B( e8 X0 e9 k2 u( T: Xcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
( F+ G# V0 a6 K1 R5 i# X- u: @, T2.启用OWASP规则:
3 F n# G& o; T/ ?2 N' S8 v. Z6 @, z; w+ b! }! x7 s5 E9 m
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。$ @8 W* ]: k! B3 t. W% \
7 Y" S0 F# P. v6 ]! T4 u9 ]
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on5 q( N/ e; Z" [) B, s
- f# n8 p, A Q8 o/ t# E8 m2 }# y
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。+ m4 J5 e% Q1 `+ v3 i
0 ?/ Q& P( C8 m T! c9 s1 h5 ]
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf9 c0 C. T1 K5 n5 C
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf: \8 j1 n1 @. n6 q L/ E* ]2 m
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf" F/ V- H3 b& R+ R j0 ~. }3 P
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf/ X3 F/ X1 I# S3 w7 W n/ T
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf b& y# Q- p7 {+ r
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf6 A0 ~* y; x% u7 R, k; E
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf, \5 I: X( ]. }4 i) R* ]8 e! R
五.配置nginx
, I2 j0 T$ {" r/ F& ^! a; y
, u" }3 X/ Y1 X" f4 u( a在需要启用modsecurity的主机的location下面加入下面两行即可:. M+ ^$ {! S, W! O( u7 G, v8 A
, ~4 t7 \; l+ G; t& l1 LModSecurityEnabled on; ; }8 P7 f6 _/ P: _
ModSecurityConfig modsecurity.conf;
; h6 ?+ O" S7 [) i; j& Q7 y# E9 `下面是两个示例配置,php虚拟主机:+ y% E/ w# G; C- e+ H+ e) v, _& F' ]
( t0 \( z( L4 T, k- }server {6 B q' ?. i" J$ s
listen 80;
9 F+ {7 d/ }2 m0 Z1 O server_name 52os.net www.52os.net;
) `8 A1 U/ ^) ]
% _) c' N$ r0 c8 W# S b location ~ \.php$ {
) @8 k7 P H7 Q% _' }3 Q3 k ModSecurityEnabled on;
% G$ L8 c5 I3 C& E ModSecurityConfig modsecurity.conf; R' |* x; m8 y" X6 r1 e
0 b2 T$ f; ~7 e( K/ N! M& G& A root /web/wordpress;
' q' [' T: ^( v, B/ {! h index index.php index.html index.htm;" J. P( l" E: _* Z) f8 J: |1 c" G
3 W6 ^. ?6 i* `7 a; S
fastcgi_pass 127.0.0.1:9000;
+ ]4 g, J* Q6 J9 W0 j fastcgi_index index.php;2 ]- C: b# d$ y/ O5 b: r
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
9 G$ J" @* C/ Y* {6 U2 \4 k3 G0 D include fastcgi_params;/ _6 s! S3 K0 C9 i- w
}
4 y% O& Q4 k5 {1 G1 }0 Y }
* N5 c" B y8 c9 e) e vupstream负载均衡:
) }% Q4 Q. Y, B( f6 Z9 m! S. w$ P, b& v& x
upstream 52os.net {
: w- l) J! e- g# c1 H server 192.168.1.100:8080;
i7 M- @) T$ c4 Q5 p+ t; x- P/ Z/ Q server 192.168.1.101:8080 backup;
+ L- k9 R9 R* J* e3 K2 V}: v6 N+ L* b- h
, R% l$ N# z: F- M
server {* n9 B6 O$ y1 ^ i2 A# H) A% g' @2 a
listen 80;
4 }# ]+ K* Y# V$ S$ hserver_name 52os.net www.52os.net;# @) E: E. K6 |
! k$ e9 r0 p' w8 I
location / {& `5 X6 ^7 l5 j3 n) ]9 o: N) `
ModSecurityEnabled on; 5 `* r) m4 d3 b# @4 v$ [, t
ModSecurityConfig modsecurity.conf;
2 q, k! L2 X2 O; }- w; E
1 n% o2 b, X! I2 D$ h- u1 ` proxy_pass http://online;+ N5 J3 f R o7 n0 f: b- i
proxy_redirect off;
' N3 d- |; B' h& i4 U proxy_set_header Host $host;! |1 ^9 ~+ P' C
proxy_set_header X-Real-IP $remote_addr;" G: u" g: J: l7 T/ L
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
) Z$ J {0 ?. u( A% e0 P }
" L, ]5 W( V& O: `5 F6 p}
, e3 d' z; R4 p1 O六.测试
8 H% M4 r Y' T. x
. _! K) v: N5 n( H3 q我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
3 k9 u: {! C9 u; U/ z3 g. ^ T/ l/ n4 q8 L+ d7 y" c* f; o' g
<?php, T0 d2 G! J' p% H! k: t
phpinfo();
+ F- X4 l8 U5 I: {/ P+ W% `?>0 \/ o# ?. q; ^+ E7 Z7 G
在浏览器中访问:
/ q% U$ w. k& Q& l2 V% A2 }% y }$ p4 o
http://www.52os.net/phpinfo.php?id=1 正常显示。3 M6 v: P' H$ C3 p
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
" d/ u) s, V- `) Phttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。# p7 F1 M9 s" {- n. f/ A
说明sql注入和xss已经被过滤了
) }) b0 y' z2 ^& _' E N" ^6 [
8 I c* J( [& P0 h9 d& q七、安装过程中排错
$ c& V: Y! o) l1 t0 y3 p8 T$ _: U
1.缺少APXS会报错3 ^' C1 c$ h. f
& S! W4 f4 @* z Q: v9 _) V7 sconfigure: looking for Apache module support via DSO through APXS
% Y8 V+ X0 n# v8 a$ A+ Wconfigure: error: couldn't find APXS2 C. m/ \' ^/ d; q5 W8 C
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。. e9 F: Y; u; E9 v, ~' {
解决方法:
) O b! `1 K/ C+ }* |! K( r
! ^) T' x' L' [4 ` L0 q/ U! Lyum install httpd-devel
( [3 Q7 s* Q' M1 p6 V0 X$ {5 S l2.没有pcre
& x* X. V0 a9 C) o, Q, S1 A! v/ i W; I1 N/ I- q8 @
configure: *** pcre library not found.3 z( N! f3 m5 z% r5 n& Q
configure: error: pcre library is required
( P! C6 H& u7 Q% R- C) n# @/ V5 b解决方法:
# H$ E1 `1 E. K* R# v! Y. i3 b$ r
yum install pcre pcre-devel# e- U- @6 L2 u4 `* g
3.没有libxml2% B; C' `3 r9 q7 f( a3 f' x
0 E: u- M5 w7 q: ^
/ e: n' z- E0 V7 \2 m
configure: *** xml library not found.
, Q5 T) g0 E3 Y/ J! cconfigure: error: libxml2 is required
) O5 \ m' }0 [1 y% ^/ O5 ?解决方法:% G9 b. W0 ^# `2 P; y4 f( v: t
0 a5 c: y" @; q# G- o
yum install libxml2 libxml2-devel7 ^# f+ D2 F/ c6 m `
4.执行 /opt/tengine/sbin/nginx -m 时有警告
7 m0 O3 @% G) _* }+ R
9 x3 ^, S2 d# a8 t$ vTengine version: Tengine/2.1.0 (nginx/1.6.2)$ t1 Q* E* ], v) V* r
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
9 G& v" |$ P( r. I- }+ M原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log; Z( O$ e5 E7 G- @& {! P1 `& D
3 V7 V4 j- _" O! \! _0 t# ^' }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
4 c3 S: _7 U# \( J2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"" u# {8 v+ ~' Q* p- P" m" O
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled! r5 s/ |, H6 c' e8 X& F$ T" h
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05" n# y4 L: k7 H5 L
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6") ~. M, X' W0 a* O7 e0 C# N$ h% f& Q
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.6 a( k# ~$ g) Z; ~6 z* e
解决方法,移除低版本的APR (1.3.9)! y6 ^2 u" G4 U4 @# \/ G
! ?, W( h1 u8 v3 u- E! }7 `yum remove apr: y" F" }0 Q4 i. R7 g- R1 ?
5.Error.log中有: Audit log: Failed to lock global mutex
, S! U- W# s; o3 Z9 h" l" G
: X2 ~. C( G, y$ U. M2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 0 H7 z( s; `7 H6 V3 i% g! @4 j
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
; Z7 S9 q1 q. u, H( c2 |3 A解决方法:
]: T, Y: ^" @; k) A+ w编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
/ N8 c7 b& C$ L8 ?( C# I3 F, x9 |
+ h" C, H7 @% m& q6 @6 kSecAuditLogDirMode 07779 Q. m7 c! z# I9 D; R6 J/ c2 j
SecAuditLogFileMode 0550
; l& L% M$ N. ]5 h% rSecAuditLogStorageDir /var/log/modsecurity* ~) x8 A6 V. }, S2 P! R# A/ G5 d
SecAuditLogType Concurrent
- |* C( e" b9 t* Q- u参考文章:1 {( v; V% }2 ]. `' ]
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
# t! t& ] }" |9 X$ ohttp://drops.wooyun.org/tips/2614 |
|