|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。; y8 s8 t; F6 ~, }1 p7 O7 H
9 i, p- \- j0 d一.准备工作/ a8 E- F2 U" d2 H" j" W, r
& |3 u" u, r. k2 M
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.07 q" O$ W* d6 f
3 f) {. h' l; a; {/ A
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz( G' `9 \4 g: A* [( J
1 P6 @ K' r, L7 [( M% Dmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz, J [ m! ^; N- p
& b$ ?) K" {0 `! x; \
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
9 u; p0 g5 m% L, R# z. y2 R
: X, m$ N! e- b' C' k6 Q9 K* |依赖关系:
! \$ E' c! B% r& X6 k8 X9 Qtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
& l) e$ j/ c& _
, g* n0 E3 |7 k- J$ S, kyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel: M9 K8 A7 B; U) @
modsecurty依赖的包:pcre httpd-devel libxml2 apr( W) |0 A S4 R# ]% E# @# v
9 B5 c2 X$ \$ L/ \) p
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel! v9 s9 D& m) o2 ?1 k# E S
二.启用standalone模块并编译( _# c9 N: C3 A& U3 g% Q
& E3 b: _7 G9 x3 S0 p下载modsecurity for nginx 解压,进入解压后目录执行:$ \# ?* N; Q* V7 } Z% r
! G9 U" V5 g! d
./autogen.sh( {6 F' Q+ c* t" A
./configure --enable-standalone-module --disable-mlogc* ]$ c E% m+ J ?5 ^1 Y! ]3 R+ i
make
; M& _0 p/ j; \/ k& y" [三.nginx添加modsecurity模块
3 q/ b m h% [ x- j) t: |
. A* u4 J A3 i在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:' w: ?3 i: j& @+ t( e6 ~2 z3 h6 A
8 U, q7 t W/ ?1 D./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine8 o/ j& ~, R% r. A4 |
make && make install
4 @& Y& K0 z7 D6 f) k四.添加规则
) x, U& m9 B* j9 l2 r+ I! \/ X
5 y/ v* R4 z! r: M+ Imodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
& U' f( L+ l# k0 l: v; Q, l3 R. ]+ c$ _7 q' e0 C
1.下载OWASP规则:
, G2 d% `4 A9 u; `) [2 s! D9 X
8 z& \; i5 Q" _7 o0 b" K4 egit clone https://github.com/SpiderLabs/owasp-modsecurity-crs- L. R! y" P& [
n9 }0 D! z" x9 g/ Z* P
mv owasp-modsecurity-crs /opt/tengine/conf/
' N9 Z$ D/ Z7 |+ h, W0 K/ E" a T& [( W: |: m
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf- k, N" t! P3 x4 {4 C, c# R
2.启用OWASP规则:
6 A' o; O( V; o' K X! O( J3 {. C- L# J* X, W
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。0 V+ v# q o+ L W6 b
7 F x. i2 ?) Z/ k) N: n5 P' A/ S编辑modsecurity.conf 文件,将SecRuleEngine设置为 on# U0 n* X( w( W# Y5 n) p, r) K
' f" C+ x5 o, g0 G7 g4 o
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。0 ]- W8 ^: k1 B5 u( m
$ q0 O% [! S7 v0 }+ L
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
3 ^9 a1 _' L. d, I2 PInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
' v) o% I* |6 vInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf. R( x- [% {, N8 e
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
6 S; F4 c2 F# ?7 r& IInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf; K8 T! {1 ~8 \' w$ M1 \3 R: }' ~! t
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
! i% n9 v6 U5 SInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf/ q. A1 {4 O5 b
五.配置nginx- s) ~) T) A3 D! D
! T6 Y- r: {+ w4 j1 `6 o
在需要启用modsecurity的主机的location下面加入下面两行即可:
v9 E( m) B6 h, |
" F# t. _, ^9 d8 ?' F; c0 F/ d+ a% JModSecurityEnabled on;
% {1 o5 i! X) ?0 v8 s# c( F: g! lModSecurityConfig modsecurity.conf;8 t" X. N" u. C
下面是两个示例配置,php虚拟主机:
0 b9 G, t, H: i, ~# t" }- o: O E+ C, e7 v3 M+ W
server {3 m& [3 F5 h& O5 U
listen 80;: G7 T0 O8 S: Y" r/ k0 Y" n
server_name 52os.net www.52os.net;
$ F* C' j: j6 E1 Y! |6 F ; s! S5 c+ S2 w/ d' [- A# d
location ~ \.php$ {8 e( m+ ^9 T2 _, P( b' p5 e
ModSecurityEnabled on; ' L# Z- F! _' L& v& P, {* J4 Q. ?
ModSecurityConfig modsecurity.conf;* z, R. T% i4 K( _4 m8 E
3 c- r& K6 j( r$ Y. J" N root /web/wordpress;( I M8 T7 E1 {4 z
index index.php index.html index.htm;& _; [% O+ m0 E! a
, I, j! a a% i7 w fastcgi_pass 127.0.0.1:9000;
7 L' z5 U" O& L9 r D5 B5 ] fastcgi_index index.php;7 P% S% R6 t0 z) a" t0 @, o! N2 S
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;0 t$ `4 S: `& H9 [2 y
include fastcgi_params;
3 Y0 {% i# ?1 X1 S }9 O6 G3 W0 P- h/ |& ~7 Q1 M2 `
}% m4 y- x- ?4 ]8 Y
upstream负载均衡:
5 O/ w: N& C' ?
8 e! v* ]9 m, o) F3 s; lupstream 52os.net {7 a& p! ~' e3 `6 g$ T$ p
server 192.168.1.100:8080;
4 @, m* c5 W2 m: H) A5 ~9 G server 192.168.1.101:8080 backup;; S+ [3 s! S( j' m
}2 X3 ^$ y! B' E- s, e1 P3 {
( {( ~& v" C, H, E/ t* N- J6 mserver {, B, f4 w ^1 R( k: Q! w# Y
listen 80;7 B. f* J$ B4 @3 c: q( f3 l
server_name 52os.net www.52os.net;
& A6 j2 h1 k- S2 l+ j2 u" H ^; j
location / {
4 r4 n2 \: U T! ?: J7 g ModSecurityEnabled on; $ ?1 Q1 D$ W" Q* U
ModSecurityConfig modsecurity.conf;
0 A% U0 W3 q6 O/ t
( Q B4 J% J. ~& i, s proxy_pass http://online;
: l7 ]" t( E6 j+ f proxy_redirect off;
. b+ A; ~. _4 D4 L5 w2 Q$ { proxy_set_header Host $host;% c. U- T) y5 O6 h5 `# l V
proxy_set_header X-Real-IP $remote_addr;! o T+ L7 |& ^; s3 b, G' U' }. C2 `
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;, X7 u- [, J: K. k
}, }% s$ k( ]; u1 @2 \; p5 d( ~' L
}: T( Z+ i* f: C! f+ ]4 y% g4 X
六.测试4 j, [# r* B ?! P: ?9 a' O& f
# K! I) l+ M4 n6 N" R" I我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
& @& E) Z0 e0 `& V4 Z% V Y! c+ y4 \9 z$ V( W5 c
<?php5 d8 U# Z; v% U0 Y4 C6 B
phpinfo();
* }5 p$ D. A4 d( \?>
" c/ ]3 N8 r* c在浏览器中访问:
& z, m& j* E. V6 b5 ?) _8 v; q
8 u& O! C1 J1 o4 ^http://www.52os.net/phpinfo.php?id=1 正常显示。
, Y& P# n9 J" G! ?: ihttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
' z# @3 M( i& khttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。7 a6 S: _) c$ `" y3 E
说明sql注入和xss已经被过滤了
& ^) F# o4 `$ k9 v S
$ b9 g7 E4 k6 V3 ]$ `七、安装过程中排错
4 i# v# e# o8 m# K) G% \2 V% B+ Z& A3 p& t1 q. C( S4 J
1.缺少APXS会报错
: C( v% R" K( E# a9 e0 m0 `# j! {. O2 w% R$ s" F; Z' I
configure: looking for Apache module support via DSO through APXS- {: l4 M' r" n( Q3 d. K4 i: O9 i
configure: error: couldn't find APXS# n" K* b3 i* v$ u
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。7 [+ ?; E/ }1 b
解决方法:
3 f4 z4 V8 w R4 r( R- Y4 F$ b8 W
8 s R: K9 [. ]1 Vyum install httpd-devel( @* L7 P% Z. D; M$ r
2.没有pcre2 J- I8 b$ y7 Y% \
0 A4 a: |' s: ?0 u9 rconfigure: *** pcre library not found.
" e: R# U6 Q, Mconfigure: error: pcre library is required4 }9 }" s: t/ ^7 H. g
解决方法:
$ I8 O; R' J0 q9 ]% w8 W2 E: O
. k/ D- L/ T' a2 F4 N3 U Fyum install pcre pcre-devel `0 L: s N, h9 ^
3.没有libxml2
1 U- e3 j I& \( v' ?& u
+ J$ d1 Z9 v1 _3 J+ M% p1 X7 W
9 t. F! v2 G& o6 Z$ ?, A( Jconfigure: *** xml library not found.
/ W- q5 A. r" {( `configure: error: libxml2 is required" \9 S6 S" ^: T3 G1 Q9 i+ J8 E8 s
解决方法: [# t' V, r0 u7 d# J& J6 @
: o+ i8 a+ n9 g, g, E# T* Qyum install libxml2 libxml2-devel
4 K5 l5 Z t9 Y3 h4.执行 /opt/tengine/sbin/nginx -m 时有警告1 \% |8 q i! v9 g5 T
' Z1 J6 k3 s4 s/ Q
Tengine version: Tengine/2.1.0 (nginx/1.6.2)7 i: `: J: j! s, E- ]4 y
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!1 Z/ D% o* Q( x0 j: P* {- N. [
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log/ O7 n+ n" z& z
4 l7 I4 H, m w' J. C4 `' {# ~7 c
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
2 N$ _& H" y: P6 O! o; d: A& O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"0 [$ F# o8 I; m
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
% a$ K% V$ I- B0 \. R! ]/ p2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
$ d" J5 q6 u# ?& q4 D$ \4 E' v2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"# V( \4 z& }4 w! b( @
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
' }/ i" s1 L: s& ^, `解决方法,移除低版本的APR (1.3.9)8 K# U& p! C6 W% @2 [6 I
, K# M9 m% J" D5 p: b- I# D, R
yum remove apr
0 n% s% J3 w m! q7 N. n- h5.Error.log中有: Audit log: Failed to lock global mutex2 Q$ z, y. t" ]- Q8 D j0 p. w0 B7 K7 E
6 d) ]$ {9 H4 i/ W8 d1 y
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
4 G, o8 E! k9 `$ p! w0 sglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]$ k$ |/ @, Q0 V5 c3 `+ Q, j
解决方法:
4 h6 E0 n" r0 j/ p2 e9 M编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
2 r/ W5 P, @1 B4 D
& b# M8 b# T2 R' q+ sSecAuditLogDirMode 0777
5 p' I1 b2 {* D1 ^7 ~ `SecAuditLogFileMode 05502 w8 q6 O- G$ L7 I3 M
SecAuditLogStorageDir /var/log/modsecurity6 K% }3 @; }; }3 @
SecAuditLogType Concurrent0 F9 P) e3 u% s8 H$ v( d9 _, { _
参考文章:
: Z$ d% _; C9 a9 o$ O& G6 E8 Qhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX, I @' z1 @5 _) A/ N6 A
http://drops.wooyun.org/tips/2614 |
|