|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
0 x5 K% \6 z( `( u4 ^0 y# L1 ] Q/ x* ?0 y+ O0 ~
一.准备工作8 {4 E1 g6 H7 }. q! [& v4 r/ n
9 {! |7 H9 S" F9 m3 s" D
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.06 o6 E5 T8 k7 f9 b
3 ?. y7 c3 p _2 q; z+ u
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz6 L8 A$ l( F) ~6 y3 X/ }! k |2 N
) h9 }/ [% h# W- V5 z3 a+ fmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
/ J8 y, V ?! s/ P
* h; G2 [7 R, @1 X: O1 xOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
+ O* o+ @5 B, C( q9 X
& G8 p$ S/ L$ r4 U/ ?" o3 Q& i依赖关系:5 w0 i2 H! \. C3 q: M
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
0 \6 ]+ b, h4 l3 r6 `7 ]& n2 M* M6 N5 W
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel$ V+ _( }" |7 L5 b! ^/ G
modsecurty依赖的包:pcre httpd-devel libxml2 apr3 m5 n4 m8 x( r3 Z
' }# Y5 n$ T j7 y) `. _4 ^/ C
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel7 c, Y" Y1 o& ?, W. h
二.启用standalone模块并编译
1 ^! A8 _% l7 r$ `$ ^$ U% Z k: i% c f" ?5 Z
下载modsecurity for nginx 解压,进入解压后目录执行:
: E( O& Y' y3 j6 F% R! w5 |' G8 K: C# L3 R2 W' q1 }/ a3 ^/ R7 f! A
./autogen.sh
- F0 w* C) p4 {./configure --enable-standalone-module --disable-mlogc% E. n7 ]: B; |& d
make
2 R0 X2 s8 O/ `1 F; q" Q9 _三.nginx添加modsecurity模块
; F" @0 h9 F7 F4 D3 y# {- f c3 Z
# {" i' s: f9 ]5 |) P在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:2 G3 L& J: [# U( o% v" n
# a% H6 V9 i" ^" L./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
7 Y2 d$ Y8 G* L( m; O+ J- ]make && make install
8 N" [6 {( T# \, F9 O. i U四.添加规则
# z' v6 e! C) U) J, F. W5 ?/ s* l& j9 o; y( d% @) |8 U
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。8 Z' O( W3 ?8 |8 S
3 x1 W4 C8 O |. n; E1 \; k' G
1.下载OWASP规则:3 z; s- }7 b, A, h' O6 L
, C& J: ]& \$ @/ ?+ l5 Q; V
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
1 N+ p2 x* c4 i9 N
' \, D+ b3 m! umv owasp-modsecurity-crs /opt/tengine/conf/# i+ \6 K2 x, b8 u3 G7 ~
' g5 G+ z6 f0 wcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf+ }. s; z! Y' }" y. C
2.启用OWASP规则:
& Q& W1 m* L5 f; A+ B! a c4 A: s- n, h+ i* r+ h
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。% U+ x% s" a' w% Y0 u# ~
/ K% ~7 n& A) G3 l# b编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
. I* R2 W" z2 r; x% l4 l
! _9 h! @6 |. E7 D5 ^9 @owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。$ T! H x; ?1 C, G; g: \
0 D' \% K3 L' u8 S$ _6 d, QInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf$ \" Q, b0 m* n4 q! Y' p
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf( x5 h( K& J# W, c6 b! h9 F
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
$ ?$ p' W( i9 T' }6 PInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
9 W$ a& _0 \. _, H2 F% AInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf( i& u& n" k8 a* G- P
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
4 T% T) s7 ~7 `$ D- ^: U! W. CInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf# j3 a+ ]1 Q4 [! @ K: q
五.配置nginx
% K' y# A' j9 |( o& o
9 v5 [- z2 l) ~8 {% @3 j X9 Q# B在需要启用modsecurity的主机的location下面加入下面两行即可:
$ i/ v* Y4 d! j" H9 @8 W9 \6 w5 Z
0 ^- t! a& {6 U j/ |ModSecurityEnabled on; & y A6 V) ~; m- |/ l
ModSecurityConfig modsecurity.conf;
8 @. A" r4 n' B* F/ I y下面是两个示例配置,php虚拟主机:
. X9 s0 [' c- ^; Q9 Y5 f# l* E1 j4 {7 r4 v# j! y
server {9 _. k1 _, @3 J' Y9 l
listen 80;
4 f. J7 Z- @4 P server_name 52os.net www.52os.net;
% _9 @1 D$ ], F- \. {
9 t; B' b$ b/ h5 r3 T location ~ \.php$ {, y5 G I( y S. L1 O: }2 y) G
ModSecurityEnabled on;
0 q, k2 l. ]8 W% y8 K* \ ModSecurityConfig modsecurity.conf;; }9 y$ C7 p6 k* Y0 v
6 b7 ^1 c( E @9 B. A2 w) [ a
root /web/wordpress;$ {" b2 \. A* F( Y& f! q9 `/ u4 F
index index.php index.html index.htm;* z. G2 A5 z( v% V$ \% F U/ s l
: |# }" X! F0 f$ c, u+ h fastcgi_pass 127.0.0.1:9000;) m7 J- Q, t, ]$ `
fastcgi_index index.php;/ ~. W% U' x" Q$ T# G
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;" W6 J W6 G2 y( O& d
include fastcgi_params;- `2 {7 Z# P& L: _+ ]! c
}
5 C" f# k3 u' {; A: A }
! z V4 b! q. I) T) mupstream负载均衡:
- R0 N& j0 _- C& x$ m0 ~ A
# Q) Z2 ?. U7 f- ^7 supstream 52os.net {. J; ]1 B+ o) U( b& c2 Q9 ^: B; Q7 i
server 192.168.1.100:8080;' [$ T9 r' H4 C5 t" b
server 192.168.1.101:8080 backup;
7 H9 Y1 e" b1 y! Y}# }: v; j5 v7 y: U4 Y
% A7 \) x) W/ Y& \! i1 x3 D9 Pserver {
8 _$ P" M, E/ q a3 ylisten 80;
% ~: h/ y! B1 n$ a3 o; vserver_name 52os.net www.52os.net;
* N3 D' B% l- I: [9 K8 e2 }1 V, I* d, z
location / {1 V9 D6 u7 o- x- W
ModSecurityEnabled on;
& }& @2 o# V8 r2 C, Y7 g" l& H( F ModSecurityConfig modsecurity.conf; " F7 j% z4 T4 q; n( B+ k$ A
8 ~" T1 A$ V& Z4 L, j0 c/ `
proxy_pass http://online;
: e+ s8 t# d9 c& p. J1 R! b. c- B" L8 V proxy_redirect off;9 T) r% r8 G8 ^& N* S& Z$ C- ^
proxy_set_header Host $host;" l9 x, t( R; T1 S4 `
proxy_set_header X-Real-IP $remote_addr;
# Y! }# s* A( v proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;. C o2 k& s+ F, J
}1 f3 m5 j0 ^" q
}/ Y- e" N3 E) E* X, \: E0 N
六.测试
5 V' t' U, y' ? ?; O0 N- k; t
) s! e! R" i, z' Z2 d我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:1 A/ p8 ~7 V! |3 P, E
* v4 G9 C1 P" w7 o$ }1 |<?php
4 p) O s( f2 M& q. F! y phpinfo();
; v) K |" _. X; X8 I% f X?>- E# f; W5 K& y3 D
在浏览器中访问:8 Z% u& R- d! w! h$ O, p) M( n( o
1 N0 J8 ^! r4 H, S% D( x) |' M7 L
http://www.52os.net/phpinfo.php?id=1 正常显示。3 ~) E1 B s' e
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。! I) |7 a4 h& x }2 J5 k6 F
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。( A {" z/ y& T- d! ^
说明sql注入和xss已经被过滤了: e" i$ w1 k" }) L4 f. V
$ B3 [! `1 j; i, P七、安装过程中排错
0 E5 T- n1 Q2 M' g _) C, t
Z! l$ _0 `3 `1 [ Z s* D1.缺少APXS会报错; n2 v6 D; z: j. @) I
5 J* ]5 @' N- }6 {+ ~9 a
configure: looking for Apache module support via DSO through APXS6 l) Z2 U1 ? k
configure: error: couldn't find APXS* I- R8 d% o0 P( n _6 F: \
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。2 G) W" O( Y# t7 N
解决方法:
! h/ J* r# J2 O% l+ i- Z0 h$ [0 A! n' d8 {- U
yum install httpd-devel+ T# f6 a7 r5 G6 [2 d
2.没有pcre/ n# M+ o! Q8 N( Z
7 i1 }( I- g6 Z2 v3 d$ B( |5 Nconfigure: *** pcre library not found.1 s% l# _) ~4 V6 h( d9 a4 Z8 T; h
configure: error: pcre library is required# r: Q# F1 `. Y5 z4 x
解决方法:
3 b$ d2 p' M& r9 w
! m2 f' w, y1 T& K- a' e4 xyum install pcre pcre-devel2 j# e& y ~" K" u( ]) d
3.没有libxml2
/ O$ S4 K% |& I+ s: K2 p/ [$ {' {9 T/ T, A& c& j
- s" u% L9 u' W% K! n* iconfigure: *** xml library not found." g) X" }3 h, D/ p7 @3 x0 ` \
configure: error: libxml2 is required
. \$ g W! U' N: @解决方法:' c; ?6 K. u( d5 V: a3 w- F
5 C+ K7 M& t( J& p6 n+ vyum install libxml2 libxml2-devel
! U5 R, b- M+ G3 d4.执行 /opt/tengine/sbin/nginx -m 时有警告7 b. M& ~; P8 i/ k$ `
; g6 e# {( u9 o8 }Tengine version: Tengine/2.1.0 (nginx/1.6.2)
, D1 f; q3 T+ Jnginx: [warn] ModSecurity: Loaded APR do not match with compiled!9 z8 P) g5 W( D/ O \) [
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log* t1 O; Y5 X/ J; K6 A& k& ?7 S$ [1 b
! |$ U% _; B8 I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.( C1 E6 {3 M; d8 e( e) z3 y" |- ]
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
( q& B) q/ A& w1 Z d3 ^2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!/ X \: }' |# `7 K' P. W* }
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
3 w( Z" L z* X# W9 u8 b2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"& Q' X6 z/ u* X3 [- [; P+ s
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
5 t9 ?% ^" k+ v解决方法,移除低版本的APR (1.3.9)
# d' C. `' }# c$ n; J" ]7 c7 o! C W
yum remove apr
" i3 t& K1 l* u' f, G5.Error.log中有: Audit log: Failed to lock global mutex
- N* L8 V) b1 @
1 ~6 H, l+ N6 q5 Z4 E3 L2 _2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
- K/ t) q B6 Z' jglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]7 v8 e+ ~! {1 F. |) M. ~
解决方法:
& ]1 p2 G" F4 r; Z; d编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:5 y) M" q4 W8 z+ Z- }# ~7 F
V) s3 x: ~, [% T5 R! n" SSecAuditLogDirMode 0777& S- X7 U7 u, j$ [+ R6 [5 J4 Q
SecAuditLogFileMode 0550
- Y' l. D, | d& A5 lSecAuditLogStorageDir /var/log/modsecurity
7 W# {) |; v: D$ S p# ^SecAuditLogType Concurrent
" _3 S* e, _) g. S1 t! b参考文章:* Z9 w' g, d& s# T: D% E
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX: R' z/ O" r' {, \5 L$ c
http://drops.wooyun.org/tips/2614 |
|