|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
, q2 i1 U; |9 f7 P8 c q2 V6 A$ Z7 u0 O; A+ H: S
一.准备工作
- M. S/ S- l8 Y( y2 N: q! L! G& G' ]0 b8 }2 Y$ s+ j; `
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
) x" l. X3 p) D+ v. p* L/ b
# O* i h% U& h- u% W. @$ Gtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz9 L7 k' |3 O. g6 \) J$ p% d9 p9 {2 {
" ]1 _3 x3 u$ J! ^( hmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz- X) j9 ]- Z6 M/ I7 a' Q- K0 B# P& i1 k
/ i9 J) R; |2 W, S: _3 GOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
0 _" c& B+ H, ?6 O* s; F1 s* U+ l1 P! Z9 u; }3 m
依赖关系:
3 \. q6 O4 N3 x$ {2 n2 atengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
a5 i2 O4 E' Y; k. P0 n
! p6 K% X) F0 [. cyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel2 e, _+ o( a$ Q5 F, \
modsecurty依赖的包:pcre httpd-devel libxml2 apr0 [# w, W- M1 k; U: [% W
# J7 V5 ^ X% y/ m/ M4 O
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel3 t8 M, [ ^" w% O
二.启用standalone模块并编译
. M: c, Q/ W3 y$ V3 q* m% Q0 I( C) V6 S7 J
下载modsecurity for nginx 解压,进入解压后目录执行:
8 c" g% C. _' Z; ^% X
1 L2 N3 @- P+ }: n# p% S; O./autogen.sh
4 V, ?0 T- _8 z3 U- X4 b./configure --enable-standalone-module --disable-mlogc5 F9 _! Q! w1 ^. j5 Z" c
make
% K' C+ n8 O' f9 z三.nginx添加modsecurity模块
3 y, F, i; X& w: K R
: c: l! C- z0 }3 d$ R$ H在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
6 h% c: ~% h' k5 `; d. {' v
% o2 g/ D& [) n. o ^8 M./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
4 h6 `. @6 Z! D6 x" D4 a! Q4 M$ I" Qmake && make install) e- I* h" L# F, j. V1 E
四.添加规则
8 Z& o+ n* c. }, r; e( ~2 k, R6 U
: {! E7 K! U( S3 v# Ymodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。; `& U9 h# G) B
, \% U ^; r; W! a+ Z u! |" o1.下载OWASP规则:& c! }# ?$ l+ d' b
' c( b/ G8 H/ |5 D# C- y. T4 igit clone https://github.com/SpiderLabs/owasp-modsecurity-crs6 {3 r3 {' }$ u3 O& Q) `# ~
7 I- g ?9 s( P& j3 qmv owasp-modsecurity-crs /opt/tengine/conf/
1 g+ k! |$ S: J5 C. T
8 ?6 W. w3 E) y0 u; ccd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf! ~9 g9 _( o: j, V, W: n/ ~; M0 @
2.启用OWASP规则:
" v; \2 I( O- Q# K% Z9 j4 u4 O$ s( |* |* r2 `2 q9 h7 l% i, l, t
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
% l' K: ]3 n/ | G& k+ }* y8 _0 ]) y! O$ G* D$ F5 L9 h5 y
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
5 r$ Z7 Y5 m) a$ q( U W/ D2 M$ o4 {; q3 h2 p, y
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。/ o. P* @/ y% y3 u( _! Y
) l6 R. j- C! \3 p7 W8 x2 t9 U) WInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf4 i* q/ h7 A0 C6 h1 |
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
J$ P+ \8 r& n c ^2 O6 m, m! lInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf( @/ x0 q# V d, Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
% G3 q" L/ i, s2 qInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
$ n& f5 m! L4 R, a2 t. A, NInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf3 P$ E/ ]' F1 t) U; A( ?& B
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
0 A* k8 q5 z3 D: z五.配置nginx0 c' ~0 c3 h- r7 h5 k- h! u \7 }' ~
* B) X7 v6 C2 F7 X在需要启用modsecurity的主机的location下面加入下面两行即可: Z( ?- V4 F8 H2 o9 _
* e3 ?$ W/ s. m! P$ f/ ?4 l
ModSecurityEnabled on;
# G% I, Q* U1 k- @ModSecurityConfig modsecurity.conf;! {1 t2 d% d: o0 l; u8 ?) v% q
下面是两个示例配置,php虚拟主机:
5 p" m8 I+ c& j" Y
$ {6 o$ V, c7 T$ L7 Dserver {; n6 D( G& `( Z2 G/ A+ }& R
listen 80;
4 r! w2 R9 F- x server_name 52os.net www.52os.net;
/ a7 F( C4 N. e% l$ u9 S( ~: K
# e. K5 H. [5 O+ \: F) I location ~ \.php$ {
: s" Q$ @: d) v ModSecurityEnabled on;
( d7 l$ N1 g! N; n( e- j. ` ModSecurityConfig modsecurity.conf;
* l1 t* W3 c, G0 z' m
" @" [8 l4 G5 \! P) } root /web/wordpress;+ @ u& J- J# |) B. Y8 C
index index.php index.html index.htm;; y" g$ l/ [$ T4 W% {
* ~' P0 z/ X G" J2 d" o) X8 L
fastcgi_pass 127.0.0.1:9000;
+ [8 H1 I, Q8 D: ]6 v* G7 |" P fastcgi_index index.php;
) _& _( q' I5 n7 H fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name; ?, L- S( Q3 S
include fastcgi_params;
+ `' L# U6 [& E6 }$ R- h% R5 G }" j# @: g. e: S3 J8 u4 U
}" n+ A. L8 L6 [6 v8 C, F$ p
upstream负载均衡:
( W' D4 P9 O L$ T+ z8 g: N# b. [- @
$ h* W. [- ?- m- U( Fupstream 52os.net {
1 u8 `/ I( D0 O5 {$ v/ g7 q5 \ server 192.168.1.100:8080;
! c7 ~ v0 g& n. S server 192.168.1.101:8080 backup;6 @- }) a% t1 |# s7 x& E4 p0 c
}- j8 L9 O9 v$ |( c( `' o, X- }' G/ S+ S7 ]
, }* W8 C( H$ G6 {# s7 y$ n$ G
server {
( S" r' V8 v3 ~; L- t* l9 U1 s6 z. xlisten 80;
( y0 n6 Z( l9 s& J0 I4 ?server_name 52os.net www.52os.net;: A# P9 X" o* w
/ ^; B: Q" p) g" c2 Q5 }" g
location / {
4 J1 T! Q. V+ U5 C, z ModSecurityEnabled on; 1 y( M/ Q! \! p0 K' a3 J. _6 h7 b- }
ModSecurityConfig modsecurity.conf; q: r, v: @8 A
# Y/ ?0 Q2 J! r. |) V proxy_pass http://online;
! ]% J: {; u3 ?3 D9 R proxy_redirect off;
8 x( F$ I$ ~8 |. v7 H- U proxy_set_header Host $host;- A; q6 q; `+ C, u
proxy_set_header X-Real-IP $remote_addr;% P# l& V4 L" Q, k
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# ~* {' `, D- L }9 I4 B# d# [: T7 T
}4 G$ A) i* {9 k# l9 B6 t% }
六.测试0 S5 P, Y% y! V6 x1 T2 o7 d
. [( t- n1 G9 h4 t4 {$ n+ d1 e# o
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
9 E% e2 r# Q) d0 f+ n$ q
% y( M4 R [$ Y<?php/ z* j" B; Z# f1 z5 g' c$ p) s s
phpinfo();
* k' R( K( ^. [) ^& G?>6 J) s. f2 T3 b z* q+ f2 ?
在浏览器中访问:
2 W# J4 c2 N' k6 [. l! i6 ?% b0 ]3 ]6 Q/ F
http://www.52os.net/phpinfo.php?id=1 正常显示。
* _; ]$ i% A. d- M' V" Ehttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
4 c7 ~/ ]4 j. @5 Y$ bhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
3 |5 m$ u6 i# M9 L+ s说明sql注入和xss已经被过滤了4 t1 L3 F) W6 [6 @. n
0 A0 L8 Q5 g7 P8 t
七、安装过程中排错3 S4 I- N* ?( O N& l( ^
. \5 |. A. K! Y0 W4 ]% ~1.缺少APXS会报错
. r, I2 g# n( }' R6 W( y/ T' C
( ~# M, r l8 r9 j* k/ \- J. hconfigure: looking for Apache module support via DSO through APXS
; r; O; r# W2 }2 V. o6 Mconfigure: error: couldn't find APXS5 y/ y& _1 D( I) a
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
4 `/ n9 z2 H, a6 Z7 B: M# Y解决方法:
' `. V. [8 t! e( s4 F( ^% R) f. t- g8 o. V
yum install httpd-devel8 J6 |, w; Q/ y9 u
2.没有pcre
9 a- b. e3 z2 v5 L+ r3 G0 I1 ~- R
! p1 _. e' P6 X) X7 sconfigure: *** pcre library not found.
( R; [( x5 {2 g* y# Q9 z% lconfigure: error: pcre library is required/ i! q6 w2 X! g- Y) y- W+ O6 P% V
解决方法:
5 T/ M) Z- o' j5 m0 N. @/ E; t( d* \
% E. H/ {8 r) j' @+ D. [yum install pcre pcre-devel
2 m" h. G' `, {7 q3.没有libxml2 V: l5 h9 w* L& v2 M
5 t9 y7 {* h+ ?' s
& {0 j. u, I0 y) N0 R5 [" ^8 xconfigure: *** xml library not found.2 I8 e" H+ B# f% l. @1 L% p! }7 \& k2 ? B
configure: error: libxml2 is required, T: r( @0 S3 l0 Z5 D# W3 y7 z
解决方法:# |1 I/ z. k/ r3 a) o2 v0 w. J
9 Q' U. I, }) g- o1 q7 p
yum install libxml2 libxml2-devel
( f \# k! S, G% v* i0 c4.执行 /opt/tengine/sbin/nginx -m 时有警告6 g! _1 [* j- Q+ u* h
, w' r! L# u$ X% \( ^7 t% STengine version: Tengine/2.1.0 (nginx/1.6.2)
8 s$ k2 n" a# W" Znginx: [warn] ModSecurity: Loaded APR do not match with compiled!
: M/ V8 O0 J; `: A- k原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
9 z' _6 z. x4 k' `- }
- a/ o; T( v( R! t- A2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.: H% b D: u8 y; c
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
* w5 h! [( V# C: `' t6 G! j2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
) X8 S: P: w7 p) m$ b% ]" [2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"/ p0 U/ T; T+ j' N- |; U. b( L# X
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
# ^4 |% o& V6 q. R2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On./ Y5 ]. o; B2 r" |4 o3 Z% M/ k
解决方法,移除低版本的APR (1.3.9)" r: W5 j! i# `* s: ^7 s1 A
B2 i# \) Q/ d# p& F# {
yum remove apr
/ ^1 B- j( r S, c. Y5.Error.log中有: Audit log: Failed to lock global mutex2 t6 P j* K- P: A* ?4 A5 d/ ~
+ U; \' R0 c( k: j7 R2 d4 q2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
$ s- X- A. J* X0 Q/ o; gglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]4 c, t% ]; p! C, i
解决方法:, q) r7 M h" l
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:$ @: u/ Z' e! Q7 @1 n
. S, n) Z* N! W4 K4 O3 n
SecAuditLogDirMode 07774 d5 E S0 {' p4 q7 _; ^ Z
SecAuditLogFileMode 0550; M3 B2 M. j& K, K2 L( x# g
SecAuditLogStorageDir /var/log/modsecurity
$ [# Z, z3 [ T% v5 _SecAuditLogType Concurrent
' v5 m: k: X8 H- g) g7 K, I* ?( U参考文章:' s+ c W1 c0 u$ D6 Z* e
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX( @7 H: I' k8 X/ m3 d
http://drops.wooyun.org/tips/2614 |
|