|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。! {0 s8 }' N# X+ {# t- ^
( Q# Z' {: B. }8 f9 m
一.准备工作) n; y6 L6 ?; ^( Y* t3 i, `
+ h2 l, b* C, I: ], u. U
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
6 K+ _/ d$ x, n9 @2 Z$ S
9 j" U/ U+ L6 n: C7 X7 L# |tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz4 O9 v! }9 o) t" r, N7 J4 A
5 T$ l- P# J0 umodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
A4 x/ n' h' L" u, B- b7 T, [2 h' A' c6 u9 Q" l, T$ r8 _
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs+ f5 \. n3 w( r9 F
C: L1 ~" C0 F* m% q依赖关系:5 w* S. B6 W' h2 O# }
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
, B; N1 g# L% [. B* z3 |6 g0 a0 T$ Z) F$ y% V; U/ d
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel8 e* b. ?$ B/ e9 s+ K! D& R# c, [. |7 \
modsecurty依赖的包:pcre httpd-devel libxml2 apr/ \, V0 {" g. \3 I( S& x
: O) x3 |: L$ O; Qyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel' K% `# F/ o% E# X
二.启用standalone模块并编译
0 G+ F. q! |$ I) j. @0 t J2 ~# m% e
1 t" g6 W/ R# ?5 I7 g下载modsecurity for nginx 解压,进入解压后目录执行:9 m/ o$ j" `3 c1 O) E/ q
1 W1 K r7 O! ~./autogen.sh% `2 i9 m+ ^0 |9 E# {: v; V
./configure --enable-standalone-module --disable-mlogc
0 M' |' b# x( R) o) mmake - A3 M/ i. B; q2 O" I; A
三.nginx添加modsecurity模块
7 p. t# M# C* _3 Q" N* V3 h" C! ?( n/ h3 s0 ?0 U- V1 B2 {% N- a
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
& u) v5 q; h0 s; p1 f
: S6 I a- J! J% u./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine/ x. P N' X1 i9 k4 s. y' H2 D
make && make install2 `0 ~/ H, E% ]- Z9 [8 T2 k
四.添加规则
# l$ J t) G4 f4 x, S
. z7 G* I3 J$ c2 N/ T# _( ~5 S9 {modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
: q6 m4 V' |+ @& Y4 F, r& k& g7 a. ?/ Z. D% _! G5 c9 X% ]3 L/ U5 O* ~
1.下载OWASP规则:
% K9 h$ s: J& i6 H1 G9 U/ i
1 }2 O6 C W) y+ S' Sgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs/ F0 X- a) B% t5 I1 ~
$ r9 Q7 q7 J' |1 ]) a, q0 h/ emv owasp-modsecurity-crs /opt/tengine/conf/
' Q% @% m a7 y6 h
8 N$ G! w% e& b, g3 Scd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
- Y/ }. }( _3 ]! K2.启用OWASP规则:& a1 }9 J A2 w. @
* X1 d1 u9 R! Y( w; y G复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
& Y+ J$ {; E6 s' P
1 I- J4 ]9 t2 L8 H编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
, |- w5 }, _$ v g5 \+ o3 Y% l% F. W# ]0 x! Y
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。' y: v7 {) L% G; [
6 @3 B) d8 J, C# i6 l4 y( u8 m
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf, V) v$ U r" Y8 |
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
( a1 V0 v ~) S( u) u, DInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
8 o" V4 o% w4 H( `Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
' C6 p3 S7 r! [3 b4 P& B! {- `Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf1 y' a* ]9 @; a
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
' w3 r2 v% X' wInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf6 u! |+ _* _0 L& o
五.配置nginx+ v. Z8 m/ K* s9 O) M8 X
- o2 `' {: l$ X: U, Z在需要启用modsecurity的主机的location下面加入下面两行即可:* S1 C* W2 Q5 X
2 u- z% z, ^3 aModSecurityEnabled on;
/ F8 C. p" Q6 ]) ^ G JModSecurityConfig modsecurity.conf;
( Z6 M1 F- u# I0 C ]下面是两个示例配置,php虚拟主机:
# G; u. z3 Z8 I/ @* U7 a u
" r& c) F+ A- e4 [- I" s6 [server {
1 k$ F k0 k6 y" { listen 80;
+ v6 m1 N. l6 Q/ ]- c4 d+ X/ d server_name 52os.net www.52os.net;
: d; A' W3 j2 Q: t; C* z / l" c9 }) r5 I$ b( Q7 n* E$ z5 S
location ~ \.php$ {5 h0 y8 |9 o3 J. _6 m/ \- S$ R* L
ModSecurityEnabled on; 6 y; u6 }! o0 ^/ A: |+ I
ModSecurityConfig modsecurity.conf;
8 X0 t/ u9 G% D) e! S4 F- u* Q
2 w$ \. @' M' R; k6 N' A5 |. p! v root /web/wordpress;2 O3 j9 |+ M/ Y, I2 X* |$ t
index index.php index.html index.htm;# x$ f) c% r- d" z3 }
8 d& f% ~! w" |" _% F2 b fastcgi_pass 127.0.0.1:9000; ?2 ]/ c' T9 Q' w8 u( x
fastcgi_index index.php;# t: W4 [2 G/ V3 W1 N5 G2 a
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;9 q- c* n2 B' }! G, p! g% ~! H
include fastcgi_params;/ `8 n! [; j$ P3 l( @
} ?' U9 o% Z2 a h! r
}: b/ P! p5 M8 x1 P8 `
upstream负载均衡:
7 Y' u/ P$ g/ Q% X$ X# D7 T- A& \8 [2 c7 I# T# f! F
upstream 52os.net {
% R* V8 v- |7 C. p/ q server 192.168.1.100:8080;8 x/ f2 ]2 k0 l. I/ b$ E' a
server 192.168.1.101:8080 backup;
0 j# \9 B* c* I2 t7 r3 U}
0 q2 J5 x5 P; P C6 I7 M8 M$ ~) ^& m7 Z, R+ R$ v* ^' w) E
server {- _$ x8 z; s$ b4 d) `- Y
listen 80;) U+ c1 m2 E+ M9 b
server_name 52os.net www.52os.net;8 p! @! s( g& K. a) s
- S: S- @( B# @7 Mlocation / {4 c* m- F# A- r; u4 b) E
ModSecurityEnabled on; 4 z8 X$ a5 M0 i/ d$ m- n7 Z
ModSecurityConfig modsecurity.conf; 2 k. w# N$ z a+ k* u1 Z" q4 t. {1 ?
3 w8 T$ Q4 p- U& c& A
proxy_pass http://online;
( N9 T4 r. U* r7 @. B% X. p! K y proxy_redirect off;8 I! X: Y w4 `# J
proxy_set_header Host $host;
2 q! V D K& C" n" B3 @+ B proxy_set_header X-Real-IP $remote_addr;
& r9 e$ X* M1 I proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;* t( V7 A2 F/ K) L
}2 G7 J+ N# Y/ m$ q. Y! i; P5 G
}
7 h8 e( [" n- C! a. B六.测试
; n. Y5 A9 i4 ]
: B; N, o' i/ y我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
6 w( [- t/ n/ P0 K
9 I0 Z+ j1 g7 w<?php# W( `6 o* P+ w2 _ H% M: P! {$ ]
phpinfo();
, s6 ]2 V; m! t* F4 C?>3 }. i( Z/ r7 h
在浏览器中访问:
; T! y/ n' k4 f/ ~6 ^. H# J4 ]+ H# `* Z
8 z1 ]( s, D) V( bhttp://www.52os.net/phpinfo.php?id=1 正常显示。
2 F) J0 Z9 f0 m8 Y1 p5 rhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
& V: Q6 C" |. Ahttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
0 }4 [# y Q4 g$ E% @说明sql注入和xss已经被过滤了- a$ u$ ^* P6 ]7 I5 a
! f4 n7 |; n9 f5 Y
七、安装过程中排错
& ^7 R3 Y( }$ k9 d
' Q x& k L" K1.缺少APXS会报错% F& r- S' q! z$ m
' [; j, M0 z) L& m' w% r0 tconfigure: looking for Apache module support via DSO through APXS8 @% g, v! a2 q/ Z8 }; P4 [4 k
configure: error: couldn't find APXS( u8 E+ G% M+ ^, |
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。( N% z* n& r: U# a! }1 |& P3 T
解决方法:
7 f0 I1 X* X `- k2 j5 y1 `* ~: p* A. Y; I/ I2 U/ p Y* C
yum install httpd-devel
2 s3 E# f: E# D, S2.没有pcre8 e7 A a6 A% ?
* Z0 D4 G/ Y1 W5 [% P3 ^( l! Yconfigure: *** pcre library not found.
5 L6 r( S8 e, Z% ]configure: error: pcre library is required$ `8 V/ V3 v4 c: h' }+ K) @
解决方法:+ z7 j' Z4 g' D4 p4 y
5 t% U8 U# Y$ Z" Ryum install pcre pcre-devel
7 z* R# V$ p0 e' D! D: M2 R3.没有libxml2$ L. m2 d, a9 O/ `$ z
! I0 Q) i0 B' g8 Q
/ ^6 N- t7 D8 H# sconfigure: *** xml library not found.
( I9 M" s5 e0 ^ q- bconfigure: error: libxml2 is required
& o' @6 y4 l5 `' f3 e: o% N解决方法:
5 ?/ O W$ ~/ l8 B
6 ]; G7 i0 E2 byum install libxml2 libxml2-devel1 ^% ]/ g. P; p
4.执行 /opt/tengine/sbin/nginx -m 时有警告# S( W2 Z4 n& I, q F
* W+ t& b1 ?$ z! M! }" fTengine version: Tengine/2.1.0 (nginx/1.6.2)
& m2 U, ?" h2 Q ?1 Xnginx: [warn] ModSecurity: Loaded APR do not match with compiled!1 L {3 I. v, H% e+ }1 N/ R
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
+ T B2 a& K: ]/ t0 F2 x' S
4 l5 [' f& ^, g* l$ K2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.) [; m1 C3 [8 h
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"/ g( m- C/ }! ~) A1 I$ N7 Q1 b
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled! i9 ?9 V: \& K& w; f3 ?7 q3 P
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
6 [. j, w% A, f4 U& n, T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
. t, A4 P# ~1 g1 w8 M7 b2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
/ p" U" M; p* r# K解决方法,移除低版本的APR (1.3.9)2 a/ \8 L9 K! ^' U8 A5 `0 c
9 B+ B* o+ H% y8 U; syum remove apr7 r' H4 R: s! m* {3 W
5.Error.log中有: Audit log: Failed to lock global mutex
, G- A+ e! Y- T9 A% F
* w0 U: {' e Q8 ` u C7 S6 J2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 8 F7 j% C2 K' i9 X1 O' U; Z# T( U
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]0 B- c: K! L, r5 w& y: r$ [/ A
解决方法:* E8 E" C, u4 `* a d( |
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
]0 t9 a* L7 [3 y- @& f+ @0 _
4 J1 T& i# M% G' _SecAuditLogDirMode 0777
* g+ L0 T0 y: O( u0 s5 A: ESecAuditLogFileMode 05503 h6 U) _1 M2 ^) ?9 L
SecAuditLogStorageDir /var/log/modsecurity
6 g- T5 i, L4 `# V/ I) c8 J2 OSecAuditLogType Concurrent. |3 W# A; N% `# ]& T8 X @
参考文章:
0 v9 E- P* k# W+ B8 nhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
8 r B0 ?- \% b4 k7 {http://drops.wooyun.org/tips/2614 |
|