|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
+ v' y- Z. t# E; P/ P# k. q! e8 J+ A3 t( E1 R* F5 m8 V0 T7 w
一.准备工作
4 B' X4 J- m- [* |# u6 g7 [0 {' i' |# X& f, @5 ^% A" O6 m
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.09 h1 n: \/ [- V/ w+ B: B' ]% ^* ^' j
+ Z; E1 ~) f& F' S- ~. A
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
* N2 q8 n+ b9 `5 ~/ X: t% Z8 D2 l
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
: @, }1 J4 Y3 Z- S% Q( Y2 Y
$ M# D1 a1 Q5 \4 V) ]& Y/ yOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
" J. F& e( | z" a! J" I) Q
( A+ g# ?; \' l$ V$ @5 H依赖关系:
) {( l+ t! k% c1 Rtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
; |" P& J% D3 C V/ u" T
6 ^! R" T" N( C" C) oyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
) ?. W1 s9 ?2 ^4 dmodsecurty依赖的包:pcre httpd-devel libxml2 apr3 }+ ~7 Y4 @/ q p2 M4 B4 H# q
' \. A, D" k- n4 z' `' ?) _
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel% |5 \0 b$ X6 G; U5 o6 u
二.启用standalone模块并编译$ |$ C% ]' q& i+ ?# c' T$ q; Z
+ |" }+ p( h y# J- C
下载modsecurity for nginx 解压,进入解压后目录执行:4 P! l8 O9 j2 M3 O
/ c. W4 @2 s* m' x5 i0 G! S( c./autogen.sh
$ U; ~$ h. r& a* ]: t( Z./configure --enable-standalone-module --disable-mlogc- O$ o6 }8 A6 U; n
make
# a1 D9 o; C- a/ D1 @: r; _三.nginx添加modsecurity模块
5 T7 l3 M# ~4 V5 \% G2 j4 y. _4 ]( g: l7 f
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
. I: S9 q+ }9 U0 B0 E
* \0 q1 R( \9 G. |1 L0 o./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
9 j" }1 ? ?3 N! Y. t9 n* f. ^make && make install# D4 e! _9 f+ r7 @, S8 K
四.添加规则+ a0 j1 y h- e6 p( r* q! n% @$ `
+ e4 {, ?( a8 z X* ~+ }
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。" } S$ \5 g3 w. R: [7 s( ~
% W3 c4 o# g# A) M- M1.下载OWASP规则:
- c3 I2 U/ _4 b! V) i8 n) {" _. D* U" E8 A$ J7 ?
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs% F6 f8 _; X7 X$ a1 p
* I# M) o* U- R0 H( S
mv owasp-modsecurity-crs /opt/tengine/conf/
$ ?5 s/ ?, e0 W0 H
4 _9 l" A) u7 k. Dcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
' g* C; t* B. V. J3 E2.启用OWASP规则:, X+ j& [3 k5 L, b: y5 F( S6 W
* N. i$ _5 L6 N5 f2 z
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。* C2 N7 }' D( H. x+ r/ G! D
, E- X) @; t' g1 S1 j
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
; D" C* ^& F& I! ^. X/ L/ _* W) J9 z; V+ s
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。* } r' i9 R& _2 |$ I
( J+ E' B9 O* ~
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
! |9 q j& Y/ Y# GInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
* a0 G2 {6 p* I4 G2 g( P4 h- s6 PInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf* S4 r1 ?5 k5 U/ B/ O& M
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
/ ]" ~5 f0 K+ E7 p7 _( v* ^) D; G9 lInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
+ p X/ k0 T8 Y' p! N9 C4 V; ^. vInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf8 A) U# h' Q4 F& f3 k% l3 C$ P
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
9 X. Y" e6 c% q% s, H7 o V五.配置nginx7 C) o7 A* M' v+ B
0 E$ o% ?8 {; h4 m8 } Q# [在需要启用modsecurity的主机的location下面加入下面两行即可:, \! u( K, N* v* S
3 q- e% i8 R" v$ \' N2 W5 sModSecurityEnabled on; ; a4 e2 U) g% ?$ q
ModSecurityConfig modsecurity.conf;
/ d5 |! k+ C5 h3 Z5 R, x下面是两个示例配置,php虚拟主机:
* z0 Q/ q: O. g4 n- v8 N* j8 H7 X
$ }" [. m. w! \5 K# X" R; wserver {4 A: L- L& O- Y' q3 P( \4 J
listen 80;
* {/ t8 `0 n9 O, T; |0 G4 r! \9 \ server_name 52os.net www.52os.net;7 ~; {4 i5 r* r- p% q8 q
3 d& I% Q @. h6 r: Y4 M
location ~ \.php$ {
6 M! w+ i0 C- q" p( \+ C% @* L ModSecurityEnabled on; : f2 O' s' m) y3 H ~& O
ModSecurityConfig modsecurity.conf;
4 ~1 a2 N3 F9 W' U
8 A* t& {" F# J6 \ root /web/wordpress;! S, h B* i/ N
index index.php index.html index.htm;& Z0 ]! i% Q E2 J x
( d% b8 ?1 Z4 Y; d( g. K# T* k, m& o
fastcgi_pass 127.0.0.1:9000;3 C9 Q& H6 a& @( D6 J
fastcgi_index index.php;
# w' N+ H0 O o fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
$ f2 b" L' W" h$ t z' g. J; Z include fastcgi_params;
0 q9 G, ^+ z5 J) v6 H }2 U5 l3 M! _2 i9 F( I8 t
}9 z, T$ U$ I1 k8 y( O4 S
upstream负载均衡:; Q/ [( O0 i. m5 F: g
& S8 Z. p& L9 T$ r3 A0 fupstream 52os.net {
- W3 C. ?0 d4 o% h' f8 {7 y! R server 192.168.1.100:8080;
. @6 F+ T; l1 _+ @4 r, r! l server 192.168.1.101:8080 backup; m r- C% v6 @) X2 l
}
2 Y8 _; s8 P4 e2 ?' J @9 A
; C- }1 u3 S0 O( s2 F- xserver {
5 G: T" k0 j: H; B Blisten 80;
) T6 \1 u! _" ]6 ^1 fserver_name 52os.net www.52os.net;2 E6 A( z/ A7 n9 Q* ]4 d! r8 h
+ {9 [4 n& r' J& x8 m+ s" v, \
location / {
( S5 q9 q' I, x- { ModSecurityEnabled on;
7 F7 i& E- @# J/ n ModSecurityConfig modsecurity.conf; 7 f8 w6 C! D0 @1 J: \
; }- ~+ b \9 I L, t proxy_pass http://online;
& U7 M* A3 T! c+ ~, N# O( { m proxy_redirect off;; t/ A, G U2 N3 V
proxy_set_header Host $host; n3 ^: P" `* t5 d! V) S. j" r
proxy_set_header X-Real-IP $remote_addr;
) A; ^ b' ]" y5 J4 v b" Q proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;- \) M% P! |% j7 ?* g, Y
}
. D0 n8 b$ Q- p& k% T}
# | a' o8 f! U$ V% \5 Y六.测试: m, n3 ^ B, I5 {6 B2 w/ V. R
% r2 u5 Y& x7 O8 L+ B* ~我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:/ r. O3 l# Q- G& ?- N# B5 v3 z) f
+ ?9 Y' x! C! r+ z( t2 X
<?php
* L- y: G4 v4 _" @2 X( O7 O phpinfo();
5 j; x0 Y1 R4 E( q' P# C: O?>, U4 l5 I. a6 S+ C! O# |/ v- o
在浏览器中访问:
7 ]. _6 Q7 ?& |- k
. U4 D, G0 `7 a- v2 Q; x; fhttp://www.52os.net/phpinfo.php?id=1 正常显示。/ m7 ` ? k% f* K6 _" j& R1 k2 V
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。2 U3 [, e% k* ?0 ~
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。) d: K. _! y/ H: E6 W
说明sql注入和xss已经被过滤了) h U V7 w% e8 B7 {
" d3 S8 o" b4 E8 L
七、安装过程中排错
9 }7 z& T( B( U! J& ~- j% x- m
6 P7 i' l0 U2 {) p; n1 o1.缺少APXS会报错
* u, D! @" b- E. `. d; G/ I4 b1 o0 T3 Q+ l7 ]$ E% {
configure: looking for Apache module support via DSO through APXS }. ]1 z# K1 B2 G
configure: error: couldn't find APXS
3 u, X" @# D+ u) B7 M N+ _apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。2 _0 V& U6 K" T$ M+ X) \1 U' |( g
解决方法:
. M+ ]# m# _' P( i) L% x5 a5 X2 e. Q0 i
yum install httpd-devel
0 g4 O8 F2 h) T% l2.没有pcre
. B+ s8 U; ~! b! Q9 h8 @1 Z% |( i7 k/ I' g
configure: *** pcre library not found.
& P- M4 e2 t6 v6 }' B! D- n+ Uconfigure: error: pcre library is required* e2 @* F# r, C, b' o
解决方法:4 ?7 T' m0 C3 n
; M' y9 F, N+ S4 U: K
yum install pcre pcre-devel
. }& u+ D3 |4 C I3.没有libxml2
$ j1 l# ^& u, f' Z1 W
( ?$ O' S5 R% r4 h4 F% V7 A7 M4 T$ n
9 |- D% K7 E1 ~! Xconfigure: *** xml library not found.1 J5 l7 f7 u Z, Z( J
configure: error: libxml2 is required
& X2 I8 I- S# s9 m3 S; v解决方法:
9 P/ ` z; q) w+ ^% H; |8 _
$ J, D1 P! H! B: @, ^yum install libxml2 libxml2-devel) F: f4 U, F6 \' X" Q* Y; q; r
4.执行 /opt/tengine/sbin/nginx -m 时有警告
: W' e: l3 _: x
3 \3 z5 J5 {% n) Q$ m/ E! k. nTengine version: Tengine/2.1.0 (nginx/1.6.2)$ r3 F& ~& [4 a) ~4 w
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
1 R$ z2 s2 C' U2 n: G$ k原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
9 ^" A1 |% N p a5 a& ]! o. g; { g8 C
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.& c3 i* s _9 O& l) e
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
. r- A( {% \, b. r, f: E: Y2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!- }: Y6 o' U1 [3 p0 L8 a8 F6 }4 j+ w
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"* D6 B& |/ k9 K6 }4 U
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
0 `8 u0 ?3 \8 Q' \6 o9 n0 e2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.3 ]5 Q4 `( t0 j$ T" M
解决方法,移除低版本的APR (1.3.9)
: p% B" I" x! G9 A& d2 h7 J9 V0 [/ v& T$ |# y
yum remove apr+ [# t# v! X& V" T
5.Error.log中有: Audit log: Failed to lock global mutex$ }0 g4 a7 t% U8 b( D& C/ w
' u$ ]+ D0 q9 B' i. S2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock & f* b6 K0 K9 u, d3 {
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]) ?# M* m4 S, \
解决方法:
( k' {; ~& q: Y$ S! @1 L编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:8 h6 f7 T. W5 i9 Z1 k
2 }! Q9 y" F9 a9 T4 J$ vSecAuditLogDirMode 0777
# P7 P2 ?) r. O8 m1 ^- T* BSecAuditLogFileMode 05508 M: s6 W+ R h ]% I0 Y5 T
SecAuditLogStorageDir /var/log/modsecurity
/ d1 r3 M: a( W9 K& t0 O- nSecAuditLogType Concurrent
9 u# f$ ?" m0 @' H& f. O参考文章:
2 a- X5 i1 c7 s& v6 whttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX6 T) e& I) [0 b0 r8 F* j( n
http://drops.wooyun.org/tips/2614 |
|