|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。* X4 |0 F5 N7 F m- D5 F
( Y! C f1 h2 ?5 W3 ]# G
一.准备工作/ a) E- ]; q; i6 c4 e
/ U8 n& X# C" U4 X; G) M" [, ~7 {' j系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0* n) ~" \$ n; j p2 F' \
. s R! ]1 B! [! o0 `2 f
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz1 o! e& @/ {3 B# O# c; `7 g! x
7 x/ v, w5 Z* ]- C) smodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ C U+ x4 K8 z8 b7 e
! X* _7 t: |9 U$ [OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
# W7 {" X$ R* m2 {2 J0 y+ |, s( N2 U5 j
依赖关系:9 ^& j0 L+ H5 C/ e1 B
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
% }' C$ o2 g) r" {. B
0 h+ }" V0 a7 Z1 u3 ] Ayum install zlib zlib-devel openssl openssl-devel pcre pcre-devel% a* x6 W4 z0 U* S4 D2 H
modsecurty依赖的包:pcre httpd-devel libxml2 apr$ i. V* r5 r, A; [- j7 v8 @6 x
( P; e9 W) {# W0 f
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel& m# b' d) C- l/ J0 z
二.启用standalone模块并编译0 L6 {. I8 ]/ O h4 Q8 Y! [ Q" {# |, }
, J* b* R9 r! R7 _4 }
下载modsecurity for nginx 解压,进入解压后目录执行:
v5 @5 a6 y( E: i; K2 | C5 K8 T. p0 Y% j7 G) R+ n
./autogen.sh) d8 |6 s; J' L
./configure --enable-standalone-module --disable-mlogc* o# H. D7 w1 g1 U6 ]1 f! O- b
make
) V/ P! C0 ]. w; I三.nginx添加modsecurity模块2 S* ^% N) P* b4 D" @. q7 d
7 w+ n$ ?) ]$ g5 N
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
/ b. i- o, ^# ~3 N1 U9 Q' h) C3 F, {8 g8 i2 W
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine$ G1 r: w: R. E! [- _
make && make install a8 V/ R+ C+ t- W2 n% Y
四.添加规则
# @ n% X. _ i# K. a4 K5 Y8 E* z# Z
( e' J$ X) x" k6 z- _' {! mmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。, N" O$ h+ G3 i& K' p5 f
7 u& W/ p' L( a8 b/ j1.下载OWASP规则:
/ @9 v! ^8 @0 T/ K5 Q. N
2 |% D, z+ ?" j0 c4 j& w/ a5 ^. e7 Pgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
5 v& [8 o) ^8 t8 w
* ?! R2 D6 x0 l( H+ [mv owasp-modsecurity-crs /opt/tengine/conf/3 A b( d: Y2 Y' W* ` s0 j* {; h
9 ^# r+ F; a) m* E% V) Q1 A5 K
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf5 s) U1 a8 N. H' s
2.启用OWASP规则:
! X X5 ?8 s# p8 o6 |$ c( I& u7 b
4 q9 @% K2 ?, r9 S; @# l复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& D; W; T+ x e, e* L: U- P$ b! f- [
$ L1 d3 H- G1 F7 Z$ l3 A7 i( M编辑modsecurity.conf 文件,将SecRuleEngine设置为 on% s. f, u; _& C5 [4 |
6 T6 P: t; U. ~8 J' Gowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。, S* V: D. R& i! N
# u% p: @% u3 H4 f4 u3 G
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
3 v- B) Y, Y+ ]" V. r# vInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf' T( ^5 A1 p! B; e3 I7 v: o
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
- T% a9 {+ @" L& `0 _" uInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf& h1 z$ ?8 W1 ]6 m, `2 e9 I* L
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
3 |3 p2 u% }) y& Z/ e7 D0 `, [) iInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf: |) j# U+ J: Q8 ^( G4 @( S
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
/ g$ \; E/ Z$ u. X五.配置nginx- p+ _1 b3 ~. b7 R% W% u
4 e/ L `; ~. b4 x. f
在需要启用modsecurity的主机的location下面加入下面两行即可:
9 p2 c; N# H$ G! M/ x- }0 U9 `6 l) w2 d* y; l5 P5 V
ModSecurityEnabled on;
A. ~4 w+ t( K% M' g3 I' xModSecurityConfig modsecurity.conf;
. D# e. s3 r/ _下面是两个示例配置,php虚拟主机:* K5 Y7 @# Z) u' F
: g6 V$ e* g/ b% g0 sserver {
# g$ |$ ^# W/ J0 i# R7 ^ listen 80;
& g( p/ M' _( A) [5 v server_name 52os.net www.52os.net; R* H% {) e/ T/ C, }8 E
% v! X# q1 J0 a. e% Q* | location ~ \.php$ {! |1 l4 i: w1 H4 _8 e. F" q( e2 J
ModSecurityEnabled on; 6 K5 T1 N$ X! ~
ModSecurityConfig modsecurity.conf;
: u* j! S4 `+ W5 T' j$ {9 }- R( ]& k/ Y8 p/ v |( S9 j5 ]6 O3 I
root /web/wordpress;
% @; t3 h3 H) u* J index index.php index.html index.htm;
) X) l: I/ N+ I
9 ?$ x1 B& ~: O' H& G4 S, {; r fastcgi_pass 127.0.0.1:9000;
6 F% r' X; W q& d5 M) @8 @ fastcgi_index index.php;
* y6 H2 r( S& x2 N1 ^) I+ t5 ] ? fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
6 _0 q8 ~& O8 W7 ~ include fastcgi_params;
9 b. x+ c9 u3 x }
# H8 O5 S+ X5 l }) z- l% ~0 M: B# [% ?8 Y
upstream负载均衡:1 t; {* L1 E- j7 g1 j$ i: T
$ g( A1 h2 c2 T, b9 bupstream 52os.net {
# ^8 @4 ]1 N- Y8 z1 e$ g/ ?6 T server 192.168.1.100:8080;
; J; E( S* K% u) _, p+ O& y6 v server 192.168.1.101:8080 backup;" m0 w& ~& R, h5 ~
}
4 w6 I9 X2 A9 N* w. V: m- K) g- I5 G- W# V9 P, I& l
server {! Y; \% i/ O! [. A1 M
listen 80;' o3 r3 w; B+ @& _/ T6 A
server_name 52os.net www.52os.net;# [' D6 [) P6 P+ R
$ Z" @- }; S. r+ ~! p2 l6 olocation / {
# _! E0 O9 q0 r6 }7 } ModSecurityEnabled on;
4 p; h$ R5 B( X( P- p9 z ModSecurityConfig modsecurity.conf; ! S/ [) n0 |& K: K6 V% h
: k+ {; i$ Z8 E( X3 j0 N1 |
proxy_pass http://online;& w+ `9 x# [& Z$ c0 H0 |
proxy_redirect off;2 u* U) ~+ T8 H5 v2 }
proxy_set_header Host $host;
- B7 A3 Y, K2 u- x' R proxy_set_header X-Real-IP $remote_addr; h+ E# Q% o# P1 Q- y7 o
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}$ }( j/ X! H: m# Y }1 I& x" Y1 s& s& k, a9 U3 X3 s/ W
}
( c9 R+ C8 p# `8 X. s六.测试6 F& r z" ~7 `, p8 S" \( c7 m
' Q9 ]7 `6 i9 M, w
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:) { F) A$ P& A q/ ^
3 v5 O; n4 p7 H1 D* e$ J<?php
) v1 t1 m4 G+ z* E& g6 Y phpinfo();
$ H* c3 s3 U( X: d! F2 R: A" \0 ??>
. }, _$ @9 | [4 o$ x在浏览器中访问: I( g: j$ L, C2 ], U( u; X
- F0 f5 h; ~. X# U* phttp://www.52os.net/phpinfo.php?id=1 正常显示。, k5 r( R, ~3 t3 ?9 U
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
5 R; g2 |5 A5 t0 h5 Chttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
: y$ R, Z3 u. t* g说明sql注入和xss已经被过滤了7 K6 t8 D! q$ J: ~( z+ ]8 Y
5 O& B. s; v# F: d5 i七、安装过程中排错( i) h9 e+ W. G" `5 `; Q* I
% H4 e" L6 e5 Q/ s8 r, _8 w; u: a! {; d1.缺少APXS会报错
! [# }4 S1 q7 G, G; ]; V
% f' y, t0 p7 ~4 K% r5 ?0 Dconfigure: looking for Apache module support via DSO through APXS' N) m% `4 G) a* Q" L4 ]$ g
configure: error: couldn't find APXS
) S- T+ N8 u" z. J. l- rapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
4 \1 ~! P5 D; x解决方法:2 b# @* G8 ^9 r6 `2 v6 u+ g, S! c
% I+ F- r1 ~0 v( c8 L
yum install httpd-devel
7 b5 D5 K$ `7 |: Q6 ~8 t2.没有pcre
3 R9 L0 l# N1 d4 b6 b. N
' u9 Y3 r( R( a) k; P/ |configure: *** pcre library not found.
! {* T2 b/ z( C! Oconfigure: error: pcre library is required
R4 ]" y0 e; A: J7 Q2 W1 T5 Y解决方法:: \' w% ?# D K3 e
7 C; c$ W+ G& j
yum install pcre pcre-devel
m" A8 N' W. T9 J. `3.没有libxml2
* x7 @* f. ?; z1 V& M6 S* ?; H% N# j3 S _: t, D9 N0 M) b( l
! \* \* J1 ?9 g' p1 m; L
configure: *** xml library not found.
4 g. a' H& y- Q! y5 U% E5 bconfigure: error: libxml2 is required
5 Y9 O; _% t. F9 B2 N解决方法:
" m' Q8 a3 G8 h5 }; `+ P. w! r6 A& h6 C) v- X" p- D6 T
yum install libxml2 libxml2-devel
( M$ I) b! ^5 r' e! _+ D4.执行 /opt/tengine/sbin/nginx -m 时有警告: V, f0 P/ ^& F$ K L4 R
5 I3 Z6 r0 o: ?+ g5 W% L
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
$ _9 @9 z& b- p: b' \nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
! x5 u- p5 d' ~原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log+ t: r% \- ?" Y
6 i& d% k1 d" R3 F/ |( q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.+ ?" v! J7 i- h" |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"1 m4 A$ r6 G0 j/ r
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
0 n- o* l( w2 o p* K0 L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
: o+ Q' B8 W& ?5 h" E! C8 l- ] Q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
, L9 e- V; W4 `* p2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
" F- g$ Y5 q% P解决方法,移除低版本的APR (1.3.9)6 I+ ~# A0 b+ P) A4 o- L( g
, @; d: n) a* xyum remove apr
9 a$ D9 R, R; g% f8 {8 B5.Error.log中有: Audit log: Failed to lock global mutex0 S& \/ X- b. c' B
) O4 \, \" b/ q9 G8 I2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
. F+ ?+ w5 |9 l! e2 ]+ M6 J! Gglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]9 a! k- h9 }) a0 \5 y' N
解决方法:, {7 n8 x' N. e% m" \4 V
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:# A" {5 y, M# T1 L5 C: [7 M' S
: ~. f5 c& v6 |4 B+ a6 PSecAuditLogDirMode 0777% a! z& S. q' A& g7 S" d
SecAuditLogFileMode 0550
; D! x& Y+ f5 c0 A& N0 iSecAuditLogStorageDir /var/log/modsecurity
/ I+ z1 a C! ^% O9 \' ^: ]( |SecAuditLogType Concurrent$ ~* v" y4 q! n$ o# k4 |
参考文章:
& | z% M$ e) \https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
4 c3 d7 Y6 A; q+ w+ g2 F9 O6 T! qhttp://drops.wooyun.org/tips/2614 |
|