|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。: _' F; B* N3 J! U0 k
, h0 V2 f. |) ]' E" a$ G
一.准备工作& E9 X' w t; A; i5 T
6 h/ m I- T. `, H+ [; G) j
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.05 F! V, H$ R& E8 k8 d k; W
$ M7 N7 G) N9 O/ N- q
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz8 C4 X& x1 d6 \' j3 p
7 c# L. v5 G3 Q2 \ a: O Dmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ n3 r4 P8 f, m3 }- D4 v, o; C" Q) L1 }3 D
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
7 @8 G; ^; K/ g2 v$ a, N
0 g( t/ R5 F4 w" L9 H5 v依赖关系:" q/ t6 ]" M, m" Q7 g
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
) v' D& O6 f0 \9 l) m4 K9 V
& Q& W0 {; P- m6 M' b0 _7 D% {% Hyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
- a/ T$ }2 m( o+ Tmodsecurty依赖的包:pcre httpd-devel libxml2 apr
( d8 ^, F9 V( E9 H4 X% d" N/ @ f" H" S
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
9 n" ?, J6 k) i二.启用standalone模块并编译( i& n' g5 M4 H: ]* N7 P
2 W* \! Y% M$ v V1 I) q* P下载modsecurity for nginx 解压,进入解压后目录执行:
: a" @1 c* z2 K
. n) M$ G, ~9 a7 [* `7 }./autogen.sh
4 y" w4 B; s6 [& l& C N./configure --enable-standalone-module --disable-mlogc' P- K$ |3 [4 M) b& G# y* A1 @
make
4 K n! a5 ?+ |3 n; ]% M/ u三.nginx添加modsecurity模块
" {- P2 b6 |! u$ G; [9 n( p' y( e
/ w3 ]4 p9 O3 N* D在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
- I& r7 z# y/ O) q9 H6 ^6 i5 n; q/ j) y$ G+ o2 Z
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
$ W" v. h( I2 e3 s3 K& y0 Rmake && make install/ H) _! z! m G. o+ C, P
四.添加规则( k* J' {0 Q, B b
0 h& ?8 V# C. ?% z
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。* e* S, {) B, O, Z
' y5 s+ `+ }, m
1.下载OWASP规则:9 o3 D7 V& \ N( |8 C5 B
8 z: y0 }3 g" V+ p& I+ d" n
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs' }8 ~7 w1 p9 V: V; ?8 m) [2 \
* S0 c2 K- P/ b& v% n' C6 M% ~
mv owasp-modsecurity-crs /opt/tengine/conf/
+ I; R& k8 K# c1 `& x4 q
! [5 q5 m& }- s. @. ]cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf _! }& @1 c* @/ F; B
2.启用OWASP规则:
* W. i- D+ t. V- v' C
* J% |8 {! R- p# H5 p; H. W/ d复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
) F [: |+ p0 X9 W7 s# Q) b$ x/ o: E& O
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on" ~5 B* \2 u$ q+ J, L/ p3 A
8 J- r! j& a) F! }owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。, h- J" O- g( W' Q; S [7 v" R3 @
/ ]2 c+ r. I" \4 t& v3 [Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
: a$ |- d2 p b9 ~2 XInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf. n& k7 ?4 h0 ~+ b
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf3 X( {' u% h! r9 `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
( A! k+ G% C1 a: g. v7 }Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf6 E! x/ }2 p* B! @9 B1 _7 w
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
) o6 h# p7 S7 [Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf+ G* N6 q; S' A, v$ _
五.配置nginx
1 U9 ?! q3 R) K/ V: `2 L' A
. @( M5 O* Z/ h" u* a# ^在需要启用modsecurity的主机的location下面加入下面两行即可:- |6 }( F) p( v1 K' }
; K4 q9 p, E; `/ W+ ~+ s, o
ModSecurityEnabled on;
. ] m" G9 q# I& U; `" s* w* @# ~ModSecurityConfig modsecurity.conf;6 ]0 ? D- g3 U+ K
下面是两个示例配置,php虚拟主机:9 e5 f% K' I! C
7 \# z& y# a, w. } z. L4 Dserver {, W" M2 Z" ~4 L& I( @* n: N
listen 80;- S& P- s0 {& ^% b" C
server_name 52os.net www.52os.net;7 n. O* G3 x, o, }$ n% ~
* F9 s9 ^, Q' N) H D4 t, k5 H9 j
location ~ \.php$ {
0 v) b' W; Y( s' @1 D: S. D( t5 O ModSecurityEnabled on;
" Y8 D* p q4 P* z ModSecurityConfig modsecurity.conf;' N3 u, `1 k- N; D; {) U6 L
5 E3 `0 U6 w9 m/ E, j {$ y' h
root /web/wordpress;
. I; C0 p# p% U; @( X index index.php index.html index.htm;5 S6 X. Z; W0 i: j
6 A' e( Q; m5 h: i fastcgi_pass 127.0.0.1:9000;
9 ]6 z% A0 [; S7 z; M fastcgi_index index.php;/ V) [& K" w* i: i @8 D0 l! Y7 ]5 ?
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
6 ^8 C5 S6 z2 i% R include fastcgi_params;
u, I! g! _8 Q! V, j }
) @7 j7 e' ~1 c1 Y( {8 e8 _ } Z" S1 K( |. r4 e3 v
upstream负载均衡:
( D) {2 v+ F; n' ]- K$ \
2 s& U% K% a. [* S" f# Hupstream 52os.net {
4 _2 O; W3 a# x" D1 \* Q server 192.168.1.100:8080;
( c, h7 F. q: V" \2 _ server 192.168.1.101:8080 backup;
0 o% X4 t) i0 ?* o) }}, X/ X& ? \3 _) E
4 _5 [0 O' f- a# K7 I. ^4 a/ {
server {
% E& p9 i- q# j" q" m' [listen 80;
Y+ M4 q4 t" K, X0 x$ z0 wserver_name 52os.net www.52os.net;! Y, S7 A* l( }
3 v2 h g, E' a4 h. g. h
location / {
# j/ n8 `/ _. `2 W ModSecurityEnabled on;
3 T! O5 b7 V, D$ ` ModSecurityConfig modsecurity.conf; ) B* a' L. }" K c6 e
) e8 m. p+ S0 S8 `8 c9 P; W proxy_pass http://online;
+ L. V, Y; Y+ }# p' x( [( _ proxy_redirect off;
7 x3 b* A! B8 O. h, w proxy_set_header Host $host;% u( m8 m- V6 n) H" i% p
proxy_set_header X-Real-IP $remote_addr;
1 Y/ f: |9 h7 \' p: ] g$ C p proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;8 V* D5 }! o- _5 w% [- {; A* d2 o8 p
}5 a& @5 G: q) |+ }; a
}
3 n' Q1 W% h% K7 ]4 G六.测试
3 f" b# \6 [3 M! v$ j7 C5 ]2 x" [% S5 _$ e* t' I4 _
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
2 L" O& ?- T% d" R) p4 Z( N' ^4 |
<?php' d* ]: d5 c) t5 ?0 O* z2 |/ E
phpinfo();
# X0 x, ?4 I/ i* k?>
: |# n( B& N, @5 y在浏览器中访问: f ~4 g8 j: r/ K! e& g, G+ U
1 n* N. p2 a. C ]http://www.52os.net/phpinfo.php?id=1 正常显示。
, G$ J) |) N& K/ ?- o. Ohttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
7 y1 N; q+ p! y/ [% |http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
- a4 T; S/ h! q/ H m, D/ X# s& K4 D$ ~说明sql注入和xss已经被过滤了( p# G1 K t& e% Q. h6 `9 v' m
9 _9 r9 W3 |+ a& F1 I8 e七、安装过程中排错
0 m. S! d7 }: s3 x( N6 u/ d) I$ ^/ V! G& H1 m& \
1.缺少APXS会报错
l. M. V0 V! V5 f' y6 e6 k9 m& g0 p
0 C* b" q5 h& c" E Sconfigure: looking for Apache module support via DSO through APXS. U1 U, e' j* D9 o% m9 g
configure: error: couldn't find APXS% h, I* e! r# _+ G6 y
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
& g1 |2 r% j1 S% @/ S# Z解决方法:
6 r# _7 v) d2 q! _1 M4 K
4 K3 v, m; E' s2 s% } |yum install httpd-devel! M4 ]5 T4 |1 u% }% j4 [! M
2.没有pcre
2 w2 L* i1 b9 {% ?1 c, Q6 a% l$ P; l/ ]1 X1 e: J7 _4 E5 I1 n
configure: *** pcre library not found.
G$ ~. Y! d5 P; V- f" B: Jconfigure: error: pcre library is required. P( L# e3 z- U' N! T- X
解决方法:
% D C9 f; J; q$ {/ A
# @; c+ e' e) a* S1 X9 ?yum install pcre pcre-devel! i# B2 p- t' G/ W- }
3.没有libxml2
$ O, l1 _9 r1 |/ u$ O. C
% v1 f7 B( s) E% i5 r0 k. U
e+ q7 y3 Q/ D: m( b- Z, ~7 ?1 }configure: *** xml library not found.9 m0 K) D. j, w; E! i7 C) w4 i
configure: error: libxml2 is required& l/ Z, k7 G: u! j
解决方法:
: i5 ~9 X) o' ^2 `7 z. E+ {
" h! r0 Y* i2 q# \* Uyum install libxml2 libxml2-devel
0 s$ r& z3 X; Z4 ~0 |4.执行 /opt/tengine/sbin/nginx -m 时有警告, j# e1 o) J. B% r
( R) P3 |+ N6 dTengine version: Tengine/2.1.0 (nginx/1.6.2)
! j `" p p" G- b0 J1 Ynginx: [warn] ModSecurity: Loaded APR do not match with compiled!
5 M0 Z* a) z V2 }7 w原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log% k5 c/ T- v/ `" g5 E/ O
2 E. p& d" c/ Y* K& m5 c1 M2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.& z) w/ j9 q9 g7 Y0 F p, k \
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"! F% `' e1 t) Y' D' f( e! j2 W
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
2 ~0 E, m; |0 s5 ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
/ W3 d2 i" L' s3 |/ x$ B2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
5 D0 p1 |( y9 _- h5 O' }+ G- f9 y2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.7 p: M4 i2 ?% o
解决方法,移除低版本的APR (1.3.9)) M; X9 S5 z% G! R
`" \7 G G3 Myum remove apr
5 @6 c% u# `- w( h5 m) Y; g5.Error.log中有: Audit log: Failed to lock global mutex
" I6 ~; p+ D. c2 n5 l+ D, j4 n4 b# i4 `
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock , s) v- m9 S6 Y- G* q7 G
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]- e4 v) t6 \0 |9 M* B
解决方法:
5 |7 E1 g# L9 K* _ I编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:; s- h4 d! Q1 q
, e1 `2 `) j% ]' J1 _1 h
SecAuditLogDirMode 07771 ?) O& v5 `# P$ ?7 h
SecAuditLogFileMode 0550
% c' R, h7 H4 e! Q1 vSecAuditLogStorageDir /var/log/modsecurity
- C% E! m3 R% Z" w' y [SecAuditLogType Concurrent
+ Z1 [ O* V: t8 T9 j& z参考文章:
' t; j9 r! w! s/ F) uhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
) O' v# r0 h8 @. Vhttp://drops.wooyun.org/tips/2614 |
|