|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。' Y$ m9 W# u9 f7 E1 a( p
2 t' L4 j {; j- K! P% H# d( z6 c0 C9 w一.准备工作
5 k) X5 _+ Q6 [$ M; X; z- C" s% ]. C
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
" m4 ?' Y7 q8 \3 f. v+ {) Y* z3 c+ O6 q- V0 ]3 x( d
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
0 L$ V; @+ r& C0 m# B* x( q$ z! @1 f4 L' o, c# \
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz% H2 p. Y6 O: n4 @5 _
2 J5 u- Q5 X0 r4 D; I% l7 {, o
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs# y9 Z* N/ y* f" |' A
' l6 r* e( p% O$ j' Z5 r8 W依赖关系:/ o$ d7 A6 y; }
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:% {9 M/ }; [" ]2 A" ]
`5 V8 B4 Z+ ~7 X& Uyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel7 S# L4 L$ V- Z; R8 v6 |+ u. N
modsecurty依赖的包:pcre httpd-devel libxml2 apr
- [7 n% O4 G+ T# P# W; @8 W& K% D% t
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
v4 z, P1 u8 M, p8 {9 W. ]二.启用standalone模块并编译
/ ^6 g2 }; t0 \
4 j5 Z/ u/ E$ n$ b$ Q' R2 [下载modsecurity for nginx 解压,进入解压后目录执行:
. x ]1 y! v: N, ]0 s# t ]" w$ T0 G/ B8 {
./autogen.sh
5 E& X) r$ I9 x5 F4 \./configure --enable-standalone-module --disable-mlogc% W# D$ ?0 f# t3 t) x
make 7 `" o/ ?7 w$ |3 D$ b
三.nginx添加modsecurity模块
# B# S" I: ^" Z- z& {- G: _
: e1 o9 H/ Y' ]6 |8 L1 `, E% @' D在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:" T0 m8 B8 S+ p: j! i, u w
5 _4 M2 ?8 h- {
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
- h4 z" ^( T! d" ~& L, o3 cmake && make install7 f7 f% h/ w6 i+ W0 F
四.添加规则: g7 r; m% _% z/ J0 Y+ `9 J3 W
8 c o* Z" x. M5 Q% omodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
+ y5 F: i5 I( F; }" i" | v/ Z+ m) U6 z( g; H: i W1 H. F. s/ {
1.下载OWASP规则:* G# u& P1 c2 ~; U: a& n
: h4 l* v* V2 }" Agit clone https://github.com/SpiderLabs/owasp-modsecurity-crs% b7 b7 F0 y8 K+ @: n$ m: m8 w
/ Q. b. z( \4 i, I6 R; f
mv owasp-modsecurity-crs /opt/tengine/conf/
' J' Y, p$ O0 E' F0 ?' {/ T4 X% c% |
. F7 J/ O6 M/ t3 \! zcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
/ K$ G: I* a4 B: R3 \2.启用OWASP规则:" N! i# k6 K R" p
: N9 ~3 s( G4 E* b7 V
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
6 y( ^0 {( X, p5 d n$ |$ B) F& h- s* L8 G% }* f5 x
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
! b7 Z- W% z* V. F7 e1 T% n* \8 h. c; S' {4 _: s- l6 w
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。, `! p" N# G, m+ F5 G- H4 f
- B& s2 I$ X3 \
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf/ ^! z8 c3 c5 a+ E! d
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
7 ~+ |$ w! a1 p# @/ E: [Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf- }6 P2 v3 K- d. E
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf- X+ k& e+ x+ ^2 K+ ~) S& n
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf/ f7 h# k$ \! \+ |
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf+ M. ~! U1 J0 p% h t- R" a% ^
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
- q9 h; T, F, L( C五.配置nginx
1 z7 A! I T( k8 q8 G6 C
2 a C/ ^0 I }& a$ ]在需要启用modsecurity的主机的location下面加入下面两行即可:
3 \' H' j2 a8 m8 v7 m+ ^7 T$ k3 i2 @( p2 a- F
ModSecurityEnabled on; 7 Q3 ?6 H8 C# _6 G# {
ModSecurityConfig modsecurity.conf;
, z( c; Y2 x( T下面是两个示例配置,php虚拟主机:5 G) w" {1 k5 F/ v/ c2 B
) j, [* [, S, G" T" {
server {% q- A% @% ^! z, ^; i
listen 80;
% q" [; @# r; Q+ w8 I# ? server_name 52os.net www.52os.net;
2 G1 Z, D2 V9 ?4 C) D: o" v
y& _% [3 R H6 C# F location ~ \.php$ {
& b- G. i" Z0 \$ }, x8 \6 S ModSecurityEnabled on;
% \5 P9 Q Z& ?" U. d9 G& I) l* I ModSecurityConfig modsecurity.conf;
7 Q/ T8 W$ P2 y c w& o3 k B, D( R5 b2 _5 w' p
root /web/wordpress;
# W. D) K8 M2 o index index.php index.html index.htm;
8 L7 r- Q6 v% p9 t : G/ A. V* Z% U5 m' z
fastcgi_pass 127.0.0.1:9000;0 N; j' y. m" k( B& l
fastcgi_index index.php;9 c% {# z5 N# ~2 z9 ` {% Q
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;% Q+ h! r0 t9 t; y, S' L
include fastcgi_params;
2 N3 l# b( q( b% E7 k8 y* w& U }# |# t' j9 ?; R
}
0 n! y5 |& u6 w8 a( ?upstream负载均衡:
( ]) u3 Y; `. B) M+ Z! d! ^( E( V4 {
upstream 52os.net {, |; Q! v+ {( H+ g6 W
server 192.168.1.100:8080;* F. `( Z; ^/ W7 y7 U' c+ e0 p3 A: I
server 192.168.1.101:8080 backup;/ c) D' {1 o& o& r
}* y3 n/ r6 o1 m$ d/ H8 C J
, ?+ x3 z1 _$ E
server {
3 a# D7 r; u$ r& U6 zlisten 80;
4 |; H* X* p1 f2 W9 f5 Bserver_name 52os.net www.52os.net;2 _' H' Q& |; A2 G" }% P3 M- J9 w
. s- g: ]+ A8 M1 \- Q- P9 ?2 k5 v
location / {
* ]" _: M/ \3 y& M6 d2 ~5 J& ~- _ ModSecurityEnabled on;
+ c: q; {4 W$ ~; O ModSecurityConfig modsecurity.conf;
: H0 T* t! r3 @% _- g* v) s' s6 K, r- r+ e
proxy_pass http://online;
! A/ n5 Q J4 C' @: v3 Q! U2 C Q proxy_redirect off;9 l+ X2 l# w0 S; W+ S. K
proxy_set_header Host $host;+ j, \, u2 [. G
proxy_set_header X-Real-IP $remote_addr;1 Y% `5 N% l [2 `! d& D5 M3 o
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;; g' b8 V2 C9 _
}
, }* [5 @0 k- U1 x V. k) H2 |}( y5 d0 @& |" w, S4 ~0 s
六.测试, C& m+ n, x' O
* ~: @ d" _+ F' {! a
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
) F+ F; @$ w7 Y+ k( s* x8 K3 T0 y' w' |; }6 K
<?php
2 T+ ]6 D/ }: |- h C phpinfo();
& {. \3 v+ @3 P o1 {) N( \, Y8 j?>: u- H! Z6 G/ T3 \. C
在浏览器中访问:
6 s! o: s! S& f/ ^5 A9 P0 T4 Q9 z! W9 I) Q) `
http://www.52os.net/phpinfo.php?id=1 正常显示。
/ A7 l- a; y9 P0 A2 t7 Mhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
- N% q% F8 `' U; qhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。! T( X) M: r5 R: I
说明sql注入和xss已经被过滤了, l6 }" w$ F, h( @. T3 X# T
" {6 l' W/ \( e9 n" k七、安装过程中排错) l' B& T& T" d
+ \8 s9 B- k# |# k: i, a
1.缺少APXS会报错- Y+ G! G# n; Q, h3 v
h1 ] x0 V* @0 q/ ~! Uconfigure: looking for Apache module support via DSO through APXS
$ _9 p/ S) s9 W1 R3 H/ }5 Jconfigure: error: couldn't find APXS
- q1 @: g# X p! B$ ~: K& U7 uapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
3 Q, W0 g: W" Q解决方法:! G! D! {: H( K& Z) T! _
; Q4 H3 q1 D& A; J O( p( l0 C6 D t
yum install httpd-devel
& X) @3 @+ T' w% z9 p9 q: y2.没有pcre
' Z+ V. O3 ]2 y1 [) X( Y/ ~% M; X6 @8 r) L- _/ R& y
configure: *** pcre library not found.( ?/ ], G8 L) p# s: R# S
configure: error: pcre library is required, E' G" \& ^, P# b
解决方法:! @- Y, n, _) B, g; Q, v
1 l/ I% N$ }' K: _7 E2 h* ~yum install pcre pcre-devel( h& X. g8 f# L. ?" a# J" j
3.没有libxml2
3 m, i# ^! d7 \2 [" a1 [0 z2 t
2 B9 t1 |5 R- J# b9 V" Y& \
' Q- t; w; D0 i# }configure: *** xml library not found.
* G' U# [. @5 n- ~2 ]) x7 |6 v# x5 Vconfigure: error: libxml2 is required- W: H: ~ P7 { D+ f0 A* K' f
解决方法:; ]- R( v( n) |
( Y+ b( l$ E9 I
yum install libxml2 libxml2-devel, D! z+ n r R- N, j+ [
4.执行 /opt/tengine/sbin/nginx -m 时有警告, O( ^1 Y) _( d4 E
, D3 h9 N. g0 b: Q; U5 pTengine version: Tengine/2.1.0 (nginx/1.6.2)# Z- ]* V- \0 K
nginx: [warn] ModSecurity: Loaded APR do not match with compiled! a6 x6 d" W& _; h, q" M" B6 \
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
7 k0 m9 M9 G' `* I3 G) c4 @- }' u* X* x
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
. T1 m$ r6 D7 p" K2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
4 ~: l+ Z. l) v. s" o" u: `4 J2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!" J) v. T' I9 D/ e" x3 h6 z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
5 C5 n1 u/ k1 w* H! C2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
2 {* H9 T! d! E2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On." P; V# m6 S: U8 |# h
解决方法,移除低版本的APR (1.3.9)
; G! {9 K9 {. j$ @+ v$ f; A8 @6 b ^, w/ b
yum remove apr/ x) h2 c: ?: r9 W( |1 O+ X3 b
5.Error.log中有: Audit log: Failed to lock global mutex% G/ O3 |+ a. |* }0 B
0 D0 H% |5 i+ \+ w2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ) D* z9 ]8 Q/ ~, e8 T6 J
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]# |0 l! h2 i5 O$ b7 c
解决方法:& }6 ]' i- f! ~% z# T
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
; _) c a4 {+ T* {5 k6 Y" l2 b& J0 f' \' b, o* o7 _5 F% t
SecAuditLogDirMode 07779 i. a$ { ^' P$ f) j* v
SecAuditLogFileMode 0550
9 t' f3 I7 ]& f1 p( E! iSecAuditLogStorageDir /var/log/modsecurity, w/ X0 d' P( W1 A# w! u
SecAuditLogType Concurrent) g3 p8 m" y6 y5 s0 b
参考文章:* H$ h. M8 Z8 u, @% E L, G
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX) }& |1 @# t# n( w) D" k
http://drops.wooyun.org/tips/2614 |
|