|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
/ b0 {+ H! W9 ~! ]: H7 n6 n( C2 K6 V5 Y+ m
一.准备工作& ?8 v+ X, m2 Q- {( t+ N
7 ` i: ^: Y0 n, Z7 w2 j系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
0 ?7 A: ]1 u; t9 m8 M+ J7 G8 b; S, F
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
% h% X7 l' ^/ f" b( D: t3 y
) s: T" w$ C, L9 o Emodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz2 l b. l, z# G% {/ T0 I" D
% ~. u$ _! A" a+ i1 O) J- {
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
7 y; ]% T _: a# V! `! P& a
3 r6 [0 \% i4 K; A% \9 o依赖关系:4 q) F! A! w! ~1 Z% K3 e+ u0 E
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
7 e- |* Q& H2 ~4 b L" k# t$ T- ]8 O$ a7 J8 u+ _9 |; t% s
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
. M a, D" y3 {modsecurty依赖的包:pcre httpd-devel libxml2 apr8 o. L9 k1 `* S9 ]& S, n' L
% F- C8 N: x3 E% n& i7 O
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
) M1 N2 n- }& h* k* m F: d二.启用standalone模块并编译
- U3 t* e1 n0 P: D N3 f
4 ]8 t; d+ P" X; w( C, v6 B; D下载modsecurity for nginx 解压,进入解压后目录执行:' O! z6 l( ?0 f' f- Z
: Q1 W0 ~+ R- ` c7 e- A/ H9 H( ]' T./autogen.sh
, A1 V$ o& P$ t" V, U& x./configure --enable-standalone-module --disable-mlogc( {* E5 Z1 q G
make
' q& ~/ u; n, I# b2 D三.nginx添加modsecurity模块: b) P% g* j- V9 {
- d9 l r# ^ q, z8 I$ b% O5 X; H
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
% |# F$ G) C+ V- r6 ^
7 q) E6 B0 D x9 w8 ~6 q+ H g./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine" P1 `3 g. {1 {7 M8 D e
make && make install
3 v$ s1 g$ [! A4 ?/ [/ O5 |四.添加规则. M( p3 W7 P5 d" P: R5 H0 v. W
: Q7 m% L& U3 J# e, |modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。* K( L) d; \% o9 @& M) ]5 I* | T
& _6 E. m" M4 @- C1 A3 @1.下载OWASP规则:$ J- `1 o4 l! A6 e, P3 }! s0 W
9 a+ v/ G. Q5 k8 w$ G' r
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs/ B7 O1 c8 L9 |. h& p3 e: h8 W
^) {4 g' K X! [2 u' {# ~; X& l
mv owasp-modsecurity-crs /opt/tengine/conf/
, Q# U5 S* b! a0 k4 J, {
$ l: Y- H$ b/ e6 K9 V" qcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf3 ~, d2 g+ t, [) L
2.启用OWASP规则:4 d" n+ o4 n& B! B8 u% f% |
( P9 l) k' o; }$ a$ Z
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
/ R* x3 M1 c3 T* t4 B6 L: k9 ^4 Y) B% l" Z# P* Q* P! S1 h9 m
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on w9 P) C/ q ~# ?: `% U1 i l4 ?0 W
4 j; G* U. @5 g7 n! T3 t
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
4 S4 h4 o6 s' N3 p( n# Y7 ^8 a4 q# N. q
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
- Q6 j' u' a$ _3 z7 QInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf# } I+ e& q. G
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf- d- S3 c: M1 u* V) o
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
. _: U4 T6 g- w0 ]% B# ]# IInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
/ L. ]; X+ K5 F1 `& H MInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf- J# p4 v0 F+ E6 _9 f3 E
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
* [6 S& g8 `8 B1 X五.配置nginx3 ^; J3 V; x( m1 b7 w/ Q1 v
, V# O5 X- K' s7 O在需要启用modsecurity的主机的location下面加入下面两行即可:
3 r- T# c& [3 y% ~) \% X" {$ U' H& \
ModSecurityEnabled on;
8 ^4 @- F9 `5 Y2 S' z' |( Z& _ModSecurityConfig modsecurity.conf;0 j. u; k6 n% y! G5 K
下面是两个示例配置,php虚拟主机: c3 k2 ?$ [. q \& V5 [) X$ Y
4 `. w4 \2 a9 u2 p3 Zserver {$ Q& |- a* H$ s3 }
listen 80;0 h& {2 A" |, H8 }, S$ _
server_name 52os.net www.52os.net;5 }1 P& C, |! P
$ _! |. P7 K6 q' i4 D) f' T7 p' D
location ~ \.php$ {7 ^5 L; W1 L" `7 T# m+ N* [
ModSecurityEnabled on;
" `0 ~. _# e H* t ModSecurityConfig modsecurity.conf;) n# b2 c7 O- T+ k4 T) u# b1 a
, R' K3 ?; I* k" y/ e root /web/wordpress;' Y( n6 l' c6 X" {/ c
index index.php index.html index.htm;
" l* s0 l3 Y1 O 9 D- R4 o( p0 I" Y, u8 r& J2 P
fastcgi_pass 127.0.0.1:9000;; X6 l5 M4 k" n5 k
fastcgi_index index.php;
6 @$ T U3 D% z+ R/ |, t1 p fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
5 z1 ?" T) ?& v" v* J include fastcgi_params;
! X4 J7 Q7 o& R% X+ Q }* @: P/ @" N9 o$ |$ h
}
. ]0 p7 h( F5 d- ]* u9 m- b% ~upstream负载均衡:
) g8 Z: M3 v) R q& u& w$ w) H6 ?' [$ Y, c. Z( @& q* z! U
upstream 52os.net {3 [1 G5 {, U% b0 N
server 192.168.1.100:8080;" ^9 b$ f3 U1 v% l, O5 ~7 f# ^
server 192.168.1.101:8080 backup;/ q! f" K2 ?7 S) p5 h: X
} m: p( ^4 V# i; E- Z
9 b4 y5 v% `3 q" O- ^: F7 w
server {0 F6 J* e1 J: \2 L5 c0 E
listen 80;
/ u4 ^6 r- M5 v, h4 y* M! L4 G) \server_name 52os.net www.52os.net;7 i6 J: L# k) O0 R8 k3 m
5 s/ L8 t7 p/ Y* m) z7 n
location / {
' l: u3 [3 X5 z) @% N ModSecurityEnabled on; $ H1 E/ Y) [5 [0 z
ModSecurityConfig modsecurity.conf; & A _: T" t5 @- H
3 O8 Q9 I3 D. l ^5 t% M& {/ S$ E proxy_pass http://online;
' z8 u. V d& g8 x( V proxy_redirect off;
% L/ X }# p0 T. q r proxy_set_header Host $host;. S( p+ \4 E3 U# ~7 ~& ?; G' d9 o
proxy_set_header X-Real-IP $remote_addr;& _) _" K/ u- M# J; R/ E( G# d
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;6 d; b% n9 j6 E
}
% c6 h, Q$ I0 i( L t9 r# p}
! c, [$ d- Q. @( \8 F$ W$ ?: {. R六.测试! j* ^1 ?5 W) {% g2 ?' y
9 F- G- ^, p* V1 N% H我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
; ~/ H) g5 l2 _1 B9 ?' `* |* Y$ j- O, `% M; Y7 P1 M) Z+ d2 K
<?php7 n! D# X9 O9 ]& p" F
phpinfo(); " ` n, P- U/ N4 W5 |# _
?>
! G [3 Z$ A% r) o; o- w! c" J6 C& ^在浏览器中访问:
/ D9 o M( O+ w8 ]. V* W( r# O$ t* ]6 n% P2 o7 c4 |+ g
http://www.52os.net/phpinfo.php?id=1 正常显示。
! ?6 Q D$ a9 n1 e. ]7 o- yhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。# b" p. x7 Q$ K1 K# R2 z8 y. a& ?
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。4 c5 i5 W1 f& r4 e0 u
说明sql注入和xss已经被过滤了
& v6 L$ e$ Q0 U. n, R
+ a+ }6 t6 s" b七、安装过程中排错4 V; F, Y0 ^, q+ Y
! N3 e- ^9 z) [6 \" ?
1.缺少APXS会报错
, Y; O' |1 W! j5 J2 M- F3 B' C+ ^
7 s8 W- U, B+ w. B- sconfigure: looking for Apache module support via DSO through APXS ~+ q h! X( {3 ]9 `* h0 R
configure: error: couldn't find APXS
" V2 c- h$ Q) f" a. U1 n* wapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
% b1 K' v" c7 b1 a& M' H' x9 E8 B解决方法:6 i7 S* G3 t1 h% o" A, P
2 y1 `/ r% E. ]9 S7 a
yum install httpd-devel
, M [6 k; a0 \ J2.没有pcre l7 g) x7 l6 ?+ T1 Z! ^2 Q
( m' ]" D+ b8 w2 c! @2 J. fconfigure: *** pcre library not found.
2 T- N: ~7 L, d9 {( d1 }+ z6 S' rconfigure: error: pcre library is required: i5 U p: c0 R7 [; J3 B6 x
解决方法:
( ^5 s4 E: J6 a s3 k7 Q: M
" W- }7 `/ [) _7 g8 g. E- d$ G: A! Myum install pcre pcre-devel
* e5 J, r3 g$ w: A( C" X% s3.没有libxml2+ _: [9 M6 \; ^. y
/ [! e- k5 m6 |' b/ @6 D5 p4 y1 Z* l( w( k! y" n
configure: *** xml library not found.
% L+ }) }2 Q$ b! b4 _* c. B2 Z* iconfigure: error: libxml2 is required
# _; Y8 M. J+ p# z解决方法:
/ I' b6 K1 K- z2 j7 h5 A z5 m) j9 l6 k S9 j; K7 T; P* n7 ]
yum install libxml2 libxml2-devel
, C. y' X2 ^' M2 |% `/ O0 x2 P4.执行 /opt/tengine/sbin/nginx -m 时有警告
7 u9 ?0 M* l- \, @ q. i. k" p8 [: |/ u7 R5 e
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
# p8 s* P- ^2 e& f& i9 Inginx: [warn] ModSecurity: Loaded APR do not match with compiled! s5 G6 J2 o6 v g, C) G
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
+ N3 Q+ [1 Y) j# x+ b3 t
" k1 f. S; n9 N5 c- W9 {7 a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
" s! C, h& p: P- z7 v4 v2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
; ] [/ Q& g$ W9 `5 g4 }$ E2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
; E' m6 k0 Z( P. R2 K2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"9 p/ l, W+ t! a6 O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"7 d6 Y- X* F4 B' }+ ^
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
" N' c1 K! j& p3 m6 F解决方法,移除低版本的APR (1.3.9)
9 k4 y8 j+ k1 ~# `- g1 {* n f' o& g5 }# n0 z- h4 \. I5 r
yum remove apr
* a+ T5 S9 E+ i7 F5.Error.log中有: Audit log: Failed to lock global mutex- j% W% q( H2 w- s, S
4 g- y n1 o w. `0 A2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock o! i8 L8 k# H/ i+ `
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]; @' Q; |) f& i* O
解决方法:' ]! \+ [8 A3 f. m
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:9 @; F8 k2 ~! j2 t% A# o1 p
. \7 t7 ~# u. `: Q; bSecAuditLogDirMode 0777
) h; n$ I- t* G; v) vSecAuditLogFileMode 0550
% `! l# H& K5 x2 B5 U6 VSecAuditLogStorageDir /var/log/modsecurity
6 D/ Y- _+ V! J9 W* w3 a; PSecAuditLogType Concurrent
" @; z4 B. e7 |% z5 N, Z参考文章:
~( t8 n" K" p! z$ M+ Thttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX0 l7 ?- h& u3 t6 _% x0 X
http://drops.wooyun.org/tips/2614 |
|