|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。7 h; w; {; B6 a
$ Q- c. [8 ^( [/ U; I一.准备工作5 C5 a6 b. O r+ z! D
3 v. ]" h! q6 h6 v6 h9 q
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
, I( d" X; p! U) Z7 I, v4 H" s* y1 \- B. L7 O& g6 x
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
0 @; Q w% Z* S/ x) @% {0 j5 {" ]9 Q) M8 A
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
5 n, f% e0 J! [1 V b. M; ?! E5 X: O& r% O3 c
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
' B+ Q3 ]: f2 H/ i# J
7 O0 m& v* T; \7 m5 H8 ?7 ]9 A依赖关系:
, H# y/ \( \, u" X/ y6 v1 itengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
( |3 D, d& B1 ]8 n% f) b6 M0 S* b$ Y- h7 _0 Z% G
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel O/ w5 O, H4 R
modsecurty依赖的包:pcre httpd-devel libxml2 apr9 a' a2 K) X6 I" @# J$ t
. y( M8 a y- v. _6 Q
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
' h6 b3 j" d3 w1 X( K* B& f二.启用standalone模块并编译( A: R% ]8 t: q4 ^5 b+ {5 ^9 w5 x
4 O& X& k- L" V" @: r6 T& c
下载modsecurity for nginx 解压,进入解压后目录执行:
* D3 p3 }8 _' m( ~' A7 {8 C/ \/ T* t) g
./autogen.sh2 t- I: e4 [ m/ ?, `9 W, n9 [5 a
./configure --enable-standalone-module --disable-mlogc0 g/ y- S2 \0 m" d
make 0 c+ g6 X, {4 \; I- p
三.nginx添加modsecurity模块/ } N( G# ?1 x0 b( u
% J8 Q+ i4 O! s# d在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:+ Z3 s2 I$ m0 J3 r! P( P: e
- f$ k8 q9 c, Z) x" N$ a./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
9 H8 g3 a) ^. w6 B- Kmake && make install4 `0 R" l5 V; R3 i9 Z; R( q$ M
四.添加规则
# _! T) _6 k3 f, j( C9 W7 c% A3 I' W l @+ q5 ^
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
" H* U/ U- M: T @' ?
- d7 O* l5 X* N- E+ k% M1.下载OWASP规则:
7 _- } ^' A9 J4 I$ j- `
& `3 f' M5 T' Rgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs( R% I9 W5 G2 O
5 Z4 i! S6 Z9 y6 ~" kmv owasp-modsecurity-crs /opt/tengine/conf/
# I1 ?% T, A/ r( r& N- p8 A8 E v; f- {% C
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf9 `' U& R ?* y/ \' u
2.启用OWASP规则:
: F6 h! Y2 P. q
4 H$ }8 \. l5 G" j复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
0 ~' M4 V9 Q5 S( w0 Q9 v! m5 P
1 f$ [2 T4 c+ b/ S. t; E6 D编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
; ?( H4 w5 K4 {& Q; p' M! a8 E# L* R/ Y4 ^: V% i3 R$ f
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
- S+ M: x. Z+ f, N& N/ |! ]! T- Y: y0 c& ~7 t
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf I1 r$ b- f" M; i
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf. p9 |3 F5 ~" v# T z. k$ ~
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
+ t* {) Q/ H0 O: i O- A; y# d! KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
4 } I$ E4 J! H9 s/ |Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
+ a# L$ u n5 B$ K0 Q6 JInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf F) y( Q; i' ~ T( O9 v( d0 @- N* T
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf+ \& i" D6 T0 {3 S; a
五.配置nginx
0 w& P6 p% W8 u# c1 l
5 T0 \8 d" v0 i9 L- e在需要启用modsecurity的主机的location下面加入下面两行即可:
& W$ {2 w7 f! ~9 Y' t
6 y) r- ~3 e0 Z, h* f2 AModSecurityEnabled on; ) t9 f T4 |' Z9 c$ w C7 `; T
ModSecurityConfig modsecurity.conf;9 C" Y* Z) U7 \9 ]: q7 l
下面是两个示例配置,php虚拟主机:# i9 W- D/ C4 e2 K0 _
: O" t" }: n& e- A9 t1 D: A' dserver {
) E; W& p2 S9 ]/ v8 }; i. V1 E* w listen 80;: M' k0 K( I6 Q/ D. u7 H4 B
server_name 52os.net www.52os.net;
2 J$ Q4 d: J# J' I * J+ W4 \; S$ {* g9 |
location ~ \.php$ {5 e6 ^. J4 _! r7 t* R( B
ModSecurityEnabled on;
' {" ]! I- H1 U9 E0 ? ModSecurityConfig modsecurity.conf;3 O& L5 U7 s- } t
, v% y% {5 @* b0 e root /web/wordpress;' o; A: {: b0 [7 M; b' a
index index.php index.html index.htm;
" p; I( \5 E$ h2 Q. L; |( [
3 @& T; h) {9 M fastcgi_pass 127.0.0.1:9000;/ Y& f, H+ m! \2 u3 O* ^
fastcgi_index index.php;
2 ]4 |. j! t- c* \ fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
$ k- W, x0 ~( b2 J4 v include fastcgi_params;, Q" O2 ?0 I( y
}( ~& f8 f$ j/ Q# D
}' a9 F. h, A7 u! X
upstream负载均衡:
0 X+ c3 Q, c4 w8 C) O. y
! q2 ?! n1 _+ R: ]upstream 52os.net {
% S$ ]2 Y0 O" x. Q; z) y' } server 192.168.1.100:8080;+ d- q! y; h% b- o+ O0 a
server 192.168.1.101:8080 backup;$ P' G! F5 L! _3 P
}7 M0 K4 Y# _( N! N' b
" A; `1 J3 U4 ~9 X6 L4 `
server {! I0 o- D/ I' [: A% e
listen 80;5 A- H% @# H N
server_name 52os.net www.52os.net;
4 Y3 H" _2 ~2 f& I7 H! a% I- \( x. H) h- q- M2 j
location / {
$ w9 @. f. ]( p5 }* R1 o0 ^ ModSecurityEnabled on;
. q& t; `2 Z9 ~4 b1 B4 @ ModSecurityConfig modsecurity.conf; 2 W7 e) v5 C1 f
+ z. \. _, {+ R% P% X I proxy_pass http://online;
7 B( Q: E) B) o9 O0 u proxy_redirect off;
- R; W) s% Q% b8 j* n6 W0 } proxy_set_header Host $host;, J! j5 c. \" h+ }" b! P8 x
proxy_set_header X-Real-IP $remote_addr;6 y9 z' {2 v- s
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
! O' y1 `7 X) s7 a: N& G! {* | }
. y( R4 Z. a8 |}* \. \ W' N) t ]' t) C1 \# @! N& i
六.测试
! A8 S+ y5 \' I( x1 [
. {4 m) j/ k8 \3 e/ n1 n我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
# K8 U6 [! ~; K a2 U% U9 A: O7 x" G
<?php; U/ k3 P, o& t- t% \. R
phpinfo();
, E& Z5 \" x# g; p" v: I?>
- Z ]$ P( M! D! N$ c8 \7 |在浏览器中访问:
9 a" K/ \2 ~! O
. L7 R% p$ c/ G4 Ihttp://www.52os.net/phpinfo.php?id=1 正常显示。
& w* N! Q, f" P+ hhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。: X" M9 G6 b$ q! }
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。! E- O& O! _) M3 K
说明sql注入和xss已经被过滤了6 g. t. ?& N6 `, R% h% a
0 R& b7 W7 \! V+ |% E+ q七、安装过程中排错& i9 c" r3 L# @% Z0 B2 [4 F; x! v; O
& `% D8 [- [( y7 t: }1 l- E
1.缺少APXS会报错
( C0 v* r2 t ]' k$ E0 t3 r2 J
2 |7 _: g( F; ?1 |configure: looking for Apache module support via DSO through APXS
8 a% J; l, }2 ]( {% ^. hconfigure: error: couldn't find APXS
: I2 s2 ?0 Z' n7 F5 qapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
0 v g4 M& f& `& {8 d4 d解决方法:8 b! T5 A* m6 F8 F3 G; }
4 s2 X( P2 M5 u. u# @: iyum install httpd-devel, H- G( m" j1 b( E( p
2.没有pcre
: \* K( A! h' t2 ]# `
- {% j! f l$ g @configure: *** pcre library not found.
6 Y3 n# t6 F) h' u/ ^' ?configure: error: pcre library is required: a- m, d: c( R6 _% C
解决方法:
- n9 [- E: [$ q/ ~, X5 l, p0 ]+ x2 N( \/ M5 ^! g* {
yum install pcre pcre-devel5 I& B Z* @( e( T: U
3.没有libxml2
8 R+ |3 Q. X- g) n" A" q2 [
- d, Q9 l, n! t- M; _4 c
; Y) m- T" S2 M; h) q0 @configure: *** xml library not found.
* i' I J; q: k) I% L. n" Tconfigure: error: libxml2 is required9 e& s5 w! K; G" P7 b. h
解决方法:
" B" f* K4 q u( ?$ F& `, l
5 Y% P" J& L' P1 T$ c& Eyum install libxml2 libxml2-devel
9 @2 w, d% M$ A5 |2 s. R4.执行 /opt/tengine/sbin/nginx -m 时有警告
* N; c o" m4 J' ^$ [+ u
9 l3 S; o6 O2 B+ X& P" e! lTengine version: Tengine/2.1.0 (nginx/1.6.2)! m2 Z% g% b9 ?/ |0 s# O% I
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
. y/ T% M1 U7 S, N0 {: A原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
9 m- L+ W8 u- l7 H. H' @+ E" }
1 A5 |3 U ^& V) C# _2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.+ N( d+ z' r3 y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9" c+ f; \# p+ H2 B4 l. U
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
$ ]* h! f9 L8 `% [6 i: q A% Q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
6 m8 I6 \+ K8 o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"; S# Q. u; s' ^% F# t
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.; z7 w8 U! ]" G7 F+ o3 H
解决方法,移除低版本的APR (1.3.9)/ L* I7 |2 N; n: U+ i
4 j/ {8 h* H! ?! tyum remove apr
! X& ]) K, u# y0 l3 P5.Error.log中有: Audit log: Failed to lock global mutex; m; b# `/ g- p1 A
6 i/ J0 \3 G# s d' v& K2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 7 a: U% z7 _* @! a. H# a
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
5 Q# a/ ]- K% A& k" o! B& K解决方法:
$ t) S9 Z, y0 u! ^编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:9 C$ z8 T# {7 M0 ]3 E1 y9 {
' e3 F! y% r% Q# s0 I9 \# e( J* D
SecAuditLogDirMode 0777, F4 s, `& f- Y
SecAuditLogFileMode 0550! A# {3 e6 n k! q" X: i. w
SecAuditLogStorageDir /var/log/modsecurity
2 |( n6 i5 T/ r9 Q n7 QSecAuditLogType Concurrent
+ t/ A/ [" `8 j! X参考文章:
, Z6 K" [, `# f" Y( X6 R2 _https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
- g" g( ^- D: d9 L2 a5 Zhttp://drops.wooyun.org/tips/2614 |
|