找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10839|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。/ H( s. F6 `7 i6 g( j0 l

& u0 P# J3 j4 T3 T
& ^* p% Z7 R* P8 j! u" a在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
# ?7 v# F- X! \. W
+ B. ^& m8 m: Y% q
9 ?$ b# O, }- Z6 J, x+ x什么是ModSecurity" b& Q8 v1 |( ^* E9 M, ?+ n8 V
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。; Q, ]$ W7 V& F, R

- ^  i# }# R, D0 z
) d$ \( v0 [% C  f2 WModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。, I3 F' U1 {1 ?( V6 @3 g' R

, X1 z: q0 o( w0 M# Z# c, M* E  s/ w+ l
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。  G# u( z* K! y" Q& `! U

. h6 b" L. b4 i) V! G; a  M( a$ `4 v5 Y  |$ n' N8 T
官网: https://www.modsecurity.org/
# z! f4 P% m( j3 h. k! o0 c+ S! g. w! t1 g! O, M% e6 w* Q: T

6 }- u( t0 t  ?+ m! t7 v$ g什么是OWASP CRS
, X4 G# @2 Q" o* o* |1 EOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
; j* {0 @, d( i& v
% T* j: V& }6 t% }+ ]. M4 N8 X$ w% e1 k1 C$ O
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
3 R9 p2 {' `2 q6 B+ R, U
7 s7 V; U  Q8 g3 v3 J, O% {4 D% `& R9 a) D7 Y0 {/ ~
HTTP Protection(HTTP防御)
8 D2 T8 M, B+ K, x: \HTTP协议和本地定义使用的detectsviolations策略。
$ P5 Z* T+ P# I8 A+ B! D+ c& |' _4 g. U* E
7 u+ z, Y0 X6 f, w7 O' o
Real-time Blacklist Lookups(实时黑名单查询)
% Y7 b, ?  ~/ P: M利用第三方IP名单。
$ H) b4 F, `# M$ v5 g6 _, i: e0 X' A; c' x2 h; y
+ H% _! M/ g+ M+ _7 v
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
2 g( T5 a5 T" x. [1 f; m防御HTTP的洪水攻击和HTTP Dos攻击。
% ?- @* s/ v' t, ?8 o7 h% f2 y2 U- C: R& N9 N7 S5 J2 C9 u% C

0 ^6 m! y" h- a$ p! c6 \Common Web Attacks Protection(常见的Web攻击防护). F/ U% R* t. j: k: J9 ^5 U
检测常见的Web应用程序的安全攻击。
5 M6 E  f/ e8 G6 \" X  [" O. W9 \, U+ Z: S6 f. R

3 w/ p/ w/ G( H) sAutomation Detection(自动化检测)* l, {3 O& z0 U* J" ^) A) ^
检测机器人,爬虫,扫描仪和其他表面恶意活动。
* p. V6 c2 Q! p( r
1 j# T+ K. y& c( k8 t( @
9 B* a7 O1 c+ j5 ~Integration with AV Scanning for File Uploads(文件上传防病毒扫描)+ N8 \. ^5 l7 d3 C1 m  t) G
检测通过Web应用程序上传的恶意文件。; L0 F. |: H3 M, M0 h
+ z5 \: j# e0 [& @# i. k% S

( r- I% H' c' C' j$ [& ~0 f9 y- ~, xTracking Sensitive Data(跟踪敏感数据)
9 ~1 ?9 @( {$ i8 F. ~信用卡通道的使用,并阻止泄漏。
3 I. L; d* W% b8 z
& {, S. i+ `" z( L: Q  \
3 b( |' H" W& @9 o0 sTrojan Protection(木马防护)% J. I) u3 H8 ~# m0 J# w
检测访问木马。' F, r) N4 j5 P7 `" x6 ^( H6 d
% i: N6 v; v* Y
5 f* y9 H' u) m$ M9 ?
Identification of Application Defects(应用程序缺陷的鉴定)8 M( h1 `+ r0 c4 b2 f: l
检测应用程序的错误配置警报。2 I: r7 ^2 G, M) @# o
' v/ ^! [, m# M# }
9 v! P* f0 j) F( u: ~' h% ^9 M
Error Detection and Hiding(错误检测和隐藏)
. T- `, c1 l4 T检测伪装服务器发送错误消息。. v7 v/ E3 O. C9 y+ G! ?
7 o8 E$ \4 h- v$ \% o* e; B, D

- v; d8 S4 u, J* O2 k安装ModSecurity
& k9 O: m& N- N& F. h软件基础环境准备+ c0 L- }, W, u4 R5 a& q9 H! x$ j- A
下载对应软件包: t; ~: _! f& j# t+ F
$ cd /root
, \  Z) R$ y7 j9 H% [! f$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'% T3 e4 ~% A- Z% ^  d
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz* f5 m/ |: n- @  }0 h! c" Q2 a
安装Nginx和ModSecurity依赖包
( X) z7 y9 L7 u- S* N' XCentos/RHEL2 o' H/ D0 \3 |8 G

1 k7 f7 S  K9 `+ b/ }! v
5 Q6 A' o. K6 l. l& R0 K. n$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
& a4 l) P" C+ z! ]& jUbuntu/Debian/ e/ i4 @# f3 U: b

+ }$ o) `) I* t' G/ Q- F0 T. _5 Y0 l( j' d- E5 p# ^
$ 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
* G# R& [: o# @) f编译安装ModSecurity
1 N" M9 D' D; P6 z3 j# {" @Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。- w. V6 v7 B4 _; L5 i
' {+ A) \) C& E- p: e- ^( U, U

& W5 }% S1 K) a$ q方法一:编译为Nginx静态模块
: f, x8 P. i7 h. C4 \2 J- l
9 y$ G- r# v+ Q: e/ W& g
9 A. H# L! F' `; v编译为独立模块(modsecurity-2.9.1)/ P: b9 `5 O9 j
$ tar xzvf modsecurity-2.9.1.tar.gz
1 T3 ?. |# ?9 J' _* m1 K$ cd modsecurity-2.9.1/
* R( F1 ?3 o; }) t6 T) I$ ./autogen.sh' n- f7 R7 D8 Z- _4 Q
$ ./configure --enable-standalone-module --disable-mlogc
& Y- L- ]. l- y' O1 k8 a$ make# p2 ^, }. Y7 B. w* e2 Y
编译安装Nginx并添加ModSecurity模块) ?; t0 E/ K4 |$ T! t2 O- S
$ tar xzvf nginx-1.9.2.tar.gz1 g2 t4 T9 h. ]8 N3 Y. T6 m
$ cd nginx-1.9.2
1 Y' Y" ]  ~. K8 @1 H$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
2 w" E2 m9 b, M; Q$ ?$ make && make install/ E  @* {0 X: P; l( R! p
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
: h4 y7 b: M& A; t+ Q' m' G" v! e( V4 h2 w" U  v( N

4 I% P! R1 s& f, }编译LibModSecurity(modsecurity-3.0)
$ c6 k( a/ t) s8 @$ B% G$ cd /root. Q8 t' V+ Y/ X$ o7 o& ^5 R" u( [
$ git clone https://github.com/SpiderLabs/ModSecurity5 @/ ^" Q. X" `! [6 u. p. q
$ cd ModSecurity
& N4 U! Y# y8 G( |$ git checkout -b v3/master origin/v3/master
# \( L6 U% R) ]. Z3 i- g$ sh build.sh
  O- k* r: C8 l) _: g7 l$ git submodule init5 P0 n: S- F7 L4 x0 e1 D0 O6 Z- F
