|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。' l# E f: s' M8 v. J
# A j% z: E8 j' D$ g" H
一.准备工作
! C9 o9 y1 N% f1 ^* ~
9 _: {8 @+ L, x4 I2 R9 p系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
/ z# z) Q* P5 }3 S& {% k0 t6 v- g$ Q7 Y* [4 Q. Y. {. T
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz/ H, K. e( [( |7 U9 J
' G# G3 g1 p/ M. S8 B4 \4 ~2 ~3 I* Fmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
6 x' d9 U9 ?$ e
w. d; c4 _2 Q8 d2 dOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs7 R, W H; ~: x* K1 X
: |, T7 n. i6 v2 H$ V- K3 A依赖关系:5 L3 h1 J2 R/ n1 H5 x: R9 a& w
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
7 _- S$ w( O9 ]. h4 u
7 S. D$ O7 f- I5 j" E3 e$ Lyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel- F. }( y; i: }
modsecurty依赖的包:pcre httpd-devel libxml2 apr P6 |; |# A4 e8 T% G( N; \$ i. t
/ `% l9 A8 i4 Byum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
B2 O4 _3 c' d二.启用standalone模块并编译
) L' i9 s2 z3 A: F; ]+ r4 [5 N: [+ v8 ] n% U& Q
下载modsecurity for nginx 解压,进入解压后目录执行:
6 v( N7 ~% {3 J3 e' P5 q) i
7 N+ K2 G K9 L& D$ A. L2 S D./autogen.sh
2 T7 _# _5 J# ^% o./configure --enable-standalone-module --disable-mlogc1 _4 @" ~. U$ n$ T9 a! H4 g
make & a: O# |! A W+ g% l* {
三.nginx添加modsecurity模块
7 l: P7 L3 \ T) t! h9 C1 p) `! N9 w( D9 E- Z
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:% R/ x/ \2 y8 A% @+ u
& I! C0 h( v+ T1 q./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
' O& N% U& r# c! nmake && make install/ |1 d" G4 b6 j. D+ h+ c
四.添加规则
8 V0 ]9 W' [+ U2 u) ^' Y9 D |: u
: m$ f' O7 \* l v' Dmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。* L% n$ i, J6 D- ~3 {/ r
% |& [; n/ _. K' {! p5 W! [! w9 d
1.下载OWASP规则:& E. n: D: ? _# H
' E3 o, o e7 ngit clone https://github.com/SpiderLabs/owasp-modsecurity-crs7 c) `4 L- Q: x5 e0 G0 R2 G
/ D+ z) \6 Z; f/ bmv owasp-modsecurity-crs /opt/tengine/conf/, s) D; Y/ _! m
4 s+ \/ z/ c" Vcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
; N+ `! S' d4 h0 f2 i! g2.启用OWASP规则:& l* d2 S. u& d/ M
" O( a% H, ~# e- P* P
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
4 U$ {0 j3 B# d; j; A: ?# |- b _+ r W, l0 A! b8 M+ C% L: ^8 o
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
) J( W5 q5 G4 c3 q" h# A4 r) X# s; ~' x0 _
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
4 n' S( M6 } g
+ D- d2 g, e d2 B, Y+ e, sInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf" U6 {3 z7 ^9 n1 G
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
; `& P9 I- V! N3 n, s: }: `Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf* m/ Z5 I8 ]- h5 B# y5 L" _- n
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf- D# \; C/ R2 x
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf3 l+ R0 ?$ j! \; V0 Z& B1 a. E, v' N
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf" a0 K# a' R3 F2 b/ G' D3 t
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
5 @) j, c( Y1 E7 U! Z五.配置nginx+ {5 m# V: x i! A; f
# f4 N8 ?& F4 i4 O! R0 A在需要启用modsecurity的主机的location下面加入下面两行即可:% l# N# V* k+ u& _4 j4 Y
+ w; \$ E& W5 F
ModSecurityEnabled on;
' s: v0 x: c; A7 v# AModSecurityConfig modsecurity.conf;6 ^% ?1 w9 i% \. q1 j) W
下面是两个示例配置,php虚拟主机:, Q) V% J3 `. @- W2 s) M' O
Y! g7 `% v; d# p1 X) D5 Wserver {/ z( F% |/ [9 S& N/ @8 {# F
listen 80;
+ S& r2 t1 E# }1 D( f3 h h2 ^ server_name 52os.net www.52os.net;: ?2 O; m4 w4 T5 g# U
/ o6 f" E! \, i; K R
location ~ \.php$ {4 j1 E& z+ }& d2 R
ModSecurityEnabled on;
: E9 l1 e, h2 A* B ModSecurityConfig modsecurity.conf;: }' h. d; H U& U
5 S+ S3 _, [% f% Q a root /web/wordpress;$ ^6 Q% v( {3 F$ y
index index.php index.html index.htm;
9 I5 f- W9 h9 X- h# \+ j
% j% W* o" ?. A& V fastcgi_pass 127.0.0.1:9000;
7 @: b% Z8 |4 ?; M) O5 q$ E8 p8 w fastcgi_index index.php;; m8 e: L6 u8 {0 O
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
( M! i4 q8 ~ a$ V include fastcgi_params;
2 p/ @' i5 a/ s }1 d2 Y, S5 u- N! Q
}9 t" B' i7 Q$ E8 D4 S! [
upstream负载均衡:0 y* @. z3 Z/ }' [0 Y5 ?6 h
+ K* x2 p8 P9 n3 R) K. v
upstream 52os.net {
4 k4 S" d, j7 W: S6 J" W: s server 192.168.1.100:8080; s. _9 Y2 ~' r. l% T
server 192.168.1.101:8080 backup;- u9 t' M: E9 B
}
/ s3 t" |3 I: \* v- V9 \& _
! f- [0 l. X9 ~' y2 A: _server {, u, h% G, @5 k5 `6 j
listen 80;. N: F; C7 |; f. Y$ B7 m9 R& u; K
server_name 52os.net www.52os.net;) q8 H! K& E& G2 n$ F+ S7 }6 L
0 T, r8 c5 n7 i: z9 r$ qlocation / {
4 D" ?2 G+ w" U ModSecurityEnabled on;
) t" u( E/ S# p6 D# a- E+ }. v ModSecurityConfig modsecurity.conf; * D) M0 W3 b2 e8 y3 `7 h
! t! \* ?+ G. D- z- \ proxy_pass http://online;' N8 k. d3 x+ q1 N2 d3 O
proxy_redirect off;; a% |& G$ c K* G) }; z; }
proxy_set_header Host $host;
3 \/ ]* a, Y: W: B$ p proxy_set_header X-Real-IP $remote_addr;
" f, U: O2 x% d proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
0 {+ E! \- o8 [. W }2 R. Z u- y% a% t: f" K3 c
}
( e6 n1 m2 i: T* b5 M9 { W六.测试
4 j" Y! Z+ Y6 N& c( o2 u& S
" I5 I0 A4 S4 }我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
8 s7 Z' d3 |3 }. G5 f
3 }& j X( i/ f2 y/ P<?php& D+ R5 [5 _+ k! m0 Y3 N- M' ~
phpinfo(); 1 k: q$ T+ k A4 {* {# R! o
?>
) U" L$ r' |3 ^/ U( i在浏览器中访问:" g5 n+ D) S2 c9 R
3 f, s" f# y$ D2 Ahttp://www.52os.net/phpinfo.php?id=1 正常显示。6 F* J0 G" A E! R/ G, b
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。& `; c5 m. [7 j2 b( v( S
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。: d# a3 f( u }! E% f) p! `
说明sql注入和xss已经被过滤了
( |4 s7 J; v# R, h0 g6 d2 Q+ a
8 N U) q: f1 A! W h七、安装过程中排错' s' z2 n& L: g4 B8 }* @
r- L( Q# r( F1.缺少APXS会报错+ V$ o2 w5 }( ~5 L
4 P; V" ~( z2 T! V* f! J
configure: looking for Apache module support via DSO through APXS
/ s/ y) b5 J5 ^8 h% H! s( [* S7 bconfigure: error: couldn't find APXS; [8 Q3 H. N% A$ J! g
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
7 |. I) [% d3 D* U8 t1 r! m解决方法:% i; \& P0 q* d2 W7 ~
@+ A- ` n1 B0 n' U6 |
yum install httpd-devel- r3 l1 t& T8 P9 ]3 l) F1 p* R- ]
2.没有pcre) @* e0 I8 s4 E% a: e# i
5 T8 B( t5 x r" S1 T' xconfigure: *** pcre library not found.
, w* e4 }. F. }6 `5 iconfigure: error: pcre library is required
9 [. ^/ b9 k% Z0 Z+ g0 F( G2 n5 k解决方法:* Y3 T D/ M0 A- @
, v' h* n8 G6 Z. k* d% S7 A* jyum install pcre pcre-devel' Y8 J* _( V" h+ s- X
3.没有libxml2( O. Q3 z; L8 X2 x$ {
; X. T! D9 C* H& c& [/ M
' I( }# {& A1 Z+ |! n. L' econfigure: *** xml library not found.; u' Z4 w5 G: s) p6 j& i
configure: error: libxml2 is required
0 \9 R8 @1 d) L o) f解决方法:
- d* S/ U# N3 v/ @2 m
" G9 \ S6 z# q' C3 \yum install libxml2 libxml2-devel" K4 o( J7 l; O/ E
4.执行 /opt/tengine/sbin/nginx -m 时有警告- j0 m7 q1 y5 G9 S
+ p. `- a; U* ~2 \! H j
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
9 G+ S! {6 W$ p+ S0 C% u; ~nginx: [warn] ModSecurity: Loaded APR do not match with compiled!0 z5 o( i! R9 ?+ q( d b
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log% N1 _+ j- p! e, V' f* c, Y0 R4 a
7 a8 n' N0 x1 ]2 A3 D/ h0 W2 O; u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.& O; }( I+ ^% r8 S6 Z2 [: ]
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9") F& G1 K; a- P T M# l
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!% s# b, o* P' m. R& d* P! d
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
+ Y9 l+ H. J" L" @' O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6", y" C9 |5 t" s4 G0 l' Y$ d
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
! q* B S9 \ }6 b4 M: G解决方法,移除低版本的APR (1.3.9)
7 n6 K/ `" }/ ` @+ m+ C+ I/ t1 P# j
yum remove apr
9 H6 _+ D" \8 u5.Error.log中有: Audit log: Failed to lock global mutex$ V$ F& A# x+ Q. `
# ?; d O3 H% r+ t2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 4 U- ]* G' [' ], d
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
/ ^: d8 |4 f4 r v/ ^0 L$ a解决方法:
* R7 F/ ~3 X' u! s5 u编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
/ T1 W8 d6 z6 ]0 @ }( e6 @$ H4 |
# f* d5 A1 ]5 U% I7 qSecAuditLogDirMode 0777# Y0 X& Q4 r( K" [0 b$ J
SecAuditLogFileMode 0550
% M4 ^" m! b1 DSecAuditLogStorageDir /var/log/modsecurity3 e6 ~$ z) `& Q9 N f `
SecAuditLogType Concurrent
: E) D/ z' c& \3 P$ r+ S9 d参考文章:1 c6 ]' B2 N- s& Q
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
' n4 w3 [7 K5 {7 p5 p, B. ]http://drops.wooyun.org/tips/2614 |
|