|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
4 {( d4 S' t& d1 k! k" r
7 O* y* J4 i+ f7 a& J# h一.准备工作/ T" e& E! W# p$ X/ |, S+ D
" Z8 m0 v6 q0 s t- d1 u6 v. G系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0! }9 M2 y$ q7 K- p" ~/ Z. a
R; X5 |6 {5 Ftengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
7 T* z: w5 P8 u& t( H7 L$ h u" @1 g3 `* Y; }) b6 i8 d! I
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz8 a- Z0 u+ W" [! u1 n% |
5 Y3 \. D0 y; z% q% }
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
% R9 n) V( ?) K0 b" ]. Y0 W/ {% i: `
依赖关系:
. N* n4 H! H, i5 F3 _( s6 J0 Ntengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
# E1 D. x j; r
w. j+ [' `, K* I& I# pyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel, d0 V* @- d: B
modsecurty依赖的包:pcre httpd-devel libxml2 apr% X: n6 U! N% w
^' T' z2 e! X% O7 l& F
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel2 l, f/ v+ z! [9 b
二.启用standalone模块并编译
- Y6 L9 B, }. ^* A9 G" d7 ]& g
7 \( K, f; a4 X. P5 g1 P7 G& m下载modsecurity for nginx 解压,进入解压后目录执行:) Z9 v/ F2 K; C
4 m7 I$ m+ F" G# B4 |/ A; r6 n+ { E
./autogen.sh
4 N* x" d+ t( L* r./configure --enable-standalone-module --disable-mlogc* W$ V; @; S1 [$ [" O6 B4 g! T
make
- a( d0 H+ V) B+ U6 J: ~7 D/ f3 X三.nginx添加modsecurity模块
( G/ [- W4 L& z" }' G( A. Z$ W. c2 p6 U% T/ Q% ^
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
4 M$ J( o0 ?5 X6 k' g% p: n* H1 m) e3 H9 o
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
Y1 {! y- m' R; v7 _; f/ omake && make install
" j, i2 h3 K7 u# p9 J3 Q四.添加规则
# s/ Z; I# y, ]$ p4 W
) ~9 C) l/ C1 k- {modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。" q2 h* ^' t y: u4 k/ |
5 Y) a. O" [8 p D0 b! J' r% F1.下载OWASP规则:6 Q; \5 e) b$ ?8 F; h
3 B, G9 f6 V4 i1 C9 L- q
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs* L% `, V7 C3 S2 b: ]4 S
/ t8 s' u5 N0 z. P, Hmv owasp-modsecurity-crs /opt/tengine/conf/
8 D4 r0 P- A& [" H7 w
& ?) r0 ^. ~' a9 R, d4 _( pcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
, Q5 h$ }+ C% J2 `8 R" C; A0 J2.启用OWASP规则:9 Q U: Q/ @% c$ X5 ?4 d. o9 j! e9 i
, P' G6 Q2 l* n! u ^
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
6 d) b/ `" g( ?. D7 H* P/ _$ Q
- u( i5 f x- B" A+ D编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
j+ {& W7 k0 {/ c: [. F; T0 g8 a! v& L' X2 H
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。6 v" \: g/ Y* M# S
' j9 j; ~- [1 P' a4 K( GInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
! B0 L- l# s0 _6 VInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
+ |0 z# [5 K- P \# LInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf8 e: E% B5 w5 f. ]8 f! z) e( v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
( a2 g8 U8 Q( B, z1 M7 {' XInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf2 {) A% ?9 U# {6 L: E& Z' q9 {0 d
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
2 ?; L. q. J6 ?4 r% }: j4 n- C. G, eInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
/ R* }" S! L$ a; B5 z五.配置nginx5 C1 ?: |, G$ P w# O: W# ?
4 }! u: Z' Q# x$ J1 o在需要启用modsecurity的主机的location下面加入下面两行即可:
# Z. K s1 U Q7 r7 [# G' N( V, ]4 f. M
ModSecurityEnabled on; ; [4 ~2 J7 g- J7 |
ModSecurityConfig modsecurity.conf;
. X& c: i- r# P: M6 t4 \, G5 p* N' _下面是两个示例配置,php虚拟主机:
- Z$ {% C: F1 d: R1 ~+ t" r4 n0 ?% t* r7 L* v$ z
server {, U$ Z; U8 Y' y
listen 80;
# O3 r) y' x }* |& t9 |. l server_name 52os.net www.52os.net;
( ^2 m: v# G4 q& X" F % H* x }9 S, E. J5 {
location ~ \.php$ {' C- x7 d; S" B; j7 g) e1 _
ModSecurityEnabled on; 7 k( B U+ \4 J9 m4 k5 b
ModSecurityConfig modsecurity.conf;& n5 u; q$ J( _( H& s4 B$ |
0 I, L3 I2 C# X/ |
root /web/wordpress;) m8 q; b# b' l: h/ K
index index.php index.html index.htm;7 K$ W5 ?! h& x/ X
: D$ q/ N7 w4 v$ y& j5 W& p9 X# A fastcgi_pass 127.0.0.1:9000;5 |* P' O& y( D! z5 A i
fastcgi_index index.php;
& @8 G5 F% S% l fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;1 _0 [) G# \' f0 P
include fastcgi_params;# _. M0 ? k l: F" W' b0 @
}
3 w' A0 S' F0 o6 j. E }
6 C7 B# j( L# B6 y5 Uupstream负载均衡:' {. T2 e! {+ b" K! `1 r0 h
( @" i: G4 q, n( `$ r/ t! E2 V
upstream 52os.net {
: O6 B! w6 d! f$ P server 192.168.1.100:8080;
l1 d+ g, F: [+ G, } server 192.168.1.101:8080 backup;5 r( |4 B$ N. y3 F
}; ~2 m9 y, N S! ^
/ ?- ]3 F2 b' x- dserver {
q& ]' D! ~/ O/ {; b3 M5 n# wlisten 80;
0 x1 s' H Z4 Z8 R) Hserver_name 52os.net www.52os.net;
0 G. e" N+ e3 ?7 h) V' u4 T0 R" i/ U7 e L" c3 F
location / {
# z3 ^* T7 x! [" K0 p ModSecurityEnabled on; " M4 @5 h; d2 n" |
ModSecurityConfig modsecurity.conf;
6 ~% W [/ Z4 H7 s' s: E" l; g1 Q1 s3 x/ ^* N- `8 H6 v
proxy_pass http://online;
, }' O7 a9 n4 {5 h proxy_redirect off;2 o4 H2 @; |$ ~0 v, ]% R# r7 M* A
proxy_set_header Host $host;- R/ K6 ?% N/ T" N
proxy_set_header X-Real-IP $remote_addr;" M# V8 _8 i/ v% D' [
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
" o8 S- Y# Z+ u7 | }4 { f- a" x' R2 b h
}
- k2 O% R `$ _" E6 c六.测试
6 E2 t& _1 w" D2 |2 B# e- @3 y" E9 `# y) z" G5 o
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:+ h8 h7 g" R7 q2 x" w
& A9 @2 Z+ ^, x' C
<?php7 P5 \6 S( l$ t" P! j3 [! t/ i" S
phpinfo(); + x8 S. D7 O3 Y. [6 h) `7 g- }
?>
: ^9 d" C% E0 y, {# B在浏览器中访问:
/ E% [) L7 \+ G1 i0 u0 w8 ~* |( k% w1 N+ e% `; B. D* d
http://www.52os.net/phpinfo.php?id=1 正常显示。
$ R" I: b/ q* Y5 B; @http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
- B& G9 d/ q3 a l- p& |http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
& k! b8 l: }. ~5 t: J* \. Y Y/ w说明sql注入和xss已经被过滤了
. j7 C4 _$ C4 f* o! [+ q+ j" S5 [8 Q/ ?
七、安装过程中排错; \# G+ U8 Q( Q8 P4 _
& ^+ G# T9 y3 B* {1.缺少APXS会报错
: n F+ q! w0 u+ ?1 G- M3 }" k* l- n: j9 Q1 w; h L
configure: looking for Apache module support via DSO through APXS
: k, X1 n- {) p( c: o% E/ v O% ^configure: error: couldn't find APXS
! T3 s& P: M4 V4 v7 \! Zapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。) w% }& M( I% q
解决方法:& ?) Q" J8 v" I& z9 g6 F
* R* {/ k' \3 Yyum install httpd-devel
2 N/ Y( ?+ I- q, b$ c2.没有pcre7 t- u! q7 m7 V X9 b4 U0 O6 j9 ~
& h9 f5 C+ Z n/ G* m
configure: *** pcre library not found.2 B1 \: U5 u# o9 y
configure: error: pcre library is required
9 `1 {* V- b+ |. R7 l+ g" v解决方法:
/ v8 c9 k2 `9 M: E# T4 k% g
8 ~( C d. I$ y5 |& {9 e1 z$ Fyum install pcre pcre-devel
. h' x, ?' w6 R# G" n3.没有libxml2
4 I$ ^* F4 f+ B1 L
0 A: ?" ~$ S5 I% j' C4 R
5 M6 h. l/ y- Y" z8 O; qconfigure: *** xml library not found.
* G9 I I3 \# e+ w. O Sconfigure: error: libxml2 is required
9 W/ k' v8 X+ I& {# ^. J) a3 [! c解决方法:1 V2 @6 U5 e$ S& i% ]
+ w- k1 s" N! z) E
yum install libxml2 libxml2-devel2 w' a& x- k+ L: M
4.执行 /opt/tengine/sbin/nginx -m 时有警告
- J$ t" g* B5 T3 ^* B7 i v9 F; j: ^: k/ {* f# {) n3 N1 ~
Tengine version: Tengine/2.1.0 (nginx/1.6.2)+ a" x, V9 S1 x7 K
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!3 G9 ~8 }; ~2 u1 q; S6 U+ O) s: H
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log, s$ v6 ]% @$ Q7 D
, I' R( |1 b" W% Y" j8 V( [5 T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
& \& Q0 X/ f; P2 W A# g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"& s9 }- w( ]; a/ b
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
8 H8 G! ~& Z& ?2 s8 w; @2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"5 [; V4 O0 F7 D3 D% C$ o, ^4 ^
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
8 j8 h, Q. | a t: T/ X2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
% L3 I; |3 b& [. i8 u- }解决方法,移除低版本的APR (1.3.9)/ A; e- R( ?; j, z0 F$ T' q5 ?% G
" n1 I; L. F5 _. ]7 Z& t1 a( d% _$ ]; P
yum remove apr! i7 w9 k2 J1 ~+ ^8 I k
5.Error.log中有: Audit log: Failed to lock global mutex
$ H, ^, e0 f$ Q# n
- ?' s; `6 u3 _" j! E2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ! Y: K" _; [/ @0 m7 s4 p
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
4 P" Y* L/ [( S+ ]解决方法:
; |1 B" k9 H1 v) [编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
% T7 d Q6 U( T; A1 @/ u+ B0 m% i: y2 w4 {- F1 T# z8 @' C+ r
SecAuditLogDirMode 0777
x- h0 |. P! NSecAuditLogFileMode 0550
$ A2 q d" G G( i9 y8 U/ P, uSecAuditLogStorageDir /var/log/modsecurity1 F: I, p+ L1 \ |
SecAuditLogType Concurrent
) v" E* N" i. d7 d7 U$ `. }参考文章:' \6 w) `" G$ B& f
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX& K0 I2 M0 w* E3 K
http://drops.wooyun.org/tips/2614 |
|