ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
5 v- X' k5 D. N* ^ [+ k' @2 x: A+ D# }
$ `. o# w* O2 u
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。# [) w' I( w' A0 X% l
- S3 I# L& {3 W' n- r. D
: Z2 R5 w8 V4 {: e9 Z8 S. h什么是ModSecurity; D3 H: u4 R+ D) q* x5 K
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。+ X+ Q# b: Y, ~) B& s
, f% z' X( _$ h4 {! K& L# E' q( n j! r
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。5 k& K4 o6 V4 E0 t# x
: k, ?5 b" ~+ A. f2 C
% o$ ?6 p1 O+ _2 k# y
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。+ z+ a% R7 ~1 _! S# U+ R
1 w# I% g* P% ?% P! D/ T
8 R3 L) t" |7 D! E3 g
官网: https://www.modsecurity.org/1 q( `$ h& _# o( p, Q
) f5 e) d7 V( _0 s8 k M
, v: z" h3 }: ]+ {3 P+ p什么是OWASP CRS
0 i; F/ k, ]7 i* c: `" jOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。% C0 }5 E; W7 v1 [6 E; p. d$ n6 b1 w
4 J% h9 O$ N6 B" i8 N/ [
9 t9 P1 m+ I( J0 ]4 K% f. t1 w! F
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。! a1 \' X5 ]9 Y
1 c# @& N J* p+ }0 ~6 b/ t0 n6 x+ ^0 d
! U. d2 \% V7 Q' F0 _$ Z4 u+ nHTTP Protection(HTTP防御)& s, V4 Z9 r4 c- p7 T/ @
HTTP协议和本地定义使用的detectsviolations策略。
5 u% c% ~& X8 U; }% }) W0 ]1 ?
6 @4 @' \/ R j$ Y" m: J
( x. K7 Y" q( V$ sReal-time Blacklist Lookups(实时黑名单查询)
$ ~: y3 }3 J$ b2 f5 H% `5 n利用第三方IP名单。3 @7 ]# ?+ C. _ G% L
' W" ]6 C* B G
' X! l. E, h& u
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
! U* H; L) [$ S6 I. z% ~8 a防御HTTP的洪水攻击和HTTP Dos攻击。! d+ P+ p6 I& V, T" ~6 T
7 l3 B: J% |* V& l0 b1 j
( }) t* S/ x" J# I6 r
Common Web Attacks Protection(常见的Web攻击防护): Z8 @' Q' [; { e
检测常见的Web应用程序的安全攻击。. E3 n% J$ @9 u+ v v
2 G4 X- f( |) T2 R: Z
6 R0 r1 _7 ~3 _Automation Detection(自动化检测)
8 }7 T* i; V/ U3 D4 R) m- V检测机器人,爬虫,扫描仪和其他表面恶意活动。
! V- T/ `; r1 Y( m
5 W/ k6 F) I* [6 b! b. V( I
, V, W: ?6 \% EIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
W1 Y* k6 g% U" t' ^& L检测通过Web应用程序上传的恶意文件。
# K! X5 b B( K- p7 C5 T4 s6 c3 @* ?) c
% t2 B+ z% J& H, Q9 v
! e7 ]9 [, O/ S" b. l9 U; zTracking Sensitive Data(跟踪敏感数据)
* i& X% o, a3 `5 E信用卡通道的使用,并阻止泄漏。
" Z4 ^2 p" ~' X4 L; O' N( P2 B! f# x3 E+ L. s, N0 q/ o$ F; U$ C
1 n: T; m# E7 D* ^+ FTrojan Protection(木马防护)
/ m( k! W/ p% v R% p8 i/ H检测访问木马。6 {: C3 p/ |. { s3 H& g/ m q
4 S# a1 v% |- n- q
0 c6 h" m# Q9 R9 H
Identification of Application Defects(应用程序缺陷的鉴定)' z+ t0 _+ P }
检测应用程序的错误配置警报。
2 r5 D" x0 e' D" K8 R7 e
' r D- Z0 |) ~6 ?' {' { C( [; T/ l3 y3 C- X) [" C& I
Error Detection and Hiding(错误检测和隐藏)# u5 k* L% ?: |6 ]5 L5 ^
检测伪装服务器发送错误消息。
T) p1 f' n: n1 q/ S% Y8 f& v" D, ?9 j! u8 ]# k8 \. \! {& R
3 ~; L# v- L* i9 C8 t
安装ModSecurity
: i3 L2 W4 x# S3 U软件基础环境准备/ X; }+ L/ o: v
下载对应软件包2 [7 W' X& o8 W; W- p6 q6 e! H- i( M
$ cd /root
! A5 `3 z Z$ |0 R9 [$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
; M! e; D$ J; ?4 }; b/ O6 B% i" 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
+ d) t) Z* V2 y* O7 Y安装Nginx和ModSecurity依赖包
: T) |" j @1 }1 Z* wCentos/RHEL; K1 Z% S* X2 H+ X
( o. t& B1 k/ E" \8 I1 Y
- w$ F0 Z4 D' {3 t; S$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel/ y: l1 n; Z1 C! y: U5 l' X
Ubuntu/Debian0 r! V g6 F d6 S$ j& t
- N6 n/ z! Q8 z6 C' A
5 W3 @+ ]8 V U7 E( o) _$ 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++-dev3 ` W0 J" K2 M2 ]2 ?+ u, p, q7 e
编译安装ModSecurity
9 ]- f* v7 D# Z" F+ YNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。! H# @. q1 @* X# K# c6 O5 v9 F# s
d7 C5 V9 Y% O4 u: {3 e- @
7 h1 Y5 X0 |0 `# j* n
方法一:编译为Nginx静态模块
1 ]5 j' Z) R7 ]) A. X- ]9 o6 _
( q9 @' H- w5 J! v2 X I+ \ y* P, ?& \% _! I$ Y
编译为独立模块(modsecurity-2.9.1)/ ~1 W0 X, D3 n$ b; I5 `
$ tar xzvf modsecurity-2.9.1.tar.gz7 n$ p* H' s9 e6 {
$ cd modsecurity-2.9.1/8 w0 f* R+ ], T
$ ./autogen.sh
: B5 Z4 N. U1 u4 B8 F) O$ ./configure --enable-standalone-module --disable-mlogc
! I3 @9 U8 [4 ^$ make' G. W. H {# V6 e$ D0 z, K/ g
编译安装Nginx并添加ModSecurity模块
- V% \/ u( R0 z1 a/ { q6 c$ tar xzvf nginx-1.9.2.tar.gz: S( }/ J" @: L* a* U6 F5 c
$ cd nginx-1.9.2
$ }8 ~+ O; w. t& v) u7 m6 o" T$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
9 _! F( @# }; W3 H/ }$ make && make install
. R5 Z+ s# e/ D, x) @方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
0 A* ?8 S- G3 M. _ F. L
/ i+ J" m. e; B9 S3 `7 T1 N1 Y2 N2 ^5 d9 I3 c% ^4 ~- c8 ~- y% [
编译LibModSecurity(modsecurity-3.0); Z% K) ]* E2 D, \$ R" b- v
$ cd /root Y- C( v, G# Z6 T
$ git clone https://github.com/SpiderLabs/ModSecurity
" s. u O8 o7 K4 d1 {6 u+ |1 f$ cd ModSecurity# j5 r& }4 M# g6 N6 P
$ git checkout -b v3/master origin/v3/master0 V1 e, D' S5 Y! H3 D3 S
$ sh build.sh
' W$ x! T+ `- ^$ git submodule init9 h* e" C+ Q4 x$ u
$ git submodule update' f7 ]+ t( o% j6 W! ?& U3 F
$ ./configure% S! C0 V3 @/ _, v% u9 w
$ make
7 L$ Z4 z% l4 G8 l* J, M: p2 h: J! r9 c$ make install
. k0 `* u$ V, N: G/ Y2 k% LLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。9 o" e3 Z J# ~0 f' G
$ j- [" c: ^) I6 I: ] j
# `" C0 t) q1 Z/ B7 b- _! n
$ ls /usr/local/modsecurity/lib3 a# D1 }/ k. T! i& |( j
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
. t- U+ L" @+ b s1 z, |2 o编译安装Nginx并添加ModSecurity-Nginx Connector模块
! W5 [( }. l) H; I: m' {使用ModSecurity-Nginx模块来连接LibModSecurity
4 J8 K& o, m9 f6 O, l, q
: h' q+ h3 |& t' x5 Q% p/ o/ h ]! C0 o
$ cd /root
6 d! D% ]* K3 y" d! }' t' i$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
5 d4 b. {: m6 y% X1 S+ I$ tar xzvf nginx-1.9.2.tar.gz
, Q1 r) Q7 d5 W! x, y' t$ cd nginx-1.9.2) g! c _1 A8 Q* j
$ ./configure --add-module=/root/modsecurity-nginx2 ]7 ?# L) ` k5 a
$ make
% O/ A& ]- ~- B: c* o$ make && make install
% j# \" U @8 ^7 V添加OWASP规则
9 l- o) _1 [8 ZModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。6 d. T; x, K( o/ f+ M; I3 t& W
8 d) Z; r0 t" y6 v1 z! V# \) G
' E- w8 R4 E, c( H; U9 l下载OWASP规则并生成配置文件7 f! B1 U5 K2 Z3 [: i
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git6 Y3 M( T* P0 @6 E
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/3 E5 v1 r: E! Z+ L
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
* }7 @' {" T- k' W5 _$ cp crs-setup.conf.example crs-setup.conf
+ L" \8 E; H" i% y2 A* D' u# f# ^* m& U配置OWASP规则
( t0 g. H, o1 I( x. {编辑crs-setup.conf文件$ F2 i! r. c0 \, \! {1 A6 ~
* Y! f" y2 z' c4 L+ H
5 X$ Z: C, B9 `2 ^$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
- M$ H5 u4 M/ A$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf, x# D& ?& g" d$ d. _6 P. P
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf" P5 L& I2 A0 G( l" q
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
8 T {. [$ F" _; R默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。( A! F' ]/ y+ J
' d$ O4 D' K' W: U4 r o) ^ O) Z6 o M0 s2 b
启用ModSecurity模块和CRS规则
X# {% _" a9 C \" m8 |复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
1 d3 U U% I2 k0 M3 \' c% D- r0 e" q# A# e" R- M
( n6 g5 ]% @( amodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
. |9 h7 i! P5 @: C# b. ~7 @4 A" _$ W1 I3 Y
$ i: n/ {0 X$ N2 w7 s6 h$ cd /root/modsecurity-2.9.1/ w* J. B- J9 ?! ?. ]) q
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf - W: \. V% n, ~/ D
$ cp unicode.mapping /usr/local/nginx/conf/
+ W8 ^1 n. l0 P- t% P: x$ c将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
. K8 E. a' n2 i+ y! S: h$ h, I# k( R& g/ U
( Z2 D5 {$ I8 x% w2 F% c
$ vim /usr/local/nginx/conf/modsecurity.conf: D& d' f7 I3 `, @* v
SecRuleEngine On
: Z* r% u- F" E# FModSecurity中几个常用配置说明:
$ H& |$ ?6 r. e2 ~0 j: M* U. T# W& g$ x6 t0 b) X
6 T+ s' O B$ v$ _, e6 A$ g1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
! F1 Z- D. M5 C# U6 s) N$ F* p! z. h: X
1 f( u& b4 i; ^4 B- _5 C
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
6 ?" s- z( \/ K2 n6 y9 b' V# I: F
) e8 h* [ ^7 V% N: x0 P4 u& G" w# ~% u0 Y7 @- u
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。+ a" o- H+ k. v! ?& p R- u
4 k* W" Q8 M. k
0 Y b8 [! t6 t4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
$ L) m) ]3 X+ ]( Y* e1 Z# @( k( e3 ^/ L
7 s+ V$ O( O- q" y
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
& F$ v& G0 x/ X+ e) m) _ f! {9 P5 ^$ e
) j# O$ x2 k$ p3.x版本CRS
- W$ O& x: x: \' m3 y/ x; B& T2 {$ cd /usr/local/nginx/conf/owasp-modsecurity-crs2 Q* k9 {7 t: h: J* G3 c6 U
# 生成例外排除请求的配置文件8 c. x$ Q$ D1 E6 A1 [5 s
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
" ]0 Z( R: B/ Y$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf: J( q3 g2 @( f8 G! n3 l
$ cp rules/*.data /usr/local/nginx/conf. d, I% F$ q5 ~7 Y. |+ @0 ^
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
. U: h2 h1 f4 f3 c+ q8 s; s' W1 O
" u5 f" W m+ a$ vim /usr/local/nginx/conf/modsec_includes.conf
5 Z+ H K) T, v$ Q
8 R( i u/ T+ }8 I: R" T* B' L[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 ' Z6 E) z" E5 T1 L, z! J& g
]5 Q. }( T( h6 t7 U
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。* y8 ?! Q- q+ C2 G" \
8 u. ~+ B) f+ y% z' e1 M2 f; b: ]( m0 x$ B
配置Nginx支持Modsecurity# b* a4 ^7 {) t1 i/ E: A B4 q
启用Modsecurity2 V5 {6 x8 \2 v' H
使用静态模块加载的配置方法2 f' X! B( G" }1 C* t9 T) r
在需要启用Modsecurity的主机的location下面加入下面两行即可:
* j9 w6 f+ d. H) y5 [+ V ^2 W: @" _7 x: K8 x7 M& S
0 _" \: W0 v# d+ I9 j/ M9 ~ModSecurityEnabled on;6 r: c& u# [4 B3 ~1 X! ^/ e
ModSecurityConfig modsec_includes.conf;. c2 o# R% H, R/ L& } k$ u
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
) U) Z n# e3 ^) Q" E. I! U0 W( y& W: V; v
+ N! w! V2 y3 b# Q9 [% F$ vim /usr/local/nginx/conf/nginx.conf
- [( W* p; |) _5 u
. Y* O) }8 d. y# k# {4 o8 G3 I* E0 Z$ K5 O. p
server {
! e6 g( Z' G- x& ^' }/ d listen 80;
" h* w. Y% M2 Y% R/ m server_name example.com; E! N- S4 v, l9 d% m
8 G* B) j0 n2 a5 _
/ T0 L: n, _4 W location / {* d0 m$ C9 D/ G- S3 w* @
ModSecurityEnabled on;
, Q! w9 s! m2 b' r ModSecurityConfig modsec_includes.conf;
1 l% e; a5 P5 B1 o- T root html;, B2 L' ^% f1 ?' z. b# \
index index.html index.htm;% [8 a) N! w7 N3 `* ], W2 J
}
s2 @4 }" }& A0 A9 p7 i2 i}
, R, }0 T' K H) e使用动态模块加载的配置方法
/ e: \; _* u( C* O7 G+ G在需要启用Modsecurity的主机的location下面加入下面两行即可:
5 n! S) W. t& m( \9 I, p$ y6 c3 W% [$ ^, ]5 p* Q
9 t: e& ?. W) j5 d1 u0 K% K& v
modsecurity on;
7 c4 h6 J6 B2 |modsecurity_rules_file modsec_includes.conf;
; B! |7 |0 \9 x8 E$ b! e) l( i修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
+ @, U8 [% q) D4 J% m j
0 ] V2 I6 C/ I! U3 G3 p! P7 R$ K2 H F/ K; i( l2 c
$ vim /usr/local/nginx/conf/nginx.conf
0 R- r1 L* d$ b
: ^3 F6 w4 W5 f G1 L! P& E7 ]
- |$ U, \6 c# M5 d: D. E8 ]% Iserver {
" s7 y1 _: a2 B1 X/ a* b0 c listen 80;
% c+ p" [0 |" M" O server_name localhost mike.hi-linux.com;
. c- M. H1 D5 P7 ]( w access_log /var/log/nginx/yourdomain.log;9 u/ U# c* ~& ]( I8 T( n* _* A
: M9 I7 M4 x: m, p: p! I7 G
u% C( m3 M# k3 F( d location / {
2 _" ~* `8 E1 w. g$ ~+ P8 C# R8 N/ C5 h; M% e( [
* ?) E' \3 D$ Z$ v i
modsecurity on;1 K' u1 B! j5 W' U f
modsecurity_rules_file modsec_includes.conf;
/ ]/ U+ J5 n9 z2 Q root html;8 y- L( M3 y, A* \. N* _& f$ ?
index index.html index.htm;
5 R! \! U, i( X# C$ n8 W' c}
* k d& w& h) i7 L; v8 a}+ x9 R W) X H' K" J1 V
验证Nginx配置文件8 `1 k* u. c$ @5 p
$ /usr/local/nginx/sbin/nginx -t
. K' }3 z9 x* jnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok) B8 ^1 t6 v2 V( x+ g$ V4 z
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful0 m- [# ~: D8 U8 x9 P
启动Nginx( F! E! H( M2 V: U$ l
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf' M9 X( u) U# T4 F# \9 E' }! _
+ T1 y Y Y" i( }
测试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能够战胜更多复杂/未知/混淆的攻击模式。
9 ]/ F) K" Z9 }& u5 x |