|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。/ Q4 b) B& r" a3 ^. ]9 x) E
6 b3 J& L9 c" s一.准备工作. C! P m& C2 L0 r3 q. a1 {& S$ M
9 Z% K0 u5 f" W7 r# ^ {. ?系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
3 P0 `$ v6 c/ n2 X: p! C4 Q: }1 l
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz# F" z1 ^. E, z3 h" O6 H4 s8 `
! N9 h# Q: m1 @+ N0 C
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz% {8 p9 J g9 }4 |8 H
8 ]; R) _- y( K$ W: E8 ]# X1 [OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs# j( a z* {* k/ m+ w5 [
* [! k* }- {1 \9 k8 c' t! ?
依赖关系:8 y* {' ?$ O9 N2 m
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:9 P9 c8 g- d2 g7 ?! F3 Z( E1 R- H
; _- X' K' v4 \9 t+ u
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel, u) r# V5 C3 V6 }, s# @0 n& C% ]
modsecurty依赖的包:pcre httpd-devel libxml2 apr- I- J% n- [# T6 a5 W* a3 z
& b& m: A! E) z% u- n+ Gyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel' c2 q. u @: {6 R* Q
二.启用standalone模块并编译
3 |. P# F; W, _3 `# x/ h T. P
5 z6 L3 ^5 h6 Y! @, _( G) \. l下载modsecurity for nginx 解压,进入解压后目录执行:' f9 a8 U4 b# I8 K
' O8 y# J$ a- z+ D( }3 J./autogen.sh
+ \3 g2 R$ ^% v5 T# t# a( h, v./configure --enable-standalone-module --disable-mlogc
- z/ z' F. I8 wmake ' \1 `( T8 v% w: U# f" n( o- g/ W
三.nginx添加modsecurity模块( p, W2 T" x0 w, k4 z
7 a" ?& S8 `) b
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
8 C T6 X( s" m- i' Z4 a3 v
. P3 U% p( x9 A, ?0 v./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine5 p" `( _, x: b; A% u( d2 F, @5 s
make && make install! {% D. t1 _( S+ J$ e- W3 \2 e
四.添加规则/ }" p! E$ ]! b& f$ \
$ x3 R& n2 J7 g, N% M* d. J# X4 |
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
" f- e* y' Y& y' ] ^' X- m2 l' h3 t/ z( A: [
1.下载OWASP规则: Y1 m X& \% N; e
$ C) x- Q) {* }8 Z* X% V
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs3 l4 p l' i2 A, I: O3 r; N
8 z- D5 `- B. t; W
mv owasp-modsecurity-crs /opt/tengine/conf/
# k* _+ {8 r8 [% O# a5 A9 A
. V% j& m$ u. a4 }! U4 F$ [cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf3 @; W5 L5 r; @ K6 _2 u
2.启用OWASP规则:0 A6 M- s9 M/ ~ L; R8 i
" d/ \* b/ r2 G$ T3 W" Z% v复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
8 n& ~7 p! P& B" Y" a/ q
+ e8 c2 E4 u* X) F编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
9 I, K& m1 c2 d0 H
' a# C4 Y* F' l+ I: V! Jowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
$ s4 [% c( q- _ ?, D& g9 {. G* @. b: i
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf% L7 r- L; g& ]. u/ U% V) W6 v4 z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
2 y( r2 u3 I; n# g. jInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
5 a; Q5 i% r& L# cInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
( S8 `! J% M. s. s# J; e+ N, pInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
- x3 z2 V# ?7 E6 g; l. p) RInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
! Z2 v( }) s8 @6 p. G" YInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
0 Z9 q: ]% B. q. }6 s五.配置nginx
' c" b: q5 @; @' F& j1 F9 a; K
4 n1 ~/ J' ~$ [0 z' k4 N& E! s. ]3 `在需要启用modsecurity的主机的location下面加入下面两行即可:- ?( E/ h& b$ _ d2 `1 e" J
7 \7 o# k, }8 w- [% m \
ModSecurityEnabled on; 0 | c/ j/ C% C/ m, O! J
ModSecurityConfig modsecurity.conf;
) g8 I4 F8 `4 k( k0 s" Z* t下面是两个示例配置,php虚拟主机:) N3 j+ }% K# v; _8 i- y
2 y# y, ~3 J; M% _6 K
server {
7 X, b5 L' f ]( k8 O listen 80;9 z" u5 W+ c1 `3 S9 p9 N
server_name 52os.net www.52os.net;2 G' A s3 v( ^& Y9 o
1 [& v) Q* [0 s( p4 S; K ? location ~ \.php$ {
( x/ a; h* n" ]: ?6 N$ i, Q ModSecurityEnabled on;
- e; _5 F) w# J# x7 j ModSecurityConfig modsecurity.conf;" k9 ]& x" |( p6 z4 C' t
9 {5 O/ u: I+ C
root /web/wordpress;
! q$ k0 m5 a1 l3 u# e8 w. q0 X index index.php index.html index.htm;
0 |: h" g" X4 C. x$ N/ Z9 q " b% B N! @# O# I# Y: U, O& N1 c
fastcgi_pass 127.0.0.1:9000;
! E5 c* ?8 M8 ~, T& o0 q; z fastcgi_index index.php;; ?5 f4 m) K6 W% F3 {/ _0 C
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
6 g- E: M2 ~# T' }+ n include fastcgi_params;
, a5 y( C' n4 K5 |* L! v- {. J }; o: N% x! z3 A# u8 d) O! P
}
$ U N1 Z& T7 ^9 s/ Eupstream负载均衡:
- g3 F! `1 S/ D1 W% o9 Z% Y
2 t2 ]- m9 f% F; ~* b8 T# Rupstream 52os.net {" Q& M- K+ \! l9 _/ G
server 192.168.1.100:8080;/ n1 D' j- Y4 y9 X
server 192.168.1.101:8080 backup;
s" _" F; Y# ~}
' a+ f& }# y8 k7 R( H& Q
; v1 ~5 S0 H" y) Eserver {& F3 {* i& Y2 o# p
listen 80;
' h" T6 I* h- P% F# [# i' rserver_name 52os.net www.52os.net;
& ~3 e# f* S O/ P8 C) \; S. O1 }! @- X1 w+ ?
location / {5 T+ b! R& R4 h ]* H" x# R
ModSecurityEnabled on; 5 q0 l- C; K4 a! x/ q g6 L' X
ModSecurityConfig modsecurity.conf; 5 e+ M3 a- W0 U/ ?. o
( M) F& T% |% y% x proxy_pass http://online;
7 t% X z4 t4 g( {; }! E# y7 p" q proxy_redirect off;
" W2 A- |7 C& E( @( [" v proxy_set_header Host $host;) o9 I5 A7 L+ b9 L; t/ g
proxy_set_header X-Real-IP $remote_addr;8 ]4 w2 U2 X y1 P
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# Q+ v6 T; B% u }
# w* E, J5 q6 K7 p! s} N7 s/ Z3 F$ M+ @/ d6 [
六.测试; }* m, h8 S" h0 z7 Z p
$ m. q) M9 ~ U( o2 L8 W- o. r我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:" M( m x! D' t# h. D
3 e& a2 b- ^9 E- g( k<?php
5 n$ @5 A' G" Q% \& e1 e% r$ W phpinfo();
: Q0 f1 ~9 w2 K& _?>& Z& P) k0 D A$ \9 k
在浏览器中访问: M+ f+ X1 ]: L7 n# ^% L' ~
9 L0 T; S$ Q4 w _6 mhttp://www.52os.net/phpinfo.php?id=1 正常显示。2 B r3 L E0 g
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。/ e/ q7 [6 x: h# N0 X/ a# r
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。1 [' f3 g) Y3 k0 | h
说明sql注入和xss已经被过滤了
+ u' f: h. Z4 _# z, V+ G P/ f9 c" e/ u f: m6 H6 K5 b3 s
七、安装过程中排错
6 e3 I x9 h E7 E/ a V' S& T5 S U9 h7 h6 K: B
1.缺少APXS会报错; Y g2 M1 P8 y( T3 a7 ]" i
3 A# y# A0 V& a/ t
configure: looking for Apache module support via DSO through APXS5 N9 p" k! R' A8 n- j3 l, w
configure: error: couldn't find APXS
; L: \+ T9 b/ ~8 t, f! zapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。) Q$ j; ]7 l/ L2 G( B* ^: w
解决方法:. {7 W/ |* B' W# i- S0 _/ g, H
8 q' r1 U& O- [+ ~3 M9 ryum install httpd-devel
+ m+ d* d8 B3 q8 W2 ~8 p6 i2.没有pcre
3 [' s$ L# l% f# B; B0 h8 I; t6 P S6 i( l7 j, a
configure: *** pcre library not found.
3 J8 U8 u0 H* `3 Wconfigure: error: pcre library is required
& U1 P5 r* _ Q# N; u5 o解决方法:3 S: J9 v8 s. J' v! f' c x
1 C, h1 u5 d. ^: o+ |- cyum install pcre pcre-devel
+ c, y& {$ T& u6 P0 v6 Y3.没有libxml2
4 ?" E7 t3 s0 E6 W2 r3 {, K* s- I, m# Z/ p. e- _8 w+ U- ^
f C1 M0 T' Y) l
configure: *** xml library not found." G* u- t5 M5 H; k1 N0 A) R3 `& P
configure: error: libxml2 is required
/ f I+ H* z! u$ b5 s X9 F* F解决方法:
# q" p3 _$ D9 e' ?
2 ~+ W; o; z; ^# x/ X( Wyum install libxml2 libxml2-devel
* G* ]( M5 \* k. Q8 J4.执行 /opt/tengine/sbin/nginx -m 时有警告/ L5 i; _4 V. s5 d. |/ e. z' l
5 x3 z2 _' y6 F3 K: [/ R" kTengine version: Tengine/2.1.0 (nginx/1.6.2)
" B+ {% s8 f& b* M2 H! s7 Jnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
* Z3 a* l7 V5 J. X- |$ H原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
2 j: J ^8 J' L+ H v0 o; V8 N5 I% L% ~2 `
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
: [5 v) W: i; R0 d$ y, l9 `( m2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"' `7 v" m! E0 o E
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
4 [9 r. z* [; T% D" f: N2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"* o: L2 `* @. ~3 G, U$ v
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
5 f' W# `+ |% `( U& ^2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.3 {7 U" Q! A! c
解决方法,移除低版本的APR (1.3.9)
; t5 B j( C$ x1 Y
, t; [* r8 d! l5 ?8 R5 a3 N; xyum remove apr
" l; \8 E' W( Q2 w- Q( }6 f. C+ E( s5.Error.log中有: Audit log: Failed to lock global mutex9 q# c' \+ d3 h K* R
3 C6 H' ~, F) S( t( B* @/ j2 }
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock : Q) @/ }5 a2 O+ G Z% a
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]; W, i5 i6 C$ e# c
解决方法:& i# W5 Z, p0 ^. Y, r9 ]
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
% O/ W. j2 `4 w1 N- u. m4 {4 N# |" G# M+ ^
SecAuditLogDirMode 0777% V* q1 k9 l- j. f4 M
SecAuditLogFileMode 0550
+ G3 {3 S+ W- ?. w% f* f- ASecAuditLogStorageDir /var/log/modsecurity$ h% A g% ?1 Y' @5 L/ _
SecAuditLogType Concurrent, u! q8 E& s" s$ O3 ~/ O% P
参考文章:
2 W) l0 G. g* V6 j, Lhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
) F' O) W( M( {: ahttp://drops.wooyun.org/tips/2614 |
|