ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
7 ]3 z/ {( U N# u1 ~, M- | t; b1 t8 _9 {% t& H) ^
9 H8 U0 j+ X. S. V. }8 L# c! D" W
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
* C, n* M0 @- ?* L, b1 Z r* O! J @- U1 H
' ]/ O7 N6 |, f+ u0 @3 A什么是ModSecurity4 A/ H3 A6 `8 v; ~! U# r
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。- V G# s" h {# E7 f9 ]. t
" B" W$ @ M2 _. J" X
& \6 h- h& F! g: U
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
; l( k" ^& J% h1 L& I h! }; x
% g4 g" e; E# J' p+ } a k
1 M& G5 l1 u% M; }5 eModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。4 ~; s" h( f" ] [1 [/ w
4 `- s- B2 F% k- _$ Q. z1 ?
3 ~' ^4 x4 x5 X( X" E* \
官网: https://www.modsecurity.org/
5 w3 Z3 n& o3 k n' D6 { i
! l1 Q( ]# _0 @4 _. k# Y; x. g$ O% H2 S @. C0 n4 l2 N6 D
什么是OWASP CRS
# y( a9 E" N1 {; d, POWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。0 V+ U9 }9 R6 s9 p
2 e( W% [: N4 C1 {, p
2 d$ q: b+ C! \7 ~6 x8 f8 tModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
) ^' `; G* r8 @ ?+ P4 I+ K& a6 X4 t5 P8 H( r# }* S! Y4 A0 E
& B7 h* J$ f* |9 ^' l1 A
HTTP Protection(HTTP防御)5 t# ?# [' J% F8 \: q
HTTP协议和本地定义使用的detectsviolations策略。
8 ~0 u/ v" m' u
+ y) {9 r" }* f0 G
0 O9 g! I8 N2 D; f1 xReal-time Blacklist Lookups(实时黑名单查询)
, D" R9 d9 W7 S9 G/ `* t" `( Y& f利用第三方IP名单。; z+ j7 J/ z- a
, r2 u5 B) o# a& n3 g, _, b, s2 Y8 v$ o) h% H; M
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
! q' g/ h# ?' Z+ h防御HTTP的洪水攻击和HTTP Dos攻击。' ~1 }% |+ T( t" }, X
6 n7 x" J) X* l. t8 e! |# H$ I& c+ M/ _+ l
Common Web Attacks Protection(常见的Web攻击防护)/ R3 _9 y$ s( }( y; D c' G
检测常见的Web应用程序的安全攻击。 |+ ~- _- O6 `- n4 C1 D8 A, y) u: l
5 { X" z, \ [% Q4 x
: X2 J6 z# y1 n6 f. GAutomation Detection(自动化检测)
, k4 T1 W7 X' [! e& N8 D3 l+ O检测机器人,爬虫,扫描仪和其他表面恶意活动。" P: E3 S! [/ c' h" L# C/ t( b, g
" s7 E9 h; X+ O. ^" S. U
" K6 M' ^" F4 {5 F- ^
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)9 w0 f- ~' F0 Q
检测通过Web应用程序上传的恶意文件。
3 u' E! M1 n6 G! w# b. E1 |+ n( d4 N& t& @6 Z
" Z1 G: o# o- v3 K: X
Tracking Sensitive Data(跟踪敏感数据) Q4 M( e# S2 }' u
信用卡通道的使用,并阻止泄漏。! m1 O4 H- K j( Z
6 K' U5 ?. n& \* V- W
( S8 a/ P& Y; Y8 G/ D+ _4 }- h
Trojan Protection(木马防护)
' D! {' p s0 W {9 \检测访问木马。
" t/ j! c' P9 K- a+ }$ a# i& Z& c1 P/ f) a9 W5 @) o. o1 p
2 v9 A5 b! ~) K* e
Identification of Application Defects(应用程序缺陷的鉴定)# o2 t7 y" M: ~( Q. C6 W
检测应用程序的错误配置警报。( o. _% w/ Q$ c# K# T ]6 u
8 d- I* [/ u+ ~ y4 x
+ e* a J5 @/ y" s" I1 B5 E6 EError Detection and Hiding(错误检测和隐藏)
L. A- _/ v# e- d* f+ b2 J检测伪装服务器发送错误消息。9 ^& n8 e0 ?' j
. W+ I: f+ X- R, E
( |% ~: M5 b; R$ _" M* G
安装ModSecurity1 o- v9 u# h7 Q3 l* ~1 w& s
软件基础环境准备
0 O: ~8 G: C4 w; I0 g* J& i下载对应软件包- P% L( m/ W+ V O6 n" q' J9 g
$ cd /root
( X$ V3 Q3 I( y7 O, [( P3 {' m$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
# z+ }4 L5 n' \9 _/ L$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
. \1 w8 R7 A! C9 ]1 H! ?9 O1 D% G. [安装Nginx和ModSecurity依赖包1 D# V r$ U* o" @" @0 Q8 i
Centos/RHEL
/ B6 I) u1 T0 z0 E) K4 g
) ^. [1 ]% S9 U3 F# {# S0 C* }- c1 M" ?5 y; O3 y: {& u( O$ I
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
! z# g! W* z# X- r/ ~& IUbuntu/Debian: A+ ^8 N1 Q$ z1 D, E+ \- w' |
, }9 t( _9 _! m8 Q7 K& Z/ P& o0 B S) k( D; K: T5 X
$ 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
/ [ Y" f1 y' k, b+ u% N8 ?编译安装ModSecurity
& _: y& L) u9 F; V% I8 RNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
, n N9 `7 ?' W$ K
2 B. D8 u l' a" w8 y' p
- t+ _) t% J/ i n0 S方法一:编译为Nginx静态模块! `- Z* z1 w4 p0 J, n- W0 A
0 r6 \# |, [# m, D+ ?) ?- g, c. B3 K- w! T8 l9 y0 h
编译为独立模块(modsecurity-2.9.1)
2 K+ S: C: f6 t5 j$ tar xzvf modsecurity-2.9.1.tar.gz
! Y. N/ o4 G) k/ N; w- {* J$ cd modsecurity-2.9.1/
# p9 k! k$ w' |9 W7 |$ Q$ ./autogen.sh
# l& M3 J$ F5 `% m. x$ ./configure --enable-standalone-module --disable-mlogc
" C, f5 n9 W* Y& a0 d8 J. v7 Q$ make
/ ?6 y- Z* s" z! ~1 Z8 {: Z$ x/ D编译安装Nginx并添加ModSecurity模块& y9 z/ r7 A2 o+ t
$ tar xzvf nginx-1.9.2.tar.gz, Z) P) a7 u) H$ |
$ cd nginx-1.9.2
. v g1 @7 e4 U7 y- Z9 b( h$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
, {" V2 z% M1 U5 W6 m0 z# o$ make && make install% k# W8 u1 L0 Q
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块" ^+ p, L9 h* R& S) w3 n
( o- B' a9 @/ `% u @ A1 R6 U4 d7 R6 Z6 {
编译LibModSecurity(modsecurity-3.0)
T9 z' E0 b! f# }$ cd /root
, z1 ?* D |- z7 ^, M& D4 n2 F$ git clone https://github.com/SpiderLabs/ModSecurity' T6 S% H) b8 a7 w' _( R& U& i* r4 J
$ cd ModSecurity
* l- s% `/ Q0 a. R$ git checkout -b v3/master origin/v3/master- F2 a+ k+ O8 u
$ sh build.sh
& P8 F2 c. ~, Z) \. f$ git submodule init
3 O+ y- _) [0 ~$ git submodule update
" x7 R3 l$ f3 f7 H; G( ~2 m$ ./configure/ ?# ]4 t- j) @
$ make0 @5 `( `& y8 ~
$ make install' Y+ P5 c: P! Q9 W7 X2 J
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。8 y6 z/ [0 q3 t) c3 y6 i- j
5 o3 K5 t- }4 S T0 B9 j" M% u; _
6 I, Z4 O' e+ m- ?" ?2 l+ ^0 n$ ls /usr/local/modsecurity/lib( I/ |3 c7 Q/ Z8 |* e+ y
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
. b( q0 n) o4 G: e% Q% A: M3 u编译安装Nginx并添加ModSecurity-Nginx Connector模块
# b6 L( n" l, f/ x& ] g使用ModSecurity-Nginx模块来连接LibModSecurity8 U* j9 ~. n. {# `/ ]7 h9 \
6 G* o' f) l8 p- ^; y- V4 k- U. P6 F5 L2 Z) U$ q2 p7 u2 y7 S
$ cd /root/ h; ^* F6 g3 }9 W
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx6 N) n' _' N9 c+ m2 r- w- ~: B
$ tar xzvf nginx-1.9.2.tar.gz
2 E" [' c, z' A& ~4 l% l* X$ cd nginx-1.9.20 W/ E4 J* n# W0 u" v- P
$ ./configure --add-module=/root/modsecurity-nginx" M; e. m) @4 K
$ make k& [6 P) C) H1 f: y
$ make && make install
) x% t8 O! j2 p& q9 q' `2 ]- T添加OWASP规则
2 ~$ [4 Q E+ Q5 V xModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
) H ~: N6 Y; h" O9 b$ L2 q
$ }: A* [0 O( d. t$ m. E
: M P6 j) p& a下载OWASP规则并生成配置文件1 [+ m+ m/ S0 k5 A: D) k, k
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
9 g. i! T4 o; u. k$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/' A# C3 z m" L. f8 N1 S3 ~
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
- ^7 o8 r$ m! H$ m9 \0 e$ cp crs-setup.conf.example crs-setup.conf- ~ P8 e& q2 `# m
配置OWASP规则- p! H {5 _3 o+ Y
编辑crs-setup.conf文件
1 u1 f! G! a# [ w1 Z
0 d% U9 U( {+ r! I1 f2 T6 `9 T9 o) n5 [ ~ U
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf' Q; h: ^& h% |8 Y4 b( s' N
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
/ F- Q( j: l8 j4 }, Z" o' F: M- B$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf9 B& X! ~8 ]- i9 z* b. X' {( f [
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf( N5 D1 p6 p' q) S9 E1 S
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
# D& D6 ^' O3 W3 \! d! ^
) W4 L% c% f: x: N/ y9 f4 u% W1 c- J A' Q/ k7 ~! V# f
启用ModSecurity模块和CRS规则
4 |$ s7 z6 U4 s1 K' w* [( k复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
, V: C- r9 J1 L$ I) R0 {" }2 ?1 H) s: l/ d4 s" _( O
' e! D& ?- b9 J3 K5 nmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。) \. @: e" _( h& ` b
: P( l; ]* }! P2 q u7 b! f$ g
6 Q7 Q- H+ y- J* x& o$ cd /root/modsecurity-2.9.1/" f+ g" \# h4 ~3 v; c2 F
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf 8 H# o- Z9 ^4 D" a" n, f
$ cp unicode.mapping /usr/local/nginx/conf/
/ p! B1 t4 N& Y/ A将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
& @: V0 t6 l: r* {5 \
5 i5 S/ J9 s3 {' @9 j0 ^( p3 n3 }$ \
6 j. D1 I- q( d$ vim /usr/local/nginx/conf/modsecurity.conf7 q8 [+ c B! f: Y& Q0 o1 M9 s/ O
SecRuleEngine On
' O% ~# j1 w8 a* Z( OModSecurity中几个常用配置说明: n+ C* `3 j* w! c
) O3 y# j% x: f0 A! v* w/ E9 u
; @ z& F! }+ ^. r# T3 u' ]1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。* P H2 U2 r5 G9 ?
, V8 H' r* H4 g, M+ B
( q1 U- H( y( |" `2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。1 c/ U8 b3 N6 v' H; G0 C! g" u
3 e1 k& C7 s1 M9 `+ A0 D; @$ ?
9 D8 G: _& y Z* u7 B& i# [3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。" P* k3 T3 v/ F+ `
! x# a2 p+ c. a0 \7 C
$ d# S: H# Y5 ~; G$ v# ~4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。8 D6 r1 _9 k! g+ f. ?5 T3 P
( f% i3 C, _& R( c( J3 b" Z4 ]
9 R* u% K/ w( I1 s2 F" o3 g: O在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
) ?8 w0 r4 w2 e2 e; B- ~! W( n3 [, M, v6 Y# p- X! h
' ]+ z3 t, A, ]
3.x版本CRS
9 {9 P( N y( y' b8 u3 H$ cd /usr/local/nginx/conf/owasp-modsecurity-crs8 e/ X) A- @0 [) A0 `, O
# 生成例外排除请求的配置文件* B: _6 P' c2 B$ [1 Y& G. G8 d
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf+ x' g: [" o* @" I+ @# k
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
6 _" v$ B7 `" L* l u' M$ cp rules/*.data /usr/local/nginx/conf
8 o R2 L+ e2 r为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。! v' d. I$ `; r2 a8 n/ S* M
7 p7 C, K: O/ B# [% O8 W/ n o% Y
$ vim /usr/local/nginx/conf/modsec_includes.conf1 A! U2 b& Z8 N2 K+ _* |
+ h% K: M3 i, u7 \( G2 G5 ~4 U* i) T
[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
1 \! {8 d0 p% P5 a0 u! S1 @7 W
1 N: s b) }7 L$ H, G. F2 s# z注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
4 d8 M. @; r2 W( T, e& [* n7 e P& Y
6 w F# v+ | s) w7 k( q* B8 {
2 c0 O% H: e- p* N' y7 a! B6 {配置Nginx支持Modsecurity
+ n7 x8 {( a4 ^2 h' {& v- W$ z4 K启用Modsecurity" s) S/ d Y" A' n
使用静态模块加载的配置方法
* A9 w) u# {$ f+ |' X4 N; F在需要启用Modsecurity的主机的location下面加入下面两行即可:& @$ y5 k6 f& ]) ], w8 l" s
9 J; h* h% B" |, `' ?+ ]
. T7 c% h# s+ S& v- U0 k- BModSecurityEnabled on;( N9 m7 _' \) g
ModSecurityConfig modsec_includes.conf;5 i; m+ d5 N. A% X: i
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
* ?9 `" j; s" I4 t8 Q
3 G- V+ [ f" i4 t8 J+ o* y1 c8 c; X! u& {
$ vim /usr/local/nginx/conf/nginx.conf, {' @' L6 w) @$ h& d) n' d, S
3 O+ o$ V$ {3 R
! w- `4 |- g) a/ K( D/ hserver {
, c1 }: q) F; `- U5 q1 a1 d listen 80;7 G; h" W( d* x# i% p7 p
server_name example.com;
$ g% m, _! C, \1 Q+ H! d( P9 W# L) F7 o6 g7 I( z9 L3 A
# u( e1 f3 S7 Z J location / {, ~. N& Z) X! P
ModSecurityEnabled on;
- T1 D1 ]+ W& K; a+ d- [0 N ModSecurityConfig modsec_includes.conf;
e& x, ^3 n. U7 l7 o* ^! {# k root html;
$ B1 Y2 p6 l+ \' \9 Y0 N index index.html index.htm;' i( r7 I- E# Y, l# b! S7 v
}
, G* s$ S0 H }8 \+ |, z) }; E}% i. E8 K2 i( G6 \6 X8 u
使用动态模块加载的配置方法) L4 ~ G8 U+ X ?" }" }& K
在需要启用Modsecurity的主机的location下面加入下面两行即可:
W9 o" d* c) D4 ^( m- @, O0 C( c. b4 A$ M2 ]! A; x
( V# J* k; z* i. `/ N2 ^6 z
modsecurity on;2 h+ E& `3 B2 I1 U
modsecurity_rules_file modsec_includes.conf;
# |1 |% D9 |/ k e修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。1 {) ^; ^+ T! ?+ Q& n- N8 ?( J
3 ^. ?- c* r0 j8 |. \' U
5 ^# b4 u$ i) K% d/ }$ vim /usr/local/nginx/conf/nginx.conf& _& O- a& h& R y9 l3 T/ @ G+ R$ M
. I/ U* K( {) p/ J. \1 S1 c: d B/ N0 {; ^
server {( j4 q& g5 }9 Y5 q
listen 80;. b# q+ H- P% @
server_name localhost mike.hi-linux.com;
8 j% L* D5 H4 e/ ^! t6 B access_log /var/log/nginx/yourdomain.log;' x* J4 r- b8 p
& n3 g( P! k- [. c
0 O0 _3 B- q+ v5 R) D8 j) s
location / {! x) r' c; U; }- j2 e# s2 P/ i
4 y, q" F/ f: _% g- E
# o. M) g# s( ?+ z: ~: A% D
modsecurity on;$ k7 [' R( w; y# W" B+ r7 s
modsecurity_rules_file modsec_includes.conf;) \; C" O) p0 {- [
root html;- x, o* T2 ?2 k9 Q1 h+ |' v
index index.html index.htm;
* o$ w5 J7 J' f3 B2 A/ Y! S& `}
6 G# o" Q: w5 L4 {}/ _; x* C4 G, ?% T. o
验证Nginx配置文件
# j& H6 R" |8 H U" L% Z. `. B$ /usr/local/nginx/sbin/nginx -t" X- m$ L9 _: i# Y& W: b+ t& n6 S2 x! \( v
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok# P& k( s3 c$ u6 m8 W
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful4 m% n+ O+ j' r8 g$ E0 O5 v
启动Nginx9 J, g6 l1 b; G# ?
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
% s4 K7 M6 p- F9 G- P4 J* Q
5 _5 V* W! W2 x1 k+ v, K测试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能够战胜更多复杂/未知/混淆的攻击模式。
. e- h, q' ?) H" G |