|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
& O+ H( b! R n% t+ b$ x7 P, d2 K; m9 K" k2 z1 g3 y( \
一.准备工作
8 U" K0 t/ V2 X; H3 {7 t' E9 E6 U4 [, d2 q
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.06 g) v' o4 w& S9 d0 d% b
* Y/ D% f& y# y$ @- q/ o
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz! |' ~+ F% H5 I8 Q
- ^; p1 i1 x) M% X8 i
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
* N* C* M7 U4 n$ U
3 f t# v7 c( a9 D- S: v0 {- oOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
F4 h* |, T/ l; E E% p( I: i+ d, X x% D" I5 p& r
依赖关系:
* M1 Y; W4 V. f3 Ttengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:, v+ Q1 f- q3 g1 S4 y8 p! w% D
+ }( N" L9 n( u, yyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
. R3 o c4 s' @7 z5 G P" r" Xmodsecurty依赖的包:pcre httpd-devel libxml2 apr* j& @) d/ V8 N7 _* w9 k6 I
: U+ C% G+ A' L* Z- l# Xyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
# ^' q1 V+ g9 o" f7 V. ^二.启用standalone模块并编译, r& { X6 j9 k& I: v! {+ }
7 n+ a) p' M0 K! x/ x下载modsecurity for nginx 解压,进入解压后目录执行:% K2 Q/ c) K, G
" P/ \/ D' b1 q: L9 t./autogen.sh( z8 b, C( u& q5 `
./configure --enable-standalone-module --disable-mlogc' x3 r/ G" \7 d0 T! M |
make
M+ }% _/ r* s7 P/ d% D三.nginx添加modsecurity模块% h3 k- g6 t, d2 i4 t
/ h% m- c- D+ r* p- |" E
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
4 ~6 |; Z# H" u$ e% P. }2 X3 Y1 U% w0 w
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
& b7 v" c+ a2 ?$ `make && make install' b v( [1 C: z1 ?6 A4 A
四.添加规则
6 |9 l; h( p9 }6 y; r4 ^; M P4 y& L$ X; T# B
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
; |3 a& k: k+ M" j
! n; H" M6 V. u$ L: z0 o1.下载OWASP规则:' s! r; x* Q- r
A1 C: {# w2 Q( n6 x" p+ b
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs: l$ y! V+ E. u+ R; S7 y$ p
. g# H# ?* H$ S! f9 r' R2 i
mv owasp-modsecurity-crs /opt/tengine/conf/
' a% D9 \" M1 o% h: D
% ^" Z, C6 O# j; P7 Zcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
# e$ C5 g* F h; O+ m5 ^1 x2.启用OWASP规则:
7 `% A! Q( F, l7 \
8 w4 e5 O8 y) L' i复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
3 P1 P- Q9 E0 _5 k: L5 f+ a
* o3 o$ l0 x1 }8 J+ a2 |( i2 n编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
0 H$ n, H8 F* l1 g" K$ J+ a" x
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。3 h2 h: Z- g A
. L, w6 c; H1 F, [0 {" i/ ?4 OInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf# G1 |' H- {. [1 w& Q% Z& w3 c/ U
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
4 U* U/ I2 \, J$ [7 fInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf) F: M% H( ?0 V% t6 x8 d3 }! q
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
: {6 G B2 m$ ]* H; JInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
2 Y) e4 \ l& y; H/ LInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf9 ]& L8 {6 ^7 _8 J; V1 c8 ]6 K# J
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf( {! b2 M. g' I, Z; e
五.配置nginx
& d# I7 }% V" {- e% H
& q" G/ ]' H3 J. J在需要启用modsecurity的主机的location下面加入下面两行即可:: W1 s4 K% G8 G
+ P* L( a2 R4 T
ModSecurityEnabled on;
) o1 `- ?3 D9 t# i6 RModSecurityConfig modsecurity.conf;* o1 D# _4 {8 O- F9 c
下面是两个示例配置,php虚拟主机:
. _' F) x& f6 ~3 s/ R
$ `% _* Y7 w; c; _server {
4 {1 A' o% k, o7 q listen 80;
( }3 T4 n7 I5 @5 o3 Q, z- ` server_name 52os.net www.52os.net;
5 S# ~# f) I! W* i" c
7 F- M. J, i4 @2 B! S location ~ \.php$ {
0 M8 Z3 \( y6 Q" Y' v ModSecurityEnabled on;
2 @; M5 c0 u. v9 t F! w" ]( w z ModSecurityConfig modsecurity.conf;. j x0 |4 [) A9 a
: y; J7 v2 {& P$ l% B root /web/wordpress;
" f* S8 e5 g. z" @8 G index index.php index.html index.htm;
9 K% D# n w1 O- R+ N4 K + N( [, B2 Y3 x0 m3 G0 v7 w
fastcgi_pass 127.0.0.1:9000;4 J; R6 n) j8 K6 e C0 _
fastcgi_index index.php;
- Z; d# B9 f' \' J( a" @ fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;" t9 [4 L3 w. `, b+ n
include fastcgi_params;
* Z, {% ?' @9 D& ?5 L; x1 y$ Y! T) l }
6 d) M# r* d d }
5 _& l1 |7 y$ g$ X; R6 d* lupstream负载均衡:
# R0 D8 V( J+ r0 {/ Q/ w
9 u* d/ E& B, r% X' jupstream 52os.net {
8 V/ C; [* d$ i+ L* C$ k: h server 192.168.1.100:8080;4 P" c4 g9 B2 q9 Z4 M
server 192.168.1.101:8080 backup;% |' N+ r5 D! }4 L8 A+ K6 g
}
* {. g1 R- _6 G0 v# v7 f. i
- w/ ^% w% O$ ^" [' ?) b2 e6 Nserver {
1 \* s, T' a0 J7 ?; x* Qlisten 80;
+ e X T2 }- ]) ?- P; Iserver_name 52os.net www.52os.net;# b' @, R! P1 x. U$ o
4 S; G) w) i: i$ ?location / {) b- T3 ^ V3 O$ N
ModSecurityEnabled on;
: p, g4 s$ ~/ V' \ ModSecurityConfig modsecurity.conf;
/ H. m0 x A* R& ^) A
* |8 t+ @+ u! \# }" w/ R2 P/ s proxy_pass http://online;
1 E# j/ I' I$ M4 u. b' F( }9 z proxy_redirect off;
( F5 b5 ]( }& m0 M' s proxy_set_header Host $host;
# u# p6 }& x; K% i5 V- U proxy_set_header X-Real-IP $remote_addr;
4 j" d5 f5 e- G4 h) b proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
; h2 o, q& o v/ {9 c, S n }
9 R9 }, i4 P) M2 e4 ^}
0 T' x M& E3 L" @. A! D+ }2 t六.测试7 d8 P7 b9 e' V u. a% X$ A
4 y; ^* Y$ C' U H1 w& o8 h
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
& o+ Z. e; @) J" S( j
4 s3 X' u& u; k1 n<?php3 e; \9 k* v. v Z! o( V3 U* J
phpinfo(); 2 b5 C# V | g* p2 l/ H6 E
?>1 G; ~4 `0 l$ d5 L% r
在浏览器中访问:" w4 c5 R6 |- P, q
# c1 x* a% u9 l( C7 V
http://www.52os.net/phpinfo.php?id=1 正常显示。
8 }# S4 a- U/ ^ [9 ]3 L; P5 Shttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。1 _" N- L1 w' z K$ z: u+ r
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。* b5 r, r) J- m' V% K
说明sql注入和xss已经被过滤了
5 t6 s- O, @: R
# b. G ?$ } n- y七、安装过程中排错
4 ~$ t) p9 p: v$ F( Q0 a
0 M5 S: v3 R, W0 u1 Z3 S# e( }1.缺少APXS会报错7 M. `: d$ ^2 w% a. c' }* H) Q [
* i7 l/ s% n) Z2 c' A, Rconfigure: looking for Apache module support via DSO through APXS
* f% Y7 f2 o8 Pconfigure: error: couldn't find APXS
. J l" _; S0 }7 Z# N3 E% a" fapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。+ D D8 L9 E" L4 M) W5 W
解决方法:: S. W% q/ O+ C
- X& M" k- e0 H7 a
yum install httpd-devel2 R" ^' |, Q4 H2 x- R0 }
2.没有pcre
/ V- Q1 {, }! M4 P) [& F' ~/ N; i4 O, |5 K* p0 h
configure: *** pcre library not found.2 i- r( I" q: R p l
configure: error: pcre library is required
* L) e% V4 W+ t$ h" z- Z解决方法:
" Z9 L5 K3 k$ u4 A. ^/ t4 x3 G+ z& }/ c% L; f8 H1 n
yum install pcre pcre-devel, N1 w( K% L3 R
3.没有libxml2
$ F4 u# w" ?! X9 C. j8 G5 v& n
: o0 I1 ~4 c7 Y' ^# w; g& p* m3 T: l/ z" M# V
configure: *** xml library not found.: _& f" \& j# u$ O/ f; n
configure: error: libxml2 is required
+ o9 ?1 M n+ R; Q解决方法:) l( W, {0 ^' m+ y
2 ~8 E$ S& G! J/ v* g ]yum install libxml2 libxml2-devel+ M6 T* S; i- ~; ?/ F3 E
4.执行 /opt/tengine/sbin/nginx -m 时有警告
V$ _9 q# w. [: h
' Y. t5 k* i. S9 gTengine version: Tengine/2.1.0 (nginx/1.6.2)8 X" a$ k/ H+ E9 O/ {
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!9 C; r4 Y6 |6 J4 g
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
, B3 {/ ]+ J; |' l1 K# w! L: d! h8 L0 C) t$ n& s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.* {' G1 k% s2 E3 ?4 w" g2 K# m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
: Q# E4 o: \' v- D. ~2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
5 K X. w9 {% i) v% ~9 E- j4 B0 w2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
2 x7 A/ \% ?' c# g! Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
0 Q7 {9 T) O' o# ?2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
, Y0 K, S' v/ T. E2 {: w解决方法,移除低版本的APR (1.3.9)
% i+ U; d+ W7 C h1 Z1 N; [1 T; W% }6 w( @4 ]
yum remove apr
4 o& n& F8 g! u: G3 j5.Error.log中有: Audit log: Failed to lock global mutex9 z' n b4 d( \$ {; D5 H# ?
. S) d" ^% B$ J k( A- g
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
7 p9 a& i! }' C5 o8 E$ n$ Jglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
9 a) _! C7 ^; W/ g& j* j解决方法:
6 ?2 f7 h' l [5 L1 P/ o' H L编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
6 d1 L% ~- D2 b+ D+ e6 m8 L& j6 X! g5 e% `/ w/ @' u4 k3 s' @ I
SecAuditLogDirMode 0777
1 l0 E6 s5 l' X" r ]SecAuditLogFileMode 0550! o6 O/ i5 ?: l2 u q& e
SecAuditLogStorageDir /var/log/modsecurity
: F3 F( Q* \8 U6 u' m/ P6 B0 O" RSecAuditLogType Concurrent
* j0 `, | Q2 X参考文章:& K1 H y- k8 ?/ Q0 N
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
- u( F7 V% W4 o7 G8 o( S. Whttp://drops.wooyun.org/tips/2614 |
|