|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。" D2 K S% T' [) T7 \) n( S% ^
" U5 q2 c' ]/ `0 e* w( ?, Y& I一.准备工作% c& h3 b, @0 @3 {/ k$ T6 u
. m: l' \1 e- Z& O系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
4 d3 f+ G# s( \. j5 L1 \/ S u( ^5 a
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
; U+ [5 g2 _- Y8 ~1 P" F4 g8 J
! b, z& g; g% B4 d* n& cmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
; g6 A2 G- `; S! h$ c$ e
8 ^: K" n, K$ J; V% Q3 t3 ~OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs4 h5 i! v2 h0 @/ [5 T/ |
/ `; c% n: D6 D. D
依赖关系:
5 N# m( J2 `$ V" t5 xtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
) ? K ~$ v+ n; s! M( b6 C( J2 P1 y# @% C& ?/ u5 O
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel4 _- k/ D2 o/ `& v/ q+ l( H
modsecurty依赖的包:pcre httpd-devel libxml2 apr- \$ F" |+ i5 o/ W8 H; a" q4 n
& A+ I5 E; `$ H% T
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
) i5 f; H7 Q+ `8 V* e- n二.启用standalone模块并编译
4 }5 V# u# B3 h7 I# P2 A8 e1 d
2 P G# T# p. ?" G5 M下载modsecurity for nginx 解压,进入解压后目录执行:5 a, o! d. u1 N/ h. \% Z) U- y: N
* l1 e8 q4 G2 v/ Y" |
./autogen.sh
& F; s% c+ K% \; o1 I& L$ |0 q./configure --enable-standalone-module --disable-mlogc
) k8 a9 |7 A3 R: R# bmake
$ d; B9 X& c- H$ n7 D三.nginx添加modsecurity模块
* v1 e }9 W: z) b& s* M: Q4 o
9 K4 v! o4 }/ ]" h) c在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:/ T- N `1 X7 v+ n7 X. i5 E
7 j& E! {5 y/ w3 @" |$ `* g./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine: t. j2 E$ w7 m% x# I) ]
make && make install
# X. [* |# n! R! z W+ P d四.添加规则" N, z6 |5 `) C( m: G; J
: R/ D1 F O) ~ [modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。( L% a( w+ Y9 t0 f) i
# I" w5 E) X1 W& \. _: `" L1.下载OWASP规则:( W# N8 N( m& w: J
1 o9 l3 t: [" n5 A+ L R
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs* N( P8 r2 x% d+ F0 w `/ u/ |' Y
0 W! X7 w" B0 U3 b( ~mv owasp-modsecurity-crs /opt/tengine/conf/7 b: c+ L0 P9 \* X9 J5 C
* e" J, F( M6 ucd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
1 `" y/ E5 o$ z8 H" \4 ^6 X2.启用OWASP规则:" [$ ?2 J, j5 p. i5 t
/ g4 k2 f$ y# p* F
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。" P5 t4 [: ^2 M. Q' `9 f1 L
$ k6 z5 }. q+ v编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
' p) m/ o2 v: P z+ ^% K0 P' v% z
, Q$ V+ H1 d& U& |5 nowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
7 V6 V* q; E) y! `% M* x3 ^& z+ u& j: X4 J" \9 t
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
6 [* B1 R/ O- q/ H) v/ CInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf1 k0 V- W( D+ i1 d l& b! p1 x- W* `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
z8 j+ N3 b+ x9 U0 U' LInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
' ?5 ]( j1 s2 C% V1 E8 ~0 BInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
. n) T0 G: b. _ A# G3 H- JInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf" o) d7 D0 B2 ~6 I
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf; |4 Q0 @% O6 p) @. H4 i t4 \
五.配置nginx# p s1 a+ T2 c+ t$ C# @( |
' m% U8 W6 _9 V s2 w5 O在需要启用modsecurity的主机的location下面加入下面两行即可:
# P2 O6 L! e/ U) A3 v. _1 U8 S; H) U2 }% |. m& w0 o, X6 Z1 U4 w0 M
ModSecurityEnabled on; $ {: ~/ ^3 |/ Y0 U
ModSecurityConfig modsecurity.conf;
$ Y3 {! p, Q: _3 @9 T下面是两个示例配置,php虚拟主机:4 r3 s1 m9 ?- a2 j& k- U/ t
6 t- N0 }, C: ?0 [0 L3 O! @% e6 Aserver {. }. k8 v1 e7 L; t: O
listen 80;
% v$ ~0 F( [& Z2 J& i( g# ? server_name 52os.net www.52os.net;( r' W& J1 _# ?9 I& F" L
' \; D% B, b5 }* m4 w0 ~
location ~ \.php$ {
/ [0 W, u2 p I l0 @& N3 G6 f ModSecurityEnabled on; + Q; w. m. h p5 a# J9 s
ModSecurityConfig modsecurity.conf;. h5 q3 s6 D: l0 H. `. g
! Q% L7 S, y8 s root /web/wordpress;$ _1 c1 \- f3 ]- o
index index.php index.html index.htm;% \! Y' a6 P4 g. t% ~+ p
8 r( U8 N# N$ s/ ^6 D5 O fastcgi_pass 127.0.0.1:9000;2 h- u b( p4 O- V3 i1 {' f
fastcgi_index index.php;5 N( A& g7 B+ m
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
0 q! m3 m. W7 e. y include fastcgi_params;9 u0 k7 o" G) c& C/ P- g! `5 A
}' M' P5 r; S9 D; K" c
}
# x% e$ j: |2 u3 r% _" q8 Lupstream负载均衡: W+ X8 n; w+ E* ?
6 F& F" v3 x% G* y. Jupstream 52os.net {' A' |6 [. K$ m# W+ |" O
server 192.168.1.100:8080;
+ P! p$ [; D( M/ _* p: [" a server 192.168.1.101:8080 backup;
! Q2 O- }) R* K( P& @6 W5 j) Z* N}8 Q& w2 j1 \$ G1 Z& o! W' |. B
, i V3 K* y+ e$ N7 r Z
server {+ B7 A8 c( [& ?% w4 c8 \
listen 80;
7 K- @2 e& e, V' ]1 Hserver_name 52os.net www.52os.net;
+ @# c) ^& J4 g5 x2 i* s
0 r, E7 t/ X% s6 E$ Mlocation / {
5 Y2 I4 T1 s( y4 |9 p% t ModSecurityEnabled on; # Q% X4 l+ j8 |
ModSecurityConfig modsecurity.conf;
u0 C6 U9 V0 H3 D
! \0 F9 Y1 _) d( I) A" Z' i proxy_pass http://online;6 z& Z; {8 \' D9 e, D; }
proxy_redirect off;
8 i. L/ Q8 w5 e P5 {1 x proxy_set_header Host $host;5 _5 P @5 W8 c- p
proxy_set_header X-Real-IP $remote_addr;) I6 _8 W) `) x( O$ `
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- a9 |( D4 h+ y' _9 M9 ]- q0 _ }9 x* ~% Z1 X, R
}
; L& T. b. o$ k9 B' X六.测试% |6 q- y- Y+ e
$ ~9 p* Y5 ?" U i8 I8 `- A9 T2 X我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:, Y& m/ V: U# M+ q; R( M I: \% ]
9 ?5 W+ z7 ? Y3 v: J; }0 L
<?php% B! e3 T6 z- t& S
phpinfo();
L. A1 p* J' Q' _4 c?> ?- o8 \/ ?: j5 A1 r9 ]- ~% L
在浏览器中访问:6 q( x- \; x4 d; S7 s2 _
- ? T8 u. t2 x& m6 phttp://www.52os.net/phpinfo.php?id=1 正常显示。
, L a }/ i; S7 i( Fhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。3 `- T, k2 b0 i( I% B" `3 q
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
( X P1 \/ L' {' p说明sql注入和xss已经被过滤了7 W3 I* r9 |- M5 _
( W. Q( e8 N, u; v2 X0 b6 Z' v
七、安装过程中排错( v z! \- i$ _8 n& X
$ q- ?2 q. H, r+ a7 y8 B9 V1.缺少APXS会报错- ~: k- X! B$ o2 _& U! S
+ O" C4 X0 T; N& {configure: looking for Apache module support via DSO through APXS
7 r4 k* J* }" C% {1 Aconfigure: error: couldn't find APXS& k9 |" X' f. S( e9 r) A7 X; ~4 A: f$ L
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。) O) Y# Z. {% L4 Y4 d k; _
解决方法:' [8 Q0 F/ f0 C/ [8 \
. _6 y: R) u% {4 y0 m( a1 ]# D
yum install httpd-devel
# g% B* w& ~ B8 J& Y+ F2 ?2.没有pcre
* ?2 y/ f7 {& r7 N# p) |! O" o
configure: *** pcre library not found.1 V! G) [( ?0 j# [ i$ C. p, T3 r* d
configure: error: pcre library is required
/ q+ c) R6 X* G n# N# B解决方法:
# i+ ~- u' o% g4 s0 Q, j* ]) _8 c
; n: G6 ?" p, P. S- `, Yyum install pcre pcre-devel
8 n L! E6 {( y- k! r* W9 I" @3.没有libxml2
& `4 h4 o/ u) I) H. E* T9 H( ~9 c! d1 _# {
6 j* ^! h$ b! J% d2 G7 G; u
configure: *** xml library not found.
: `( C1 p* Z/ v8 Y( Kconfigure: error: libxml2 is required+ X# S& P8 x# Y
解决方法:
3 |* o5 ~) b% F% X7 ~& \: h$ W: Z4 v, B: h( C0 M8 W4 F6 d$ d4 c' I0 T
yum install libxml2 libxml2-devel
+ k8 k8 L9 T# J3 @1 u4.执行 /opt/tengine/sbin/nginx -m 时有警告: z* i# v. q Y) k
9 z9 R$ t) Z7 S6 I) KTengine version: Tengine/2.1.0 (nginx/1.6.2)6 e+ }5 g# m( v$ a
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
9 ?6 E; w4 N# b r/ S/ L原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
' c0 [+ X8 b7 n Y
) l, R; c4 Q1 _9 y% s2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
6 e- { c5 ^, [4 l2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"4 T" O W' g) ?8 L0 T
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
1 f2 c# m/ b; m v B8 S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
, F; S C/ R& _& u/ u2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6") I8 V& ?) j( v5 |( s4 F3 r
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.2 B8 E' {7 w t% O
解决方法,移除低版本的APR (1.3.9)
% R# G$ y& z. v5 |- g7 b# p* [. M6 Z, Z
yum remove apr
2 r: m% O* D3 e. O- \5.Error.log中有: Audit log: Failed to lock global mutex
" i4 b8 h* n, R' ~6 }: N
/ {0 s: W1 L9 J8 ~$ b4 O. a% n2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
0 H V4 }& @) d/ j3 A0 vglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]: s& w8 n. Y. e& I2 o
解决方法:' d* x8 E& V3 d0 [9 `, W
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:7 I1 F. ?8 I4 u4 x# D5 F
# M& W2 y3 `" a, E4 s3 e0 ]SecAuditLogDirMode 0777
3 B7 w) L3 K# l- `4 R+ W4 JSecAuditLogFileMode 0550; m% S8 M3 d. Q4 U
SecAuditLogStorageDir /var/log/modsecurity& b- k& x1 r9 V/ D
SecAuditLogType Concurrent" s7 o3 k A; K7 g9 G2 u
参考文章:
& _- M( z5 }3 c6 `https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
- D) u3 U+ t. i) C3 _! C6 A' lhttp://drops.wooyun.org/tips/2614 |
|