|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。! I4 P, @' e3 l1 U" Z
8 q2 J( v0 ?( [( P7 |9 h p, o
一.准备工作' R: P |$ d2 R3 L1 x5 s
6 B8 v! k. b8 V0 ^4 P8 D: n M% k( |
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
% b* v5 b. @1 f/ q J U! l% e2 l" f- t, c8 f
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
, G% |9 A8 K7 f' g- s
: w) t) I$ C. H- q; v' |% smodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz, |; `" l; Q* _! k" k
3 n# \" `1 j, H3 N. X. t
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs5 H/ w; N7 j5 @& y u0 W- H/ m" E
8 W4 m" f& Z6 R5 b; x+ @8 }) R依赖关系:
$ ^, T: d1 `# P7 v5 r6 J5 Ltengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:; s! I" v9 E2 l+ u" c" g1 g
2 f/ z1 O" q9 ]yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel$ M, j6 B# V; L: l5 K: t) I! h
modsecurty依赖的包:pcre httpd-devel libxml2 apr6 k, v5 ?- b* O6 G, j4 m8 q
- g7 @6 Z& c7 t5 o. k+ I
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel" v- {* ]$ b7 \; V& d6 o: L
二.启用standalone模块并编译
* U# S3 }% X4 A/ B( x6 O1 M6 J6 V B$ \/ c2 s3 t% @- q3 n( @3 {% f. ^
下载modsecurity for nginx 解压,进入解压后目录执行:% A9 H" S x. ]" l6 x$ V
4 ~4 y5 F2 H5 r8 E. a: ]./autogen.sh
) b* ], I! Q6 M L: T1 F5 v% W6 w/ d./configure --enable-standalone-module --disable-mlogc
/ {% k& |: Z& p: n- J' H9 `# Bmake
8 U+ {1 s/ t" X4 w' Z6 M三.nginx添加modsecurity模块
& {9 w/ w) J5 _6 K9 |# e! X1 m5 f& Q4 p
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
; |! q& R) T' X" v; D- x9 \: F2 W7 i
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
! m! h7 k! o3 v- T* ymake && make install
4 j' `+ I$ V% |( D1 i6 N四.添加规则
7 u2 D; n9 b* y* b, Z: @
# G `) m# j6 w/ C( tmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。- ?- H3 ^4 Y8 U
5 H2 h9 i- `0 `. Q
1.下载OWASP规则:
4 X' S B0 K4 u6 ^- X: n: @' `
* | Z" ~0 G2 P7 Q( `% Q' tgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs W8 u1 G, ?6 ?9 a2 T" n
" M0 h( \ t' v, {$ S3 S
mv owasp-modsecurity-crs /opt/tengine/conf/3 j g/ N$ y0 _) L- n' V
0 q; C, k; a7 |- X; K; ]cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
& P+ s5 |- C5 J8 j6 |. c2.启用OWASP规则:
& Q& O3 q8 B' E+ ^( B6 h1 G6 P! C9 @3 A- T# X0 F5 r
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。+ S6 v9 A2 z5 H7 A
" n. p2 d1 I* v2 M
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
3 m5 s) i2 O* j C0 ~
- a( @: d! d& z& Bowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。8 |1 I h8 Q# F: m
& G3 X9 Y. F7 F: w/ Q. I$ g0 [Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf& \3 p, }& W+ c
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
# p6 u! Q1 X- I# QInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
6 S1 Q# s8 v( L( @Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
7 }# u, m3 J* d& D6 KInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
1 W# X- {" j& L! |2 rInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf' C" P" h$ _9 A$ L) I1 M- Q
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
7 m* d. y+ X! J7 n6 Q五.配置nginx
: K* ?) N; p* ]; {8 O" Y9 y# b+ k ?: C2 h
在需要启用modsecurity的主机的location下面加入下面两行即可:# D0 ]* V x' T+ k1 K
9 T* L( _8 C; ~9 U; s9 n r" A! G0 {ModSecurityEnabled on;
, R' r; x4 r: E* q* w, I SModSecurityConfig modsecurity.conf;! h$ z% t: k) [* K9 p9 e
下面是两个示例配置,php虚拟主机:1 f0 ?2 `! ^; T4 D
5 Q' F& S' B0 e. [4 l. t0 g, v
server {
2 \/ z+ O& ~% } listen 80;: s3 k& `! J; i1 q% X0 X
server_name 52os.net www.52os.net;& a& c5 h% l' ]5 I
% {' L7 N' r$ W( h% G3 H x location ~ \.php$ {
; e1 X# e$ l' e ModSecurityEnabled on; 2 B+ ]7 O3 [7 E' ]3 Q( k
ModSecurityConfig modsecurity.conf;
7 r+ c8 ~+ o, F" N9 w- n! p, X6 j, T' G
root /web/wordpress;
6 |5 J+ i8 G; K+ l: u index index.php index.html index.htm;7 x6 _5 m4 D0 x3 I0 G5 h* p
1 }# y& b: ~9 V' M4 z fastcgi_pass 127.0.0.1:9000;
8 D( {1 `. E& Z fastcgi_index index.php;
; @- G' _2 e. Y5 k fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
5 K# X$ ~& x$ o$ J include fastcgi_params;
: k' T; A _' X% N: ~ }+ C% K* L' K/ _1 F* @/ I
}' }& v* l* b0 z, s) n/ j+ d
upstream负载均衡:
0 {& l* v9 i5 R& |( G: q5 ], }0 `( v6 ^. h! {+ b; U, [
upstream 52os.net {6 N7 `/ n7 t5 z! q$ [ f% L& W7 V
server 192.168.1.100:8080;
- K9 F# n$ Y' `& t3 b* Q. u server 192.168.1.101:8080 backup;# n) |1 R7 u" G; W
}4 V) _3 Z- F+ N7 a
& g# r2 a. u3 @$ a5 Eserver {
1 ^8 a8 ]: g2 |' J% L1 `listen 80;% k# R6 S9 _5 l$ Q: @0 a, I
server_name 52os.net www.52os.net;
) I9 ]3 T3 ?- e0 e( l% A0 B; B! R. Y6 ^3 z! `( V
location / {
' D' h$ K: {. D' b( O ModSecurityEnabled on; : J$ O" v4 q# A, C. C6 B
ModSecurityConfig modsecurity.conf; $ \3 _2 M* M3 G: U4 h! t
" ]) L2 J* ~0 P3 P
proxy_pass http://online;+ m' T9 V4 x& I: Z
proxy_redirect off;
! B ^, o7 { Y. g" t$ c6 O7 { proxy_set_header Host $host;
! _8 X$ f( f4 [' H proxy_set_header X-Real-IP $remote_addr;
4 _% u: l1 S) _9 F3 v; e proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;* N- ^) b9 m% L) m6 K9 M
}
$ |3 L5 [" ?" ~! U}
9 A4 r, n3 i4 g# M% ~* s, U/ J六.测试. i; X. q- v9 [& }7 p2 X' | Y
5 U! k/ l0 n! D4 R( X! n我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
5 K8 [3 V- c% X6 a; [0 x) Z$ L( I. X$ M
<?php# u4 ~! O" c# a# Y, W9 Q0 s* ?
phpinfo();
. \) c1 W* [5 z. ?5 X' Z?>0 L4 Y+ O9 |7 S3 |
在浏览器中访问:0 P" S1 s% H5 A" h
9 D# s7 H) W' k& e: Lhttp://www.52os.net/phpinfo.php?id=1 正常显示。! W& W. E7 o+ Q5 v1 ]* {
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
9 [7 J$ ^4 V9 t6 \http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。6 T) A1 u/ a9 g/ j& _+ ^* e: r1 b# B
说明sql注入和xss已经被过滤了0 V; A, a y0 J- R/ L0 v
) B! @+ |6 W; k+ q
七、安装过程中排错 `5 w. o4 R8 w; w/ _# u- a7 u$ P
. W9 Z1 |( ^( j" M& ~1.缺少APXS会报错: T9 }. A6 b0 L r+ `& v; ^) z5 b/ {3 _
j# D9 C# @+ z" d8 _' h7 Kconfigure: looking for Apache module support via DSO through APXS4 K/ q& F: O$ y- D: q, h3 ]
configure: error: couldn't find APXS# _3 h8 {( I) l
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
+ l/ Z3 q9 g9 J; W* n! q" [; w+ R8 s解决方法:1 ^! I: O8 d6 Q/ H
, S5 D- \( F8 A/ z9 }5 J
yum install httpd-devel
3 z" R9 {$ q" G# J2.没有pcre. }, O) c6 s! W) T
0 J& v0 [8 I- k1 S- G
configure: *** pcre library not found.
1 R+ E6 ~+ L$ _3 x4 Gconfigure: error: pcre library is required# I# h" O8 Y* V- V3 t* K
解决方法:
6 T: h: y( U) ?) t6 L2 b ]9 v- t E) J9 n M/ w8 X; U& q k+ L* _
yum install pcre pcre-devel
0 p0 c! A3 F" L5 A$ [! ~3.没有libxml2
% @* b8 C' ^3 H7 ^
1 k8 f; R5 q8 Q/ O. m+ h- x( e. G' A8 k
configure: *** xml library not found.' A/ i( @2 P: r3 `# b5 N0 \
configure: error: libxml2 is required9 j$ a) ?( m2 L: u6 p
解决方法:4 u4 M$ w8 W# y' V
! F. @3 c8 H$ X) s0 qyum install libxml2 libxml2-devel
& o {8 ~. m' b1 W: q+ R3 Z4.执行 /opt/tengine/sbin/nginx -m 时有警告
" j: h/ Z, P. z! W5 B
; p. l( F- m% h2 P% jTengine version: Tengine/2.1.0 (nginx/1.6.2)
, t9 H0 x& |& H0 E; e; f( inginx: [warn] ModSecurity: Loaded APR do not match with compiled!' Q. B* k7 R# \; q
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log9 K2 Z( l& Y% F+ W& l, L
5 q% ?6 c6 y( n+ h0 W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
/ X1 `$ {7 h$ V M/ K9 ] O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
7 d2 H& m4 [, _( C2 t3 G' v4 _8 j2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
! Z' {( s5 {% A3 D2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"2 M8 j, m. Y2 `. B* o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"6 w! c4 W6 ]; N5 g6 o4 E4 z7 M8 m; c
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
9 u1 H. R- K+ }8 z3 r解决方法,移除低版本的APR (1.3.9) u" w7 G/ g- G; E$ o
% O& \* y, w- [* O0 F6 U/ R
yum remove apr* }3 i" _8 R0 H3 d# Y, A6 Y/ ^2 W
5.Error.log中有: Audit log: Failed to lock global mutex% F% d' u, Z, o
1 g" }/ G3 Y) i# y2 @! }& g2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
+ b7 \4 S ?1 [/ M/ Tglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
# Y9 Q6 C) S( L4 x X解决方法:0 E' n* E5 u3 }
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:% J, R9 f5 `0 ~) Y
" c: ^) L8 O0 u
SecAuditLogDirMode 0777: @4 `+ W" I" ~! `. }% O
SecAuditLogFileMode 0550
6 _' ?* o O% z/ K; r9 VSecAuditLogStorageDir /var/log/modsecurity
: \5 w, G& I4 L5 bSecAuditLogType Concurrent
+ o' @& R+ l% U' G: P参考文章:/ O- D) |# G% B4 X5 Q) T6 S
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX% D, }0 q$ z4 U: D+ j0 z
http://drops.wooyun.org/tips/2614 |
|