|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。& ~3 g4 R! u! n5 E4 S' ~6 G
\- y% ?9 O0 p3 y一.准备工作+ U0 h/ L% l1 K! _
/ w/ Y4 L2 u& l
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0& O: v U* l2 B& j$ {
8 o' y( y- {. y! H6 j' Q- z' `; {, n! k
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz8 L+ @% q1 \( f( a
) q: K' n8 o! W1 n! O. pmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz: }- C! G- Q( h) A1 ]
- W* |% f) b& k8 p$ @0 QOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs& F' }& Y: E7 g4 Y( T9 H% R; n
' O, U% M$ V9 f$ a: }1 g依赖关系:
2 X; r& W6 y$ J9 Q% L6 }tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
! P5 ^- F: S, ] A/ F! k( n4 L
$ S3 B. Q6 l7 d* e$ s8 xyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
7 y6 U f( _) r8 k, h9 Nmodsecurty依赖的包:pcre httpd-devel libxml2 apr
$ u7 k! ~( B) k) i1 ^- f
+ Q! `5 e i: m/ n7 z, l c; q$ @yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel; Z$ J' }4 _, q! ^- P# Q0 J3 V: e1 W/ M) B
二.启用standalone模块并编译5 Q. ^" l6 B+ I U
* ?& M: u/ k1 g( M2 S: h下载modsecurity for nginx 解压,进入解压后目录执行:5 W x9 v q; h6 V% W/ X3 ?
; x. L! R5 L5 P: t7 n./autogen.sh, j ] z2 H. G3 r
./configure --enable-standalone-module --disable-mlogc
3 Z! |# Y, R+ d6 \make , P6 U; |' C2 L! L. G1 \
三.nginx添加modsecurity模块
- X+ r" C$ F' V, E! n3 w4 F
0 e" b {) ?# {在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
4 F0 i) M7 [( B8 r& \9 w2 @2 E6 A& O$ j9 f5 }
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine3 d$ m1 r6 [$ r& [: l0 q5 N9 j; e% ?
make && make install0 T) j; c. K% K- j( F
四.添加规则+ W8 l" j! d9 I, t3 b5 K' T
1 n# ]( k- Z$ K" L5 k4 N0 }modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。) v) @3 C% j: B' q, s9 ]# g
6 C: f8 c! d) u1.下载OWASP规则:) |: e+ N) h+ g+ n' O6 D
4 b' d2 e1 [2 g ^/ ~' o, V+ e* _: l
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs9 M( I4 o' E" V3 h! }
8 \5 q. ~" n' d
mv owasp-modsecurity-crs /opt/tengine/conf/
+ }" x! o" P6 V7 N7 v5 D3 B& j/ x) X2 \2 _
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf: J: V& d1 q5 M/ H V9 I
2.启用OWASP规则:
- `0 n/ B ?$ W" |5 i
) p5 ]* b' f. S; P复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
# i* f- K. [7 _+ Z1 Z% B- A/ t6 S3 v; U
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
$ s+ z- W" J! N+ \1 n" Q$ ]0 [& v* j" w5 g6 Y
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
0 `: P6 D* w* z$ g, U. k$ e. r- r0 x' Q+ k, H' y+ }" g
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf0 F) }5 E ]+ n& T7 ~0 B6 e2 }$ s
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf* ` a; p5 c$ R3 ?' }, R; \
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
6 ~) d2 Y5 p* M( ^2 y. Z7 P. jInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
( [9 e+ w# ]* t* h; y& i3 b# BInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf) G' h" e4 \, ]
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
# M0 |. D& ]' `2 M8 {5 V) E, Y3 qInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf* K; h# Q1 O$ k- z! \+ x: t
五.配置nginx2 ~* V# ^9 Z" Z
/ C4 y- v& k. m
在需要启用modsecurity的主机的location下面加入下面两行即可:
0 k# m# T/ U4 P3 t& S* d+ K% s) S) a0 @% P, E
ModSecurityEnabled on; & y( s9 q6 k2 K" s& {9 x$ o4 s
ModSecurityConfig modsecurity.conf;' K1 g- k4 b" u& k2 |; K
下面是两个示例配置,php虚拟主机:8 h, m7 p# \$ s2 v5 l
9 \. {5 M8 C9 ^1 D! K
server {
- b& ?5 H1 w ]. a listen 80;7 h* T6 ]* ]7 j: h0 Y F1 v
server_name 52os.net www.52os.net;
# y) I( A. j5 t7 S& v4 f5 N. k & i; A8 i( T& }4 O0 d E0 n
location ~ \.php$ {
; y" h+ F' S, C ModSecurityEnabled on; 1 q5 I/ N, P2 o0 W% K. q) O) N
ModSecurityConfig modsecurity.conf;# ~+ j2 C% W# z8 B* n$ t; g! _
7 {* B7 S" _) T% N5 U* e ]4 d$ ^ root /web/wordpress;
; h9 r0 m R" Z& k index index.php index.html index.htm;
/ o# ], I$ h9 H: v* @7 R+ D
/ b7 A7 N: V8 ~: V M* q4 ] fastcgi_pass 127.0.0.1:9000;
1 h( d" T% ^8 z fastcgi_index index.php;
3 R9 [8 q0 C. V% H( X fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
) Q0 H. i/ s' m" g. Y: q/ ^ include fastcgi_params;
2 L; C3 K; B; J, ] }6 l8 X8 E- E/ y3 n1 P6 t
}
7 N) i: Q& ?. qupstream负载均衡:
7 x" J& L$ `6 F" A/ s/ d
! M' R8 {) V/ U* Iupstream 52os.net {% {0 X% F* ` i0 ? ?' [
server 192.168.1.100:8080;3 G$ j! G$ t; W4 N( h9 o
server 192.168.1.101:8080 backup;9 e2 Z# S7 x7 _3 t$ z; c- q4 K6 P
}: ]' H7 V8 K E! R6 S9 q
& `% m! l6 T) I/ ]# L! Userver {0 b1 N! A$ F! O# e$ z0 k
listen 80;
6 t. k! l/ A( w% @. e( ^' n% Fserver_name 52os.net www.52os.net;
2 ], l0 v/ _4 z; [3 E. M5 ?7 L4 [# P. }' _
location / {/ h y! V, l8 Q
ModSecurityEnabled on; 6 N* x' |4 U3 N; W# q
ModSecurityConfig modsecurity.conf;
1 ^! W6 G5 y. H3 o% z `4 z8 g; M! }" {2 z
proxy_pass http://online;6 G- [6 C' ]$ `. d
proxy_redirect off;- q5 G$ y/ _5 |1 _4 L' Z( @
proxy_set_header Host $host;) K( R9 ~5 U& s( N8 P9 F5 O
proxy_set_header X-Real-IP $remote_addr;
" S! n% C7 m- Z" P( Q proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
" x: Y9 m8 E6 ]9 y }$ t3 w8 |7 e" l1 j8 ~! g
}
3 c& O) Z( j( |; B六.测试+ {! f6 L3 `9 _2 v( `: m$ x+ ]) t
% N3 @) _* H, B ?
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:2 |/ D2 c" E& A/ A7 _0 B
w+ h; }: ~! R! T5 Q3 w) g- w- ]4 o" U<?php; G: N& p7 g( R" a
phpinfo(); ) Z+ S4 n8 X# I
?>: @, \0 O3 j. P+ c1 r- q! b& E& c
在浏览器中访问:
/ ` `* B! i( C6 K* N
. A- Y, Y* M" S: Z7 h. W- Q; mhttp://www.52os.net/phpinfo.php?id=1 正常显示。
* Q5 j9 |% |% @; P v% `% y* Dhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
; l3 }/ C- A- V3 a* i; dhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
8 [1 F' H2 {7 E. M K说明sql注入和xss已经被过滤了) v7 G: I! m, W4 } ?
: V; N- d: G/ K5 r
七、安装过程中排错 Z) L" H6 D& o8 I7 }4 R4 y
/ Z4 k* X- H/ E \1.缺少APXS会报错" e9 ?5 F# u! C2 \
, G+ W% f% z# v! k/ J
configure: looking for Apache module support via DSO through APXS3 y7 H/ B6 l2 G$ s& p, H/ L
configure: error: couldn't find APXS
$ c4 M4 i% ^3 |apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。( n9 ?; q- Y2 M5 m
解决方法:8 _5 E; `* a% T5 o3 {
p0 _1 Y: \ L3 w% V2 {yum install httpd-devel
- ?) q6 J2 }- H3 L2.没有pcre6 ]9 ]: u( u) s6 m& e
$ ?5 X; Z) E; C2 U$ s0 Gconfigure: *** pcre library not found.
6 p# s' l. ?! ~/ @6 D" L: e9 Zconfigure: error: pcre library is required9 W. ~* s6 U4 _6 s# U
解决方法:- s+ b# B3 b( _- s; I
5 c* @/ m6 m6 P: wyum install pcre pcre-devel
6 h+ e* m6 o$ n' @6 y9 q3.没有libxml26 U' I7 s U; _/ B* p6 b0 L: ^
( x' m& {" b0 F( [, L- _6 j& N' F
# y0 o" O1 D" i* w8 ]; vconfigure: *** xml library not found." R0 Y2 {9 p! c0 [% H' S
configure: error: libxml2 is required
6 D* i' _4 E- m; S4 j$ F% _/ v解决方法:, a9 W% ^# O3 i! C& c1 ?6 @5 X
( p! L0 I! Z( v+ t( D/ ]. p
yum install libxml2 libxml2-devel
3 G# t5 h6 p% K! Q4.执行 /opt/tengine/sbin/nginx -m 时有警告4 B* k( W$ ?* N+ C- `
5 C1 ?# z- U" W* ^6 R0 s0 Y/ a- W0 q
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
+ a* P( X( `2 v2 Jnginx: [warn] ModSecurity: Loaded APR do not match with compiled!# O0 |3 A) K: R( F' |
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log# H' b. G# ?5 a- O. a2 h
+ K$ V$ {3 {; l. ]2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
. g) K8 W- E3 @1 ^# |9 O0 c; L% k2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"7 y' _( I+ O' K
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
3 e! V" W/ E6 [5 S( I' e2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"3 r9 H+ X) g. m* h3 y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
$ J% ]7 j7 T, t' G8 V7 j5 s/ m! Q2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
4 n9 c: U# f* I$ J7 F, Y5 \解决方法,移除低版本的APR (1.3.9)* f( j& Q- n; J) [1 @
7 k* i' F/ ^+ u0 ^5 ?
yum remove apr" I( ?# I# @3 H# M- s+ X
5.Error.log中有: Audit log: Failed to lock global mutex
y6 g3 T5 M+ Z) ~, y" W
) ]) e$ W9 H; |% J3 i1 Y0 Q2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock b1 r8 m' E2 d3 N
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
5 t6 \( p: `6 V; V解决方法:; I- x5 r! B: p/ E7 j
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
( r$ J1 B- C( P. d; E& f5 i& a& E
SecAuditLogDirMode 0777
! q3 }8 B/ @% i) zSecAuditLogFileMode 0550
7 m6 d6 I. T1 Q% t. |, JSecAuditLogStorageDir /var/log/modsecurity' E# P2 v0 J6 q8 a* T6 B
SecAuditLogType Concurrent4 D% n; L1 `" n0 |0 w
参考文章:0 j5 U* A5 ]( `5 B3 d; Q8 i1 V
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
; E# G4 H& ]$ _2 F& \# A: W* U/ `http://drops.wooyun.org/tips/2614 |
|