|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。( s3 ~5 w/ D- x$ _* {0 {& I1 y
% |1 ]7 l1 j- i- W- d8 r
一.准备工作0 z' ]6 c! D4 A; p( E
/ O) G1 ~9 A& [) e系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.07 z8 g2 o4 B' u0 X" Q" g
' `( q5 Z# w" c* ptengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
+ l T$ Q. y8 Y. ]4 D& S6 U. u* t5 ~# Z6 {0 O
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz2 x7 g5 U8 h7 R; [: X8 Q
) ]" O! D, ?7 d x& s7 d" ^$ I# T
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs; C4 x6 \0 K6 Q, G6 G
" `4 l. \5 G7 q4 k' }( b
依赖关系:: w! ~8 ?$ i' E2 v' w- t
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
% J% L% Q- {4 K/ J. e6 u. k6 w: K5 E) r- g/ M$ c4 ^8 _
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel2 Y' o: L# u% W& P# z- Y
modsecurty依赖的包:pcre httpd-devel libxml2 apr
& C! R3 j" M, _
+ |; S/ n4 z) ~+ j! b1 p/ \" tyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel3 K; ]8 d, M+ U) V) p
二.启用standalone模块并编译
4 w. b' S. @2 q9 r$ o) U1 Z/ l6 e
$ ?, K. n6 F+ c下载modsecurity for nginx 解压,进入解压后目录执行:
8 p; Q' g% Z* f# S1 Q* o; C0 P4 w& D6 m# K
./autogen.sh4 `" J6 k, Y0 S( L6 |# Z* g
./configure --enable-standalone-module --disable-mlogc. A- b) O, b" ~' L8 D
make - d: ]. v( F8 J" {# x
三.nginx添加modsecurity模块0 z% d. v' E8 u" L: s6 v$ A# D
4 C) w' J q& K1 K1 W在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
, d/ \8 X4 r* y F) Z( C: |7 t( Q w% W! [, k% \! x/ t1 K
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine1 W- p- `4 [# _/ X9 q* }! S& v
make && make install
1 W# q& x; j0 I四.添加规则
1 h) s4 p* l, p4 D7 \1 ^( ^
; M$ C6 Q# V Kmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
4 z1 E& w# c& y+ M9 T0 l" y5 b9 E# x3 C0 A; h3 U7 ^7 i
1.下载OWASP规则:9 R4 s- W/ [. T$ E
7 C: Z/ |2 z" I2 t) P- M7 d. |/ i3 j; S0 T _git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
7 p9 l& H# i/ T* P$ x* l; [) E# [* X' e) m/ F
mv owasp-modsecurity-crs /opt/tengine/conf/
5 ]* {( \4 [3 y- p% h8 t1 O
/ Z- O: _; b1 F# G, zcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
q$ U& S* N9 W/ @: v9 E$ z2.启用OWASP规则:
; W% t& ]) l( V0 S( f/ z$ O$ j; F1 N; p x
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。1 n, \: u6 f& H
; ]. s0 _( m* ]: D1 ]& O
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on* J! v( i% g5 C
: o% E4 D; Z. O3 P: c( ?7 Nowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
# |2 m8 H- h. R: O) L
6 L( O4 E! F" Y$ v. XInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf8 z' @8 J6 e) ]; [
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
; e$ d$ ^4 p0 _3 z, V* a; ^Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
: g" l7 Y7 I) I6 i; c# IInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf+ J3 H! C$ k9 r1 u6 z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf2 M! b" @5 E9 T5 S% o
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
2 y! A) z, W( s2 p& L( sInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
0 s2 m& U, M1 c8 ~7 ^; q' B五.配置nginx
) M, ?5 C& _7 e3 h9 S u4 o% l/ U: v, l9 C c4 Z
在需要启用modsecurity的主机的location下面加入下面两行即可:3 y" g5 ? t5 b+ J3 y
: Q p F- u R3 J: O3 Q, a& V- y# \
ModSecurityEnabled on; . [0 W# q: T% P3 C4 V
ModSecurityConfig modsecurity.conf;
4 Z* O7 O% l* ^% ~下面是两个示例配置,php虚拟主机:2 ~2 S' X8 }( h7 ?
3 I4 D* B) ]1 A
server {
7 s, Q; O; l* r* T2 y( @, r listen 80;2 u6 g+ A. ^ P' X7 k! H( f
server_name 52os.net www.52os.net;
( e+ |( F- y" } V1 m
- o, c6 t. b1 C0 ^& x location ~ \.php$ {
8 N8 e0 ^) x B# f% ?- E ModSecurityEnabled on;
9 K. m J/ c# _, g ModSecurityConfig modsecurity.conf;
( R f+ Y7 a, t! z0 |3 m1 _" l( Z: w: S0 a
root /web/wordpress;
1 @2 v5 [9 h/ ]9 H$ F1 v2 W- ~, m+ V index index.php index.html index.htm;
% O/ F8 W: d6 @ , Q' ?& P8 ?. w- H. B& i# d
fastcgi_pass 127.0.0.1:9000;
# I& Z( K! h, e& }( E fastcgi_index index.php;" H- R$ m/ y7 i8 Q- ~+ ?/ b
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
/ ~* ~6 ^( u- L7 }7 { include fastcgi_params;2 ` C! R/ A3 a+ y; N
}8 `9 M# ^5 w+ x6 ^$ S- N
}
/ ~/ k; E7 Q, k9 [upstream负载均衡:! C* t5 J8 B, h! w9 W% L: w
/ N3 R2 y6 A7 w" f" f: ]* Y
upstream 52os.net {2 v" f% l$ o: l0 p2 n; q( B; P8 t
server 192.168.1.100:8080;
# ?& H7 h# v# V% `4 N2 K# B server 192.168.1.101:8080 backup;' @: T# U# ~4 @
} i' v4 h9 K/ ^. A* x
( M3 _2 b' p! n$ W. M+ @/ [
server {5 k+ G+ }: U/ ]1 j6 K& g9 r f
listen 80;$ K* q* t) w# s0 v$ D' k
server_name 52os.net www.52os.net;
+ M" T) `5 m& f$ g/ U& U ]- E* p0 W, {# ?) N- x& j K
location / {$ ^! ^, p- q& |2 R; q
ModSecurityEnabled on; % X: k/ v/ ?+ Y! o, c$ O
ModSecurityConfig modsecurity.conf; 3 K0 F2 f4 t# N
4 \1 i; z1 m- ]) Z2 Y+ T% u0 l
proxy_pass http://online;, t* ~8 |5 F7 G& R4 E
proxy_redirect off; N" d. k( k! D+ u( k2 i! e9 t
proxy_set_header Host $host;: m, c: C6 k' ~! H4 ~$ z0 U
proxy_set_header X-Real-IP $remote_addr;
3 X8 ^( ?+ C& p" M# ~1 u: [ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
5 W% _2 Q# W U% G }( m Y2 Z3 S+ s3 W7 C
}
' z" @0 e, F( d2 O' y3 r2 z六.测试
$ g! T- \7 Z4 ?% Q# |- F# T5 ]/ |. D' k9 G+ u, |
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:% x3 P" @. H$ o7 i8 z+ D* ]
' O8 p: D# @2 l<?php9 ?4 q& B( z+ C% m! U" T- r2 F
phpinfo(); " F' O8 @ A+ W
?>
. {' b# M' l" G& g+ y在浏览器中访问:
: X- u: U9 F/ s/ S* |* D, O( k+ |+ Y* ^- Y0 t, Y, z
http://www.52os.net/phpinfo.php?id=1 正常显示。
; _, U2 m- w$ N) ~http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
. ]2 v2 G$ o1 u Z ]http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。3 p' ]7 Q5 g9 E/ O8 z! `
说明sql注入和xss已经被过滤了( F2 ]3 E) D' P/ U
/ D( l3 I* U h( `! h6 {七、安装过程中排错. t; k0 H1 U w! j( W; F- v
3 H6 l' B0 I' C. s
1.缺少APXS会报错
* }( I, T* ^3 I. Q9 @- r) {
# T# {( \2 W5 F/ j/ f& Z Jconfigure: looking for Apache module support via DSO through APXS, [8 P1 a- U( |% _$ u7 z$ Q: R
configure: error: couldn't find APXS
0 }+ S; T9 N( S3 z4 D8 w1 e7 zapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。* T+ D4 i& ]( t5 Q1 ~, p2 @
解决方法:( X3 P" Z; s: `- m
" B0 A( _" ~+ G
yum install httpd-devel- z& r( t. p* d s. F# r
2.没有pcre* B' a, Y( C7 [% Q) n% a8 g
y3 ?* O! E; s' {0 A3 aconfigure: *** pcre library not found.
; ]- O- _& H- ?) e6 G0 B9 mconfigure: error: pcre library is required
$ ]; B4 H0 |% U5 J解决方法:9 Y8 H) {6 O0 @# U7 G& A7 Z7 O- |
: m9 S3 W; q# q9 p1 g [: b0 s* Yyum install pcre pcre-devel) K' x6 q, R3 B, M/ u3 J8 X
3.没有libxml2
" M. \& D! S. X8 b" o7 Y
9 Z1 f9 v6 g7 k& H1 K% T8 z
% M4 q y# a( I1 x% d0 J+ c! t; oconfigure: *** xml library not found.
. h( d3 i7 P# M" G4 Qconfigure: error: libxml2 is required
1 @: S$ [2 j4 x4 [" U# e解决方法:! a% s: l2 P; q6 k
; ~% D% ^- Q; k) m5 `! z
yum install libxml2 libxml2-devel6 D1 g7 H6 m; @" E% F @
4.执行 /opt/tengine/sbin/nginx -m 时有警告
9 \7 N E7 X# G- u" G# ]9 D" e* A
6 U5 S- E2 }2 dTengine version: Tengine/2.1.0 (nginx/1.6.2)9 o: M& Q) D; ~5 d# V, D
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
" X e N3 m; Y. u& g7 b原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
/ U3 ?, X$ a$ ^! f
: v' A& L. N# |# S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
5 u: J, R$ r/ k& _0 G7 L b2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"; S, e( t/ q$ T2 N" d' E
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!# e0 d& Q3 ]: b
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"8 D8 k: v5 e7 H' Y( P- L
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6") A$ o5 k8 m+ _6 f0 c
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
+ V5 ]1 K& Y# v! ^( [& n, U( s解决方法,移除低版本的APR (1.3.9)
/ _( M S# H7 G& _; x7 ] V, U' ?7 ? ~: e, K
yum remove apr6 }+ X( _: E( n5 V3 C* q
5.Error.log中有: Audit log: Failed to lock global mutex9 F* R* r, p5 N; z# f
2 h4 `7 Z, s o: G. \/ ~) `- B2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 9 v, N9 L! T' r* R
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]" _+ P! C. K" I6 @
解决方法:
& j3 @7 J2 I& G; o, [& Z; [编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
) w# f& h J# B; _1 u! |
% n5 m: G: y- Q; H- ISecAuditLogDirMode 0777. |7 l2 d2 v! g6 [
SecAuditLogFileMode 0550
& a( \! T& G4 K6 NSecAuditLogStorageDir /var/log/modsecurity: W8 M7 Q& r8 V# l
SecAuditLogType Concurrent
) u7 Z( E4 K1 c参考文章:9 Z f, u, D7 ^) ]% k. s
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
8 f+ {7 B; @# Chttp://drops.wooyun.org/tips/2614 |
|