|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
) a! k# r4 \ J( o! s
0 a, E: B: q% E% L, g0 ?1 S一.准备工作3 J2 @' P# _( b
I; P' }4 W( Y1 Z
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
4 m, M2 U: R& t
4 e$ \' Z1 J( L) Xtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
% j2 o; S8 ]3 ]' q
' N+ b% g ~' M) Imodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
; \/ m' @! X1 V7 X3 x" P+ Y* p; k' ~( C3 |
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
( y1 r$ v. p6 X* q! s/ k% E% ]# n5 `
依赖关系:* a' W+ |7 S3 o% ?1 Q9 Y; P# X% G
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
+ a# v0 d+ `+ ^( [4 g/ k0 C
# R- z% |& Q" W, w, f, n9 Xyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel( D: w; V$ F- f* P u( d* ?8 G b# E
modsecurty依赖的包:pcre httpd-devel libxml2 apr2 P, t) j4 ]% L: I( l
" ]# E5 l0 p& G' q& @4 c+ h2 q
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel, S [ x# j9 a* U; Q% d& {( Z& @
二.启用standalone模块并编译
! s+ V4 Y2 B$ T. G' L' {8 O3 }, o: I; l0 l# X& V k7 Y# L
下载modsecurity for nginx 解压,进入解压后目录执行:
" N5 ~$ k) i, k
- R+ C. N7 A* N./autogen.sh$ X. [) B/ E% k9 U" i/ g
./configure --enable-standalone-module --disable-mlogc; j7 i1 N* N/ J4 u7 O
make 1 g! x; [0 E* Z8 V
三.nginx添加modsecurity模块# u/ v' Y2 g- a
$ j$ P6 P |' X E: R在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
1 s( @/ D" Z% {/ Z' _. _$ x& s
: l8 U5 m6 o8 ^: }( f2 ] Y./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine! a* @. J @. K' X4 w
make && make install
. W. q7 O$ q7 \# u, T, g t3 N四.添加规则0 {% ~" z( \6 @; E
! K# K5 ` A$ E+ t* B. d9 O
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
3 S3 h5 H8 f y1 h3 |1 y, a, l( V1 n0 L
1.下载OWASP规则:
7 Y+ _' U5 c% s: Q
# M% j L [- w" d* kgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs0 S n' e7 y" @5 @
9 G0 k7 p: z u9 t: Smv owasp-modsecurity-crs /opt/tengine/conf/: k$ z( D- L* j. }
0 v; X; N' t$ V- v; r
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf( D2 T% N8 i7 I, M4 ?, ?9 c
2.启用OWASP规则:+ T7 L# H# D" v1 ?6 u
) W, Q% R$ W" v9 ?/ V" ^$ q复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。$ w. ]) s$ H" n- i
1 o" M2 n# {( {: y
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
4 y, G9 G+ A( i* k5 \1 _7 D# r# }3 H4 ~1 x
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。1 \1 j! _& P* L! x- e3 t5 A2 V
& {) \; h* f6 }8 W6 \% I
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
6 L& V+ A# y+ |0 z( nInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
6 a6 Z, M! |9 N$ QInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
: Q/ C! o. u% h3 [Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
5 G! m V! {! D, ~" SInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf2 `( C8 |& Y; G; U" Z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf. G" V) O% I' u4 d/ W* c8 w
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf/ e3 `9 b* D3 F
五.配置nginx
: w1 m( t" L) _# @' \& J2 H& i4 e# R b7 E& D, m
在需要启用modsecurity的主机的location下面加入下面两行即可:: F( I% v. F6 d0 q( [8 ]
e9 p' u0 {3 B* {' p
ModSecurityEnabled on;
, a+ X$ `* x6 T0 M9 @1 N5 HModSecurityConfig modsecurity.conf;) Z* x4 Z5 y9 [
下面是两个示例配置,php虚拟主机:
}3 H4 C2 N& M; K7 \4 U8 @; a6 I9 G0 E% G; o+ g! D
server {" O& N6 c) u* J
listen 80;
* o6 r* x7 B R server_name 52os.net www.52os.net;" J, w1 B4 K; G8 P
" A# Q. d8 h" @: w9 i' m
location ~ \.php$ {4 m5 l" d% y, |- {1 S
ModSecurityEnabled on;
1 r( b6 S) [% l( N2 y ModSecurityConfig modsecurity.conf;' n- T3 }! X. u( w- I
* K4 {6 M) g& ?
root /web/wordpress; z+ @5 A, r3 @: b& ^7 J4 W
index index.php index.html index.htm;
d- i; v1 c2 }( t - t1 x% W# ]2 r8 x- `5 V
fastcgi_pass 127.0.0.1:9000;5 \$ j5 V8 T3 g
fastcgi_index index.php;
+ J7 z. n$ @* G4 }; }9 m b fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
' C: ^6 R' D6 e1 e' p4 r include fastcgi_params;
7 Q1 P/ a9 G3 `6 m+ R }
& d& ?2 @; D3 k+ n }. ?2 U/ j6 N5 U: Q3 G
upstream负载均衡:
. w* ~8 \1 N0 U/ |9 e9 _8 X" w* u, x# F2 V0 f# ]+ e
upstream 52os.net {+ N5 k8 j f9 x+ L' n
server 192.168.1.100:8080;
/ p0 o2 M! b$ `; n) e4 `$ H server 192.168.1.101:8080 backup;
6 t, n8 w) f% ?2 }( k- ]1 _}
, _+ G' W1 M6 s( V5 w5 ]! d8 h* p$ T4 ~2 q/ J/ _9 m
server {2 k; A; r* F" G8 W
listen 80;
4 z9 k& e' q# g/ Pserver_name 52os.net www.52os.net;
- }8 ], Y( ^: r! P( k" u& v
- F' ]6 t$ g7 Olocation / {8 [- @7 A# @5 ^ o4 u
ModSecurityEnabled on;
. e# b5 I7 _1 S ModSecurityConfig modsecurity.conf; ! ], s' ?% m+ b6 l- b& P1 ^- O
: S0 b6 i) [: e+ ]5 ?9 K4 W proxy_pass http://online;
, [7 G0 _ i$ ]& T proxy_redirect off;
" l5 t: _: \6 W) d& L& T: |- {8 Z' G proxy_set_header Host $host;) R) f3 `" v0 }, W7 v
proxy_set_header X-Real-IP $remote_addr;
" k- Q0 R0 ?: z7 _ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;- K* h* s/ ?2 Y2 i0 K
}
' g! S& Z$ q+ K3 ?6 u' f& }}
4 d# @' {* C9 v& ]% e0 G六.测试
: F/ O9 P: `/ @7 Y
8 f" F7 v2 g0 A- z我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:" Q) |" T0 T1 f4 H: N% s7 S* Z
" i. b( M) ]# \) K7 s. \- H<?php( q+ A% [+ p8 E6 E1 i8 E- @- n% X
phpinfo(); w" u, P9 @; Z7 V |* W& [
?>
( k: |5 U# m3 v/ ^) G在浏览器中访问:: m3 l0 i* f0 V! i8 b* [
* `5 ]: b" [* S# D+ Q. ^0 |
http://www.52os.net/phpinfo.php?id=1 正常显示。, k# w$ g/ c: O/ C6 ]0 \
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。! ]( d3 p5 `" h! T1 X/ |# g
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。2 ]- g9 d v0 K) D8 K
说明sql注入和xss已经被过滤了! }4 v6 {/ C- [8 q) @2 ?1 u
, q/ i. n( @, L6 v5 b3 `, U
七、安装过程中排错8 w" ~1 y6 Y% R* r, A( Y
- F( Y' I; v6 L" _0 m( t, k9 _
1.缺少APXS会报错
- Z! w1 h0 `3 P
1 n7 N. m( N0 B. P, n+ `% Cconfigure: looking for Apache module support via DSO through APXS, h4 e" m0 t7 f/ e% t; j" X
configure: error: couldn't find APXS
4 _% W% B9 b' P3 h; [apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
4 j$ P; L" G+ T5 ~7 ?解决方法:
: S( c0 |2 p+ B# z
7 l4 O- j# [. H: ]4 u' myum install httpd-devel
; h0 U" V, e2 v* G3 S2.没有pcre
6 C2 r+ [/ u$ }" V9 S, A
2 N2 H d( o3 Iconfigure: *** pcre library not found.$ U& R$ ^3 H" H9 u) O9 u+ _
configure: error: pcre library is required
8 M4 |. q5 j1 {2 a S解决方法:
; a9 E' w" @( s) ?: L: f3 w( r+ y' g& J( s' F9 A4 m
yum install pcre pcre-devel3 U* o- G4 _; }7 A7 F, t& ?
3.没有libxml2
( E5 U5 i# |- _5 a! `* O$ y% d4 U1 j3 ~! t4 p2 Q
) c' S! O4 W; B; v8 m% kconfigure: *** xml library not found.' n9 b3 G2 c6 u! V) d
configure: error: libxml2 is required- [6 m: f' _/ |" N
解决方法:
5 }3 I8 S4 |$ F9 ^; G' v6 b
~* d. p4 H9 A( ayum install libxml2 libxml2-devel
3 E! o' B+ Q( l4.执行 /opt/tengine/sbin/nginx -m 时有警告; t4 N' h/ o7 J( s- r6 ~4 z* ?
0 v! d7 ]/ g4 k! i2 k8 f
Tengine version: Tengine/2.1.0 (nginx/1.6.2)" j2 v! _; s3 t" a5 I0 {2 {/ j* }
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
' |9 Y c, P6 d原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
6 Q" [# i X! d" K G
9 |* ?" W G5 p6 I9 ]7 F) [8 u' M4 I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.1 I5 p% ^0 B$ \4 Q; n |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9". l" Q; q: H( K* {$ U3 b
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!( M6 I4 `6 f. }+ T' r: Q) V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
* z" A, p6 ]) e8 x2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
e7 Q1 x% ]) \2 |2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
* j& \1 ^: e3 R) c8 x Z! i8 c解决方法,移除低版本的APR (1.3.9)5 }+ R% z8 B9 @2 `
) b3 n% _+ V8 i1 k9 ]- ~; Z; C
yum remove apr
' ]; K# _8 ~- n8 |4 d. O5.Error.log中有: Audit log: Failed to lock global mutex
, P) {! S+ G& ^% c: u9 y s J8 e: N, {) ~, H
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
T7 G) r& K! z' vglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]8 @ Y: B. F% @, ]7 K
解决方法:
0 t# t. L, s. O编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
7 T& v8 V$ g, \9 u
+ X' p2 O, U4 d+ [0 KSecAuditLogDirMode 07774 X, W8 X9 E# A7 j$ s
SecAuditLogFileMode 0550
3 H$ q7 E% N* d a% s0 i$ _4 M5 SSecAuditLogStorageDir /var/log/modsecurity& h7 c$ B& _; d6 h9 u5 Y
SecAuditLogType Concurrent
4 p, K- Z9 ?: H8 O4 C& T参考文章:
o1 r! p& ^6 f+ y/ {https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX2 I7 `+ K/ y" H8 M3 b/ g
http://drops.wooyun.org/tips/2614 |
|