|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
% D8 V; |, w) y4 E7 ?3 A% q1 A' T
% r+ D+ j3 t. T x一.准备工作. Y9 q" g6 T0 k: N
( n, Y( E7 w, K$ ^$ c; r系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
: o2 W7 M1 W2 C# r% n
& D3 Y; ~6 c, h- q' f% H6 N: Utengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
; E- R. }2 B8 | D! L3 X3 d; Q# R A
6 c7 l1 s+ @+ p% n, Q! K$ d: H$ vmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
5 `6 N1 O1 R1 o7 n4 v; R/ i3 |, d' x0 J. S& x4 c$ }0 A- F( c9 l
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs; M& C9 g" g" x3 M. [/ |
7 e: i* @0 E( N$ @
依赖关系:
: y6 W9 T; d7 i/ S& ztengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
9 d* e: U) k0 P4 O& r
/ `- m; m' Y* ?# r8 \yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
+ R H P X! P5 }; i. rmodsecurty依赖的包:pcre httpd-devel libxml2 apr1 g% r. @. r+ a) \1 \- _
) F A6 M8 m( D0 i# Oyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel4 l% a! M$ i8 s( c9 ], ~' F
二.启用standalone模块并编译
$ h+ L' o2 J) L/ F1 u: e
5 z9 b6 t& q% A. H" a N下载modsecurity for nginx 解压,进入解压后目录执行:
5 D6 T2 M2 W0 @: g
5 v' L; U2 S3 d& |* ]0 r6 l, Q; o./autogen.sh) ?: ?2 s- m- }9 P _; U# r; v
./configure --enable-standalone-module --disable-mlogc7 b& \3 `5 M e& K2 j
make 2 H1 V7 H- x; }( m0 \; X/ E- V7 W
三.nginx添加modsecurity模块3 m Q3 C6 c9 Y h, P$ V! k; f
6 o0 c i p9 ]
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:! b; |. X7 g y
2 A7 E. w8 W" `- R
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine6 r3 d1 c' b+ W) ^/ P: B" y/ D
make && make install
8 _2 e; @& P6 T h四.添加规则8 h# z( Z% E' K" {
9 A; Z; i" a5 |" lmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
( W. _% N9 n6 D+ G! o+ Z
0 `' M f+ O" q% L1.下载OWASP规则:2 T" m" E% k! S
; ~2 o* n3 s, N5 u
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs3 q: h" N8 M9 e! \3 c7 G1 c
4 v1 ~5 Q N' j( [0 `' Z+ emv owasp-modsecurity-crs /opt/tengine/conf/8 S8 u3 @8 B- h1 O/ \9 B8 v# l
- M/ T: F. X" e' g, e D
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf' ]- n6 t7 q* o) _9 j$ l$ t
2.启用OWASP规则:4 p) M- V% Q8 y2 x- z& {
5 M9 G- f9 I, c! m5 z9 w; ~# E z
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
, C6 p& p( n* t4 y# r& c* [0 }
' w2 Q: [) n* K5 @6 j, Y6 h编辑modsecurity.conf 文件,将SecRuleEngine设置为 on5 z/ c5 P" H; a5 q3 {
6 L2 o5 N5 c* n' l9 {
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
. ~6 ~5 A" ?8 ?
! c* ^0 L) k! e2 j" T. i* I2 SInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf1 G0 [1 _4 U F! ?/ }" q1 Z7 p
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
( j3 M1 Q6 J2 aInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf( c! K* q R: t1 i4 Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf& E2 J/ r& E5 e; B
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf! A- R% Q; S( ]8 Y( ]" E
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf/ P+ f) s; W$ [
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
5 K# q& g" {2 v+ F7 ], V五.配置nginx! ] _. d" q7 B1 r' X4 i- F, _
1 r J. F7 \% x [/ c在需要启用modsecurity的主机的location下面加入下面两行即可:
9 V( R+ o5 s8 Q8 L9 L' W. h, H1 Z( J4 L+ X
ModSecurityEnabled on;
, v* |) H; w# D& V" s' kModSecurityConfig modsecurity.conf;% T, C& K& n4 m% V( m% s, b# e1 o5 {
下面是两个示例配置,php虚拟主机:
: v/ ?. r' {+ E% t x
6 t: M* U% S5 V& X; eserver {
2 A+ I6 G4 l. _8 m% m1 d listen 80;
4 r$ z& e2 ~) s5 A- d" h3 \3 j F server_name 52os.net www.52os.net;
% T: s5 {1 B, e1 g# j/ _* N % B0 d+ X; [, W8 S
location ~ \.php$ {0 r+ N" I# u5 S1 H& }' c
ModSecurityEnabled on;
, K! z( Y c& w ModSecurityConfig modsecurity.conf;1 W6 R$ Y3 c2 {) u
' w6 V2 `& m. g) c# o" X, g root /web/wordpress;7 G( g/ [# m$ Z5 C* i
index index.php index.html index.htm;; x6 w1 X ^9 x. B
, D% g8 I# j1 o- R- N$ P, s
fastcgi_pass 127.0.0.1:9000;% [) A5 ~5 z1 z P# B& q" _
fastcgi_index index.php;* R+ }1 U4 }8 d! z. e* q; H' x
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
% @3 a& F1 t2 b3 Q; f" }* R include fastcgi_params;
$ u$ i4 p: @+ h5 ^ }, Z2 U( M8 |4 i( I: i+ [
}7 A3 N7 I5 m' T0 x$ F( }9 v
upstream负载均衡:
; O! f( |' T) k3 P7 M
) J4 y8 n: p! O t' L$ S8 p( _# Kupstream 52os.net { Z/ i; n1 s% k0 d' c
server 192.168.1.100:8080;! r; T$ n3 T/ s
server 192.168.1.101:8080 backup;+ t- d5 H h$ M5 I+ c
}0 N! q O$ ?2 W) M C" }+ D! h
: G/ F! f- q1 {server {% K$ B; v [, w7 S* v3 i4 ~
listen 80;
: L/ ~& \& K5 d/ aserver_name 52os.net www.52os.net;
2 g; a! C5 `% @8 N3 P. I7 _2 j9 C/ m( Y9 g/ b" B. @$ ]: P! m: I
location / {/ e/ m2 p! b* v8 G
ModSecurityEnabled on; $ w( Y% j5 ]5 P3 t) }, E6 q
ModSecurityConfig modsecurity.conf; & Y) O+ T" l& M& C# x+ B2 n. c
" `; s# K2 j0 i* J) k" G* A! e! Y proxy_pass http://online;* F1 L2 |; ^! A8 X
proxy_redirect off;' ~- y( ]! ?6 n9 h
proxy_set_header Host $host;& ?$ V; R7 u, F
proxy_set_header X-Real-IP $remote_addr;
: h! p' Y$ ^& N) S. `7 p1 ?% l3 r! Z proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ h; w6 H8 u; T7 U1 H& j }6 P! u' i' e& n# I1 v7 {
}
6 g: s" o' B3 r A8 o1 d2 m9 A, ^六.测试: s. c. [1 n5 ?; T! Q0 J8 D
% Y0 S" I+ N& F9 J1 W5 E2 p7 Y我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:" R" I& W8 t; G b: q8 y
$ Y# V" [: z2 @% q# j/ x
<?php
; k/ K! ]& g9 x& ?! B7 M) N phpinfo(); 5 T4 ]9 Z: P& `4 b4 }% [( \. e
?>
6 Z+ q0 S/ z7 v9 i+ F在浏览器中访问:# s: @9 d6 v& k# d
+ ^8 o/ j3 g: C& U% w; Mhttp://www.52os.net/phpinfo.php?id=1 正常显示。
# }- ]5 x) `5 t9 I# Z/ jhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。! k0 i6 @0 X b
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。: h$ u$ e; S: w. L
说明sql注入和xss已经被过滤了
. }! ^- l: J7 Q2 h8 E+ S' J% K! y2 G1 \8 R1 }4 ] ]! V ~" {8 A- ?0 G
七、安装过程中排错
9 h" j w; W; O M) \+ t
4 s6 b P5 T0 s; {1.缺少APXS会报错. |6 j" s) }6 P+ T9 @, ?5 ~) P
& o) ]6 N% e" f$ p
configure: looking for Apache module support via DSO through APXS& C2 ?" U1 Q* y, s! ~
configure: error: couldn't find APXS8 A( r ^% }3 R2 v2 U
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
2 R* F' v* {$ [解决方法:
8 F& Y U# |; A" R' K! g: b- |/ K8 I) A
yum install httpd-devel2 m% Z. V$ l6 w2 Q5 `1 n% M' A& B
2.没有pcre* ^1 M- V! C6 m' n- Q- ?, M4 x
% W! n; |7 f6 Y. ~* n+ W
configure: *** pcre library not found./ P ~* M2 E- c/ Y
configure: error: pcre library is required; V! m! q5 ~; W# i+ W- U0 O: V
解决方法:* {1 {$ A* x: g: Z% F* H
! o" N4 m0 N" r$ N& }/ u6 j w6 @( N5 Uyum install pcre pcre-devel/ G3 t) T: J' y; R# ?
3.没有libxml2% U) b& e: D/ b( @) M
. {$ J2 h" v7 ^
2 x' T+ ~. x4 q# y4 Wconfigure: *** xml library not found.; H+ D5 _+ Z3 v5 t
configure: error: libxml2 is required: |! d( X0 @+ I1 @) F: s" O2 m# b
解决方法:
6 s& Y& u# Y7 ]
' o' K+ M$ k8 ]7 T. A( o+ H5 s5 Dyum install libxml2 libxml2-devel
1 q; G$ l/ l' x o4.执行 /opt/tengine/sbin/nginx -m 时有警告
2 S: ] C6 G7 {5 [& I$ l! @
' }& t: L0 v9 l* FTengine version: Tengine/2.1.0 (nginx/1.6.2)
/ F- R% a, u, Y! }nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
$ P" W0 g% x" U4 T2 d0 L原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
$ |; V$ M3 i8 d" G" z2 S( h; S' \' T! ~, }7 y) ^2 X
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.% k6 |. Z# P4 Y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
0 I" j" L9 m3 B- ]) l& L) S2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
: m/ b# D: r8 m9 Z* L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
- C3 r9 A, ?. R2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"# N- o; a( h* s- V" N, n0 \
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.) J# }3 X5 B4 o8 ^% X5 r
解决方法,移除低版本的APR (1.3.9)
% M u9 K+ h6 L; T' g( D9 {! M; P) t' \) |
yum remove apr
5 j( Q( M2 R4 | X/ A, L7 L/ l1 ^5.Error.log中有: Audit log: Failed to lock global mutex' C* V1 I( \7 i
. S. I' r$ r5 H$ h2 v. d6 w
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock $ m& X( M7 b: ~9 }$ w
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
% r9 q3 O' F) n: N- ~$ }解决方法:
) a; ^: U- d" k2 N# P编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容: c) B1 n" w1 R- O; L2 {
4 [6 M' s' G1 A+ {8 C
SecAuditLogDirMode 0777
! \3 O. o0 x( U* KSecAuditLogFileMode 05500 B$ p8 L. i8 d/ T
SecAuditLogStorageDir /var/log/modsecurity; x* ]5 K8 z+ a! X# I
SecAuditLogType Concurrent& f3 V) Y( G0 J7 l1 e! v
参考文章:+ [' b N5 s2 f6 a' o) o
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
, X" Q+ o" J6 R, I0 H+ a. |http://drops.wooyun.org/tips/2614 |
|