|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。0 m" O- O, k% {2 i
1 ]4 ~0 x+ }2 t/ K4 T一.准备工作
6 c3 V' K2 g8 O' h% C" |& V/ h" ?4 c
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.06 j5 o8 P0 s4 I
R s+ K5 h0 P8 |, u/ N4 T
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
/ R9 a; v- l. P2 H" l* ]2 a/ L2 O7 N, g
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz/ b, L" v) p8 t2 V) P( J
! p( B e+ a6 Z# U# d
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs4 ~- t. y5 S7 U; s2 q, i P) d
: ?# F9 w5 h% q/ ^依赖关系:$ g6 N7 y$ m5 C& {; H; j( N
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:9 @9 N5 e4 z3 p3 @
' I! r3 U8 x9 O8 H; V% ~7 o. xyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel# @6 i- `# V! k( {0 r" b; [
modsecurty依赖的包:pcre httpd-devel libxml2 apr& m: Q5 P+ T+ \: [
+ O) z4 W0 B; [" B
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
7 ~) o% g- r* u" G二.启用standalone模块并编译
- t. I, ?2 C' h" A% M/ |0 _3 `5 } f! O E7 \
下载modsecurity for nginx 解压,进入解压后目录执行:4 W4 j# s6 b8 L9 P& u& |% C
0 {+ \, X9 D6 g2 M) w./autogen.sh
/ s1 N" n z" E& I+ h# i./configure --enable-standalone-module --disable-mlogc
4 Y9 U |$ _. h7 ?6 pmake ( o: z4 }8 w; u+ G
三.nginx添加modsecurity模块
- i- x: h+ v- Q7 x1 I! m0 p/ ~; H$ p0 H3 S- N/ |
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
& c/ S) P: O7 ^9 F8 m
- p9 e2 w9 o% P2 F* q: `# i9 Y9 @./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine& c4 ~5 E4 d4 h' x
make && make install
5 V: S6 u- z" u: `- h7 Z四.添加规则
- J' ~, G! J$ [: [. [- b6 y- z& I8 P9 B' X3 b
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。) P; @8 r/ Y- g$ ^3 I6 [( ~
0 L" p7 b& S5 j" T
1.下载OWASP规则:
/ W. \9 W4 o, `2 o+ @ e' W) [1 ~; ^" h( X2 l
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
1 j2 m# N- q) U& @. }9 d
4 {6 e3 i; W3 H3 C) c/ y3 R/ imv owasp-modsecurity-crs /opt/tengine/conf/
, n7 R2 ?! q& t0 T- [8 @; k, T* S* [! c( P
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
0 L3 s n) o7 Y. O+ c2.启用OWASP规则:
2 `( e% u# a& F! i0 U( Y+ l, W) M ^% u) `
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。' H. t: i- P' `5 j' d
+ l- e; E5 b. ~$ m; @* D$ F编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
- k2 c. y1 C, K: E( g7 }& E7 m$ Q. C7 Z. ~4 R$ l7 [1 G
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。) W+ J7 D( [4 A$ O& Z
2 @% |7 i0 z* s0 u9 @Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
( ^5 @" `0 r4 e- W7 @9 E+ s8 qInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
# j, p9 j. `* O8 P6 D# M. TInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
; d0 ~$ G. P6 q3 ~8 MInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf G8 r0 F h- H* Q1 [" H
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
" V; R7 g5 W: H! g9 sInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
8 c7 `: p. g8 e- }# ^, u% x! D* JInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
# b: e, n" m# |2 p U5 M& d+ D五.配置nginx3 a$ p/ c) p( a( p
8 d" w5 g2 s5 _$ x; I. r% Q在需要启用modsecurity的主机的location下面加入下面两行即可:
@7 |+ m" W& |2 Z
6 M& I+ q+ I" P7 PModSecurityEnabled on;
$ Y/ u) C; O: V: M' l9 u, C; V' lModSecurityConfig modsecurity.conf;; ?2 e! s1 v; ?) L' @ g
下面是两个示例配置,php虚拟主机:
- b+ ]8 s/ s( v! C, I# F& j J" Z1 T1 d1 q" A! w
server {
6 `, i1 e6 ]# }1 _6 x+ _! v. B) u listen 80;: X3 v( ~! \( D+ I6 ]
server_name 52os.net www.52os.net;
; O$ Q7 a. t# G# F3 I ( C( Y8 J& _9 i/ F" S
location ~ \.php$ {
9 _9 A0 k8 M4 ~6 ~ ModSecurityEnabled on; + J6 l Q: a6 P( L
ModSecurityConfig modsecurity.conf; k) i+ D4 v' {! x
0 p6 z7 v V$ M. M
root /web/wordpress;# H( {9 y- D5 ?! [' @% a7 T
index index.php index.html index.htm;
; x+ w* y& B, S. P2 x1 Q- _- p 9 P1 a7 J' }7 ~
fastcgi_pass 127.0.0.1:9000;5 S7 F" Y' {( A! f! }
fastcgi_index index.php;
! w9 X& D! Y# }5 K; p fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;7 g3 i9 K6 ~3 D! m' q
include fastcgi_params;
3 [7 B3 V5 E# I/ |$ o& Z/ E }
4 M8 z( |5 }2 k' G: W3 R v" R }
) u: ~+ k( X! ^! Qupstream负载均衡:+ r6 V- Z( Z# d' I
+ F8 C, W% b7 N) ?' `upstream 52os.net {
5 B0 d: j7 Q1 M* a server 192.168.1.100:8080;; U. ^+ k: `3 l# c! j `
server 192.168.1.101:8080 backup;0 s4 Q6 t. g9 g( p; @/ t' u
}( J2 ?/ P- Y# n' j% |; u7 G4 H9 H
5 M1 t4 R6 w. r9 _" O# ?server {
# j. e0 d! _9 l" Z3 M! Dlisten 80;
' x5 e' {& w' Yserver_name 52os.net www.52os.net;. |( {1 L: b; O3 Y* m! Q) ^
0 x: }- ^/ K7 U9 ?2 G# h6 slocation / {1 }0 z+ p/ M) I" m" \) C& D
ModSecurityEnabled on; # @- I# r1 l( ~
ModSecurityConfig modsecurity.conf;
6 L* Q( [# ~% S0 l+ {
3 a ]) U0 d- {5 K8 r% \ proxy_pass http://online;
' j; |4 O2 y: j proxy_redirect off;/ o! ~$ b- J6 t& r# |/ W- K
proxy_set_header Host $host;, i! j' \; l$ ~$ k3 Y0 Y6 p
proxy_set_header X-Real-IP $remote_addr;
) V$ M( @3 {, o4 F" u' t: b proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;0 V' j2 t) }0 P+ m- m8 E9 [7 F1 q) k8 G9 E
}8 O6 S* P* _3 V0 S2 o1 S/ G
}7 v+ Q6 d0 p. a: Y F3 S D8 @
六.测试% d8 Q+ V7 {( U! N
: S3 t7 V1 @ t) x5 ?4 P1 B我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
) ]0 n3 L: ^& g# ?- y
& W2 w1 W5 q9 j<?php
5 l* j1 n: D5 ^' l2 f phpinfo();
^1 t) k/ m. n/ ~7 N?>8 N |) D% o) Q1 Z9 w8 M$ w# T1 C
在浏览器中访问:1 m" x* L& x! w# A$ D" @3 [
# ]" e3 Y- K! h+ E
http://www.52os.net/phpinfo.php?id=1 正常显示。$ _+ D; p5 w* R0 D0 J( T0 K! h4 Z
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。1 U3 E L; N# h# H: W
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。3 ?8 g% n) ?8 O
说明sql注入和xss已经被过滤了
+ _/ W p- r$ ?! c9 Z7 J- X5 @( a* j, b* L0 i
七、安装过程中排错( [3 `8 G' E! E. T8 @: v
' f- p: ?" X( J
1.缺少APXS会报错
1 N6 K6 P1 k) s( G5 F# Q; r+ r8 v8 j/ S; V
configure: looking for Apache module support via DSO through APXS
- [: B/ F: ? q- r9 P+ P3 s% h) zconfigure: error: couldn't find APXS
( S6 T: |% Y( V, ]/ k" Z1 H" }) eapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
x$ ^% b) o: g b8 u! ^解决方法:0 b. W+ r2 k5 v0 K* I; O; E
. L7 }4 s+ n0 }* kyum install httpd-devel# f- Z' a/ v" M( E+ B
2.没有pcre+ z9 j m' T5 U& \7 x4 w
/ q, a" m8 Z" w: O
configure: *** pcre library not found.
2 Q! n4 L6 |/ f3 b: sconfigure: error: pcre library is required
`" a# @2 w: p0 e' L5 {- B9 `解决方法:; D( m+ B0 X# Z* X* N) a& D
2 z; v" \; B; j6 f0 M! ~
yum install pcre pcre-devel3 B3 }: y9 D1 }$ v. x* r2 X8 I; ]
3.没有libxml2
' s2 |7 h- x6 T0 `1 [' D: H: Q- W9 B+ R5 M
I% ^7 U! K, u& k, g$ Jconfigure: *** xml library not found.% F d. J/ \8 B4 Z& U
configure: error: libxml2 is required
+ L, u5 o- [4 E, B解决方法:( x1 Z' J% g- s$ y" u/ }
2 v u* K/ ?* A! T9 u; qyum install libxml2 libxml2-devel+ K: q: _: h4 X8 |6 x2 E5 ?6 R
4.执行 /opt/tengine/sbin/nginx -m 时有警告
/ q+ D& G- [$ o0 h9 m: a! S0 m' r
4 h. f/ n0 @6 g* ~Tengine version: Tengine/2.1.0 (nginx/1.6.2)
; G# p# t& x4 c( E$ P- |nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
7 @! f2 `1 A* e原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
' J5 x- K/ A5 ^5 C, I
$ X2 _' }, _/ ]* v2 }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.+ ^5 H0 k, }# X! w! P3 ?9 Y% n
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9" d; T' b$ r! }# C1 s2 i
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!& F F- {/ u) F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"2 L4 O. r' x( y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
2 x- j8 [5 E2 f( @ F6 B4 M2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.4 k) Q* ]0 y; \: g, H' S
解决方法,移除低版本的APR (1.3.9)0 Y( X- ~# a/ Y' w9 M
- k2 V: b3 W4 S1 m- eyum remove apr
3 \+ v' z7 A5 K5 q% }5.Error.log中有: Audit log: Failed to lock global mutex
/ w' D5 s8 ~( k6 |/ G7 n0 z s9 f& j$ h' y8 ~
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ' Z4 Q0 |! O5 j/ P0 Y; |
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]: T( G$ H Z4 j$ h+ @
解决方法:2 C/ v( H' o6 V* J+ W$ M
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
6 b" R! ]7 u, d9 _2 w2 C$ \5 F
8 Z: L0 m0 d( v, ?* g" ~SecAuditLogDirMode 0777. k' a; }9 K+ n& }* w& e
SecAuditLogFileMode 05507 j$ q2 O% C$ X! |4 s& U A5 n
SecAuditLogStorageDir /var/log/modsecurity
+ i, w7 z, Y$ D9 `" }' i/ oSecAuditLogType Concurrent
6 c" N- Z7 G$ `参考文章:% n% N$ A, {7 e6 }1 z9 V
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
2 Z" t! W7 g8 l9 ]1 shttp://drops.wooyun.org/tips/2614 |
|