|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。6 R2 {9 q o1 B# r3 y8 }
: w8 _% m4 b8 b& ?
一.准备工作
8 V" g9 T% {# H* W9 E" |9 H* D% g. g7 h+ V$ L* D
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.06 h1 D' P- [7 J0 ?. @
" w; W8 Z; c& I- j9 |: R1 l( K, B- @tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz2 ]" n. ]1 h! z, d
" X B! N+ M4 Q* wmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz {; h, V* I$ @
0 E5 n: a' I2 i) T1 j8 E' dOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
3 U4 L- n: H8 i* ]/ \8 v( y2 Z
! Q) Y2 J$ J: D( P5 c1 O依赖关系:5 t J1 @# V% z' [# |
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
/ R+ ]0 @6 K A6 K Y. Y3 C( [3 r4 X$ j! D! R3 Y
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
# Z% D- ^2 F+ \4 m' y# l+ Fmodsecurty依赖的包:pcre httpd-devel libxml2 apr( o6 [. z# Z9 D6 A, Z- F, T
$ U$ R% Y$ v! [+ d7 ?yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel6 T/ k+ P i+ v# v
二.启用standalone模块并编译' U8 C A n& \+ Y$ g, C
, G3 q& l, C2 p8 Z f4 k
下载modsecurity for nginx 解压,进入解压后目录执行:* C- \( i$ @- E
* e% M7 S! D( Y4 z./autogen.sh
4 ^- p1 S6 s: r" S( u./configure --enable-standalone-module --disable-mlogc
4 K9 M0 Y; w1 t4 Vmake
9 U3 q! G8 b& {三.nginx添加modsecurity模块
' D5 ]# p. D6 o8 N) U ]% D7 o3 C9 w( J; i/ w
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:! l2 U9 h0 g$ j6 i
. ~3 q* j) S, Z- Y& H7 Y
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine% K) K3 T, K5 Q- p. j
make && make install
4 [$ ]- v& ^# B8 }3 w. s四.添加规则
* `$ c/ |, t1 I5 j# T
* a. O( V& }$ R) ~. U( ?modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。+ Z. ] A* L! t5 T6 V E
L9 H b3 M3 a$ K: Q! O" A+ D1.下载OWASP规则:; U6 U3 `3 s( n: O" j
7 h- w$ q6 }' R2 m6 J" m
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
* q8 q! f9 {3 p. i3 X- ]3 _
# \; _+ H* [6 A/ q7 Mmv owasp-modsecurity-crs /opt/tengine/conf/5 ]2 Y0 K( B" [
2 [4 S y+ v. j+ vcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
9 h; p" B0 D) j: M- ^2.启用OWASP规则:1 x# Z- s. e8 O* g7 u: H
5 T( z: Q$ c a) Y+ F( _复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
% l& F7 C' H5 j, [) {$ t
/ \ J( y7 M3 x编辑modsecurity.conf 文件,将SecRuleEngine设置为 on9 ^- Q4 p6 D5 H0 n; }
7 @$ B6 Z T% I5 ]% I1 j
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。; M& E( B; o3 ^1 Y$ }
/ e1 O6 e6 \/ C) ]1 Q) x
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
% |$ \/ J1 Z) w7 q- L& V9 c0 ^: QInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
" T% g' {' f( m- eInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
! q* }! G8 |9 A" Q+ jInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf, Y6 ~& l. |, r$ e( q
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
: u1 R: I) d8 E6 E' mInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
! f, y M: \8 g2 u; @: V7 vInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
# x: o- c( a. i: F/ e" V5 A五.配置nginx
6 B3 T2 M; U8 Y" H& A1 \. C9 z9 T3 x' d" _
在需要启用modsecurity的主机的location下面加入下面两行即可:
l: f/ W( U7 @+ ~* J2 @, B+ ]6 U" x) W2 X: w4 Z4 @7 c. Q
ModSecurityEnabled on;
. z9 Z0 Q' Z* i: uModSecurityConfig modsecurity.conf;
: E4 N& e6 r/ D/ s+ b$ [' u下面是两个示例配置,php虚拟主机: O. Q. u) r3 S: T% M
9 a; r' R& P) Eserver {
2 p# m6 n6 d. s listen 80;
+ ~: X( [8 Z( G& C9 T) ? server_name 52os.net www.52os.net;
# n' E( C4 K7 M, |4 T* i $ F' J5 E" i ^# G: i- [% d- x% J
location ~ \.php$ {
: A/ j. Q& j1 |0 ]( @3 h+ e' o& g/ K& K ModSecurityEnabled on;
+ y5 ^1 M; S. M8 x* ~$ r; `$ O ModSecurityConfig modsecurity.conf;2 I% ~$ q" W# d* T3 A9 D8 D
) ]* ~- G& ]7 c3 Y2 j& B7 Z( |/ _ ? root /web/wordpress;' S5 {) r9 Y0 w u9 D: h( G& k
index index.php index.html index.htm;
& T8 W" V% B, ~9 k. ^ / n& X& _* X5 I$ {+ l% S
fastcgi_pass 127.0.0.1:9000;6 }4 h$ A# |$ r$ V/ W' v( e" w
fastcgi_index index.php;$ G6 C5 r0 ?, z
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;/ n) y _. |6 v. ~$ B J. }
include fastcgi_params;
6 e& F9 j! {; ~: ~9 a2 u7 b5 D }
$ H* a1 ?, Z2 U5 r+ a+ d9 t8 ~ }$ C D9 m( k! ^( Z3 `
upstream负载均衡:3 v, E/ T/ Q! H- ?3 a
7 |' X) W$ K5 A* k+ E9 a
upstream 52os.net {! C- X6 y0 r. x0 V
server 192.168.1.100:8080;
4 J$ B8 Z& y: a6 v9 s+ y, s- i server 192.168.1.101:8080 backup;
) F5 B7 \+ E4 T( k- e4 P}4 H' a @6 x- H: Z* A* X) W# @
( p* T* \4 N* Aserver {
& W5 I! z( T! y2 klisten 80;
) c# l1 R! L* Lserver_name 52os.net www.52os.net;, u- ^7 s3 @' O) D; s4 p# A4 L
* t. t6 [' g# m7 u7 }
location / {7 M" q7 v% i7 P
ModSecurityEnabled on; : ^0 D3 S& q- |; D, Y% F
ModSecurityConfig modsecurity.conf; 1 j3 d9 g2 z D+ {. z2 E0 n
% d4 ?. y3 E. N& ^/ O" \ proxy_pass http://online;+ k; i5 s T$ I& d1 @
proxy_redirect off;
7 G# m6 B* W$ M7 _ proxy_set_header Host $host;/ T/ G4 h/ ~! [
proxy_set_header X-Real-IP $remote_addr;, T7 N8 m- D2 C% f/ b0 C( E
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;! j1 d" r9 ?; R0 j; k
}
6 N& _% h. O. F% Q: e6 i}( d/ c; |8 z4 ^& N9 G1 I) i
六.测试
, m/ W; C) Q, }) Z& \$ E$ _8 ^. D. @) R8 F" _4 W
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:- ~% ]- M+ B: a8 B/ r9 i
* [" u. C* M4 Y$ |<?php
B+ M# h( k" f9 h! J+ W phpinfo();
1 r; E2 K" t& p?>6 l8 x8 |; B0 h3 I
在浏览器中访问:9 c" ^$ K1 j' M( d H# B/ D1 o+ _, j
; n/ n5 t4 J/ B$ g Z8 G ^, W7 jhttp://www.52os.net/phpinfo.php?id=1 正常显示。( Y' U |6 J5 N
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。 C) Q5 X; }. t1 H; D
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
3 w2 h9 G3 Q* A# W2 D% a) Z1 }& w说明sql注入和xss已经被过滤了
& n z+ \6 B, c3 c/ G8 ]2 g1 k, K/ P; f9 R$ u% n# N
七、安装过程中排错
" ~- a7 v1 ?" l
9 r6 x }7 ^ |- H- E( D1.缺少APXS会报错7 G% }, H- d7 _: `( H+ s+ m
- e8 K" ~) {5 T0 X0 uconfigure: looking for Apache module support via DSO through APXS3 E) d& R4 E8 x
configure: error: couldn't find APXS' s3 t; X1 n e+ w( }
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。! A$ ]0 Y7 J# V( h0 ^
解决方法:4 Y" G3 O: D1 _3 q$ j
# H) x3 R- B/ @' \ N: C
yum install httpd-devel
$ H! r: c A* k/ D7 a8 v1 l, h2.没有pcre& S) I! i8 d: C4 C
( G% n/ D7 C6 `( z3 P/ _( hconfigure: *** pcre library not found.6 U- x. F; N- W% M* j8 h! x
configure: error: pcre library is required
; s+ D! W) I0 H( U h解决方法:- B8 k( z! C3 r( W
0 t& j6 A: w; n/ h% Yyum install pcre pcre-devel
8 h3 [) }+ T/ d3.没有libxml2
, l0 U V5 b1 ?$ V! v; C \# x( a* t" Y. p4 ? |0 Z9 W
4 E/ I' D' z9 _) @1 u2 Fconfigure: *** xml library not found.! e q4 v* e0 P2 b
configure: error: libxml2 is required. A; R7 }: |# ~: G; l+ ?3 j* J
解决方法:
. Y2 b; R$ m+ g7 m
$ ?8 [& e# ^5 h* B. iyum install libxml2 libxml2-devel) n, c0 f3 u& @
4.执行 /opt/tengine/sbin/nginx -m 时有警告
" V- B( t" Z7 O6 s# b: k3 U D. J
. O: |- A6 ~2 w: k: f* J4 sTengine version: Tengine/2.1.0 (nginx/1.6.2)
8 E% A7 X- [! O, O+ Y' J8 Rnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
+ X! x" A( X& y# ], o% u5 Q ^原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
$ ^& a6 V2 B' x0 c0 V
* q$ F9 G3 Y( w2 C ^2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
: _, [$ D( p" y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"( M+ a$ v v& p! ~; x6 M
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
. T% {) Z4 ]+ `; Y) g+ _" W: l5 u/ s2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
+ i- Z G4 y( `5 m) w- h2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"( N/ k. C7 a' R
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.* z) n2 V e' }: A1 _0 ?; Y
解决方法,移除低版本的APR (1.3.9)
/ O b7 D* r1 u7 I
6 {/ o I8 i7 B5 ayum remove apr" O1 {9 l v; f2 a
5.Error.log中有: Audit log: Failed to lock global mutex
' n2 d7 ~. h: X1 `, Z8 m* i7 [1 J2 j! ]' J
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock - ]" K- b9 b* I- Y' n0 B- \ p/ `
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]3 j, u' |$ W2 p6 D
解决方法:
- h/ I* w% v" d; K: I/ [) w2 D! Q0 T编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:3 f: V! g, a$ p9 n
' l/ a3 T& h( SSecAuditLogDirMode 0777
, d+ e. \6 a# Y* uSecAuditLogFileMode 0550
- R# e/ Q1 a; Q$ m' USecAuditLogStorageDir /var/log/modsecurity
) c8 m; h7 f* j/ [. H3 i% p/ K. FSecAuditLogType Concurrent
6 T/ C% o( V8 K7 c参考文章:. Q, w9 O8 t: f% e- j
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX1 [6 H/ @- s- j) y' j+ W$ Q5 E |
http://drops.wooyun.org/tips/2614 |
|