|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。9 x# @% L2 e% P- k8 p, B1 k
$ m7 ~( A8 \2 O& C" H一.准备工作
4 w5 N* g& P' e. a8 x; v; E" X, ?, Q
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
% |1 s V% j4 M" ~8 P8 e( I r" f9 u9 f. m( a
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
. x8 C, |+ }9 z! v" V6 [8 @2 l$ L/ C: r# t% L8 Q' y I
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
% g* A$ V G) n* _0 ]7 [- w# y1 A) B' I" B5 Z# i/ I5 v. r
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs$ [( m, l4 O+ b9 q* B, o
0 g; i1 j+ x' B* Y# y2 l; c
依赖关系:5 F4 [9 U0 |- @+ W
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:% W: ]3 b# J3 o* m0 k9 [
% @% |2 j7 q+ Z; Zyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel2 D5 m* F/ `9 i' Y- _+ x
modsecurty依赖的包:pcre httpd-devel libxml2 apr1 T @& G+ W* A) g
8 x4 ?8 x8 j2 l9 Z; T, K- lyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel" `6 Z) S& i' ~1 H
二.启用standalone模块并编译
0 [) A4 X3 p/ p! d! D5 F v- T" w* R! F2 Y7 [1 x) f2 u# T
下载modsecurity for nginx 解压,进入解压后目录执行:
0 a3 `- h' m5 g- d, ?2 a. Y) u" s9 d
./autogen.sh
( n/ o c q. O./configure --enable-standalone-module --disable-mlogc
, g1 j: j) r5 n: m% g, Lmake
: U; h& ?- O+ F% M3 r N1 D3 t5 G三.nginx添加modsecurity模块5 `2 G- B' R3 P
` u& \" L6 x* {: |在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:! V$ b; ]) H ^4 o, l! M
1 ~3 ^3 P' d4 Z( A7 v' f; w./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
, B8 a% s; f' U9 Q' s( g! Vmake && make install9 `: M1 f; I, i
四.添加规则
6 Q8 s, {! Q/ o7 h1 }
6 j& k" {2 k M8 L) p2 jmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
" R/ h; @/ Q& u3 @/ @
' h _) ~9 J* G1.下载OWASP规则:
; m4 M7 S, _7 a5 S. Y" l( N- K; P8 F" @- I# `: M- U# ~8 E
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
9 {# i G4 q9 U: C* c
1 X3 x1 o7 d; v% Nmv owasp-modsecurity-crs /opt/tengine/conf/
* H* V5 o3 T, |4 L0 R: v5 B# X* z
, Z6 P1 B" @. ?4 `7 m5 z+ T$ Ocd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf4 H' y1 K& p$ e* B. U
2.启用OWASP规则:
- x8 Z8 Q/ ^+ v- {+ l% h/ l8 ]* |0 \
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
' C) `: x- q- U/ G
$ O7 ^$ O6 w- J R( V+ _+ h编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
9 S. G. U2 q! s& ~& e: T4 _8 i) }" d6 R$ k8 ]0 ^5 i" j
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。1 d4 j6 G2 P/ ?, d( y& l8 W6 }
/ @# H; o! S+ ^) P7 \3 L, n- W3 ^
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
4 F, @4 a5 t" k- M) }Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf9 Q) S5 d6 t5 v% Q6 C: z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
g# k7 S+ R$ @0 B& V2 m; @- hInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
' Z8 q: h8 d& {8 XInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf( s6 h5 h2 E0 d0 @
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
. Z/ b3 J3 m5 W% J5 j) E( _! ^& |- J5 sInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf' W( h s$ s5 d, i( Q. i/ V
五.配置nginx& o2 }( `$ ?8 k0 V- H: y
+ d0 ?; y! U: s/ w) i
在需要启用modsecurity的主机的location下面加入下面两行即可:
& [8 w, T4 s; k4 {7 |3 Y
+ {6 b1 Y( X! |" }ModSecurityEnabled on; . q+ ?5 R- b5 b
ModSecurityConfig modsecurity.conf;; `3 p8 u8 \3 Z' a7 h
下面是两个示例配置,php虚拟主机:
$ ]* h$ f9 U% E2 ?, _
2 ~5 O6 k3 a" K/ F. R; K! Y6 S# s; userver {
# l: r' l9 }, O6 h. m4 F listen 80;8 r; C* l7 K, P# D& @: Y9 j
server_name 52os.net www.52os.net;
. L" \$ S7 s+ g; j
! Q4 n* [( H2 N2 Z7 P" m" H# u location ~ \.php$ {& Y" u% w, R1 M# E4 ~. Q; G
ModSecurityEnabled on;
4 f4 _0 d0 u* z& B" ~: ^5 N ModSecurityConfig modsecurity.conf;
8 _# N2 |4 d( H w. m. J
( x% I$ O+ @" V0 v) [+ w0 X! r root /web/wordpress;- @9 }/ k& z" S1 x. G( Y* k0 W+ [( k
index index.php index.html index.htm;0 L. z* v' h% W$ r* K0 a/ s9 ~
) I; F1 a' w6 G/ |6 c
fastcgi_pass 127.0.0.1:9000;: a9 F1 i$ S; x
fastcgi_index index.php;0 t$ P R0 z2 h
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;! _7 C$ r9 ~0 }, y0 |& o9 v8 ?$ s
include fastcgi_params; ~1 r3 S: Y$ n3 ~& K- {' o0 R: [
}
6 v2 s( I/ N K% O L) ~ }
9 \3 `" f5 j# I+ M& yupstream负载均衡:
; D5 v/ D; b2 F1 R7 D8 B1 o) o
3 G. s- I0 v W I# h3 c ]upstream 52os.net {0 S3 u4 R7 Y9 u* C
server 192.168.1.100:8080;/ j7 E/ B7 h- V; t- n
server 192.168.1.101:8080 backup;
# O$ V+ {1 d2 \}% h1 C/ ?# T U$ Y
' \ ]; ]/ k( X3 Userver {
2 F7 }( l7 Y& t* Q3 llisten 80;9 V/ ^# S- K& M; W7 B
server_name 52os.net www.52os.net;* U8 E: M3 l. a' G' N
. m2 D! t) j4 [6 g0 glocation / {% L2 k( P) A4 u: x6 C9 W
ModSecurityEnabled on;
# U2 s9 o8 `: g, @( Y5 v ModSecurityConfig modsecurity.conf;
0 a! s# x5 `2 \! w% { Q. ], r( B6 W3 i2 f* Z2 A! `2 w m
proxy_pass http://online;/ I9 z) r- o- g, M; J
proxy_redirect off;
6 f4 B6 F; P, E. m proxy_set_header Host $host;
# F2 y8 O+ c$ y$ n% G! f) ` proxy_set_header X-Real-IP $remote_addr;& {9 x! Q& B* @( T; h+ ~* V
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
/ U* k( S& U" V- @+ x: k' w% w! H }
5 h# R# P- X- b! z1 U}5 A# l( Q" P. z, X- e& \. i8 o O
六.测试
" ~( R" v) s. C$ _4 _
0 |6 r( H& T, H$ a: x我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
6 t) W/ P3 e3 T6 w# ]" C$ e, c5 v# y! m) D
<?php& `3 N# G+ j; [! {0 R; p! V- J: d
phpinfo();
M5 t, o# r e* ?3 H' c+ {?>
! t* } E3 ?& f在浏览器中访问:
$ C, P) G& B3 x1 ~* F5 U5 H% V* Y+ J4 S
; `+ Q5 x5 F5 L/ Ahttp://www.52os.net/phpinfo.php?id=1 正常显示。# P. k$ N. a7 Y+ ^/ W
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
4 T; Z |1 `/ g J5 l" z% a5 xhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
) y* r' c# K" v9 p说明sql注入和xss已经被过滤了
; P/ X( Q: A; A0 g5 T% K# {) ]' a- w$ d
七、安装过程中排错' k: }' o: f- }7 D9 g
7 t* O" Z* t. v: C; X* W
1.缺少APXS会报错) ~4 j: U3 F1 z4 p W
7 Q7 s0 y5 ^, H9 ?
configure: looking for Apache module support via DSO through APXS: K/ X: A6 H( S7 j5 E$ A
configure: error: couldn't find APXS0 ^1 m0 K: z9 u
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
W* H, F* c5 A8 V. j/ s解决方法:3 {9 A! j" M0 k8 i6 X
+ I+ ^" }5 _. _8 n% ~* Tyum install httpd-devel
( \% Q9 c9 a% I* M i6 S2.没有pcre
, N; {2 h) O8 G3 d9 p: c9 U
1 I% W4 c+ @5 l5 l- \configure: *** pcre library not found.
. `/ M# @" o3 k5 H( e8 _configure: error: pcre library is required
4 S2 Y: @$ v% X0 {解决方法:
( }: x0 ^1 f0 R+ X, E' Y+ S$ K' r2 [; t: x& \ j5 e
yum install pcre pcre-devel, f7 L: w# u' I: d6 [
3.没有libxml2/ w( T3 k; G- {* u' s
& a( u1 g6 T! C$ Q8 m' x. V- O. X% F" r& h# Z* U" q6 ~
configure: *** xml library not found.1 K, w$ A, r0 N6 _2 L+ ]
configure: error: libxml2 is required
2 q5 Q' h! H* P3 |3 u解决方法:
2 ]* A L+ a" X: b
7 [, V+ M ]& `yum install libxml2 libxml2-devel
& g0 J& @/ c( o2 z; Q: e7 Y4.执行 /opt/tengine/sbin/nginx -m 时有警告
) E9 H( r5 m. L* D+ X0 J i @! f) ?% W, l: y& I2 V5 G
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
) F0 o! E& N4 m; O+ E2 inginx: [warn] ModSecurity: Loaded APR do not match with compiled!
, u& m6 g3 S: h1 z7 _. e原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
8 T8 c* Z7 W! A, A+ D4 p8 \. v) C- V3 o5 d
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.; Z" s; H8 F0 k+ q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"4 ?7 {# `/ C3 W5 n& ]4 S$ z' Q
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!/ t5 V) `) a% r0 m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
& m: J0 J* i3 P6 D" }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
. {* G8 D( P4 l+ Y1 p2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.9 @' |' R4 ]1 w' m
解决方法,移除低版本的APR (1.3.9)
1 J. [5 S- T% @" K* G2 u9 N6 E1 g8 W* M" G/ f' Y7 u- k: t
yum remove apr+ A1 s4 {9 _+ |4 b6 R6 {
5.Error.log中有: Audit log: Failed to lock global mutex
9 H& ]- _. W& _9 g$ l1 b1 Y; ?1 [& P# ^4 {1 `
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ( R3 d9 _% U6 T( x
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
% m! U5 {( O: ~解决方法:) |% ~3 T7 y# G- D2 J
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
) Y' n- B1 P& l- T- j5 J/ ~% ^! }& h
SecAuditLogDirMode 0777/ J5 _1 Y1 V/ Q. ]7 e" Q
SecAuditLogFileMode 0550
" J, N" o! A! u, f& ]& g. n. TSecAuditLogStorageDir /var/log/modsecurity; q$ p8 ]6 w7 ` n0 I7 Y3 l7 D0 x2 x
SecAuditLogType Concurrent' P' d- [7 [6 w! o
参考文章:, | c8 u- [0 @4 R. h7 S8 |
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX( z8 x3 x. L( y% o
http://drops.wooyun.org/tips/2614 |
|