|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。* ~; a) b5 y% s* J) p; q( a w
5 |; [1 S5 Y% T6 s7 L2 Z
一.准备工作
5 {! F, p5 D/ c9 c3 i
* H8 r+ e6 V. A: a% D; y% ~( b系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
: ?+ E9 ~& H" l# {+ U8 J
! ^( x* M! D( ~' o/ f- @tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz5 f% e4 |* Q& X- u- @0 H& G
" r; \5 I! z/ W0 e k+ Z
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz' y0 |. [" [# D9 `
4 ]' m: u f: ~
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs+ ?2 a, r+ g: H+ a5 T& ?0 D3 x6 b
. |* {4 z* m W+ F依赖关系:6 N1 k' ?9 c; j9 O
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
! h, Y% \8 @/ T# w
+ g& \( C( _) I: Zyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
$ M- |7 o: r l o3 E7 l2 q% M% }modsecurty依赖的包:pcre httpd-devel libxml2 apr
9 L. f) ?$ ?( _3 Z5 M! |) l& R
8 D( |4 Q/ s. Vyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
1 ^# C) j) m, b# O$ z, y0 Q1 C二.启用standalone模块并编译
! {. q7 Z6 b m' h6 K8 _
5 N! u) ]' t1 g v5 z1 V下载modsecurity for nginx 解压,进入解压后目录执行:# N: M$ c, V& r1 |4 o( m5 Q
9 i! d% a+ f$ R- N7 r, d9 j) D./autogen.sh, h/ k* C1 x8 S% l& |$ R
./configure --enable-standalone-module --disable-mlogc
% N. C4 c) H3 M" z) r! \4 D3 o/ ^" ymake
! U. I% J6 n1 X9 e8 U三.nginx添加modsecurity模块
: [0 k% B7 K/ _) b, F' z; x8 Q* C) S5 M1 i
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:* |4 V( L) O* o& Z0 L0 m: T
* \/ h3 @+ ~7 P
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine9 m5 Y7 F. K& @/ n; u" Q
make && make install
2 [' U' J! J. M! A四.添加规则% e/ x+ v M2 |. v3 K2 P) R' P
+ k, M. {" N; Kmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。- g3 b$ t& T+ j/ o( |* u
, j0 }2 B: O' `. h. T3 T
1.下载OWASP规则:
2 f9 x! M. `' v4 F+ @
& q3 _/ o( B' o1 H% I8 Jgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs$ T& o0 }" M2 ~8 J i" S
1 g. M* ~/ e- ?' amv owasp-modsecurity-crs /opt/tengine/conf/
) U; M0 Y- e5 Z* r' S( D* s& b S' O
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
. v& x" r9 {* l) V2.启用OWASP规则:
- A4 T3 |( d2 i6 Y# b! x& ~
, ?; {1 m7 a3 G7 d复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; O D/ _! @# q* P: ^9 |9 F5 c
7 x6 I! \0 C. H3 n7 ?
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on- H1 Z/ h5 p, N
# e0 t1 m; x+ m% B2 Q: {owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
" c/ i! b+ Q5 E7 u! u6 B9 R* h' z9 O! k- V0 u1 N
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
7 }& o/ h3 p( I+ j( @0 g6 fInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
3 C: x# F1 w* Y4 oInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
$ H( u2 `% b* D7 z0 ?) {Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
( c. K$ J6 ^6 Z4 uInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf1 P |8 y: ~$ W. Z0 M+ G
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
/ B/ f0 j5 r: P; E7 F3 _Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
' z9 K! j# M2 x五.配置nginx4 [8 O$ A+ M% W& y2 [
0 V' N; m; I5 o: w- _4 Y& M
在需要启用modsecurity的主机的location下面加入下面两行即可:
+ G; P* R# {" E! y7 {1 s7 z9 n' ^0 J8 H9 [
ModSecurityEnabled on; ) ^" ?8 Y0 w( s
ModSecurityConfig modsecurity.conf;. R6 [7 p7 S, k
下面是两个示例配置,php虚拟主机:
1 Z% l$ ^+ U$ @+ K2 \5 A+ q
" E8 a+ @# w d, R: s. l+ jserver {: G4 c( p1 u# X. g# Z P
listen 80;8 c7 `, h' e* ?
server_name 52os.net www.52os.net;- p- R1 }5 Q. i5 O, ~9 X
+ y0 y# S9 k+ W2 d location ~ \.php$ {
6 A4 ]4 D! j; t0 ~( s5 T4 m ModSecurityEnabled on; % S8 K7 }! \! n( y4 q
ModSecurityConfig modsecurity.conf;* B0 N0 M1 Z; p
: G' @& v1 g" N t0 v root /web/wordpress;. y) Z" i; v E
index index.php index.html index.htm;
5 o: N- m/ ^3 _' ~) a _ - a$ [0 w, F, E
fastcgi_pass 127.0.0.1:9000;
) @7 Q/ ]- j; P& p2 k. n M fastcgi_index index.php;
) o; i8 b; ~2 b fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
8 y: m/ `- z1 M& c( E2 z6 N) b include fastcgi_params;: w/ J/ n( t% W6 a
}" u( {& [$ G3 l
}( i4 q; U3 S; _; ] Z
upstream负载均衡:
, ]; s5 f4 _ O6 L0 \5 X: n: g; X( a- \9 h
upstream 52os.net { k; v3 I8 K$ Y
server 192.168.1.100:8080;3 @5 [3 e5 {& j5 c
server 192.168.1.101:8080 backup;
: T O- M7 P8 K4 A; {- C}
0 u# V/ }% z; Q
% X' H& q6 A& v0 o" H, ?server {
2 `0 E2 w! V; O) ^listen 80;$ E; Y# T, L J6 s( p2 B0 Q+ q
server_name 52os.net www.52os.net;
5 ^' B y7 j1 a2 l% a1 L0 F
* U+ q1 `. Q, w- }4 Q4 Glocation / {7 s' ?& m0 u+ }, k4 S% B
ModSecurityEnabled on; 1 n+ J, z: d+ ?9 N
ModSecurityConfig modsecurity.conf; ( Y2 {& |- E# c* @
9 W+ t% l. i3 P
proxy_pass http://online;
3 c8 i8 D/ \( F3 U+ t; z6 }$ d proxy_redirect off;5 ]* w* V5 A2 z0 A$ r/ v s
proxy_set_header Host $host;
6 r+ H/ b9 x" p, a4 @ proxy_set_header X-Real-IP $remote_addr;
3 \1 V/ S8 H6 x: p$ Z' a' g proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
4 u% x( X) k: J0 y, i8 v6 l. x8 Y+ j }$ L b! N. G# @! ]0 T
}
! s% K: @7 E6 P. q; K, `) K六.测试. ^6 V+ ^ [) p3 D& \, n) a
* @* V( P1 `+ e2 X- B5 b2 y
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:. v, Q( M v; h( B
/ X' @" x' s5 @8 y<?php: _; i/ h9 X: Y) e. m
phpinfo();
; b2 f! B1 ?5 l% K' c- ?2 j; F?>' H9 w. f M7 N/ X9 o8 C
在浏览器中访问:
* E3 y8 ` e1 g. Q, C4 ?/ p8 c2 m$ S- Y1 I6 n
http://www.52os.net/phpinfo.php?id=1 正常显示。
. R4 Y. y4 S2 j/ |3 ihttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。# L9 v- c6 `! o( [5 |9 i
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。- d+ n! L) `+ ~
说明sql注入和xss已经被过滤了- E/ [! R# o, G+ M& ^; I
: m2 b: i. W8 S9 |1 {/ e七、安装过程中排错
" v/ X- H n7 N
7 l( t V2 w/ L3 n) |) H, f1.缺少APXS会报错
$ ~" J0 B5 [% m7 y4 ]* z: v3 M
% k i y: s5 G* j$ o5 Iconfigure: looking for Apache module support via DSO through APXS
; C6 E& ^! N7 ?* I3 wconfigure: error: couldn't find APXS+ N( e! a# ]. f% v+ P+ e( H4 i
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。) T1 H* V* E( ?) f
解决方法:# I/ n: l8 E2 p& w+ y k4 r
( _3 J# t* @ n3 L0 k0 _
yum install httpd-devel: S' \! Z8 S( e8 u- g& H F: L
2.没有pcre/ F! b7 n" Z8 L1 ^2 }! ]+ B! w
6 r7 I9 |4 ?9 t) Sconfigure: *** pcre library not found.
6 V1 {/ g( K, L1 F/ Uconfigure: error: pcre library is required
6 y4 B0 ^) r4 f' [解决方法:
& E( |- f1 b/ J- h4 S8 n% ]. g+ c2 y2 o9 l% ?8 [. a# Y
yum install pcre pcre-devel
7 f6 G5 e$ }* R7 g) u4 b- \3.没有libxml2$ V% {4 ?/ o. O" J* O5 ]" `1 j
3 N5 U2 s& ]9 T% T' l% k5 q+ | F0 k$ X. ?
, F C8 i/ R, u: D# f+ a4 J n4 u
configure: *** xml library not found.
, U2 H3 \. Q" c/ [$ R5 o! Nconfigure: error: libxml2 is required
* N/ v. c/ [) R/ E' X5 V解决方法:9 R V4 U O1 U. v* D
9 I- T4 \0 K R& R4 qyum install libxml2 libxml2-devel& J9 Y4 w5 y( Y: t: w4 }7 P
4.执行 /opt/tengine/sbin/nginx -m 时有警告
" P5 G. U1 i8 ?' ` N! X: X) D+ R! \# M5 u" x
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
, L1 Q2 X$ g' ~$ J4 W. M2 Pnginx: [warn] ModSecurity: Loaded APR do not match with compiled!$ i0 o/ t4 h' `! m/ Y6 L* p$ a' |7 n! p
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
' F* {. b0 T+ j: { U, H% S( k# T+ d$ o( d9 C- R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
" b+ a& b! X* r- C* \2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
! O! _- C! b; I s% Z- A/ n* L2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!5 g" H8 N# m s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
: H* N6 S6 J2 O. q- S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6". y; a6 E8 _ u1 |, L
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.' e9 G: q# |0 c! V4 T2 v, L- R
解决方法,移除低版本的APR (1.3.9)
; u" U# ~0 e0 E% W
7 V8 X+ D* l8 h, p7 |yum remove apr
- [$ C) B/ ~( _+ w, I r5.Error.log中有: Audit log: Failed to lock global mutex
3 e: B' h5 |& J' k. `
- ], z+ g( c7 s4 g1 [/ `- a% Y( t/ v2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ! a" \& s5 Y @
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
; ~ `! W1 p" i$ x, V. e解决方法:& P+ f! J5 i) ?2 n; h& u
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
7 ~) E9 B" x# l8 ?% \* M; X
! @* A! N) m/ s& Z& H. ZSecAuditLogDirMode 0777$ D+ z1 _3 e. P) `' Q
SecAuditLogFileMode 0550* `& X2 @+ U- y) \! t& Q
SecAuditLogStorageDir /var/log/modsecurity1 t( {- B/ {! V6 V+ f- Y7 \! D
SecAuditLogType Concurrent
- r0 s* }. |# L+ E! E参考文章:
8 ?8 [6 q0 c! }% ^/ Xhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
. B3 [2 U/ U8 f& _" b- Thttp://drops.wooyun.org/tips/2614 |
|