|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
; ]3 T+ l4 F5 ^/ J! {2 d' S0 B" A
一.准备工作
) s) i* g- Y2 H. X0 y0 b& v) w* \8 Y0 d$ D2 I3 C
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0% n; T" v% x, c8 _8 E6 k L
8 r1 P; u- F" h% a+ _tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
1 x& F/ F% \$ K: m
8 `3 h! X1 l' _1 t* G: zmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz& w/ z* c/ H: U: o) W5 q0 M
5 E9 M9 q2 Y* i' ~; o2 @
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
0 V/ ?; ?6 C% ]0 b' ^2 I: I' I; u& R5 d- d1 [
依赖关系:
# A7 b' a, V8 @tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
; @. S) ~) n! u3 B: \; q+ W8 b+ `
. @/ W& |1 g0 o Dyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
7 b. g0 T, p1 [/ Tmodsecurty依赖的包:pcre httpd-devel libxml2 apr; m h, W0 H" Y- v: ]* B
& |( n4 W; A2 v; c% o7 \6 P" R
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
9 g! v) X. z: v二.启用standalone模块并编译; F) Y+ H) S$ I3 b! x ^
6 c$ p& x' q1 D
下载modsecurity for nginx 解压,进入解压后目录执行:: P9 E O; T ^2 I! \+ l
& X. E3 e; H7 ?! F9 R0 R1 R./autogen.sh8 s! S4 i- C( x* K
./configure --enable-standalone-module --disable-mlogc: d$ Z6 q& } l# b( W
make
3 H" M' o# `+ ` D( h/ J三.nginx添加modsecurity模块
3 z2 r, j3 l+ l" @( T; M
% b3 X# \! O& |7 C在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:+ O9 T6 d l/ w9 s: E* u
6 U: b& W& T2 g& i6 G
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine( a9 g! i" F6 Q! o+ A) U
make && make install7 C: C6 O6 {7 y" r' `% _
四.添加规则3 ?$ X6 w& |0 U
/ d* D$ A1 I4 g5 v0 A0 Rmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。% W: s; s: @! h$ A' e
6 U" ?* | E/ b; ?9 R4 A& t
1.下载OWASP规则:. u/ g3 i( a+ B" Q5 c, R
W2 X" o% I: X d/ ]
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs: ]4 ^8 i+ C. V3 z6 o
7 |. s; R. Z% j; z9 M% i# j# |7 `mv owasp-modsecurity-crs /opt/tengine/conf/! q: L1 Z0 z2 A$ \' X9 {! v+ |
6 K- m4 H# N2 C E! a( m/ M$ qcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf, I: t' n" N2 t- t1 l& y/ r' }
2.启用OWASP规则:0 u; h$ I- u1 ]) Q
$ e1 ^8 d, f- V6 l- }" S
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。+ ^ @( V! g% V: N4 q+ n* }$ \
' h* [: x) t$ x4 Q0 g编辑modsecurity.conf 文件,将SecRuleEngine设置为 on( S( d6 `5 `7 G. F, i
- _0 e. G& I+ E* z, | v
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。4 m; q) d! [' n& t1 Q
. K' E& v C' x# Q2 h8 w, HInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf ^6 Q. S7 _0 R! {
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
' u. s: g- w& T, SInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
6 h% }4 T& k! P5 F" }$ A( EInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf r' Y. d; j9 F5 ^" M6 o
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf! Q9 M7 l) R1 F) c T; ~7 h y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
1 p `. O/ v Q0 s1 dInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
! y2 P) V3 U7 N) v五.配置nginx4 ?6 ]4 |" m# D( a( f6 O
- J) V& @) z" I3 `/ ]0 F6 w5 T
在需要启用modsecurity的主机的location下面加入下面两行即可:6 F3 n; ?8 U% ^
* B0 X8 L7 e5 b9 x) C: R! T5 y
ModSecurityEnabled on; % i* P6 K% Q* Q( Y& ^$ D
ModSecurityConfig modsecurity.conf;6 N) V0 j9 U2 E1 u. `4 {. f
下面是两个示例配置,php虚拟主机:
, U" m1 P8 P4 B1 ~* Z! b" C' V
5 q0 q5 }0 G& `( Qserver {4 A7 w: q2 S" E8 ]/ {4 P2 E
listen 80;! D! }" \, Z( m
server_name 52os.net www.52os.net;
0 z! i8 [+ w% \) _( B7 X* t p$ r& F ( R% }- H% ^/ j% E# S
location ~ \.php$ {
6 {2 K) Z8 X( f ModSecurityEnabled on;
5 k' s* d$ Y0 k" w4 ? ModSecurityConfig modsecurity.conf;
; ~- J( C% ^$ l6 ]& L, F2 `, C; D o6 \. v0 E5 i. L( U
root /web/wordpress;
( v- U A0 v/ i! ^3 l$ X" w+ u index index.php index.html index.htm;6 E6 ~7 n2 O! g) J
2 w# A) i; v% ^; J
fastcgi_pass 127.0.0.1:9000;- E. O- Q3 Z Z; w, p
fastcgi_index index.php;- D; q8 X, h( I3 z( S. H+ z+ Q
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;' }4 W) N" K p$ I2 Z
include fastcgi_params;4 G# b& z& I8 i7 b! v8 c9 ^; e. {
}7 H+ B% U8 v& N4 Z
}/ P" g' U) [5 |$ n( g; z
upstream负载均衡:1 B; _7 I* T; R
7 @. u2 ?4 u5 Vupstream 52os.net {
# G& i' o3 v2 P5 R+ ] server 192.168.1.100:8080;
, n% T6 Y' ]* w server 192.168.1.101:8080 backup;5 k4 s- Y; s9 b# E1 B. p- I
}& U B4 R" ~- u7 l, d3 s. q0 w
# b: Q- F3 Q! J) M/ t& ~server {
# P Y0 A- b# `) k+ S, Alisten 80;2 g" v3 C6 U) V4 {8 @
server_name 52os.net www.52os.net;
; ]+ C c, b& G# M$ ]8 \# q% v3 O" F( V7 |( K2 ^
location / { ^6 m/ v D- I( u/ `. x' }
ModSecurityEnabled on;
7 b* r F5 m! X# G* U0 g0 i1 z7 L ModSecurityConfig modsecurity.conf;
( F) k1 |1 z8 @) B+ I, ?% K
9 m+ `& X8 q: X0 s$ G proxy_pass http://online;
n$ _/ ^1 L* \6 e1 \4 } proxy_redirect off;/ N2 r' e5 z# V
proxy_set_header Host $host;$ ]. d/ \+ X/ [/ ]( D
proxy_set_header X-Real-IP $remote_addr;
U) U0 w, M+ ]0 E# B2 p0 _ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
6 D: ^) S1 V$ m }: l' o/ Y3 V: v ]1 r0 j8 w; i, R* H
}
! k4 R* y/ D9 j* e3 ^1 G六.测试, V b, i, U3 _: h
3 W1 \ e+ N% ^- a) v0 M我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:$ j& r' ~6 Y8 E/ x
) B% e. x( x" F: F0 U/ o9 f8 r: M: o
<?php
8 j! ?+ O/ s& o8 I* ^ phpinfo();
2 _! J9 b& {" u! w) o5 i2 A" V?>& q2 p+ l/ q; W k
在浏览器中访问:. _4 Z: {* Z9 H
& S* f; w& v( D9 V8 q& Q9 t. ihttp://www.52os.net/phpinfo.php?id=1 正常显示。! @# o P, S+ A% N" m
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。- b: \+ t8 e/ r$ X
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
2 d) ~3 c* @) d9 e说明sql注入和xss已经被过滤了7 {$ N' a4 R: w# ]0 ^
3 }* Z: S! Z9 r, D; l) L( {
七、安装过程中排错
" _) N1 F" G& A2 I* z
% R) i/ @' k1 }, l. l1.缺少APXS会报错- r3 G* r) Z x! g0 h
2 i' {1 ~6 |( u. \, Z
configure: looking for Apache module support via DSO through APXS( Y- v1 m9 M; J: ^
configure: error: couldn't find APXS
% u& x H0 u9 lapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
/ \* W. g! w% B! A4 y# d7 d+ H4 u解决方法:5 V6 F, ~9 ~- W! |; V. K6 [* V% \
% E% F1 i* z2 _8 P
yum install httpd-devel4 `- g0 J. _3 z- [) g6 d, k0 N
2.没有pcre
# z. j* x$ n! `& d; R
$ w$ L4 r9 u% }5 D: _& t) ]' n( [/ ]configure: *** pcre library not found.
$ o& K/ \6 R$ e2 P0 c: Jconfigure: error: pcre library is required p$ S0 p& d, I9 e3 z; U
解决方法:+ P/ P# h+ m7 Y" \( D) A( d
7 v' _" y, |8 g2 F0 Q& [4 Z2 {
yum install pcre pcre-devel
7 Q8 s+ U3 c. q& f" e4 l3.没有libxml2+ W: P3 N% {1 ~7 R5 ~
( u+ d& a. p7 ?7 t6 G; c3 `: J: h! F: m- z
configure: *** xml library not found.! \0 n M0 A8 c) v& O
configure: error: libxml2 is required, \- C" _! r2 c9 A- Y, ^& P, p
解决方法:' I5 J/ N8 o. z* i# w, Z: d |
5 h- I5 g/ r2 n6 T4 l* P
yum install libxml2 libxml2-devel; z0 U& \6 P. ^# r
4.执行 /opt/tengine/sbin/nginx -m 时有警告
, v% X* U. t9 { ?/ `( @& v* b8 _! B! G9 F9 ? A
Tengine version: Tengine/2.1.0 (nginx/1.6.2)( B2 v* y$ l: {# ]1 B5 c# ~
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
- {0 l, N" G8 H q原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log, ?0 w) t0 s3 k/ c" l
* n( s+ Q y0 y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.) V, o5 X6 m* |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"5 q' k! W0 E4 G7 |% z. t3 I
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
$ C |1 ]6 W' k/ L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"' ]. w# _; k9 p0 @- Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
; q* t- U8 j7 p' b$ A% U% | c4 N X2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
9 @: M: T( }+ T+ y6 i. P/ T, b解决方法,移除低版本的APR (1.3.9)
" ^4 ?: I& ?& X# P$ l, Q
3 ] x" ~( y% n* m+ K2 Ayum remove apr
* _& ^2 V, E0 R1 W0 C! T* f# `5.Error.log中有: Audit log: Failed to lock global mutex& P- Y& r% P `$ B
( \7 [4 q4 v* `# l% T3 i. ~4 L2 Q) f
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
: d0 N7 s: u0 S7 k7 ?4 n$ F/ ^* Lglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]* [7 i i; g( K; }2 \4 ?8 n1 E
解决方法:; q {- K7 s }6 y% Q
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:0 ~, Y5 B6 B+ `
8 ]: D- s4 O0 W2 a9 ySecAuditLogDirMode 0777
7 `1 a: h) E& H c9 x" h+ s8 ESecAuditLogFileMode 0550
* O2 y4 Z2 W, s0 m7 X/ c# ySecAuditLogStorageDir /var/log/modsecurity1 e1 g2 h# ?) A4 o0 f1 [" _" ~4 Z
SecAuditLogType Concurrent* n. i* @( z4 u0 V: |
参考文章:5 N$ k+ i, j4 S: t0 P0 _, R
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX, T% P3 O! q7 X( B \( A5 [
http://drops.wooyun.org/tips/2614 |
|