$ git submodule update, z9 q' ^5 R& u4 z% ?
$ ./configure
/ q* W/ A! \" `+ w$ ?$ make9 ^! @; i- r$ R* c: L( C
$ make install  `$ ?3 g) q' ]6 d  d
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
* j: w' p/ n( L2 C& Y( D
2 k9 N/ h" D) i* B) J. j+ z' a
: e0 q; Z5 Y) p$ ls /usr/local/modsecurity/lib3 s3 j" v, @* X! O0 w: a5 N; `+ @4 O
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
  w% N, D% Z0 G/ K3 q+ X编译安装Nginx并添加ModSecurity-Nginx Connector模块# V& X/ }0 A/ E
使用ModSecurity-Nginx模块来连接LibModSecurity* g0 q  ]) K) \6 L3 v

! F% B5 U3 X4 _+ R
1 |; \7 S+ i; ^! e: }9 C$ cd /root
1 D3 L; `, V& D9 L1 \  D$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx$ q0 W) F) ^2 w
$ tar xzvf nginx-1.9.2.tar.gz% a+ d& v% b7 m. j5 N; K
$ cd nginx-1.9.2& x7 Y: R2 I  Q
$ ./configure --add-module=/root/modsecurity-nginx
, m/ S. h2 r0 s* K. S$ make
( d: ~: B9 y" \4 A7 o8 L: X/ r+ p$ make && make install/ u  h* r9 d8 \4 t
添加OWASP规则1 \+ P: W1 D+ g
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。' I/ c: Z- Z& H/ F' C

! _' U) t8 F8 q  @; `. ], _+ }( q3 o+ y
下载OWASP规则并生成配置文件% P0 k' r4 k, t1 c) ^5 I/ a
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
9 n' Y. ?+ P4 D+ t" ?1 o. y7 \$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/! |4 G$ ]! r4 V4 _! v  {
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs6 z/ G# H( D7 @- y2 @9 ?5 X5 ^# w. Z1 n
$ cp crs-setup.conf.example  crs-setup.conf7 u, K% f2 v& G3 G2 O
配置OWASP规则
9 x; l: O0 Z* Z0 F4 y编辑crs-setup.conf文件
" k5 [! {0 B& E  Q
  u" t8 {6 d% P  x9 D# ]: g1 c
; {! P- |' d) j$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
7 ~$ `7 @8 ]3 O5 n- a: K, L, I" K" s$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
; `0 K" l: ~+ e: Z3 ^/ q$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf+ E! J: H( f+ e4 @* |# ]% I7 S
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf! z4 g' S1 d/ \; N& f9 s: `
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。3 {4 ^1 o6 G+ a) s; X' |- x/ [1 s
8 `+ S" K+ a* O3 {$ r% t/ k

& g4 p; W+ t$ Y$ b启用ModSecurity模块和CRS规则
) D5 S4 V2 X4 L: ]( b$ ]复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
- P1 r2 b! e0 D7 ?
5 L9 E) V/ L9 D5 d
! R/ H* r3 S! G* g8 Y& c! Q- d- vmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。& B* ]: E( x0 g, T& u

# Y* ?) }. [3 X; C/ K+ N1 x* {* H; L; L1 w
$ cd /root/modsecurity-2.9.1/
: b+ q! _9 e* v5 m- y' F4 \$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
( [0 ~' x. l' W3 A4 o$ cp unicode.mapping  /usr/local/nginx/conf/" i; B8 I  N6 v. u) w
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。1 j2 t/ l" R6 l2 B! d
+ }+ @7 @/ c1 f8 _2 E* K1 Q
8 ~2 G  i1 s! Q! F9 r- G
$ vim /usr/local/nginx/conf/modsecurity.conf5 p: B* H1 [4 J* q5 l
SecRuleEngine On
0 I$ d* z9 M7 ^* n2 Z$ k# oModSecurity中几个常用配置说明:
: ^! M0 x4 v( ]6 i0 N1 j
& U. V* {; U4 c+ g& G" R# O+ Z$ d' k, n5 A
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。" \+ C" O" ^. T2 O6 t

. Z1 D6 H& |9 E4 E3 B; _6 p9 X+ @4 W# [) ~6 u: A4 {( `1 c
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
8 n) W( Q1 I5 b' |
1 r9 _% Q5 q' C) K: u# e" w7 I# v2 d- x6 |' W( ]  r
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。9 h. Q+ [1 x: E$ ~

. u/ H+ X& r; P: v) H* d" g, m: g  A8 Z
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。' r4 u7 x7 i/ C( a( U+ U
3 m6 ~. ?7 I8 K4 \

$ y) i4 L/ `( o5 m/ K+ n7 ]在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
- I& o4 [% v2 n: e0 g) q
3 C5 |0 h% t4 g' n8 Q* d0 ]* h4 {! i5 D
3.x版本CRS' O" o7 B( R& q$ A4 w: o" v
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs' @  |8 q" n% w& K% p; h& G
# 生成例外排除请求的配置文件6 Q  }7 @/ y: Q
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf$ I- Q' K/ {5 x  q
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf- R# g0 d: e4 M: {$ _9 T1 q6 W+ o
$ cp rules/*.data /usr/local/nginx/conf3 S- _6 G3 S: f$ G) o
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
: _/ i1 ^3 Z$ J+ C
+ U6 Q& t- [/ |: v, f' p- D& M' _2 y- n! z
$ vim /usr/local/nginx/conf/modsec_includes.conf
8 r+ j2 o3 S6 n/ c* I5 U8 P
  Y- f2 Q8 I. ]! J: E( P0 ?* h& l! I
[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! p9 E: `8 S5 q  ]4 x+ A
/ C3 L4 Y0 b, ~# E. v. q
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
  D) y5 x8 M4 m1 X5 q9 g7 ?: x7 P4 _/ q' R  w. b

  ]; g1 p* r% r0 I, }/ A# }; K配置Nginx支持Modsecurity
