|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。/ c2 D" r$ @7 A/ }. l6 F
. K+ b' h. q, M3 e4 ^/ U一.准备工作( a) K# C: E5 g: Y6 X: c
) o- F8 m+ J( _/ Y- E系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
5 s W5 P5 b: V% s% Q( {. A" o: T' `) T* N) B9 Q
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
8 {3 w$ f: K/ W% w
8 y- O3 N# h8 Pmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
% c) d' Z8 |3 _: x# X+ f6 ~/ x$ g
# {, V3 g$ q: r( u0 sOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs& ] L& @; U( @4 _+ n
! x/ P8 ^. `7 o* {2 Z
依赖关系:5 ^; l: M. P4 e2 x d% G, P
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
% C' r( j8 ]; v3 N* O, g# m! T
9 F: J5 `& @5 `' N5 f2 _yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel e/ g1 V5 P0 R9 K- a6 v1 M
modsecurty依赖的包:pcre httpd-devel libxml2 apr5 k+ @6 \# M9 ^5 k
" N$ B" g# ^3 s7 x
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel, n5 o3 |3 {9 S, t
二.启用standalone模块并编译+ B' [$ b }9 e P$ f: a5 v
, p6 h5 N' [5 S下载modsecurity for nginx 解压,进入解压后目录执行:/ r- @( y$ A' Y; D* c- W% I
! V/ T! `) [- U0 d @./autogen.sh# J6 }& A$ H- r A* A- X2 @
./configure --enable-standalone-module --disable-mlogc4 y4 `' f" q. O% \
make
9 Q# t i& w0 L% H% j( k& r三.nginx添加modsecurity模块
. D8 H' y z, J3 [& w. v- u$ f4 |. a. g
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:$ C2 D* x {1 Q* y9 D* Y
, c6 a1 d( J* V& e: n" r
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
/ d8 o- ]6 i$ dmake && make install
' q3 s0 V& X1 x" q0 C! A四.添加规则" D6 ?/ c: e3 X* s& n/ Y
+ h+ D9 W2 r: ?8 l: Emodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
" Z( c0 K; r$ M0 [) U) U. q0 a. g1 Z
8 }. W6 G$ `$ t6 B" `7 _- k }1.下载OWASP规则:
8 N7 q' @3 q8 C+ a# l* Z0 E4 T* r+ a& o0 w% O& ^( N1 p
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
$ R% m- K# j" x4 a9 g) m0 I$ Y
4 q/ \8 b& g' Z8 W7 x6 Smv owasp-modsecurity-crs /opt/tengine/conf/
7 z4 |* f' q, B3 x3 t0 ~
]7 S0 Y. D) L( X1 d5 \cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
" @' d2 e' y4 j2.启用OWASP规则:
" E- J5 X' [: x2 b5 _# V$ [; r/ t7 T m! i9 y8 {
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。$ n4 w! a) p" [7 p; G
! U# Z) H2 d4 Y! X编辑modsecurity.conf 文件,将SecRuleEngine设置为 on. v% ~9 M% y3 O6 Y$ X6 S- K4 h' m
[! z: W* j, A! @0 `
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。+ ?0 o( S' g) @' q# L/ E: [# _; s
, L& y) N, f& L+ j: [
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
/ A: F9 q1 @3 M8 s9 _- BInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf% C9 C8 m4 F, d& i. A
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf1 b0 N3 ?$ m/ a' D
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf* L7 `: }7 u9 V) v" f% F; d" T2 n
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
/ I; Z( F1 Y# F, J5 g1 RInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf: A: ^) b& M N/ F8 S8 U9 R
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf8 n; ~4 |, Y% B0 [1 E
五.配置nginx
@$ t1 S4 s- J" B7 c! K1 d! a$ i; D! d
在需要启用modsecurity的主机的location下面加入下面两行即可:4 B; ^2 v7 \6 B/ o; {
* ^5 A1 p2 |7 b/ lModSecurityEnabled on; , h$ b2 S( K" N0 e
ModSecurityConfig modsecurity.conf;) F/ b2 P9 X, o
下面是两个示例配置,php虚拟主机:: ~5 m0 c4 r: v4 ~9 o' H0 t0 c5 \
4 D _4 z0 a! M5 W% ^$ @( r* K
server {
) e; ]& U C9 [ o; t( u D8 X listen 80;
$ j' e$ Z: o0 a4 p6 @: W server_name 52os.net www.52os.net;
& ^: J8 I4 X8 \9 h6 E ; {- X# x6 Q( T' X0 y
location ~ \.php$ {% f' y1 E$ H0 X& W7 t; |
ModSecurityEnabled on;
& r3 W# u, C5 m- a: l ModSecurityConfig modsecurity.conf;7 [% x2 d7 O2 E' [- f+ G* I; O
* r& n0 t9 q8 W7 _/ f root /web/wordpress;/ [- x* L e" o' p/ f- V
index index.php index.html index.htm;
( }* ~& U3 A" `1 `! I/ C. K! G
7 F$ Z! j+ h C. _. m fastcgi_pass 127.0.0.1:9000;, K, w. |- Z3 _; _
fastcgi_index index.php;* u. Z |8 y0 M' q# @
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
! s9 [% v8 _2 ^4 o' L) F6 w include fastcgi_params;
4 J) H, q( A) p( R! X/ ~# z9 u, @& L }! ?! b7 X" L* d: {9 R0 R& J( L
}
' l$ q! |/ A/ J. l& Bupstream负载均衡:% N$ z" \2 s2 C6 b
* d% ?. H" l1 t3 _! r9 aupstream 52os.net {" }! i; H1 [# W3 y
server 192.168.1.100:8080;: a& A. i) y& ?+ p
server 192.168.1.101:8080 backup;
, d3 C# n9 i: d0 E: s}
' G3 j! @% ^1 {1 u7 z/ s, n. L$ @# c+ \/ _' s% H4 i
server {
% Y9 c; b6 {* V) }4 w- `. blisten 80;- {, v s6 Y4 z9 f- w% \
server_name 52os.net www.52os.net;8 E8 T9 h# M0 d$ t- y
) u& n. B) w4 R+ u
location / {( @, E9 ^& ^1 W
ModSecurityEnabled on;
& S8 h0 r* {+ m! K4 [# X0 a ModSecurityConfig modsecurity.conf; 3 D: @! ]# @; ^3 B2 n5 @3 {
2 Q3 R! \* S" m: Y5 m* w proxy_pass http://online;% q" \+ _/ O, D
proxy_redirect off;( d- l8 l5 T( R, K& t0 U5 n; v
proxy_set_header Host $host;& P |; [* d% {5 ?0 U+ v
proxy_set_header X-Real-IP $remote_addr;
7 p: O) D4 i9 p. [0 ^, I proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
1 ]* Q1 Z: _- d+ j4 W: N }
1 p9 I) |5 @, E$ z+ f( S- q}7 m' R7 W I& x
六.测试5 v- z% d4 J6 X6 M( T6 t h! D
% p$ Q/ z) z# l6 |
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:5 R, f9 U+ U8 o# J8 c
5 x" _0 @- D" Q) ?/ e
<?php$ K* v. }$ l" ]0 `, L( i3 n
phpinfo(); R6 g8 U- n* H" b9 n( e
?>
- E5 n/ o/ D* ?在浏览器中访问:
& ]3 F8 o1 Q8 D
6 E4 H% e5 n; ^$ p( rhttp://www.52os.net/phpinfo.php?id=1 正常显示。* {1 W2 n- P- r: C' Z
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。# v# E9 i( L( X
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
& y0 g8 R: N: ~* X说明sql注入和xss已经被过滤了
' Y$ V, S! b3 R' m" H. H2 f- A# m& S
5 K, K! I4 f6 v七、安装过程中排错" w& J- z' x y4 ]5 `4 v7 B, n$ e
N, w5 P- k R: { ]4 b
1.缺少APXS会报错: X9 S% P ]) F2 C; n/ m' m% P
& ~) c- |& n8 i+ ~& }* Oconfigure: looking for Apache module support via DSO through APXS
6 u# n1 ^7 K+ _3 ]9 sconfigure: error: couldn't find APXS, z& c; K% {+ ]
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。1 X3 s& C& A, A) l
解决方法:
' f! ]' j7 J( L7 {
8 B! f4 B' j) a- I) o& Xyum install httpd-devel8 j3 H& L& y6 I) \& K) g
2.没有pcre& {4 A$ m+ L) ~% d' H
+ j& i1 I7 w( z m9 C, v
configure: *** pcre library not found.3 y0 l' n, d& x8 A. c% x, j1 c* y7 P' G
configure: error: pcre library is required
, ^. N5 \ q7 ~+ o解决方法:
7 B. J' X4 n* p% R% C; t, c0 v' P3 o* J* i
yum install pcre pcre-devel5 B4 u3 U2 H4 a- h3 n$ v4 s* E7 I
3.没有libxml25 c1 }' s+ G* P& f c8 g
* K7 Y- s; ]' i/ w
0 u4 }; @; H# S+ e% ^+ }; f9 b O
configure: *** xml library not found.
. P) l% U( r% X0 x3 d" b; a8 V& iconfigure: error: libxml2 is required
8 k, Q" B% ?4 d J2 M! d( g1 Y解决方法:
; w* N9 l4 ^" G
: J, W& E' |: T$ `# G8 Eyum install libxml2 libxml2-devel
Q. y( G+ { T4.执行 /opt/tengine/sbin/nginx -m 时有警告' ?' t' W D3 g; m) q0 Z
4 f; G9 I1 _5 `8 ~( r$ h$ cTengine version: Tengine/2.1.0 (nginx/1.6.2)9 Z; a* b. `+ u8 ?/ T
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!* [) P E2 U+ g3 _8 j; G
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log w- c( O) x% D1 f4 Q& K
9 w4 k9 V) p2 y) P' F' U2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
6 a- l/ v8 c3 R& ^" w2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9". @8 I1 w/ y, c( E6 j2 S
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
9 W i7 n) s4 M2 a$ t2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
+ k: h3 l7 p; e* z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
" E& a* _ h" W2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.8 f4 y Y% b: Q) C1 u4 }
解决方法,移除低版本的APR (1.3.9)
# Q5 o% p' Y9 }- ]6 \% r
. c# S) `7 T* ?! Zyum remove apr! R8 Q4 H6 r2 `0 j% z- k
5.Error.log中有: Audit log: Failed to lock global mutex( w0 q5 F# H' ^+ O) \9 ^# U. O* G
, x! C( M0 g, @- `' V9 u5 O
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock " e0 a# R5 M, z
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
; B1 \6 p- u6 L" l9 k2 M/ L解决方法:, W$ {+ L% {' H: |8 u
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
/ W2 y8 v( F8 ^6 T" X
# ]- w4 _) q: m1 [6 SSecAuditLogDirMode 0777) k: t! |2 v6 @ x
SecAuditLogFileMode 05501 j& ?( `7 p( x
SecAuditLogStorageDir /var/log/modsecurity" x$ R8 D7 D- l( m' W& L8 U
SecAuditLogType Concurrent1 k7 u( {7 q% w* A5 J1 |! ^
参考文章:
( a3 }) _, g6 e2 d$ r# B6 Lhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
8 J) S; M0 C- t; qhttp://drops.wooyun.org/tips/2614 |
|