|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
; a5 t0 A3 t( m2 N6 O: l2 D# \4 V5 E& m& h
一.准备工作
2 b) ^8 J! C0 S
, d3 `) C4 \- r8 y/ B系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0$ r4 W# \- q1 m( ~5 F X
: I0 L+ {/ J3 A2 T! ~$ Ftengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
: S) k: B2 I7 A m# `8 q& N' T
7 B* t! X8 A4 w8 smodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz2 O, w, |; a$ H& m
3 \: f/ @/ {4 d" AOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
5 @8 f) e. i3 g. F3 P
) E3 o b9 K& E) Z! B7 U$ P: H. i依赖关系:5 c3 s: X4 |9 L, G4 w' I+ g
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:; ]7 q. }* G: `" S3 N$ M* Z
' o% v) V8 t; K: [7 B
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
4 a! L! @# G: f' ~- s# tmodsecurty依赖的包:pcre httpd-devel libxml2 apr
0 X( X! w1 B5 H' B% T6 Q
7 y& _) n- x: pyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel" [2 S5 W# S2 }/ c7 f6 G3 k
二.启用standalone模块并编译* w6 O# k8 W+ n6 Z: F( Q) B
8 o, L6 X# U! N* m0 y* ]
下载modsecurity for nginx 解压,进入解压后目录执行:. @1 [3 |/ H$ T1 D( U7 X/ m8 n- u
; ^8 s! t( E7 @2 c./autogen.sh
u* K; e8 g' e* ?./configure --enable-standalone-module --disable-mlogc9 S2 J5 _4 Z3 u' s- [
make 0 ]3 m' L( e2 W8 ?4 x
三.nginx添加modsecurity模块1 S. w$ U* p# l# o8 J
5 o2 k6 D7 [6 V. A' Q& p( w; i# b
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
0 R7 `( e1 w0 Q j# L0 ~+ T
9 _ R6 W) I7 |2 k./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine" t# {3 ?* V3 M1 b; k3 a
make && make install
9 l9 v, |- n, m0 v9 z四.添加规则% O3 C- C' H# o8 N4 o q
- u9 f8 \6 S7 J
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
2 I, n" |1 I. `; o r) c' l2 S6 {6 K; |# o/ d
1.下载OWASP规则:# `: w4 C9 H; f% N; l: G( r/ l
. Z8 V4 p( C- G2 r: W0 s6 }5 M
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
( e' _& g: s4 k$ I; V8 I
, K- F7 g( q' L/ o4 X# x& Z3 q! hmv owasp-modsecurity-crs /opt/tengine/conf/
, z! Z x( r% l
' j; l9 v9 Z8 N- Xcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
' X4 ^# Q# D# w' a3 `% ^2.启用OWASP规则:
; m) O' Y0 S C; R9 j% s8 o. _* k$ G5 [' e5 @' i4 \
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。' ^' k0 D6 \! ?5 P/ H# K+ p
( e4 P4 D" u2 K2 N2 R* o0 Z, O编辑modsecurity.conf 文件,将SecRuleEngine设置为 on+ m) H0 K5 v# x; Q: ~7 L9 U
$ f2 ?6 t( T; E* ^/ h$ Q" ]. ^. d/ v: vowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
% u! U, t; Q5 m( M+ w7 Y7 m
2 h& |* O0 W0 [& b$ R, ?/ c5 aInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf4 f9 v2 p1 c- ?1 {* Z! o5 \4 K4 P2 n
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf( _9 R* R" p9 o" M/ m2 w7 Z4 v( R/ a
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
) @8 c& Q2 Y3 Z3 j y) OInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
. E- I# ~' ?- x$ t3 hInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf( X7 P2 [ d% L" i9 I6 K6 e/ X' q
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf8 I: |4 `8 f: {) T. S
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
n, }! F( i3 j. A1 \五.配置nginx2 I4 w+ `/ g5 l- D- Z; o$ c
; C+ Z1 S0 O: a0 w4 V( w) \在需要启用modsecurity的主机的location下面加入下面两行即可:
$ _, {4 I% y# [. K
2 P4 [& e! B/ z2 @( c9 U( j0 Y2 _ModSecurityEnabled on; 2 H3 { _# J/ o$ u3 U7 d0 x" r# t
ModSecurityConfig modsecurity.conf;
, _& }& \$ _$ [2 x+ d5 Y' [下面是两个示例配置,php虚拟主机:
# D4 m+ A; M: n4 Z- }+ n$ _& i; k( H+ M2 m& F; `
server {: S- q& ~0 c5 i: J: o( D, g' ~
listen 80;6 |; [9 U h: U m, [( m
server_name 52os.net www.52os.net;* q# @2 @ w7 J7 A* |, S- b5 k% k
1 [$ g# C5 U) o$ i
location ~ \.php$ {% t3 F; n# u$ q
ModSecurityEnabled on;
8 ?; i% ~6 ]& f* _) {6 h ModSecurityConfig modsecurity.conf;" a u2 E( N W+ }
( G/ l0 t2 d& g0 t& e/ z" w root /web/wordpress;
; u0 ~+ i( c7 Q" F! b+ v& n, y index index.php index.html index.htm;
& i* P# B T8 u) m8 o& z0 ^ ' @ o0 ?/ G+ {9 Y( T( ~' `0 I
fastcgi_pass 127.0.0.1:9000;
' Z2 k/ |8 s9 R" X fastcgi_index index.php;4 X6 ?, g4 A5 i- D0 x$ l& e, U% D Y
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
- Y7 r N. T3 d3 @, |4 f* @0 y" Q include fastcgi_params;8 y1 F& F+ H0 V% H! ~& d4 _) m
}; {4 _7 t1 b; m$ H5 X! V9 R
}8 B6 Y7 p# Y- i8 G6 c
upstream负载均衡:5 @% a0 K+ ~8 A( s% @9 G9 Q
# H+ P1 P: Q. b! S% j6 N' O$ H
upstream 52os.net {+ I8 R# ~ P {* I
server 192.168.1.100:8080;9 k# X( z1 Q7 n% O; _: X1 S% j
server 192.168.1.101:8080 backup;$ n! `; l4 @( L. Z; F' w
}' G+ g! {* c+ X" E O
! c# s( H: u9 D" E4 ^server {
& V# f$ u5 [7 ^listen 80;
# E% A- T! C+ `server_name 52os.net www.52os.net;7 F& V7 d: v: [& |7 u8 q1 |3 a
: r/ ]) j( G7 k7 ~ ?
location / {
3 x. E) X; ^( a* Q ModSecurityEnabled on;
g& `& P$ q: [8 j4 f2 @9 a3 B( w ModSecurityConfig modsecurity.conf;
8 U6 z1 f4 I1 F$ Q$ r3 n/ r6 p$ D0 g2 u) D
proxy_pass http://online;
6 }2 q! S' ^) B9 D, o proxy_redirect off;
* m1 J! a8 R8 g5 P) q. a8 _ proxy_set_header Host $host;
! H; W5 z; j5 z# Q proxy_set_header X-Real-IP $remote_addr;
! n0 z3 `0 }4 m( L proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
. ? V, C7 x+ r% S2 K }* f. v: o: s* ?" U2 _! k
}
2 F' L, h2 K5 m0 l1 z6 Y8 i& M六.测试% Y$ r9 q0 D' [! h! a' v. j# J
. F, ~$ s" Q; `+ [$ h
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
_" _' m# u! v' I$ l8 c6 S0 b$ R& L; p
<?php
5 M: r# G' a+ y: g2 Y phpinfo();
3 {4 D. o" u8 l! S5 p?>% N' I `" C# [! X! [* L+ E6 x
在浏览器中访问:. M3 l/ G2 ?) h
% ~6 k; m8 o) g# O$ Y1 g1 u" t
http://www.52os.net/phpinfo.php?id=1 正常显示。
% Q8 m( @5 a" Q3 A& xhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
1 I# k9 U3 k7 n" U; J" m9 qhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。- c g/ |2 m% a- G3 a# b( O3 v! L
说明sql注入和xss已经被过滤了" n0 z3 t% r( P' ~; {/ {( O& e# V
5 _5 l, q- c% S# k七、安装过程中排错7 G8 N% d: T$ D+ D+ x5 ]2 K
) H2 d8 Y7 f" n( f1.缺少APXS会报错( \6 [8 t! X% v
2 \8 m8 i$ P$ t% e0 oconfigure: looking for Apache module support via DSO through APXS! x! \2 M' c5 r' N, h
configure: error: couldn't find APXS" ]$ L" h `( l" K! O
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
( b( X& H$ }7 l. x" v% b" K9 h解决方法:
J9 W" {% t! _) j6 e( r( F- x" U
2 G$ r$ P+ z. s8 ?9 ]1 V8 Vyum install httpd-devel
* m; h) p7 ~3 L$ g$ @ h2.没有pcre! x+ L$ G* l5 T: V
8 N8 m# R5 g; G
configure: *** pcre library not found.
# _8 U" { g; Z4 dconfigure: error: pcre library is required
9 i1 P4 O' O- l, d5 c8 H解决方法:
6 x+ V: ? L8 w1 U
4 H, y/ F" k; B- f' H8 P4 ^ S( pyum install pcre pcre-devel C) k+ u( {5 T
3.没有libxml2
) Q& A' c6 \# n, ]
: j$ {8 Q2 b; z" W1 n ]- w a" ?$ {; Q ~ s, W$ V
configure: *** xml library not found.
, O. |, z- e3 z$ Dconfigure: error: libxml2 is required
; u/ j5 h \. b3 V" D1 K解决方法:
7 k4 b7 c9 @6 r. g$ i. L: |, o/ _; ~/ e& F" {3 ]0 L
yum install libxml2 libxml2-devel2 G+ `- \; G7 s0 ?5 N
4.执行 /opt/tengine/sbin/nginx -m 时有警告
, X0 [3 f5 p& N- S7 G8 a* I. y+ g* K4 [% ]) ~" S7 `* j
Tengine version: Tengine/2.1.0 (nginx/1.6.2)" _9 }; v4 s- u
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
0 k c6 V4 s. L5 D原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
, |7 k8 B+ }6 ~" c) n+ o. Q6 O0 B; v- B' {
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.! r# c* b5 t$ {$ d/ G" K
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"0 R% v9 o& D/ _! r2 E3 W
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!+ S6 j, @1 w7 ^! P
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
, G9 K' Y2 O7 ?; @# i+ b+ t2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
, ~- Q% g; O0 ?3 G2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.8 m/ J: { y' e. [% e, T2 }
解决方法,移除低版本的APR (1.3.9)
4 W9 v! z( Y/ c+ h1 d
9 n" g ]' ^6 g3 K" myum remove apr1 b t, g) F% {# s
5.Error.log中有: Audit log: Failed to lock global mutex$ M+ }/ c$ z' V! \6 ?
, g4 `9 t& A1 I, N1 e
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 9 `$ V" {8 ~. t) Q: ~3 r
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]4 ~$ n# c+ R$ U3 g9 e! q+ p2 m
解决方法:+ b2 E+ u/ D ` Q7 v! C- l
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
2 b) b# x; W' t
# W) }. Y5 d- w2 r& L: Y" O0 CSecAuditLogDirMode 0777
& r! P# E% q8 ySecAuditLogFileMode 0550
+ [1 R* u" y% X, J1 wSecAuditLogStorageDir /var/log/modsecurity
J* y% L3 m5 y& ]4 ?SecAuditLogType Concurrent
1 Z5 n! N7 Z1 M E& \参考文章:
) [# E ~3 v; l, q5 I( Ohttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX; e6 z& T1 B( Z3 L1 |
http://drops.wooyun.org/tips/2614 |
|