|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
$ p/ j/ p) d, }" r, ^
" z* Z! J ^5 F+ S @3 a一.准备工作4 H) K! N" l4 e5 E# U1 R
' k) u, \( Z1 X7 l9 L' K系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.05 ?" {3 Z1 r5 z2 a
; C! Y* Z4 ^/ o5 N' \' A7 C+ V2 Htengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
8 r# }9 \+ V7 D% X# e) B& Y. R1 u8 _( }9 r* N# h3 _) I8 D
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
( ?, ? o' t4 _5 ]7 F, @$ ~2 v) ]! q7 ~/ u Z' \1 k; D& s% Y3 p) ]
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs. u2 Z- G% s: f' ~ A; h F# a+ Q7 O
' V8 ?" _- {7 G, }) g依赖关系:
. ?7 H4 W. W6 G, ztengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:- i; _: I* G' s# i4 R! Y
* e5 l8 ~* w3 v( h% W# |( B. nyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
7 E- k }/ E& z& i( _3 O# `modsecurty依赖的包:pcre httpd-devel libxml2 apr
5 Q* j: T" A9 \/ @
6 e* J |3 H+ p5 eyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel' [% D' P9 m# g- o
二.启用standalone模块并编译
! A) x3 C6 Y5 O: }0 H. M; X0 l7 [; Z$ }9 W# R! \4 _
下载modsecurity for nginx 解压,进入解压后目录执行:
$ g; ~) b- @2 s9 {% i5 o
1 E6 ~ G, M7 T0 k./autogen.sh
4 J3 E# y% n" u+ [./configure --enable-standalone-module --disable-mlogc
0 y; e- c8 f/ V5 {make 7 y! J- v: i$ w+ X& r/ a3 B* n" `
三.nginx添加modsecurity模块
i S" R) g) O: S {! ~
0 x( s9 B% f8 _1 a+ F在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
d. @3 H( k3 H# ~# v! ~. i' L+ F9 W4 h9 [
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
& V1 ^& @& { K7 m# ^6 B; h* f4 Jmake && make install
/ u' e( w- p; [2 g: M4 K- @四.添加规则
, R u7 z% g! `$ t3 s% x# O z+ Q$ z- j$ d+ p- ]* _
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。% R" N2 F& i4 p
* G4 ~6 ~ d$ Z: A; A# @( |3 I1 E1.下载OWASP规则:
" H% V4 G z$ e8 U) K
4 I, {* y' y/ i2 fgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
/ G/ E, }" a9 L- K4 s- g; Q+ }$ ~2 e" a' i, \' v9 y" ]0 ?
mv owasp-modsecurity-crs /opt/tengine/conf/! i7 r/ {9 W. J
. f9 M. ~: X5 n# f; A8 z
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
$ j( M1 I9 C5 X) N2.启用OWASP规则:; C+ ?' C2 o: C& ]
7 ]$ M/ J% M, C+ F复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。7 p* C' T0 r( t. ~$ d5 \8 j% q! V
- c1 O5 B* S) g+ L, \
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on* \% }2 Q( r2 `8 T0 \
+ Y( Z. T0 l0 N8 F( J! sowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。* h+ `& n7 `' c" E
( L! P, L% Y! b& z! E& u0 A
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf }: j- u" {/ f% g% _% O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
3 K. R, N9 y" \. y5 \" c5 RInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
3 ?& w6 B; O; }( A; q6 iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
0 X7 c, W) t. nInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
$ c) R( _/ }" @; I% _Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
: |$ f# A: A% c- O5 dInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf: C8 B) @7 w; i
五.配置nginx8 g/ o! F n" T9 R6 q% A
! V7 p- B( B2 }; g9 p0 q
在需要启用modsecurity的主机的location下面加入下面两行即可:: s9 Z! d' o3 L' ?
6 ?" m7 n; E! |9 ~6 ]
ModSecurityEnabled on; % r: m9 @6 r5 ~" H
ModSecurityConfig modsecurity.conf;
' ^9 F' g% M, r/ D; o下面是两个示例配置,php虚拟主机:
9 \+ E! m/ f5 x' D( S, Y4 \& Q9 t+ D
server {
( \, z, I2 h" j& i s+ n7 A listen 80;( k# E9 D! y9 w5 @- R0 c/ J
server_name 52os.net www.52os.net;
+ ?* w" A! w0 _0 _8 O ( |& n* p; ]* I) E) G7 d
location ~ \.php$ {
: ]6 r' L* R* A0 f ModSecurityEnabled on;
6 H5 C y4 O, b- p- } ModSecurityConfig modsecurity.conf;
# R& j6 J2 M% m! V; Q. C2 @. X
6 \2 [9 Z* o" w4 n* \: } root /web/wordpress;
8 M7 r) g o% i* v& X6 ^ index index.php index.html index.htm;! u) X+ R0 v/ V6 ?. m% ]
$ e4 {; n5 I' Z1 \1 {1 ` fastcgi_pass 127.0.0.1:9000;
0 |9 [9 x R5 [/ E2 k fastcgi_index index.php;
1 Q+ M% F+ W1 `% e! a6 b fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;5 W G# x* \* w+ H. D4 ^
include fastcgi_params;
$ i6 d/ Y; a& o4 \! `2 [; d }
) C! O. i) ~/ r" Z }
+ x+ Z4 |. b" U. [0 s* c$ p6 rupstream负载均衡:9 |8 l" I k+ u. v" v4 @2 ]
2 m$ `# g- B: H# `9 B6 ~; Z
upstream 52os.net {% y) f1 _2 D) ^+ D
server 192.168.1.100:8080;8 `6 l: i) I" _9 M
server 192.168.1.101:8080 backup;
?* P# W6 ^: v}4 T$ a1 p# b2 V6 e
0 O2 ^5 L! A2 w+ l, ~+ \0 Fserver {$ ~* a9 Q$ D' @2 Q: y
listen 80;
4 z, O, |# a+ K( ]! X7 I7 qserver_name 52os.net www.52os.net;
3 m7 s" G |* l9 l" I' \, |4 E" [. f$ [
location / {, c& \4 Y7 {/ ^1 h% I: W8 D. T
ModSecurityEnabled on;
: E5 u/ n' R; H/ L, P6 U5 l9 C ModSecurityConfig modsecurity.conf; ' O" Y( O Z! M' E g" E% \
% m2 p4 _, T r, }
proxy_pass http://online;
0 ^% d% n; S9 R2 q+ n9 U1 M" H; Q proxy_redirect off;
7 z1 A0 h0 v4 H" ` proxy_set_header Host $host;! `2 g1 u/ k% q
proxy_set_header X-Real-IP $remote_addr;
& E8 k* l3 Q3 |6 I proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;- B/ i/ c: \! K+ h4 I" B
}# o9 X( U. @ d1 }: ^7 w* }
}
( ~$ D: J5 Q& b6 @$ c- c' M5 P+ d六.测试) O& j4 w9 G8 k
; w/ o& V' {; @我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:5 x/ d3 c0 f% ^! ]! n$ {
' v q* d, V3 K2 `<?php6 L# s% V" u7 |( m c d8 m. X. b
phpinfo(); . k' ~* M/ T- B8 j
?>
$ N6 i- X5 b; r V在浏览器中访问:
$ H3 g W1 H, K
3 {& [, }. ]3 y( m, Ihttp://www.52os.net/phpinfo.php?id=1 正常显示。* x' E8 D3 @+ F
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
6 Z" D7 W0 z4 ~7 \http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
$ E! h0 Z; b2 R1 |1 O说明sql注入和xss已经被过滤了) V& g N5 s) I$ c- n! i D
& Z; I1 G4 E; J1 P
七、安装过程中排错
. `' d+ k2 g6 h6 N# `
) c1 t: U# L9 x$ I1.缺少APXS会报错- f o1 M. j+ I+ V$ H: q. y- W: v- u
; w1 ~0 `0 p7 j7 Y6 mconfigure: looking for Apache module support via DSO through APXS
4 N1 V2 N O8 r7 H- a9 A; g/ n" w" yconfigure: error: couldn't find APXS
! J+ g; m' K2 O4 capxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
' c* R2 \- _5 E解决方法:
5 i# [# N3 ~4 Z+ I. g
+ L& n+ W4 Y8 ^# f$ Z, Uyum install httpd-devel
5 W _8 E1 s) g0 |( F2.没有pcre6 \: @; o& M* p/ m+ F
2 v: p4 z7 s' ]& v5 D+ ~
configure: *** pcre library not found.7 t! j1 h' m, p- r9 x+ q
configure: error: pcre library is required* c1 Y! Z' K' f. |1 K
解决方法:: I/ P: d8 y9 ~) g
" e+ _! H4 o$ i" o
yum install pcre pcre-devel% u* \& @6 e* n. R9 K3 g
3.没有libxml21 u, T/ _+ j& K0 c* f1 R
1 f! g0 s$ r4 K# C1 Q2 y8 r
" ?& s- }, R7 b/ Y$ l
configure: *** xml library not found.
# e9 a0 l6 Y1 A+ U$ \configure: error: libxml2 is required
- ]& C9 r, Q, M. v R% `) ^/ t解决方法:3 m. Y/ ?' [8 O
7 z2 Z1 T; {. k/ g8 e
yum install libxml2 libxml2-devel
9 p! \ E/ F2 i9 h; U6 e4.执行 /opt/tengine/sbin/nginx -m 时有警告- C8 L) u7 Y: r9 x7 ]# c' G
" d% }; n+ E& ?& i! NTengine version: Tengine/2.1.0 (nginx/1.6.2)" H' W, C# d2 K& ]& `
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
3 t8 l3 ?. v+ P, B: L, Z; e原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
& A( N4 d g/ L& C% |+ J' N
& ?3 A, z- F, j* I- Y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.3 |( b; n4 P' v
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
9 M. S: V+ ?/ G0 {2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!" B+ w2 ?8 p+ X! e/ u: c& F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
/ C! u' b1 y n2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
( x* r" h% U; c m" A2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.8 A# |- k4 q! B& F, m; I
解决方法,移除低版本的APR (1.3.9)+ X `! d) h8 {8 R1 h
; d& c. J0 c6 O8 \' K
yum remove apr
: e% d4 k7 T3 }+ m8 k5 H5.Error.log中有: Audit log: Failed to lock global mutex
% N+ g8 O; |& M& K7 T2 S. x4 V
7 I& x& x3 n1 ^+ I3 |3 l9 x; Y2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
2 b5 s- i+ i0 j+ Y, Y uglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
?% l: o: v; \. a# E0 C解决方法:% o" p. k; s0 \8 c/ z1 z
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
2 i. W- d. T$ {$ r8 f8 @* j3 v0 F! C4 A4 h9 t
SecAuditLogDirMode 0777- v" k# k+ Z$ k( k+ s
SecAuditLogFileMode 0550
5 c/ s* k0 c" }) W6 x) uSecAuditLogStorageDir /var/log/modsecurity/ M! S+ U9 {- w) t& L+ U/ M
SecAuditLogType Concurrent9 r, w$ M" w- \7 R- X- d
参考文章:
# W4 Q+ t* y" u) o: Rhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
9 T ]2 x8 A1 o5 Zhttp://drops.wooyun.org/tips/2614 |
|