|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
" I, }% K5 {! |$ Z3 a+ u; t5 D9 b, }% v% D7 g# j ^
一.准备工作
& M7 f; n1 e" V4 `8 @9 q# r5 `4 q+ b: j+ s) l
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
3 I4 j$ F: K& n8 r4 h' N. ~! i( I4 h/ y; b" P. |8 `
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
! E+ X8 t' V3 J1 w1 Y8 o, A( d( F" J G( \
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz/ C" ^5 n1 ~; @4 x0 B
$ ]/ C2 t; y+ u4 q; I, GOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs8 C; a' }4 t2 u, N3 b
& B. h8 [" l, i6 p2 I依赖关系:
" s& I2 {, c2 F" W# W8 ~tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
% m; k5 _# `2 L' k- T5 r: J2 |8 P/ u, F
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel# h8 v- H, Z0 y$ L4 @6 b
modsecurty依赖的包:pcre httpd-devel libxml2 apr
N1 w5 D. @2 U7 e, j! x, Y
% _- Q3 p8 ^" Tyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel8 p; ]0 G9 D( Z4 Z3 p+ {8 u
二.启用standalone模块并编译9 E! w* c9 n3 K1 @6 W+ o1 M
4 y* ^; m/ ]5 X7 ~下载modsecurity for nginx 解压,进入解压后目录执行:( W! k8 O5 ^; |$ j
! G% b \, T: X3 ?1 E1 a; t./autogen.sh- r, J8 D4 V: A- [6 g9 _ ?# x, c+ T
./configure --enable-standalone-module --disable-mlogc. {3 s$ J, x, D/ Z/ d% y+ F! j
make
: W- w1 b0 z- C三.nginx添加modsecurity模块
0 v6 D5 d2 ]' K; e* T/ q2 x" }- I Z; J" E
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
3 o8 }* A4 \* Y" M
# G8 @" d; Z# u+ d' g) y& Y0 ~- ?./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine" b+ G) ^3 q6 X6 {1 D: X" A2 B
make && make install* v6 g/ [# k- h/ x6 |6 L5 W
四.添加规则- b5 Q( U7 X4 j t8 _
0 @2 z! {" L# P u! {
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。 k5 ]9 I. ~, C9 C- P
2 i7 ^6 s6 e8 ]$ q& Q
1.下载OWASP规则:+ `( A& ~7 |* r5 v3 H* W2 Q
* ~6 y% J# x+ G6 M9 [git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
. V- ?. v. j/ j% m
% n- c# }, F3 n5 j5 O) F1 _% t, Smv owasp-modsecurity-crs /opt/tengine/conf/! j4 J/ V1 w9 E7 k
3 N- R8 ]; l5 q5 ?& m: w* v. X, ecd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
: K* @* k) C8 ^9 H( f2.启用OWASP规则:. t( D/ G: j1 i+ G. F Q
* m8 Q6 T2 n! [% Y8 |
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。8 A Q2 G8 R! v" i! o
7 p5 t4 p8 \3 a( {
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on4 E4 X9 c6 C0 `1 D8 Z% B/ `& U
, i, ^! b/ e8 W' z$ A5 j7 ?6 `
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。5 |3 N* a5 Q/ ]' B+ B9 F1 M3 M1 h
4 z% `: n# ~8 a2 V* T R
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf- o: J: w+ U0 M8 }: b0 [# @! q* B
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf$ O9 P5 B: O! O. B9 o1 U
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
7 A3 S( f5 }, { o" h! @Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf9 T7 Y1 R; V/ X# o: K/ b$ A6 s) c* N
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
6 B+ v- ]$ o) A. C' r$ p2 BInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf* V" n! T& N0 N! K( |* {
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf2 F- L( }7 ~- ?0 P
五.配置nginx6 s) H/ l! a, R0 y
0 b: f) F+ B1 o* e1 q0 t2 r, W在需要启用modsecurity的主机的location下面加入下面两行即可:
3 X$ T; t. j/ x9 J0 t% I3 V8 i; ?3 P; B' a) [" v3 D1 e
ModSecurityEnabled on; 7 w. R6 U* ^1 W
ModSecurityConfig modsecurity.conf;
# `) G) q' g+ B6 s& H% x! Y下面是两个示例配置,php虚拟主机:
* m' R2 t, a* n1 ?8 ~2 n, L x
9 g2 C* W+ n: i; }# X! E$ Q& F3 Aserver {
! m; Y" j* q3 M3 o& G3 t7 m listen 80;
e7 X- K& t P( l5 _) w# l server_name 52os.net www.52os.net;8 V6 x7 [7 m8 ~
O$ p2 l. r$ Y8 k
location ~ \.php$ {
! j6 b+ _: ?! i6 c5 x* o ModSecurityEnabled on;
$ U& }3 X6 {. ~1 x! N4 {- X' e1 m: } ModSecurityConfig modsecurity.conf;
$ S+ F f! G3 }. [5 x& P0 L7 x# }% `. c* W
" S" O# S+ \) x& m root /web/wordpress;
4 K2 U: R, s# j6 _9 D( Y+ c" w index index.php index.html index.htm;
+ Y- L% w. o R 1 s; o U/ ]3 V
fastcgi_pass 127.0.0.1:9000;
' @' n; H+ S( K* n. i3 i+ v2 W fastcgi_index index.php;
a+ f/ `) t% @1 k2 R f fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
8 o$ p4 y9 U2 e( {0 m% U include fastcgi_params;
! R( u( Q/ v; d/ B) j7 t }0 m5 T- W' `" n1 ~, ^1 [
}
) N/ b" d! U0 J, l3 h' {1 |1 J. Gupstream负载均衡: |) e7 s) |9 t0 }
! T/ }2 m! ]* Y( B+ Mupstream 52os.net {% H" I; V1 v- w" D
server 192.168.1.100:8080;4 n- L0 }- X) j
server 192.168.1.101:8080 backup;9 a1 L4 |+ e t$ ~9 o% Z
}9 [2 j2 p- r# v: U" }" ]
" a3 Z* t; K" A( M" ^% Y9 |
server {
$ @7 f, z5 o2 c9 L) _2 elisten 80;9 s# _2 l2 x0 r
server_name 52os.net www.52os.net;
' C M7 n& b2 i" ]/ Q+ ^5 C& a3 r& s5 s( @
location / {3 A- _6 Z8 i5 S! p3 t$ |5 i
ModSecurityEnabled on;
; ?6 N. [: N) }3 l/ r& Y ModSecurityConfig modsecurity.conf; ) Y$ g! w; S4 ?5 d% q8 }; U
4 z3 P- u3 T3 n& J
proxy_pass http://online;/ X4 b" w0 ]6 e, L! f9 T
proxy_redirect off;5 e# h2 h0 s1 ^0 j: ?& V' h
proxy_set_header Host $host;( A; Z2 m+ v$ ^/ i* u
proxy_set_header X-Real-IP $remote_addr;4 p6 ` h: U2 y5 _- J* e
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
; I& k; z. w3 | }
6 y( b0 I9 r; b/ A}9 t6 R3 }$ n1 s+ r4 Z* [6 u
六.测试( O& i, r- s" J7 m5 B, L
, F4 m) K Q/ `. ?0 O
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为: E, h' \ {% n. _
6 E4 h! B% |# E4 P8 Q V
<?php
7 T* R& N1 o7 j4 O9 p phpinfo();
8 G$ \+ Q7 m# P7 I?>% V- v$ }$ }7 D: i4 K- j; h
在浏览器中访问:1 x! S& L) a, V
! b' z7 G( r( V/ G: D
http://www.52os.net/phpinfo.php?id=1 正常显示。4 R9 N3 C1 C+ n) u# @; H( a$ D
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。! j9 K4 N" u q8 j
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
4 {4 A: U% C5 l* D* k; [说明sql注入和xss已经被过滤了
8 b P& [$ I9 W7 L( C; f
, s% t( X# V* i0 H+ K七、安装过程中排错6 {: A+ v. j( _+ _
; q' c) o( @1 K- f% ~! H( q! e
1.缺少APXS会报错' E' X5 q. V1 e) }3 p. M
+ a# {6 o; W s7 l) u
configure: looking for Apache module support via DSO through APXS
/ `9 {0 Y/ z( [8 Cconfigure: error: couldn't find APXS/ L. C+ `! m* n
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
F/ v; L7 N, z; b0 `' g8 b6 Z解决方法:
q$ u" U5 T. M, b1 H
6 t' C U/ I. ?5 E0 `3 q7 oyum install httpd-devel' ~& j* g) z! q! I7 H/ }
2.没有pcre
& {/ p- S3 s( N( h" B( ^' X+ {) W
, P# K6 F. [# A1 B' l, pconfigure: *** pcre library not found.
7 K, B3 _, k- @- h4 M, b. C: ^configure: error: pcre library is required
( ?3 v2 P2 s6 A, A: x解决方法:
0 l, E2 ^: g0 K& o2 O$ C9 V5 H5 q0 E; J2 c- s& U2 P* a
yum install pcre pcre-devel) R5 _: s* N5 k- ^/ [
3.没有libxml2
8 c. r# H1 r. ?" g* I- c$ h
8 ~8 i* j! z0 g
' }. w! K! N: k. G, o$ t$ g yconfigure: *** xml library not found.
" E! u/ B6 A V4 V. bconfigure: error: libxml2 is required& A, {) o8 J+ z$ R6 `+ D9 p
解决方法:
! {6 i% w# Q. ?7 t, N
6 B# Z9 A/ `; D2 A$ n: `' ~9 N' T) Jyum install libxml2 libxml2-devel
\, l& k* W. u! g5 `" G; [6 c4.执行 /opt/tengine/sbin/nginx -m 时有警告0 L5 O* D# P6 J1 K
8 X2 q' A" {' a1 P8 `6 [: hTengine version: Tengine/2.1.0 (nginx/1.6.2), Z# x0 h: }* H7 R1 r! h; o6 y4 G0 ~
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
- y& r5 h& M; Z+ A原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
# r. j/ y3 v7 H" W7 @' @
- E- |; @) a" s+ j4 _4 H6 F2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.. d4 l. t0 }- C6 z% ]
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
4 s% T1 M4 m4 y9 U5 g- v! L2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!0 R& f: P" f: }% j! o5 Q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05") E* k* P2 i7 _3 }. U
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"7 `7 L2 R4 E/ H. C+ S
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
( T2 J& R0 o! [4 H解决方法,移除低版本的APR (1.3.9)! y2 t$ \( d# }7 v4 ?
/ x. p8 T( _( }0 A( R% Y3 B/ yyum remove apr5 X+ W q8 e0 e) W" w
5.Error.log中有: Audit log: Failed to lock global mutex K `! S, r3 b
5 j' k( f$ O/ S# y: G8 E
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 8 Q+ n$ A# z& ?7 |2 r6 y8 ~
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
1 K, {( J' [/ j解决方法:# f i5 `# J, _3 [, X$ T
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:4 m- |" D. D( H$ s2 g
) `" |7 d I( g# G
SecAuditLogDirMode 0777
) I( ]1 V+ P0 _% nSecAuditLogFileMode 0550
! w' S! v8 g& }9 I- |- pSecAuditLogStorageDir /var/log/modsecurity0 B, t5 f, K- U& Z8 D7 {
SecAuditLogType Concurrent
& P, z( \8 |$ [+ M参考文章:
! a$ \! B/ b' U! |) ahttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX3 Z. X [/ Q$ F, U# M( p+ t
http://drops.wooyun.org/tips/2614 |
|