|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
/ H' w) C$ O. \) J, b
$ M9 h. G9 C% c1 w0 H- k |5 ]: k一.准备工作
/ M+ \, t% Z0 H0 g# o" y3 u4 V# ]9 z7 Y
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
. y) N+ e! A) z9 `/ D1 L- m5 Y. w! j* K) Z$ d P- H/ g
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
5 _/ G2 C- s: n! h# k: `0 v0 m6 J/ P4 D4 ]
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
% @2 N4 B$ F$ q# n" r; X t" ^; {. N% d) a3 O
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs$ b4 S" B h+ O# j) q) A
, u! d) E1 v! M( g O1 z& Y, S5 b依赖关系:
8 f$ }1 K! S/ k0 @0 z" htengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:5 S8 D. J2 w$ j' M* M5 v# K
& ]! H, y1 |8 R/ kyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
- G& }3 M$ z9 u; ]% n2 Emodsecurty依赖的包:pcre httpd-devel libxml2 apr: w( L* Z( k' E, y0 P% d e
) Q. f" K3 h* h/ |& P
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
4 x4 C% g2 j: m二.启用standalone模块并编译" h' @$ N- N1 s- N. n, M; D
9 ]. L! T! J6 p$ W下载modsecurity for nginx 解压,进入解压后目录执行:
6 L# H! Y2 m8 h3 r0 j9 ?# v' u( X
: H# N6 S' B, ~; Z! |, H( q./autogen.sh
& ^5 R/ l" D$ y: d. I% _- ^1 ~" L4 q./configure --enable-standalone-module --disable-mlogc9 }. G: u; F& @3 _* J
make % `9 p8 p# P. _7 t+ S
三.nginx添加modsecurity模块
( U( V/ q s; ~: D5 Z+ O
* n: ]9 A7 I+ N; [在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:: ~; Z/ S1 f! N& A0 k+ m) l
# t1 \ H$ Y; M6 b/ ]7 U/ U./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
7 s% G5 U% p: ?+ N! M& _make && make install
/ _9 ~% X. N: C四.添加规则$ G( D9 N8 G& N& ? j
: X: Z) e% s# j: V5 S4 l% ^" K9 V
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
( B0 a: z+ z0 X# h" h# Q- l
" v9 _7 Y! o6 S* T7 I1.下载OWASP规则:
( O& k: X$ ~8 s% y4 ]; {* K
$ b9 \, P3 E0 t1 z- u" E5 Egit clone https://github.com/SpiderLabs/owasp-modsecurity-crs% v7 `2 T8 z7 Z `% z
, T4 l# L. p/ T4 V0 Q
mv owasp-modsecurity-crs /opt/tengine/conf/5 B- ?# U; X" Q* L; ^
1 }( u2 s$ }5 z9 f6 jcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf4 S0 M1 {& w5 `1 t
2.启用OWASP规则:8 {4 w A+ S7 `9 n3 k4 z: V. z& A# Y
0 \( {+ c; ]/ S( u$ I4 s复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
" `0 T# i2 D6 C' j' ?
2 N+ ^4 j( x, n9 V, L) ^4 _编辑modsecurity.conf 文件,将SecRuleEngine设置为 on7 a$ `) W: Z$ N1 f& b( e
8 s+ a! O0 {4 P2 u
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。, [# d' ~. A/ m" E4 B: I. Q1 g
4 _' F3 ]; [: `# @/ t- KInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
% J* l- }+ V! Z' VInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf6 l! f0 m% Z) \5 {8 ~* m1 d
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf4 c; n) }2 Z9 N; i2 y3 v, ?
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf2 Y6 N( S& O* K/ t/ {. ` @
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf L7 L6 J: `. h* J) d
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf4 u$ C3 G, p& A9 C& i' w. e
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf, m, A( u) I7 @5 x) d
五.配置nginx) T# _7 [+ `5 ~5 x+ k: Z/ q2 l
% M, X1 o7 @1 ]3 A6 |在需要启用modsecurity的主机的location下面加入下面两行即可:% p- Q6 c8 |# w; q6 y" W. O/ h' G! [
: J) R/ B- U- d* E$ k
ModSecurityEnabled on; ( B/ e! T4 h4 d" y
ModSecurityConfig modsecurity.conf;
9 i9 ~( @" L3 R5 ^; M# N1 N下面是两个示例配置,php虚拟主机:
/ m6 Y/ b3 G4 M. I' s% ~: S: I) [! O) c, U$ [- \- j: ~: e' Q% a
server {0 r* U$ n* ]5 H( a' e4 m
listen 80;3 x, g) m/ M0 u6 v! U
server_name 52os.net www.52os.net;( C- l: E) o4 M
3 I" [8 E! f5 e' o" l
location ~ \.php$ {
3 x6 `5 i) ~* E% p' ` ModSecurityEnabled on; 3 O$ u9 r+ A3 w
ModSecurityConfig modsecurity.conf;. K# ]) ]2 v4 `1 _: S* }
2 u& r. w/ v n4 b5 m3 j1 G8 W( j root /web/wordpress;
, Y1 C' T$ l( n7 Y7 i, H4 ?' U index index.php index.html index.htm;5 ]. K3 C7 y/ l. ]8 |' y
# G% g! Q9 |+ d
fastcgi_pass 127.0.0.1:9000;
1 T+ _" O4 H, J# L4 a, c: D2 _ fastcgi_index index.php;
( V( v% {% {" p fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;" u) M) n# Y) F! S, N: s. m
include fastcgi_params; @) b% C1 q/ M$ K% B, i( L
}
, w4 S I0 _. j) u }7 N! E7 S$ V; N* U; e- P( y @6 i
upstream负载均衡:
' _# {: Z7 |# k' |* [, T" _8 \5 Q7 R- g, ]
upstream 52os.net {
1 J0 l4 R/ x O8 {( k server 192.168.1.100:8080;
Y; J) P5 L" ^4 c* |) k! `. e; f server 192.168.1.101:8080 backup;# u- L3 X& u$ n2 R' J* G
}5 r- [; n$ h- f; g/ H* P) n2 D
) T) v# u3 _6 E, |4 _8 F
server {
' U# l; {5 J- @ r. B3 ^) alisten 80;
3 P' J6 C, A: f) Q+ @7 x2 Kserver_name 52os.net www.52os.net;9 Y' W" {6 V* Z! q" F3 n; J
4 u; q N+ x+ E/ r- ~( z1 T7 |
location / {* L. J& e- p) L, V8 V. l& v5 q
ModSecurityEnabled on;
8 V7 N4 A+ i) Q( O+ Q ModSecurityConfig modsecurity.conf; + l7 p6 ~/ p( Q& ^, r# ?9 y
- o7 C2 q$ U) s, m2 a3 H proxy_pass http://online;% x- v! j9 L0 \8 o9 I* L4 a$ |
proxy_redirect off;( G4 V) n% } O1 ]' r
proxy_set_header Host $host;4 {6 \5 ?; @0 B5 |
proxy_set_header X-Real-IP $remote_addr;0 S. R0 M5 J7 _2 o
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
; K+ k# c. ^/ {+ X6 B6 w5 x }* r( G( k' g# }2 w) W$ E- g
}
( z8 f; S, h8 _& `六.测试
! v3 ?5 s' E# T1 }$ @# q2 P* T& C1 [1 Y; r* a
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:; {* H; Q. y" p1 L5 P v& l) I
6 u5 F# `/ p9 Z) q- l0 Z
<?php5 j- F* o7 ` G2 E) }$ Z- W. j
phpinfo(); 8 O \* X6 L) ?2 T; A4 S
?>
. H3 g7 j, a; g在浏览器中访问:
8 ?( C/ u6 f( K9 H3 Z2 i# w/ k% `$ J% |/ e
http://www.52os.net/phpinfo.php?id=1 正常显示。
' m+ _! Q# \4 f% j' L8 y# [ _http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
H0 M" m6 k8 e# |( ~1 nhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。. G0 ^1 N" [8 {
说明sql注入和xss已经被过滤了+ `% `) N, y4 A+ x2 h
- H8 I) T2 N- e8 q
七、安装过程中排错' w! H% `* V! C; N* h7 Z" e9 k' l
4 ^8 }6 `7 n0 g. W1.缺少APXS会报错
" a p6 a: X5 d- b9 p
7 \; e8 t% s2 h- h: Gconfigure: looking for Apache module support via DSO through APXS' Z5 P/ L8 Y, f- Y0 ^: p8 l% O; {
configure: error: couldn't find APXS8 f# r+ ~. a! v+ \6 }) V
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
% l. c! e; E8 C4 {' c解决方法:" p+ Z7 n( d0 g$ E7 o
6 |- Q& T6 p: W# f0 o
yum install httpd-devel. e( s. p' r+ L" ?, W# d
2.没有pcre
' e/ N' {* n9 e8 w3 Q! a' Q% f1 M0 ?% U# d0 w
configure: *** pcre library not found.
; o) ?/ |6 M+ }& G- O0 mconfigure: error: pcre library is required: N& S/ Q7 ~( N; V
解决方法:! X, w- U6 B, T9 n! ~ k4 k
7 I6 b" T4 c ?4 hyum install pcre pcre-devel
% u' X O% W! r) Q3.没有libxml2' F& f0 O/ C0 \: z; e8 }
# o O' U, W8 T5 G3 R& P
8 _$ {1 T' j: b7 W
configure: *** xml library not found.% t' J( O+ p+ p2 k$ M
configure: error: libxml2 is required" n5 W* K/ V1 \5 B
解决方法:& h) K z5 B i H: y* Y
) |2 U4 [ v9 C7 f
yum install libxml2 libxml2-devel: t2 P1 S: ?* X" Y
4.执行 /opt/tengine/sbin/nginx -m 时有警告
) S5 ^1 {' f1 v4 O2 l( ~3 |1 b+ U+ L
Tengine version: Tengine/2.1.0 (nginx/1.6.2)5 |3 J7 W8 R( X3 H, D' R/ p
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
5 j' p) f" R! L原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log% `. _# f. d7 W) D& \0 }
* r8 u& v; l# s$ b3 T- q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.4 M( t$ V: m) F2 A5 r
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
5 R$ ?3 N7 v3 C, q5 K4 c5 {2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!: h5 g* k. ~) b8 a+ G1 s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"% d- g/ e+ L/ s2 Q' d$ ~7 i0 a
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"1 O( N# e, H1 l! o* b {
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.8 p. G8 R9 i4 W1 X; ?$ ]. p! Z. w4 f
解决方法,移除低版本的APR (1.3.9)) M. Z; Y0 r) O8 ]- j5 c
$ z6 k; D/ S/ H( a. _; H( n7 p
yum remove apr- v6 r8 Z( a" X' L) N* M5 b
5.Error.log中有: Audit log: Failed to lock global mutex7 ^6 `! g0 L4 Z, E
; Z0 a% N0 n; R5 @ x( \
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
! t b! W& K, `3 Vglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
+ b0 m# D3 u3 h4 {3 z$ K解决方法:% C5 o K$ V6 i& F& Y; C3 m! v* X
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:1 D' j4 \% T- Z% k
& c! }9 n; h* p: Y, ^
SecAuditLogDirMode 0777
: A1 D+ L- `4 x! t. d l! CSecAuditLogFileMode 0550
, n, X4 p. C/ OSecAuditLogStorageDir /var/log/modsecurity
- N, _0 d4 i- C+ r4 @. a& TSecAuditLogType Concurrent: f5 d2 _# G+ c* W8 ]
参考文章:1 x! K# N& F2 s8 F
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX5 C1 l& v4 \& D& H$ R
http://drops.wooyun.org/tips/2614 |
|