|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
) {$ l( G- w8 [1 T5 w
0 q5 w. r7 Q, C1 P一.准备工作- b% H& L8 ^/ C% D: g \8 h% ~4 ]5 E
% n6 o* A8 R q( c系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
$ m6 \3 R8 ^; q3 T
% t, Z0 G( W d7 e+ w7 F2 Btengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz; p. Q3 Q/ X' z0 I1 G& k" G# z
( r$ `( M/ g* ~: h0 {3 h% Kmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
" ~6 H- L) W+ J* _' m2 m! X8 R* t X( E$ ^
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
+ a* I- _. u$ y2 l9 [0 U' [, R: k7 k4 \' {; e1 D
依赖关系:
9 M8 u% E( }. A# }( H' ]tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
4 @* |+ C9 o& `5 t' { ~2 M3 V, C9 N' g4 a- ]. R
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
+ N2 @9 N: T5 k6 k4 N: Z% Smodsecurty依赖的包:pcre httpd-devel libxml2 apr0 H6 {4 w) F4 T" Z
8 b0 Z9 B! E4 w. t2 _3 Q) pyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
& O" S% K' F( D- H% i/ u$ x二.启用standalone模块并编译6 A# h- ^+ ^1 t
, h) B5 u# F9 C* K) N
下载modsecurity for nginx 解压,进入解压后目录执行:
. ?8 U7 ?* z4 H* c
5 Z, R: l4 E4 x./autogen.sh/ O: m0 v9 }# R- @
./configure --enable-standalone-module --disable-mlogc
' R! l4 ^1 v- {make $ n0 z0 z7 C9 k- M
三.nginx添加modsecurity模块
9 s& }7 L" Z( M% d( S
) f; U% k* U; |: S0 H( U在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
, U* D" j% n- t- ]8 c. d: O1 i0 i7 _; g6 l5 }' v
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine6 d8 @) v2 A+ O
make && make install
; @* n8 I" s3 {2 N0 Z2 f5 i0 P8 k四.添加规则
: Q( e# P$ k' A4 K7 n; n& E
2 V/ ^3 |1 W1 Cmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
; s. R0 v! z# B2 N& U6 X" h8 N# W- Z7 L P+ o
1.下载OWASP规则:& {9 O7 `" w; w+ ?8 j6 q7 u
3 b0 ?! z, E: O6 Z- x* J5 P& K
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs8 M5 m' G% B9 D# q J5 l
6 h. y. h# e5 S7 P* T; s D
mv owasp-modsecurity-crs /opt/tengine/conf/% l+ Z0 f1 J1 g9 {: y
- U5 U6 y# Y4 y6 gcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf! U& N9 g- M" ^ {) F0 ^
2.启用OWASP规则:9 c7 u6 h/ a$ l$ R! c" v6 _% x
) D' J# e; U5 `9 W* @
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。3 R; }' ]5 L- b$ q$ n3 b5 V
9 R0 D& D& W! M7 @编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
3 ]5 v, O7 g/ H$ ` j, o3 R1 v: F/ n! o+ j
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。2 E" a) x/ s- |8 ?
0 A5 `; x( d# ^7 q, G( U& R
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
' l1 c; R3 V- y7 S# _Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf+ r. x" T7 Z* h# ~, o/ W' P
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
3 j; |+ N& D3 z1 |Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf0 P9 X+ @' d: v, e' y# n
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
' x7 b. F0 t. ^' {. x/ R0 B$ S7 OInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
. F; [" ?5 G. t) {" JInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
" X$ j* [: ^$ h# o4 w* A五.配置nginx& Q# _& Z" _" g% F+ {
/ K2 k0 Q9 G6 y7 d, S2 S# ]
在需要启用modsecurity的主机的location下面加入下面两行即可:+ _* y/ X. w' k/ ?
- _; @* v2 v7 c* r3 w# eModSecurityEnabled on;
7 G& [) i+ b; w: U, K4 UModSecurityConfig modsecurity.conf;! D, W* N* S+ ^+ b- |# b
下面是两个示例配置,php虚拟主机:
W! [0 h- Q" v: n9 ]4 S- y
" w! W* q9 r u/ r2 H* S/ Gserver {8 k* P/ M- _: v
listen 80;
! X) g- N. H* c/ H. C server_name 52os.net www.52os.net;3 U$ H8 a/ L. b0 F' f8 P# ~ p6 ~
+ M, W: w4 z% m2 l( ?$ _! J location ~ \.php$ {, }# y& o& [0 x, H( `
ModSecurityEnabled on;
4 e i# @; T8 N: b7 h ModSecurityConfig modsecurity.conf;; C: X6 u- r# s0 e- i! d
: G9 n( G3 W7 i: g# C
root /web/wordpress;
, J3 e' e% |, h: x o. j; ^6 f6 z index index.php index.html index.htm;8 y* l* {% L0 U2 T: ]4 @
* `* g; Q- N* { }* @5 s B+ K: a
fastcgi_pass 127.0.0.1:9000;# _* J) v+ A4 n+ i5 U3 _
fastcgi_index index.php;" o4 l$ X) B: A# {3 e
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
a2 ]5 \6 w( E3 N include fastcgi_params;" F5 p7 U: I1 Q/ g4 m& l3 f
}
- ?% f! U7 E1 C( i8 ^ }
- O, v6 S' T3 w) bupstream负载均衡:
+ n: E- j, S# [3 m" ` S6 o0 J$ B+ z8 }4 s
upstream 52os.net {8 Y! f4 {9 g3 l
server 192.168.1.100:8080;0 B1 L, Q" C1 G8 |) P# a
server 192.168.1.101:8080 backup;% a% \! Q _; z B+ i! y. X p
}
4 O/ E- @7 b" m2 x/ k8 c- Z2 O: ~
9 P. L' H: g. W% [1 t: C; c4 Qserver {
- b! B# A3 H: _% p elisten 80;- _: k5 u& K3 o+ w9 F
server_name 52os.net www.52os.net;, @) j3 g! j' g/ G9 }+ W% X
( o \; B n6 i( m& k9 N* y
location / {
* O, ]5 V ]* L ModSecurityEnabled on; 3 }- I7 _1 p; f
ModSecurityConfig modsecurity.conf;
8 ^+ d8 J2 W q. z' z4 g( Y/ L
/ F$ ]% ~ G2 z6 _- [ proxy_pass http://online;
9 ^( V1 ?6 U! _. r& \4 H proxy_redirect off;
5 b0 M4 A6 U! h O5 g proxy_set_header Host $host;
5 K8 _" N! T- B9 e9 [, D8 K6 o1 e proxy_set_header X-Real-IP $remote_addr;: m, q' i6 U* E$ D4 _
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
1 t. U0 x) A/ C9 K) b }
6 t/ i% t# p6 h# V+ P, ^}( P& B' Y5 K5 A8 b Z, p( Q$ R
六.测试
' S7 i- h* `9 B! `3 l( e$ { R4 `6 S% Q3 f! U4 F# p8 r: U5 ]
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
+ v6 K3 _" l" s, J& d, q
# V8 ?! B2 j$ D- Z; s7 b<?php
) R& _0 L" Z& K phpinfo();
' P0 U. `6 D# U- h/ \) f?>' {2 q0 z+ U! d
在浏览器中访问:% M; M( \0 @; Y. w/ g y O1 j
* v/ @" ]# O' M& `# _; shttp://www.52os.net/phpinfo.php?id=1 正常显示。1 r% l$ o, H& h
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
N0 j% d# L- d! t: whttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
( ~0 Y/ N7 F, g) L* F# G说明sql注入和xss已经被过滤了" g/ {) L; v3 l4 I1 O& a# u1 N ?
. g7 `7 T9 Q- ]) q8 y8 W% D七、安装过程中排错
' k: M$ P# t$ a3 ]7 ?* X
- R4 c; I0 a6 z; W1.缺少APXS会报错0 C) j8 E3 e1 A/ ^ U6 `
% ~% M5 _0 l. \# \$ C3 o0 lconfigure: looking for Apache module support via DSO through APXS
: {1 r+ @5 [' }1 a6 k/ _configure: error: couldn't find APXS* d* p; _% Q4 d, o3 ~" m1 [
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。- v2 i" S' @6 V7 j! F
解决方法:; o* v8 L+ k; B
6 F$ ^8 \* F" k9 Wyum install httpd-devel7 |" l3 Y* I5 d) u
2.没有pcre
: a' q# ^2 s5 F6 R
+ o/ m' n" c$ w5 {configure: *** pcre library not found.7 ~& l- C- Q- _8 r) ]' F/ T# A
configure: error: pcre library is required
# a: n0 l4 ]2 w, e4 G0 \% v- S2 Z解决方法:! ~; l& L* f0 h; O3 n* M% f2 Z
' q) X$ V. P$ M7 Dyum install pcre pcre-devel. ]6 `( S: g5 T) {
3.没有libxml2# e" y) W- n @# @: O, ~
5 V5 q1 |6 }9 _( i) M9 Y' U( M! N6 `; o: L
configure: *** xml library not found.3 }7 z4 ?/ n% e7 M* H
configure: error: libxml2 is required/ o$ O, E6 P; @5 J3 n
解决方法:
( u5 V' M! P7 J& z, t+ I# B6 c& Y# e. \& {- g, @' g' q
yum install libxml2 libxml2-devel
+ a7 T# X& }# `( i! d) Y4.执行 /opt/tengine/sbin/nginx -m 时有警告- @" D3 J7 t% L9 J* n7 h2 z( l
6 f& E! [* O1 Z; }2 eTengine version: Tengine/2.1.0 (nginx/1.6.2)
( K) N# `& O# Y5 h9 Ynginx: [warn] ModSecurity: Loaded APR do not match with compiled!
9 \8 }9 t, A0 S" W! E4 J原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
/ }6 ]) X% N1 i2 }) E! K( y* i7 \% ^! Y4 k- E; l% [' R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
: S* \1 O% z- D) w; F2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"9 M z- N. M5 ^, \0 y% E: K7 M3 R
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
9 v, i6 G A1 m9 {! E2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
: Y7 p+ ^+ Y7 F3 M+ T2 y8 k2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"% m% ?4 w; u5 k$ s
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.# t* @3 R1 O: c2 V
解决方法,移除低版本的APR (1.3.9)
9 x N4 D' ^# L( ~2 a+ l: s, D' I# y
yum remove apr
! o" l) F- K8 y2 t/ a. s: U* I$ H5.Error.log中有: Audit log: Failed to lock global mutex; {: j |9 \; W) f% O0 |
) o% s/ H6 c' \1 V
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ! r3 d% _" r3 G' k6 E2 B
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
3 E: ] U/ o! ?# Y解决方法:% h% C9 b5 g* V# o( C8 j4 i. b
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
) i: f& y8 ]- ^$ G7 E4 ?# m$ C
7 ?7 L5 C8 ?7 q$ K) c+ l& ]# o. USecAuditLogDirMode 0777* o* V' A. k9 L2 d c, N
SecAuditLogFileMode 0550/ D7 u* q# n) Z! ]
SecAuditLogStorageDir /var/log/modsecurity' L/ c8 e X3 V# {$ k
SecAuditLogType Concurrent
; K' z) N& f5 ^# b0 p参考文章:, c0 `, U! @8 P7 x- [1 w
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX5 e; Y9 X* P* Z
http://drops.wooyun.org/tips/2614 |
|