|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
4 h6 C" {1 \9 m7 e1 {7 f- M. K! T: K1 K; i( G3 l. k+ a
一.准备工作
: y/ m# ?- [7 |* W+ ?3 o% w' B7 Y( @$ E/ b. L$ n; `
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
% G4 w9 E5 a9 Z) E) L( o
. R! E t% o& s9 n7 u7 { _tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz; _: ?3 A' W6 c) B
& ?) i) N4 {, _1 Q# d1 }, Q
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz. l3 n$ e- p8 i7 E8 d0 J
# V, ~( t2 X( i& E, v0 YOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs6 Z [6 d8 ?0 ]2 z: r* k
. @& B% f0 D* _ P
依赖关系:, S( \2 o5 \( L$ D0 {
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:/ z# s% f6 B S' y( M0 d6 x* P$ J
/ Z0 j4 N9 S- G8 {: j
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
1 H, Y0 ]( }9 G) z5 Y$ {modsecurty依赖的包:pcre httpd-devel libxml2 apr$ A# l7 A, x: T' ]. r# E0 e3 a
' O3 u, s! T7 P6 J7 j' Kyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
8 z+ C$ R; G5 H0 B3 Q二.启用standalone模块并编译; _+ q7 T+ Q, }; D9 f& `/ e
9 Y4 g1 V) K1 X, ?4 L下载modsecurity for nginx 解压,进入解压后目录执行:$ X" M# u+ g1 M$ X! }
0 A- {0 h% ~- ^* B i- D
./autogen.sh U" i0 ]5 }8 \5 v9 A. i% h
./configure --enable-standalone-module --disable-mlogc
/ W" i+ E; F9 U+ i! Y! G5 [. @# r9 ^/ Kmake ' g# H4 k& |( j& E1 S! A
三.nginx添加modsecurity模块
) e V5 ^8 X* d3 T% l
" V5 o% P4 o! R7 k9 L; R在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
- n2 o0 w, b6 g
! K! y% N# J! o& N! o) o% m./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
# }( W! C3 e" @3 B+ p8 @, cmake && make install
, U7 q8 [3 o, y ^7 ?7 P4 N. l四.添加规则
# `7 t8 X* f$ S' u$ K2 A7 u
) C; m& y5 |$ ?) x) K, V+ fmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
4 X! ~. l( e) }$ z. f6 Z2 x
% }7 f; V: F( W4 a8 t1.下载OWASP规则:* `5 T: a- b: r
7 l; n+ y0 H9 i( E+ l# j& U
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
" B. X4 k8 N+ ?; I ^8 J3 k$ i3 u% Z2 c W$ c/ |0 A8 S
mv owasp-modsecurity-crs /opt/tengine/conf/2 f6 P9 a- [# o9 ~/ k
! A1 k6 U: m: X/ Ocd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf( r, s4 ^ e7 V" e$ h* \, L
2.启用OWASP规则:
4 E, q) k# x/ y
6 Q6 f, u& O4 I: P' \0 u3 }4 Q6 g复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
1 ]- a: |% u8 @" T T5 T( v3 R+ I: _7 F0 t# |7 Z6 U( i3 M- x
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
+ H! a: c# g, M3 ]. ~- ?) }% x" _5 K8 A) I0 W
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
5 l- F1 n1 ^8 t4 ^: O6 {/ r; B- Y& ^; l
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
; \4 Z* a' e% T5 R5 a: kInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
5 s. t; w/ P, P' l! nInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf" @0 o& I* t/ R) |# P7 v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
/ }) Y# m# \& N% qInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf8 }( z6 N! V0 D, d2 m
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf+ B1 I& B, g( e. `
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
! v* E1 o" b" B+ i6 v5 E, K五.配置nginx
; ?& x% j$ I- c, o, Y% m
* z, u4 @! j) O: j& u在需要启用modsecurity的主机的location下面加入下面两行即可:4 n c4 {6 t0 X7 x* M9 Z
4 _9 y% D" }- @! Y5 H+ H% UModSecurityEnabled on;
( C' _- X: ?+ c% UModSecurityConfig modsecurity.conf;
$ S) Y5 z5 `8 n8 O' a下面是两个示例配置,php虚拟主机:5 z3 M* [" d+ ]! i$ d- p4 I
3 O8 S1 Z: T7 |) i* P3 t7 bserver {
0 y" D4 W) b6 K2 Q2 _# G4 c% G: [) O listen 80;! b: ^; |* |" d9 E1 V+ o( H
server_name 52os.net www.52os.net;8 h7 G: g' z; z. y6 g
3 F& f* A: c% v3 d. V' l location ~ \.php$ {
! X/ d$ u0 Z( k9 _$ j ModSecurityEnabled on; 1 H2 N/ N2 } t5 Y' @
ModSecurityConfig modsecurity.conf;
# m' G; C* K$ t. T
# ~9 p- S4 x( R& o9 H9 ^# B$ W8 l root /web/wordpress;. b* [0 Y- k3 E4 j
index index.php index.html index.htm;% u+ e3 X' N( }
+ W4 _7 f! D) t% S& w
fastcgi_pass 127.0.0.1:9000;
7 k9 L9 H' U* c, x fastcgi_index index.php;
9 j- G& O+ z( [ fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
) w2 T' m8 I! N2 m5 g7 y: L4 Z include fastcgi_params;
; G6 s& {0 I6 M! ?- Q }0 t. L' O% z8 }! {
}! k* M6 ~2 I( @
upstream负载均衡:
# `: L( q* K: Q9 V, J' \, W; m: J! W! W7 @( ~' t
upstream 52os.net {
; T, c1 A- O5 e# ?" q1 c. f# i& p) j server 192.168.1.100:8080;7 m' K3 T+ O0 m$ |& M* G" j* N
server 192.168.1.101:8080 backup;
+ k% d* d. x' ?$ \+ Q}
# C- @6 F+ a+ J; C2 @
+ L% s3 d4 q j( y/ K. f2 U+ J5 Wserver {, U: F7 M1 g: [9 n( e
listen 80;7 j" j1 U) d# l7 ~7 K
server_name 52os.net www.52os.net;
! C1 S6 L+ M, D% t5 `& r: v% c; R2 H3 k& U) K
location / {
* H: \) f3 g# a ModSecurityEnabled on;
7 z% a+ g/ }( p, }# V ModSecurityConfig modsecurity.conf; ' Q7 L3 l1 p. j0 _8 J# n) |
* e1 N+ K4 W! X0 k proxy_pass http://online;8 Y; i5 Z- A( T( [/ A. q* j% ^
proxy_redirect off;2 v8 ]6 Z( Y; u: U6 W Z
proxy_set_header Host $host;; e( g* p9 B) Z2 r3 v! B: N4 @
proxy_set_header X-Real-IP $remote_addr;
3 o' J0 F+ X; S Z proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;5 L) T2 i$ D ~
}
/ i8 h: O9 ?% G}: P& n2 l; y) s6 C9 q! i; D5 H9 Q
六.测试- G, w( j* j' l9 J2 j
! t' i- l7 g# K9 h J我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:" q/ M$ U* L! q( `6 [7 U. r1 ?
Z. R8 D3 l3 S% N
<?php
. F! b; s/ g0 l% W phpinfo(); % q) d0 ]/ y6 p$ b8 F1 t6 ?8 K
?>5 m% F3 y. ~# L+ Q/ @4 o( y' Y
在浏览器中访问:
W3 Z2 Q2 J; D
0 E' [" h5 l- Uhttp://www.52os.net/phpinfo.php?id=1 正常显示。) f2 v, ?0 D2 i7 n7 P2 Q
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
J/ p# B* }1 j$ z3 t R* f/ ]http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。" y+ G. ? V1 Q3 Z5 d6 |& D
说明sql注入和xss已经被过滤了7 G( j8 g0 R3 u- J6 f
3 z( ]. x$ Y: S
七、安装过程中排错
3 O2 ?1 ]7 G2 x" N ~( O; @ G4 G: G2 d) w+ ]$ \
1.缺少APXS会报错
) p2 I# g& Z' V5 [! J
. S7 I5 i3 t# e/ _8 rconfigure: looking for Apache module support via DSO through APXS1 h, i2 e% j- @5 b1 ~+ M
configure: error: couldn't find APXS9 W0 Z3 g' @4 f
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
7 J) Z. r/ C) i" ~+ }- p解决方法:
% f7 x: _0 h1 g W$ s* v/ t4 T& X0 s2 \3 v) B( z2 @5 X* d1 g
yum install httpd-devel! c) x' L7 ~% z% a8 ]' M2 \0 Z" c' v
2.没有pcre( G% \) \3 I* b. ~( w4 |
9 Q R4 M+ {5 _0 S( G4 x" y- d9 W
configure: *** pcre library not found.2 r- U5 e( E: ~3 y1 M) S8 P
configure: error: pcre library is required
2 c+ k; V7 M5 M+ z$ q解决方法:
7 @7 x1 ^& G! i. A+ Z" G4 [3 ]7 w) e# r0 {
yum install pcre pcre-devel) j6 y% H, Q. E4 Q, o
3.没有libxml2
7 D$ u& Y% E7 d( x! v8 E
! Y- E6 y& B! H0 o Y2 [- A+ R2 X6 V& C0 U2 C/ D
configure: *** xml library not found.
, t1 y: L% ^ e7 s/ f6 Sconfigure: error: libxml2 is required! o' U( `5 K( ]2 \% o* d
解决方法:
( Z% c3 u+ g# x6 d+ G) {! w( H8 K/ e" ^6 ^( b
yum install libxml2 libxml2-devel* f# g" ?- [. w
4.执行 /opt/tengine/sbin/nginx -m 时有警告
& Q( m: I! v9 @* {6 x+ c- d, |* ?+ R( S* Z& a
Tengine version: Tengine/2.1.0 (nginx/1.6.2), Y$ R. Y# B& F: ]; P$ u
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!# d" t2 S! ?- O& B6 `
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
$ f# w i- ~$ p* |* Z4 H! L' V4 k( c u$ A1 I. b; u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
0 j' ~4 P7 A# n/ Z# u4 w( v7 T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
: e# P* R8 c4 H6 Y4 c/ u2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
1 c2 D7 V# ~; T- z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
& j8 a! \9 v8 t1 y( @8 p. c0 N2 K2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"- n' M+ T* ^; F' P
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.7 ^' M8 l2 j, p4 L1 v
解决方法,移除低版本的APR (1.3.9), [7 U# s, t, c) r3 I1 I9 ]5 X
8 u, k7 P% f" j7 ~9 jyum remove apr
* }* q, C: h7 C/ ` @' y) [/ b5.Error.log中有: Audit log: Failed to lock global mutex/ c" L4 }6 g6 M0 v; _$ V
/ N) t5 i) d1 \5 \
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
4 | ~8 {; ^+ @ V" X) r5 Y7 oglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
8 k- I M1 I" f5 f/ V4 C7 H, m解决方法:
6 F7 F6 F2 F$ j+ g. ~9 j编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
" n' t7 O% T1 `% y
* H8 p" Q1 o' WSecAuditLogDirMode 0777- e- W* Q! U8 F" o6 D
SecAuditLogFileMode 0550
8 f0 a) X" ]) V3 q* Y2 BSecAuditLogStorageDir /var/log/modsecurity' `* \8 T7 z, n. h$ j5 O
SecAuditLogType Concurrent2 M1 e& J* P) B' h$ S
参考文章:$ S. }& Q5 q9 i' _! Q
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
$ _# z/ I7 u, `http://drops.wooyun.org/tips/2614 |
|