|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。$ _- d* m2 [& p1 u8 \- @6 t
" U8 r9 Y; R3 g) D
一.准备工作
6 t8 g O3 Y( D4 T
- {, O! D! Y- U0 D系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0. t5 d$ ]6 n; b4 w' N* d
* y+ {' {$ ?: ~: _' S6 j
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz+ j* k5 C8 l5 L1 Z1 |- L, \
6 T1 } R' C% I) L$ E0 j; M
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ d1 Y7 h0 B+ p" I
5 ?7 O7 L7 Y- p9 C3 Y( vOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
1 L0 `5 _- i) f2 M
' _" ]$ @+ V9 s依赖关系:8 f. A h9 k. h1 E1 _ T
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
( U: n9 T7 v8 s- t3 s8 \, C; y e% ^2 G8 u; v& Z
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
1 b/ k2 r% d' f) ^' dmodsecurty依赖的包:pcre httpd-devel libxml2 apr
- c f \/ B: s3 X; Q9 M" j: r: G2 x ]. ~. _; |3 Z
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel) T e' B, `0 N+ _! K/ ]) v7 K' O
二.启用standalone模块并编译
8 m) T5 C8 I* l: Q8 k/ p0 z: R7 i4 q" i0 q
下载modsecurity for nginx 解压,进入解压后目录执行:# ]+ B: T) Q6 A L
! ?! H6 S5 C( X8 k8 l' F./autogen.sh2 N" v4 L( A& B" ^5 b6 j! F
./configure --enable-standalone-module --disable-mlogc! M, ~- r7 m, O0 a/ \
make
l) [$ v2 }& v: f5 c! \三.nginx添加modsecurity模块6 S O; ~# u/ n' c6 o" g
* a7 X, R# ^" O4 N+ ?; l
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:0 N+ U: p0 _$ S! X; v
: o% t6 J+ b, C6 {- E3 d./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine7 d- E3 _: Q. V
make && make install8 s! n, D$ [, Q! V4 l1 D
四.添加规则
/ {7 B, p( g5 h( ~$ x4 z" l F) z8 s1 K. F! t
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。* h7 `7 v: _8 X* m, Q2 k
& O8 v. p( P0 E" r
1.下载OWASP规则:
- z+ E3 V8 s( p K, j8 B% ?. |2 {! k$ {# N( ]* r
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
# b& p" b( a% r8 w! U/ x) S" \7 _5 c4 t+ O' l
mv owasp-modsecurity-crs /opt/tengine/conf/5 Z" H; C. v% Z+ L7 |) h
3 l# q4 e+ Y4 R+ q8 y2 `6 {cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf1 m3 Y5 J& a7 U3 A. Z, \
2.启用OWASP规则:% a$ T; q9 j" b% B1 ~5 o5 G' \
. B) j# f' I( m9 V3 a$ K1 y
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
1 ^# Q& s$ F% ?9 I( p8 U6 \
9 _3 W; z3 Y- j: A4 _6 h, s$ [编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
9 u) ~* _* X5 y# k! j
: H/ ~/ G, z+ Howasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
) ^/ C6 D9 v% F! p4 Y- e1 B/ v5 d5 M- ], W1 ~0 O
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
! T# A* o) I6 j( P8 {) \ WInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
+ W! w. J$ U# X& SInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
. ]9 O7 X$ C: K% n& eInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf3 g$ \; g q: i0 i( }3 V/ o
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf: z# b4 i0 V9 m1 d3 s& m3 `
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf- J1 h0 @7 h3 }
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf9 g; j3 a, L/ m% ?
五.配置nginx% j7 N' q& d5 k8 p5 J
4 X6 w* j, R8 u/ l8 }( g
在需要启用modsecurity的主机的location下面加入下面两行即可:
: y) ~1 X) k! P I2 f8 H4 E9 u) K& u
. A- }& j0 i. @# `: G3 Y- c p8 j: fModSecurityEnabled on;
q! Q' ~2 m& s- l7 G% QModSecurityConfig modsecurity.conf;
2 [8 N0 I7 i; s! f2 i7 t下面是两个示例配置,php虚拟主机:
8 S$ U* D7 R6 w/ Y: _
4 a7 ]$ M( C# J- \server {
8 f. [" X) n" Y! w' w listen 80;$ W; \" J$ O( O8 z) X6 e1 ?
server_name 52os.net www.52os.net;* J) m3 d! N* U( f& b1 H/ y
; A, P5 z' q- T. N/ h( L location ~ \.php$ {" a' P/ M' v2 [0 g5 d; j
ModSecurityEnabled on; ( v+ K9 M6 v) h: U
ModSecurityConfig modsecurity.conf;
" a4 `/ I) p2 \+ K* k# s; b9 O- }
! _4 A+ D# G5 o root /web/wordpress;- k# C/ o s# n3 y
index index.php index.html index.htm;7 y. d+ }3 E2 ]1 |# b, N% Q5 g
" w P4 W- {6 c' F
fastcgi_pass 127.0.0.1:9000;8 Y" O2 q# s- E; Z" J3 p
fastcgi_index index.php;: \' E# s: |# r4 d1 `3 J9 v, B; R
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
. D) `$ y& A( v2 D+ N* Q E1 \ include fastcgi_params;
$ A6 }% U6 \3 I5 S$ R6 I2 g/ Z7 @ }! Q6 t5 a& N# m2 t9 I' ?4 ?0 M
}& y9 Z* ?- J& h" J) A3 `9 X1 F
upstream负载均衡:3 t1 j$ J1 E0 ?1 E" \& u1 B! K9 @
* L4 h' x1 n0 D; C; {
upstream 52os.net {* E) ?. I$ i8 F4 H3 j
server 192.168.1.100:8080;/ f4 J2 e9 C; x1 a$ ]: ]4 X) k+ e4 Y5 T4 t
server 192.168.1.101:8080 backup;
) E6 E% R6 G. Z- m# E}9 v1 E% f7 v5 S$ M8 U
) j" a& Z B; }) c8 c
server {* u) {2 R- v5 w; _$ `
listen 80;
A J$ b" `# ?. ^/ p0 pserver_name 52os.net www.52os.net;5 z4 a# w: E% V$ Q" F
; P- _* m$ r: W) h. X. Wlocation / {
: ^" _7 ]$ \2 ~/ t% U ModSecurityEnabled on;
8 `4 [; E* E6 @8 z# S# z2 V1 F ModSecurityConfig modsecurity.conf;
8 O T2 ~) `2 U8 |! y% j$ m v. v, |$ A
proxy_pass http://online;) J* U8 C& N* K# w4 }# z& |6 e# J
proxy_redirect off;
: O& \, ~0 C% T/ w% G7 ?/ Y: Z proxy_set_header Host $host;, E4 k3 A0 r' ^
proxy_set_header X-Real-IP $remote_addr;; J0 a) o( ~6 N# o0 q
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
1 S' k0 C5 \) D( F, p+ u }
8 A* X- y" b& G6 y* R. e, A- t0 S8 Y: U}$ d9 W2 F' C3 a: J
六.测试4 b# H/ }& W2 K7 T& z
, P# H' u6 I8 y! b9 K! Y, C
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:' J- M* |( s. Q* h
- m w3 e4 C2 B
<?php
- M, w+ X) Y1 k. S7 D: t9 {. {7 ?+ K phpinfo(); A J D$ O7 `2 {$ [# B3 v
?>4 }# I$ e& E- I9 l
在浏览器中访问:
0 f6 S: ~& O8 h% @( h
# O/ O1 A' r1 {+ @" y# Rhttp://www.52os.net/phpinfo.php?id=1 正常显示。
1 O/ u+ w( E5 [2 G7 {http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
4 a$ F9 P7 a( i, s3 \http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。; B8 e5 p% a2 @5 @- m! \: p) r( y
说明sql注入和xss已经被过滤了5 F# }) i9 U0 _6 g
' ]+ A. p, R" O' D% F- y; z
七、安装过程中排错
6 H. H( C$ p1 t' Z- y: w6 X5 E. _ u) h: z# n+ x+ X; I
1.缺少APXS会报错
/ r7 ]6 A- @( K' x; X: Y+ Y; U2 U2 H% m0 V( Q" y) H) L
configure: looking for Apache module support via DSO through APXS. P/ R/ I2 O) O" K7 _# U
configure: error: couldn't find APXS7 R! ~7 G N% ?
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
! Y0 v; Q5 u# }: v解决方法:
9 a! H1 _5 M) J% M
1 b1 O$ I# v& J% ~. zyum install httpd-devel
% d4 u: l+ O8 M2.没有pcre
( G# `, ]" x; J: h% T0 A) b7 R0 i5 j4 `. A. S9 p
configure: *** pcre library not found.
4 V% Q3 j; x+ I( iconfigure: error: pcre library is required }# F" ~* T$ J4 p
解决方法: d, H( u1 A- J3 y% l
$ Q( f8 S! k2 C# k6 j1 L2 }
yum install pcre pcre-devel
: k/ g' a: h4 ?3.没有libxml2
* q* Q8 V' m# f/ q8 }2 H5 y% C, A8 M, [
# r M% }' b' {- ?* m U. @
configure: *** xml library not found.
+ K0 Y0 ]6 P% ~- oconfigure: error: libxml2 is required) ?" i, K9 R- H9 L, j0 T) h: j l6 c
解决方法:
$ S1 V M# K9 d/ B/ r- {" m' Q) L2 V
yum install libxml2 libxml2-devel
8 _: |) c$ J$ T% n9 d* \) k4.执行 /opt/tengine/sbin/nginx -m 时有警告* {; v# P: U( q: j) ?* W5 r
" r- Z& _1 _, }7 d+ \Tengine version: Tengine/2.1.0 (nginx/1.6.2)1 _" n+ u! { Z9 F3 a$ u% k& w
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!" ?' m' P2 v, i- T' ~+ a/ t
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
; x6 y' q0 U( n; L- o, m0 a" Z2 @* P2 r' d% w; n. M
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
$ M* I+ N. {# J' N. m2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
6 H/ n2 W% L) m" K& q2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!( u# r( v% ~4 t
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"" o/ s2 [% \4 m6 p3 Q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"1 J% L# Q0 r- G, M. \3 r& s/ ~0 k
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.4 x1 e" ^% B( e) V/ d& H
解决方法,移除低版本的APR (1.3.9)+ b* |) R( b; D7 M3 ^2 Z/ ^6 w
+ w- |9 \5 s/ T
yum remove apr9 v: T1 i0 W2 {: w
5.Error.log中有: Audit log: Failed to lock global mutex
) ^% s( N) X% `" v3 ]6 T7 w" M7 x7 o1 m5 b( ], q
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
& n: n% m# v4 W+ W& e' `9 [global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
4 |0 q) R/ o" |) B L! ?解决方法:
+ S8 g. F$ L8 } y1 L. o& U$ Q3 m# {编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:( U" I5 N+ [0 P
, D. b. q3 f/ E
SecAuditLogDirMode 0777
) B& d. T) T7 Z4 d0 H5 J/ ISecAuditLogFileMode 0550& c6 V9 n! ?: ?5 x+ ~
SecAuditLogStorageDir /var/log/modsecurity5 F4 A, U& W& c! H5 {
SecAuditLogType Concurrent! I+ i* Q4 }) J; G9 Q
参考文章:
$ M8 D* F" {1 E y& L5 \6 Z$ phttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
/ Q: c8 u9 W% Vhttp://drops.wooyun.org/tips/2614 |
|