|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
9 P8 e2 B6 |! M, W1 \9 X6 A: [: y# t; [" b3 i% @; u# F
一.准备工作
5 P! k, A4 Z2 s8 D
" S; z' K2 v' S8 o s; T系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0! {3 H& n6 H7 h
1 `& r8 q3 y' @6 R& Etengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz' i% B( z& E# t( B
1 Y0 y9 C3 J/ G, C3 v" M
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
2 e I- c# ^0 N/ F
3 O" H9 Z* e- |( T/ F+ eOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
$ H0 S0 v7 v+ [5 j+ P& \) G' _7 B& @4 @
依赖关系:
" x" J- f" K$ J! D* A, Utengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
# m7 ^6 B! w5 r6 R- }7 f, S
0 x; K4 [7 O! `) ayum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
' m- a. I: p, h k# gmodsecurty依赖的包:pcre httpd-devel libxml2 apr
6 v9 i, @5 M! m; `8 X+ W- D8 N* ~; x! r6 X. G* [
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel7 ~7 A8 ?4 {/ T3 l/ ?: x% A
二.启用standalone模块并编译: E) }# ?# v& ^" P8 Q
5 m( w: U p* e6 [ A: i& m下载modsecurity for nginx 解压,进入解压后目录执行:
" B. { F% H4 P8 _3 ?7 p$ m+ V1 x" q$ j9 {' a
./autogen.sh
2 M v0 m, g7 D3 @& t; D2 L./configure --enable-standalone-module --disable-mlogc
$ G) V; [+ ?3 |, |5 V* ]make 0 @* h* H" n. \+ d+ ]5 t& U9 a" i7 F5 _8 a
三.nginx添加modsecurity模块
8 K5 b+ i1 O8 V8 ^1 |
. A, f `9 I1 e8 j7 V" O在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:& H i, E2 D9 M% A
! Y% v4 Z) ~" i; f$ R: v; b Z./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
' N7 A1 j4 ]4 q0 s% d0 y. Smake && make install$ y) R6 _; ^1 q- w
四.添加规则+ `7 {: x3 Z* t' m
$ k! T6 g3 O3 Z y2 j
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。+ o6 i+ G: C7 L; q( G
( m9 j3 Y& q- J2 i: @1 i) g1.下载OWASP规则:
9 r# s& D- m. _
; Z; J, C3 Q; X4 `" J5 Y/ e1 Z4 ^* Hgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
2 o+ n$ K, p1 P T$ A8 K, L$ q% c0 [' }; T- l5 h6 S; Q1 V
mv owasp-modsecurity-crs /opt/tengine/conf/( @' c4 E/ V3 M1 B. f
. @& r! J7 x5 R$ @* q1 U Ucd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf \: R1 f5 {8 Q( X
2.启用OWASP规则:
' q; w. P5 o' U( u. T, S, [/ C. A0 X; o1 Z- |
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
, a9 d2 U. {9 \9 ^$ ]+ ]2 w% A1 Z
; S+ H1 G6 k g9 R2 V编辑modsecurity.conf 文件,将SecRuleEngine设置为 on s/ }3 P. t* E/ N, \( g0 }3 n
- i3 L2 i3 n1 H( L# b7 towasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。9 E& q) b; F7 e9 @
' u" N+ Q7 Q0 n4 g, k
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf: Z" I& |$ \) O! J/ k$ u0 l
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf: V3 ]: E, u% v/ S- s: v/ Y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf( l- Q$ t+ R- i8 R
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf' M, E0 a* V# f& q3 f+ \
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf4 {" W) F4 A* V) s$ K9 K/ c! Q* u9 H5 I
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf3 k5 }" i+ v, k
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf9 F: u7 f$ D# A
五.配置nginx
2 T/ y* f( k9 h$ i
6 f1 e% o4 T( Y: p- V/ ]在需要启用modsecurity的主机的location下面加入下面两行即可:6 w# Y9 f: L& ?; u6 e
" R, o4 x& e7 \1 s ?9 R9 h
ModSecurityEnabled on; 2 q j" ?# k, E5 o8 [5 [9 ~% n
ModSecurityConfig modsecurity.conf;# o3 @4 |3 a( g- P; D: Y9 i- W
下面是两个示例配置,php虚拟主机:
0 j0 [2 U0 f9 [) o
8 x( G% s4 p9 @8 Z* Zserver {; I# k- b$ I1 P( j( E
listen 80;8 K4 [! m" a' x) M6 \" `
server_name 52os.net www.52os.net;) P9 m0 a A( G! d. g, q3 j
! W. a0 ?7 W, e& E5 s location ~ \.php$ {3 `, }& O' _; M0 N
ModSecurityEnabled on;
. R# J8 K0 `$ c ModSecurityConfig modsecurity.conf;
; e+ ~% w, a6 K5 y5 j; f
. \( f2 @3 ?% ~1 f' f: }' ?+ [! i, \ root /web/wordpress;5 Y ]4 D5 I: |* b6 L# o+ L
index index.php index.html index.htm;8 N, U ?# O" G( U# p7 l3 w7 l! X
( l7 ~4 L* @( `+ p) ~, y
fastcgi_pass 127.0.0.1:9000;
' O H! f7 S# e2 ? fastcgi_index index.php;5 q, A. t0 S1 ]; w4 |! H* _
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
% F& h4 P+ T9 J" \: K include fastcgi_params;' H5 W; s4 _* o9 {" A; H& ~
}5 f9 ?1 }# ]1 w$ ]' R- Z
}$ U; A( W: E, q! q0 c
upstream负载均衡:
% d& ?$ q s1 s: r6 R( B
/ Q$ ?! C5 G9 ^3 }+ Q6 s+ gupstream 52os.net {
4 v" U0 }/ F$ D$ I+ U server 192.168.1.100:8080;
) Z! Q6 G q4 d, z9 J) p server 192.168.1.101:8080 backup;
; R0 e$ z" V! k& ?! A' @& h) u}
6 @9 K# \: x' w; L' S
7 j9 w' d H. e9 j" Rserver {+ q _: @; u' a. F; P
listen 80;, W. t7 a5 O! g
server_name 52os.net www.52os.net;
. C7 Y0 ^4 J2 v& F6 t( ~
* F( B9 T K$ Z4 `3 Y0 |4 H. I k: Xlocation / {) w0 X5 W: X5 }; `; X2 u6 u
ModSecurityEnabled on; , K2 g7 G7 g1 x* U
ModSecurityConfig modsecurity.conf; " b& F) B3 f- N- D6 ]7 s! _
* ^$ _( [# s4 f% R) C1 b
proxy_pass http://online;5 s6 f2 J" `. |& {; I7 ]
proxy_redirect off;
# }( _: q: l2 Z; k' ]. q5 v; S+ T) i proxy_set_header Host $host;9 \5 {0 V0 |3 u3 b2 t
proxy_set_header X-Real-IP $remote_addr;% [, q* t6 U E. m& I9 s
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
( w. b% y. @' y4 x }
9 n k! z$ L: ?3 B. K/ @9 H) F9 Z1 ~; W}
7 v A0 ]3 i$ r" A% o六.测试: F3 k9 V6 V1 u) _6 a
! m2 W" K' L1 Q( t) E. D我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
. \6 n; y2 O! K& l' T
, W) d9 h/ K4 Q0 k s+ ^! |<?php; v' B4 |- L8 C; M' f
phpinfo(); 1 h' o! J: _+ X' a- T! _2 v& y
?>
' `# Q( y! f/ b在浏览器中访问:
6 b: P9 u4 x: v+ d- V
( G- d O1 ^; [& N& xhttp://www.52os.net/phpinfo.php?id=1 正常显示。
! I) H! ? P. k7 g [http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。7 n# o; H0 T5 X0 }6 \0 n' i" I
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。. \& k% P' g1 O! e, Q
说明sql注入和xss已经被过滤了: }. Q5 Z- f6 s+ C, n4 U0 }& a8 i0 _
2 X+ F% C8 `% W- i七、安装过程中排错
) d: Y: g. `# M+ w; m T
3 ~6 z9 n, @! R- O1.缺少APXS会报错% t" b2 G9 @0 o4 t: B
2 i; a' L1 U' s6 V! @1 f" |
configure: looking for Apache module support via DSO through APXS
( w/ [1 t$ ?! zconfigure: error: couldn't find APXS
# c, J% O9 Q2 w' Lapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
' a4 r* v: b7 k& \/ s解决方法:
" B W, x* l5 r! l: G* h) x" y! v/ {
. \& j- O# n5 v. z2 \ M3 ^yum install httpd-devel
/ A+ N# o4 R% k2.没有pcre
- m, M+ V* V: D: @4 u% `6 U7 q
5 Q6 y9 r+ m! ~" }/ L- Vconfigure: *** pcre library not found.
( V2 B7 m r' kconfigure: error: pcre library is required
7 I/ i8 P- A' `& I' n+ P解决方法:, c4 O: Q; p" I
6 K P' }0 y1 L8 y( T- ~" Y' D6 eyum install pcre pcre-devel6 [. E' N; N7 K: I- _7 i. @
3.没有libxml2- b7 i6 P0 j5 k
2 ~; A- a3 K4 P6 @ u
; H0 m7 ~, n1 B/ t/ aconfigure: *** xml library not found.3 L+ c$ i4 \( D4 K3 W& {
configure: error: libxml2 is required
+ _" s0 b/ \9 S2 k$ C1 [7 D* M2 u解决方法:
0 @ z" Q; m" I" z( |; v, R" F. i1 {. t+ ^2 g9 V/ L
yum install libxml2 libxml2-devel
9 I, ?" ^2 f: Q; |8 T4.执行 /opt/tengine/sbin/nginx -m 时有警告. s" {$ `* q( f2 b6 n. o
' `# ?5 f# w. H; s' X% lTengine version: Tengine/2.1.0 (nginx/1.6.2)1 s" O2 V" [. C- Y6 N
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!( b! B/ \! O) F
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
6 y% P& S" o' e- @ _2 k
1 ?. H7 y( V# W! `/ c. q$ } U2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
5 n0 m8 ~" \% ]0 X$ Y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9". E8 U' W: l5 t B- L! A' `, i/ ]
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
, q, }3 l. v6 P4 k2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
2 I8 A- Z, h+ {0 F/ W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
& Q# j+ ]) e& O; Y2 N/ P2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
, n% V. P. w' O; x解决方法,移除低版本的APR (1.3.9)5 Z) i1 K8 V# E# X, a( b
( t2 T8 [5 v3 r2 y( M' B6 Jyum remove apr
: v9 f- t( x/ c( d/ Z5 o5.Error.log中有: Audit log: Failed to lock global mutex
# K. d9 u; t5 D+ h
8 M- x% I# X+ o- v2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock & l6 ]/ ?" L6 I) @* A
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
+ r6 }$ A2 } p7 e2 E. H2 r解决方法:
& h+ c `; ]4 w( T- y编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
* e$ M. o4 J. o8 g p5 n! }
! x, N3 Z0 O+ U& n& P6 ^( J' s- t9 O, ZSecAuditLogDirMode 0777
/ l( h& e' y; u; N- USecAuditLogFileMode 0550% ~' U" s' t& _2 v1 I
SecAuditLogStorageDir /var/log/modsecurity
5 v4 a/ o; z5 M) C, E/ CSecAuditLogType Concurrent
& i# L* T; o' a* l参考文章:
7 k1 E$ ^- I* Z# G$ |, @https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
; w* X8 g. a" S" Phttp://drops.wooyun.org/tips/2614 |
|