|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
; ]4 {# J Y r+ U/ h! o! E# B: j
一.准备工作+ s8 _" l6 V/ y% P9 z
+ b0 R6 V' l/ l8 e. x# R2 V* O系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0; _! k; O! O% X' ~- R
" q1 Y1 @! ?4 p. U" y7 R B0 w
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz, [" [0 E# g8 B
* e7 d. z3 \0 D1 v1 T# J" A
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
6 ?5 r+ H% {: l2 A4 A# v- s# y* P/ n! l% q7 J2 {2 U1 \. R
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
4 ~7 V6 C! |! @6 n
4 q" ?# W0 v2 r' L! d依赖关系:; ^2 S. G1 E' Y4 O5 @) V# T4 d, g
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:6 Q' I6 b5 k6 J
3 x% g* ^" q) O) W" d+ u6 S/ myum install zlib zlib-devel openssl openssl-devel pcre pcre-devel% C2 T0 O' i. l4 ]' E$ |
modsecurty依赖的包:pcre httpd-devel libxml2 apr! a* D7 ^* d6 H2 u. {) b! h$ |
3 b2 H4 f1 v9 Q& o4 r8 k) \2 r& l* _
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel k5 K9 B9 p( c; u( U* Y( m, f5 U- p# c
二.启用standalone模块并编译% J0 C0 A5 e$ H+ L5 u7 ^8 b
: X4 Z4 M/ Y: X$ h0 N# h+ h
下载modsecurity for nginx 解压,进入解压后目录执行:
: C# |+ D$ w) t% T
8 R3 B, D7 s. {- R4 j8 }6 x/ g. \./autogen.sh
2 V/ N4 H2 g$ ]* N; f./configure --enable-standalone-module --disable-mlogc3 o _9 \/ X# j
make % Q* x" ?- F0 ^" u2 x
三.nginx添加modsecurity模块' D+ I0 C% ]# D9 i$ C7 A% s3 x* o
5 a/ J+ g h7 K3 A! V- w
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:) o5 g/ ^+ |1 d& q: Y
& D- H* P: f, x4 Z9 M( X
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine4 B4 I3 x4 i9 O$ }1 p# u& ~
make && make install
- Q" l2 a9 o! u6 ~0 T C: F n2 z四.添加规则
& W3 L2 a6 W# ]
" \# u! V, Z- y" o8 \$ H5 m3 v* g$ ymodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。( l0 ~; S+ o T7 D. A h
9 y5 V, m% e# d9 d% r1.下载OWASP规则:: P6 q, ^9 ?% D ?; s+ m
# _6 Q+ G+ D% I: K' A) j6 zgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
2 n0 H8 Z. d, o9 O: L; ^5 I: ]) _8 ^$ O! F
mv owasp-modsecurity-crs /opt/tengine/conf/; ]$ f! [7 T+ n& S0 I6 j
. ?9 Y! Y0 `! H& S! x
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
8 W% R0 I+ @. _; ^) O4 r* K2.启用OWASP规则:/ L a! r$ C) @
& S' V6 n6 ]0 \! O4 h- m& D6 s复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。' @: [2 Y9 h8 Q% Z
6 k/ i1 Y F/ m+ ], n- Y编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
, E3 Z! T- r$ U5 p& o& @/ X3 h: k
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
) ]1 M" _" _1 u6 ^4 O4 k
# x9 T% |) ?+ T2 z, _, V6 EInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf' }( N3 r3 g# S
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf4 S2 ]4 d8 U3 [- `! C
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf* e0 C7 k+ J/ {/ J9 b" D. W) z6 l
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf7 z5 f9 \8 t* S6 l4 i# k* z1 y9 T% ^
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
$ d# Q" I5 g8 C g8 p( k3 @4 OInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
0 I' [/ w9 M, Z1 xInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
9 ?% ?" w) P$ h5 @, |/ u% t/ G( ~五.配置nginx
, Z( l* K0 P+ D& H* {4 c9 R8 R1 F4 U) U7 }
在需要启用modsecurity的主机的location下面加入下面两行即可:% w% R# t" h. y& W. ^( f
( X8 N) p& Y7 \3 J& Z
ModSecurityEnabled on;
! O! x( J9 ]+ I5 c8 Y6 J, j6 vModSecurityConfig modsecurity.conf;
* s( \- A- Z7 P0 }% q- Z下面是两个示例配置,php虚拟主机:* _( ?# J! `3 Q: ^0 H R
' }! M9 ?0 s: H- `
server {
! z6 `/ B$ M" Y" g: ]8 Y) A* m listen 80;3 a W2 R" C* S
server_name 52os.net www.52os.net;& S5 C; J6 J: f9 s+ C" y5 f# Y
* k5 j6 p) z7 B7 I0 b
location ~ \.php$ {' h" @# D( W3 A5 S% y
ModSecurityEnabled on; / U. ]' _0 s0 z3 c! D3 r5 q
ModSecurityConfig modsecurity.conf;
, k; o/ e+ I8 b* f4 a: M' s! r/ d! N! d1 F6 R8 m1 Z9 V/ a
root /web/wordpress;" ^, q% H: U# H
index index.php index.html index.htm;
" I% i2 N3 F+ F' s" Z+ ]# e3 Q+ c* M
/ t4 f+ b0 \* U) L2 Q' D5 C1 T fastcgi_pass 127.0.0.1:9000;/ Z8 e# b I% D7 F# ], w, d3 X
fastcgi_index index.php;
- ~8 I1 [; |2 H9 E& y* ` fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
( b! h+ `+ _" z. K% r( D include fastcgi_params;
$ t+ y) b% Z7 p3 U& k/ w }
l; c0 K9 l+ V" ` }9 }! s( o0 `& d1 [
upstream负载均衡:* }. B6 g" \1 S% ^
7 k# N5 A N* t; e5 u% Q- _8 l
upstream 52os.net {7 y3 ^+ f! l: D! c4 x4 G$ f
server 192.168.1.100:8080;
X8 @- }8 I( q Y8 v& N, @ server 192.168.1.101:8080 backup;
; x, k3 O5 v; I0 Z; C+ H. T2 X4 C}
% m) k7 ~& F- _8 K v# _, l: C/ o
+ j( J( |: ~) i# w% r% qserver {1 S% ^9 o; C9 ~, q/ w2 j
listen 80;5 `$ v' H# d M8 }
server_name 52os.net www.52os.net;
8 K/ c; u( `: r1 Q0 m0 `& b6 U: J1 j% D
location / {
, v, O( Y9 d& e- ^ ModSecurityEnabled on; 0 n( N0 f ` X$ k
ModSecurityConfig modsecurity.conf;
; `6 z/ G/ e1 B: q8 I& V9 m, ]; r# g; d
# x. ?1 u4 i1 f- C proxy_pass http://online;
% I8 Y# d- t& n* S+ B- z proxy_redirect off;. S2 `. P: K! p f7 H- M
proxy_set_header Host $host;$ m A( {0 N. D9 g9 y
proxy_set_header X-Real-IP $remote_addr;
& ^& |7 n A6 O/ g5 R3 S proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ T! o: L! u3 O) \, E3 Z }( s" A ?6 l# t+ ~' m: L. o/ ]
}
8 ?/ T. Y R9 w六.测试
/ G6 Q: T1 r6 I R7 J1 L# }6 A& N) P, c/ u
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
' H) D8 x ?. } k9 _3 f: [) c
- j$ P4 {+ j# f<?php
4 \( o: @1 k, t+ v$ ? phpinfo();
+ h' ` [* m0 S) V5 Y?>1 g* m2 O, `5 @ H( f! r$ \& {
在浏览器中访问:
! M7 Q( o4 n( E$ ~6 @4 c5 z+ g" a- \
http://www.52os.net/phpinfo.php?id=1 正常显示。- ^$ D# N7 e9 x, L) k
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
+ x* r6 t; ]- o: Ihttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。. w9 z1 z# b+ L* W8 v
说明sql注入和xss已经被过滤了8 ]4 u5 u; r) d4 M. w1 b* t* `% h
N! W- c% P2 u0 Z: h! ?
七、安装过程中排错2 ^, `1 l8 d3 V7 Z8 P
6 U2 T( j8 v; O$ ^1.缺少APXS会报错6 y' Y! r1 W4 C: x
/ i% g& q1 W6 @' K
configure: looking for Apache module support via DSO through APXS
; M+ ^6 W; K; I* o Y# W wconfigure: error: couldn't find APXS* G" p5 S* S/ u% x9 n+ t: u$ ~
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
2 a& l% Y7 ~- t1 w解决方法:
9 [9 E- ~/ O$ d6 A
* |8 c, R* b gyum install httpd-devel
( n% \- P/ o/ z- [2 J3 l/ x2.没有pcre
+ }' B* L( k' u) w( I2 g; X# G# c9 t1 L& K
configure: *** pcre library not found.5 j# P" Y. S3 l# k) B% W
configure: error: pcre library is required) F5 m. }' v8 ^7 c; b& ^; H1 M) m
解决方法:- A/ v0 H _7 S$ c
) |$ z$ F" S* b4 G0 a$ w
yum install pcre pcre-devel1 X) m8 {4 ~7 I8 x" ~
3.没有libxml2
( C9 e( j0 D6 o
8 H+ [" l0 ^* X k2 \: p8 k3 z* r" z4 [% t1 Y7 K- T( Y
configure: *** xml library not found.
* ?3 J/ n+ O8 z( Kconfigure: error: libxml2 is required
' c1 c2 ?- T, ^* ^$ z7 f: o解决方法:
; }: f$ `( W4 m; J2 ~, f% u% h! ?/ M- t4 Q9 r; J7 W
yum install libxml2 libxml2-devel
6 O. C7 z9 L, ^, f4.执行 /opt/tengine/sbin/nginx -m 时有警告/ e6 e4 B) @1 k) G8 ^! f' l
4 p, M/ h: f; W0 N3 o& p
Tengine version: Tengine/2.1.0 (nginx/1.6.2)' r/ ^% G6 A" L. ]1 T. Q- u
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
* }5 V- B: H2 v$ \% z& H原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log" x3 Q5 Q, E7 j7 I% P3 X" @
# t/ E9 ?' n, m! T: J1 f/ z. e
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.9 [; D* L8 q+ _' k
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"+ U# c" N( I+ B* V' a+ I
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
( ^' _# M( D* m1 m3 ]& ]( x2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
8 |; Y1 Y/ D9 ~2 x. i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"' S' t- v+ J6 h$ u( Q; M. k
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On., V; N4 {; U2 }" |
解决方法,移除低版本的APR (1.3.9)0 g+ ~( p& G Q, u0 t5 \. w
& g* N' @% ?0 jyum remove apr
% H$ Z! e0 C1 x1 v5.Error.log中有: Audit log: Failed to lock global mutex* B. u8 `0 t! E. h) O4 [" ^3 X( H
& i! l z& I# G# i, G u" S' F) J) K. b
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 8 ^( P2 I5 e- H1 L! C
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]& _* e( C' W: X* K- F( G+ L
解决方法:$ \+ I: a, s* I5 N
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:* r( {+ D) ^0 v# Q u$ {
& W8 |' c; `5 {4 dSecAuditLogDirMode 0777
: b0 e+ _* L& o" q+ c; r4 R7 VSecAuditLogFileMode 0550
5 n8 j$ l- d3 `) X0 L) [: aSecAuditLogStorageDir /var/log/modsecurity
# K$ g) B# Z6 v; F( RSecAuditLogType Concurrent
" \9 F3 K" \. a- k. z m1 r- v参考文章:1 ]' {+ s; q- \5 w+ X) m
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
. m" @6 r4 Z: ~( Shttp://drops.wooyun.org/tips/2614 |
|