ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。6 o1 Y, M1 H9 C! s" O
# `. e# k4 o" d$ Q6 H! s/ d* |; A9 h4 i% m9 W6 ~
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
( J' t" |. U! x h4 X; G$ P2 f. p
4 N! c" s- u; ]' e6 Y% s9 ]
什么是ModSecurity
# z4 ?5 J" Z. b) ?7 hModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
' x0 Z+ i F% h- M
7 j; w! |3 o5 [) f3 F( c: C+ D |: F6 q$ K& Y" D$ y
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
n+ k9 m( ~+ l3 m: v
, i7 \% O, B4 t v7 t' n: k8 a' f
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
3 R. [! n7 h5 _: z" p
8 n* p2 {: [( ~+ Z3 t* p- g5 ]
9 D# k7 P, U5 b6 ~6 T& {& d, w官网: https://www.modsecurity.org/
; n( i( p1 [+ g
0 ?; r+ r0 E5 c& @# _7 ]
, c2 T) T; Y6 a什么是OWASP CRS+ j% E9 [9 Q" ]! ` u
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
" {" g' ~9 N7 k* J6 N$ K" k3 o' j+ V( o* s. J9 q
0 U: e# P% g( @9 K* g3 V/ @ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
2 H, v( t" V3 i- T) w2 W _; ]. J$ ~6 ~1 p
0 s) D2 `" f3 A9 D% O) hHTTP Protection(HTTP防御)
+ q; [. _- \7 K6 j# w& ]HTTP协议和本地定义使用的detectsviolations策略。2 l. @- D7 |& x3 |7 O4 q% I9 n
) T' m+ [. X& S4 g7 x. r/ E3 w& ?! H2 R
Real-time Blacklist Lookups(实时黑名单查询)$ n C& w3 P) N& }* X
利用第三方IP名单。
# c7 V+ t" w5 c+ f1 t+ W; P& z& s1 O |: V9 e$ v
% c; P7 v: G( R, \# ?3 FHTTP Denial of Service Protections(HTTP的拒绝服务保护)
. m' j7 |/ n( w$ M! M j9 ?0 r0 H防御HTTP的洪水攻击和HTTP Dos攻击。6 ^2 F! b' q9 p+ m/ e4 ?( \# o
( }5 n7 E4 Z/ o# {( \& B) b
4 [2 L- r( j3 s/ b( s6 Y3 WCommon Web Attacks Protection(常见的Web攻击防护)9 Z g" f, U' i2 @/ B1 Q
检测常见的Web应用程序的安全攻击。
4 X7 R! _. k& y" ?8 G y5 e; W3 ^: @0 W: I" g' T8 ?3 h/ A( ?6 b3 y6 N7 Z
4 W# Z- ~3 V6 Y9 B% K3 l1 k' g1 q6 `% ~
Automation Detection(自动化检测)
a6 j. n7 P/ ]检测机器人,爬虫,扫描仪和其他表面恶意活动。
! r% k+ c% h9 E8 H6 n
5 B h, l% i3 e! r9 D$ F+ j# j1 A& e- f: b$ ?& q
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
1 `( ^! m2 v- D* y; W检测通过Web应用程序上传的恶意文件。" X! l6 Q' R* r. D; b
/ `7 r, L8 ?8 w- F# n: ?: q
$ m2 l3 p5 T0 D5 i7 L& e( B" a( F. WTracking Sensitive Data(跟踪敏感数据)
6 x% P a: @3 j信用卡通道的使用,并阻止泄漏。
2 o) ]: j( t( z$ n- d/ J5 X/ j
# {# T5 w. q3 Q" G6 ]; s2 q+ [3 d3 F- D! P o$ Z# f m+ ~! W
Trojan Protection(木马防护)
" ^4 S8 n4 `9 A: n% D ~检测访问木马。 a( ?. ~5 B: {( {
' @) T5 Y* w; B0 Q* u
" T2 k5 S# |: S6 A/ f! rIdentification of Application Defects(应用程序缺陷的鉴定) s/ z) {5 K; F& u7 G' ]: j6 E
检测应用程序的错误配置警报。3 v4 z- C& V% W8 \1 @0 |# ?
; [: K0 a5 O0 X! c3 j
% f# F7 q) u# ]2 V$ V, ^/ IError Detection and Hiding(错误检测和隐藏)* o* c2 J9 n/ T' A4 M: [
检测伪装服务器发送错误消息。
6 c6 ~' B/ _# S1 C( f5 k% T5 M; m& H& k
. V. Q" f2 ?. r$ f) d) W/ \2 l( f: S
安装ModSecurity
% v0 @! a. V' V. q" ]$ @软件基础环境准备
' ^) U0 k) s# G, T& p1 P0 e7 r& W下载对应软件包
# o. K- I; m7 Q1 G9 `8 o2 [$ cd /root% a8 J6 _9 {0 f$ a& q# e( J
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
( Z0 A8 X5 s! y/ e1 d& g; g$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
8 O6 X/ |- l/ z& F2 r安装Nginx和ModSecurity依赖包
- {, H+ f! C- l7 }3 y8 V- OCentos/RHEL
1 D( X- F& i: h y5 Q$ N8 ?% K' q. m, q$ {9 s
! Z5 P: Y {+ ~( N) k$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
( R# u0 R" d) n$ ?1 T. RUbuntu/Debian" k' V' a5 R1 Y, E6 d! L
5 P. t' p1 h: Y; L
0 M3 t$ K. Q6 w3 G9 \$ apt-get install libreadline-dev libncurses5-dev libssl-dev perl make build-essential git libpcre3 libpcre3-dev libtool autoconf apache2-dev libxml2 libxml2-dev libcurl4-openssl-dev g++ flex bison curl doxygen libyajl-dev libgeoip-dev dh-autoreconf libpcre++-dev
% f6 e: |2 p& X. ~% I编译安装ModSecurity1 I l: _+ O6 v( I+ K: ~% f( f
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。7 K6 V4 }1 j" d$ j0 ~' s2 |
4 ^7 v8 J8 G5 v, k7 F9 u) J1 ^3 Y
0 O- T( {$ h# A, p) q9 |方法一:编译为Nginx静态模块
, q# J0 G! _) U! Q6 ~1 ~3 L+ e8 G6 o$ C c5 B: \' u; s* c: V/ y
/ @' l" A9 W4 ?" U0 d编译为独立模块(modsecurity-2.9.1)
5 W: v* m% i3 E# `$ tar xzvf modsecurity-2.9.1.tar.gz1 V/ b- Z! ?: w+ A
$ cd modsecurity-2.9.1/0 P' h/ _. n( W4 a) i# Y- U' K
$ ./autogen.sh
( s& n! A) ^8 P) d$ ./configure --enable-standalone-module --disable-mlogc1 Y6 ~7 p2 t5 ?% y) R0 y
$ make
* Z$ V# Q) d' o+ p2 F) Z$ v编译安装Nginx并添加ModSecurity模块7 t: d B5 f1 j- W# m5 \
$ tar xzvf nginx-1.9.2.tar.gz
! I! P8 l. Y& z) e$ cd nginx-1.9.2- G+ t# `( Q7 S' G/ U+ U
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/( n$ J: r* S. g( s- \0 h
$ make && make install
3 @6 l, z" n) f4 N# Z! U& z7 g) Y5 ]方法二:编译通过ModSecurity-Nginx Connector加载的动态模块 J2 p# y$ |) {1 f- U4 k: E, f' o! v
_/ G4 y4 q5 N5 {
- p4 W; W9 @& q, a/ c) w: d$ @- J编译LibModSecurity(modsecurity-3.0)
6 ~9 b2 Z6 k# U1 u& P' |2 H$ cd /root
7 u' }7 H. \% _9 F x( E+ q! ?$ git clone https://github.com/SpiderLabs/ModSecurity
2 I/ H" P. K- [+ n$ cd ModSecurity% S1 |5 ~# Z2 g: \; p/ r
$ git checkout -b v3/master origin/v3/master
: H' C2 S/ c Y7 }! W' k$ sh build.sh
" ?8 s) M$ o; g$ git submodule init
; X+ m/ E% A9 h$ w& O+ |( Y$ git submodule update
- @* m( `4 q# d# c n$ ./configure
; Y1 K( P! |( z' V8 l# k0 F$ make
" Y# A/ Y1 E' {/ j/ |$ make install
2 H% d+ m0 f5 ?LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
) s5 L9 v4 n* W4 e: L( S: J" ?
3 i: H$ f S8 |3 [, V+ c; T+ z" I( ~
$ ls /usr/local/modsecurity/lib0 m) ]3 d! s# C
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0 ^/ Z d: q: ?
编译安装Nginx并添加ModSecurity-Nginx Connector模块
; w3 c4 F( S: ~; d% V( M6 P, t使用ModSecurity-Nginx模块来连接LibModSecurity5 |' R! V. k" N8 A
1 h& ^& m- s& d6 J
5 L" ^/ o: \6 R1 g( _0 U7 N
$ cd /root5 Q/ @7 U) N- q! w* Y& l
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx& i, l3 U1 ?- G- c r# w5 d9 N' b- e
$ tar xzvf nginx-1.9.2.tar.gz* ]$ {$ L9 ]) K/ B, s2 h: H& o. T
$ cd nginx-1.9.2
9 I. M+ Z1 J8 E# B$ ./configure --add-module=/root/modsecurity-nginx
3 R4 e1 m4 F9 }9 y- e. q" m+ }$ make) u8 y$ }& }, o8 I$ V
$ make && make install# N! F3 z8 p( M: D
添加OWASP规则
0 q; ?5 g! t+ { H' K3 ?ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。2 `4 t7 ^2 y* x1 v$ n4 G' { o
# u! _/ e6 d" w1 \+ z
7 {$ O( `% s; ]" Q+ n4 w$ l+ s/ |下载OWASP规则并生成配置文件
1 v i' q5 b. S( @; ] C. [3 A$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
* H. ` n0 ^6 e7 d$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/3 K1 ?6 {- k" Q. E6 |3 e& C
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs% C- K, V; |+ m. n Z
$ cp crs-setup.conf.example crs-setup.conf
. J! c7 H- R$ r6 @. m* U2 m配置OWASP规则
) h8 ~3 l8 w5 S* Z) @; i9 [编辑crs-setup.conf文件( C/ _ t6 i. p! u0 k
$ X( p3 d" h9 r7 f
( ~ K* G8 e1 S. D- I# p& x/ \
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
' _; ?0 U# v; [$ d l3 N9 ?$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
6 U; f+ _% u* j# p$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf) \' A# Y, R$ A- t. g3 m7 \
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
* A- m' c4 ^% A. A默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
4 j0 ?# y3 }9 E& D
. D5 U, v" @5 u5 q1 j3 ?
2 [& S7 t5 q# V4 ~7 l' p启用ModSecurity模块和CRS规则/ G- G$ C' M- _0 R
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。. v8 c2 W! [3 D8 k
1 f1 r7 x7 u) I" M
' k! D* d7 ?" o( B* P3 }modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。 t* d [9 I0 @- T+ z. G% s
: N p5 ~8 \* M( g5 S$ O4 r
( P. q# Y; \8 d+ f7 ^$ cd /root/modsecurity-2.9.1/
$ s, x5 A# o$ B: ^' W3 J. m3 J$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
) v f+ i" ^% P0 h6 s8 w' ~$ cp unicode.mapping /usr/local/nginx/conf/' h3 h# P. w* p5 _6 Z7 k% D
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。4 j# Q2 i3 m4 I2 c- Q2 H. k. G) O9 ?
) ~; m. C% z) ^
6 n- s2 j; n. [7 M7 S$ vim /usr/local/nginx/conf/modsecurity.conf2 ~) M: K* Z3 `
SecRuleEngine On
! S- e1 d$ F9 ?% L! P4 F0 b, k$ z1 lModSecurity中几个常用配置说明:. _( G0 T* {' _4 i4 F
9 d* C9 l8 |7 C2 K4 U" {: P/ l/ N9 F, d: H
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
1 q! ~' {& F/ s" I. C) _
$ G+ L% q5 \# H4 |, V4 V
( @; m! Y0 ~; J# h2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
3 P9 j' v; a8 Y4 L0 R+ R n& f4 R/ E
" y" a) g& n, R5 M' @2 e! X* |3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。0 }" K0 R4 B/ Z V3 }8 F
3 j* y( s" Z: n: J# f0 R0 d
! h1 H, y4 Y$ E4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。; T+ n6 l7 W3 {
0 M- U; ]% \3 g$ q8 p. V
) {6 u1 m% S& u/ k$ x l+ }在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
& u: o" r* n8 k: o+ m/ K5 J) u/ L0 ~( k
`& {( D* b6 i9 \5 b7 j
3.x版本CRS
/ R+ p) O+ W6 Z- y$ cd /usr/local/nginx/conf/owasp-modsecurity-crs/ q' b2 V, y$ V8 q2 J! z1 g: g
# 生成例外排除请求的配置文件3 M# w- K& u# c" S |3 [) H* B
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf: S$ {1 K& A8 {' U; k- O! v& T$ `
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
" S- x9 g# I7 K$ cp rules/*.data /usr/local/nginx/conf, W9 ?/ m* Y" q& N
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。: Y9 V8 B+ i h; A# n2 k- p
9 q- S& |2 o3 G/ F/ i9 J1 F
- j. A7 Z' M$ ~, V4 M" X$ vim /usr/local/nginx/conf/modsec_includes.conf* }0 }( G" a+ G# C+ C$ m
" K) v+ m W2 e2 v
[Bash shell] 纯文本查看 复制代码 include modsecurity.conf
include owasp-modsecurity-crs/crs-setup.conf
include owasp-modsecurity-crs/rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
include owasp-modsecurity-crs/rules/REQUEST-901-INITIALIZATION.conf
Include owasp-modsecurity-crs/rules/REQUEST-903.9002-WORDPRESS-EXCLUSION-RULES.conf
include owasp-modsecurity-crs/rules/REQUEST-905-COMMON-EXCEPTIONS.conf
include owasp-modsecurity-crs/rules/REQUEST-910-IP-REPUTATION.conf
include owasp-modsecurity-crs/rules/REQUEST-911-METHOD-ENFORCEMENT.conf
include owasp-modsecurity-crs/rules/REQUEST-912-DOS-PROTECTION.conf
include owasp-modsecurity-crs/rules/REQUEST-913-SCANNER-DETECTION.conf
include owasp-modsecurity-crs/rules/REQUEST-920-PROTOCOL-ENFORCEMENT.conf
include owasp-modsecurity-crs/rules/REQUEST-921-PROTOCOL-ATTACK.conf
include owasp-modsecurity-crs/rules/REQUEST-930-APPLICATION-ATTACK-LFI.conf
include owasp-modsecurity-crs/rules/REQUEST-931-APPLICATION-ATTACK-RFI.conf
include owasp-modsecurity-crs/rules/REQUEST-932-APPLICATION-ATTACK-RCE.conf
include owasp-modsecurity-crs/rules/REQUEST-933-APPLICATION-ATTACK-PHP.conf
include owasp-modsecurity-crs/rules/REQUEST-941-APPLICATION-ATTACK-XSS.conf
include owasp-modsecurity-crs/rules/REQUEST-942-APPLICATION-ATTACK-SQLI.conf
include owasp-modsecurity-crs/rules/REQUEST-943-APPLICATION-ATTACK-SESSION-FIXATION.conf
include owasp-modsecurity-crs/rules/REQUEST-949-BLOCKING-EVALUATION.conf
include owasp-modsecurity-crs/rules/RESPONSE-950-DATA-LEAKAGES.conf
include owasp-modsecurity-crs/rules/RESPONSE-951-DATA-LEAKAGES-SQL.conf
include owasp-modsecurity-crs/rules/RESPONSE-952-DATA-LEAKAGES-JAVA.conf
include owasp-modsecurity-crs/rules/RESPONSE-953-DATA-LEAKAGES-PHP.conf
include owasp-modsecurity-crs/rules/RESPONSE-954-DATA-LEAKAGES-IIS.conf
include owasp-modsecurity-crs/rules/RESPONSE-959-BLOCKING-EVALUATION.conf
include owasp-modsecurity-crs/rules/RESPONSE-980-CORRELATION.conf
include owasp-modsecurity-crs/rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf ' q' ?5 y# j' C- B
; {: V f- i; O, h* U3 P3 j
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。0 i Q ]/ T9 {* G& \: V7 P
( q7 b+ g. b* u* B6 H! x
1 v+ M o# k2 H9 k- X配置Nginx支持Modsecurity
( n( X" Q( Q9 J8 J启用Modsecurity
$ q" `- X7 T4 G9 n# O N使用静态模块加载的配置方法
4 G) `; H! k G: F8 D在需要启用Modsecurity的主机的location下面加入下面两行即可:
T9 i8 P* E: A' F! i, N
. T4 y; Y2 \* h# @" k7 b! J% O( P. Y" Y v, ]
ModSecurityEnabled on;
+ [6 x& d8 a$ Z% h% f# u& SModSecurityConfig modsec_includes.conf;; o! E& ~$ [, N% A' i% L6 U2 O3 v/ P
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。% W w. M( c0 F# _, e
5 G4 W2 K6 K5 W
! k; h2 d2 h T: |: R$ vim /usr/local/nginx/conf/nginx.conf7 M3 ]9 k0 s' B3 l# Z/ F
, X: }$ O7 h) X
9 ]' w# f8 C) ?( g7 {8 T- [7 Tserver {' ^- e8 a, Z7 r/ E$ P
listen 80;- x6 J6 t( q2 g4 U( D8 J
server_name example.com;
$ l5 b2 |" p6 {
- A' `$ t) W+ u: ]' \; z4 d8 ]0 a- c) | ` t
location / {2 q' S, k5 \7 F9 p K* t
ModSecurityEnabled on;/ l( g8 f' A/ }! l" y3 S6 \
ModSecurityConfig modsec_includes.conf;3 Z. d6 J3 S4 Y# [6 E: g
root html;
. T- u2 Z# L/ t# p9 g index index.html index.htm;
2 H6 D4 V9 z$ I& b6 v }" T2 A+ W. _2 M4 c" |
}1 `6 ?& f3 B9 S& P# G
使用动态模块加载的配置方法% M; e) `5 n1 E1 `/ R1 t
在需要启用Modsecurity的主机的location下面加入下面两行即可:
$ s+ h7 m0 U2 ]( Q; b$ A: K0 B" P1 c- s: w
: e) E8 P/ h+ B1 p n K/ K, Qmodsecurity on;. t( e; A* M" u9 Y( l
modsecurity_rules_file modsec_includes.conf;0 h N7 h9 T' k' [2 t
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。9 k/ X; u7 h* \& T9 y# j
* D1 f4 ]) {2 J x; {' D
- L! ]$ q) q$ q& A2 J: |$ vim /usr/local/nginx/conf/nginx.conf/ B9 c1 J/ T- P$ k. B) V/ H
: }) m7 q4 n& s. R, J1 r! ^
/ l8 w8 ^& A/ h9 z3 M3 L; J- Iserver {
9 \7 W0 l6 Z* I4 t5 @ listen 80;+ m6 Z0 }9 }8 f! n
server_name localhost mike.hi-linux.com;! }7 S8 _" | W; X) ~/ d
access_log /var/log/nginx/yourdomain.log;
# U/ y- i7 c! V$ b& K( |8 X0 p3 \7 A
" G, K4 T* _) y. ^ e$ v, J' ~/ r location / {2 c# ~1 y0 T: A% Z% X; `
( y7 F. M7 C' J6 H3 q
/ c. H0 ]8 b4 Y& {/ Y/ i2 h: o modsecurity on;) x6 s$ i5 J/ Q
modsecurity_rules_file modsec_includes.conf;) y2 F; C9 J& @; ?3 N2 F* W
root html;: H7 o) U" N. V% q% u5 b. Y6 W& a
index index.html index.htm;
% p4 H, U$ u8 x& S4 e5 T. W}, f* g, z" r1 E& \, c: b7 v
}8 y/ n! \4 Y/ U& t: U, {
验证Nginx配置文件7 G1 o# a, V9 \8 P% y6 g+ L
$ /usr/local/nginx/sbin/nginx -t" p# M" K; a l! @- K. e) C
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok% Q' P: q1 Y0 j4 i% L. G2 `6 n9 S* L8 F
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful! r- X0 [6 r b
启动Nginx
1 ~8 U3 f0 O& C' v7 i4 T$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
3 O8 A% q1 D. Q4 a! q& _1 Z
7 b6 v: g; }3 l5 k t# @1 w测试Modsecurity ModSecurity现在已经成功配置了OWASP的规则。现在我们将测试对一些最常见的Web应用攻击。来测试ModSecurity是否挡住了攻击。这里我们启用了XSS和SQL注入的过滤规则,下面的例子中不正常的请求会直接返回403。 在浏览器中访问默认首页,会看到Nginx默认的欢迎页: [/url] 这时我们在网址后面自己加上正常参数,例如: 。同样会看到Nginx默认的欢迎页: [url=http://img.colabug.com/2017/06/842f48f203c6c2cd30144f29b57af97a.png] 接下来,我们在前面正常参数的基础上再加上 ,整个请求变成: [/url] 就会看到Nginx返回403 Forbidden的信息了,说明Modsecurity成功拦截了此请求。再来看一个的例子,同样会被Modsecurity拦截。 [url=http://img.colabug.com/2017/06/246ce28e95310a32f791893d4f5c55ca.png] 查看Modsecurity日志 [url=http://img.colabug.com/2017/06/ae44dcb58b8a4a0ea761317e398b3101.png][/url] 所有命中规则的外部攻击均会存在modsec_audit.log,用户可以对这个文件中记录进行审计。Log文件位置在modsecurity.conf中SecAuditLog选项配置,Linux默认在 /var/log/modsec_audit.log 。 $ cat /usr/local/nginx/conf/modsecurity.confSecAuditLog /var/log/modsec_audit.logModsecurity主要是规则验证(验证已知漏洞),Nginx下还有另一个功能强大的WAF模块Naxsi。Naxsi最大特点是可以设置学习模式,抓取您的网站产生必要的白名单,以避免误报!Naxsi不依赖于预先定义的签名,Naxsi能够战胜更多复杂/未知/混淆的攻击模式。
: H; a- M% U+ j O |