|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
8 M, B% u" A; Z4 r( n
# _" P$ a' i9 K5 {- _. X2 d一.准备工作/ N' x* V4 g% O
8 {5 Z' g% j& T
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
* n+ T8 Y3 D4 n3 N1 h8 ]2 ]; r
+ c5 d/ w! q' e, Itengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
/ D- D1 Z4 a- Z# X0 q% s9 u5 ?
$ Z8 W7 @1 o, |" J# {2 Emodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
; Q, _$ a/ j1 T' @) ^, S
+ ~( f Q, M9 z/ N/ T! E" k0 v* rOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
/ S& l; C3 Z) v; [# R% |) b. w$ f* X4 }& V, ~$ U/ `
依赖关系:
$ y3 p4 o- N7 @+ z7 \: D; {) V# mtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
& V2 S+ X9 L6 O
* w' s# `& u" G1 Iyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel+ z4 Z0 ?% [: z
modsecurty依赖的包:pcre httpd-devel libxml2 apr9 w4 c) s( X o7 M+ l$ e* x
5 m, P& i; Z! |: Y2 X
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
. G: E! D2 p: u! `二.启用standalone模块并编译
4 ^- A; K$ a0 o, G/ r& w$ m. ^; ]8 W3 C6 u( q! M5 B, t$ m( M
下载modsecurity for nginx 解压,进入解压后目录执行:3 a0 ?; c5 }- m F) M6 Z
- j: ?+ b3 A# ^5 j# H./autogen.sh
/ Z# x: i4 V# I4 ~, k& Y' C$ e./configure --enable-standalone-module --disable-mlogc
' b4 R. O- }5 h8 y) Y7 B( T# dmake
: d+ k. [ f7 c6 l三.nginx添加modsecurity模块9 C5 c9 Q) N4 v+ `! q- a7 S
- J/ {) Z' ~* }
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
! t: W2 Q4 x. g
) h7 k3 @2 v" p, ?8 S./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine2 @7 @) r& c7 Z! X8 c! A
make && make install
$ O' V- I/ Z" r: x四.添加规则. X8 B% Q4 S, N/ G
8 ?+ {0 | U- wmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。4 C% ~3 g9 k, O* b& v9 C/ t' |9 ?
; f8 S' v0 a8 }0 l& v7 k$ k* U
1.下载OWASP规则:
b2 T) m( T3 X" q! r j/ v( P9 A* S6 s
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs+ \8 i# M; O% J/ q
* b+ d) }3 h* V& G# k7 f: r
mv owasp-modsecurity-crs /opt/tengine/conf/' s5 o* `3 K: x, F
0 B' j! [3 B4 F' ?* ?( L
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
6 C- X) B& b% j2 S$ n( p0 E2.启用OWASP规则:
( i! w9 ^1 t; r# c. P: E
" j5 X8 l' z* z7 O7 @ \2 i' s复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。, n& z: _" \) {) T
* l+ ?3 N! u" o& T( Z* A3 O$ X编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
! L# w: t8 g) U9 }, N( V7 Y: E& o
4 y, _2 @+ Q* Z0 ^( ?- `& L$ Sowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。, v* V- W0 w6 p; Z- T" L; I
3 | b# G5 }; C: I5 oInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf3 K' r4 Y. {$ Z7 h5 o( l3 K( x
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
3 A/ r' H& i+ ?7 A" KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf5 A6 r/ Z$ G# j
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
3 g) q9 i0 ^# R0 J MInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
$ _( h: C4 S1 _5 G/ e" t9 F/ c, qInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf8 c# E4 J5 C7 X' Y
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
4 K, u) D( `: E" @五.配置nginx& L$ N% ~ z! y, p- I
- [. j" g9 a* X& d在需要启用modsecurity的主机的location下面加入下面两行即可:
/ P9 f4 t5 o- N# P; ^3 B3 L+ U3 t
( Z9 w- X; m6 e3 a) y* S6 ~* I EModSecurityEnabled on;
6 W6 f. J8 r; x( l! D: yModSecurityConfig modsecurity.conf;
5 i; a8 y; @% V* k9 A+ C. H下面是两个示例配置,php虚拟主机:. b p' B! }- e4 V
) E" L3 Z$ X+ E% u6 B( Tserver {2 s- e3 V' [. J: Y7 H* x
listen 80;+ K( A' Q" k7 ~; e, M
server_name 52os.net www.52os.net;' T' E, q5 i/ }+ \& ~( B( F% P
; P8 K5 |) o/ |$ V
location ~ \.php$ {
! O) q; p/ S' p ModSecurityEnabled on;
! |% T4 ^+ |7 Q7 a6 S, H1 J% Z, J; I ModSecurityConfig modsecurity.conf; F6 U) {9 ~$ I& N- Z5 M9 i
% a- q* H: N3 |+ ?+ j2 O9 |+ A* \
root /web/wordpress;5 H D& o d7 C" I8 u
index index.php index.html index.htm;
2 C! x4 Z; ^$ q3 m7 k 0 O$ j5 `3 i( [
fastcgi_pass 127.0.0.1:9000;
- N% k' y; b" V d, m fastcgi_index index.php;5 z! s- Z0 {3 j3 |( O/ P
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;( e$ n$ N) ^2 y, j. ]4 z9 V
include fastcgi_params;/ {3 O/ n. T7 n6 E) E8 X1 [0 g
}
0 B0 s; F1 I* S) l }0 B# B4 D% ~1 t0 R; o2 h
upstream负载均衡:4 H+ b. j" i) w
: V" T2 L9 h/ d4 ^upstream 52os.net {
6 O) _+ h( h v' M6 |% Y+ M/ w8 q' o+ r server 192.168.1.100:8080;
3 o* ?; G6 Y& A0 V+ K0 b* a1 d* m server 192.168.1.101:8080 backup;
( H' f0 N w) r q. A1 t}- j% e% O8 M8 z, {% |0 M b
1 a& q& D- B# H o' O4 a! p. ]
server {
4 q. \- ^3 V+ b! Alisten 80;
2 t! K+ p/ c0 t: Qserver_name 52os.net www.52os.net;8 s$ ^, e% q: X4 i2 J+ s5 F
' A( d; O7 p c" l9 _+ ulocation / {% Z# q% [- R3 X4 T3 G- w
ModSecurityEnabled on; # ?, _) t9 v) q6 w* z% ] e% e
ModSecurityConfig modsecurity.conf;
Y$ x! s, m/ e6 C4 T& Z% c8 L5 d4 K+ R8 d }
proxy_pass http://online;( w3 w V, H" `9 r! ^
proxy_redirect off;5 V0 `$ z( J$ N& D6 }" }" O
proxy_set_header Host $host;9 p# S+ J6 ^1 G% X' p/ ^; [& {
proxy_set_header X-Real-IP $remote_addr;
# C4 K5 w# f5 W proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;7 m' ~0 P$ V' Q, e* Y
}- D3 h3 C1 w( A$ O+ w
}) [8 b0 E: `! ^
六.测试8 r1 K0 w. d) o1 Q6 P
, Z' x6 ?% f1 _. b* F
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:3 u& Y. O, w% h- }/ t8 f5 H
/ Q0 i6 J! p- m2 H2 j$ x. t
<?php
! i# m5 u, d6 {9 f phpinfo(); $ ^; w9 \# J! H& a, ^% }2 e
?>4 J9 K& x4 p! o6 Q5 R
在浏览器中访问:
1 \6 U; j: J9 a/ J' R
5 D6 o' n- D0 p6 J. Phttp://www.52os.net/phpinfo.php?id=1 正常显示。
, _0 l a; P0 T5 `- o- nhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。" [ \( s1 r4 e- X( R3 x
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。7 h2 e* Z5 E) T0 T& R$ S: u
说明sql注入和xss已经被过滤了- w1 o: M. K& {8 z8 f
$ I1 J% E$ s4 I3 H3 Z( @, O
七、安装过程中排错
: N$ @$ X# |2 a4 L9 q- U" p& A8 ?8 S! _6 U9 j. f5 x( n
1.缺少APXS会报错
# D0 _, Z0 V* Y" V& Y
) g% B# s! \) c. cconfigure: looking for Apache module support via DSO through APXS9 s+ f6 s$ l4 a1 V. W
configure: error: couldn't find APXS
" `8 r) J& o, m: ]1 [$ [apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。3 m7 E/ i8 I0 i. c
解决方法:, I4 D2 Q- {& I) C
& L$ h) G9 m2 U1 m! | [1 }yum install httpd-devel
, F5 F+ T U" n; X* P9 H. }: \ N6 I2.没有pcre& t* W- Q! o) h1 g6 Y! u$ @
8 |1 z$ b/ _& c" t+ S8 @2 }
configure: *** pcre library not found.
5 C7 k0 O1 `9 ]# }1 t3 F% e( G; K: l. Pconfigure: error: pcre library is required1 R* ]4 \ S* R8 W: c
解决方法:3 `, M0 G+ x0 B! i
$ x3 ~( [# O! P8 N
yum install pcre pcre-devel
. Y$ y- j- e; E/ P9 \3.没有libxml2/ i$ N( e3 e+ Y
; x3 T# F; B8 E& n' E. v! Q$ A
4 N4 H y# M0 Z% ^3 Nconfigure: *** xml library not found.' f) ~. p3 C1 }: _1 H
configure: error: libxml2 is required
. _8 H0 u2 h/ @解决方法:: E0 }; P/ w& ]" a1 [
$ ?4 I; v7 P6 H6 R% u( M7 lyum install libxml2 libxml2-devel
# ^5 O+ A( n3 o& f( }4.执行 /opt/tengine/sbin/nginx -m 时有警告/ o$ `' y* Z! f$ j+ ]& T( D6 ^
, N6 F5 q3 t. z0 w5 C4 N9 f4 y- dTengine version: Tengine/2.1.0 (nginx/1.6.2)2 u* V( u$ R: w6 X$ t. |
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
3 N Q8 e4 ~" O8 u: {! g1 Y0 ?% \原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
, g3 Z- ?3 e9 X( ]) M$ M7 J4 T, _* n' x4 ^' r5 H0 x; D- ]
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
" X) e' m3 A+ _' j: N2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
( X- x7 q3 @; V- u. H3 @2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!1 h# l! L$ A! M0 Q8 C! Z; R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
, O( c. F- Y9 N! y }( R2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6" t) D: I5 r3 n) d& p3 |
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.2 L% ]4 I+ ^: r; F
解决方法,移除低版本的APR (1.3.9)( x: x# _2 ]4 J3 N/ P4 ]1 J
$ U2 @7 g- A+ i( k4 \5 \yum remove apr
+ v/ u0 L/ K1 u# a. B5.Error.log中有: Audit log: Failed to lock global mutex
) p4 E. e- N# q i, Y& L1 ]& M7 r# T e
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
# G: {& m# Y! I0 O% xglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]4 e- z: R6 @3 P$ b1 {8 ]& e3 H
解决方法:
; U+ n+ {, k- `6 z2 e编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:: V1 Q: V8 d5 y: w/ x. M+ K
) w4 {+ Q3 f! F% e$ c. f7 W6 S
SecAuditLogDirMode 0777. u% G4 o7 v& G! c! }& ^
SecAuditLogFileMode 05503 S+ a4 H& V, u3 F# r
SecAuditLogStorageDir /var/log/modsecurity
8 U- N7 Q9 r n' {% |" T/ H, SSecAuditLogType Concurrent2 X& s! }5 v1 W* Y" q' E1 ]1 \; |
参考文章:
; v/ ~7 L3 |2 U; A& |5 s1 {https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX' B9 e" a# w1 w A; U5 I8 p
http://drops.wooyun.org/tips/2614 |
|