找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10806|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。3 R, L0 t5 l+ q5 X3 S, J0 s% k3 V: k9 }
0 F4 D% U1 \% F- k

! p0 `" q8 W. g+ [1 p在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。* l8 G) Z6 ^* h2 h
8 w; l5 |# Q+ A9 |# S
" k2 w2 y# D# Y9 W
什么是ModSecurity2 i0 H# z% t% Z  K
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
6 k3 u3 M  o/ N5 s& [$ d9 h) D: K7 W4 W) Y- p. O

) R8 W3 w8 L9 |; |$ KModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
3 _* u8 c, S/ n: L. R, A$ a. R" a4 m; ?. J" A

& V1 ~1 Z- Z- |  @  iModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
; W) Q; ?/ v6 a% i; L. h0 L3 L
! S4 |! F% O6 X2 b, H* \9 c7 c9 v# H9 [# G/ C( z
官网: https://www.modsecurity.org/
4 V0 s1 F' V5 `' A" g3 i
. E* M* ~: a' q9 j7 X0 u" \1 Y
0 m( F: f1 W; _  X& M6 c什么是OWASP CRS& _$ ~) ?) y$ K/ E* X  d
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
7 P0 c5 Q6 `4 t, k0 }' S$ s' ~. x8 k" e# q
. c' \6 i0 t: `2 Z  q" b
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。! V& S6 U! [6 _( Z/ y" y* y
4 G2 c) l7 X* n9 s7 I7 [
3 Y3 C8 M; E( }" }+ ?! U2 W* b( E* L
HTTP Protection(HTTP防御)
7 O1 @8 c. O7 n8 `/ R' \/ r# h/ ?7 L, ^HTTP协议和本地定义使用的detectsviolations策略。; E/ V5 _& g: N0 K
! ]3 R# _  Y6 _/ W
6 a5 o) ]( \' I1 J+ c+ {! e; z
Real-time Blacklist Lookups(实时黑名单查询)5 a2 Z3 N9 d5 z2 i. u" `/ r& C7 u
利用第三方IP名单。% _4 `2 T# J7 [2 U# R
9 p- W8 S- Z( B
; \+ k$ }: q$ P- @
HTTP Denial of Service Protections(HTTP的拒绝服务保护): B: Y( z% ?9 d7 ?6 F( A# a
防御HTTP的洪水攻击和HTTP Dos攻击。; `2 V: p& _: y/ ~3 X1 \" d( c7 k

4 U3 [: @2 m) ^! m' f, F$ p) d( a$ }. V( i/ B; P# J, x
Common Web Attacks Protection(常见的Web攻击防护)
% l; \7 l3 p) A* s检测常见的Web应用程序的安全攻击。2 n/ t7 U* c# H- B6 }4 ]; B: |- k
% x+ W+ @5 i0 M; f4 _6 P1 t, P- n

$ H0 P4 ?6 H/ g+ |  t  NAutomation Detection(自动化检测)% p% s! \/ y: w7 p) t3 M
检测机器人,爬虫,扫描仪和其他表面恶意活动。2 m. G9 h6 K/ K4 A/ J

( t  t7 ?% K( _! h) Q+ l9 [) Y! ~1 m! T* \# n! a
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
" T: U% u/ _1 o* x5 K, M8 ?检测通过Web应用程序上传的恶意文件。
; Q2 N4 a. ~. [: @
3 Y  O' c6 G/ P% {  i* M
- Z/ b, \5 R9 W) P/ x' CTracking Sensitive Data(跟踪敏感数据)0 c) R% |4 s! E" v( H% o
信用卡通道的使用,并阻止泄漏。
0 I& p' i* h6 U5 }6 A3 Q9 z; l, S! x' z* B. s2 V+ b6 k  @

, I  u+ f/ B8 N% FTrojan Protection(木马防护)  ~9 E# O) X* C' J5 `! ]
检测访问木马。: F! c# ]) a5 F

2 ?7 Z4 i" j' o# H" x
5 \$ \+ ^! R4 j7 y9 lIdentification of Application Defects(应用程序缺陷的鉴定)2 B) K6 q' U+ b# E) M3 ]/ A
检测应用程序的错误配置警报。) M6 d5 n. M8 _
9 O$ G! K0 D6 A7 g

9 r3 C3 }' U# q8 w  e" P0 y1 v& O. {Error Detection and Hiding(错误检测和隐藏)7 F1 _$ e; H# L2 n8 y5 t( H
检测伪装服务器发送错误消息。9 X/ T: C2 F$ R! O' l% ]

