找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11178|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
) {- h4 w+ X' [/ X2 y& K
% n: S% g% E& s- O. @
' R6 I3 |9 N6 v1 h) M) K在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。2 {# |' H2 ^% I/ h/ ]( H
; m3 e2 B2 l/ ~# i; M# ]

( v, I4 K$ N1 ~5 Z5 c" i什么是ModSecurity
: \4 h1 y9 L- dModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
/ Y- o, q0 `3 ]* X, R5 j% e: D( @
0 I2 H! X, ], p* Z# s* `2 u% k7 K% G, D0 I4 }- _
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。6 f) i9 v5 n/ X

) Y  l+ h. \* q  I) v, z9 b1 Y% G- `9 r/ Z) Z  O
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。* a7 M0 D/ h# ?
. {/ |& c+ t, \1 H5 R$ A
5 D9 R# a/ r) \
官网: https://www.modsecurity.org/; ?) _* d  I! a* N7 y
# x- m2 |7 `2 {* ^, f( z
0 _! V5 L; O5 l0 M3 x  q
什么是OWASP CRS
* f  N# m8 a% Y. r0 A. B( X' ~OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。2 f! ^4 n$ ]' V3 c+ ~! `8 \

0 {8 ?% Z! l5 l( _8 ]* H! i! Q2 L! D" c% {
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
" H0 j; k- W* m( C# K, s% Q) R9 B
5 z/ f+ I, B$ Q1 _; W
! O# i. B2 |. w' BHTTP Protection(HTTP防御)
1 w8 A, B9 W( G  ~0 o: H2 gHTTP协议和本地定义使用的detectsviolations策略。
' f. Z4 l$ X% g4 H3 H9 h
4 T% W  f. {0 T2 n3 C* y+ s, C8 @4 p4 p
Real-time Blacklist Lookups(实时黑名单查询)
' x& d9 [( C1 g7 e5 K利用第三方IP名单。8 p& j$ _+ f/ b! E* n4 [( k. C3 @

5 m' @  i+ i" `9 w
1 C! S. E0 V" W/ A1 w& nHTTP Denial of Service Protections(HTTP的拒绝服务保护)9 k$ H+ G. P% b" v) J* q
防御HTTP的洪水攻击和HTTP Dos攻击。
  W4 g$ y  n! I& ]+ w- r* q, L' d* c, `% L
' o9 q$ w/ i) o# S
Common Web Attacks Protection(常见的Web攻击防护)5 A% ^! j- D& B3 b* U
检测常见的Web应用程序的安全攻击。- {- H$ S% y( g# v
' v" P7 E- Z% ?

0 `7 F- J! I- l& h1 @8 \- ~Automation Detection(自动化检测)
+ I  y! V% j0 b5 |! H1 U" w, ^9 }检测机器人,爬虫,扫描仪和其他表面恶意活动。/ v2 l9 ?6 Q. J6 L$ L9 i
* k1 R9 C' c! w
1 m# K! Z3 s9 r, m) x/ ]
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)/ J: E/ x* U( w+ u& }
检测通过Web应用程序上传的恶意文件。
9 V5 I1 }! O2 Z0 }" J; L2 K3 W/ k1 v* ^; _

% Y7 J) N6 j0 G; \; ], YTracking Sensitive Data(跟踪敏感数据)
, d# y) [. R0 F1 ^信用卡通道的使用,并阻止泄漏。; b# L( C- m$ P( W8 O5 J

7 c3 z3 D6 m6 x8 o4 o6 j; I, |& ]
% g5 O0 k. o" r# DTrojan Protection(木马防护)
( i6 `1 G7 M8 M: b! s4 z$ ?检测访问木马。9 f. y1 k9 T% b- c2 Q- B* @; }

( b% n4 o" y' }& L% J
( p  S) Z( u# E. x4 a3 `Identification of Application Defects(应用程序缺陷的鉴定)
' x* C( v; }, `  x0 r检测应用程序的错误配置警报。
0 \" k: j2 h) v+ z% b' m% {- s( y/ J; \7 |, l

& i, Y* F6 H$ R* OError Detection and Hiding(错误检测和隐藏)8 i1 D7 Z- [( G0 v3 L) J2 I
检测伪装服务器发送错误消息。+ v* I! d) C( r, R7 z0 o5 E

0 o' {; r) W5 V. Y( C4 J
9 f% g# l4 }  x安装ModSecurity
) b1 l3 b* o+ L- ]1 D; _  J; e软件基础环境准备2 s! Q' M4 H0 W! @8 |  @! M% [
下载对应软件包
; F. i  j! a" O$ cd /root7 Z, H/ j* f+ a3 @' Y
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'  m( A: h# @7 x3 E9 I* O6 j, 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
( c3 A  R/ |0 S安装Nginx和ModSecurity依赖包
: o( h* R5 ~4 W; _# fCentos/RHEL
! o7 [/ |2 B. f! M" O- K3 U7 p- L1 I3 y" [4 B/ E
2 p, D% J1 |6 p( O( [
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel8 t! v8 q7 q' z& T8 j- y
Ubuntu/Debian6 o- B6 l+ c1 J6 {- F  K$ C. X
% r$ P) {2 @6 B: M, ~$ x' S
; D% ^. V4 w  k  O6 `
$ 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% a4 u% ^* M! u
编译安装ModSecurity
: Q) ?, ^4 Y: Z, O0 E( rNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。4 Q3 |4 q, g+ O. s3 o* b. ~5 ~

$ W, e3 O% G! A  }. j$ G6 G5 {9 k4 l. T' P0 b! j0 \9 j/ l, Y2 B# B
方法一:编译为Nginx静态模块1 F- s" t# Y5 U( a# [  @. }: @1 w

" [2 G) C" y: Q) _7 c1 q& W7 y- M1 H' l4 W
编译为独立模块(modsecurity-2.9.1)
: j- B2 U7 v8 w) ]$ tar xzvf modsecurity-2.9.1.tar.gz
7 `$ [# X; |5 _! _7 e) E$ cd modsecurity-2.9.1/
% Y- o4 b# e3 h$ ~, l$ ./autogen.sh- w; E% U1 P6 t: ?4 r( M! G% X
$ ./configure --enable-standalone-module --disable-mlogc
; h* t* Q) T% J+ e# z$ make
9 K; G+ s- \; A+ X- S* X- S编译安装Nginx并添加ModSecurity模块
& x: x& p: ~" }7 `* R: Y$ tar xzvf nginx-1.9.2.tar.gz; w% @6 U6 s: A) P7 D( a
$ cd nginx-1.9.2# C& E+ }& G& i- v
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/1 P5 ]8 r: Q! ^( K
$ make && make install7 [# V% J& }6 X8 @, o( v. I: o, u
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
, w, p! s9 w7 _% i; y7 ^  m$ I! p/ ]% c* ?0 X
; u& s) H2 Y" ^/ `: E: o8 u: ~
编译LibModSecurity(modsecurity-3.0)8 w3 J9 G( c+ ^$ I* q
$ cd /root% c" \: e  L4 |
$ git clone https://github.com/SpiderLabs/ModSecurity
1 Z2 @2 S. T8 o$ cd ModSecurity+ e+ x% Q& \" B; V
$ git checkout -b v3/master origin/v3/master
  ~) r$ A6 T, N$ Z+ O+ G9 D: P2 ~9 z$ sh build.sh2 i) l/ c3 G* h. d& M) t* g
$ git submodule init$ O8 B, Z( K9 T9 Z
$ git submodule update' [- p( N0 Z9 ^; r* c" o: _' I0 q
$ ./configure& T2 v( c1 `) A  `4 w- k
$ make
7 H- M' i: W" n" T- }0 H+ M" D$ make install, J+ V: H0 E" b- G
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
" s9 g( Z! d# k$ p2 a1 m$ B! Z
5 P- a7 R+ N: ?) t  O; I# \/ y/ L7 V
$ ls /usr/local/modsecurity/lib0 o; v# v  H" P
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
3 O* O* r" h5 z4 s5 U  I编译安装Nginx并添加ModSecurity-Nginx Connector模块( {+ Q0 Z" ]) o0 r; N$ p% W3 B* w
使用ModSecurity-Nginx模块来连接LibModSecurity* o2 ?* @8 Z  z7 Z# o% l" I2 [

: Z& M, `1 }. l
( F2 @% v% J  F/ E( ?/ @6 ^$ cd /root
  j# l# e+ |7 ~8 \8 p$ d+ B4 f$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx& H6 U7 _2 K. v8 r% B3 s
$ tar xzvf nginx-1.9.2.tar.gz% c) d: N" I! ?* }( C
$ cd nginx-1.9.2) g( J4 m0 y+ D4 i- h
$ ./configure --add-module=/root/modsecurity-nginx9 k' L8 |& _1 x* u2 J: f% O
$ make. {: H0 K! C) h) X" ?3 b% j. O! |+ v
$ make && make install7 l  ]3 m1 _$ T* K
添加OWASP规则4 L8 n% Z) G8 r# |4 ~
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
  u0 l. S& T1 k9 o' i% ]- g8 V# X, ~1 J# c4 i' f

  J3 [$ A: U8 _' u; I下载OWASP规则并生成配置文件$ u4 E3 T4 h: Q4 D' J0 t3 B2 |
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
: ]& S! r: C9 O. E, ?- ?$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/" z( u2 g" W2 N' Z: A6 h0 `
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
4 U" r. c- e- r8 C: z$ cp crs-setup.conf.example  crs-setup.conf$ a* i& ]! |! B) N+ O0 q
配置OWASP规则
8 R9 U1 L2 h, ~4 v  ^  O; M. q8 d+ v编辑crs-setup.conf文件
2 A4 c1 L. `* E7 M7 b
) o9 x2 j1 U1 u# v) `: U
# d9 h# S+ N* w% T$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
6 P' j6 a1 j' E$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
9 W3 ~0 R! M( ^$ C! [2 V: I$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
7 a( ?6 N9 x( V: q& E3 N: Y) Y$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
+ T# U0 T# X9 P. ^默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
# E& n9 A+ B! |, K. e% }$ T- ~
! T  _9 I0 B& B# @) u
7 B; w! e! }5 l启用ModSecurity模块和CRS规则
; U  j7 u0 t! ^! m+ A& R2 q2 S复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。% D3 |- P% q/ ]1 Q- ~1 v
5 O9 M7 D9 G" W7 v( P

0 \' g' t  h  S1 }- _! ^8 pmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
  {/ E) V$ l4 ^5 z% C4 E( [8 u& b* }& _$ T+ v0 ~0 m$ y  ?

: e1 Q1 F. X1 M, ?* w# m  [% d! r$ cd /root/modsecurity-2.9.1/% o7 ^/ o0 t+ j5 H& G9 z# P5 O
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  ; X0 e& c4 f) D& t7 B1 E' e) w4 J5 A
$ cp unicode.mapping  /usr/local/nginx/conf/) [, ^1 j6 r; P( d# k. z
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。9 ~% t6 ^. j9 H; Q! l
: z, q  r/ o. z! j% d% J& C% h
7 ~; A- W9 J' [$ N9 L0 ], D$ G3 O& N" h
$ vim /usr/local/nginx/conf/modsecurity.conf9 }! w* s* Z+ l2 q4 S! C. F
SecRuleEngine On
$ N4 x( z4 r* ?& V% n+ L+ ^ModSecurity中几个常用配置说明:
2 V9 S4 D. n' `* u) }
3 ~$ [' [; ~2 {- k$ k# X+ h; u9 ~& e# g  |
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。/ l& s+ a3 N6 p. X) ]$ J8 B. v

1 U; E. Y9 G0 g/ q7 i- s5 d  @" c1 n( E7 [  Q3 n/ |, U, m! l7 A
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
2 L' Y2 M: n$ S- U  J
+ F/ O) _! e( E2 Y& g
( u: z+ X7 {' p' H  s) S3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
) a/ g, Q2 s5 U+ K9 o* `& _5 O' B. K5 ~9 \
1 q& R3 d% y3 x
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
7 J) H( O' ]! ]* o* e/ o& l/ \. ]4 O
# R4 N7 I7 u( l
) O" o. R: z/ m+ ]在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
# |: z- X: J. A* T% ^# @0 U# h% u2 i- c/ y# D8 z% }) h* g

" K' c# {4 E* {' M' b) V3.x版本CRS* S9 X  X. y. j
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
- ^8 r1 r' |4 q$ i6 t/ M& I( S# 生成例外排除请求的配置文件& g4 z8 u2 p+ P) s; k' {2 q' }
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
, ]0 c% i1 K* m) l$ B$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
! w4 H; A# j5 \4 J+ r1 t. A$ cp rules/*.data /usr/local/nginx/conf
* e+ \; ?, p9 }为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。; j) z0 ?: J: H) x, _* V! T6 X

6 k: J, _* |5 g8 K( w( b4 t( p  ]3 B+ M8 Y) {( z, g& f1 W. f6 ?
$ vim /usr/local/nginx/conf/modsec_includes.conf2 b' r  M3 s& T# |2 [1 v* G
0 J# e% l% F& L0 `9 P$ g
[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
4 K& m, S% w' |: A# \6 K( F) M
0 @1 r" B6 n$ N: T
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
5 l* a) J, L4 i, C
6 ~. o% K- ]" i
% |4 \) Z4 V! I2 z. M) t9 ~, d配置Nginx支持Modsecurity
' k1 e1 b/ Y$ C+ P, y) Y- X) \启用Modsecurity
; G3 g5 g5 y$ i* D7 e& Y- ]使用静态模块加载的配置方法
! U4 |, [, ]+ [4 b* S& v: v+ j在需要启用Modsecurity的主机的location下面加入下面两行即可:" S# ~% M+ j- `) m( {7 i
. Y( f+ u$ V+ M, u# }

6 e6 P  y$ y; f! R& LModSecurityEnabled on;$ M' i0 A  t/ s( d
ModSecurityConfig modsec_includes.conf;
8 V0 ?  ^4 d  r/ V9 r6 f# t% }修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
9 ?! J$ y1 p6 e7 D5 O! [
0 Y* [  o5 j5 n  L- v- E) C/ \+ L) ]2 D6 A% q* W/ ]
$ vim /usr/local/nginx/conf/nginx.conf, e; J5 |9 @0 o+ r2 z# R7 w# w% `4 D* A
# I* {, f" @, o4 T; w

4 z/ K( O' ]. o  L; y* t; dserver {" D1 J) |4 o# `6 B- T6 T
  listen       80;8 i. z( c# I9 D' I. f" r
  server_name  example.com;
2 _  C- R4 X8 G5 s5 w! q. ^% ?/ I' f
+ b& n  e. @* V% B
  location / {% t# {, c2 a; t
    ModSecurityEnabled on;0 e" D6 e" ?8 m6 {7 o
    ModSecurityConfig modsec_includes.conf;
: L( {& c. P  x& u- ]' c    root   html;
# B. \( @( {: n: @& n* ~# h    index  index.html index.htm;, q- ~& b8 L* ?
  }" c; C, E; r" r+ f# L2 y
}, G! O  @, X6 _+ @
使用动态模块加载的配置方法: A, L4 @! Z7 H: {) B( l* J
在需要启用Modsecurity的主机的location下面加入下面两行即可:1 x: w' ?' K6 A

( K' [' Z1 l( Y/ `; D7 _, u: F  E
6 W8 n$ P* c1 p; Fmodsecurity on;- O& F. ?5 a$ C( d
modsecurity_rules_file modsec_includes.conf;) j0 ?, K! e/ c
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
2 A3 C6 c7 k. {+ f& m! P  @8 L  e' V8 ~6 z3 k9 N
* w+ u: }7 A3 Q: u, [. P3 O
$ vim /usr/local/nginx/conf/nginx.conf
# D# l, C' N6 @1 V
  y, c. f$ \' j- ~  t3 m2 M" }0 R& e+ U
server {, z0 a. Q: c/ v; V- X5 o; f
  listen  80;
8 F0 A. |8 S# E1 E, F& ?- i  server_name localhost mike.hi-linux.com;
3 x, f2 {; [; @3 ?. B2 X  access_log /var/log/nginx/yourdomain.log;: \4 g; r2 W& v; R" |* c0 K2 d
- U  Z7 j! x# V6 f! Q- ]6 j' n9 ]& \
2 f9 ~0 c4 S4 m) q" G1 j; s
  location / {
( c4 u3 I2 r8 \' H% g# f' c  h" e& S. J

/ b2 C* b& m! |0 k. R4 Q  Z  modsecurity on;# P; z" g) k6 ^% `
  modsecurity_rules_file modsec_includes.conf;  I6 i) \# j- |0 m& D9 {
  root   html;
7 U! h8 [" W/ V" n! c  index  index.html index.htm;4 R; V/ F5 m7 K- Z6 r3 P+ ?
}
2 h+ m$ }- F5 j) V}
$ `- l* Q" Y# j5 n2 Z验证Nginx配置文件* E, D4 T$ T  g2 S9 F" z. P$ k- A
$ /usr/local/nginx/sbin/nginx -t5 H6 C7 ~* _9 J5 s% F- c- Q
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
: N: v3 P$ ?0 i2 ]) g/ [* wnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful3 s3 H; b1 Z6 h$ }
启动Nginx
, A- ^4 y9 ?/ i' T! O$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
& f  w7 h0 K# Z* ?
! O# e5 d0 Z: g$ ]3 H

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

2 e- P  ]7 o& y4 E! N1 i# ^% q5 p

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-5-14 07:46 , Processed in 0.064479 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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