|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。& \. z6 K" N4 X6 g; A6 M
) E8 {3 |; ~+ |- Q$ U1 B一.准备工作8 N( ~0 w6 W% e1 _5 O6 }; L
" q+ T6 M' g6 j6 N7 I; h2 {
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0- e4 l1 A& Q5 t- m4 n: a& K% M
! k9 p2 p0 b6 m+ Q" H7 R+ U
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz+ X4 z+ G6 I8 m$ g% U4 B
" C5 J- S! ^3 ^% J# J0 b4 Omodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz# Q9 s! ]; a3 Z% `2 }2 S
" a; t7 u- C7 |OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
: |7 K, M) I2 N! l) R5 |! J& N/ c3 T6 Q
依赖关系:
. b2 A% \5 S" I* ^tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
4 u* ?3 Z" H0 r7 ^7 L5 ]
2 \6 M0 B5 f% ?- ^yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
# n! K- [2 q6 K+ Wmodsecurty依赖的包:pcre httpd-devel libxml2 apr
7 j! P4 k' V5 O- r, V/ P6 g: K+ ?( r5 d
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
# I( N$ P) X; _) g" D9 x8 ?二.启用standalone模块并编译! B0 L( g# m6 O8 f; T6 X
# j7 w% \: |! l' Y& e3 D/ N' V3 l! k
下载modsecurity for nginx 解压,进入解压后目录执行:
: z5 }' K( u, v+ O
7 O9 @. D2 Y8 O8 t./autogen.sh0 x" o$ U5 l2 z* G8 F; x* \
./configure --enable-standalone-module --disable-mlogc
& U( w" X' C, dmake
+ O* k- O/ V% l& q3 j( q三.nginx添加modsecurity模块
6 B$ D" w! `% H3 J, ~/ m- z& g. n' Y4 ~1 a9 l: E
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
$ Y/ v$ z1 f& n. `3 l2 i
1 C j, P: M, j; u1 F h./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine7 \; Z) L- t2 u4 {
make && make install/ y* Y: y6 o. E2 b1 n9 o
四.添加规则
6 H5 v$ L2 T: D1 D J% x. N' s# c( T
q" w5 J0 r0 b# @# E+ h6 Hmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。 K- d" P1 i" J4 u7 [$ Q3 k
: g/ p9 k7 J- X1.下载OWASP规则:
- L8 w3 O6 S9 i2 B# [2 Y7 U( S" x2 L0 Q0 @ H0 m
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs, i! n: l y2 t4 V$ f
0 m& \! W) T* o* G, o
mv owasp-modsecurity-crs /opt/tengine/conf/, h/ x U) u% o6 M: S7 v
3 Z' u) R" c6 u5 M( D
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf$ ]6 T# h- C+ H% ?
2.启用OWASP规则:4 `3 d# n' F: O! r7 S
_/ Z( `9 M" A8 s% E, e- F
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
}7 X8 l) {6 p) q$ G# @" {# }3 Y. z9 f; \1 a1 u0 r+ v
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
2 p- ]& ~% s( S* J$ M
. \6 P `1 j- G6 g+ J4 z' Nowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
- Y ~$ D, ~ c% t
, O" E; [5 X0 n4 l; Q* iInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf+ w! K% M' S) ^
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
3 S& Z$ r) |4 N2 hInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
* @) |4 W. N+ Z2 }Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf0 U6 Q$ h1 P7 ~1 |: V, N( p
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf9 N3 H5 o8 H( y* B% z8 Z- U
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf$ R4 o, W l1 n$ J$ ~+ Y
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
; `: U! H6 x8 s6 V" \2 ~1 o( x五.配置nginx& s7 P, K2 g8 M5 e! @, U8 \
3 f; O5 s' Y9 l9 b& Y
在需要启用modsecurity的主机的location下面加入下面两行即可:9 d- G1 u4 L& F% m* y0 w: `3 R
3 W; O# f+ }# a9 x2 @. t
ModSecurityEnabled on;
& f; p+ d! Y' N# I6 [4 zModSecurityConfig modsecurity.conf;
! c* D# s. `+ f/ q/ Z9 A9 t5 p. v下面是两个示例配置,php虚拟主机:5 L I3 g7 b0 o' K6 X/ `; j2 B* V
' m& }+ `4 x) t* h
server {
/ o7 b2 u1 w& E4 N3 T; J listen 80;7 j% z4 z+ h, l9 m8 k, l
server_name 52os.net www.52os.net;
' W% c* q# @! y2 k8 J( |
+ C& n2 y/ d* L3 i( U9 z location ~ \.php$ {, u7 W) h- J+ G$ h7 X' N
ModSecurityEnabled on; ( _3 @$ U/ t) g; }- ~: H
ModSecurityConfig modsecurity.conf;: L$ l" @- D" `2 w. Y3 X
' Z: i. D3 n4 K7 A [" b& n root /web/wordpress;
. l# M, J' G7 S index index.php index.html index.htm;
% |) U0 N! n, |. V5 h; O; i
s1 ] c) g5 ~9 V4 L fastcgi_pass 127.0.0.1:9000;9 {2 |$ K7 s2 _3 w! }
fastcgi_index index.php;3 M* h0 p. {& y, s6 w, g6 R
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
0 Y, v4 M% H' X3 d include fastcgi_params;
2 D9 Q+ ]1 H# V8 o/ | }
8 m ~: j7 G7 W* v0 C! u' o }
4 K: b7 a! x5 L: Zupstream负载均衡:* z) l' n1 H' ]7 h6 i7 n- o. N" ~
3 |+ m/ s/ Y" ]& V, Z. J; Zupstream 52os.net {
0 K2 K9 l2 k5 y& @! K) g server 192.168.1.100:8080;- p/ P2 C1 J4 u
server 192.168.1.101:8080 backup;% I- }1 V. r8 b- V6 [ ~7 p
}
# a& K- {9 R) Y! z& @6 j$ P% r% b# i- ~) w( t; d6 d
server {6 ^# y- A, K* `$ r0 o0 s( ~
listen 80;- E5 W1 [* w( z
server_name 52os.net www.52os.net;' b) B3 a0 s) F6 m2 V
" O, k) ~" ~0 X8 I) Ulocation / {
( \# s0 K4 ~; f- p8 K) J% C ModSecurityEnabled on; & P. z4 g% N/ d4 }
ModSecurityConfig modsecurity.conf;
) G" H7 o. Y ]3 f6 [; P9 g4 L, {, O& z: C5 A- Y! L
proxy_pass http://online;
9 z0 h2 e8 F- S4 l proxy_redirect off;
0 _& M- A8 q3 c% H2 z proxy_set_header Host $host;
! I/ C) Y, O2 N& I$ M' |; K0 T proxy_set_header X-Real-IP $remote_addr;
5 T2 }# i+ i: V ] proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- W- M5 ^& u4 H# }6 v }6 g9 d3 }' h" r: I7 ^
}5 w l9 j+ i4 l3 _
六.测试
. S: A9 y6 g& P+ \% z
- p1 Z' o. m- b* h W" B( n8 @我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:4 ]; z( f& a: V
/ n) g! J! L8 L- b5 \
<?php
7 Z' }9 u2 _% |* p7 C phpinfo();
# b+ J) ^0 F& Y, L$ Q" F% K?>
; o# w4 \0 Q3 O( C在浏览器中访问:6 G% ^1 G6 E% Y7 c' U4 I
: w* y3 ^* f8 Y; {# `; xhttp://www.52os.net/phpinfo.php?id=1 正常显示。' O$ E: t& y$ b9 C4 \6 z
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
# ~8 R* C& _1 L& N. ~1 f8 @; ohttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
. r& i p5 _5 B ~# k$ ?% B说明sql注入和xss已经被过滤了9 e: A( J5 ^1 f2 ~* n- q
, G7 h, Y8 ]$ _- Y: S七、安装过程中排错6 A& N% N' d7 n- K1 K
( d/ P% \/ Z# N! Y* @ ^4 F, |7 Q8 a) i
1.缺少APXS会报错
$ w/ W: Q) Z' _) H# U7 ]+ r- A. y, x' n" b
configure: looking for Apache module support via DSO through APXS; M) D0 h" S2 ]4 U/ `) O. T
configure: error: couldn't find APXS
2 {) R- e* y- I) J6 @: lapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
1 e" @, Z9 N) _' m解决方法:
+ o9 y( u. s z3 J" ~ X3 v" c% E0 [6 S+ H- V# V( l# q
yum install httpd-devel
k+ B# m) N5 Y0 N, j5 _! Y2.没有pcre
( z: U+ }8 _4 |* s) ^& X0 m8 A i+ O8 C+ m! x+ j, o
configure: *** pcre library not found.) N# R2 C, M. U) n/ r
configure: error: pcre library is required
% q- P4 d' E: z3 L8 \+ x解决方法:" t6 }0 |2 D k8 m) v# z- x- {
$ V' g# w* T3 u( C& K6 ]
yum install pcre pcre-devel/ }: s$ L3 f* k! ^- k; b
3.没有libxml2, K" d7 F+ R' L+ s8 m
+ n) t& S* z( @* |0 P4 d) b- P
/ m, j% t# h( g! e0 a
configure: *** xml library not found.( @$ F4 v! L! |" C& t. V- D
configure: error: libxml2 is required
$ _* b4 m c: u- Q+ _, A解决方法:, S# S/ \5 s( J+ `) d% i# z
3 }; X C+ T7 P
yum install libxml2 libxml2-devel
, f7 x# f; z; \$ T) _8 t) w4.执行 /opt/tengine/sbin/nginx -m 时有警告
: w5 ]% ]' p, k) x' [1 I, U& p( b' V; I) a8 m0 l; I
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
* d+ f3 X Y; W/ N( N7 E2 ~nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
" g: E0 `! P2 t) f+ h* B$ x0 m& G. n, O原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
5 D$ C4 m6 [! E9 S3 J+ v w6 [8 F( R# A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
* L0 V; P* i- T2 l" l( v+ a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"( X3 C1 S9 |1 G: a4 z M" c8 t
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
: G( @: `; w1 W9 X2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"* e6 A2 r# s7 u% y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
5 ^8 o& w/ l7 Q- y2 B( |2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
/ X9 h2 x7 [5 A" }1 H$ Y. W6 c2 s解决方法,移除低版本的APR (1.3.9)2 p8 h. x" C7 E% a
7 G; f" J5 n( x0 r
yum remove apr
9 p( z" R5 q! U; m C- Z7 e' q5.Error.log中有: Audit log: Failed to lock global mutex" f; E) C& u; w& n
) `9 `7 e6 ~- S% T2 o9 s# @& j2 I
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
7 }' l; J$ ]1 X, m, tglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]. p, i# @8 c/ s) c8 H/ F* _
解决方法: }) m% U: x- T0 }) P# x! u
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:7 M1 ?$ h& J8 L" y
2 I# {2 x. H9 p9 @1 }5 n4 g( MSecAuditLogDirMode 0777
+ M& C4 p# f6 ZSecAuditLogFileMode 0550
3 @% `. ?! N/ q: W; W. r6 D* hSecAuditLogStorageDir /var/log/modsecurity6 t8 P8 P( O' w0 b1 _, L8 v" B
SecAuditLogType Concurrent$ A( C3 f" B x K' n/ u! m
参考文章:- d% e2 e! c# N6 ^; S
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX: u" L6 p$ i+ q) j/ \8 K3 {
http://drops.wooyun.org/tips/2614 |
|