7 `2 x$ b- c- ]& S/ |5 ]- J) h
+ n8 U. T! a" ~5 O2 ~! I1 l# r安装ModSecurity
6 N3 s) U& f  D% V  u- x  F" e8 O2 E& M软件基础环境准备7 Z8 G7 l. N7 I# D9 j% l& C9 U8 n
下载对应软件包" a  L7 m6 m" H+ _
$ cd /root
- u; n4 {$ j! S6 o9 L% t- Z$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
! Y7 z- e% _$ E1 ^+ Q6 q$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
6 u. c" g! N/ _/ L安装Nginx和ModSecurity依赖包
) k( ]$ x( @8 ]; N# n' p. H( DCentos/RHEL
2 ^/ X% J9 C, b2 J! {
# O% q5 T4 Q. t9 f5 ]- N7 M! }3 C: k- q% ?
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
; u1 q4 |% V5 C; \' T5 e3 A# q- KUbuntu/Debian- S# I: t. [3 s) Q" H. H: j
9 b7 P; q( Y9 I9 ?2 U$ R! A4 G

9 h7 o. V+ J$ {- U* l, a$ 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
& L* Z% D) P! }0 Q% f! P9 f编译安装ModSecurity3 w/ z. D& c  I# m/ K" A0 \4 z
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
  H7 T2 S6 k" X1 |; U5 O" l1 b) t4 A0 ]+ z$ i5 c# X

# D) Z. y$ {0 |2 w5 I7 V5 |& \方法一:编译为Nginx静态模块
) L1 N7 t* C9 n) V9 q" I" v; H5 g% i9 _9 p' [
0 U. c- Z$ y5 S( ^7 ^7 }8 z4 b
编译为独立模块(modsecurity-2.9.1)
) c- i* l8 t0 D$ tar xzvf modsecurity-2.9.1.tar.gz# J/ _; P  j3 Y# b( L" d. m
$ cd modsecurity-2.9.1/
8 t+ q. t. B# X. M$ ./autogen.sh
! A4 _3 F7 T. v" `8 A$ ./configure --enable-standalone-module --disable-mlogc7 w# }7 D7 D) W; ?4 i) v
$ make' x2 N  H. G0 D3 j' g6 {
编译安装Nginx并添加ModSecurity模块# ?: H9 U. c' y/ l2 _
$ tar xzvf nginx-1.9.2.tar.gz+ r, R$ Q/ `5 U/ Z0 _! O- v* R4 |
$ cd nginx-1.9.2
1 O0 o" R, V% ?9 p  C, E$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
# a% D. c+ C9 ^, u9 a2 ~7 d5 |$ make && make install
  |( u# m4 D  R9 b7 n方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
/ a4 H; C4 r: \, O* H
7 j/ Q% K/ y( p. V, S/ \
, {0 y9 Y7 z8 C: M& R( P8 ~编译LibModSecurity(modsecurity-3.0)- b( |. P- G  E9 J1 @  a
$ cd /root9 q4 p  T8 ]- A6 w
$ git clone https://github.com/SpiderLabs/ModSecurity
" ?+ n% t7 y8 D$ cd ModSecurity
1 g; D0 A' {6 O; C: R9 n3 B$ git checkout -b v3/master origin/v3/master
3 d. J# D% c6 V$ sh build.sh" P% @- \3 M9 j1 T4 o, l! p
$ git submodule init
' o1 U. ?. {% Q/ s" S8 z$ git submodule update/ `: \0 P5 q$ N/ `, Z! |( K  P* R* g
$ ./configure
$ H/ ~) g0 `& o- K, J- B$ make/ ?$ N4 t+ {  b
$ make install2 R6 ?, }  D  V" y/ D4 P0 A. Q+ M. j
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
  D6 R3 Z7 t/ r# A1 }1 S  A% G
4 l8 l2 |! R5 A  b+ }" L5 A2 d8 ^% [1 B; I" t# b* U2 ~: u7 z
$ ls /usr/local/modsecurity/lib
' a1 d9 }. d8 Y( i( _) Wlibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.09 Z! l6 C# ~$ o/ {0 W
编译安装Nginx并添加ModSecurity-Nginx Connector模块( h+ o/ W* @- W) D9 j1 \* R* u
使用ModSecurity-Nginx模块来连接LibModSecurity
3 ^) [3 {+ l5 g3 a
9 n; P$ r: ~; \5 ?3 v3 `; B% l. l8 T% Y7 r; i$ d
$ cd /root
" t$ x' J1 {  a& m9 c$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx. s, d0 Y- S$ o# V0 R$ ?
$ tar xzvf nginx-1.9.2.tar.gz1 o. G( \; \& L$ x+ o8 |, L
$ cd nginx-1.9.22 n9 X. J% e  p1 R$ e! |; n
$ ./configure --add-module=/root/modsecurity-nginx
4 o: T: V, a3 `* o1 E* J3 B2 o, p$ make0 g% U8 B2 v# z6 T! x0 R9 p
$ make && make install
# {) r2 u  z1 I1 x# ]8 X5 `" X3 N( c添加OWASP规则5 W' k) z+ i! d( Y; ^. w0 I
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
: S+ y) i- D7 P" L8 G  q5 U, K; I5 P

) j+ t. s( Q5 I8 |2 m' x下载OWASP规则并生成配置文件
4 b9 o( U) o( _) W$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git3 n+ J1 z4 u5 v! v6 p
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/2 q6 R0 D# U4 t7 M% J
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
+ {/ M! Q! H( b* V- @. Y) n( I$ l$ cp crs-setup.conf.example  crs-setup.conf
  t9 v4 F& S$ u6 k& n/ e配置OWASP规则# K+ H- A) e5 c, r( q( u4 E/ s8 I; Q
编辑crs-setup.conf文件
/ M" F7 o% n# ~+ K4 K! j' j; I) T! ?3 }% z; `6 [
8 F0 O9 n2 z  t) O
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf* g+ @# {2 p: k3 h
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf/ O" o7 n6 p0 E- k
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
4 L# j& o" q# q  R$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
& Y" j$ f& I: Q; M- X默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
( T; ^' }" }% w2 \8 e
* m+ P3 u) }$ G' P8 M- {/ a7 L# g4 M/ I- `9 n. `; v5 N
启用ModSecurity模块和CRS规则( H9 }  g; c$ O
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。/ t- Y- w) Z4 T

