找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12946|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。9 E- ?9 r& j6 j, X- g8 K6 w4 x
; ]& Z+ I& T  @$ n+ O/ Q# U

& J. p5 @, W( z( {在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
- D- Y" F, U% W" m
4 R9 L$ ^  g/ F" t6 @# m) o  m- I7 _* U
什么是ModSecurity
$ [$ }+ {* y* n6 YModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。. n/ z$ I) c3 P$ ~0 `9 |& o# M

( x+ {0 r3 e9 j( K+ Y  ?, N# F. p! G1 _4 @" c/ X
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
, S4 N: n  S" e/ u3 ~& F* p: Q
* C6 {  `! F6 ]- W% x% V; B6 g
/ e5 c! ^% b" }% z& w* `" pModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
5 i, W! @: w' _3 h6 w/ Y4 p" Z5 }( O' S+ t

* E* e4 E  @' J官网: https://www.modsecurity.org/% o' l# a  Z% @* o
  t1 V6 t/ a# [* K( h
( i  S3 e. d. [6 s4 S- k$ O4 L
什么是OWASP CRS
8 ^  b  [& x; Y  S1 YOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
! c2 n3 y8 _  T! f# S) S5 W* b( E) Y) r: r

0 q+ c" ]4 o+ t0 BModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。0 d* i! O$ ?! `7 g3 L3 V6 A

4 M5 Z! p( X) I# u* G8 y
- X% f( C) _# oHTTP Protection(HTTP防御)
# X8 w/ ^" I# X( U1 f5 [! r. lHTTP协议和本地定义使用的detectsviolations策略。
2 P4 f- m9 S. A1 j$ T1 t3 _  v# p1 U5 _. T; |" t0 F8 @
0 Z8 ]; w2 O+ E7 f1 ?
Real-time Blacklist Lookups(实时黑名单查询)
. F4 J5 d( O  b7 R" @利用第三方IP名单。6 i0 c& ^7 X$ E3 x- \2 x* x
' N' \5 W1 u1 Z" u& F# ]/ G
( u8 [" U- |  S" i* P! G: A
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
2 ^& z! x( {9 K$ }4 ]  h防御HTTP的洪水攻击和HTTP Dos攻击。
- G( I5 g& ~& m
/ B$ x: k% I/ T6 {/ C1 Z( o$ k6 g( ^8 T( e- G& Y
Common Web Attacks Protection(常见的Web攻击防护)0 x; w6 q2 |, i4 \( V/ L$ {9 [3 g
检测常见的Web应用程序的安全攻击。' i3 W3 F/ o/ q" ?' q5 V

7 U0 ^$ R7 \- h1 i% ]8 G1 [/ i3 W% T& E# s% A
Automation Detection(自动化检测)
7 }/ _$ K  i' z# e检测机器人,爬虫,扫描仪和其他表面恶意活动。# {* V, ?6 {* o$ _
# f- p, Z2 U9 `' {

1 E0 C- j' f7 B9 N" FIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
& q5 q+ K$ ^% Q  E6 a) [1 q检测通过Web应用程序上传的恶意文件。
$ c, u2 ]' D) ^+ @0 B  G
, a+ P* {! Z2 n$ m! c. M) J0 m8 p* I  ]9 F5 ]: s
Tracking Sensitive Data(跟踪敏感数据)  Q7 b: j% r! P: r5 S. U
信用卡通道的使用,并阻止泄漏。
, ]0 Q2 @6 i' [; V' {* `5 D2 u/ t" [8 [

+ [& B; G  O! i$ a- lTrojan Protection(木马防护)0 Y# t# W5 W3 a" h; A
检测访问木马。
& L8 y7 \/ i6 L% R5 o" N9 d- }0 g2 H; g- B  w
; J6 L# w; p; x
Identification of Application Defects(应用程序缺陷的鉴定)
, U' }- C; p4 }. x" G. [检测应用程序的错误配置警报。
% w8 C- @; o4 ^/ J' O5 K6 D2 j. I" O9 m2 |2 s7 Q3 `
! B5 V5 N  w( h: l
Error Detection and Hiding(错误检测和隐藏)
3 A$ z! ~' I& I5 M5 G9 @检测伪装服务器发送错误消息。
1 K. }( Y% [2 M" A9 {) D. E6 v/ I7 J; X+ h/ H$ P6 F2 O
. W' |) `3 i& W, l) m
安装ModSecurity
' \1 O- J3 [. Q软件基础环境准备! J- q* b6 P/ u' g
下载对应软件包
) [0 r  M+ D* k9 }1 \  _' n5 j$ cd /root
7 {! i; r6 E, C1 m. C/ s$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'- v3 a$ D2 U0 V; [" G+ t1 B5 a' K
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz* y/ b) m" B. `- M
安装Nginx和ModSecurity依赖包
) T& Z) @9 s; I8 h! GCentos/RHEL
6 c" k% D/ h+ b( l% G6 X$ [, x" ^: c, H/ \* r8 Q: Q7 N
3 J0 T; j" @* ]' H
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
# `# }, {% ?7 e3 n3 KUbuntu/Debian
& E  ^/ M/ g9 ^, C  c# e: N
( h  R2 n  A% n5 C3 R- x' W8 C- D( o2 C; m) Z5 n. o% ?& 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++-dev9 b+ G6 _5 @9 T. X! b* |( N( U
编译安装ModSecurity
+ g$ S9 N0 h: e% a4 ^+ r$ N$ L# NNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
# b  l8 y1 z$ [5 F, n0 O" ]: n0 M4 t9 b2 d& B# ]- b

1 k+ f  d' {5 d4 p: t方法一:编译为Nginx静态模块
# \7 N% q; b4 W8 G+ ?9 e5 R( E1 E) Q9 C
4 Z0 l/ q6 Y6 ?4 i+ h# T- t1 |
编译为独立模块(modsecurity-2.9.1)2 ?* w0 A$ G7 e: y0 Y
$ tar xzvf modsecurity-2.9.1.tar.gz5 ~! Y0 t7 U# t: |* x) e3 G; |
$ cd modsecurity-2.9.1/* h! c. C6 @- I: q8 W
$ ./autogen.sh" ^' Q3 ^) h, V! ?3 u
$ ./configure --enable-standalone-module --disable-mlogc* s# C8 C5 d" s0 O/ {! i% {
$ make
: R  O1 A5 Y$ \6 x4 J3 S7 O编译安装Nginx并添加ModSecurity模块
- ^3 p8 K/ [1 T( N- t# a$ tar xzvf nginx-1.9.2.tar.gz* C% s1 A2 l* ?" G/ n* Y9 j- ^% G3 e
$ cd nginx-1.9.2& I' e8 o- W' J$ T! d9 ?
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/4 d# l  R! Q* R- B
$ make && make install  O2 J1 {! q% O! Y! i# I9 F
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块( \  T& v$ p7 q1 l4 n
. g1 l' O; B4 u6 K0 i. ]+ H
5 s0 a8 l, q2 ^
编译LibModSecurity(modsecurity-3.0)# A6 n7 g2 E* _' f% ~
$ cd /root
; d. Z/ v8 j, S# E; v: C. \/ i: ~$ git clone https://github.com/SpiderLabs/ModSecurity& T7 K9 R* V9 [2 K3 Z5 J
$ cd ModSecurity7 z7 m3 q: w6 t
$ git checkout -b v3/master origin/v3/master
! s2 P2 r. q) d3 }  g, u9 u$ sh build.sh% W# r, p2 v" B
$ git submodule init
* t6 z2 z: C3 V2 r/ E9 }) m0 b$ git submodule update* I* A# {9 E5 u1 |5 E
$ ./configure
$ o+ x, ~. P4 O! p( o7 k, e$ make' B' N) u0 n$ B8 ^
$ make install  i+ u; G5 F- ?+ l) j
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
) Y& {5 A- T1 ?/ l" @4 e
' q/ D. x  m& L/ b% `' P  o) C! v" C' w7 H
$ ls /usr/local/modsecurity/lib
- ~, P* U( r8 C1 ^+ [libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
# {3 b6 [- }/ a! [5 b编译安装Nginx并添加ModSecurity-Nginx Connector模块0 r, s! m- b! a0 X+ u# S
使用ModSecurity-Nginx模块来连接LibModSecurity( d. V5 ^4 z  V+ F: c( l% j

. d% b9 i; @# L9 U. j3 |
2 u4 I7 E6 O' U! b$ cd /root4 T7 t' u. }' G. v& w
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx( W% N1 c7 q3 b* s& Q. ^1 M
$ tar xzvf nginx-1.9.2.tar.gz- r: e. u  C1 s% M
$ cd nginx-1.9.2: X3 h' F0 j& W0 N- }
$ ./configure --add-module=/root/modsecurity-nginx, `( |( f1 @3 ~) Z1 O; J/ b
$ make
8 m  U1 V6 \! A# L! C) d" r, W$ make && make install
- ~$ ]) k- `% q' W添加OWASP规则8 r' Q  H# Z2 o& g3 v
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。/ b+ l+ a1 l- a4 j  V' w
3 @' a  G6 `9 W2 w. ~
* k$ w/ i5 p- `5 O$ _4 J" L* k
下载OWASP规则并生成配置文件. b9 ]  H: n- V1 Q0 l3 E
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
0 E# R# ]2 D& x/ B2 `2 o: Y$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/! k1 J2 A+ G$ [' y, b& i& s
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
0 Z4 X$ [& @% O7 P$ cp crs-setup.conf.example  crs-setup.conf# d) y) H' ^3 {. {# K& o
配置OWASP规则
) b. W# n6 S1 n编辑crs-setup.conf文件
( C. Z  B5 e% M; k8 ^) j1 y, w: N1 v8 C. {2 T( B) H* X* C* d

: Q8 \, X; q9 Z& M7 R( y: _$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf1 C4 h/ l) L1 r6 X& q9 q
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
, h! y, ~* z* b9 H$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
2 q- v8 ]  t8 e7 J* z$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
9 x  N/ _( P# y+ N/ Y4 c- m默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
$ b, K0 h' j( O4 D$ Q/ }+ l( W( F3 ~
7 A& Q3 r4 c8 J& a( D- n. x/ E% {
启用ModSecurity模块和CRS规则
( y. j7 [( Z4 H: Z% T5 n复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。$ J- o  d# x" j: Y/ ^& X7 f+ P

- U  ~! w7 V- M. L
. z3 w; `" b% {2 l! n; vmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。8 r) D' M5 a" L, o+ P
" x2 J  E5 c/ T+ L8 |, w

, {# ?6 Q5 d* }) q# k7 E$ cd /root/modsecurity-2.9.1/
0 _& i: x: X! H, h  {$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  ! R2 M1 R/ H8 D. U
$ cp unicode.mapping  /usr/local/nginx/conf/$ l' G' n$ y* B3 F
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。. m/ v' P2 l9 C9 h1 l) n
8 o- ], Q) K& q: v% s
- ^# J$ a4 ?7 V3 l5 o6 a" o' B
$ vim /usr/local/nginx/conf/modsecurity.conf9 f2 t$ Z0 ]/ p. H1 Q( _  V1 E
SecRuleEngine On  k+ V/ D9 W1 F! E! V. S; `, w3 N! ?
ModSecurity中几个常用配置说明:  B0 f8 ^0 N2 T* Q

3 W6 ?4 d3 m9 M# X- A# @6 M. q* l5 C
- o. \+ o# p  a% e9 [9 e, E3 T1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
* x" K  e! Z8 k5 G9 K
( m' w- F, L6 w# e9 B, ]8 m5 |. d  k3 }* ?# ~
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。7 k- @% ]# ^; X/ v, c/ u! @( V

6 F1 j! H& o; v( q1 ~* d6 e
5 `* H- ]; M7 K. O! x3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。- F$ ]5 j5 `8 C$ T, q
. X% k, N2 t2 Q$ S' p2 f0 _  g6 m8 N$ Y

( I+ M$ G! \, G0 W% \! v4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
+ V9 J- p3 p+ Y5 i0 N1 z; Q' H% t4 Q

" Q0 ?2 z) n2 \, Q在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
- B' ~; p* U9 _8 U) e% g% u. f3 t  x" }3 B0 j
0 Z" g+ N4 `) H3 |
3.x版本CRS+ {, D$ b. A3 t" }" M+ q$ e
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
- ~, r; Q$ K& p" d- d" n# 生成例外排除请求的配置文件
+ s8 A5 e4 S2 T* x+ k/ ?$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
: k  m0 Z( `# i  b( ~$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf9 O7 ~; j, G$ Y% }: r
$ cp rules/*.data /usr/local/nginx/conf* g' p) v- r& ]) Y) `8 P
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
; z( B- ^& K  x! e: v2 o
" w! J8 f; D" O+ A) b  X8 n6 G" i1 r* L3 \: h8 ?& j7 W$ J4 @
$ vim /usr/local/nginx/conf/modsec_includes.conf
3 }+ c3 Q" n2 p! ]# B' j: k: r
7 e. m7 z6 y; s) W. @( P6 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
6 _7 O6 i! B. d
9 ~7 h) T( I( `4 C* ]( o" t- e
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。5 b. g, j& o, M$ M1 }, Z$ @
& @" U% M, m9 J$ X, P: P1 b+ E
. V; s! A% F. ?: |8 s
配置Nginx支持Modsecurity( o* V0 w( D  F6 F6 {, @% J
启用Modsecurity
2 T$ Z- C  z& w6 }' u3 l8 F8 z使用静态模块加载的配置方法6 e% f! V# K' s5 u$ _  `6 X
在需要启用Modsecurity的主机的location下面加入下面两行即可:: p9 E8 ~7 v& |! o) T3 D! R+ [

- z( B3 a2 ^: v& I5 ~( }
* i6 V9 Q& m& R; KModSecurityEnabled on;0 }" R" s- i  X4 p) A2 c0 i
ModSecurityConfig modsec_includes.conf;
9 j4 L8 g' [+ d3 |6 X修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。& n* k! s7 m8 O) N- [( o0 A3 l

; ]2 `. }& S) f( b. T4 z$ s) J( f; y; Q: F: v- i) o
$ vim /usr/local/nginx/conf/nginx.conf( x# u3 T* w; k' v/ l, F

% v( b4 V/ B9 l9 O: l
3 P* V3 B. i3 X; {server {
( Z2 C8 Z6 B* R! X9 t' h5 B. B  listen       80;6 j5 p& Z  @/ M0 `. S; l
  server_name  example.com;: l0 ^6 Z& r& N  Z' j

/ m4 p% ~8 W& ~) q5 c1 m% }& ~$ f2 u5 Y" L# T9 Y" g, B
  location / {
! K- p3 M6 e; G  A1 w1 s: ~( n    ModSecurityEnabled on;
' F3 n" I& r" K+ v9 U& u    ModSecurityConfig modsec_includes.conf;
. ~4 I. T% g4 u  U    root   html;
& w' i$ b' u" {6 z) }/ X    index  index.html index.htm;
/ G9 Z: X" a+ d" R$ m$ y: K/ k  }; G5 M3 h" P! d1 d
}
( c4 {1 H" p1 b使用动态模块加载的配置方法5 L7 a7 T! o. N2 X
在需要启用Modsecurity的主机的location下面加入下面两行即可:7 |( l# l  q, W

! f$ n7 C5 g* ?% X4 {: L9 y( b) K
, i, P9 G9 i$ b3 V, rmodsecurity on;
% f8 J3 I1 W0 ]modsecurity_rules_file modsec_includes.conf;
2 D2 Z, [, I2 W+ j- W9 r6 t& y修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
3 Z0 c7 O3 d0 ]/ L. c2 S7 Y- e5 Z( N  t+ n+ ]4 z; `& \4 F, e0 A
. l9 w3 i3 Y& a7 O
$ vim /usr/local/nginx/conf/nginx.conf, c0 }6 O1 C. b0 ~+ R+ e

, f' z7 G% l; U/ m$ A
7 `* i: N. d6 bserver {
( Y( c1 q, @2 L) [- j% b  listen  80;
- l9 \9 W  Q7 C4 Z: m( u  server_name localhost mike.hi-linux.com;
8 E4 _5 u% b4 G' O$ k  access_log /var/log/nginx/yourdomain.log;5 z: Q) H5 m  \5 g6 E9 p
1 \/ A7 c& s7 N& R. B* m
  d# {' M. X! o2 S( Y
  location / {
$ x/ J( O* k% n: I6 E! q0 I) p8 O0 H8 A- O7 m7 Q4 a

1 g. `$ C5 j& N7 n) p, i: \  modsecurity on;
" F. g& `: E" Q- l& w; Y  modsecurity_rules_file modsec_includes.conf;
: o9 M4 \% D' j  s) O. _) Y  root   html;
8 ?) |+ B* g, ?& F, }! C6 |  index  index.html index.htm;
: p- Y1 @3 E3 k}
4 z, n. E2 [" F& y1 F% G}5 [3 `3 u: Z; r, ^9 L
验证Nginx配置文件6 ~2 H" m( \/ L  j$ `9 ^4 U
$ /usr/local/nginx/sbin/nginx -t
2 N, t; |, ~; A$ knginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok- {% i% j) i$ n3 T  _3 x
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful5 e$ b+ J1 k9 B. X2 u7 Z( l$ K& o3 B
启动Nginx7 j' W. @6 [# o7 X
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf# T6 ]/ d1 b5 D3 N1 H6 n6 F7 x

& a( {7 k2 v" ]5 a

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

, k7 K+ w" O3 t3 D7 m4 N3 T

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-12-3 12:25 , Processed in 0.076488 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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