|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
6 x6 O* X" k( \/ @ Z: r6 C. ~6 G) r
一.准备工作
! W3 h' |+ c$ n% G9 G+ z1 V( x6 f9 ?' H. D7 g6 E
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0, d+ r, q W: \0 K$ I5 A
$ R9 s9 f5 f& {* g1 ?. t! E! C% q4 `
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz4 C) S* b, E: Q5 y8 i3 Y
0 @% D7 M& j% A2 C- m
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz/ s/ q/ q: G" K
+ _' E1 T" j4 y4 `OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
& i) z8 r5 `- \ ?' Q+ Q [. k1 s/ V# _) G" Q; `" ?
依赖关系:
; H3 Z' }5 N# i, o1 l* ~tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
9 O: e5 [ F6 X z5 N* \- p# k( M* y7 z5 s: g8 m Y
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
9 z% v- w& g: k9 g0 J1 q! nmodsecurty依赖的包:pcre httpd-devel libxml2 apr
5 }8 o. _# L8 r+ C6 c# B/ [4 ^# ^' x% `7 i. K
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
8 G$ o; r+ q; O2 I L, c2 t4 c6 {二.启用standalone模块并编译7 ~$ d# E! l: c$ B
. i& j3 V7 ], q, B0 }/ W5 v0 _- j
下载modsecurity for nginx 解压,进入解压后目录执行:
' ~! j. D) [' b9 R; y& c9 V
$ H" t5 o5 c( o H8 K. N./autogen.sh
4 c* F/ F7 k+ U3 o0 X./configure --enable-standalone-module --disable-mlogc
8 e0 w4 V) W' E& X6 h/ Jmake
) ^6 N" ~& c) a- d9 W+ @ ?; b三.nginx添加modsecurity模块
1 U6 a1 x4 ?. n" H5 Z; U8 a
) z/ V. j1 p4 N, J7 J在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
+ [7 b. z- ~; ^/ h9 m% C" t& i8 C% J4 C, i x. J" G. Y
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
Q! A- x# a8 fmake && make install
1 e. N" O5 U/ a$ I+ q5 z* T四.添加规则) y9 Y1 S. b/ l4 K* K; g6 J
( n; O1 \( v8 T
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
* A J3 ~, g6 d4 e4 l/ b
' Y1 e1 \- }: T1.下载OWASP规则:/ z/ W5 u6 c9 f" B
: C) Y1 A U3 _( ?git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
5 d8 k, L5 h* O0 m
' |! I L9 z) Tmv owasp-modsecurity-crs /opt/tengine/conf/! Q+ w! K9 J9 [ m- H# X' k
% K; S& Z( R5 t0 }5 M7 o* j
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
% O; D3 c' \. g9 \8 H2.启用OWASP规则:
; ~) L! z/ @, d" M% S0 B! w' ]" H. k
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
8 v5 _7 o+ O0 T' |3 @! m* u4 |" j2 n6 l
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on% k7 N: |. G2 D! D: j$ G
1 H( L: r9 H0 Q8 @# [1 W9 i R, P8 Eowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。( M* m+ }9 e8 h! J Z
1 G; _) M. Q, N, Y
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
9 }; m( w' l4 |! V' T$ WInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
5 A, f) k- y& }7 L9 QInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
* s+ d$ @, x0 y8 {/ t9 u5 m% PInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
0 G5 n0 L& e4 _. B$ [% b& c$ [! iInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf7 L/ W1 f0 ~, e! }# P
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf3 W* y2 j1 N: N1 _( r( q
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
1 q: @& r6 X. q! w& z五.配置nginx
! E e' h4 y' _- {. b ]; ^0 {+ j9 f D- O: r
在需要启用modsecurity的主机的location下面加入下面两行即可:6 h5 N4 f0 V- ?1 e1 F2 F- F- E
5 q+ h5 w9 L Q8 |! n0 S$ G+ N; _: m* q
ModSecurityEnabled on; # {# z& y+ L, B2 d. z
ModSecurityConfig modsecurity.conf;
) f; n R$ a7 s, g下面是两个示例配置,php虚拟主机:& U5 g7 ^) v. ~1 d0 L; h
) g* H% I) R- ?0 O4 T0 H4 Zserver {
* f# t' ^* u w6 F+ \6 C9 E6 u listen 80;
& B5 Q: k4 V, n$ G server_name 52os.net www.52os.net;; j/ r% r' o9 |
' B2 b; t$ K, ~/ J
location ~ \.php$ {
$ j' m4 E1 [; D) ~2 M- |7 d ModSecurityEnabled on; # c6 a( y" \3 g7 B& G G3 Q# k0 x) p% `. b
ModSecurityConfig modsecurity.conf;0 V- F8 _8 e3 l! P& D! }
' E; x ^1 G! f3 e# G# M6 T root /web/wordpress;
! d- l3 ~% u3 r# ^ index index.php index.html index.htm;
8 @ E4 f. i" N$ c- G* u! M
7 t8 h( T2 o: F6 l5 P- Y$ B fastcgi_pass 127.0.0.1:9000;
/ Q$ c% i) W: p. T fastcgi_index index.php;
" {6 Z9 @6 O( Y: B, o, l" H fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
2 L3 n. \8 W( ]! f include fastcgi_params;% u& s) o2 A: Z0 ^0 m
}
/ }4 D; ^7 w3 O* K; \& d4 W }
2 P/ f0 F3 r9 F. }" X5 supstream负载均衡:
1 Q) I5 _& \5 @9 T$ a* v9 K3 y3 l# m3 C. j& \! D7 O, s# x
upstream 52os.net {% t/ |6 x% E! m4 E) \; n
server 192.168.1.100:8080;; v0 W/ U1 o% ^. h3 T6 Q- ^
server 192.168.1.101:8080 backup;
4 W2 D" H4 O3 S3 @/ M}4 k; z) q/ r/ ], ~* o
7 M4 ?( [4 c- `7 x
server {$ O5 C4 _, n: r7 n- f
listen 80;; ]. i: n* ?2 ^" W9 F
server_name 52os.net www.52os.net;
+ z, _- d! u8 ?% V4 G& v9 G7 v8 K% X9 y
location / {- ~4 W1 A8 |, b8 `% b
ModSecurityEnabled on;
/ W* {0 `! u, s* ?- k# V7 ] ModSecurityConfig modsecurity.conf;
5 R9 Z3 p5 P9 m, U- a# v. q7 ~- r4 a: \4 l N# s
proxy_pass http://online;' W, w! F1 ]2 {' C7 j
proxy_redirect off;- L# o; T' U" a. X9 S* y
proxy_set_header Host $host;
4 @" M$ J, }: n% C [* Z proxy_set_header X-Real-IP $remote_addr;
9 y# D- A& q. }" Z2 H% z( A) J9 h$ s/ w9 l proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;, T* v3 v5 m5 I1 ~6 n. J
}: y3 Y! P( F, X$ H ~ x
}
4 v1 a- {2 u% O六.测试
2 N& V3 e1 Z& u: W4 w* H! E
& X6 F' K, i6 k) D- Y) N9 N我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:( F2 V5 L% |3 K
$ u0 Y% [8 S) @: ]<?php9 A5 C/ d$ \. _; _( [5 T) m
phpinfo();
( }) G( ]! Y. I6 p4 X?>
( t# V& t; G: ^9 }$ s在浏览器中访问:8 s' z+ B; M* m
; _0 l: \( j! j
http://www.52os.net/phpinfo.php?id=1 正常显示。
% `$ u5 ]! n9 P% {6 [$ Jhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。5 R4 [1 |3 N. U- X
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。* H* Y* ~& \) }! e
说明sql注入和xss已经被过滤了2 u9 w. I' \( x3 U. _
: G6 E4 K% a9 S- }7 u4 y七、安装过程中排错9 B9 d- l+ s# R6 V, ^2 T
) a/ V4 Y' a# m
1.缺少APXS会报错# |% ?! c# d% r0 L8 L* W7 d
3 d, g4 V0 m' {configure: looking for Apache module support via DSO through APXS
C; q6 t2 g4 Z C/ m( E6 w! u7 Q. Lconfigure: error: couldn't find APXS
: L7 e6 p7 I2 }2 Capxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
4 x; Z3 H/ y0 x# r- G解决方法:% s' y, z( d0 S4 A6 M/ x
; W7 H9 b8 u$ @6 x0 Xyum install httpd-devel
5 q: G; _/ v) e" r2.没有pcre
# a- }( Y1 H) M% n( J& b' }" z$ v3 X3 v, L8 c2 A5 _* b4 L" M1 {
configure: *** pcre library not found.
" m1 C) L9 u' w" L6 C; O3 Dconfigure: error: pcre library is required
}" S. C& J& l解决方法:
* u* T, v! t. r
" i/ q% G$ e* u& ~& R8 Z4 Gyum install pcre pcre-devel8 _% {$ r, O0 t# G |2 L
3.没有libxml2
/ G8 I$ ~4 M) K; v" @- O0 m% `: @/ y' E3 W5 q/ o8 u ]9 c, ]3 Z
6 T+ L! M8 y3 T c) I! h
configure: *** xml library not found.- x6 M, Z q5 H
configure: error: libxml2 is required
4 \* G& L2 d: M, p" _解决方法:4 x' f" j1 ^5 B1 p6 [
/ F+ t: [" z. S
yum install libxml2 libxml2-devel
" Q$ K k4 v6 P5 v' Z* T4.执行 /opt/tengine/sbin/nginx -m 时有警告( @; O2 C" H; _7 f1 e' U8 [2 l" q; @
& Z! M& X. A7 e7 \1 c; `
Tengine version: Tengine/2.1.0 (nginx/1.6.2); u* [- u; A/ a
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!7 Z0 B& ?9 ^6 j0 E
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
( K* c: r. L4 E% ]1 o% a4 j" R# ^+ v: `' K# ? t" [
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
2 L0 n+ X3 d# o% w" z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"6 O* S6 p4 B% O; F& y+ Z
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!# u4 |4 n0 n, l/ u3 V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
/ a+ t8 ?0 |. @2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
* S4 T- P( ]8 Q( \2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.+ ^( |! |7 E U2 ~
解决方法,移除低版本的APR (1.3.9), F1 G+ ]1 {" F$ a! z" ^
$ Y+ E' h. w9 m7 M8 ]. v
yum remove apr$ ~ c+ ]! j* T3 j0 ~# T
5.Error.log中有: Audit log: Failed to lock global mutex6 x# B: y( P7 m* Z- w' J. M
+ M% k$ T5 _' ^) M5 u
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
% l+ z# B6 {* i9 h0 Y* O3 G% Vglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]! H8 l8 T5 r* i
解决方法:
9 b8 I: B! p c+ u! N2 o Q7 V* J编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
& n" U3 J1 e: W; }
' X; U9 g$ e- jSecAuditLogDirMode 07770 x u; L* `) k3 ]0 N) a) L
SecAuditLogFileMode 05508 ^, I3 d; T5 _! S2 B! B& }
SecAuditLogStorageDir /var/log/modsecurity2 r$ e8 L; g1 I1 \, J- u. r4 n
SecAuditLogType Concurrent8 u2 L: u/ [5 v
参考文章:
8 Z0 Y/ C* q0 M7 ^https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
: J, B. \* x/ N) whttp://drops.wooyun.org/tips/2614 |
|