|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。" p* m9 n& S0 @, D
8 @! U6 D+ j- j' @/ W6 ]
一.准备工作/ u- ]8 F$ q) b
( z3 r( ]+ h0 w( ~2 Z
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
) w4 r( r# ]/ ]% a0 F% u+ b; u0 {( {* |( Y& o% z9 |
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz& o. K N! Q0 w; ]6 m
( \: }0 Q: W. D7 }1 i' U9 ?6 v
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
. ~" j4 i/ \2 h
3 j9 T8 C$ k( n/ w. Z* h7 cOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
1 p1 F" U# e0 Z0 z5 k" ~
1 l7 Z& W( `4 \( V8 b3 c依赖关系:
) M' N( {" U4 J+ Vtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
6 O# h2 g$ i4 Y5 o% v6 }9 n+ U% l8 \* w( n
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
9 G. U- v k) I1 k6 ]4 n; amodsecurty依赖的包:pcre httpd-devel libxml2 apr1 ^ g" x6 p: q$ o# j
% O0 g& `2 f- K
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
' r! }4 V. h8 a! W二.启用standalone模块并编译
# J! W$ B" ^/ |( U3 z# a) K, k
! F9 p3 A* D3 M$ Q; F下载modsecurity for nginx 解压,进入解压后目录执行:
. q S& q/ c/ i+ v+ X
% |. R5 I9 P, G2 T/ m# b./autogen.sh6 x2 H w+ f8 X. w8 u+ t
./configure --enable-standalone-module --disable-mlogc6 v3 @7 ]" X' M0 N) n
make / d$ Z3 S, T0 `) ?5 p. L
三.nginx添加modsecurity模块' @, \( q# [3 y
7 G0 @9 O& c" P$ S+ D# i在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
5 R; r; P/ i5 w* G1 w \5 Q
, y7 w O; L4 U+ C./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
( ~" a+ K0 M, J: dmake && make install/ b% U4 ?5 m# s; j( Y& r
四.添加规则4 t" W$ _, h+ @
2 E4 a/ M& o( J( T P/ e$ c8 Vmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。: x2 M' i5 ]7 R' R
1 m1 Y- ^9 p8 A& |# y1.下载OWASP规则:
* ~( n* E3 X" P k' [( G$ |+ u' n; k
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs) J+ A$ f1 H f6 d
- A. h7 d0 _8 `# a( `mv owasp-modsecurity-crs /opt/tengine/conf/
) B# C* C! [; Q
6 D( {0 L/ z6 @/ ycd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf% B( P1 m4 K" i+ o+ A
2.启用OWASP规则:. a" F& H5 f$ ?( ^0 r) V
% O- c9 G# z e) H/ a7 C
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
/ W+ Z6 E) d" u: o5 o/ L. t! K2 A% D3 O% W1 ]# O, u
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on9 a! |! I. z$ D2 ^0 ]' k3 L4 _4 y, x
3 @4 R, [: ~* \) i& f
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。3 ^' L1 ?3 x% g- \. q) G: i' [
1 p% ?+ {2 P R' W" U) h. WInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
8 E Z1 p! g2 U' b' `Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
2 d) A) E2 m" R; xInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf8 R. f+ G$ ~, s' k' I4 E. z2 K3 ~) t: v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
% ~+ }0 I4 P0 [Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
' z( y5 b3 g7 C$ T7 |Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf" |4 m; z9 h, f" e
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf F* _' ] O; A' W! \. e
五.配置nginx1 A: O7 j7 J; H* C: S
# W9 _0 y: J7 y- z7 V1 s$ \: _在需要启用modsecurity的主机的location下面加入下面两行即可:
4 U, \0 I7 s# e$ @
d5 u* Q |+ o4 XModSecurityEnabled on; d! e3 q3 N2 s, }: q5 {+ i! S
ModSecurityConfig modsecurity.conf;9 K) m/ v8 t) L% ]% A# o2 F1 B
下面是两个示例配置,php虚拟主机:
. {# @" L1 E' `% W" H% b9 s: u+ Y7 {" g5 r% C0 A+ G. Y
server {
7 t* Y+ `+ P. K$ X$ H8 Y1 U3 ?# D/ {1 m; S listen 80;
( k h l% n' [/ X5 F% K- _" @ server_name 52os.net www.52os.net;5 h( |2 b* ~; N
% P5 i; }6 q2 Q# C. Y/ `+ i2 c location ~ \.php$ {
# k5 }, S; f! K ModSecurityEnabled on;
7 i0 K% p% B2 r ModSecurityConfig modsecurity.conf;
; v7 K5 O {; ^) m- {1 f' @5 h; g4 F- n6 ?( V/ ?/ Z* p4 q+ d- P
root /web/wordpress;
/ H0 T: Y5 {2 h& c0 h8 q index index.php index.html index.htm;+ M9 n& }& T* K- G: I2 y4 n3 a
- ?4 k/ q; D/ a3 N/ ?' W
fastcgi_pass 127.0.0.1:9000;
9 X& I( B/ f. B L$ i fastcgi_index index.php;
, K; b6 A+ H; Q6 ?+ ^6 s fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;1 p) L7 O( ^ f; e3 y: v
include fastcgi_params;9 i. ]; I4 Q( F+ s I" b& u
}
" t% A) c/ d( j$ l }
! `; Z* B, ~/ \4 I e+ ?* I) Hupstream负载均衡:) [/ u* e8 I# ~5 }
0 O# h x4 G7 o: oupstream 52os.net {
: }, s+ \9 p: E% r g; C, D server 192.168.1.100:8080;% F3 l& e$ S1 Z/ g/ B' Z% J
server 192.168.1.101:8080 backup;
6 s" s- g6 s/ n& C* x* `. `2 ^* L}) T0 X$ ~8 O7 k
5 s7 N8 c1 @. E' V+ G7 Zserver { \+ [2 p) R H
listen 80;4 z; E" A' o- A
server_name 52os.net www.52os.net;# n4 a1 j2 O6 ~) o4 p2 B8 y
: ]% e3 A6 L1 k( ^6 M9 ^
location / {
$ H; D& W. H- e0 q' F ModSecurityEnabled on;
% {( @0 Y# Y' v$ O ModSecurityConfig modsecurity.conf; ; B2 R6 }! Q. D/ z! q0 ~
% v6 v; e8 q: B0 C, }# J. A proxy_pass http://online;/ }) w* o: i4 E+ t: r" T
proxy_redirect off;8 a0 Q1 T ~3 q3 ^5 J
proxy_set_header Host $host;5 z) \% {# E3 }. e& C
proxy_set_header X-Real-IP $remote_addr;, L: D; s9 ?! n k* g$ y6 [8 C: [
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
' X1 X+ K7 C: Z8 d! ~/ | } v) ^: G4 H O/ C, W
}
6 m1 O* @. x ]$ c6 H六.测试! h+ G! M8 c1 T c* ?8 R( u# t
4 D7 F9 D0 M) D0 F% Z3 u+ D3 [% X
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:9 r4 n1 c9 K' V" T" o2 S
2 v7 {: K- {& O( b1 F<?php
5 h: C: ^2 ?! @6 [ phpinfo(); . y5 f; _+ T7 z# r# U
?>+ |! L! A( q4 {1 [
在浏览器中访问:
i+ p+ W4 Z6 k4 Y' m, S" P; d8 J" w. ^3 s, i% j# W
http://www.52os.net/phpinfo.php?id=1 正常显示。
+ r/ B) j0 \6 k2 G8 dhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。. r0 X" I3 U% n5 }$ D* W' u+ G- @
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。' z( s' s7 f: q+ d. U5 l& o2 q+ O
说明sql注入和xss已经被过滤了
3 ~# w5 t9 j+ k( l( p7 N2 B/ F/ n7 _& V
七、安装过程中排错4 `. O$ x% F6 }+ V: B9 |
! }& {2 K& A' N9 b! |( C
1.缺少APXS会报错
; s" }) c) {7 P, v$ N1 M: o1 z% y3 f; S7 I
configure: looking for Apache module support via DSO through APXS U( y0 x: _4 p3 F- H+ U' ~ {4 e: M
configure: error: couldn't find APXS. _/ t, p% ?3 O& S9 @2 q5 n7 k# u. I
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
' t( c5 c, m# t0 G解决方法:
+ f# ~. ~' R, j0 g$ v8 O' ~0 L$ |/ P; H
yum install httpd-devel3 O# y/ M. c5 d3 H( ]- J w3 a
2.没有pcre- s: w* Y4 F( O" O0 R
" O( {4 D, d, c/ bconfigure: *** pcre library not found.
{9 v9 v8 U0 c8 P1 D3 y' m1 vconfigure: error: pcre library is required4 O+ w+ _: `9 L: Y/ q( h& d
解决方法:
4 ^1 |" k* K/ F/ ?7 D4 q, R
" c5 A2 @# J, L- {0 oyum install pcre pcre-devel* V& v }7 r7 y! b5 `3 L9 D7 ?
3.没有libxml2$ p! w$ h# [2 v( f
5 ^% n! c: y1 a' `9 S2 M
; U5 }: b1 Z Z0 W# R# @& K5 |! Fconfigure: *** xml library not found.- n {% `6 o+ _' u6 B( t, ^
configure: error: libxml2 is required3 {9 X1 s# o2 w1 a) a* C' y
解决方法:$ Y" O7 `% x6 O
0 c; E: @3 v4 w! ^3 s
yum install libxml2 libxml2-devel% Q$ {1 E, A% S, g4 q# A/ b8 ^/ r: c ~. \
4.执行 /opt/tengine/sbin/nginx -m 时有警告
8 E- \0 L- D3 [& D B# e8 h5 S9 ?3 L' a$ Q* y8 J. K
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
! H1 c/ B1 k3 I4 hnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
4 `6 x3 a% b, |0 y4 m: A原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
0 ~% g) I- u( a
4 L5 X' V% u6 f" o6 f/ D2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.- F B$ v0 [: `8 p" T8 _/ a* c" i
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"& r6 m/ ~9 z8 n" s0 A. h
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!6 }, s& w: p$ f- X8 d# m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"0 @9 ~% l z- T# E _2 _# @
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
2 u; T+ x5 v2 M( c' k3 y2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
% A1 b, d- a" G8 ]4 g4 A/ [, W0 a解决方法,移除低版本的APR (1.3.9)
7 \; p8 L/ f' l R8 |3 g
. @* h8 k* C2 l& T- [- xyum remove apr
; D4 y @2 @+ q& x) D% Y K6 X! G5.Error.log中有: Audit log: Failed to lock global mutex. o; q1 K5 h6 q6 l9 W, K# ]3 n
/ ^2 L. C5 S8 K" c+ u% P2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
) X( Q# k; i# D% J' o3 ]global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]4 V8 w ?: y. ^$ R. R4 j6 y. Z
解决方法:2 ] s4 ?/ v H( Y# W; }
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:) y: \' N' b" `" R: E- F) v
$ A/ e; o. j: B4 `8 N! y
SecAuditLogDirMode 07776 F$ S+ R; y3 U; E8 a3 N/ q# N
SecAuditLogFileMode 0550/ \8 n- w* M' G8 C
SecAuditLogStorageDir /var/log/modsecurity% w/ E) e7 n$ c& S# M; J3 u+ u
SecAuditLogType Concurrent
) T! @% \2 \* h6 n e* V% i参考文章:
: L: x0 J, v2 f% K5 _+ d" I) zhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX8 |, {$ O- Y; a2 q
http://drops.wooyun.org/tips/2614 |
|