|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
5 F# X- K- ?6 r& _; v+ R
; }. d. q1 l7 }一.准备工作; `0 o* a8 b) ]0 F0 `
7 V* o3 @- P3 E5 m( h系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0 z% m3 A$ q6 k3 p# E1 E
v% C ~) D; B* e" x3 x9 |tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz& [& E }- M6 f
- ?- y' _: u0 ^' r2 ]+ z! {% }- o
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz! k& ^8 P- g! D0 l/ u9 F1 Z
5 x2 Z, i' G" U# ~6 COWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs8 G- T( Y% f/ W! l: w, p
# `' \9 W# h. `
依赖关系:
& f; G# T7 E. k4 ]3 Etengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:2 z6 o+ ?6 t! q9 m. g @/ M
2 v- x9 g1 P$ H P: P+ o- ?# d2 s! L
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel/ `, M" O, f h7 J1 I' Y
modsecurty依赖的包:pcre httpd-devel libxml2 apr! b1 W- m# ^' s A2 @
8 R. Z9 c9 a% X4 r! a3 p* ~. @
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
7 q0 x/ e( J# [. [8 S/ \- P2 Z1 C: e二.启用standalone模块并编译
4 z' U8 \3 E0 s9 ?! y
6 J$ [& e4 i. F% T; b) a, z下载modsecurity for nginx 解压,进入解压后目录执行:% m% ]. u8 p7 }8 ^
! B. p: a i' m) g7 K2 u./autogen.sh
" w* O% Q! V9 \7 s./configure --enable-standalone-module --disable-mlogc
, \) a$ w: f9 B. |& lmake - Z: Z% ~8 `. f/ ]- p1 j
三.nginx添加modsecurity模块: R: A6 P# Q6 N' u- C
9 p4 G, J! t/ Q% ~1 \在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:( k$ s& [* D0 C! ~' E7 W- J$ U
# M% ]% T8 _& N* D( c
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine5 | x! Y0 w* N9 O2 J* E' P* C4 d D
make && make install { x+ s! Z* p0 d9 a% g- {8 s
四.添加规则
$ E2 g1 ]1 M' X/ p2 Z/ a( W8 l- Y- ]! I f9 s9 s7 A+ V. V9 b
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。; y a- D4 m2 R+ ]
0 j7 ?) ]7 [3 P1 a, ?$ `
1.下载OWASP规则:
8 T! x9 L& L" z- m( t8 |7 C: @% b
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
3 o D+ _1 j! T5 ^% z3 {0 d; y( {: S9 x- M9 O" _$ {8 I! F2 F
mv owasp-modsecurity-crs /opt/tengine/conf/
- e( ], M! b- F0 v3 @" K+ n
7 {- s6 W+ p6 L. S1 Ecd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
! H0 D% w% Q$ _% Y, T2.启用OWASP规则:
+ m' ]6 b$ a1 [( r6 }3 ^0 B+ i6 L" Y5 ?* E( Y
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
; T1 @( Y( q0 U2 M; j4 {; X0 n" m6 B( _& S9 w# p h9 r+ d
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
/ |1 q" w! n# {7 k: F
* b& x3 i) c4 O( K0 e; ~owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。: d X- [) U+ f) G5 o
' P! z) `$ N7 | x* |Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
! n3 b1 {- Z+ M# i# kInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf1 n! M% g$ ?, i1 t- S p
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
5 E v' I B- c' aInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf7 J$ v/ Y3 a* H; m$ ]. m9 Y2 C
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf; d+ b6 _7 U* W
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
& _- a n, j P) l+ gInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
8 P* D) ]: h* k9 y' v五.配置nginx( Z/ R4 d1 f e5 n+ I0 P
& d2 g% p. [6 g' Y在需要启用modsecurity的主机的location下面加入下面两行即可:
2 y" `3 |& Y, F7 Z* M" c& x8 I# M6 B
ModSecurityEnabled on; 8 g* l9 n! f0 S+ [# }3 g [
ModSecurityConfig modsecurity.conf;
]0 u8 Q5 e6 p* J6 I7 f* m下面是两个示例配置,php虚拟主机: r W" t3 K% O9 ]8 S
. x* U C4 ]3 v8 D2 R' B4 d
server {
4 U6 F1 @. F- ~3 f+ d listen 80;1 q. O5 n% t& V8 o5 o/ l/ N
server_name 52os.net www.52os.net;
/ J$ S/ w- \% g! ^ 7 P& k% R1 y/ e5 \: r
location ~ \.php$ {, c# \1 A( a; c/ z
ModSecurityEnabled on;
{) c3 D& X- T: O3 E& Z A ModSecurityConfig modsecurity.conf;
- G4 V% }5 I- N( S9 p; L7 ^4 x, j% t1 Q9 R; f* s
root /web/wordpress;1 P0 S# [5 m- h- x5 P
index index.php index.html index.htm;
8 P3 {( T7 t+ M* ]) ^2 |! {
: m5 s( o! I9 W: J" { fastcgi_pass 127.0.0.1:9000;
- Q0 d: L8 Q& G3 w7 |) o" _ fastcgi_index index.php;2 z, I, {, G7 m2 u8 A+ L6 @1 i
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;# V$ r$ Y4 q2 O: E+ T
include fastcgi_params;
8 F4 C) x7 O1 S# F }
* z( M; z# l0 ?& r$ K; F }. H2 q4 d: C, W0 G" I; J; h
upstream负载均衡:
' q) Z3 D/ m# a) O. c0 h6 H- r0 N8 V) z! q' [
upstream 52os.net {
: M6 r! A& C. V8 S7 O4 \8 f8 ? server 192.168.1.100:8080;
+ I' k, V: S( R8 o9 a: g x, _ server 192.168.1.101:8080 backup;
8 |9 o9 p# O7 h) W}
6 S/ K _. j& o! d6 s+ r5 ?* p9 |3 R5 Q
server {5 L: J8 D2 X0 g
listen 80;
9 g" w9 I* `! z/ b Z2 _# n# Dserver_name 52os.net www.52os.net;
0 `% }3 E$ [ q1 {0 q: P, s, b. P- K: u. z' m3 c W1 O5 f2 }
location / {
# |! V, H% k3 E+ H: f, m ModSecurityEnabled on;
. w; X& B! j) ^6 v0 h ModSecurityConfig modsecurity.conf; ( ^" @. H" V# l2 Z& B) q* H
- b% C4 ~+ r- o" s7 B5 p proxy_pass http://online;5 X3 q6 c! o5 {: Y1 C
proxy_redirect off;
" r- L0 u# j G; @0 Q/ \ proxy_set_header Host $host;$ P/ o7 p$ I/ B1 ]. Z6 V
proxy_set_header X-Real-IP $remote_addr;
4 U j; F6 p9 f. R! r2 s proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
& w! s5 q* W1 X6 p( E0 R, g }7 v- f: S! A- M& E6 ^
}
. b. E. ]% ^3 R. M% n2 {9 |六.测试
: H, a& A% t. C
! r7 o3 e3 h$ O! A+ Z我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:) O, C0 i' p0 u! J
; f1 {. W# ^ j! q<?php
c) }+ P9 _! n: m phpinfo();
! ]# e" S$ i. X/ F( x?>: \2 P, B1 O4 B$ |9 N3 _
在浏览器中访问:
& m e3 @4 n0 \' L: T3 Z4 G
2 h! }) O. i7 p! J, l+ |, jhttp://www.52os.net/phpinfo.php?id=1 正常显示。
& ]& F& U* u6 X' l: @' a" qhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
! C( Q# d; ^ S; V# A& g& d5 G/ xhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。- S/ \" c1 M; Z" y, d
说明sql注入和xss已经被过滤了
+ T/ c* [, W1 z0 j
; \( l- [7 n% A9 y" z七、安装过程中排错2 h/ \* j# T2 d: D- z& c; V
) T/ B. w/ v1 m" B8 h& q: `
1.缺少APXS会报错
0 W5 w4 u5 S' d
" D; i; E# i& Rconfigure: looking for Apache module support via DSO through APXS
) S. D% x0 n# f' G0 T6 @. ~configure: error: couldn't find APXS
+ n1 q8 s. H) {( w' a7 |7 Gapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
# |7 W- k H# e解决方法:2 d* _! Z3 p6 x8 @* N
3 I+ z2 n1 T: D# i+ g' ~
yum install httpd-devel/ y1 h6 E( y( ^ Y6 C @7 \$ x
2.没有pcre
2 i! n2 K4 k( U1 P
. p) m7 S! ?4 X: e5 m8 y9 Aconfigure: *** pcre library not found.
! Y/ J0 J. g+ e" H- xconfigure: error: pcre library is required
. p& b2 f& e. q: e解决方法:7 ~3 m, G K9 O. c# b* B
( I! v! X' Q8 c: @8 w
yum install pcre pcre-devel5 h' R/ v9 T" \$ H0 z$ E
3.没有libxml2% K' P; y5 v3 I9 Z5 y
: v! ]" z% }8 E) r! r+ D
! z+ [4 C' \, [" Y) |configure: *** xml library not found.+ H7 K7 P/ L8 [+ F/ b, K) S
configure: error: libxml2 is required
# o3 q" H- t' r) ?8 A3 Y& ]解决方法:
: }0 D% v) Q4 z! b/ {0 I# ?4 G: b: `/ y4 X
yum install libxml2 libxml2-devel
3 P& v% n& A, @' @' d F4.执行 /opt/tengine/sbin/nginx -m 时有警告
- o8 Y, U6 I2 C( l* k& |5 \
9 a# Z6 r" Y* z% ~5 v6 xTengine version: Tengine/2.1.0 (nginx/1.6.2)
k# y/ \5 ~8 w R, a' ]nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
% r/ U1 f% {# a原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
2 d9 |. A4 E- X1 S. b
! x% r6 F/ m. m7 A7 j2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.: C+ G( V$ Q: q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
3 o: n( g* S/ u6 Q: a9 y2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!+ a' r- B( h8 F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
, V4 x3 Q8 g) ~: p2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
% a5 i, R2 ]& D# M7 w6 z3 B1 L2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.: C1 J8 e+ p: L. Z+ S- X
解决方法,移除低版本的APR (1.3.9)
: i) @. c& o7 v* O; w1 \
# i- I) A" F% I! ^; K3 y6 f; t Gyum remove apr
% R$ @2 G! a2 S9 C5.Error.log中有: Audit log: Failed to lock global mutex
# F) V( O) r2 s$ B. m- L: g0 n. d; f1 A( K+ f$ E
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 9 v6 g8 T# u, P* l
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
5 ^% Z+ r ], I解决方法:9 `2 a. E+ X g' S
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
2 {9 M% K8 {. A& N: Q
! |7 S0 P( d) ]SecAuditLogDirMode 0777
7 z* e4 o" `+ P* W6 JSecAuditLogFileMode 0550
6 a/ k+ r3 d% sSecAuditLogStorageDir /var/log/modsecurity
9 ?& L1 U4 C6 @8 E7 h8 n0 \SecAuditLogType Concurrent
4 p$ Y2 w8 _/ r# |& l9 X% t7 ]参考文章:% ^" }$ X3 p6 i4 B
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
, c8 N2 y, o6 ~& ^3 ^7 Shttp://drops.wooyun.org/tips/2614 |
|