|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
) w6 A% u9 P! e; B* h3 g3 Y$ y e$ _ r
一.准备工作
: y9 e9 x8 [" t E5 t' m0 S+ T4 p3 @! ~; Y) s0 v. b; G+ x
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
) g0 F$ N; J) \1 B# W+ h9 y0 b; H+ t, U2 H) Y1 C1 M0 z9 u; s
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz. f' S7 V7 V" Q; I) K9 }7 _9 Z
( D, x) O2 r3 D/ ?" E( f7 a- u% u
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz ~5 _9 A6 g" j, \
5 r, `- p% S( j6 M7 H4 H; yOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
+ w9 u, }; T& x O2 w! t- t8 b
- u. D: x. s9 k( ~- A3 }7 u依赖关系:
: d, C6 ]5 I. }6 u6 Xtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
0 X, E5 M6 e9 E1 Z4 F" }7 v8 x) p( `: W6 ~9 u6 A" k" K
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel& Y) }, R2 z3 U1 q4 w. S3 [ _4 k
modsecurty依赖的包:pcre httpd-devel libxml2 apr
) I( g+ ^; E _0 ]! U/ A
3 h( e# A% s) w+ |# Hyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
# {" z2 s& @" b5 t6 i6 x" A0 l; e* t, V二.启用standalone模块并编译
* ~& t5 ` p3 U' p; R
& n5 }; \ M( X下载modsecurity for nginx 解压,进入解压后目录执行:
3 Q: j) F) y% r
* y* o* x: D+ Z- E5 T A) P./autogen.sh8 Y# {% ]( Q3 q- I
./configure --enable-standalone-module --disable-mlogc
( V& b% L/ n1 P' l* y: {- q8 @make
" O" _# ?) z9 P三.nginx添加modsecurity模块
% ?0 f/ a" q& V2 y
6 ?1 g* ?' M7 \0 p7 z( r) o在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:& H+ l) E# [- S( z8 M$ E4 t
! p* z. `% q/ ]8 g7 L2 t5 @$ F" e
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine" M( {) f1 v7 ?$ o
make && make install
" P' I/ }$ ]0 u( |) Q四.添加规则- p1 k- w8 f* I
' G% ^" P( V$ Y/ [, Tmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
4 t7 }7 @# y& O0 b! C+ S: b
5 \/ G0 m7 a- O- S$ C! _5 y5 M' B1.下载OWASP规则:
+ `1 S+ A* {* E# \. e. G+ r
& c1 J; Z; p, a0 Q/ L2 I8 O0 |; q6 Pgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
3 x: ~" o& Z8 Y; ^9 _& K* V9 i% F* K$ Q4 I' Q+ j% E+ Y) h
mv owasp-modsecurity-crs /opt/tengine/conf// E D- h9 h: c$ B; W; A# L9 q2 N
& z! [- E( P6 `* x' v% T% M7 {
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
/ C5 P$ l) K# O2 |' a8 h0 i2.启用OWASP规则:3 t) E* [+ k! ` [
) _- l) p8 p: W
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
1 y {! t: P7 C% I
" h- o9 Z% P: }8 G& Q编辑modsecurity.conf 文件,将SecRuleEngine设置为 on2 V" n7 w6 j; `+ o5 }
7 n2 ]( F/ ]7 |) _
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
: H$ ]% H% }( ], I7 U) A# ?2 }7 c( Y& E% f+ J( O3 P& Q% U
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf4 ?: \% ?: [5 {2 e; \/ H
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
p) a8 R, q1 |Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf5 T2 Q' ?3 A. {( a, Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf, N" W0 \! r8 A X# |# g" c/ {
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf) ]/ d T: }* a( f! ~: J
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
# A$ z2 u, b+ h9 h8 C$ U+ YInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf d4 T7 C3 ~, f% G& A. ]. Y
五.配置nginx) I) y8 Q, X9 D4 H6 T; r% t
, j. r' ]9 [* j" ~/ v9 T0 h' O4 c
在需要启用modsecurity的主机的location下面加入下面两行即可:
8 ?6 _: |- h0 I7 A: B0 b1 n5 q5 M( l* W$ M+ ]
ModSecurityEnabled on;
' _5 F$ u% W- FModSecurityConfig modsecurity.conf;
3 w8 N6 I9 N8 a! J下面是两个示例配置,php虚拟主机:1 V4 k$ i( s- q1 f. _' I0 r
* \5 P: z# X% \6 A5 y; H
server {2 F. {; I- o9 }" U$ H$ z
listen 80;
) N& W: g& K0 M0 M: N/ d server_name 52os.net www.52os.net;8 H' J$ O8 o9 |% R
6 o$ h/ v# Q7 J( U2 R/ y" J location ~ \.php$ {3 g9 S- L, N3 X2 J% w' h# d" j
ModSecurityEnabled on;
+ W) V( L- R: k* s' Y4 M ModSecurityConfig modsecurity.conf;
4 j2 R& D6 L7 a
; h! l$ S. g! f8 I root /web/wordpress;
2 F0 j2 k* r0 ^4 e! u( F. r5 ] index index.php index.html index.htm;
' y0 X9 L; N" e' ^) K6 j
' ?9 ?0 V6 [; L fastcgi_pass 127.0.0.1:9000;: D. K' J7 W3 v5 D6 a/ N" R' c- G, r
fastcgi_index index.php;7 q8 X, W+ ^; A# I
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
& p8 N2 C5 V% L& R9 _+ L% d# u include fastcgi_params;+ |2 t/ d1 o" S# i5 b/ e
}( c8 |+ i/ O) l3 K7 b- U
}/ X, A' k5 ]! Y+ k6 l3 [
upstream负载均衡:+ l, @/ X& r. U4 d
% v) _1 D% G* i6 d2 u1 B! {upstream 52os.net {
7 ? t9 _$ U" k7 r server 192.168.1.100:8080;
" t' X+ g$ A a. @3 `( }+ |+ @ server 192.168.1.101:8080 backup;
# s- u- A4 O) z6 q" c/ C B& I: F) u}
" K* ]! _# X" i8 [% w* v8 G& K( o3 k- l5 ~
server {
T S4 R& q! V& |) ~listen 80;
' t) }9 Q1 u. _/ |9 Xserver_name 52os.net www.52os.net;
5 X: h& Y4 e: |' S; X$ y: y
/ Y% @8 Y$ m) r5 ?2 j% t1 Hlocation / {/ O) G, B/ ^9 o
ModSecurityEnabled on; % x2 G- w1 F D6 D. a
ModSecurityConfig modsecurity.conf;
% M' N$ P6 f3 J8 ^/ W3 y9 o4 G# B% A# ]% l, G
proxy_pass http://online;* Y, H- o4 {: Y
proxy_redirect off;
6 Y! L9 V0 k [1 e& ?8 t proxy_set_header Host $host;! G& N, h. H: f! Q+ a' c3 z) _7 l
proxy_set_header X-Real-IP $remote_addr;
& g; y6 P- H3 ^& u0 I5 G' b proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# O* U; @; \3 z( X }
- o$ _ K: ?# N ]4 `1 l+ e# ~/ E}1 _* `* I0 G& T3 V( {' n' Y P U
六.测试$ p/ t& A7 E. n- \3 S
5 F9 _: P7 X$ Y我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
7 w N# }. d! M& U {
6 n) o+ a+ `# m- J' T) P) z<?php
/ `; Z/ s i2 O% N0 X phpinfo(); 8 d- k/ t+ o! C4 A; E7 \4 v7 s2 M* u! b
?>
2 u: M8 E, Y$ Z2 N( y在浏览器中访问:, U T# w( H8 P, w
( V: f# S& @" ]" F8 B. \) A
http://www.52os.net/phpinfo.php?id=1 正常显示。/ U8 R# z5 f% q: U. D1 Y! ]; ]# i1 Z7 e
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。+ \6 O- l3 [9 z& ?
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
! `" r1 W0 w* A! Y说明sql注入和xss已经被过滤了
! P, K4 d) |* c# y& p4 J" Q" t: x: {1 a9 [8 K5 H: U
七、安装过程中排错
6 _5 [+ D7 u' E
; e5 K$ ` G2 Q6 j0 z5 }/ {+ _8 D1.缺少APXS会报错5 b N* N% v/ A
$ g. X9 j: T( n! Y6 N5 J9 ]
configure: looking for Apache module support via DSO through APXS
2 j9 L9 K6 i6 i0 Q+ O) n! M. |configure: error: couldn't find APXS+ N0 m; r1 A! ~* k5 E- K8 c! i
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。$ I) ^$ u/ \( I' B3 n- `, T' q& s |
解决方法:
0 B, ~! a; L5 ~7 b. v* k5 Q8 J5 Z' [' Y; z
yum install httpd-devel7 c: E# f, W0 O" w: W5 U7 L
2.没有pcre
4 k `3 }# f' P* p: [2 h
% Y: i5 g; u' {7 Econfigure: *** pcre library not found.9 i% e' l1 y8 z V) ?9 k! n2 t
configure: error: pcre library is required
' n2 f7 E+ W- X4 a# T$ z解决方法:& d) a) I0 x( ]2 \ O# ?. G
* [( a7 c+ W1 c9 ~1 q6 J6 m
yum install pcre pcre-devel
+ q9 g; ^& |5 Y) [% p+ f3.没有libxml2
9 d( m% b3 R+ U; u2 S/ n0 {6 M4 v- R* @: @# M# V$ U; b% f4 ]
( q/ d L0 q2 y) m0 ]
configure: *** xml library not found.
- ]6 s5 [$ Z3 K$ W+ X- Kconfigure: error: libxml2 is required( R: y9 Z; [. d I( {0 {6 L, }
解决方法:
& o: z% C+ C3 o" ~$ y
1 S# V) e6 ]) z6 D/ Iyum install libxml2 libxml2-devel
2 h q4 J9 D4 a+ s* E2 S% u4.执行 /opt/tengine/sbin/nginx -m 时有警告
. s, N- l* v3 o$ a4 Y* X: u0 T; x+ f% u% g+ a
Tengine version: Tengine/2.1.0 (nginx/1.6.2)( J. [) h- F' f, o1 F% j+ V
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
" b% X# Z% F1 x W9 l9 G原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
8 |. [6 ~/ E7 Q% ~2 G+ d% f4 p9 p3 c. x6 d! N- @
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured." D' L+ S+ [& ~1 l( J+ x% _
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
4 R+ l: l7 k1 }1 F2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
- }! ~' L [% g/ p3 o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"* F. Y* M/ ?8 @& A& ^8 A1 a8 p
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6". ]! K. o+ g- C/ ]
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
- V/ ~6 B9 y8 s2 V# k解决方法,移除低版本的APR (1.3.9)1 p; s0 X: Z1 g
. S0 ^. N# | @# \ b% byum remove apr" o5 q+ x8 h7 S
5.Error.log中有: Audit log: Failed to lock global mutex
' E3 Q- P# @# D* ~3 x
& |% Y; N% o4 w: T% ?0 [9 D2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
/ L" E1 ~( }4 t7 Y8 Jglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
8 s5 z6 y) |! [' A6 C解决方法:0 q" U5 h! [5 ? q5 f6 c5 F
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
3 R Y, @8 k6 v6 O+ a
! O5 L% f% P) z2 `9 mSecAuditLogDirMode 07772 T9 s6 Q# `( g" \
SecAuditLogFileMode 0550! S7 T z3 p+ b% C# e9 a
SecAuditLogStorageDir /var/log/modsecurity, n# ]: p) U2 [! k8 `
SecAuditLogType Concurrent
. Z. l7 O9 P8 h参考文章:
& T) D4 B+ q/ ]' R) v0 s9 Ehttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
9 K$ y5 g# I+ C3 G) q* T9 d- U4 {http://drops.wooyun.org/tips/2614 |
|