|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。+ ^6 |$ K( G2 q! T
% G- A, _$ s8 F5 s' c) M `$ x* D一.准备工作$ q9 ?2 p a, b+ r2 T6 N
0 w% R3 r* B2 x9 v% J
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
0 f T7 r- _7 t+ v6 `9 W9 @
: ^ {. z: A* D, a6 h4 D8 o5 n# Ltengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
% Z8 ?7 n/ P4 q+ x: W
! K$ [3 F' I6 b I0 H" M2 J' omodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz2 q( y7 p4 g: v) P4 n( F
. r" R a! e. D: U3 ~2 lOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs; }) e! |: S9 T ]
! \' J0 v# ^. V. j/ n$ f2 G
依赖关系:
/ ]2 U- i. I3 u4 T0 ?' |tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:) W' f. ?: ]+ E: t8 Q
! Z. g/ }: u! T6 H1 d& r
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
, F9 h. U8 f1 g6 q5 wmodsecurty依赖的包:pcre httpd-devel libxml2 apr- N4 W9 R7 V. z/ H
; ?" j3 B9 q7 g
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel, `- V1 {! w- ~% C$ K
二.启用standalone模块并编译9 i2 h9 |6 b' {( l4 A
6 l+ [4 M( F" ^1 l& g8 B) ?
下载modsecurity for nginx 解压,进入解压后目录执行:
7 j5 X. E9 z2 J" _$ \2 G* i8 b# u' F0 @; o
./autogen.sh+ v. H- n% o" _4 G: Z
./configure --enable-standalone-module --disable-mlogc
. b4 Q& i2 r3 g6 K; c. c/ \make # G. m' X, P; }7 s! v+ d) g* F
三.nginx添加modsecurity模块. M& V! x! ]) n `' m9 }
. a+ ~" o7 g7 l8 X% v* C
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
$ {8 p3 w7 q" L' L: Y4 b# d% B+ x8 u! s5 U
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
' ]6 z# f) t, Z. C+ imake && make install
; o3 U/ K5 A& D* m- ^四.添加规则
+ `; G* m3 L: G, r8 S* x+ Q! @
- k+ M1 a8 `4 |( e' R2 ^modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
3 |/ q- L8 `( }" M7 p7 X+ ]5 k/ G% U) p9 x6 y
1.下载OWASP规则:
# Q/ K Y2 z/ m3 b( L+ Q8 \' Q- _, c5 ] e2 m# T. }) V' E
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs+ V% f( q! q& _
8 s# B' S+ R! g w6 r/ a
mv owasp-modsecurity-crs /opt/tengine/conf/. \7 o+ U( L0 k T7 z
% e4 D' N" P- e' I2 J7 v& b
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf* R% J4 f3 }/ t0 l
2.启用OWASP规则:" ^; \6 s3 R0 }6 B" j3 B
6 Z5 e9 i" Y/ Z/ f2 I! Y
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
0 v+ I/ j* n) j7 `# u8 Y, f6 y0 V8 S: M6 u4 K
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
6 `% {- X3 |, i2 x
9 w+ ]7 J& Z9 M; P( yowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
- f% c% L/ i2 X5 r
! e* w, U8 p6 F& _# S6 OInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
6 d! E5 d. T }9 e& CInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
+ U1 `( n, M. E; q$ Z wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
5 | Q& F/ `2 Q( U; Q* `: _" vInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf, V' ~" k N( Q" U# I d+ R- M( J
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf% Z; |# S" @, C! A% M: h% ?
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf" g' h7 m/ ]. F$ `+ H3 i
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf7 s3 @* x m) i! j0 H8 R' x
五.配置nginx2 K- @# @5 t: S3 L
( u9 h" I4 S: j) t1 h8 S5 _5 h' S& x在需要启用modsecurity的主机的location下面加入下面两行即可:
6 b( a8 J z {3 O! T
% f9 @$ C+ {- t: @ Z- m. nModSecurityEnabled on;
2 z* j0 [) `" Q3 [/ l7 CModSecurityConfig modsecurity.conf;# P" j( k& u! E( A1 L" [
下面是两个示例配置,php虚拟主机:. j. ?2 C0 j# c; L: E" C
& p9 M1 X* c8 s: {( f
server {' W4 E( B }& f% f
listen 80;& u; |: ^4 `- | D, K) `1 [
server_name 52os.net www.52os.net;
5 l5 K; r. b( v/ j0 H' h% x/ T
2 H" O# n/ }* f7 { location ~ \.php$ {; d* E# {7 I I# _ T: P7 K
ModSecurityEnabled on;
! f/ o7 i+ q ]7 k) T ModSecurityConfig modsecurity.conf;2 ]0 H) X/ J# L' L7 [6 O
6 _# T& o9 ?, m5 K; K1 l6 v! w root /web/wordpress;, ]$ c5 u1 `) e
index index.php index.html index.htm;
~* [" }0 h- r) B 6 [4 f, B' b W% \2 C
fastcgi_pass 127.0.0.1:9000;
- Z7 S; S+ ]2 R/ a- \3 a$ Z6 }( {" L# x fastcgi_index index.php;
9 @9 w9 |9 K' l6 a# _, |% d/ f4 y: D fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;; ?- a" O$ j7 I4 g
include fastcgi_params;: b6 ]# O* Z) I& K, i
}9 a$ Q& K0 n' ?8 ^- E
}+ S+ |/ {, O. B6 L
upstream负载均衡: j6 `2 H* T+ J0 F9 V, [
9 z& ]2 L- H3 R. d
upstream 52os.net {: A3 V$ c1 B, P
server 192.168.1.100:8080;2 s" O3 B$ a, ?
server 192.168.1.101:8080 backup;% C3 Y. g7 Q+ N/ Y7 A. f
}7 L# M2 t% M! z9 k7 x
8 h0 _; _( }" m+ n5 S; X1 [) R6 \server {
8 W Z& I6 c" l9 ulisten 80;
" U& I: m' N5 _! o& ]& K) _server_name 52os.net www.52os.net;
2 O: P$ R( n* c* V
; b1 \- _" g& ?! Alocation / {/ s2 l4 A4 M) R
ModSecurityEnabled on; 4 i7 o2 ~3 s9 i- p `
ModSecurityConfig modsecurity.conf; 2 o p, T" r5 q0 p/ R( d
/ N! n4 X" {7 p5 D* N proxy_pass http://online;
; o; G! k z" s proxy_redirect off;
( R- U& ^- N. Y D: O9 m proxy_set_header Host $host;
" I$ \1 o" L8 w/ T/ p+ o6 E2 c proxy_set_header X-Real-IP $remote_addr;
& ]: g6 | t- X5 j proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
: b% F; ^3 Y; f# G; w: b( N4 X }: u% K1 N2 P" R( |: n
}
2 Z% @. I" h0 j+ }, u六.测试4 P7 Z8 O, z$ @
7 ]8 R% i+ D g
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
% G( K+ o6 H% D8 f1 J( S# F0 V) r3 X* b" w5 u5 \7 _. \6 H# l a
<?php$ k7 @/ h4 z) N
phpinfo();
+ j/ ~1 \+ D e4 C% v+ x9 j?>* O' w6 }' c# e- x% B
在浏览器中访问:
0 N* r- W+ Q8 _6 r, I% u% A
9 t- V2 g k' [, }; g* {: Fhttp://www.52os.net/phpinfo.php?id=1 正常显示。
1 g6 L% G/ V5 E$ e% l4 B, y- Shttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。 r1 C( M0 h% ?. Y
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
2 x! W; ]1 s' B5 q说明sql注入和xss已经被过滤了, ?3 U! z( }, q* ~, r, Z2 l
2 T9 m4 e& h9 W X' ^七、安装过程中排错
- _8 T) ~% o2 I3 g6 L" O* h; c( E3 _# |2 F3 Y% f5 F& y
1.缺少APXS会报错4 U9 o" L0 C. D0 V# Y
4 U. @% y# I! y0 L
configure: looking for Apache module support via DSO through APXS
% r0 ] j( K. Jconfigure: error: couldn't find APXS
5 X% I, W: S+ y) \apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。, ]: F9 d6 b( j6 Q' Z- a1 a
解决方法:- `6 u' u G. ^ b
& R6 e& p$ n4 Q% `* y) H1 B
yum install httpd-devel& u5 J+ F$ e! ]. F" q$ j
2.没有pcre; Y1 I0 T9 ~, L% p
& O G0 D0 s: N3 C5 w
configure: *** pcre library not found.
8 _, D! s- N% Z* B0 Uconfigure: error: pcre library is required
. t9 R" n* i' ~/ r% P4 q. k) ~8 A# [解决方法:' f# \* w2 ] w" R1 I) }( y/ H6 Y! ~
) E6 u7 J! b2 j0 ?% Z& W0 J# ?yum install pcre pcre-devel
3 q7 u7 P! x5 w3.没有libxml2
! r) [; D' {. S+ t; V' `- @5 {* w9 T% p0 n0 A7 h
$ l/ t: v/ Q, y0 N' fconfigure: *** xml library not found.% c* g- [2 @8 G% d2 U# u# G- g: x+ W
configure: error: libxml2 is required$ ], [( g0 ~9 c: j
解决方法:
# a/ h) w5 B9 K9 D* q+ D3 J5 o2 S) x) p
yum install libxml2 libxml2-devel
% P: P1 Y& A* l$ E) O4.执行 /opt/tengine/sbin/nginx -m 时有警告
2 O: U: R( o* _* N6 C2 \: e! n. \& w4 b4 o- _6 W' s+ ]7 |$ ?+ k: w
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
: B2 {; K. f3 O/ p3 _2 inginx: [warn] ModSecurity: Loaded APR do not match with compiled!8 e. E" H7 v; B
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log g) @' M. ]2 ]2 h3 D" ]
% I+ s, ~3 a: R6 k0 v% o! l5 B4 O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.& x# }9 s& ]+ k# o6 S, @
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9" p7 @5 \0 d7 O% ~6 E
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
, s3 B& q$ G4 d0 f- c6 f: Y& Z8 q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05": Q" J5 L. a, ~1 z4 \! }( V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"8 M9 E n5 i, B+ F
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On., N% O. {. b5 y, Q
解决方法,移除低版本的APR (1.3.9)/ c! y5 Q( G8 w4 W
; ^# I3 K# p; v0 b' o) B+ Myum remove apr3 }; a ]8 ?* l( L2 [
5.Error.log中有: Audit log: Failed to lock global mutex
! Z; ]5 F; I! _# |
; @0 Y! ~5 R" @5 }2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
$ \& U$ L# R. u6 q! [4 _: {! yglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
4 l5 x# c" [/ S. [解决方法:( n2 t9 a7 M4 Z6 Z8 {6 w1 Y# n9 r. X
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
; {1 e# f+ }: P% B- V1 x
0 J0 d- G1 ^2 h, c- d- E2 K8 cSecAuditLogDirMode 0777 m; D4 |3 ~ i
SecAuditLogFileMode 0550. v% \- d) u8 e+ d9 R
SecAuditLogStorageDir /var/log/modsecurity
* ?. Z: K O" GSecAuditLogType Concurrent1 U6 Q, H r. ~- k2 D( E# m0 P+ h' p
参考文章:- V4 l1 U$ t' K1 J
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
4 @% f& ^6 ^7 n$ F& D( h4 ]http://drops.wooyun.org/tips/2614 |
|