|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
" K& Z0 N; f8 r: }) x: `8 e9 M5 V M2 }6 Q
一.准备工作
& ?* F T8 O, }; T; b; I1 v6 T( b- |3 ~4 \9 t2 B
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
. V( S' A# f/ w3 X6 x( M8 `
* O4 b( {3 g: A! Etengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
5 x4 l0 } w1 X. a- {$ T1 J9 u/ T/ ?8 x
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
* F' }7 b) b1 W& R6 I4 }* M) J5 D+ u' m `
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs. i/ {: F, U- I; X ^& M
# Q. n: _3 f8 E( |依赖关系:7 s) u" m' `2 _* I, ^: K( F
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:/ R/ o( S) g ]7 J, t/ d
+ I5 C. s R- W4 o) t2 R# Ayum install zlib zlib-devel openssl openssl-devel pcre pcre-devel: K* q) q, G# y/ L- X
modsecurty依赖的包:pcre httpd-devel libxml2 apr
* ^- ^0 _- f7 S* I# r: d' _1 s" B' @8 F+ P/ _9 S2 o2 ^- I
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
! _/ K2 e0 o; y7 _二.启用standalone模块并编译
6 l% L: O) E% `, Y
, q/ D. n% l- }# l4 C; Q: T0 Q7 ]下载modsecurity for nginx 解压,进入解压后目录执行:
/ }" J/ f; ~+ n% N- S$ A, `4 R
1 G0 d$ F6 y' V" C) b* W6 j0 @./autogen.sh
; J6 i! b9 ^: k5 _./configure --enable-standalone-module --disable-mlogc/ X3 ?$ [) a# v( f! H
make
6 n- N) u+ k3 u9 U三.nginx添加modsecurity模块
2 d5 W Z# X$ \, }; k M2 ~* e) }3 E1 O
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
. K( ^ v* ~! w# q# m) [- T. j/ P7 ?! v* A+ R
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine$ h: P# M' ^+ [- s
make && make install+ h& _+ ?/ P. \3 `, ?& N2 E+ V
四.添加规则
' f$ g6 Q: v8 A' X1 B! X7 }7 y2 Y$ U( j2 x/ C6 p1 z
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。* X7 ]- I7 E! P6 f
# r3 S6 r$ u3 ?8 j. j& f1.下载OWASP规则:9 \9 i$ V- t4 F* a ` f
' p# M2 X3 _5 D/ _
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs; i7 ~( C W% N
/ A3 S7 S. P0 ?* D3 j8 B+ u
mv owasp-modsecurity-crs /opt/tengine/conf/
5 e' Q* k8 h, P$ D C7 ?3 e8 D9 D2 M; Z: D# T! g2 n
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
; q' f) N+ K* B' I+ I. S5 y5 e2.启用OWASP规则:
8 }( J a0 ]$ F6 H4 f7 {7 Q/ U: G8 Q8 w+ Y. b& h+ l9 L) R8 o
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& x+ O s! \ N; k
1 F: D' e9 `% l编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
' W; @& E4 F& q8 h6 l2 p
2 d* T! \ n5 D2 vowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
; Z4 w A: k% M& s4 ]$ P) m P) H( k7 @) E" U! [1 G
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
/ J' A0 D6 t1 q/ b" X3 ^9 @Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
( w/ o& z( y. J+ j+ a. B3 HInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf5 {3 s% c% Q( q7 s1 y" ~
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf! q: ^/ s \) i }5 s, C; b
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
5 v% P( z1 w: i/ x0 m& {2 c6 [Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf& X z3 c* Y2 T, N
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
% v/ T: }2 a9 O* a; r. j: S五.配置nginx
5 C8 O, {; c6 r. B
2 e7 t) ^' L# ~在需要启用modsecurity的主机的location下面加入下面两行即可:
' S2 X; T+ a \# }2 j
7 A; _/ w/ l* s! sModSecurityEnabled on; 6 `5 x' @) w" G7 q% v( }' M
ModSecurityConfig modsecurity.conf;) u) c0 M( }; C0 p* u* x
下面是两个示例配置,php虚拟主机:4 n# b+ Q" V# G
4 I1 q; k2 y; r9 |1 H1 i0 w" tserver {1 H4 W5 A* m" s k
listen 80;- }* }+ _6 h, [ b
server_name 52os.net www.52os.net;
# W, _. |4 A" r8 R& U/ K
8 \6 B3 K( i# F& l( W( D location ~ \.php$ {2 E6 M; C* n- ^; c
ModSecurityEnabled on;
8 Y8 o, r% q) m ModSecurityConfig modsecurity.conf;
) t( L/ p# | d3 q& T& l1 @+ N% `, y4 N& ]% h$ T8 X
root /web/wordpress;( M, J7 s C z- ~# ^# T. G
index index.php index.html index.htm;
5 B% q- n* @. a; P$ i0 p
. l6 _% x# g) [ b) L8 c' @ fastcgi_pass 127.0.0.1:9000;+ @5 ]' v$ [0 J2 R. p4 P
fastcgi_index index.php;9 s: D* `$ T3 i/ _! N
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
" j( I: \! W( Q* G/ e5 | include fastcgi_params;
1 g2 @( K. U; j% f5 C+ A }4 a! d/ \6 T# B/ u: [/ e
}; k/ V. Y4 J3 S* c, G1 p" ^% W
upstream负载均衡:
' J5 B) z, L0 E2 W4 U+ K+ q3 r5 a
upstream 52os.net {
! }4 P! G. F& @, B server 192.168.1.100:8080;- s; F# ^ i3 g+ ]2 m, v7 P
server 192.168.1.101:8080 backup;& I6 i& `# n, H- x
}
8 W8 [* |& e# @
( j; R, M& M+ D* qserver {
% G9 l+ a$ e; z1 r E% Elisten 80;
) l5 r( k/ i; X/ k; g7 yserver_name 52os.net www.52os.net;% f2 D- T& g" B y" P6 \5 |
7 j# {, _7 p$ i6 L
location / {
! C% B. M, M) V+ W, M+ C ModSecurityEnabled on;
% w! R* ]7 L. @5 S/ ]: h F ModSecurityConfig modsecurity.conf; ( @4 G1 u/ ]: W3 Q1 H6 b& U
! r6 t* i7 ~! J9 H/ [7 L proxy_pass http://online;
, {: S1 j& h" L- J9 d% l& `; K proxy_redirect off;5 g3 S ~8 C1 o
proxy_set_header Host $host;
U. y0 w$ |- u5 } proxy_set_header X-Real-IP $remote_addr;
7 C( t7 p# i" o$ _0 E T ?6 M: i proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
. ~& u8 p/ Y0 d }
# J1 b% U2 C. b) I) \* k}5 u; ~5 A$ L N" e3 [( W- X
六.测试
$ Z- Q) k# q% p2 ?4 W* R ?/ X4 T8 u0 B0 r8 w
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:5 }' k, w1 c. Y
8 {$ C; C5 m7 e# @" A, W7 `<?php
% [% T7 Z. |9 C5 R8 k3 ]8 H phpinfo(); " b& B' O, J# t
?>8 u: p; Z( G$ _- |
在浏览器中访问:0 B; b1 r' l K$ e$ f ~4 [9 s
' X3 Z2 ?; V) J6 _http://www.52os.net/phpinfo.php?id=1 正常显示。: Q; ~- X7 q# [: W- Y8 }
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
# O3 o, p m2 k4 v! dhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。" W/ s7 y. Z1 ^: a& {
说明sql注入和xss已经被过滤了
( l* y ?6 U5 ?% D4 W0 x- o6 _* k: T! m9 x$ O
七、安装过程中排错
2 ]$ d5 z f0 y
# Q: M" Q, X* Y" _1.缺少APXS会报错; l n# I% k! V! n' \
$ [; m p x& M4 i" Sconfigure: looking for Apache module support via DSO through APXS- b7 \% Y4 f7 s, ?& n, O8 t5 b
configure: error: couldn't find APXS
! c) F4 R% U& x0 ~2 B( lapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。+ J- Z3 ^0 c$ \' T
解决方法:
2 C* b! `, L4 e; ]5 U! N2 c
" _( s( F/ \1 H8 y$ @) Xyum install httpd-devel5 _0 L( r0 R4 H
2.没有pcre
+ i4 h. ]" J& T% q0 @1 b- ^% b2 ^. {7 ^4 Y
configure: *** pcre library not found. L) U- L, V0 d$ T& s
configure: error: pcre library is required
+ [! u9 e' _& g( g解决方法:# v8 B! u* ]( w+ m- q. ^) _' W0 [
( k# q* @* A7 s2 Z9 m& C+ Wyum install pcre pcre-devel
! Z/ A. P$ W5 s( c! c3.没有libxml2
" W9 X# ~' d/ [, \5 h0 J
& {8 A' |$ I' }) Y0 Z2 _" h& k1 t/ G3 f6 D
configure: *** xml library not found.
4 Z8 |9 B+ h0 q& G$ T1 H. F: Kconfigure: error: libxml2 is required
6 x$ }* G$ d$ C6 A n& R解决方法:
9 l. j0 R. |; u/ J) m! x z/ W
; G; S4 g# ~( z4 g7 n R7 F8 I) Eyum install libxml2 libxml2-devel& o! C" e# o6 }3 x
4.执行 /opt/tengine/sbin/nginx -m 时有警告: {; g' q6 Q; `: L" J
7 c* u( @2 }4 |7 C: Z q/ STengine version: Tengine/2.1.0 (nginx/1.6.2)2 {' c8 m" x0 c
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
9 u( o" R V5 V6 b( e9 m, h. c原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log% ~5 _7 l* Q5 k1 T
: t/ Q9 X; }0 K4 y0 D2 D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
( k( N a$ e: a$ [2 }0 T( Q0 T: K2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"3 A: S3 J8 {* W0 [ K
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
9 ~& O' n; P$ ]/ Z' _4 o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"" \# D4 i9 g9 l9 \7 ~/ S
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"+ W6 M& O: W% S- _! e* M( c5 p
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.& t% _ K* N$ ^4 B. _; M* e0 X
解决方法,移除低版本的APR (1.3.9)5 X; v+ f: S( w8 ]
. C5 P, ^. g* K% j
yum remove apr
% n( p& s& P$ g6 T; ]5.Error.log中有: Audit log: Failed to lock global mutex
7 y7 }& S. v0 R; D$ |: Y* D! U) @0 ^
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock $ c" B7 \' h2 }0 W$ f! J X
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
0 c( h3 r& s* x- Q解决方法:
$ W! g) [, @; a. H2 X编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:- `' ~2 W4 n' a5 t o
* ^ e( o4 R$ _7 QSecAuditLogDirMode 0777
$ ^. ]; u& E* @6 g) u/ ~SecAuditLogFileMode 0550; f6 }3 p' m* g* ~/ w) R
SecAuditLogStorageDir /var/log/modsecurity% h1 r: K2 }2 C8 A. F) ] a
SecAuditLogType Concurrent
8 z4 {- b) M& @: E& B6 o参考文章:
9 ]0 K) g1 C) I. H% n" I: ihttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
" l. s6 d- o0 y( j" |http://drops.wooyun.org/tips/2614 |
|