|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
& }$ x9 j. g( w6 v+ S
; _7 D1 P V, ?9 O一.准备工作
! t: e, p! V6 Q- |3 M: [
! b# Q K( ?! W系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0" C2 u& a. |+ r# J- ^0 l! f
# |$ D4 N: r) B& Stengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz* j4 t% s* a% n& y
. u1 u+ `* l! W1 C" U+ ~1 _# M) d. amodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz' n- p; o9 z* K/ t. F5 E* A/ R
4 a: A! M- H) v K1 U3 ^OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
6 e9 {8 c1 b7 v. j
7 u6 W1 g$ j: K- d' X' j依赖关系:
0 S$ E% o! K# I ]tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:8 D7 E+ j& i" b
( {2 O8 Z; |2 Z1 ]6 H, f+ myum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
3 g! p8 R* J6 c; r- ymodsecurty依赖的包:pcre httpd-devel libxml2 apr
9 y1 v1 u/ x. N# n: e4 o9 j7 c8 ?0 I) X; [5 J
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
% B( g) _- A. c( z二.启用standalone模块并编译
9 C: W; l [) c8 X( T6 h# a" h1 H: C3 }! f6 t- R. R7 c. N+ @
下载modsecurity for nginx 解压,进入解压后目录执行:% \ W. U+ K w/ K# V. |/ V- a
; h$ }+ {* P/ y1 p. U" N
./autogen.sh! W0 j: Q" T6 F
./configure --enable-standalone-module --disable-mlogc
* `( {5 Z: K; k8 ?make
8 @* B, ]9 h/ K+ i* P三.nginx添加modsecurity模块
- J- b4 D. s0 |6 x
$ {) F( m) G* D3 s w在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:, c4 [ Z% H, ` h
8 k6 E0 H6 H$ U./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
$ a( v; X' [6 m5 Y4 W7 E tmake && make install
! L; u1 k7 j# A7 I+ {四.添加规则4 e6 N. z4 ~. ]' B* r/ W) I C8 Z
1 S' l, n# B7 r
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
& @" K- D% T6 I% ]( Z3 ^2 j! l$ j% S$ n n$ A% b) i
1.下载OWASP规则:
0 o) R- k: Q. j- I3 R
8 o: J8 V2 H! rgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs5 }$ M* M+ y3 [% ^
0 n3 H5 S( C3 T4 B; x3 T) Q
mv owasp-modsecurity-crs /opt/tengine/conf/ ]/ a9 {- A8 @3 b5 Y" R1 e
: b; i, I! J* H( O" Q! E: j
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf( F/ A0 @6 @% A* v& L1 Y% S _
2.启用OWASP规则:
- G' A; ^8 G( t, p. V# ^3 E
6 K& `" m1 Q) S! t1 w1 H9 {! P复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
. u' {4 U& X$ d9 ?* k8 R; w2 F; z' T6 f
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on! }, D4 j+ p' t! P9 e
1 H7 z9 D* B6 H5 R; kowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。" Q, ^5 X! {7 {9 }7 I. g! Q
2 v- J4 K; f4 a: IInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf5 b4 l: _8 q& Z/ L; N+ N8 _
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf( q5 g |/ b% C% t
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
3 q3 X* T4 b0 b; FInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
1 B; O/ A$ ^# N4 D0 qInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
1 H) }) W! y3 k* o4 Z" R. F5 n ZInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
. D) _$ F: M% P LInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
3 u6 o1 M6 I$ I2 z! F' Z五.配置nginx
. Z2 d) y4 T% v6 V- }; ^* K; K1 E. ?5 O
在需要启用modsecurity的主机的location下面加入下面两行即可:
5 d [, ]6 @. o% m" G9 m" z& d) M- @0 G. w
ModSecurityEnabled on; * K, l* Q* P' D |+ d
ModSecurityConfig modsecurity.conf; c$ Q9 d M1 x0 d7 r6 e D
下面是两个示例配置,php虚拟主机:
: }9 D: |4 |* d. z3 `' z
$ L# v4 [/ v$ p$ J7 g' vserver {
, e9 N% Z" E2 `/ j9 [ listen 80;8 ?6 i0 _. f" Z+ ^) i
server_name 52os.net www.52os.net;2 p8 A$ V6 j9 C& y- K% v/ Z t
; |% n. A+ m& e4 A location ~ \.php$ {8 ^- ~8 G+ y Z Y2 ^. e; D
ModSecurityEnabled on;
' {$ e% w0 s P e9 `9 b ModSecurityConfig modsecurity.conf;- P$ |% |, c$ A1 V
' H) N3 `# s. d7 a9 e7 v
root /web/wordpress;
7 v4 d7 z! J, ]6 J$ a6 r# E index index.php index.html index.htm;/ g; H, O0 R% K7 ~! F
4 V7 F8 t2 N7 U$ M+ ]6 ~; J
fastcgi_pass 127.0.0.1:9000;
0 e4 q! d" T) V& ` fastcgi_index index.php;- V% b$ s" s* [/ s5 M4 ]3 [
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
5 v6 q8 W) w" F4 g4 Q- L include fastcgi_params;" _- T8 I0 I8 N7 G. ~, ]
}
9 [( C+ J k0 K& o5 ?. B' h }9 w0 [) h/ `3 `2 O. v. \
upstream负载均衡:7 c& i% Q8 d0 x, ~. j4 z
D/ o6 D& a# d2 S8 L8 L; L
upstream 52os.net {
w. r* J$ R& l& Q0 n) ? server 192.168.1.100:8080;
9 F1 z2 l6 ^8 Y& y7 y0 F( q server 192.168.1.101:8080 backup;
( J9 [8 N3 y1 ^& \8 @6 {}
9 K& H$ }6 r: L# S% e$ v$ a9 S$ B4 n) W* R; F0 a) o
server {4 m3 J: _7 ]) A. s8 P' ]( m
listen 80;
3 p0 z+ \2 D0 x k% T1 ]+ H* lserver_name 52os.net www.52os.net;7 y8 v+ o; i* g8 N
6 c3 r- A5 H+ A' D, f
location / {2 J- f- Q6 G% m: p
ModSecurityEnabled on;
( i! l- k! u' F. C9 }! \ ModSecurityConfig modsecurity.conf; f7 h& y! o: m p% S' W, |' e
( X' b9 M8 z2 ~
proxy_pass http://online;
0 p' I2 F {6 s0 I4 W, ]% D2 b proxy_redirect off;
1 }1 d$ O, U2 B2 C+ X7 Z proxy_set_header Host $host; X1 N% _/ t2 E2 W! _' y
proxy_set_header X-Real-IP $remote_addr;
% C( Q( `; V* R$ Y5 l+ b proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
0 M) \5 R9 W/ ?" j; Y9 N }
2 _0 N+ Q% }: y% b) A}2 V3 ~1 M9 P" A3 p
六.测试3 E1 Q, a G0 j9 I
( u; e% @8 z% v, {3 a
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
9 U5 w$ q. q, ?0 C' N! P2 e7 k1 ]8 T2 f% ]
<?php
& b: r) K- O0 I. D% E/ x4 i) N phpinfo();
! K& h) \: v! |( V6 R3 \2 m?>5 C3 `+ k1 H9 o$ B# H( m) V; Y
在浏览器中访问:3 G5 B4 ?8 y& F* i" S
: f& q, r" Q9 V' Q1 f& V# A
http://www.52os.net/phpinfo.php?id=1 正常显示。. q; m4 L6 j4 k' P
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。3 _ y9 ^) ~8 q' Z# g% ]
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。. k' T7 N2 O6 g* j5 |
说明sql注入和xss已经被过滤了3 ]0 `! k' ^! Q* c5 q# `: j
0 E; x9 @% h, h8 y8 G8 W七、安装过程中排错
$ K/ O; O! z* Q( j
( T- w0 |5 E) H+ s) Z4 ?1.缺少APXS会报错
) v. S' z9 F2 N2 z2 K
% A0 V& Q6 v7 L' Z0 [/ j/ Aconfigure: looking for Apache module support via DSO through APXS+ w6 Z0 O8 D! n: c; B. J- n4 _
configure: error: couldn't find APXS
6 p0 Q9 E; Q7 ]/ M5 rapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
/ R. e+ y7 Q3 r+ V7 B解决方法:
/ d2 |/ ?, [) x- _3 X& |/ Y( a/ U: f4 V h
yum install httpd-devel
}; C5 V* |0 i2.没有pcre9 i L2 O; k, o# z$ Q8 m
$ F/ a9 o- n9 f Yconfigure: *** pcre library not found.
f I5 L% H- ]6 `# j* ^* {8 U; econfigure: error: pcre library is required
% [* m ~- Y5 t4 t! s4 B0 M解决方法:" c' U- ^( K, A
+ r1 R$ ]4 z* v, r/ m
yum install pcre pcre-devel
7 L4 {. Q6 V+ r% T' B3.没有libxml2$ B5 Q( r# H6 P) T$ U' F
- u/ X+ u2 L& v/ {# X( I4 r$ Y4 z
( X- O3 f" _. r: jconfigure: *** xml library not found.& F& C5 j2 s9 q9 @4 ^9 _! P
configure: error: libxml2 is required
/ B' k. e/ v: ^$ V# m解决方法:$ V2 h7 ? W X
4 ?2 O. }3 l( L0 N4 y4 }/ z2 I ryum install libxml2 libxml2-devel
! a7 C# B: a' s. X, ~$ R J4.执行 /opt/tengine/sbin/nginx -m 时有警告( d( A1 S' |3 B) C
, p# r4 J* q! l' G1 sTengine version: Tengine/2.1.0 (nginx/1.6.2)7 c! m' c+ ?- p; h i+ S
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
% I: P6 y$ q4 S$ T: N2 j; N原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
n6 i1 R+ `1 b- M: ~1 }3 y. l
. b! E; E4 n. ^3 b+ l2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.. h% |* C- w* m$ k- Q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"2 G( Z# ?6 t9 E
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!# \' G& Q/ V/ ?* U$ N f9 ]
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
3 E6 a4 ^% ]8 C" O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
- R1 \4 Y- L t. g5 K) \9 U" u2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.1 v9 X1 N: Q3 J' C: p9 a! Q
解决方法,移除低版本的APR (1.3.9)
1 |2 f% S6 y: ^8 K0 E: x( B, {& y- d
yum remove apr8 o3 E: b7 K$ J7 E( @9 R% n% R8 y
5.Error.log中有: Audit log: Failed to lock global mutex
2 E! @) M/ _5 D: c, Q0 ^" |
6 z" i8 K+ t. i2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
, P' G3 Q- l4 @7 M, eglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]5 c8 K% x4 ?, @, I) h" f; I5 l
解决方法:) \5 q: |' V! U& }7 c( N$ w- [
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
+ n2 f4 M4 C; T( R; k7 b0 l; ~8 x
% ~3 n5 y5 F: a) aSecAuditLogDirMode 0777# `; N' B/ U/ G* X5 V! R5 v
SecAuditLogFileMode 0550* P6 s# V: `2 S2 x r0 s
SecAuditLogStorageDir /var/log/modsecurity
: t# K3 [! o2 K0 f% JSecAuditLogType Concurrent
; V* R, J0 G: i" M6 `' M参考文章:( g* h2 a: a j6 s
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX" A U( D7 f0 q( k4 W
http://drops.wooyun.org/tips/2614 |
|