|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
# Y, ?* T, U: k) v+ Y2 x% b
+ g1 e/ O) s4 e7 `+ b( w9 @' `一.准备工作1 j% t: ?8 z& s' M, f6 @
* r: S; O2 {. v- {系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.00 _% A# r8 x: {, M I
5 E2 e; G4 V" X% @tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
* q. W- v8 T9 P7 O5 D
) V+ _( N3 U, n! E+ Umodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz: L, D# H4 m/ f {
; N1 ^! I$ l5 G* g3 ?: F
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
: a. |; W1 c' M# [. @# ?& _7 v& _$ l
依赖关系:& b e' R8 S( f/ l7 ~* s
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
- P' ^6 T* o& L; r! ^$ S& u& w+ b' O4 z
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel4 p- ?8 _ K5 P5 I. v5 c1 s
modsecurty依赖的包:pcre httpd-devel libxml2 apr3 N5 x% J7 t* u4 _1 R* j; i
6 Y. [% G9 F, f4 u! G |5 `yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel7 r. d" Z" y% I0 {
二.启用standalone模块并编译
* C6 @0 B8 B2 _
1 i* p2 L7 O0 i( C4 T下载modsecurity for nginx 解压,进入解压后目录执行:* {! Z$ }7 [3 T; B8 a& w& _
6 V3 a1 H. I2 N: k7 Q( A
./autogen.sh# y1 H7 o( t. _+ V3 U1 r! t
./configure --enable-standalone-module --disable-mlogc; h: R- r P2 g; N' y3 V% l
make
0 ~7 l5 w& f) G4 k5 E0 m V* j' ~: d- V/ Q三.nginx添加modsecurity模块
; q, X$ E4 q1 c. N: b2 a6 ` H" D H6 Q! Y2 m
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:+ `% c9 b) B$ f: s" D4 d
& r3 ], ?- p+ C/ Q( ^
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine0 j. M0 O9 K8 i" e' u \, B
make && make install
9 M; V, R1 a/ E7 k1 k2 F四.添加规则
1 Z4 J) r. q+ H; X' h5 f \# X. `2 b: d! C: n8 j% U. G1 ^) `
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
" F! C* ^# H/ t- o
( Q0 s$ W' _ N& X7 G. b1.下载OWASP规则:
9 @2 C* { c2 ]3 ~
, F9 X1 h, T5 Q2 Qgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs8 ?6 K! a7 A5 X+ W; R
; |& q" U9 Q7 q$ O. M. m* t
mv owasp-modsecurity-crs /opt/tengine/conf/
0 t7 Z& l1 V) j9 J" g* X! Q" h
2 g' ?; C/ Z7 ` v! D8 jcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
- C5 |1 n/ v9 u* d% E: a* C2.启用OWASP规则:
7 k, E$ G! I0 q% r' [7 D9 Z) `! U ^5 w: Q! a7 W. x7 D( b, K
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。- X" W: K e$ _: ^/ g
- r/ M' v8 T: L5 W+ N+ S编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
1 G6 i, [8 I4 X- Z# T, E" c6 D$ A9 q$ V
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
) n9 P- p1 l3 S7 r: i; Y: W% V e* `; c. q2 o
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
" p7 A8 x; X, G( V8 p3 YInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
k0 ], x4 p3 m5 \Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
7 L; z3 v2 ?5 t- D+ C# F5 f' d. _Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf4 ~0 j; b, s2 ^ X, T/ }+ q
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
" d' ^" `& ?- d% p% uInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf! u; k" Y& Q* m, [$ I
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
% t2 Z5 o, U) V4 a$ ~五.配置nginx
6 X. S, P( o E- i1 k. n/ e! Q( ` c2 D
在需要启用modsecurity的主机的location下面加入下面两行即可:
! I7 U2 g# ~3 ^4 y. q! t( k0 J. K! q2 C% t
ModSecurityEnabled on; $ z, }- E4 `- ?8 `
ModSecurityConfig modsecurity.conf;6 P7 L" M7 V+ D$ \, ~
下面是两个示例配置,php虚拟主机:9 p! R" b: j6 P+ P2 D
! L0 ^2 n( ?) V0 b+ F3 p" hserver {
) h- ^7 K4 ~7 L2 C* E) U listen 80;
4 E) Q; Z9 G U7 S4 \* Y' ^4 ? server_name 52os.net www.52os.net;" X; n5 @. l% V' U- p0 e! d
" C2 E! I* Y2 x; ~+ i) y
location ~ \.php$ {7 o# _4 ]- y! _; F- i; |8 D& m- v
ModSecurityEnabled on; 5 \5 l0 o% n; J& N. R9 p
ModSecurityConfig modsecurity.conf;
) L& f9 a9 l; {0 E4 L5 t
% y( y5 f$ W# D; h9 Z* i, \, G/ z7 d root /web/wordpress;
* X; B3 G9 h/ i- W4 D7 `- G3 w& o index index.php index.html index.htm;
( x' K# W9 o# W; p. p7 [# }
9 A# @* W- j! v( E fastcgi_pass 127.0.0.1:9000;
; h t; z# i1 y fastcgi_index index.php; c* P4 a2 Z' |) X) M3 z
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;- K$ k/ K/ j; K) `+ c5 n
include fastcgi_params;
' {3 d: o! `& L8 m }
1 R5 [; O. Y7 F# v2 v }4 j" g# u' M8 `9 d5 {
upstream负载均衡:
$ z% b0 Q' R! y1 B' i
8 |* h/ w( ]4 k, f+ P2 X9 t8 {upstream 52os.net {8 ]" B& \! u; h* P1 h+ l7 r6 O
server 192.168.1.100:8080;
5 @$ l) a8 V# j+ g/ R server 192.168.1.101:8080 backup;3 I: b; F' h8 k
}9 \( i) N- g1 b5 l1 x+ |9 I
7 p _, I# a. M8 v- ~
server {
4 m% @' V' b( w" L$ G$ hlisten 80;5 C! j) Q; j3 w
server_name 52os.net www.52os.net;2 E7 j7 }: m# v( }( M2 H
* x* t4 B' B' W9 ~: _( r
location / {
0 d; ?: y$ k/ q) t8 {3 b9 M ModSecurityEnabled on;
- Z9 J; i1 d# H4 w5 J; { ModSecurityConfig modsecurity.conf;
; u( N# B; P) L) X- h$ D) [; m" w; W/ |) H+ l, ^# [
proxy_pass http://online;( t" G& w* V! o
proxy_redirect off;
n+ D# }* r6 w7 ^ proxy_set_header Host $host;' u5 i1 p: u* k- j
proxy_set_header X-Real-IP $remote_addr;
9 ]& A! X; J) k% _0 M2 Z* a proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;/ r. |9 j% I8 |% [
}! m! N" _9 ?7 B# k% Y7 f! i* b. }
}
, L% w! b' v4 E& D4 E+ m k2 e六.测试
+ J. R4 ]: E+ V4 Z! _7 F8 a" \9 |
- c" B' V% [0 q6 D我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:9 ?/ C+ [& {% I) ~ K
1 s0 S X/ k2 k/ h7 w# P B) m- v
<?php
1 V$ i/ \9 g. ~# q1 i0 W phpinfo(); # x2 R6 b, c& q; ^3 y) t& E8 Q
?>9 r T9 k2 b& K, p$ x: F* u% P
在浏览器中访问:9 z* q5 j6 S _
1 w* {5 s, ?1 ~" y! hhttp://www.52os.net/phpinfo.php?id=1 正常显示。" D% b+ R! c0 n @# N+ v& |' ]' z3 V
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。1 U, K& W2 N7 ~8 E+ k
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
# b6 S, Y0 G3 ~* u& V说明sql注入和xss已经被过滤了9 e- Z U( U2 B. Y. B; t. P0 t* G
$ o8 X G) H+ |" _, ?1 D+ m( w七、安装过程中排错
) f; U( N9 H5 L3 @1 y+ ^
' G- j1 V3 S& m. u- O1.缺少APXS会报错3 b) U+ c2 T1 S) c. p Y8 _
. p& {+ {1 ]0 t" l8 w2 d" l; A) U
configure: looking for Apache module support via DSO through APXS
- J7 J' e, U h4 D9 Bconfigure: error: couldn't find APXS
, u. n0 Y# Z' ], E$ ?apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
+ W0 P6 K) k3 k' I, D) E' b解决方法:# F$ E6 k+ E9 U$ a/ \! M# X* b
+ Q; X2 l, h$ N. a$ a6 z2 Y$ t
yum install httpd-devel" h+ Z3 w4 S3 z! p8 i( p; b
2.没有pcre, P5 n) Q: d7 Q
$ ?' t) _8 y3 {3 p2 lconfigure: *** pcre library not found.- D, j; l3 |8 |4 }
configure: error: pcre library is required W& C# R U' b/ b, H
解决方法:
6 O9 Y% w% @. w4 I% ^" E' G2 H# D8 ^: I( i0 k
yum install pcre pcre-devel
w# @- @+ {; S6 N3.没有libxml2
: m u3 @7 f/ F; S; p0 F
; _# w7 `% ^- q N2 S' C) Z0 \0 T- t2 Q8 C& |* \, H G/ \
configure: *** xml library not found.) c; F) J- `1 S
configure: error: libxml2 is required
c! g9 F0 L# Y1 A8 j解决方法:
7 P2 ~" u5 y3 x) E1 `* X5 m1 R
) j* w: e8 I4 ~* T* ayum install libxml2 libxml2-devel8 ~3 n1 b) A D& f# u
4.执行 /opt/tengine/sbin/nginx -m 时有警告
: {, q8 s4 g2 y! \( {& J6 H1 G# b
. M; g3 |* J( Q% {9 {7 }" |1 STengine version: Tengine/2.1.0 (nginx/1.6.2)
1 c8 J c+ t8 `. D& pnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
8 F8 Q8 _# Z2 o8 U/ g: B% c原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
! K5 U! I( v% a
+ z- j/ t% K0 O3 G3 C2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
- p% j6 _' k9 g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
# n) K/ H" Q/ b# ~9 |. A4 h2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!, T6 u% M* o( {! m' h& {
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05" R" }0 w( V- ^- K# w5 n- m: \
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
0 X: n& n ^! c. E) k2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
- j8 Y# Z4 W" m- C解决方法,移除低版本的APR (1.3.9)
% ~/ O2 B4 m+ \. [# k# H5 S4 n K
/ L, u. j/ l7 r' A K& k" W: M$ b0 Oyum remove apr
* ^7 G! V7 E _8 {2 Z/ R7 g( U5.Error.log中有: Audit log: Failed to lock global mutex
% ?+ X4 v! O2 m* j z
4 i: r" q5 }7 Z3 n. D7 q2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
3 X+ }: ?- m+ H. A! m) ?9 [2 tglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
2 d6 u0 o$ l7 Y) t1 n; b/ J2 B9 \3 v* Y解决方法:! [% W) r. U) l9 I2 K, \5 W
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
' Y. R/ q- C% u( x0 [ m% V
" s$ i) n0 c& M6 H' }SecAuditLogDirMode 07776 R* l1 [" t0 N2 U6 r7 N0 C. a/ ]
SecAuditLogFileMode 0550( e6 k( P! k1 |4 _7 U0 ~
SecAuditLogStorageDir /var/log/modsecurity
3 Y5 t1 V- z" @, t! FSecAuditLogType Concurrent
0 ~4 L3 L) x7 Y/ m* {1 ^8 R2 x参考文章:
- |% u( |3 i' j) {% Shttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
p0 A; r- U! q' W9 r2 Ehttp://drops.wooyun.org/tips/2614 |
|