|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
- F7 A1 r" ~% A* ]' m
( U- M0 _# j, F; O' }一.准备工作
4 A9 q* f0 K+ E4 H8 n: t* [0 j0 e: i( |( S5 G$ k, P
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.07 l3 b5 E% ]0 Q/ ?' f& j
/ m" X \5 w! b) itengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
. D& F6 `: E! p) p% m; r$ I% s4 X) Z5 E' M/ V0 Z& O
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
( N; {4 @ `; b8 _" \3 u& l
) [& o- w- S( t! F1 u% }, bOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
3 u1 [* m }- ~% f7 e
) ]( e1 @" ]- ^5 G G依赖关系:
$ r, }( N3 i9 q4 X4 y" ]2 etengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
+ f# g* F3 Y0 J) k) i- Z [
$ c& K8 \9 I" u8 f) M; B8 ~yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel9 c9 I1 `; J) j4 ?9 [: i: e
modsecurty依赖的包:pcre httpd-devel libxml2 apr
7 f6 {8 X& M/ l1 P& U
2 _: i+ [5 e9 M4 ryum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
* X% b9 J! U7 G; u. ^1 {/ w二.启用standalone模块并编译
1 e9 F6 y7 u* |: T; K0 x0 b
$ o: f/ u0 C6 [+ O下载modsecurity for nginx 解压,进入解压后目录执行:
3 R4 U L$ a7 Z
. g1 U% [5 N; v( G5 D! S' C./autogen.sh
6 w9 {* _. c' d9 `' U./configure --enable-standalone-module --disable-mlogc
+ V; R% l! i' u* m, `9 ?7 v8 Smake
. u2 ]7 b6 o$ ^3 q三.nginx添加modsecurity模块
' |, K' L$ j& H9 ~" y8 p1 L2 j2 v0 A8 Z) s$ d2 v
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
; w0 V' R! P7 Y2 N' g0 e/ z- C0 M! J+ v# G) q! \: ^- B
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine8 q4 Y* `! i; P; k; N
make && make install
# V, X3 v$ Y" `+ P: a四.添加规则6 L/ R: S& S" R/ ]5 F" I
+ H* P0 V9 N4 r' u d
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。/ l6 s) E; I, e# a+ ?
1 X, o H) k' |7 B8 R1 d
1.下载OWASP规则:% e* `1 Y, w9 t6 ]
' w( I8 t* _8 o$ O
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs1 u6 c1 h& i: G2 Q/ L/ L
0 p9 d! u! P- Xmv owasp-modsecurity-crs /opt/tengine/conf/
& B. i9 G& p I% P3 h3 i% f t, a
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf" \- V( J. s, h3 a3 Z
2.启用OWASP规则:* m9 h) E* P8 R5 U5 v
, ]1 |2 g, A" t6 X
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
; `7 \! t }# u6 a5 o! V7 p
1 G8 s- s9 s! d# W" Z0 O- O+ m- f4 _编辑modsecurity.conf 文件,将SecRuleEngine设置为 on& V9 W- D. e! P" x6 q' [; }0 p: H
X) ]5 Y4 J8 ~3 n8 rowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
: _4 M2 \9 n/ K$ `( N- A
2 s! l, s. ]* X5 DInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
) y. A7 Y* }* ]/ s+ g* YInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
! R& _( s' V# _4 ]& {Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf0 X6 w1 d( K, }2 h- k6 W5 r0 p
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
! h' `! K) c1 u$ ?8 N4 R9 J1 [ BInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf- _* X2 S1 W+ J& J/ N1 H5 @0 ]
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
; J% L8 S; k7 |: N" J, D- rInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf. l* @6 l4 h0 Q% [
五.配置nginx
* k% u8 @ n5 B. M3 X* {% p5 @& f0 m; X9 i5 C6 J
在需要启用modsecurity的主机的location下面加入下面两行即可:
5 B/ w7 u7 K6 @% t& Z* P- k0 i3 s" `) J
ModSecurityEnabled on;
1 i8 n9 U6 c, kModSecurityConfig modsecurity.conf;
0 k% P0 I* f- N1 M5 u- Q& Y下面是两个示例配置,php虚拟主机:
# [7 D* W$ ]' G0 T R
& ^- U0 J- x6 C3 @0 Nserver {; h8 t% A) r2 Q6 p' }
listen 80;
2 W# X! c2 Q0 k8 @ server_name 52os.net www.52os.net;
: P" p3 t, ]8 a6 i9 ?1 Y( q
6 I5 O$ S, k8 c8 M location ~ \.php$ {+ @& j& f( P8 L# G8 m |
ModSecurityEnabled on;
6 q9 q/ i7 ~: p! S+ {5 i ModSecurityConfig modsecurity.conf;0 b9 \0 n1 r4 K) Z- q+ K% `9 ~' h
; [ @% d* l3 Z+ m
root /web/wordpress;' P, o# ]' Y G' F! H) t4 d$ Y
index index.php index.html index.htm;
& D2 p( [, u- e; q7 w* t 9 c! s2 o* C. \% { N" G8 V# g
fastcgi_pass 127.0.0.1:9000;
9 k: [: f3 a/ e# ?5 F fastcgi_index index.php;
# P# V- L7 b6 I! U fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
6 Q1 [# k h9 S/ K include fastcgi_params;
4 R5 e4 F# I ^8 a# N7 ^ }
: {( K l- k. o6 V; y }
0 q. b3 a: f2 g, G A) Zupstream负载均衡:! x1 \) a9 ?: t1 C2 h, R# D
& {" @* |& ] M% r" ]( B1 z; R$ L" L
upstream 52os.net {# W. I i, C2 I5 I4 x5 }
server 192.168.1.100:8080;% g# c7 o7 K+ C5 q( p
server 192.168.1.101:8080 backup;
( }5 }6 W% S! i: P}
" o- U0 H8 H4 {1 j$ E- k! }+ \$ E, q* ~- ?9 t7 t2 B# t4 N& K+ {
server {6 G5 Z5 D* ?! x) S* e2 p8 N
listen 80;% G; u7 C( d5 s* W3 u
server_name 52os.net www.52os.net;5 ?! {5 _8 H, f! d- L" }
" r. q, U* J- |( @
location / {
% Z, X! ]/ A% M# r; g! E% B ModSecurityEnabled on;
, Q" L8 `/ @ Z1 D8 c. G ModSecurityConfig modsecurity.conf; $ u2 e$ y+ I9 b" v
! X& g! m: M) R
proxy_pass http://online;# ]/ u3 R8 k( ^8 k
proxy_redirect off;6 ]! ]) L5 m; a8 G5 C0 v# C! Y2 c
proxy_set_header Host $host;8 r% L0 y! Y9 l+ v& [( ~, a1 W
proxy_set_header X-Real-IP $remote_addr;$ H$ ` {% k5 L& @1 P" V
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
. [! s! v# E# L& M+ F }7 P, c4 T9 e7 s
}
# p8 t$ Z; `$ A* _% F六.测试# \: c* }- f9 S. F- E
3 J6 g4 l! I, ~# \, V5 [$ N- \ j, s! V我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:5 U. `9 r. a' a( M7 P3 b" ]
8 i+ y0 ~. m" p
<?php" |4 j. N6 `# t/ q2 N+ w) o: |; h
phpinfo();
0 h9 N; Y3 X% e# {- h?>* x5 x: x' r) K$ e5 F, _# T
在浏览器中访问:
# |+ U/ s, V: S) Y* e
5 P4 w) E/ r4 o2 g" \7 T% [http://www.52os.net/phpinfo.php?id=1 正常显示。, n& r+ M d% [+ c3 [% v
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
4 d B8 j# _/ J( Mhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。( L5 I W; P; x$ j: w M
说明sql注入和xss已经被过滤了# _2 f( V0 ~& d4 a4 c) ^# o3 x
, T/ c! P K7 h1 W# _七、安装过程中排错
% T+ G; }* ^, y3 |( Z! i; a3 o+ \/ C7 V* o1 f
1.缺少APXS会报错
" l: ^1 o, v% m& Y( J+ a& J% {( `7 g. w6 x, X0 K/ @! W# Z
configure: looking for Apache module support via DSO through APXS% z7 ?# H2 I# f3 N2 L9 g
configure: error: couldn't find APXS5 ]0 j7 s3 v: p$ v" S- _
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。8 x$ e( V8 B* {% [& {: J
解决方法:' x/ A( g+ m1 F/ P- }7 C, o$ P
! M0 d* C- H1 k$ c7 Q: j2 _
yum install httpd-devel
; K) v1 F+ w. M2.没有pcre6 c. y: _ d+ e$ T% u# q( e
; m' v: I( ^! S& @7 ?6 t% j* h0 R# o3 Qconfigure: *** pcre library not found.
: f) b: }8 Q6 tconfigure: error: pcre library is required
* p3 |5 L6 a) C6 v: U5 ?解决方法:
. [% h3 O a; v
: f% t/ b0 }. G5 ?2 k1 Ayum install pcre pcre-devel8 L5 e& y. l; U7 G
3.没有libxml2
* c1 x( h4 M' J5 ]' ~
, z# r+ x+ h0 @# b8 |" l- a$ l
9 N& R2 a9 n! `configure: *** xml library not found.
& T& [# J/ B; ^5 d- B. Econfigure: error: libxml2 is required7 f" |8 s$ C5 [7 J( D' k, I
解决方法:
: ?* P) X( f7 ?) L6 z$ Z* x' g- b2 t$ K; {8 W7 G* J
yum install libxml2 libxml2-devel2 Y/ u8 @: ?0 R& D/ S
4.执行 /opt/tengine/sbin/nginx -m 时有警告
2 R7 T' l+ I4 ~, {& x
4 C- O) B$ w( u2 K8 |% @7 tTengine version: Tengine/2.1.0 (nginx/1.6.2)/ P- ]1 ?& w" e, G% v2 W
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
2 A& d+ ^! E* ?6 s3 a( N原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
3 D P' t( c% D4 }
" t' V* K0 H6 E- I. \4 O' \2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.' A( R/ J8 T4 q7 B( _4 M( @) e
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9") N8 ]( \- W* ?- d9 o* o
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
" s! p0 }! @+ A' [6 D* I5 B E2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
4 d/ R# I6 V8 h, U/ e8 `& _2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
7 ]% o0 A' j: D. i2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.3 q: j6 h& Y: E8 ~3 j, \- z2 S9 f
解决方法,移除低版本的APR (1.3.9)
4 }* {( s% d% x7 p8 V9 ] x# ~( \7 v% v6 G8 S' R
yum remove apr
$ g, w P( F" K! Z! T, w+ @# _5.Error.log中有: Audit log: Failed to lock global mutex
: V. h* z- y' I- Y% }+ v& `% z" B* P, F5 \: {% P1 [
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 8 G6 O5 s1 k, c8 ^6 J, c
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
4 m! p+ H, Q5 b& i解决方法:
" N/ H7 x/ R# O编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
* w6 @$ B* V; R; } Q m! C
4 q5 K* p) i: t( C2 ^1 t0 f! Y) a6 ~SecAuditLogDirMode 0777* } u; l% R7 M. b4 x
SecAuditLogFileMode 0550
2 f! R7 V8 p: i# q2 G! A5 r* f9 XSecAuditLogStorageDir /var/log/modsecurity
, F! o+ m. b. f6 `% iSecAuditLogType Concurrent( D0 q0 j# f( _. ^' |) S5 e
参考文章:
i3 o/ v6 B' v6 whttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX* e k+ I2 U# A( Q% _
http://drops.wooyun.org/tips/2614 |
|