|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。- F5 `4 t% @+ H! u0 L9 E
- l) R8 Q' L) m一.准备工作& y: Q6 P+ {+ r! w% k* a
3 U% T9 v$ S9 y: ^! E系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.00 d* T% f! G# V M, p4 A, B7 f6 C
5 U5 P/ l2 X& v; X1 ^! l2 Q3 q
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz4 h+ [* l8 ^* L/ r& R& Q1 e
% \/ c; M# K8 O7 I5 Pmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
& p$ U& X2 z& i0 p' c
1 R2 F7 C4 r- [' }: c* mOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs. D6 b4 d6 N3 F
+ Z' H' m2 v% n
依赖关系:
) v' {% R' @" }! h! Ttengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
" d# z; m9 l E! b8 h, u" {; A) d; [1 v* b
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel5 ]2 u9 l2 N' r! V% }* B3 r
modsecurty依赖的包:pcre httpd-devel libxml2 apr
+ `4 f* `* |) O: Z4 C4 U. a% U7 r' G4 n) b9 p* h
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
8 n% y4 I$ e; c二.启用standalone模块并编译
/ c& G6 c7 v8 E: @' y7 Y7 {
9 v* [% a- T1 m3 B下载modsecurity for nginx 解压,进入解压后目录执行:- |6 E$ S" q" Y4 m# Q$ S( Z
+ z& D" `, P+ u' r! e# e
./autogen.sh
8 s+ L1 ^( ]4 N" F& i! G./configure --enable-standalone-module --disable-mlogc" ~6 c4 R1 q2 [/ X6 h" D5 X3 Z
make
* `& W$ y/ `$ ~3 g三.nginx添加modsecurity模块4 W+ O* C: l3 g3 W
* m3 T5 E9 z1 M6 J& d0 U$ @5 W在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
- y! _* P9 Q' ?6 H: P1 i) ~& J P+ V- D: u* r! r# x
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
# L2 b! p9 ?: X# v' I% { m1 d3 Lmake && make install
& ?0 [+ M5 L; ^! i7 P. r# o7 v四.添加规则
, d! q% b& C/ `! J8 T$ W" o3 G& O6 E6 p o- Y, Z! m1 r
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。4 g& b5 Y+ }" q0 @, t, } V
! |5 H" x5 ^ I1.下载OWASP规则:
0 d9 y# \7 l! j. `8 n+ U/ }1 Y$ H
) z8 N& |5 F: I/ x- o" {git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
/ K' V9 ^1 \6 h$ W1 S; ?, }+ R0 J) \$ T+ \
mv owasp-modsecurity-crs /opt/tengine/conf/6 Y0 G0 U. g( b0 [% S, u
9 [) Q; ]& j0 T/ @- k$ g- @cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf% D; M( f' W6 `& E
2.启用OWASP规则:: W% k5 M; o0 f4 h9 i) C n
% o/ p) p$ ?% F. }1 } l
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。) f. U) n* l6 s. w8 [0 h6 D
& Q' g; [+ G1 ]
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on& f/ Q4 e& ?4 a0 X- ]7 v: K
8 B+ C Z3 V7 ?owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
. f4 W2 N7 e. x( {" z( u" \
8 ~$ e% n1 D6 C9 S6 D- nInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf( O, N4 c4 }) u2 D
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf. z! ~% s5 ^; a& k/ f% D# c" q- p2 x
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
# @" x% d d8 e+ L7 C2 EInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
2 P' O m# _ N0 k8 ~Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
6 T) H9 O& I% }7 D2 z* x& x _6 vInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf6 E5 _' u- Q" f+ j6 V) l- s
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
$ u8 a4 y% L7 w) U五.配置nginx
6 R. F4 F! M) S& {# A( q Q. Y
在需要启用modsecurity的主机的location下面加入下面两行即可:- V7 x" P) h* Z) {
* K @5 K" A9 l! h+ h- DModSecurityEnabled on; 9 R8 Z! U# j* k) l+ J
ModSecurityConfig modsecurity.conf;
$ e/ y) w8 t* B下面是两个示例配置,php虚拟主机:
# Z0 c2 q5 I1 K- i8 m: B. v
3 e5 r5 |& f1 T& |server {8 Y; j# p2 X0 r/ F
listen 80;
, {+ Q' q% m. L( M' h7 y- d server_name 52os.net www.52os.net;8 O; P$ b* C% w! n2 \
* _# u- r, Q' E location ~ \.php$ {
5 N) o5 D. j; F' ]! v8 j: K ModSecurityEnabled on;
/ w. f" o8 Q% Z2 _& g; M7 c8 \ ModSecurityConfig modsecurity.conf;
9 g3 |" n& ?8 B: {0 C0 C
, q" o5 m4 Q( W( Q root /web/wordpress;+ s9 Z/ Q# T5 w: w0 [0 X0 k
index index.php index.html index.htm;) W$ p: h4 Y3 w3 M) q! k' {8 t
* y9 ~/ H& J& U" ~ X$ H fastcgi_pass 127.0.0.1:9000;
6 x- [! @+ }: e1 S fastcgi_index index.php;: y: B* y) Y" l( q. i" Y. j7 ^
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
+ v9 i' R+ r$ \ include fastcgi_params;
% z3 N- Z2 ~, y" n/ R }
- l$ A1 X& Q5 Z$ x1 M }; F: {; d, m0 l- n/ V) d% d
upstream负载均衡:
' h4 w5 {5 K3 b+ Z
, @0 E# o4 A7 _upstream 52os.net {! w3 i4 j6 P* U$ }4 d y! U
server 192.168.1.100:8080;
4 R( `! U0 X; i2 R2 K! \ server 192.168.1.101:8080 backup;
$ \9 ]4 ?( `6 f& g$ a$ z} H# \ c9 a4 h9 b0 h# n1 D/ s
$ P% S; b9 ?# K9 T0 s% R% j
server {
7 q: q6 B$ w& Y! U) T0 g( U+ Vlisten 80;$ l" W+ j2 N3 a2 c p0 j
server_name 52os.net www.52os.net;
, _; n( \ @2 Y1 F, G5 M
0 k) h, n2 d* S# s7 {. o) A/ Tlocation / {! p5 O0 w* i5 D( I5 c3 a
ModSecurityEnabled on;
, b* v' ]: J* r" n; r ModSecurityConfig modsecurity.conf;
; S, [) l/ [! E
3 Y N9 n7 }% P/ m4 H3 a6 ?5 h proxy_pass http://online;9 u; a7 F% c1 k* ]8 a9 t
proxy_redirect off;/ P Z( K1 F6 y* _
proxy_set_header Host $host;! k" T* w6 I& Q" e! {8 f. Y# C0 r E
proxy_set_header X-Real-IP $remote_addr;
2 ^2 k( j: [. ]' [" V; |* g proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
1 Y) S. O- d# V. e5 s; n }
9 m' c8 D% {# u. }9 g}
. j/ u; b1 I7 o. c六.测试
: D+ H2 N9 K' a# f. |) F* T2 ~4 g
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
, q8 n7 \7 ~$ @" C- S
/ W5 r: ]7 b# p, y) n% Z4 I; W<?php
) R& x% q' b. ~2 ~! ], o/ M q phpinfo();
% B- |% m6 z/ d. X1 K7 {! n$ R?>
# k& x+ v6 a" E2 z# @在浏览器中访问:
. e9 x; H, K% L8 c8 }+ b2 o
. o% E7 E0 j% g: b+ j9 u, M& k' Ahttp://www.52os.net/phpinfo.php?id=1 正常显示。
- r) `& a5 l6 Y* W# yhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
( B$ N9 P2 |: g3 Phttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
8 J5 \5 K3 y+ g' c; ~' n% g# ^说明sql注入和xss已经被过滤了4 l& c) b/ L. q3 h
$ t9 H g" i! \七、安装过程中排错
@/ r" e" w* S# ? j' Y" ]( N9 W- @/ V0 u0 d! e, G% G0 |
1.缺少APXS会报错
) X& p6 }# E2 a: c2 q" y. K" w# ~- c
configure: looking for Apache module support via DSO through APXS/ r6 E: G% D) U2 |4 a5 B
configure: error: couldn't find APXS
. W6 N7 S% X3 A8 i* ], b# l, A: xapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
0 ~' L+ Z7 \( ]: H: T" @解决方法:: W0 R/ H/ u( o3 i
5 R! A/ O" ~% n8 T$ ^# B: T9 G; Eyum install httpd-devel
; S: L1 v2 w+ a7 Y% l2.没有pcre
& s3 W$ A$ }, N# `: C2 c, ?$ { C# Y, O1 _
configure: *** pcre library not found.
4 P$ h+ m7 z- n* Y6 a8 M' @configure: error: pcre library is required. `3 G" `& o# F" Y
解决方法:
- R* A5 Z, j$ Y( u) R/ I. E3 a4 Z1 O7 C: f9 E
yum install pcre pcre-devel% {1 U) Z/ V# g. E7 ? r* X
3.没有libxml2
& z; T) `4 x% l. ~, u* I8 b2 M6 P7 O3 f# R6 w. k
5 n3 x+ C) b/ T- l' L3 ~
configure: *** xml library not found.; f5 k1 l9 c/ M3 _
configure: error: libxml2 is required% G( ]. | |# J j3 c) u5 P8 f ]% n
解决方法:1 _3 I" @; C: l2 j0 {( ^- X/ p
* s5 R. w0 C/ p; Lyum install libxml2 libxml2-devel
7 C/ G1 K1 N z r, q4.执行 /opt/tengine/sbin/nginx -m 时有警告' Y1 O4 S9 E4 C% D
* H# w% g! T2 q1 D1 F1 p/ g
Tengine version: Tengine/2.1.0 (nginx/1.6.2)9 h" g- l6 {+ o( f# i; Q
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
" ^( I! Z( J5 `; J' s. J; m' \原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log l. W9 N: G% n0 [: I D
8 E/ a; I" U) v. H
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
) @+ Q$ e" Z, t7 n, C2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
+ N2 O2 ~& _; f0 c; X, p4 \; S2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!; M! a; Z& z0 r$ V: e
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"0 r! O' w* B% Z8 H% P3 Y6 B' o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
. E. o9 x2 r' s2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
; J! P& a6 F$ o' W% |解决方法,移除低版本的APR (1.3.9)
& h4 m7 q. B' O) F1 |) T
% ^1 t, ]- W# u* c; Jyum remove apr% u- I( u% C9 t4 g7 k% z2 d% E
5.Error.log中有: Audit log: Failed to lock global mutex
[3 R+ u: A8 A* V& A& ~8 ]- o# ? }8 d U$ d4 C. b. t( Q
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
7 d- ]4 L% @' @4 W- \; }6 [global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
+ W9 i9 Q9 ]4 {- o3 F, i/ B解决方法:' G, r3 `/ W" K% P
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:+ |; i& B% ^3 K# m5 ?7 `; \; c$ J
6 h9 k' [: n& ]
SecAuditLogDirMode 07773 r- q* `4 c! g8 \9 X6 R
SecAuditLogFileMode 0550! m: x) D9 z$ b: ~! x$ C O
SecAuditLogStorageDir /var/log/modsecurity
+ b+ A' m' i3 s9 e9 s+ X7 `SecAuditLogType Concurrent
' k1 r( h( g+ J% o2 d e4 d5 w参考文章:, [, b0 y: _7 _: p. j& a5 P
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
* C' v5 R" z' s! }8 z& Z) qhttp://drops.wooyun.org/tips/2614 |
|