|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
" J, K. v {0 h
9 ^; P- J6 P8 |3 g- _一.准备工作
, g- o6 X1 \; z
/ q0 j7 G8 t' ~( z) k3 M- S h6 q系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.05 \& H0 @4 D% ~6 F
- j( o+ b! J/ B' mtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
* K; l' P8 p0 @) e. n' u, V+ U! B4 d. j8 K6 w/ a2 k+ _
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz! j( k& g) b* p: \
g$ l* L& b- {1 \, @OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
: t' ?! t2 j- M* `7 }& ?5 y) U
^; a5 s- j& [ F依赖关系:
& x$ r2 o8 X: n. b- P6 rtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
. b0 W5 [) {' g# s7 }1 M; x" s1 X! ]- K. V
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
- }7 l: J0 W" s, j8 J/ \modsecurty依赖的包:pcre httpd-devel libxml2 apr
: q! y" P+ b, e! L- |, Y& W
+ R7 m. g" A/ Kyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
- X4 `+ J5 h* b% W9 \2 I+ |$ c二.启用standalone模块并编译
1 ~: e4 V( [$ F* c" @( h( a: \' B, u0 W) e, U) z0 ~$ ]/ }
下载modsecurity for nginx 解压,进入解压后目录执行:
+ P7 f& O1 b# t( ?
Q: ], T4 z; H+ t* e& L./autogen.sh7 K7 p" {) v- I4 g9 B) e$ S
./configure --enable-standalone-module --disable-mlogc6 ` ]! y8 i# E
make 0 T& \5 I/ F/ U# p# m& k; {
三.nginx添加modsecurity模块
! D9 \$ g, W2 M% E+ P8 N# o5 ]; J2 j
. Y, E6 x& |' i在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:# z: y. C* L) K5 Y7 Z( }
3 u5 }, b* Z& l2 N6 i./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine( V/ I3 Q8 `6 w
make && make install
5 i3 {. \- Q. N7 M( O2 p7 H O E# P四.添加规则6 p/ s9 I; C) z& [& Q1 W+ T
- b: j3 v( @& t( U6 Vmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。8 T7 `" \9 u0 O8 W' n/ w; `+ j
: C! z6 i; ?3 X/ L7 s0 l c1.下载OWASP规则:0 l0 _- J3 w4 c# T* a6 t
: O" O3 l5 X7 f; L( cgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
7 a. q/ R4 s0 Q8 c8 k3 m( |, Z# q! v, c$ g5 B8 z% `) V* R
mv owasp-modsecurity-crs /opt/tengine/conf/- q: E, }& V0 A- J& w
o" ^% E$ I# d- vcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
$ ~& ]/ j- F& e6 G& g, y2.启用OWASP规则:
1 }) A, V% V: d7 {5 a& V/ `1 w& H& I& [' k8 {
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
p' y+ U; x& P/ k
6 a/ B% T- z& S编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
( D% K1 @/ P$ Z4 H9 o! b4 o" G# q' }' B
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。& z, x7 Z# C- G( c$ t
! D( T* P: L% ~ H7 I) H7 V* BInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
/ s* h6 h4 C }3 e' c9 `& QInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
& t- N- L6 R' E2 u3 w+ FInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf2 D4 k# z A" r0 S: n! N
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf/ c3 Y$ u7 `5 W2 z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
& S+ ?3 @* _8 AInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf8 l6 q# T# V% L% m
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
1 D" o. x+ }& A; J5 \7 Z7 t5 v五.配置nginx: ?3 O+ P) A0 p; j) M8 A" D% M. b% q+ l
0 i# X% P0 O/ \- R) `6 e) I: N
在需要启用modsecurity的主机的location下面加入下面两行即可:' Y& f+ B) a. d. ^' b R! P+ s
" M6 o g- e: L4 N) K- xModSecurityEnabled on;
; }& @0 q j) _8 B; P2 H vModSecurityConfig modsecurity.conf;
( [! i: H* I' X. D7 `4 Q' @7 d$ f. _下面是两个示例配置,php虚拟主机:3 D0 V2 O! r' _6 G9 }- x" ]
0 |$ ~! L7 v6 ^7 E
server {
# O& R; w- o( l$ ]+ M1 f6 o8 }0 Y listen 80;1 i! a D# t/ }* U; T* W7 B) [# q
server_name 52os.net www.52os.net;! l* F I$ H" x& z+ R" U
+ m( a) w- X7 ^' }5 q* U) R" d- Q
location ~ \.php$ {, g$ L2 o. X: D
ModSecurityEnabled on;
( C5 [# H. [ ~2 l ModSecurityConfig modsecurity.conf;# ]6 u+ z9 o. B
$ }8 n* g6 m9 a+ O0 X root /web/wordpress;
! b: r% M/ a/ B0 e- ^/ n: ^ index index.php index.html index.htm;1 j6 v1 O# y# o7 q1 s, m1 r8 |% y
3 ^3 D. d/ l y2 l fastcgi_pass 127.0.0.1:9000;- t3 A6 t7 E$ b3 L& V
fastcgi_index index.php;1 f# g" y5 q) y
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;, W# U9 O* H9 n1 {9 X. k
include fastcgi_params;7 ?5 s5 s7 U! S' O8 l; q
}
' O& p- M& G4 b4 i- p, k. ]3 m }+ ?1 k9 B$ O4 i
upstream负载均衡:/ ]7 V& |8 a4 u4 t' D$ P; J) N
; r# s) O' c3 t
upstream 52os.net {
/ @* b# \& }7 a4 i; o3 F server 192.168.1.100:8080;
3 J8 r. i9 |4 s& P7 T9 \ server 192.168.1.101:8080 backup;
* c$ o4 `4 g* p! a0 ?}
, W: ]2 V* w1 F1 O% X, o9 C' E8 Z
server {
8 O3 C$ N: t7 ilisten 80;
& J. s# X+ \ t% Q( bserver_name 52os.net www.52os.net;
3 q, d( ?& u+ y, A0 o1 N$ R) |
) m$ H1 d1 E2 U2 n% j! Glocation / {
- G4 D: r) x' R2 F: n# E ModSecurityEnabled on;
( R, C; ^6 g2 }9 ^" ?' i ModSecurityConfig modsecurity.conf; ( U" T9 ^% f2 L
- z3 Z( \( X2 q2 L+ Y
proxy_pass http://online;8 ~: ]+ J$ n3 x7 Y& v
proxy_redirect off;0 R; ?( I* o V
proxy_set_header Host $host;
$ ^+ b% V- V0 h5 ?* u3 F proxy_set_header X-Real-IP $remote_addr;2 u, N: r: `, |( N' f N; i! E
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;, s" [% f# O/ x9 i5 q6 s
}: e9 H6 _) U9 `- Z) o4 h; k
}
- q5 |+ t+ i( G) J) M0 a5 t六.测试
' F, }2 y; h0 K- F6 F, v- m+ m/ C4 I
% F) v, h6 Y1 w3 ~2 ]我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:" |0 g- ^7 ?1 L/ c" v
6 D' h9 r( T+ m- b3 M<?php1 W @: u! W: L g0 J3 ^) {
phpinfo(); g0 J0 z2 Q H3 U& v. [' m
?>
" L) t6 y$ q6 I在浏览器中访问:, `) j9 u0 y/ U9 a: C5 w
4 X! |( v' F W+ K: c, P$ ]http://www.52os.net/phpinfo.php?id=1 正常显示。' f* X, [# X" y+ K
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
' e# C& m0 k1 G& d" y3 u& M0 E' \http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
/ R. q, l' Z" @6 f1 k; A! O说明sql注入和xss已经被过滤了
8 f' I: o( B, n5 P. B" g4 @2 D0 I
七、安装过程中排错7 k7 N+ j+ x/ I) A: k
, C3 G' [2 ?; \- Z! M
1.缺少APXS会报错
! n6 M8 y: d+ q; z: ?
$ Z$ x9 } `5 |0 d; Bconfigure: looking for Apache module support via DSO through APXS) j* X0 v0 N% I; y. n! m
configure: error: couldn't find APXS
. z2 S6 ]) X" a' C2 ] }9 s" @apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
3 L& G1 W8 \& o6 W L% E; |. e% T解决方法:; S! s% E, g: F5 [! q
! v5 M$ L" q- n9 q) A( x
yum install httpd-devel% r& f6 S) V6 b9 y: W
2.没有pcre- f4 H' b- |. t- b
; t% g" p! u9 U
configure: *** pcre library not found.
0 J0 N' H# w5 l* p; E' e+ |) sconfigure: error: pcre library is required) g: b/ F% ]' A# A3 k' b, n
解决方法:
# j# b7 f5 f, |% `$ X- V
1 E6 u9 D! l/ q2 m0 nyum install pcre pcre-devel- N2 T4 s7 J! d) `+ |
3.没有libxml2% y% i& v K& D( p, t9 h
j9 z# S1 m" x2 f$ T2 Y3 j: y. J* q4 n) r( X
configure: *** xml library not found.
5 h/ C* j$ t; T& N, Q9 Cconfigure: error: libxml2 is required
+ P4 V- V4 \* |6 E0 F解决方法:8 U9 @: S ^/ E
9 B& c6 d( H+ h1 I
yum install libxml2 libxml2-devel
; y( L* [1 S9 d4 Z4.执行 /opt/tengine/sbin/nginx -m 时有警告) Z' g" R* ~* A+ q
# l) A8 g8 Z$ X/ O, Y" O! jTengine version: Tengine/2.1.0 (nginx/1.6.2)
5 Q3 o; o* @* s+ F, B; @$ y4 qnginx: [warn] ModSecurity: Loaded APR do not match with compiled!3 |/ X; W% d* ~
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
2 u D8 M& {% v; T0 C" g% S! c% i6 @/ f) ]$ T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.1 t- z# H' @, Q ^; x
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"+ }! W! p& E6 Q8 w3 T: k# O
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
8 U" p/ y' f$ E, |# w: D" X" R, Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
' L/ G# w( A7 ^$ B3 U; [2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
- n: g! _' x' P, {2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.' C* o2 W# Z1 o* t" t5 M
解决方法,移除低版本的APR (1.3.9)
) f' j9 d! ~* Q* L* X
7 ]. p' p; U& wyum remove apr
: P) u: f6 L$ U; o* S1 F5.Error.log中有: Audit log: Failed to lock global mutex
1 H0 C. M0 `/ ]+ d$ x- N# y0 `: S2 A2 X
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 0 m: w( u5 Q: x6 W- G
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]& t d3 S& r# v
解决方法:( V9 q2 F# r. ^
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:7 z; x3 C. B( x
3 K, U: \% S0 E8 b8 A! F; X3 p
SecAuditLogDirMode 0777
$ s( l: u, g7 j: Q7 |# O' @3 e1 H7 F; OSecAuditLogFileMode 0550
8 V% i: J6 k5 u; m1 J# Q/ } q& o8 QSecAuditLogStorageDir /var/log/modsecurity
( A3 `0 \: U" ^4 \6 c5 x5 ySecAuditLogType Concurrent7 Z& d: u' h+ V' y9 G* S: ^" o
参考文章:
8 t. Z2 ^- [* t; r7 P1 y0 U" lhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
- W1 e6 U) ?& Q+ P% J! j8 }9 fhttp://drops.wooyun.org/tips/2614 |
|