|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
3 c$ @# W( u m8 h4 R( ?2 ?. ^# R- A9 ?0 K$ Z; K
一.准备工作0 f) U) z6 S& {) C6 Y; g
+ d! @1 t0 I) j" r系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
2 N+ @0 L( w0 [0 w9 n" A# \( G; Y/ u) y( ?& D2 ]+ U
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz5 z0 D% {! T( [" k+ [5 Z
5 d( K" Y" K& L' [modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz0 @" Q! E( `( n1 _, I
/ D, D8 B, t6 ^9 vOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs; M: E( I8 I! }: s* v% q1 f
: l/ h3 K$ M/ i( S
依赖关系:) r4 v, E# t8 c& x8 Z
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
" N6 r9 B# h) [
6 `7 Y4 q0 C# U$ cyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel" z! {0 R# E/ `: v( H0 J- S
modsecurty依赖的包:pcre httpd-devel libxml2 apr6 Q& R7 M1 I1 Y# ?/ B8 C1 }" c$ n
3 y2 v: x1 \$ C& t0 Iyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
. w+ v5 D' s( Z2 h) B二.启用standalone模块并编译) v5 M! c8 t2 G- I/ u& f* N
+ S( @5 M1 _& x) z$ G u3 x6 g
下载modsecurity for nginx 解压,进入解压后目录执行:
$ N* j( D1 \% s- J( N) o5 z* ~1 ^' Z6 H; I. l2 o. D R% p$ t" u
./autogen.sh
, o) i: S: }$ \+ _# j* Z% I) m# P./configure --enable-standalone-module --disable-mlogc
- @2 A4 y! T+ y1 K# F/ X% qmake
- c+ w; b; w' I. f5 x三.nginx添加modsecurity模块/ A( {; ^$ s# b$ Z2 u4 g! ^5 O
* i g/ R! `4 j# S2 L# v1 P- J
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
8 C" u0 r) h2 z- @# v: q
Y9 I" g4 k9 B/ B4 q& H./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine* }8 ~* m4 B/ D6 @7 u) E5 O1 ]) Y3 p
make && make install
3 c5 I, w2 @# p5 c3 ]& M1 x四.添加规则
8 U! D2 `8 b8 a" b/ f, `5 i4 {. C/ R2 X( B* O
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。! n+ g, Z/ \; |, t+ J( w S# q
1 i& W- x) i. S1.下载OWASP规则:" L* x7 U/ ~$ g1 c6 ?8 _% ?
% e! d6 J+ k7 Y# dgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs5 F$ ?, k1 r4 r6 L
( B- Z- b3 Y3 e9 L: Nmv owasp-modsecurity-crs /opt/tengine/conf/" D$ j7 ~* c/ r
9 K4 y5 e$ c* x& kcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf! O: ^3 B5 L1 P3 _4 p
2.启用OWASP规则:
" M( e1 o+ _/ s! m5 H/ I, {. V5 P
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
' c! g; W& H5 ^- T2 f( B( g' u% U4 ?# r4 ]/ Z
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on: v$ n) I- B( G
* n% b4 l7 Q$ N$ Qowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。/ }1 o. { T4 K' o1 x
( T* Y2 d7 [- i, x
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
* I( L# Z+ O$ g$ O6 s# y( G4 KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf3 f, p4 ]* K4 }3 d9 @4 X
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf/ F3 d, k4 d( m
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf4 f! T9 P' [' c K
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf2 y, M0 q$ p- B ?! C
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf {8 q e: q J+ k: g
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf @3 B* r+ U: y e$ R ]
五.配置nginx( q: m$ D% I- U
. _- V6 {# g% c1 q4 L/ |: @在需要启用modsecurity的主机的location下面加入下面两行即可:
w& J4 A7 c' S7 ~2 D! p) q1 q- t o
4 l6 o1 {1 Q: Z; x, J, ^ModSecurityEnabled on; ( }' b1 a. L! C' X
ModSecurityConfig modsecurity.conf;5 f5 f% |5 t" }7 Z5 l
下面是两个示例配置,php虚拟主机: c6 u/ H/ Q; E% o" D
7 f% E4 v. A6 r8 Y- p# K" S Vserver {" A0 d: {: r5 g9 v5 W
listen 80;0 L. \" R+ S# D( y4 I
server_name 52os.net www.52os.net;
& E: p% r6 U E' q! V3 }4 @ 7 t W; S: e/ P; k5 A
location ~ \.php$ {
/ A3 [/ f$ E( }" g ModSecurityEnabled on;
; i8 B. r: z3 T% V ModSecurityConfig modsecurity.conf;
# j# T$ G7 {; G1 Y# h r2 e* Z! v A
root /web/wordpress;5 B& p( Z' D( N L. t p& r6 ^
index index.php index.html index.htm;, \7 T- c; |3 l
( P6 @0 Q& U" ]
fastcgi_pass 127.0.0.1:9000;; n0 \8 D1 V, @) O _, N
fastcgi_index index.php;
2 F$ O5 |" I* F fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
. n9 k9 i5 A3 D3 v+ e) E3 s include fastcgi_params;
- V! C$ b, A' y! Y: F& X }8 F' C* `8 n4 ] c- l5 f
}
- U" d/ N$ o( c& supstream负载均衡:
# X' X/ z9 L& @
8 x' O$ W! y# [' j3 L+ a+ Nupstream 52os.net {, W7 g' ]! Q7 d* ?& ^5 {" }
server 192.168.1.100:8080;
( I0 j: n8 z8 m6 E server 192.168.1.101:8080 backup;
1 x# {) r) Y: n}) B: R% L* {/ J# ]4 U! {: I
3 H+ s: H2 P, O" e$ p7 H) ?# d7 oserver {
! i- n* Y+ h0 x( n1 f+ O1 p) xlisten 80;: W( u" n7 D2 v2 B
server_name 52os.net www.52os.net;3 j" t: {- k& |5 \6 O: U
) N' S# O; j" N% i$ h$ |% J$ g' F
location / {6 ~6 I" e5 J. B2 L6 R
ModSecurityEnabled on; . v/ d% Z- |/ M) D
ModSecurityConfig modsecurity.conf;
; ^7 i4 b2 }4 o1 e* C7 p! W) K5 |4 O4 T
proxy_pass http://online;
7 d' H7 Y5 T/ [) u" i0 f proxy_redirect off;
; ]$ Q7 l. j5 d) e7 q, ^& f proxy_set_header Host $host;
& N# |1 F6 p# T' t* M+ y proxy_set_header X-Real-IP $remote_addr;
5 |, Q7 `# \/ _# ]! v; J( L* w proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
4 o+ L7 ^+ L2 H/ Y; G# ~. F) Q1 O }
) \& j5 V! Y- o* w$ ]( P, @}7 T& V @; Y- ~/ j0 z% w1 u5 v
六.测试! }% C R- b0 w: o1 ]
" V( d; F' ^& ~3 y+ w/ {0 K6 M: j% Z# q
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:/ x$ Y+ v7 d7 d+ E( V1 k
, U% m3 q' a6 G- b, F<?php
: F: f) i+ \) O4 ^ phpinfo(); - L W' y! X1 k4 U
?>
- u2 P' ]) M5 I8 d在浏览器中访问:# Y* F' t4 T+ A( k; A
. G) l& d* x/ ]1 {' a
http://www.52os.net/phpinfo.php?id=1 正常显示。, }) R6 A- G: Z. @
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
+ z: C" t( i, Nhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。2 b& D" _2 i) Q- k& x+ {: n
说明sql注入和xss已经被过滤了
8 x, L, Y, X+ w5 g# w+ c+ ?9 |/ D j8 H" K7 O
七、安装过程中排错3 M8 S) }5 X4 y- L
, |3 g. s" y" T3 \: c1.缺少APXS会报错
& l/ s6 k3 q2 \9 U, `2 v" U0 t8 K/ A2 j. `8 D, i
configure: looking for Apache module support via DSO through APXS
4 S B, ^: q2 X" [8 u# R+ }% Yconfigure: error: couldn't find APXS/ c! {$ q; ]) {( z6 {
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。. e% ] Z, D3 |! G
解决方法:6 E/ o1 J4 S8 M5 s, b
/ n: `& M* M X" ?5 q/ d
yum install httpd-devel
" k4 w; y8 L! y2 F! v2.没有pcre7 e9 t0 |5 `: r5 [! c0 U% A
: h' o. w0 k; Dconfigure: *** pcre library not found.$ C9 a/ h& o! |7 R
configure: error: pcre library is required4 @# B' j" e9 M% V, {# s" ^: I
解决方法:
2 U O4 ]* z5 ~9 M
) s3 Q8 l: [) y3 K9 vyum install pcre pcre-devel
1 F3 @5 @+ S; z4 `3.没有libxml25 k6 t: I# v6 C
" N$ ]+ t0 Q: ]" t% w m
- t8 m2 V, N# N7 D. E% {- B1 M
configure: *** xml library not found.( p1 a! ?5 m) h$ G2 y
configure: error: libxml2 is required
6 k1 J8 c5 ]2 [* p8 H# Q解决方法:+ i9 e* h) ]6 r+ o
/ S/ X( n# @. c z: Q6 T) Z
yum install libxml2 libxml2-devel
- ]# ^/ a9 M0 m# _5 W4.执行 /opt/tengine/sbin/nginx -m 时有警告
3 @$ e- S/ U7 }- E! ~
( ?3 Q5 h$ \1 N" v, n/ i9 J+ k! ^Tengine version: Tengine/2.1.0 (nginx/1.6.2)& ]# Q+ G- h9 L. n! i. s
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!1 P6 u+ |* N" T2 t
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
% H1 P# D$ d+ k7 W& U4 V
2 E) U Y# Z* L0 v$ {: C0 U2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.) E. h, F! I6 ?3 f( p
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"! I3 o9 c) G9 z2 N f" E" [- r! l5 H( I
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!, ^# @4 R ~* z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
$ j2 k& N Y5 V# o* n" i0 l2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
4 D! G9 J* l& \; K/ ^4 _2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
0 m* w7 f2 Q( D) [* s1 J解决方法,移除低版本的APR (1.3.9)% o) c( Y% I' ]7 g5 f: G7 e; V
- Z2 X) d& S$ e
yum remove apr4 K2 e' L2 ?' \) _
5.Error.log中有: Audit log: Failed to lock global mutex0 d. f; @0 r1 G* M% y3 s4 V
2 ?( j( c: U: X0 b; ^4 Y! F
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ; d9 ^8 k4 a5 H# H
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]& u( d- w) v! i& o9 _6 n
解决方法:+ p+ A5 G/ o% }; ?
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
( q5 g% C( J1 Z+ O) X [2 f9 I9 V; q! |8 [3 @
SecAuditLogDirMode 0777
3 F. ^( b6 @7 W- B1 t, N6 m1 }SecAuditLogFileMode 0550& }8 u" U9 U( e# N' _5 g
SecAuditLogStorageDir /var/log/modsecurity! ~) G( g4 R! c+ U% |' K0 R
SecAuditLogType Concurrent- h Z$ M0 s! }2 }- C
参考文章:
# F* [; r# N. X# fhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX+ v0 e6 Q j$ i& n, ]
http://drops.wooyun.org/tips/2614 |
|