|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
+ d) b2 t4 |; \' W% B/ s* y& A: E9 d5 p/ p- g
一.准备工作
N' d. B9 V$ a, Y, G5 _9 B% x4 I: Z, p8 ^, ]5 v* \
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
# l8 u( D3 h! {( \* F3 \: @5 l" {; k$ ?6 @/ Y+ H8 X. t- i1 L
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz) p$ y# p+ ~0 b- b
+ R9 L K( v- ]! Umodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz) X: t) x1 h- Q! P3 e, c
0 A8 d/ J4 b& q$ q
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs8 X- \( U( v3 u0 r+ J
+ S( c9 n5 B% W- V. l/ P+ b
依赖关系:3 L; P8 f" }4 u$ S3 A6 Z- x, s
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:* ]0 O; ] v% X! i
4 Y' |: o7 M+ k/ y6 |$ z0 Y: t' Xyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel; L0 u. j2 B7 N3 A
modsecurty依赖的包:pcre httpd-devel libxml2 apr) ~7 e$ i! u/ T( L# A( y, x
. y7 s% J1 _* Z3 v" ~yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
- c1 Y4 N3 w; ?! [4 t0 g, b二.启用standalone模块并编译
) _) g+ }8 C% l% u% t
8 q& S% O+ p* o6 S- W8 H: g1 l' h下载modsecurity for nginx 解压,进入解压后目录执行:
: o, n- C) U& `& U' ^: n
! ] ~5 [ N% R- |$ O( d* ~./autogen.sh
9 J# Q# E0 T# k6 G& O4 C" ]! L./configure --enable-standalone-module --disable-mlogc
, \3 {) e) t5 f$ S* ^1 O1 Vmake
0 l3 Y# A5 ^) l- J' z4 V1 i f三.nginx添加modsecurity模块
! ?6 }3 t {- a; y# p7 w6 }( w; r6 E! d3 |2 k4 F
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
5 c G6 K, ~) t, s8 _- s: E$ o
( |8 @: m* Y" t+ ]1 O./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
5 f) l- z% j7 Z, M. Z4 X* A/ N# Xmake && make install" K+ L7 Q' G6 b
四.添加规则
7 M2 e1 |7 x( h6 [7 D
z7 P5 ^& f6 i: w9 w& D. rmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
0 E1 M! @# ~9 n! Z/ x
+ j0 c9 P9 H9 C# L6 `. G h, g2 r1.下载OWASP规则:
6 `- w( ^+ D7 e; J( j# G
. C3 c5 ~) `; r: j( x1 mgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs# H3 V7 W$ n" _+ O$ ~) q8 j' Q
( J0 { j; V" [7 s1 N/ [7 ]9 a: Pmv owasp-modsecurity-crs /opt/tengine/conf/& T" w5 \6 }& ?, h
8 j* C! V* n/ w6 mcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
4 I& r9 h5 E# M( ~2.启用OWASP规则:9 f! b$ ?6 ]1 s& a, W
+ E) g5 w# m( U
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。0 I- W S& T) m5 e3 k
' ]; |+ K0 [" S# h, w& l! m编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
: ^0 X% H. ?4 B& P" J9 ]2 C1 y$ X- J1 R) Z
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。+ i$ P. w# f G; q, D
' h, E" \, _5 D$ O( K! ]Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
: q8 Z$ W+ m' M, v' sInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf: E2 R2 L2 Z" Y& E
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf" U2 T1 a: ^* t8 u' S
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
& }' L9 p; W0 {" F# F% T0 zInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf4 Q, N; {" h0 B
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
8 H6 Z1 N' } d: z0 |' q, f; F) uInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf5 b4 A$ k$ x- C5 ^5 S& E
五.配置nginx
: Q1 n3 z, v# s5 ~% h8 J& Q8 p* R/ E9 k7 W# g& J
在需要启用modsecurity的主机的location下面加入下面两行即可:4 e, G/ Q$ y: w! S9 h% Y
; O! p1 `$ z% ]! \8 P
ModSecurityEnabled on;
3 b+ O6 j6 f' R" B oModSecurityConfig modsecurity.conf;( m9 o9 ]5 e ?
下面是两个示例配置,php虚拟主机:4 O0 }& _, h1 R) I
5 \! d1 s" E$ ?" P7 [" u
server {' p) X* q3 i8 `9 Z" F; u+ `
listen 80;
3 l5 e u C0 Z- m( C0 J4 ~/ S server_name 52os.net www.52os.net;' C. n) B+ q/ w9 u+ O& K! u
O, X% X8 r& z% k
location ~ \.php$ {
; J: e8 f# q$ _ ModSecurityEnabled on; 7 ~/ o6 w) n3 r$ J
ModSecurityConfig modsecurity.conf;
* |8 E4 F; n" O- j, J
1 D% J. b! `) }; n" k root /web/wordpress;% M. X7 z9 P2 k6 \
index index.php index.html index.htm;
0 J* Q) ]: p& m$ N f/ ?
, k4 f+ A7 ^, ? fastcgi_pass 127.0.0.1:9000;# T. F( O6 n+ P) B
fastcgi_index index.php;. L! I* E7 X2 g0 x
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;- a7 E R7 [$ H& L. n! ?2 a
include fastcgi_params;+ K8 y. [4 q& w8 I1 ]4 Y
}) y$ g0 Z& Z7 ?. _5 V6 `% B, m
}" O! j- O' O; R* z' A, Q8 Z
upstream负载均衡:
$ J) x7 b- V# d* |7 W. G/ ~5 V( Z0 L7 h z3 F
upstream 52os.net {; A( ~3 x+ S& f) A
server 192.168.1.100:8080;
2 m. ?% S4 s) d2 a6 u4 k& N server 192.168.1.101:8080 backup;
! B: D; v! @! m- T* p}
( e( K% q" I) B( S8 V0 S( n f3 a9 j; n/ D
server {' m" {& @0 I# B7 Z
listen 80;
- t& j; c$ z- ]6 \, N0 g7 lserver_name 52os.net www.52os.net;
$ l$ \# c( r% o6 L5 i. r
0 e1 h# _1 X' [" P, glocation / {
) u) M: U q8 A" o0 L- R' Y ModSecurityEnabled on; % q4 m" U& n; Q( c6 t
ModSecurityConfig modsecurity.conf; & I" \) | a' P$ h
, [; M/ o) }. j& L( U
proxy_pass http://online;
( v+ S1 T/ ]1 p, [: _3 q: @ proxy_redirect off;+ ?$ b) {5 F* d9 f$ X
proxy_set_header Host $host;
3 L( N0 K/ x5 W proxy_set_header X-Real-IP $remote_addr;5 c. E" O4 B4 w5 C7 D" i
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;: U: j4 K) T8 }9 H
}
2 d0 C* J& x) F8 x8 h0 w}/ r9 @+ x/ J/ O
六.测试
6 ?6 y$ h+ S z* q$ e/ i- J( `$ L9 t9 ?6 c
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
& @4 U) i! Y9 u1 O! I8 X! D! R: V Z0 U
<?php% {0 d9 |2 y0 A% z
phpinfo(); 8 }6 S: _: O* r% _5 }8 b$ v. E
?>$ Y0 [5 q* ]) q; T$ s8 o/ p
在浏览器中访问:
" o% I" v* [! ]- }/ t7 `1 U% g" z. a4 E8 I m6 J, ]( P5 _
http://www.52os.net/phpinfo.php?id=1 正常显示。
! e2 d$ _; q6 Y9 \. V$ g! L& xhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。& U8 v7 B4 m' C' m
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
, Q" n& \' u l5 N7 ~* ]3 _8 j说明sql注入和xss已经被过滤了& n: p8 ]$ H6 U- `- Z1 w! h" N
& H/ F" h3 }5 R- q/ @4 H七、安装过程中排错8 D2 O0 T8 h1 v( A! P
# w# P4 x" \ Q# K6 G4 T, m4 x1.缺少APXS会报错
6 u+ s0 h$ j& h* D6 R3 k
0 `( D2 _/ k" A0 k1 ~2 jconfigure: looking for Apache module support via DSO through APXS
& C; x+ p1 \; @configure: error: couldn't find APXS
K+ v9 e* c0 Y& _! Mapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
, [% W4 I* j+ x j6 o3 b0 j; x解决方法:* U5 j0 g9 l0 f' r
B, y2 D4 ]8 V. g3 ]' p9 z Byum install httpd-devel
2 C; D1 }# }, v9 n2 G2.没有pcre! G$ k0 Q% r4 w8 Q
Z! T; F7 k |) P! s" H$ T
configure: *** pcre library not found.
7 r3 _4 \7 B& `% nconfigure: error: pcre library is required) e) N/ i4 n, R; w$ |
解决方法:
. H5 N9 K/ M: m0 b
) K6 }: b" C' ~6 W7 h4 K9 q) hyum install pcre pcre-devel
+ Q+ P2 F5 K X0 @. L3.没有libxml2% K. G6 |: e1 O( V& Z3 H4 |
9 N. P* Y0 y* L# p! g: o8 y- M$ E; W1 L6 z" L
configure: *** xml library not found.
9 u8 X0 P: Z. m9 P* Tconfigure: error: libxml2 is required& Z- I: S, |5 _5 U0 }. K
解决方法:
1 f, q0 L- V: I2 _
) u( |, m0 ?4 p% jyum install libxml2 libxml2-devel
f$ ]- `9 j/ c9 a: U$ k7 X4.执行 /opt/tengine/sbin/nginx -m 时有警告7 d6 {/ z+ _3 Y- l" O: u
; [5 o# @) K7 ~: r. T3 t4 J
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
% w* |: ^8 V) Pnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
! g- E+ a$ l. W" W原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
! {0 J: W3 x$ X5 G3 b" t1 y' @. F4 e* E. z' ~
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
* A, X) F0 n% e/ ~4 d0 j2 q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
8 w! y% C$ |: z3 B8 K# o$ |2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
7 V) N2 R; J3 s1 B) {5 t: X( V5 ^2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
2 u, D' a# O0 ^6 r) I0 T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
5 Z. }3 z6 h% d5 f7 z6 p2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
' G1 ]3 d. N* X6 B# ^/ Z7 C$ ~解决方法,移除低版本的APR (1.3.9)+ B8 Y1 [8 [2 |
7 m& ]+ N! m/ ?+ S
yum remove apr# U$ `: t/ D* p6 M1 D. L; O
5.Error.log中有: Audit log: Failed to lock global mutex
* p, d; @" z/ T) [7 |1 c; z" R6 ]3 {; n _' Z
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
P2 e4 u* Z D- K5 lglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
: Q- I9 V1 d& A1 I" t3 g0 ?# l, Q解决方法:- ~( [4 ^0 L f/ G
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
q! I9 r3 y4 z! i! Y7 K; W% L8 a2 y& A% \
SecAuditLogDirMode 0777
9 e! o* o# G/ V# kSecAuditLogFileMode 0550
" P. N+ |6 ?% G& y2 W; Y' D) a4 _( ESecAuditLogStorageDir /var/log/modsecurity
5 }3 u3 t% T4 B7 h0 J) oSecAuditLogType Concurrent' ^6 D8 e- B, ?/ \1 e9 z3 k& x
参考文章:
; M# c5 k8 ]$ B" Shttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX" m% c- N6 \# R) s* n
http://drops.wooyun.org/tips/2614 |
|