|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
: k+ D. T- z8 c; f2 o" [' e% S8 `$ F$ g4 p1 |
一.准备工作- D% ^; B5 k _0 W; Y
% z; b# K+ R; X: ~& A% E
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
+ l( F+ k* n5 J2 ?5 @" h1 F Z, r" ]" w' y
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
* j) K2 P3 [' w, U: J: B/ @( F H9 j1 f& r, P% a
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz1 \! P' L: N, P% U6 f4 G4 S
6 P6 T$ U. |$ oOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs- @8 _; [" s0 [2 r
9 o, a2 k2 F% J {) ^
依赖关系:
' a5 N6 _( d* @9 Xtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有: W/ }5 g4 \1 G1 p3 X
; c7 m3 f$ _ T0 Qyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel J( n! M6 _+ h# f
modsecurty依赖的包:pcre httpd-devel libxml2 apr6 d& g5 l* F: N
9 z# P0 t4 q+ C# R% u8 r" R
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
; [' n# r& R9 t3 a二.启用standalone模块并编译
: r& `& t% ^" F8 P! ?2 t! o: j+ m, ~ @
下载modsecurity for nginx 解压,进入解压后目录执行:
5 Q* Y5 ^6 W, J+ c0 s3 L) }5 `' p
./autogen.sh
6 w1 ~+ h* T* O) t0 x9 C8 m./configure --enable-standalone-module --disable-mlogc
7 q" S F( r9 }8 M) h( D+ V% C) R% ^1 cmake / ~5 S" G) U' v( I% Q
三.nginx添加modsecurity模块1 ~1 c+ S2 S( p. `. R; {
8 @9 o" z& ~2 X
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
" i# F8 j2 ~; e0 M9 g1 i( Q& Z( b" o2 t2 |' F
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine. h* k( M7 f0 W- t3 O/ k2 g
make && make install8 S/ d" N+ j5 D# Y* x, c. h
四.添加规则, M+ ~7 u1 Z5 ^% D
K5 Z: H7 @% D/ k9 ~3 {modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。$ M B2 Q, y7 g+ H* K4 _2 |4 I
2 ~, p) h! F+ g' ?% e4 ^, t' F1.下载OWASP规则:
- t7 X# Q& Z+ z" B" r0 y2 \6 I. Q: E4 P& x! O& S; M) q
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
, W7 A: ]6 Y' y+ U" y' ~4 e6 w! U9 J+ r3 C. F* ?0 @: K1 ?
mv owasp-modsecurity-crs /opt/tengine/conf/& E2 _0 ^# i2 a- X& G* S+ x- _
0 ~3 g" X8 F" b
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
, _" D' Q# z+ s& `1 q0 W8 z6 V2.启用OWASP规则:$ \/ ]+ |) K7 }; w, i$ t
! `9 A+ O, f! G; t
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
: p# b# C% I9 _3 k. R
) t) C/ Y; K* h编辑modsecurity.conf 文件,将SecRuleEngine设置为 on, Y$ k' K2 g& r0 [) F! B# M- m7 j
8 O r3 S, n0 j$ k/ u! K+ }owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
$ t) w- W- r6 A3 M- ]! a Z% K+ D3 R/ J: i
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf4 o% f2 ^& q# \5 o8 W
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf* U* L2 m3 P7 f( n& B. j
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
' H5 @1 B, N7 \" W! a: v) b$ tInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
5 h2 z+ R% Y7 M' Q) x$ XInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
( [( y4 W9 H* g1 A2 |: `- tInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf) {, U; V9 z+ L# ?# h5 c
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
1 q! N- l* `4 V' i五.配置nginx
* \- e+ z. y; ~3 L1 ^" [) q m% f6 [" W$ O1 R
在需要启用modsecurity的主机的location下面加入下面两行即可:- E P" m# _! w- b" N: m, u; J0 e
' W& r8 ?9 j6 mModSecurityEnabled on; $ h/ a6 d" {7 _8 E2 g( I
ModSecurityConfig modsecurity.conf;5 ^+ o$ e H7 C) i% J6 [* ^
下面是两个示例配置,php虚拟主机:
3 [/ W, \7 }! P% s+ f1 x
- g* Z+ o& m* A- N$ P: R# V! [server {+ Q1 V" E2 l! ?8 z
listen 80;" M- K1 s- X% R) c3 W6 z
server_name 52os.net www.52os.net;' e5 E1 j8 S4 s) n- {+ b8 Y
2 V# p/ t0 w$ E6 O0 a location ~ \.php$ {
1 p2 ^# g n0 l) X5 O% Z8 R# U2 ` ModSecurityEnabled on;
$ i# Y* Q. v& I h0 i% g/ n ModSecurityConfig modsecurity.conf;
. W, z( f) ?0 J" ]3 N0 C# A, S3 S, R' I( b. n3 Z9 i
root /web/wordpress;1 X1 ~8 R+ }# I/ s# j1 o
index index.php index.html index.htm;
# \6 |& J' _4 W O- n9 F& l! K
- `( H9 X$ D) s+ y; t$ | fastcgi_pass 127.0.0.1:9000;, e3 _7 ]4 [" Z% z4 @9 x; f
fastcgi_index index.php;! W% n+ Z. g$ @9 w; c1 |
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
1 H3 L, k/ C2 |6 J include fastcgi_params;
" U4 _/ U9 `* [; _9 ?1 w2 {8 D8 N2 m! d' R }
3 c; W: A/ D" u) @' w- \( [3 D+ K }5 y# {( M' ]- q
upstream负载均衡:2 y {& C0 Y! A- K
+ W9 ?& i% q0 u# `9 s
upstream 52os.net {& p: w- Z9 p6 c- F% {1 q
server 192.168.1.100:8080;( X$ X" B% h2 A0 ~$ O. d
server 192.168.1.101:8080 backup;
7 Y! T0 m2 J, U9 v* \}, A3 }* H, i) C* m; u" T ~
2 L& G# n: L( I: [, }
server {- l) |4 h4 U6 }$ Z1 S
listen 80;
. S, @* U' V, Xserver_name 52os.net www.52os.net;
. N( q6 }" {) g3 Q1 G% O; E
' B! n* b# d5 j% llocation / {4 p% k( d1 g- D E
ModSecurityEnabled on; 9 b& u: d5 O* _1 z( P9 N Q
ModSecurityConfig modsecurity.conf; 0 N* i6 |3 p# E; ]
! N/ H# q3 v* t R! a. s
proxy_pass http://online;
& @9 F: N% B/ {" V proxy_redirect off;2 ?; c, W4 K2 a7 p
proxy_set_header Host $host;
1 v. Z4 j" `$ _$ {1 O) L/ { proxy_set_header X-Real-IP $remote_addr;/ T7 ?1 J( }3 j, \# k- t+ X0 @
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;, q6 D" C/ e# P& _
}
7 N9 [. a) |) ]# G d}4 O- s; X& d4 A
六.测试) d3 ^; `4 v( r* H, v6 C5 }1 n
0 r+ d6 l0 N' J( k0 X; N7 _我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
' k7 y& ?, C% g5 Z/ D ?+ p8 z0 M* q, ]; M
<?php
" s4 m$ N' h1 G# h0 x phpinfo();
5 D; X7 [# L# L( h# [?>: g7 |, ^. `/ J+ B* v0 ]! S1 ?7 o( ~- G
在浏览器中访问:
3 T. V$ N2 f- z. c; J7 @! k- R O( W9 ?
http://www.52os.net/phpinfo.php?id=1 正常显示。
+ K) H {6 \+ A% z2 ohttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
( D a% O8 x* f4 r1 q+ R6 }4 Mhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
. c2 Z2 C& g/ O0 L9 J* b2 H说明sql注入和xss已经被过滤了/ ^8 ]0 c2 R0 O
. h& M; C$ [; U七、安装过程中排错; U8 _4 z1 g: e6 [8 z2 @3 L
. z+ N/ D% Q, Z' {) M# F8 b
1.缺少APXS会报错1 x1 `4 S) i' Z9 Y) O& L" [ j7 p
) G$ N$ l1 X. M0 E+ G1 |configure: looking for Apache module support via DSO through APXS
- g( v8 L$ `2 M6 C. l5 O" x. u b$ ?configure: error: couldn't find APXS
# W+ T7 }# c, E0 q# Aapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。* b- F! c. Z7 j0 `! W( G
解决方法:
1 q/ O, t7 v. ? u
3 R: X$ m9 p; d- m( w9 O/ a7 U: U. d- Pyum install httpd-devel; J5 d1 C$ A, d" f! c$ W
2.没有pcre
& p$ Z. q1 e2 M& [5 N# ]: s
! n- Y, K( w1 r- ?& O. v! ], wconfigure: *** pcre library not found.
0 |8 n) `1 d9 B& r; O; s' rconfigure: error: pcre library is required
. z; g. b) N" k解决方法:' P% I% ?2 ^3 z) {6 {; J+ B; c) O
! G, y5 v2 T7 J. syum install pcre pcre-devel* @6 [5 G7 K/ U. |
3.没有libxml2
3 Q5 e5 n: o! F; D; O
_/ B; Y9 _% s2 j1 }. K" B3 D {; \2 S# x' V& G+ x
configure: *** xml library not found.- A3 c8 g3 i# C- }; O/ z
configure: error: libxml2 is required3 N: [+ N; Y+ [2 Y4 G2 }- V. S
解决方法:
7 [7 f: y; O# p
. R& y' S0 V) E4 {; J; tyum install libxml2 libxml2-devel
! u' `, _ q& A: A9 H/ v4 X$ R4.执行 /opt/tengine/sbin/nginx -m 时有警告& A! S+ X1 h9 P; K
& ], K# i9 |6 f/ B, g
Tengine version: Tengine/2.1.0 (nginx/1.6.2)$ F1 B: Z. m% _7 Z! v
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
) i# e4 P, P) e5 Q: `原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log$ I$ |: ^9 x8 e* I, ^
1 g8 }- P- [% ]! @! D% r+ g/ h2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.8 p! k& h. S, i6 n7 @+ i( Y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"1 r( S) l9 @# b1 X4 b3 J! V
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
& @% o& ~4 `; ]8 p( P2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"3 D' {6 X4 G6 [% X9 L5 o# j7 {7 q4 s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
& T2 @3 ^1 ~ O: W2 a- u$ ]2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.( ^% w n+ \( c1 r
解决方法,移除低版本的APR (1.3.9)4 n6 I' w- J: L& p7 q7 j+ q
' U! [' B- h$ c
yum remove apr
6 D2 ]) X& y8 @/ }5.Error.log中有: Audit log: Failed to lock global mutex
! g; i9 W0 g( a5 x
: z" Z$ ~+ h+ C" _/ l2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
, {# Q* z' {1 M7 Dglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]0 K0 |- Z% |" S2 z9 G
解决方法:7 e7 U" U+ V% }( ^* j
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:! R8 v/ d4 d/ P2 W! G* F; x9 _
* m" g0 K: p- Z- k* t/ G# ^7 G0 ZSecAuditLogDirMode 0777
% V4 \- H3 L% j; z9 i. MSecAuditLogFileMode 05507 W6 j3 ?; G* S7 y G2 u4 V
SecAuditLogStorageDir /var/log/modsecurity) P9 X& I8 w( v7 r
SecAuditLogType Concurrent
' E+ L6 Z# M" A$ \参考文章:
8 W( x4 I1 E: t" G! v( Qhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX1 L% X4 e6 f y+ W
http://drops.wooyun.org/tips/2614 |
|