|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。4 m7 J. f$ N/ `+ J
; r" T/ d+ G/ r; L! Y, k! H5 R/ c% `一.准备工作0 Z; A' R" _6 R9 R5 d
8 v% t& O% A8 P
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
# y$ }: l+ T& `& n- g6 p A) w6 Y% ^- }2 D) M6 ~0 O. k
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz S. ]6 v5 M1 ?4 H- c5 R9 c
) B7 b: L+ v5 X q# Q
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
/ W% U9 U1 }2 a; i Y
) X/ V) J& U2 [& z! |5 VOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
) z2 z* e) h6 D5 x, q) Z6 B6 `4 ~. Y. C8 E8 y$ h4 n: t4 H
依赖关系:
" h$ L0 T% s* _2 a5 e9 vtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
1 S6 L: V) \. ?6 [& w8 z* @! D4 ^) X& ^* @
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
- S# n, y# E& d! y/ \modsecurty依赖的包:pcre httpd-devel libxml2 apr
0 o2 D9 \, g9 G# Q; N0 w# z( w- e* n- c& R" m
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel. v: H3 W q. A3 c. \; ~& }. B- H( z8 g
二.启用standalone模块并编译
# H- Q h$ r2 L7 Q* f5 }' a0 T6 ?- h l- k$ p
下载modsecurity for nginx 解压,进入解压后目录执行:
9 G7 H. o% ^$ F1 d4 P, ]$ X8 M6 k( T1 k0 Z) ]2 C
./autogen.sh
. \3 @4 n$ Y0 ?6 k6 I6 _./configure --enable-standalone-module --disable-mlogc
- w) W3 ~ U2 ?make ; [+ E. E5 o( i* b O; V9 o1 `
三.nginx添加modsecurity模块! b9 o- n2 t3 l7 G
- j( s9 ~2 F3 N# X
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
* `& b8 p3 x% Q% h
6 {+ r( b: w, |9 w* y8 U9 i/ s./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
& e% a( z7 m7 N" i* Fmake && make install
: g+ Q8 P: n! D) p# p四.添加规则
+ d0 U8 H j8 @) E/ u9 O5 h; f% L( y6 c$ U9 T8 v- u" B
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。' ~4 @7 B- B' y6 ~, I
& L, K- c7 V3 O8 h) w; R1 \$ d
1.下载OWASP规则:* {; k0 k# |7 s% \* S
+ w& V4 O1 A# x' y6 M, r+ D
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
: p' g& `+ l" ?0 {+ h
# |) x( X& n* Q$ w. Z& Dmv owasp-modsecurity-crs /opt/tengine/conf/
, J6 ]7 n6 \. T% ~' q2 C- M' a) H" I+ f. c2 L& c5 T
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
! n6 k4 C" j0 e& c+ \% [2.启用OWASP规则:' z: Q5 C5 K4 j+ p
2 Z" W0 L) ~) n复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。, r3 v/ W7 O+ ?* X- O
; X' q+ L# y* p4 H0 @0 ?编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
& \1 a7 O2 s6 G* v- a y% k( F3 R: j4 }* X
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。2 r2 Z1 c, m4 v- F4 ~: [: H
9 w2 E$ y+ \# t6 t. I0 V1 `
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
/ L3 _7 @8 U- ^7 H; eInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
9 t+ o- c( k# H9 [Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
0 E6 J, P6 D& Y; A+ uInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf2 l( t0 I: i, \, f( w) E) @
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
3 L0 c5 F# O% D2 K# IInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
7 c3 Q; g, o8 w- p/ f* f8 UInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
4 C a+ X8 Q5 ]$ T+ |* @五.配置nginx
M6 X; r7 p) Y' X3 F- [& j6 w0 `' J- C8 w
在需要启用modsecurity的主机的location下面加入下面两行即可:
1 N, ^0 B3 g$ x8 C: b5 {) [& z6 x2 [
ModSecurityEnabled on; ' b5 S3 x! B0 e* G
ModSecurityConfig modsecurity.conf;/ [& T B; L1 N: ~/ k+ x
下面是两个示例配置,php虚拟主机:, c ]' a, u$ |1 I; o4 t
& _5 V, R4 X1 {
server {0 f3 O! b8 u% C+ q6 ~1 `: i, \6 F
listen 80;7 E- \) R* T- l {
server_name 52os.net www.52os.net;/ c8 h/ B0 S+ }+ G
% k d& C: G1 P+ ]( x* \3 B7 R
location ~ \.php$ { w5 Z4 |" z; M! S) O" b
ModSecurityEnabled on; : U$ f7 G, y+ `9 m0 n- H; E, I0 W
ModSecurityConfig modsecurity.conf;: \. }4 O- u; I) n5 O3 e/ I, N( E
$ ~' w0 n* o2 I V
root /web/wordpress;
2 S5 T8 P( |/ g# M7 c3 } index index.php index.html index.htm;! [2 U: \. A' f$ X# w. S
: P2 k# K1 `: P$ K0 [
fastcgi_pass 127.0.0.1:9000;! y8 O' P4 G4 G
fastcgi_index index.php;
! T0 L$ {- M8 K5 N+ S; i fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;- X8 M' x n9 K! V
include fastcgi_params;
* c& v# g+ b5 f }8 k9 U L- Z1 y3 l! U& s- m
}9 F" y8 E" A) \3 G: i e
upstream负载均衡:& q5 V; `+ z( M- c
& p5 x$ ~1 E O7 W" y$ E+ @- Yupstream 52os.net { P* m- `2 u) i* [7 t4 c7 B( t u2 J
server 192.168.1.100:8080;
1 z C7 j+ d/ H% d$ v d server 192.168.1.101:8080 backup;
( k" T7 }5 Y/ v/ R% k- p}" n- i, b/ H4 \8 ?% B% o
& i* H: n; b3 x0 G Wserver {( ]; a% K$ H u7 l# h
listen 80;4 u/ x1 i: D: b: ?7 Z% D
server_name 52os.net www.52os.net;
" u% D) |* V! B# y) t: X) k- X' ?. Y' M
location / {
. t- A4 m3 i4 }3 }4 n# }0 ? ModSecurityEnabled on;
* u" B% Y( {9 E8 K9 ~* }- H ModSecurityConfig modsecurity.conf;
# [1 O% E5 R' S" W
) S, l) ?) i9 e x) L' b proxy_pass http://online;" T9 k7 n' x4 j+ T% z
proxy_redirect off;6 d1 r$ a2 x A7 B# d
proxy_set_header Host $host;
9 j- W) c# c& k$ M3 c proxy_set_header X-Real-IP $remote_addr;
2 l8 `( Q4 z n: w3 s& Y proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;9 i) [3 L: v) N2 ?0 f$ t
}/ }, {, M" h+ B
}1 k5 X* o( C0 l8 [3 a- l- P
六.测试1 Z8 |2 t: }3 P: S0 X" S6 @
2 ^3 t- k& H& R, M2 v: }; a3 {我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:" U4 i2 s5 Y7 i r, B/ p2 J6 c
W" l2 m3 o. c9 U p1 W
<?php. ^- l; v" m8 q" }2 d+ V+ a
phpinfo();
0 _$ ~8 ~' T4 r?>
$ z' @9 O6 ?/ ^! |在浏览器中访问:
* g1 N: r I% x( c) J0 g$ t3 h% L0 I u2 ?
http://www.52os.net/phpinfo.php?id=1 正常显示。, r* h9 {8 D+ _% N% i: N
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
! H* J( [3 m0 b, @6 J& W _. Nhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
' u( w" B. ]3 A5 p说明sql注入和xss已经被过滤了
2 q/ ]) b) P% b) o9 \" N3 ^( [: ]- T% j9 c* H/ s" x$ I+ p% q$ ~
七、安装过程中排错
- N( R$ a8 |6 b7 G8 ~
, @" O: z, K( u! u: K+ B1.缺少APXS会报错) T6 c, q' m- Q
! L9 i- s7 _$ P0 H4 y# f
configure: looking for Apache module support via DSO through APXS. @2 U. L6 k! x# T( n: h4 r
configure: error: couldn't find APXS
1 d$ t4 i# l0 z" z6 V& X: |2 yapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。! V0 R) N0 D/ [7 ?5 y9 }) ?* u
解决方法:4 ?, l" [6 h2 S6 z- r. R$ ]$ a
1 t1 [2 a# I+ W9 U/ ]8 R
yum install httpd-devel
: I5 S9 c j! F3 p8 b* h8 G" I2.没有pcre) t/ q. y5 h* K& i" s
. B2 v: P& ]( U$ ]0 M( y8 o
configure: *** pcre library not found.
7 \/ J) J5 Z3 |# uconfigure: error: pcre library is required$ H, |8 k9 e2 J- ?0 k' K
解决方法:
4 `0 H& j% | c% v. o8 u0 y0 t: G: ^! a! \2 F) d% u
yum install pcre pcre-devel
& H) |/ q' W$ ~% E3.没有libxml25 ?! ?/ x+ R2 ?$ x
% X: G# p) Z% |+ |
2 O$ B) K& o& r) v+ C5 A. ?configure: *** xml library not found.6 m" W6 ?* ?8 k+ q6 _
configure: error: libxml2 is required
9 ~2 a) c* p l解决方法:1 T% z' W {0 K6 R0 x
( p) I7 w4 N. O1 I) ~, ?- p8 Q6 J
yum install libxml2 libxml2-devel
* ^' y+ j8 d2 u2 R' H' H4.执行 /opt/tengine/sbin/nginx -m 时有警告
/ U9 N5 _' }/ K% s" `4 X# j/ v( R! U- c
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
$ }6 ~ t6 j2 M" gnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
! @; b! k5 N) i# M$ @& r1 }* l原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log+ K' |6 q% P4 r* K9 m4 F
# H3 O; `- q, n. g4 N2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.' q5 w9 U) Z; k. v( T) c% |" \
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"1 P/ C! N E/ M5 m: l4 T
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!' t. j8 s& V6 E+ `
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
& }" c2 w# _: W" t @2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
8 T7 X% B f H( S4 Z2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On./ q% K( ]9 M3 I
解决方法,移除低版本的APR (1.3.9)- Y( p# q6 l2 U' p$ I3 m- l
/ C" s, W+ e* T8 R
yum remove apr
- V; d, g( X6 e8 O! J5 j) o5.Error.log中有: Audit log: Failed to lock global mutex
" R* k0 [8 @7 r1 o9 Y% i, A. ~/ D
; o8 @: C& J. i: {2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
2 Z0 N! ]# }6 X: {global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
9 q8 c; m+ d+ P" X6 t. N) K3 ?解决方法:& Q1 X1 F$ i3 ?4 o- h( E" D; Z3 G
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:# U4 B7 E% b, }
. c$ N+ \; H3 C% \SecAuditLogDirMode 0777/ i6 h* F# e; W4 Q
SecAuditLogFileMode 05507 r) R# T$ n! Z4 z% n5 e' H: t
SecAuditLogStorageDir /var/log/modsecurity) X4 q5 i! A& R9 I w7 X
SecAuditLogType Concurrent: L, |* d+ v/ [1 A% i7 ]" b1 A1 N
参考文章:
' O# n& }. ?& ?: L! v* ~5 X2 Ghttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
G- p! [1 _3 y5 [9 O) }/ Rhttp://drops.wooyun.org/tips/2614 |
|