|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
9 G5 x. C$ Z" L* Z5 \1 V8 _1 H
8 ~* c+ v& Z. F6 V! e一.准备工作
8 s- q7 P. Q- ^
$ l7 n, c4 L% H系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.07 G& m" h; _" ^% f. Q
5 _% h; N- b' X( h9 s% r1 z$ ~0 m! mtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
* H$ c' @7 n k9 }/ I& K& K ]! W2 w5 J2 \5 r: b+ ~
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
. x- U4 ~, y8 q* Q% e: F" O7 j/ B. Z( w/ X) x' G( _
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
+ A' R3 y W; ^$ w/ t
! z& Y' a, y, t3 {/ D依赖关系:
' f1 }, X6 z1 \tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
5 I0 k. |7 k8 C2 }' C# h7 a
. f w/ e4 y, Zyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel5 ?/ }$ g" `; v, q* I
modsecurty依赖的包:pcre httpd-devel libxml2 apr
0 G1 m2 t# A; l: Q
9 l' H8 t* @, r8 A% O; X7 e# @yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
1 _+ e) h" D/ A0 [+ A二.启用standalone模块并编译
5 R. s q: M+ T& z1 e& f) M* |- D5 I% s- Z" C- V, H h4 v- i
下载modsecurity for nginx 解压,进入解压后目录执行:
6 v6 Z& N3 D S. T+ p4 H- e3 |! j( X8 }
./autogen.sh$ A0 J% C4 _, j, w, i" H
./configure --enable-standalone-module --disable-mlogc3 P9 Q* b, Q' ]" F
make
- L7 h/ l: D- W3 j# B三.nginx添加modsecurity模块
) j2 w3 f" t* z5 n, {
6 V/ `& Y9 a4 _ X3 }7 ~在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:, Z+ `& r1 ]- v8 v+ ]
# x9 Z. p" R, w2 @4 x9 u' V1 Q
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
" N+ G) H5 b+ R9 H K# w$ Wmake && make install
6 ~! _9 T; ]& a$ X( @四.添加规则1 i( S' u* l& L' E$ h
& l1 t4 I% y, t, U
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
/ T2 v, S& A5 c1 T" _; k; W* e/ w4 F% q% ^0 o- T: `: k
1.下载OWASP规则:
, L; q/ @: H8 }' T
8 S! k2 \; b# l# J0 egit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
/ J! U/ d; }( v7 [- S! ^ p( [. |# y; ^1 k4 R5 z
mv owasp-modsecurity-crs /opt/tengine/conf/0 V: k0 s0 t$ [0 y
4 Q5 \, R: d, M% `5 ycd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf; I: B+ c9 f4 [* q' n
2.启用OWASP规则:/ |: r8 [5 C4 m* @
: k) b& T$ D6 b- y9 }- e0 `/ s
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。7 {% H2 i( `( p+ l9 i
1 C; [3 n6 c4 @ A' \# [: l
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
) U8 u3 m: g: x
$ ?9 C9 m! x5 r/ \" P" yowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。/ r5 n! I q4 ?: ^/ V7 l, j+ P
" c& Y7 L0 d- ?! }+ r1 [% rInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
' i ^' S. B% L% N6 ?% r( WInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf# ^2 C' f2 ^2 P1 E. w% t8 `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf: m4 V4 i; {% _
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
7 {8 E% r/ y( I! yInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
8 I2 C$ @' {3 I1 b2 r- X/ OInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf% ~ F& [1 T2 s( T
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf) A! q0 c S3 [( V# a# i/ X
五.配置nginx
$ f5 B$ N, S, W9 L8 \) I& q, A! x5 D) y* |3 q& K2 {
在需要启用modsecurity的主机的location下面加入下面两行即可:- |. s+ C! h; E" N; A
% B7 `5 x- u3 }9 G0 D8 d
ModSecurityEnabled on;
7 w+ ^1 h" \" r \% Q, HModSecurityConfig modsecurity.conf;0 Q+ ?6 ]1 e* k& S
下面是两个示例配置,php虚拟主机:
$ X2 s: b1 q+ M3 f. z) o* a6 R @, a9 n6 ?/ B& X
server {: N- V7 n; @4 T, n, d& Y
listen 80;
( L3 ^) M! m% Z( W& `' N$ n server_name 52os.net www.52os.net;' c! N: t; h9 v, T
( @5 B, D& p' o: X& r2 a location ~ \.php$ {
9 e" k0 `- U6 Q- } ? m ModSecurityEnabled on;
' a5 D" `- o$ P/ r& s1 g5 W) Q8 A ModSecurityConfig modsecurity.conf;* e B+ c: ~3 s- z2 S$ j
& B# \1 q6 j0 K: { root /web/wordpress;
/ Q' k! N+ S& P index index.php index.html index.htm;
R+ F; l6 o; m+ p: F5 ~ 8 f& |& q! G$ s s2 P1 ^4 P( Y) B4 b
fastcgi_pass 127.0.0.1:9000;7 ^: p/ y; A$ o8 P3 ?
fastcgi_index index.php;( v2 h8 w) d9 O) d% T% c& f$ ?
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;' e+ ?3 W5 Z% t7 H& X
include fastcgi_params;
8 q( s0 [0 K8 l; r, q8 K }( f& M- }% |; _' j, [
}/ C- m3 o' R% b5 Q' c# }5 F1 g
upstream负载均衡:
" F. L9 h" |7 V, ^1 N% i# e1 r' o# T, j; O. M
upstream 52os.net {
' h( E& f1 V/ z/ D# \# t# M server 192.168.1.100:8080;7 H, F+ C8 Q' k: G
server 192.168.1.101:8080 backup;
1 l* L- @8 ?/ m% A( m% ?}1 \* f1 d7 P/ o9 E' q
: v$ `# D1 _5 j& m
server {3 ?2 X4 E3 ^+ e5 v
listen 80;* y4 g7 C. L* B. M2 \$ x$ d- n
server_name 52os.net www.52os.net;+ b9 D4 G' r$ l9 Y5 K8 q0 T7 b
5 Y/ i4 _' }& u: b! rlocation / {& O' {, G4 s; p$ o5 M l- M
ModSecurityEnabled on;
1 K7 Q2 h1 E c2 B+ s( E0 k( U4 { ModSecurityConfig modsecurity.conf; 0 A- }3 l" [/ [* r G( S* U% i Z
/ I' n8 u- }9 G: O* B proxy_pass http://online;, S% L W& X0 N' U
proxy_redirect off;" g/ i* R& v. a
proxy_set_header Host $host;
6 A9 i% ?( @7 l proxy_set_header X-Real-IP $remote_addr;
! _6 ?1 ~. T' X' h8 s5 p" \; ]1 T proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;5 c1 d4 x6 [7 w
}. c$ p o4 B8 w2 f/ p
}. v: n1 ?' T3 t5 |! I% M; r
六.测试
. U O6 V4 u1 }
8 v4 p* x6 F# r我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
c* H2 H9 ~- ~
1 }* f9 A3 x# @9 X% W/ b<?php
7 X2 @! _7 f7 u2 C5 X! @: \, |$ X phpinfo();
: Z- o# s( G" n" D?>9 \) ]" N0 K5 E4 A. p2 n' t
在浏览器中访问:4 @. Q+ B- f3 R+ s) u
$ V0 g t8 \. U6 i
http://www.52os.net/phpinfo.php?id=1 正常显示。
o; J* \% B: R9 ~. Lhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。1 R' |! B$ \2 Q: }. [: ~# Y0 a
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
! W7 r7 \4 @' u. z说明sql注入和xss已经被过滤了5 J) L8 j+ i( D1 t1 w
+ T }0 j4 K) n3 v& f七、安装过程中排错
3 B7 y6 O' n. m8 [( H
3 X) ?9 J8 M9 u5 e- M) ]% {! C1.缺少APXS会报错* ^: z: c+ H f5 V, U8 d4 ?) v
: F. c8 y$ ^0 Q- C+ hconfigure: looking for Apache module support via DSO through APXS
1 r) E' i* n* o2 r" d4 ~1 K vconfigure: error: couldn't find APXS
% M+ V: `' _8 u0 A/ zapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。# p, p8 N4 m: O, y1 y; }& l
解决方法:& Z# X& p5 i6 x
, U8 @! y; b& p% l d
yum install httpd-devel9 z3 {0 N, @; b/ A4 _: M
2.没有pcre6 T, W7 \4 m9 r W# `) X
' P5 M( W% s' _4 B# E* A" zconfigure: *** pcre library not found.
m2 R+ b$ h2 A$ v$ Pconfigure: error: pcre library is required
) z: h: y3 h, f0 z" b解决方法:
8 `7 U5 S1 G" d5 @! s+ o- b- v9 l+ K9 ^2 Z
yum install pcre pcre-devel
# E& F: V8 ?# ]- ` M; h2 Q3.没有libxml2
5 p: E9 Y: m. d& A3 b P. [- b0 m/ g8 _5 d5 d$ t
8 Q) h; o! V& X- S E; z% o/ v2 K( o
configure: *** xml library not found.
2 F' e0 X) H, }' V) rconfigure: error: libxml2 is required" w8 h/ G' a0 N) b! S5 b! i/ |8 C$ C
解决方法:4 Y4 V% B6 u' m9 F" x* q4 D; n4 \
0 o7 [$ M0 H& k+ ?yum install libxml2 libxml2-devel7 Y' U% ^0 F1 {# Q e A; a* \
4.执行 /opt/tengine/sbin/nginx -m 时有警告5 V4 y( J4 P s4 b% J
& V) _7 v7 v2 A
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
+ l) o0 z, x- ?+ inginx: [warn] ModSecurity: Loaded APR do not match with compiled!$ i) {6 E+ U1 H
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log" U1 M+ Z! S+ T1 ^' G# L2 Z2 P) s. h
) L {7 h1 y) l6 X4 r+ b. C
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.: d, G7 f+ m( y9 e a
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9" b$ g; [9 k6 ?9 b: ~
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
: q# ]$ d" D- s% [# D5 X2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
* X4 t }% n, k# e8 `3 u# g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"4 n/ P6 \% w# ^# ?7 g9 q6 O
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.* |% L" G) l1 \0 ^" C- p Z
解决方法,移除低版本的APR (1.3.9): ~6 k5 w. z! r0 G5 E3 u" }9 y
- r8 W3 ~+ U" Q
yum remove apr
' B& g g0 q- V8 Q% P1 p) L5.Error.log中有: Audit log: Failed to lock global mutex" p( s5 i1 \% l# u" u0 ?
( |9 v; B* u! O" ?4 |
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock # }! p* z7 u9 J
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]* R* n, D% C3 m0 H: s
解决方法:
3 x5 S0 P3 m1 n7 d编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:5 T6 {8 N- y' x
2 ^( J. W9 R6 U7 Y3 [3 I( Y- xSecAuditLogDirMode 0777, x- C: X9 [% Y: f5 F2 n/ x
SecAuditLogFileMode 0550( v& H: r1 a0 |, x. _( N( G6 l1 S
SecAuditLogStorageDir /var/log/modsecurity3 v) F7 ^' k; v. P( Y) E( b1 i" C$ A
SecAuditLogType Concurrent
/ k3 M( P: ^) k8 H2 r A5 |参考文章:1 k" o$ S) X2 x5 u5 w: N P: Q
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX3 ^- w" H/ m" d5 i1 O, |
http://drops.wooyun.org/tips/2614 |
|