|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
; N( k, f# T& y* \8 T, h7 ~) d# i4 N7 K/ ]" ?1 f
一.准备工作+ c# k4 ?4 |' m! C1 U& _3 s: F
0 [, N, i' }8 [% U
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
; ^; I: ?' \4 o
j7 J1 X* _2 i' X7 `9 }- Q* Itengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
" p1 I7 A+ }3 @' d3 D+ m; e$ d# J% Z1 h. [/ W: C
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
* [) J5 A, e: m8 }8 r% T8 r7 p4 ^
; b' r# _) \: T/ F* gOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs/ w. _& \, c- |" R# J. v
7 T$ c) n, V/ D% g依赖关系:' v% u8 I1 u- V9 r+ N' w
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
! H& g) c/ H/ n" a0 U9 e7 V8 M% U; n1 ~4 A! X' j. T: h m7 F G
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
1 i* `0 i4 |% H8 Q! }+ e9 [modsecurty依赖的包:pcre httpd-devel libxml2 apr6 f: i' F& J8 h6 f+ S. E
% R0 t6 X8 @: [$ w0 k2 b/ O a# Nyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel1 R9 T" w x6 N! z$ T0 Y
二.启用standalone模块并编译
( }% d9 q; J1 T7 s1 j' [) f+ ~7 M& V& H
下载modsecurity for nginx 解压,进入解压后目录执行:/ o9 o$ D; `0 L# t+ G- m3 h# i
# Z2 d" Y" E. y1 ^
./autogen.sh
6 P- A& `/ \! z./configure --enable-standalone-module --disable-mlogc2 v5 C. b% r* ?
make 3 Z9 F2 {) U- R u
三.nginx添加modsecurity模块
9 a( }' I/ v1 v: [
0 A/ }8 i0 \0 d- ~+ a6 B# f在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
0 p+ |& X, H2 @# ~. `8 j4 B
/ e, }0 }4 f/ E3 @" e: ]& s./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
/ e% N% e- d% a- h* i7 A' U/ A9 B# xmake && make install" Q2 q9 w# R0 R* B+ v: q( j' I
四.添加规则9 R8 i% ?, O* F( s5 A/ b r
. N( ?) J, Y% a3 c% imodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
, s* |! P' C6 g; z# Y6 j1 W
: i; Q4 d2 Y7 J) {( k/ [1.下载OWASP规则:
% M# ?* F5 O1 f; t+ W8 E
4 a4 r( v/ {1 c. m. L2 Y- K8 ^git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
/ b: h$ h, ^. [7 t8 S; `
: w& T/ }, ^3 T! }mv owasp-modsecurity-crs /opt/tengine/conf/
" D! m6 e) m8 R# _) g" o* E% A3 J! Y3 c& X- I9 s( _
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf7 W5 n! W+ @/ l! d! X4 F0 @
2.启用OWASP规则:$ g$ ]9 b M( \5 V$ R
3 f/ s% O. R; `8 S' M8 H8 o复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
D, v$ _4 i5 G l- E, Z/ R F
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on0 Q7 X% H0 H" b4 b4 d# V0 h$ x
( e3 M- Z$ ?) q6 @8 i5 n' t
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
1 }& y5 N1 u( e# Q" X
2 M# O& S+ s: @/ v, mInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf% k5 M1 n* ^! M' N: @# T- t2 l
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
0 e, s+ x9 F$ F/ X/ mInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
; F3 c4 x" P* HInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
. G& Q: \0 W' l8 ^. q8 B0 T3 q% hInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf) J* a; P( a! A- J: m8 `! J: d) K- L
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
: o0 }1 L7 k) |% K" eInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
4 U2 n0 Q7 l2 I: r L# ?. x五.配置nginx
. k+ K0 p% X) k$ s" [0 _# x. r G
( I I( F' l6 ]在需要启用modsecurity的主机的location下面加入下面两行即可:
" R- m2 i+ W$ z* n4 t9 V5 ~+ s3 \
ModSecurityEnabled on;
4 o. C$ C) N& C- d7 i7 hModSecurityConfig modsecurity.conf;5 t& v; {, } L/ {0 ]
下面是两个示例配置,php虚拟主机:3 t1 _8 _( r B
3 c% J1 t0 ?$ Q/ [server {! @6 \0 d7 l" l. |+ a
listen 80;
1 j8 Y; t: g# @ M9 |6 j4 m server_name 52os.net www.52os.net;
, D1 d) h- j( L: ^ / L; r6 c9 I- a3 K
location ~ \.php$ {- A8 ]& E5 s- ~4 n- {
ModSecurityEnabled on;
9 W' N% p2 _3 d) ^; @ ModSecurityConfig modsecurity.conf;
0 X1 i- r4 X) C$ X: R' U5 p3 l2 X% {# y; F, T$ w( ]/ A
root /web/wordpress;
1 r# n7 a% B! h+ ^) d1 g index index.php index.html index.htm;6 Z* ?! ~: B/ b) j& c% n. Y% }
* t' C) T: a+ E9 x' Y+ q5 u( b- r1 M
fastcgi_pass 127.0.0.1:9000;+ m) h1 h- t" s ]' b
fastcgi_index index.php;/ n1 B3 ?9 K+ }
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;% q9 h3 I5 P9 |; m; A
include fastcgi_params;; Q9 y0 E E9 |# N
}. g2 _1 s2 K- a
}9 J3 ?) r$ T+ y/ |* V& B0 A3 O: a
upstream负载均衡:
, N r7 M: C* c( o2 l; L! K# o- e
0 v% d1 S( G& Z/ P d8 |upstream 52os.net {2 B# I: G1 U/ Y& |4 [% F4 w
server 192.168.1.100:8080;
; X) ?7 E- R2 }6 d. n server 192.168.1.101:8080 backup;
, M, v5 S9 [/ E7 i, C; X; K$ V3 v}" k" b, I6 }# c" v7 J; A6 m1 @
2 V2 [ L& U8 y3 X
server {9 D8 P8 u4 K. I$ L7 T$ N3 @
listen 80;4 Q P( U% q4 A: e7 h3 B
server_name 52os.net www.52os.net;3 p# `6 O- V! X0 _
( [; D, \: `2 ?. zlocation / {
' s' z; I: A) h ModSecurityEnabled on; : p* M; J Z# Y6 W% s: |- U
ModSecurityConfig modsecurity.conf;
* a, m0 z; f; f' c' v- e& H9 i' j* b! O% Z
proxy_pass http://online;# c9 ?4 m+ V0 O% V9 L1 k6 e' c/ c
proxy_redirect off;) y% w' h! |$ d1 I
proxy_set_header Host $host;
# F/ O8 i0 D. N1 ] proxy_set_header X-Real-IP $remote_addr;& T8 {6 r0 ?- N9 d; H
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
2 r, M5 N [" j7 L& u }# S! U: X0 | w: n) j2 t/ s
}
) r, s6 K# r1 `* h- ^7 o2 O0 F& a六.测试1 Y- z, X* w g1 I- |
# K7 t* C! ?! X2 w我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:8 a7 ]) Q. k$ D; u8 Y: H
`! `6 p2 y) B; @<?php
5 e) L2 S. J8 M* H$ { phpinfo(); . B8 F# }$ J. c B- _) w
?>
5 \% W+ s0 @' z9 r6 B在浏览器中访问:
. z3 b) {6 e" N/ v2 F" t- T
9 v W4 l* ^3 M/ X9 g' ]# jhttp://www.52os.net/phpinfo.php?id=1 正常显示。1 ^' K* C4 Z2 O# g2 u; c' }; d
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。& M: S+ z" Q3 ~! O# C! Y! `
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
4 j4 B6 K2 J) P7 D说明sql注入和xss已经被过滤了) U3 y& o7 K# L
+ q0 G1 l, b+ I- M七、安装过程中排错
' `- v/ \4 r: Y) ~6 V
5 h1 o( n0 u# ^) m1.缺少APXS会报错
+ o4 u3 Q! [- ?: g$ ?
2 m+ ?. X. H0 b/ e3 u+ Uconfigure: looking for Apache module support via DSO through APXS
$ Y5 A* u' R. T' o iconfigure: error: couldn't find APXS
3 L2 O% u. ^, ~: z/ B9 Sapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。! A9 C/ p3 {+ E" G0 \. M( T
解决方法:" T& C# A9 t/ X' w* X
5 }4 `( `9 f* _; j' e ryum install httpd-devel
4 T$ V+ U j' H2.没有pcre' M0 \; @3 L" q+ J( C
8 _( D# P$ Z v6 ~# [# j! |! |
configure: *** pcre library not found.! d/ _, x# R }9 u6 W: e/ |2 g
configure: error: pcre library is required
, B: z$ a9 T) G% o+ u解决方法:6 y! l2 y+ w: R& S- G2 I
+ ?$ Q5 }( N0 g/ Eyum install pcre pcre-devel) Y, U+ U3 k, y2 I; o
3.没有libxml2; F* b5 m! s* A3 p$ L) h
" [$ v* {) G1 S% C5 A
+ a' U* v. S8 E6 e
configure: *** xml library not found.
: R/ R/ M% y$ Z, J7 f! A( l8 hconfigure: error: libxml2 is required
q, ?; R# y/ K) N解决方法:
+ N; r9 h7 P+ \$ I+ v l$ q" ^
) U. T, u0 F0 d' R2 p) K& t9 g. tyum install libxml2 libxml2-devel+ A: p7 X3 `) Q! L' V
4.执行 /opt/tengine/sbin/nginx -m 时有警告
- _% R! B y# b; r
4 K- `% `) Z x/ RTengine version: Tengine/2.1.0 (nginx/1.6.2)$ e# g, m/ d5 d6 M" }# n
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
" }( B, o, b7 v, ~; L: n原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log9 U8 P( w5 f; y M: [2 s
) M% E8 V$ X' J$ I
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
6 p5 w; y- r. P& X9 {% M2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"( M. G2 @7 [0 @" w* _4 p, g% Q
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!/ u/ u! r) S+ Z; s/ Y4 j
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"8 f6 u6 d0 C! v" Y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"1 {& S1 U! h9 ^
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.; L6 Y4 ^0 C, u
解决方法,移除低版本的APR (1.3.9)3 D' U- q4 l' ~* S
4 a% I! w- z7 _7 a
yum remove apr
& v4 w4 x) d. i$ L/ k2 _: ~5.Error.log中有: Audit log: Failed to lock global mutex2 D; A& k) F/ A4 [
( ]% w1 l0 ~. t
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ; `8 b( I0 @/ ?$ S
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
! q# i: P" e8 W& B解决方法:( c3 z0 r* _7 y) R; I2 {- z# @2 ~
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:" t& {; Y& V$ e, ?- C8 W2 G4 x
3 M# Q3 G9 ]1 e H
SecAuditLogDirMode 0777& v3 V8 _/ a2 p, Z
SecAuditLogFileMode 0550: X2 O6 U0 p* s7 ~9 r; L
SecAuditLogStorageDir /var/log/modsecurity
* n# l8 t; T' h) P* z cSecAuditLogType Concurrent
) Z5 m W" c) L% R8 V, F B. V参考文章:
8 B; n; @$ Q1 ]+ \' F; ]& n% Rhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX- o5 }9 Z8 o, _ @) p/ l1 b
http://drops.wooyun.org/tips/2614 |
|