|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
W; W7 e N& r& T! V) c
6 i ?2 E( j! n/ }! S1 d \1 l一.准备工作/ ?/ W# T. J+ S) @8 N: r1 v% x" [! v
" o3 E/ Y0 H) `& p) \系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
5 l' {2 G8 _. W) K" S3 [6 H ?' T+ u: x4 Z' D
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
- j5 [3 s7 u" O$ J+ q
+ U, [$ W2 v. R$ @- ~! `/ _$ C/ u _' cmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
9 o9 j% a1 Q) Y4 ]& l$ O" |
9 u; n, k; ]/ k* fOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs! c4 } E, ~3 b: {& ?
! S9 X' q% r& X- B6 D& f, R$ t依赖关系:: u/ v4 i( j4 J! j! S) ~2 D* `9 _
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
- W8 w; G. m+ \
; V' s$ I( p* a7 t. e# o6 P( Zyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel" g6 ~ n" g5 K4 G8 O
modsecurty依赖的包:pcre httpd-devel libxml2 apr
! E, d1 X2 O. B# I% k9 o2 F" F# a$ F6 d" c# R, t5 h: M1 E
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel8 q1 o, ]' ?6 F6 w% V5 p/ {
二.启用standalone模块并编译' E- V0 Q6 ]) {0 z
+ Q7 D8 O6 H) [* I下载modsecurity for nginx 解压,进入解压后目录执行:
+ f5 }8 x$ _" N6 i5 H: Z9 I6 i+ j( V# y3 o- F' M1 g
./autogen.sh
. s$ G1 D- w' ?. H8 b./configure --enable-standalone-module --disable-mlogc" [* E& X1 f. I( [: |
make
+ P; c4 k2 g4 C! z三.nginx添加modsecurity模块& O, X, Q! ~2 N. F7 H3 T! s
" a5 k3 C& g" u0 I% t8 h
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:+ c+ f! A6 u: B. Y5 n. B; v3 Z
4 ^6 Q. I- `9 X( F6 r% e9 b
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine9 X9 \ x5 ]/ e
make && make install8 l- n$ J* r& z, t( k5 x+ X4 V
四.添加规则
, u5 O( U. T% K8 p0 L4 Q- s9 B$ v3 K
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。" m4 b* J1 R( Z9 d+ ^; Z
2 R* h3 v/ C$ R# J: A1 S; L
1.下载OWASP规则:% K. ~7 j- W! M+ f
. y' t, W" }4 ^4 @$ ~6 dgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
" p* ~6 N& B2 n) o$ B% q. X+ O8 S3 E6 F2 a! t
mv owasp-modsecurity-crs /opt/tengine/conf/
/ q1 z+ I: Z6 a( N8 g6 ?% ?! j" c4 P
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf* u, x5 [2 z% ?# E0 {) _. R& M# S
2.启用OWASP规则:3 L, o( o7 n1 \+ B; r
6 G! Q! R. f1 k/ C- d复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
: H0 M) D* l( _7 O
3 X8 o( z |5 }- K! D& r编辑modsecurity.conf 文件,将SecRuleEngine设置为 on* T4 a7 D, c" {( Y
9 D6 s2 r; U" i8 q' B
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。+ c/ F1 b5 H! I% Q6 ^- ^
3 U4 }, a2 Y. M9 z2 q& KInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
" I$ H1 ^% R, H+ P, p0 VInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
! {" S" H2 H$ z4 s+ ]2 OInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf; y( H, W( x5 g6 j5 C
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf1 z9 u$ Q3 K& [9 y' A9 t+ ^: _
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf) I' W6 R) a' a. u
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
- `5 Z! J$ ~, P, S( t" DInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf5 a# j* {8 N0 v( h
五.配置nginx3 m5 T# @$ s/ \& C1 H
2 n9 k. `( B; H1 h
在需要启用modsecurity的主机的location下面加入下面两行即可:
( T, i, S2 d0 e# u U1 q2 l$ P) C. l* G9 G' k% m V
ModSecurityEnabled on;
+ n# d6 J2 f: R* YModSecurityConfig modsecurity.conf;# f, L) ]$ K' ?/ _- ~+ t" Y
下面是两个示例配置,php虚拟主机:
& W7 d- Y, D4 c
: b- z) ~. `0 y' J3 i; `) }; I9 zserver {
& N1 x) [* J( O listen 80;% \7 U1 m$ Y+ S3 U0 I4 t0 r" u- B1 y
server_name 52os.net www.52os.net;, H, E& B; y$ {" f9 f
- `4 K T1 R0 i* a7 g5 O* s
location ~ \.php$ {
; N7 x9 y# U+ @ ModSecurityEnabled on;
, N4 \& F: w3 [/ G ModSecurityConfig modsecurity.conf;2 ?8 x* h! k( [) f) y
- V) t3 u% W; `' R; f5 p
root /web/wordpress;' J# o7 ^% y& z6 I& L; c- K
index index.php index.html index.htm;
3 N" V- V n4 o0 P$ V8 e d6 O ! ^& O" ]/ f% q& g1 p1 g$ g
fastcgi_pass 127.0.0.1:9000;
, | D9 T8 T) I" O v: P7 V2 ?, O1 ?! | fastcgi_index index.php;
7 e% g) s8 h& ~4 I7 Q fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
* `0 y8 `1 K7 A% `# b+ q include fastcgi_params;
+ T$ {8 ]& U* y! _. H8 j0 I( K4 j2 [" i }
# f* A1 t: _2 t$ U }
& @- u" ^0 w. [' O1 _upstream负载均衡:
( H. m" C+ B3 X9 P' P3 S" x, `# z# R7 W" O% k, w8 I
upstream 52os.net { L, f- x+ U5 T: Q
server 192.168.1.100:8080;
# l. ~* V! `0 ]7 I" g server 192.168.1.101:8080 backup;
9 K; ~) x5 D5 ~% r! K}, v7 q) n" \) D, @- s* O" {
# d5 O, ]3 X- S6 V; i$ v
server {
, A" S7 B8 t2 I! ?listen 80;
7 b. m0 o% c9 Z1 h2 O# `server_name 52os.net www.52os.net;
* _# R8 W1 F2 u3 ~! _ F: F
# c' c! X3 |7 Ylocation / {# Q! A& T# A; m2 l$ ]9 z# U! q: H4 T
ModSecurityEnabled on;
8 I, L" C5 a/ ~ _7 t9 \- f ModSecurityConfig modsecurity.conf; q, C$ k1 j4 Z% t' R% \
3 A5 G. T' r& _' a' _
proxy_pass http://online;+ F$ }( [# j b6 K! `& g
proxy_redirect off;
0 f+ D& U3 ]1 i5 Z0 _6 q. I! } proxy_set_header Host $host;
0 w1 \1 x* j+ ^& t( L; J proxy_set_header X-Real-IP $remote_addr;) H- E% t. P5 ]. p5 k; }9 Y
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
3 [- Q- f* o& ~1 }( p0 k1 [ }) @% \5 T. u) w3 i S( h4 L5 I
}+ i0 S+ q4 Q1 z5 |: l: O% V$ A+ ^
六.测试
4 Z3 w4 l6 g1 d, v, Y. ~
! l& C' ?) M/ l1 P: i; _6 q我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:- B$ ]2 X O6 z: I, W$ ~
% E! B' ?+ x# E$ O* R7 a<?php7 I O3 u: k8 r: A, H. @: i2 J, i. e
phpinfo();
; {4 }/ J1 o5 G?>
/ i" g! U. ^% | o- h7 M0 l在浏览器中访问:
; r5 s# S( C+ _$ `6 b! G1 z) p
8 |, P) a& y6 S+ L2 C6 }http://www.52os.net/phpinfo.php?id=1 正常显示。
/ e8 W7 E2 g; E* G( e/ M% _& O( shttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。2 |7 c' e5 K0 Q L- y
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
/ W5 Z5 Y$ s: J8 T% l说明sql注入和xss已经被过滤了
% @8 b6 G* U. H7 o
1 ^, \6 V: T7 D七、安装过程中排错* G# ?7 Z, ~ z$ k$ P# [; d
9 S. n. |: G7 x3 N6 l8 Y; }8 c9 q
1.缺少APXS会报错& c" c8 X S% y, g1 n
3 i) W0 U4 U2 `+ Q+ g% f
configure: looking for Apache module support via DSO through APXS
& G9 M# N- F+ c- Q' R- `configure: error: couldn't find APXS! k& G- M5 [3 ^% _
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
! A6 T& w) M& w! k0 k& w解决方法: C$ F6 O) ^- d8 W: q) W
$ M( k8 D4 ]9 Z+ @: Cyum install httpd-devel+ ^& t+ ]& E! {9 ^9 P
2.没有pcre
" ]7 {2 z( w# V; {5 s2 K& E
3 Y' f3 q: f; \) aconfigure: *** pcre library not found.
* ^- u' w0 N6 j" ?' e! aconfigure: error: pcre library is required
' J$ ?) S2 ]# U4 I6 `解决方法:
* N0 x/ F1 V* _- v& \: ^0 M
5 ?# x) r9 f* G' k/ x/ o5 Gyum install pcre pcre-devel Z; f8 g3 E8 x0 O$ M, v& Q
3.没有libxml2' q6 ~" O3 u" w6 E
( M; t! o5 |( S0 \1 b0 Y
' e- I' T6 t) m4 m
configure: *** xml library not found., J7 @4 h6 U( U, B1 M0 T
configure: error: libxml2 is required
% f c" _4 R0 O( u' b; I解决方法:
' s x* c0 w! G. T) z/ B
& B: H+ m* O5 N; R. ayum install libxml2 libxml2-devel( W4 W9 n! Z; H
4.执行 /opt/tengine/sbin/nginx -m 时有警告- _; D- j" z$ f! v" G
# ]& b* B, j. _6 Q* n3 b' M4 T
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
* q+ c' t" l# D0 U- Z( x, W8 } pnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
: I- ]2 ~6 g$ F# t, X原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
9 V" N" t6 d3 M& {# Q# T+ ]. J3 {/ H$ y7 j& T0 O0 J7 g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
. T% p: r' o- c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
8 ^$ Q* ^- J% q4 W: {2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
; c; C6 P( `- z6 I( ]3 \2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"8 c6 q9 }5 Q; B( Y0 z! D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
; @# k" B+ K9 Z2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.. D5 ^+ I' j' _! }6 x* d
解决方法,移除低版本的APR (1.3.9)3 ^% r5 d2 ]1 o! @4 ?( g
4 y# @: P7 `2 T+ S% r9 ?' c4 |9 y
yum remove apr
: E6 `# r- M$ Y& n; K V$ U5.Error.log中有: Audit log: Failed to lock global mutex3 \4 r- M7 ~: h+ ~! Z8 i
6 K* [$ _8 w: w+ \6 I* g) ?3 d
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
$ { S% b4 B% [5 U5 O; zglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]( U& T9 m4 l9 Z6 j
解决方法:
8 h+ C: k" r* _% S2 Q2 E- p编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:( j4 t) N% r3 A% }/ n0 a1 Q5 I
2 f5 e8 k+ C% U- x O z; X
SecAuditLogDirMode 0777& c2 V" _0 r+ b; w# K/ G
SecAuditLogFileMode 05504 M. l( s6 n) J3 W) ^5 V
SecAuditLogStorageDir /var/log/modsecurity5 i' ]5 X# o) ^. q1 d4 N( d5 @
SecAuditLogType Concurrent
, q' [) l( L' n/ A) m参考文章:
+ L% X1 R5 V( y; I# I9 Khttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX" b" Z. \* V8 M) c- Y
http://drops.wooyun.org/tips/2614 |
|