|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。3 G6 Z# c1 {+ B3 m4 t
* p$ ^6 L/ Q7 T- S1 `8 Q2 S& L
一.准备工作! \ A4 H* [8 k' b8 M. W8 T
5 }3 S! O- _0 [- i- O6 g' m系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
; E3 E& K9 c' f
8 X- @5 s8 }; W* p0 otengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
8 \% L- g! }& J" U$ W4 ^
0 A2 U/ ?1 m* m6 C+ }) E3 |modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
2 W" S: b0 W8 @2 h x
" Z$ u! _9 J5 a. O, wOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
# P. K! a" E* M$ e9 C- g' }' k' g8 A) O) u8 b
依赖关系:7 O, G3 e; k% u* |
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
1 G- f: z9 H+ N
, @" k @- [$ h5 v1 E; Kyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
# {# ?* Y+ Q4 ~% b5 a# [* Qmodsecurty依赖的包:pcre httpd-devel libxml2 apr
0 U: L' e0 U" \: B- L3 ^4 ^1 h
' g; y2 E7 |! n4 _1 U2 V, \yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel# i4 W) M0 \" w0 M# u) P" a
二.启用standalone模块并编译- o" q- I" D' F5 L# K& f1 L& g
3 x1 @! K! P& [( Z$ ?! _7 t下载modsecurity for nginx 解压,进入解压后目录执行:
3 W+ V& i7 C6 P2 I* q p) F9 s( t1 t& o) K n3 R( u `' X/ b* J
./autogen.sh
& u- _. J9 z W) H./configure --enable-standalone-module --disable-mlogc% ^! w2 X5 `9 l. ?$ p% a
make ) C# G9 d9 E$ i* f5 M
三.nginx添加modsecurity模块
) y7 a- S& A- l S% {
9 J, m/ p% _; a7 T! j( V; k在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:* X# T( R: { R7 g' @1 h
# B+ t& A; n9 v! n) H
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
2 e2 w+ |/ |% O- y Vmake && make install6 m& V% }7 U$ G) i3 z" r% a
四.添加规则7 J% W9 K, A* S3 C! H( a" E
# Y) h7 q( y# w* d$ D2 f) Cmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。0 ]+ D/ y* T3 F
9 B: N5 e8 f; Y6 ]/ C& L
1.下载OWASP规则:8 U! j3 N" I6 h7 j" |0 J
' P# S2 d, z/ V3 c
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
' Z7 D9 i* l/ u$ K7 g) j2 E! B+ j5 Y0 D6 a
mv owasp-modsecurity-crs /opt/tengine/conf/
, Q( d8 z2 c1 r3 @( c% F0 L8 i# r) y/ b5 d A; a! d
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf( N, d+ W* F' Q2 c- b( x# O0 Y
2.启用OWASP规则:
3 S4 U) M' m7 a6 O' _5 w# F1 r; Q+ x' Q* E5 p* K! N" l
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。1 P {$ v* `5 w( _/ y
9 \* a$ b( O m
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on. K) {' Z% g' \$ ]
8 l8 o* _5 Z% howasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。8 W8 ~7 `) m5 e3 R# o7 q
y2 Z7 _ ~# `! F7 j
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf5 h) \6 M$ n7 z+ O( z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
+ [! f8 S( O! T. r( ?Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf3 T( H* K4 r: T6 u1 _3 l/ B1 p
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf6 q, ^8 J5 X- q. a7 p5 W I3 g
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf4 L: ?$ N ~0 K+ V4 W) Q) d0 K/ j
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf+ a: K3 G; M% W/ L8 ?
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
0 z5 N- w2 x. ~4 J1 j五.配置nginx& x" R- V4 s2 x5 U7 u3 y
, J: L& q; U0 I! k在需要启用modsecurity的主机的location下面加入下面两行即可:
9 F. Q/ A+ v% V% I9 t2 \# t1 ]
2 Z/ S2 s7 F0 uModSecurityEnabled on; & T+ R1 X5 a( ^/ N; M0 |: A' t
ModSecurityConfig modsecurity.conf;
4 `8 X/ K9 U8 u下面是两个示例配置,php虚拟主机:
4 b) z3 G! B* h+ f7 y, i4 z
Y' R' @ Z( L1 ?3 ~server {
0 z" p' T: ^6 V. \8 I& Q% V listen 80; |" U! z% ]3 r) ^, T9 S
server_name 52os.net www.52os.net;0 M2 g; M5 Q6 w5 V! W( o
( M0 \- M. l* H% t( q location ~ \.php$ {
- q- F5 V6 @3 o2 o# |/ m* V ModSecurityEnabled on; " k, y$ T* ^3 K$ \
ModSecurityConfig modsecurity.conf;
/ T* D7 ^1 r# J7 t8 t
8 [0 f( |2 ?: ]7 }/ a/ t: M root /web/wordpress;2 G/ f" l8 i: c. }1 Q/ k0 v
index index.php index.html index.htm;
$ v: l1 C+ W+ I, c+ a & N' v% r. r! u2 K* A# a, ?& h
fastcgi_pass 127.0.0.1:9000;
9 m" P! g4 x( O! J: o fastcgi_index index.php;
5 N* e# }9 |. h/ w Y fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;1 \8 J0 F. c+ D6 ~& P/ A
include fastcgi_params;
: V' g+ o0 i/ v0 A# x# [ i" j }
; @( z( B7 t; Y1 |( z& V0 S' R }3 O8 B! c( z% d& i
upstream负载均衡:1 B$ Q) J0 U4 n' c5 i
6 V: b* |) r. \/ r; Pupstream 52os.net {4 F6 A x( T: j
server 192.168.1.100:8080;
6 ^% r' j/ K( q7 p! | server 192.168.1.101:8080 backup;/ S! i+ B+ [! e l. d% t/ ~/ L
}) Z2 q! p% u7 M, z: O, T
* Q+ j8 Y) V# z4 E {+ s, R$ ]
server {
! c7 Q3 A2 f% H5 s5 S& U: o3 Mlisten 80;- R" z) K2 b! w# M: w. j+ S' L8 o- ?8 y3 R
server_name 52os.net www.52os.net;
. @! o8 } C2 X3 m* N( E
) @: {# e5 P2 u' llocation / {
" A8 @$ R) U* z" I ModSecurityEnabled on; - w! p- ^7 k+ \! A4 c
ModSecurityConfig modsecurity.conf;
" O7 ?7 X+ s% N, O q) f" Z' k/ \
proxy_pass http://online;
/ }7 ]# m/ g" I: A v/ l8 B6 b proxy_redirect off;
4 ?( u4 k6 s0 Y+ _2 D proxy_set_header Host $host;$ f" Q" G, Z9 e) L4 L; G' Y
proxy_set_header X-Real-IP $remote_addr;: ?+ ~% e7 t/ ^( {( U% `
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' q# @. `% |/ y
}
% u, K" M/ u, T9 U a} L+ m# [- c) g% c
六.测试
3 ^: U' _; e( h$ T+ f* q; @5 u2 @% s9 N4 E
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
$ Y7 q6 Q/ w. ?" [$ i# D- l4 j n8 |! ~
<?php
) `1 b$ F x* Z8 h5 }5 m$ \- } phpinfo(); , M8 V! B1 F' H7 f' s
?>0 J! V- u3 [1 D. I4 q% b
在浏览器中访问:4 M# d' g. ^4 P3 b* x1 L
- O: M2 ^) A' k B
http://www.52os.net/phpinfo.php?id=1 正常显示。
8 j0 Z r2 e, S+ Nhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
) S& ?0 E7 i$ K9 K1 Fhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。% i p9 S6 Z# z' R9 t' `
说明sql注入和xss已经被过滤了
, o" c" n6 T7 c4 I: i" V9 O5 u% e/ e8 p9 y5 `0 Y* a5 F
七、安装过程中排错) V, ]2 @, S" I9 H$ U
7 D# C9 B" A+ t# p1 B) g @ w1.缺少APXS会报错
1 d3 Z" }1 m& t) \! y$ m9 E Z
) `; C& D# A$ `, qconfigure: looking for Apache module support via DSO through APXS% }0 q( A- T' g) E- Y# t( K" ?
configure: error: couldn't find APXS" l5 W5 y: K7 }, k( [6 U0 {' f
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。 P+ B) D @$ ?! g
解决方法:
6 s! @: c' G1 [# t' t* ?5 F: i' ^3 m5 O, Z( `/ t& i" c
yum install httpd-devel
4 Q! O- f: J. S- a& D2.没有pcre
5 {. o9 d! q: G3 ~+ Q( U) s3 k8 n
% K* s$ j) b' C& Jconfigure: *** pcre library not found.5 |' r1 p& @0 y1 f9 ?* v6 M
configure: error: pcre library is required9 A" Q1 Q+ t2 G( G( `
解决方法:# _5 E/ h; f% G* O
+ I# }" q* T" X
yum install pcre pcre-devel) L7 h/ ?8 ~% y3 ?. k% K
3.没有libxml2+ I) o1 `4 j; \2 E. v
h3 f0 C. Z4 ~! k0 t
9 @- n5 |1 d. [! s' wconfigure: *** xml library not found.- ~: ~ y) t; S) T# t
configure: error: libxml2 is required* h9 M* ]8 b7 O
解决方法:# P. H# `; d! O
8 n5 Z1 K5 D! E( ]6 jyum install libxml2 libxml2-devel9 L& L# j- k$ K R7 c3 U
4.执行 /opt/tengine/sbin/nginx -m 时有警告
' Z0 u, z1 I2 ?6 B! @+ Y- r
5 L9 @: ~4 U# i, @2 ^: lTengine version: Tengine/2.1.0 (nginx/1.6.2)
, n, Y3 K6 k7 p9 U: dnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
7 b6 ]/ U3 h! [1 s9 W原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log/ L$ G7 \ w+ L$ R; g" U2 J
8 b% c) S, Q( N6 E5 o9 Q, l5 f" j* P
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
) h7 o# F! x5 ], x# H- O9 _' s2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"! G9 r6 Q7 N, T2 n! t: _4 C- [
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
8 m# R' x% D, a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"2 i: G7 e3 a: {! R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"$ c: y; |$ g" R& d7 m
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.& z' b% i( D; v
解决方法,移除低版本的APR (1.3.9), [2 A: N9 m8 r# m6 @& F
+ w8 P! @3 ^- e; fyum remove apr
6 Y) { ]2 _/ n- F- O9 w5.Error.log中有: Audit log: Failed to lock global mutex. |( i3 Q7 k. d# Y; y% w4 d
; n4 `5 Q. B3 _; r, j2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ; g$ s1 z) l q4 D: [
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
& v9 R \7 I1 H, W2 ~' [8 f6 \( n) V解决方法:
4 n# T: i% `4 T$ t1 Q0 N; v编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
4 {7 [# L* H+ J0 `& j
& a% D# O* Z. T7 ySecAuditLogDirMode 0777
D# k4 I+ H0 w: R( j4 NSecAuditLogFileMode 0550
$ F: g8 @4 `2 I2 J' W ?3 q+ bSecAuditLogStorageDir /var/log/modsecurity
c8 O! x4 T! r% ]1 dSecAuditLogType Concurrent1 ^+ B6 h; X1 p+ C, M2 t2 ]3 y
参考文章:
) F: X1 I6 y B; c# ~! Ihttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
3 W& M# n$ b! Q% X; Q8 ^! ]http://drops.wooyun.org/tips/2614 |
|