3 q9 T2 v  H6 @. }0 |7 h启用Modsecurity) j. a$ g. E3 ]* x# F
使用静态模块加载的配置方法
& O/ {# I  _' P; A在需要启用Modsecurity的主机的location下面加入下面两行即可:
: F- c( ^4 P3 g+ E  C" s, |5 S
0 v5 s' y3 t2 |' a
) p) ^  T0 k- G# ^0 X# s- u8 DModSecurityEnabled on;. r$ \4 y3 |: E& G5 R5 T1 s7 Q
ModSecurityConfig modsec_includes.conf;! [3 H$ T4 ?6 Q2 [3 P, c
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
" b$ ]! }4 n& d& ]  b, `
1 B5 {) `9 m  V8 x# S
1 c: i# Y& m" T2 Q5 B5 e( i3 c1 v$ vim /usr/local/nginx/conf/nginx.conf: G* d" g/ r" t" C- L( ?' V6 A8 P1 @

2 \6 j  U4 S6 Z3 ~  \$ f: ?7 n1 C% }- N0 d4 a! v
server {& K! a# K- h# A) f) U# z# C" @
  listen       80;$ Z- u# ]& _: i
  server_name  example.com;* r4 P0 E. T$ W8 g

  [/ x" z6 e; S- E/ g. \5 R+ ?* W
  location / {
/ Y/ o( X. c- N# u. b$ t* c, L    ModSecurityEnabled on;6 t! S/ J: ?2 j$ I  e. e' c
    ModSecurityConfig modsec_includes.conf;
0 i1 l: u! x: Y1 ~    root   html;( f+ \; b7 ^( U0 p( S' f0 Z$ h
    index  index.html index.htm;6 x1 [) C4 O+ K, m# f
  }* s8 ^1 o5 x* w4 o) i; ?3 G1 ~
}
& Q. Q4 w: q/ ?% g) u使用动态模块加载的配置方法4 H& F! [) H2 f2 k0 G
在需要启用Modsecurity的主机的location下面加入下面两行即可:
% e4 l* f; I" j6 ]# E
' \0 T6 [! M1 v% O, {6 v
& |1 p- |9 l3 {. X$ ~; ]. Ymodsecurity on;
$ o8 t- A: Z) h+ Umodsecurity_rules_file modsec_includes.conf;
: L* H' [* f# l& u- T, X2 H修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。$ M7 o- F+ C9 i* k1 y  Q) ?

/ n' M9 [  t9 U5 E9 V& N9 _8 ^: ]+ p4 U" n% T/ D
$ vim /usr/local/nginx/conf/nginx.conf  D- r% E9 k6 _. D
+ j, G+ N0 m5 o: ]4 t+ W# @
- S' L& k8 g, R0 n" Q
server {: [; q, @2 Z7 q3 ?+ W$ L, p
  listen  80;
5 _2 @% i3 D+ G7 N/ P7 o! o: h2 e  server_name localhost mike.hi-linux.com;
6 D" q$ K" J6 B  access_log /var/log/nginx/yourdomain.log;3 m. f. C: Y% r& X, ?# x

1 i1 h5 e) E+ \; J, O# |
* U$ Z" M1 k' A% l: l( X' }1 X) ?  location / {
. F9 G9 {% L7 |' }6 G) u" R( e6 u" N1 S7 o6 W/ F

. H4 R4 ?, k6 b0 Q1 \- I! D  modsecurity on;; c( e$ M2 {* R: O3 N" k& c7 F2 m. V$ z
  modsecurity_rules_file modsec_includes.conf;
) C) @  b8 h  {  root   html;" g, n% Q" I6 `! q' K
  index  index.html index.htm;
) W/ W5 \% V2 h. o% k- X}( i( N/ d, U2 |
}9 A: k5 x/ b3 C0 A- D4 A
验证Nginx配置文件
: F% R8 g( _; d% K3 c# s$ /usr/local/nginx/sbin/nginx -t
5 v$ c+ @3 x$ ~7 }nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok* Q+ w- l2 A7 n9 ~3 M; D% ~, R
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
% N  Y7 w( P* G& y: g: e# E2 t启动Nginx
# d# z5 Y# E, o& E( H( |# n$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
* }( C& ?& D0 K
: {6 z9 S7 Y1 [5 d* ?

测试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能够战胜更多复杂/未知/混淆的攻击模式。


9 K$ p- P) g0 K2 Y" A

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-4-14 15:56 , Processed in 0.051614 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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