4 \# A9 h/ w  C/ l
, G& b" V  [9 i+ Rmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
. P3 H2 H# E9 E8 g# z5 a0 C
+ q0 f% L7 w8 K6 v0 v2 R
+ o' S8 S+ ~9 k' f; n" i$ cd /root/modsecurity-2.9.1/: @% L" L* F3 Z' _! M8 r
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  % @6 H' H8 Z: W3 W0 Z) p, {* x( r
$ cp unicode.mapping  /usr/local/nginx/conf/
7 U$ M# r* f# ~$ c将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。7 W; \9 B& i* T: o( A8 r* `) v
8 a3 E( B9 v( O& O, |: ~0 i
9 V8 G8 e) y* c
$ vim /usr/local/nginx/conf/modsecurity.conf
5 @) k9 x! C- J$ pSecRuleEngine On
3 @/ W4 d- R2 w6 d8 r/ r1 C; G1 pModSecurity中几个常用配置说明:: |3 E% A* R: k

' V0 q. O% O0 y1 L" L8 _/ d, t7 D0 @
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。1 C' t. J! `2 y8 \' Z( R; |* W

: X8 u  S, q/ `" o! _- u. }9 o+ w7 e' h) y/ N
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。% |; v) |: k3 \  ^, L

; U- E9 @* O  a3 c& E! I2 Z1 ]" H$ k/ d4 Y, g6 i
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
. @  _3 _- ]0 B) E
6 P! U0 K: F- T
1 V7 N4 ~: J( b3 b4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。1 @: N' Q2 y% a% c
8 x$ x, [+ C4 L1 H; l1 L8 C

) Y' b+ S& u* Y% W! F在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。$ W  M, U0 i' z( c
- C) b) A+ D6 |- \0 A" c& C( R4 T
; v- n( r; B, Y8 \. U$ @, h
3.x版本CRS& b& R7 [6 ^6 b+ ^1 m
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
/ ?. Q+ X  m) t0 z+ V# 生成例外排除请求的配置文件
6 t$ V' {. ?8 `. m$ c0 p! ~$ X$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
0 M5 H8 ^4 E, i3 V5 [0 L' X$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf* a3 g$ l6 L+ z' w$ u
$ cp rules/*.data /usr/local/nginx/conf
/ v9 M' X% X* _+ H, s为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
5 U8 q, G( \8 I( O! d! N4 d+ O
! X" |/ f( O4 Z2 s) b7 I
1 t3 y3 S8 H5 d! h6 ]$ vim /usr/local/nginx/conf/modsec_includes.conf
$ y7 T8 H4 ^$ R
" L# x4 P1 m+ F' V% j  s% c% \
[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

$ g" s3 _  S% y  l
6 W# f& c" @/ W注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
4 c5 D; ?: U3 \. s. a+ ^  e  M
8 X2 v9 ]  M& O. a' R
9 N: N) W/ j' S& p. w% |; @配置Nginx支持Modsecurity
$ e8 G' }: @* p# H4 o. |启用Modsecurity
# g6 d: K3 @+ e) H( d# y# I使用静态模块加载的配置方法
) j; \8 R% a$ I3 n在需要启用Modsecurity的主机的location下面加入下面两行即可:/ v8 P4 l+ K% f/ }) o$ e
+ ?1 G  T8 m5 @( w; M5 M% T
  B& P8 y2 x( B9 _
ModSecurityEnabled on;
  n8 J8 ?' V5 ?, f/ }ModSecurityConfig modsec_includes.conf;
! S7 l& J$ B/ m6 ^修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。3 v) B$ `, k& A% ]

; {! O! R& u5 s8 C+ l6 u+ Z7 w& `: c% W2 J9 h& Y
$ vim /usr/local/nginx/conf/nginx.conf
$ q8 K9 P0 M  Z; g9 s! E, Z4 t" U2 x2 e+ Y) T
- V) ~# @/ ~2 Y. }6 [# r# S; I
server {. H2 @/ ^* f7 Q: _, f
  listen       80;: [1 C3 e, K+ S# }
  server_name  example.com;
& o7 q/ H+ I0 h3 d+ x. s9 v
" }6 c2 a; A7 Y; g( a' V; {- U! N, |( _
  location / {
' Y1 D( y- T! G5 Z; ^, c- X+ r    ModSecurityEnabled on;7 f6 \8 W3 b. p
    ModSecurityConfig modsec_includes.conf;) m1 n" [* _8 B3 j7 n( x
    root   html;( ~1 A/ r0 r% o) a
    index  index.html index.htm;
. C6 ]8 C& p! Z/ a8 v! t! v* P  }2 F! V, g+ d, x% u& H2 s: h
}3 `/ W, f: L" M7 u. ?2 w) H/ p+ w
使用动态模块加载的配置方法
9 w- A$ c1 B+ T7 J% z# X在需要启用Modsecurity的主机的location下面加入下面两行即可:
9 Q5 Y: B) A( r. @8 X9 ^+ ]# }. o- V6 U4 _) z7 w  T9 E3 w5 F: \1 h8 T. e
  B8 Q% F7 Q  [0 a, W8 P
modsecurity on;
5 q9 D$ H/ j$ Lmodsecurity_rules_file modsec_includes.conf;  a, C; f0 n8 r( ^5 \6 S5 H+ }( s
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
& u  ]6 p5 @/ n" D, i( a3 J- l7 O: U% w

0 l- t( U5 c( j" }! `, m: Z$ vim /usr/local/nginx/conf/nginx.conf1 T/ U: C, O2 W5 J5 n
; G& ?- u: K& N2 c+ Q

% d) R3 I/ D: _  V+ N0 N+ b+ Pserver {
) m! O+ \6 A, j3 u# @& j2 p+ l  listen  80;
* d& Y* |: S% n8 r  server_name localhost mike.hi-linux.com;
. w* y2 r/ }% w# {- N  access_log /var/log/nginx/yourdomain.log;
5 ~. m* t4 e- Q$ h6 N" F  i% M
9 _' J  y- g! X( T& }# W
0 v+ B4 i1 z/ j( d7 G( x) B  location / {4 I% G3 J# L  t/ J5 L
2 [. U- ]) Z) L- J. }5 B* W
5 w+ Z. L8 W7 f: `9 d4 L5 D! P) ?
  modsecurity on;! l$ F  f5 @& U& n, a
  modsecurity_rules_file modsec_includes.conf;7 u6 @, y& `) K- L# F
  root   html;# n  t- {& E" C0 {
  index  index.html index.htm;
: I3 }1 A5 w$ Q, F- @) q}" r# I/ o6 [! b! O
}- D4 ~' ]  M- r- h- P, J! y* |
验证Nginx配置文件6 y1 i2 [# l( ]' }& M4 Z" }3 M
$ /usr/local/nginx/sbin/nginx -t) W2 U6 J6 \1 S. e1 R/ w; q
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok1 @7 V( G+ C0 {
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
2 b# m6 E6 \+ S+ e启动Nginx
: w2 |% H" B8 d. r5 P/ ?; V$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf2 J6 X1 W7 @. o
  q- v4 j2 y, Q- n( Q

测试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.log

Modsecurity主要是规则验证(验证已知漏洞),Nginx下还有另一个功能强大的WAF模块Naxsi。Naxsi最大特点是可以设置学习模式,抓取您的网站产生必要的白名单,以避免误报!Naxsi不依赖于预先定义的签名,Naxsi能够战胜更多复杂/未知/混淆的攻击模式。

& I7 H- V9 Z- n1 E1 L

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|第一站论坛 ( 蜀ICP备06004864号-6 )

GMT+8, 2026-4-11 21:43 , Processed in 0.080214 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表