|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。0 O. D- {" b# p) h1 r+ n* o
7 I: ^7 Q, _& \一.准备工作
! S& y! b+ B7 v% l8 p0 \- j% q
p9 ^& H' U* Y/ b2 t# P/ N系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
2 I2 S7 q1 h! f) O# {: v, R
9 y. _, G# ^. a5 [tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
B3 F! g2 L' [- K* P' X$ G* @3 y% L; d+ ]2 ?) I* g
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
! c( ], a: e4 F y* [: J& z1 z
' V5 q" [/ m4 h0 @OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
* f+ D0 [" Y3 D0 S9 r
; c0 H. F7 V7 y) m! A依赖关系:4 l& N/ J3 E3 \# p) W) K
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:- G' N; g; R9 \4 Y) E
2 X: P8 h/ ^ p2 j- E
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel1 d; ]& ^7 A3 w
modsecurty依赖的包:pcre httpd-devel libxml2 apr
5 h2 Q. D8 I+ }- B$ M
1 h" L% V7 d. l" L+ f* l) k! l4 Iyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
/ c* _! v0 [5 Y$ q* X- C/ Y二.启用standalone模块并编译
0 D7 n. ]+ O' c7 Q
# X4 c+ Z& W2 r& Z下载modsecurity for nginx 解压,进入解压后目录执行:
3 H6 N- c9 W/ r
) v/ H% Y, A) N* ?, Z4 Q% E./autogen.sh
9 o: k: n, a/ \ F# L0 G6 x9 \7 S% N- r5 E./configure --enable-standalone-module --disable-mlogc
& K8 M# J/ v, A8 ~5 mmake
' v0 Q5 B; A! n! u( N/ f3 D% W三.nginx添加modsecurity模块/ u8 ?+ u- I) h; R6 r% N
" I; _6 w# ]6 D R2 x在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
2 P# Q- x' A9 v- r* V& N; ~& T8 U2 k& D4 V5 {
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
( n8 M0 k. G1 M1 j. Kmake && make install
6 d; A' K: i. d7 g [9 y- n. M四.添加规则! x8 l0 X- S7 I3 B( X
: ]6 D! O) p/ s: D: v7 h- S( T6 L
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
- R* Q" V( H, D7 O$ O# K9 G5 j" O! t4 D; X# o T1 q: J
1.下载OWASP规则:
$ R0 B& u, A5 S2 H% m8 ^' C6 R' y* w' E% g# f \" }$ f
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs& G. e; G) a! b7 R
( N s. T& l$ @+ b( V* d
mv owasp-modsecurity-crs /opt/tengine/conf/
* j5 @& i! Z* j& T" h" e6 D( F
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf! U# V% v3 H. `' I7 d$ g
2.启用OWASP规则:, G, p2 M8 ]% _. [% R9 N$ G0 k
, @- F" K x, v+ d* S
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
2 z7 v. B4 d( H9 d* |" m; C+ E
" t- m6 {# {' ?' a编辑modsecurity.conf 文件,将SecRuleEngine设置为 on5 w% g! D& o( {+ x Y8 w4 x! b
# L. y) L$ U" m) s
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
! T- t$ J6 A4 |$ |: {. s+ C0 f4 Y8 m( O1 E! l( Z$ {* ^" B
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf; M& _' Q, H! s4 m
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf- B: Y# \6 k) v$ X: C
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
, T) e( @" ^& E/ k' kInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf2 g. o; C$ H3 _4 h
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf/ M. W1 V# i, Q- ]- {
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf( ]7 L( C, d ]9 O
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf8 ?% x0 u, r" `1 @3 m) U
五.配置nginx! L3 _# _9 r3 P) I5 `; T& j- [
$ x: g' T$ D8 S4 B1 p0 x
在需要启用modsecurity的主机的location下面加入下面两行即可:
! z" D ~3 k# C# e9 O
5 b0 E% k$ [9 Z4 [ModSecurityEnabled on; 2 K+ u( e1 d5 S! I
ModSecurityConfig modsecurity.conf;
6 M0 }& z( H" K( O$ z& l1 W下面是两个示例配置,php虚拟主机:
* {. P. f! I" L5 d
7 d# n& i" d$ c& a( S6 R# oserver {5 V* P3 w9 N1 V% g, A4 F
listen 80;
* Y7 j% P+ b# o# X- l) u q server_name 52os.net www.52os.net;8 J5 U% G; ]; K/ p, u
: I7 W+ z: k# H7 P& e/ Y k location ~ \.php$ {1 q5 e: ?$ [- B" F; C, q
ModSecurityEnabled on;
" N5 E9 ]8 o# Q/ {# v! Z ModSecurityConfig modsecurity.conf; L: H/ b) g, c
- H y; Z2 c4 s root /web/wordpress;: t6 l! n) X" l8 g
index index.php index.html index.htm;
# i- p' C% v% O" `/ ] X / E A( l4 u7 m: X
fastcgi_pass 127.0.0.1:9000;, D' u, j" d0 P2 j( S5 z
fastcgi_index index.php;
, u$ R( e0 S4 ~% ]3 Z$ {0 r fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;# N" l- J6 V" l* K$ S5 {
include fastcgi_params;8 b! c. [/ s: j4 k& i2 |
}5 f4 l0 L8 s. S4 y5 n! N3 J/ b+ I
}
8 ^* \. ?7 f2 J$ e& K" b0 E7 Vupstream负载均衡:
5 ^/ S5 [7 j- c7 a2 e* z m0 E- V2 l: f
upstream 52os.net {: u( X. I8 z8 ^: I
server 192.168.1.100:8080;8 L) h$ ?7 n/ Z: ~! K& ~
server 192.168.1.101:8080 backup;0 V$ g' G1 k3 P J
}% M1 `) b* S3 T8 X! q
1 S3 |- [. Z5 n" Y% f6 ]! v
server {
2 d/ C V& U' p- olisten 80;
H& t( N, J0 Y8 I3 N, iserver_name 52os.net www.52os.net;7 \8 K6 j9 {6 @& K& R3 K& A
2 f" X6 r: I/ ?# ^4 ?0 v5 w7 o
location / {" P: j- G7 h' d! B! r" o: n
ModSecurityEnabled on;
0 @& ^6 k; v" U; ~/ e& h7 W ModSecurityConfig modsecurity.conf;
( J! a. a% F1 l& U: ]& K- C: ~; ^, G$ T# G" G* _% Z1 T* p
proxy_pass http://online;
+ ?# N% d# ~- V$ n proxy_redirect off;
# j9 U/ K; v( r/ P) N proxy_set_header Host $host;
Z& M3 h7 M4 N; s c6 _' T! | proxy_set_header X-Real-IP $remote_addr;
/ _8 B; s( [" l4 A proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
6 v3 J' g2 `8 ]& j5 q" \ }; S; f/ S! [/ L0 o
}
0 h+ ]2 j; y) x* q六.测试
( Q( I3 t* ~" q5 |. j& D8 E
3 E) B' f4 }; m# A9 R' n* D我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
$ @( a# [& U8 }
9 z0 w4 \1 p: \4 V0 O3 q<?php
) p) r! w n8 J. V1 P phpinfo();
. S* ~7 R) \) z9 `?>
. s. g' D3 r1 K2 T4 |$ e. E在浏览器中访问:8 ]% ?9 v( _) _0 R$ F* E
( _$ w/ q1 Q4 w2 u5 l* mhttp://www.52os.net/phpinfo.php?id=1 正常显示。
0 U$ I+ K7 K) |% ohttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。! h8 ]- s/ o) g5 v7 T# Z
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。, V% Q* q9 G+ x& V: r+ {# ^
说明sql注入和xss已经被过滤了 ^- ^; [, r) \& P! W! Z
- t7 z& E/ I8 i9 O3 N1 Q' X
七、安装过程中排错, r* V0 k1 t4 }) t8 ?
5 z2 X6 q* t2 a
1.缺少APXS会报错
) V* e# q$ F$ I. F
) ^. u7 {7 w. a: t. uconfigure: looking for Apache module support via DSO through APXS
; T2 ~8 h6 k" }1 k% V V; @& ^% sconfigure: error: couldn't find APXS
3 y, ~% B8 u7 F6 ~. Capxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。- s4 R9 N8 O3 D8 I$ _
解决方法:" ~4 C0 A% y4 x' V ]2 `
1 @7 `5 r0 a) g$ C" S \& v
yum install httpd-devel
1 k3 H6 U2 `3 t* I2.没有pcre
7 H+ A% V7 S7 t' f" V+ O. l0 t# o8 M; D* j N. i( N* ]2 j5 `9 O" w$ L
configure: *** pcre library not found.
1 O& w- V/ j5 lconfigure: error: pcre library is required- W2 M' K: t( Q: \2 R9 D
解决方法:
/ S! \9 W2 u! Z1 T, |
: n5 Y9 N& A5 g ?* Wyum install pcre pcre-devel* u/ `( [% ` x# ?' x
3.没有libxml2( j) }# P: x- c2 D
, ?4 r/ {! B8 U
( ?4 {' G- t7 B; z( N. r1 M/ bconfigure: *** xml library not found.# @+ v* ^, @+ g3 N4 E% w" }4 i
configure: error: libxml2 is required
! h" u7 t$ v: F解决方法:
4 r! ^ C* D# q- V$ q1 @" b1 J* D
3 i' E2 j2 ~, `* B' yyum install libxml2 libxml2-devel7 w! s3 @* m+ Q8 ?5 T! y o
4.执行 /opt/tengine/sbin/nginx -m 时有警告
. N" y: N0 k6 M" W( h/ W3 p& y3 m2 c! ~& k! {. V1 D; |4 _
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
, ]) F1 Y2 ^2 Bnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
0 F$ @# n7 N: P9 [8 i# f# {1 k+ \原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
& b8 w( Z$ p2 Y
1 u5 ]( t' n; k) W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.; z; \- X& f# r4 r" x" g u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
! H& D# b7 P) @* O; s# E% T2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
' E0 O$ Q: O& q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
0 S9 G0 z# S8 v# A- |2 b& o8 V; P& ]2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
8 i/ [, Q, X: A" J5 l2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.* I! R- S# k' L1 Y( v4 |6 y
解决方法,移除低版本的APR (1.3.9)% u1 b- i8 H7 y3 m, m
9 v% b0 s- N3 x% ]yum remove apr
1 f6 p3 }* m7 M5.Error.log中有: Audit log: Failed to lock global mutex
- o% y: v/ Y, B( |5 z% l/ U
5 I2 R1 C6 I* o) U# Z( l0 I2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 7 k W- o8 z! `% g% h
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
; F# o' u P# y解决方法:& q% s2 S. u# H- S- S& J
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
, M0 ^# Q7 R" z }6 S8 Z7 x l t+ ~3 c3 M, A7 l
SecAuditLogDirMode 0777
+ u8 a, w! ~$ T9 @1 iSecAuditLogFileMode 0550
( Q$ @7 X3 h$ |5 Z: T! |' t \SecAuditLogStorageDir /var/log/modsecurity" b# a' X1 K4 T4 h
SecAuditLogType Concurrent7 c7 ^7 o, z! S
参考文章:
4 x' m9 t( i! ^$ e6 r/ F0 k0 p/ Q5 Zhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX/ ^* |! S0 X# K& J9 Q! n
http://drops.wooyun.org/tips/2614 |
|