|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。" Q. w* K+ ]) I5 V5 ]0 Z( ~: V
- P# Q! \6 `% V6 l一.准备工作
7 U O) i( p) g
' b, O5 G7 Q7 m& o系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
' @0 `$ l5 h$ ?3 f) c* S4 _/ w3 G8 e U( z, V4 Z X
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
# c! l; Q: b7 o2 s& P
) J* \7 j: ~2 ]- r, \: a$ \$ z8 {modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz) i! i$ K/ z2 r2 _
$ B' F& Y3 ?# E3 D! |% S' K% G) [OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
" P0 ` k; ^+ l$ k2 k
1 `' _) t8 P6 I8 k+ v3 l3 v* I依赖关系:
) @* t+ p2 `- O( ?7 e xtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
: C$ r z: q) x, U+ _2 S& V; f7 ^' q/ N; C1 R
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel, k1 q+ Z! S) \4 ?( P# ~! h
modsecurty依赖的包:pcre httpd-devel libxml2 apr
( o% g- \- O% A. t: p2 _7 z
- |+ ]8 K4 y( }6 y8 Jyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
( Q7 T$ M6 T, k; `% ?" [二.启用standalone模块并编译
6 ?) h4 J# y! I* }9 P" D" W( \; k" |5 |& }9 U
下载modsecurity for nginx 解压,进入解压后目录执行:
- p1 e& f$ V; U. c* R) t1 |' \% d+ n. x: {& ]3 C$ y) Q' w) t8 l9 p
./autogen.sh, \6 k; O6 i3 T7 i# i+ b
./configure --enable-standalone-module --disable-mlogc
: h) M4 Q7 N/ gmake
* c1 V w( y# Q8 n" ^& I" i3 m& X/ e三.nginx添加modsecurity模块
! f) x# e2 x" L3 w0 G0 l" r8 O0 u0 L1 P8 M2 m
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块: @5 W% R% u; o% ~' ^" R
, a7 C6 t4 O& U; p# l. R3 X! d& J: P* s
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
$ [* W( }( J0 h" r. {make && make install
% |6 M. P; M: L/ K* L2 ?四.添加规则2 a9 X2 Q' H1 `+ h/ e" D0 j. L
# m; Z, c6 K( S7 k, L
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
- i. S6 N3 e7 O7 k" k5 U$ ]# v* B6 H7 k% d( G# j
1.下载OWASP规则:
! L- P2 I3 E1 ?6 H# `, x9 @; }2 L7 j7 D! y9 Q3 b( `- j; h
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
e: E( w" V- v9 B C
: ?& l+ ?0 Q9 l7 J: _ Vmv owasp-modsecurity-crs /opt/tengine/conf/
$ E M- j& I1 J3 ] y, A' ~
! V3 r2 K9 a4 K& w; K3 r0 Hcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
$ I* U3 |/ m$ R E2.启用OWASP规则:
8 a8 n3 a! ], D: K, {
5 V3 a. B6 \) ^. Y& h& y8 `* r复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
3 [) N( o4 {& o: N) K
3 |" l( ^' J7 y. p1 ~编辑modsecurity.conf 文件,将SecRuleEngine设置为 on. U& H, ^5 k7 v4 m6 ]# W
) {5 N, `! c1 i/ Z, uowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。' ~' H' w# e: E7 D) ~. x7 J
: d2 V" {! o5 z+ A9 I% ^& P: uInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf% c6 ?: ~! i- i7 w
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf5 Z* T2 U: e8 j2 S9 h& ]* h
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
4 D. h* Y- i$ m/ uInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf- |: j0 {- }( Q& b
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
) D. | ~0 h/ _Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
1 R; [2 R: y5 p% ~" _6 z7 J' e9 wInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
# p) g' S; E! o; @五.配置nginx
4 m6 A" A7 U% W9 @$ h$ B9 u
9 _1 ~3 g h/ ^6 e( u; c6 [) S3 X1 u在需要启用modsecurity的主机的location下面加入下面两行即可:
; ^9 S. U! B1 s( C; U1 k. N# z! `" r9 k6 \3 o
ModSecurityEnabled on;
0 o* D2 R8 t8 S4 A% JModSecurityConfig modsecurity.conf;
- z) e& H6 c: R( e" l! F. I3 y) b下面是两个示例配置,php虚拟主机:
# z, L1 r, A+ O9 U1 L9 F& r5 P: {2 R
& B& o4 ]+ A- M1 m% b0 h. G! K pserver {
3 w+ F- e9 Z! y! I; u listen 80;
- I7 i9 D# x0 @5 }9 h server_name 52os.net www.52os.net;
$ g. I& `$ z- }' C! [' y+ R + \" x& e1 a. w9 {
location ~ \.php$ {; @. l1 ?+ _+ T
ModSecurityEnabled on; : \6 i P) e" u C
ModSecurityConfig modsecurity.conf;
* f6 H; u$ [ t j( B3 O; m8 L/ }9 K2 i8 u& L7 P
root /web/wordpress;, j T" q/ U3 w1 X# F- ~6 U3 S
index index.php index.html index.htm;
: d% [: O* |$ T+ d, F- g - n8 v2 M+ r+ t+ j: t
fastcgi_pass 127.0.0.1:9000;
; B+ ~0 e- s; M4 R0 j8 O$ s+ l fastcgi_index index.php;3 P g+ J# Y7 i! C( U: V( [
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;& Z$ W1 D3 N3 H8 h5 k+ u! W
include fastcgi_params;
7 T0 z6 A! h2 i/ A( m# Z3 T }& H7 h1 o: ]/ J
}$ u6 O- ^: I0 k% ~0 ] {, }: B
upstream负载均衡:! a' i- J- S" S5 ~5 ^5 Z& {7 {
2 P1 ]( Z5 ?! M. Eupstream 52os.net {
0 i8 m+ h3 o; E/ D) M server 192.168.1.100:8080;
" F. e* U1 |+ ]$ b' q* w server 192.168.1.101:8080 backup;
7 R1 x0 b8 Q* H! Z: f& I z}
5 E/ {# L+ }6 W# I' ~: F/ i" @6 H; F5 }& Z) y* f- z
server {0 F( d& r3 `# X A6 [
listen 80;
4 d* O' L/ d* ]# l3 Gserver_name 52os.net www.52os.net;' Y$ E. d/ ~0 r$ X
0 R$ h) Q# |$ `6 ~3 V8 tlocation / {
( ^ k- f. q5 C7 ^0 l ModSecurityEnabled on;
' n' `, }5 }$ d) ?& K ModSecurityConfig modsecurity.conf; # _- T8 v$ z8 h( F5 [
% ^8 q% U" v8 x) o, k proxy_pass http://online;
9 ~: D0 ]' C5 V! C) j9 }3 u proxy_redirect off;
3 t* V' U/ t. i T, u, X) K proxy_set_header Host $host;$ ~) A" t d5 v& @! u5 F8 V; A
proxy_set_header X-Real-IP $remote_addr;
# ~. Y9 b( Y4 U proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
5 D6 p5 E2 s* e6 [1 F: N }
2 h6 F8 ^+ U, w' P6 O" E6 W0 k# ^. X}
, B4 C/ g6 G: P5 }# ?! F六.测试! w* L( r& S% _& [( Y
& i% B6 ] ^ i* z% {: b. n: f
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:0 C. |$ A) K, |* ^( i7 _, T, j
" _' [' h* E9 t3 G {+ ~& M F
<?php
$ _5 J2 ]7 }$ v# s( F$ b/ t phpinfo(); " O& h# f& R8 x& o6 u
?>
9 x/ d' d! L- I在浏览器中访问:1 W t2 g( \- o/ S' A; _( ^& L1 q
t; Y+ r1 G# i: F" w
http://www.52os.net/phpinfo.php?id=1 正常显示。
, t+ w) z8 ]6 l* m1 ihttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。) N" G# ?& S$ H5 s
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。! g# t, z1 i0 B+ F4 G7 t
说明sql注入和xss已经被过滤了* Y5 i$ e( J" v; w
# O j2 o, p$ E) g
七、安装过程中排错0 M1 Q8 d; G- A, O( f j
N2 o$ d0 x# Y h5 w) ]1.缺少APXS会报错
$ a* w+ D) A) Q7 c n. }
v' B/ c: ^6 ^" r! G/ B# rconfigure: looking for Apache module support via DSO through APXS1 T) [+ i3 |4 Q% @
configure: error: couldn't find APXS
2 P5 L, {& b8 R" Uapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
: P$ w5 V2 l9 }# ]解决方法:! K6 v5 J+ Z6 ^" G# x t/ Z
- x. m8 `$ \% [* [ Y" R
yum install httpd-devel
9 q5 @$ ?- r+ ~, G+ \2.没有pcre
5 J% c; e, a- ]" f- ?
& \3 U5 H* M0 V5 w; k7 t# ~2 qconfigure: *** pcre library not found.6 I) Q) C. y/ D' P4 M
configure: error: pcre library is required. x8 k) L9 A) I" @( m
解决方法:
* t, e9 t( U; O& r
0 m8 L6 z+ G$ {3 `( k) |: dyum install pcre pcre-devel
$ ^! l6 N, u, C3.没有libxml2- [3 [* m+ d5 ]# B8 E
2 {; @( i3 z: \! Q9 v1 ?
& D& e n* P( x
configure: *** xml library not found.
) A, j K4 Q* I: B8 `+ Cconfigure: error: libxml2 is required
* i! E6 m9 c( s8 z解决方法:
' a5 S% E) M, T3 ]3 h
: ~& A) K1 s/ H: I+ Dyum install libxml2 libxml2-devel. `) L. @( L/ A) I: ^4 s* ]- d
4.执行 /opt/tengine/sbin/nginx -m 时有警告
! a( s" ?4 _* P( {; p% T8 Y
6 [$ V* B! W9 [4 \6 |8 N4 C! ETengine version: Tengine/2.1.0 (nginx/1.6.2)$ j" j4 Q4 Z6 G% r7 b1 O0 Z6 l M, ]
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!! X2 p. ?( R/ ~# r, U
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
$ ^- F: c: }* u' A4 X. i. O; T1 V! }) ], b4 i) f! g2 D9 z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.$ e, y& ]8 U; U
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
" s( i& X' R3 N# G: n: l p2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
' X! H, w t1 F2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05") ~) ~, @) h3 z$ R* R r
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
4 D) _2 q; H9 o m8 t+ W- O2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On." W1 N9 B, ~% p1 G
解决方法,移除低版本的APR (1.3.9)
: B0 w: [3 g2 P1 t
0 h4 e7 W! P( R8 M" N2 ?# C' wyum remove apr' n9 o& f2 E' B- |5 a7 C! `
5.Error.log中有: Audit log: Failed to lock global mutex
# `. _, D. B8 u, m
. t; k k/ h6 ?& O3 A Q) ~2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
8 d# j6 {7 _9 l5 |& I! D9 {global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]) A4 m0 B+ e, O, p
解决方法:
. T M9 P" b5 G/ n编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:, V; Q8 ?' x& k- {9 ^
4 I# O% {$ u+ I
SecAuditLogDirMode 0777
" X$ Q5 Z4 C F2 V* |) _- ISecAuditLogFileMode 0550
4 n, @0 T5 `& Y! S0 h7 K$ NSecAuditLogStorageDir /var/log/modsecurity, _1 k( H* ~& H4 V* Q5 W- q2 C
SecAuditLogType Concurrent0 U7 v+ w3 M' p3 M9 T' }9 k( H
参考文章:
- R8 l M7 k6 R, H! m. bhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
* E9 a+ P( ~& O7 o2 W! Vhttp://drops.wooyun.org/tips/2614 |
|