找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11985|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。& k, j( k( |( ]& _/ c! E0 G+ i1 u
# |; O. C8 q: \" }3 U. ~

4 t1 j% p# f8 `7 a在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
. n2 ~& ]- Z/ ^2 e0 u1 ~! R/ j  m  F4 J  ^! J- h5 K1 o

& X3 W) f) Y; N7 I: T) G什么是ModSecurity
7 f. s6 m' f# k: zModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。8 ?: X+ z& C' _$ _+ o7 F% ]
! ]% v- f0 y+ R/ F
- }# p9 h) c& I
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
) f% W3 p& v; s: @% z/ d8 @' u" j5 I8 v5 h
* Y* i( o5 R4 \9 v6 ]8 ]' R
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。$ e% n+ O0 E3 x) L' b- ?. Z4 ]
6 u% K. B& S/ G
+ \2 m  u1 s9 e. Y
官网: https://www.modsecurity.org/( O; T; Z9 {$ g/ K9 ~6 m

# a/ q7 g) ^  ]6 t, g1 ~* p& }1 \$ P- G2 r
什么是OWASP CRS' t% R; X( o7 S; _0 M, B
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
6 x( q+ l5 S/ o9 W3 s) R& L
9 u8 P7 o& J1 x2 c8 y# u9 X$ d
3 i8 I+ {. l7 M- j4 |( B% |ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。# t. R0 C' C, T1 @/ _1 _5 A
1 n8 {, C/ I2 o4 U$ `9 z( }) T
' v7 C: P5 \' }4 T3 ^+ u/ T
HTTP Protection(HTTP防御)
. ^) W' `; I, \HTTP协议和本地定义使用的detectsviolations策略。
& A9 y' s0 V0 n5 X- @$ _, E  L# |4 X8 U% y% n6 j1 T
; H* c! ^" M" B% p2 s; a
Real-time Blacklist Lookups(实时黑名单查询)
# n6 V4 t& |' w利用第三方IP名单。
# x0 R5 c6 r$ o, J
# B# X* x2 k/ c% M+ w
- {$ A* t! j: ~( y* fHTTP Denial of Service Protections(HTTP的拒绝服务保护)9 V7 L- o9 s: C5 l0 l1 f
防御HTTP的洪水攻击和HTTP Dos攻击。
7 P2 R& n) Y: c/ n- V
7 a0 n1 q  K' m/ [  a5 I( T+ a  M, l! \
Common Web Attacks Protection(常见的Web攻击防护)2 D' `9 N8 I3 ~, k
检测常见的Web应用程序的安全攻击。& ?- c/ m0 g: D" H
5 z7 P4 T  ]8 E7 [  D
5 U8 ~6 _( _% l  \$ |
Automation Detection(自动化检测)2 B( w3 [$ [3 I. P8 f; M& S
检测机器人,爬虫,扫描仪和其他表面恶意活动。
/ U3 v; P# E5 k6 t2 R9 g
3 n$ O3 o$ C6 [0 y3 P8 N! N# b7 R8 D8 K: Z7 m  h
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
3 t" L; m3 e5 V% V3 O检测通过Web应用程序上传的恶意文件。
  U) l1 U; k, b  `, F6 Z5 L* c# S5 b6 ?/ J! ]
/ `/ F' m4 r; S; ]
Tracking Sensitive Data(跟踪敏感数据)% X) X7 P) f# Y9 e9 o) V
信用卡通道的使用,并阻止泄漏。- M$ N) }! |3 t0 R- Z$ V. l/ H  [

) c5 j9 A+ T' D# h. ^! O7 M7 f; O* F3 o% Q# U: n" i
Trojan Protection(木马防护)
* O3 ?- k; f( i0 e# {& ~) _( X" l检测访问木马。
/ M9 E# M" |: R4 ~! j1 P
4 s0 @, @( G3 e) [  s
9 e' C9 Y7 ]# P; B5 C/ J2 NIdentification of Application Defects(应用程序缺陷的鉴定)$ L" O' ?! k+ c" p" V% `, o% \$ i  M
检测应用程序的错误配置警报。- L5 Z6 w5 `0 k9 C- `
& L" e7 o- b4 }2 J1 p
9 e# M; ^* x9 p- Z0 O1 o0 b
Error Detection and Hiding(错误检测和隐藏)
' ~2 \! w  ], [检测伪装服务器发送错误消息。7 f5 {1 \3 W6 k- y4 W* m

% H3 ?, u  u- k5 C. C0 X
3 X" O4 w$ E% g7 {, Y* r安装ModSecurity
, C" g& f3 C4 N2 ^0 i软件基础环境准备5 J9 @- `3 }7 E& Z: j' d; O
下载对应软件包( S# }# ?) @. u/ c, [8 p2 Q
$ cd /root6 m: S3 Y* Y' M* q  p
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'$ N, T1 A: `" g  `; Z
$ 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* T3 v; M* d- L安装Nginx和ModSecurity依赖包3 Z0 @) C* H. w+ \& w, j
Centos/RHEL
/ I: R2 a2 E) l2 j8 p: ]1 f- H
6 g) Z0 V- P5 }5 I
' Y& h7 R4 W  k/ J' ]6 m9 c, T$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel& b- z; A, ]7 ?- X5 W" a5 v
Ubuntu/Debian
& f9 U2 I1 f1 |* T. R9 {! z
4 Q0 D4 u+ \6 ]8 X; p5 t) ^* V
" H; B# x0 }4 c. @+ w3 J( r/ j$ 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; x6 ^  e8 U- {- t. z
编译安装ModSecurity
! P  ^& |! ]# K4 U6 L2 pNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。% h7 j& \6 G0 V% Q4 D: P, J

( l/ R& `. N0 v
. J( ~3 U4 |7 m5 ]& x方法一:编译为Nginx静态模块/ |$ @3 [/ A5 ~% G# V* [1 p9 N
9 u6 K$ y- O+ S; ?* H1 ?* [6 }

; T! Y5 P$ z  a编译为独立模块(modsecurity-2.9.1)' n1 ^" }+ K% w, ]( l0 Y
$ tar xzvf modsecurity-2.9.1.tar.gz
, y; |1 y7 U& [+ a: F( f/ F& |7 I) ?$ cd modsecurity-2.9.1/5 ?; w  P" t6 @5 p9 k! ?2 i
$ ./autogen.sh
6 P3 ~: L3 Z! N1 ^1 J$ ./configure --enable-standalone-module --disable-mlogc
: c3 R! S! ^, s: E& x$ make9 ^8 R/ o; ]3 }# y; @
编译安装Nginx并添加ModSecurity模块, y3 S* h3 C8 W0 q+ m7 J! y5 \2 i
$ tar xzvf nginx-1.9.2.tar.gz
) Q0 `* H7 o4 a1 }2 d" H# V$ cd nginx-1.9.2" t5 v! s' L& ^* C) w) j
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
1 d. j& f6 p& H' R$ make && make install4 ~) `1 b0 e& v. n' S) b0 D
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块& P) r) O" g5 u

+ b* [9 U- e" F7 O. e& _; C5 I( E7 G3 h/ e
编译LibModSecurity(modsecurity-3.0)
* n4 b( ^) f& Y  c) c6 o$ s$ cd /root
4 P. Q+ z6 |5 L4 J$ git clone https://github.com/SpiderLabs/ModSecurity
( n4 j. l8 T6 L- n; |0 s( a- U1 d$ cd ModSecurity2 S; t2 }6 P, O2 l) P6 P
$ git checkout -b v3/master origin/v3/master
1 L; V) H4 f$ q% ]2 m  L0 G# x$ sh build.sh. L2 d  q* ?1 g0 v
$ git submodule init
% {5 H& M! \) `+ ^$ git submodule update
. O' w# B( o& N* J4 v$ s! J& ^. T$ ./configure
5 H4 H  e2 `/ J3 ~8 L( f) f3 Y$ make2 q% p  O% ?2 {: O7 N
$ make install
) A7 Y$ y( x$ K' z. E% ULibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
8 q0 \! m) D& }8 X; }& U- _$ V0 o

" g: `7 S" o% h& D$ ls /usr/local/modsecurity/lib
9 Q$ E+ f) o6 F8 O4 }& M) clibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
+ L0 Q1 g8 r8 K3 j. @" o5 b$ E编译安装Nginx并添加ModSecurity-Nginx Connector模块
3 a5 ]! Q/ c. }  j% [+ Q3 M: P1 p使用ModSecurity-Nginx模块来连接LibModSecurity% K1 `5 Y- v/ f$ X

/ M/ x8 {' {8 u$ _5 D
& j7 c% e" n( _' X7 p$ cd /root1 g" ^4 H0 J4 |. Y9 Y4 t
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx7 [6 m; I, {. V4 X& W5 [
$ tar xzvf nginx-1.9.2.tar.gz
7 o" |5 J5 _# ]$ cd nginx-1.9.2
' o1 G, M, A( \1 F* I+ ^7 _1 C$ ./configure --add-module=/root/modsecurity-nginx
9 b& P! I$ W7 \$ n4 H$ O9 J% O$ make
, P, ?+ v8 O! ]/ B  `  ^. o  m" c$ make && make install, ]: {5 U% \3 e) \3 H/ c0 `. ~. U
添加OWASP规则: [, T% n! B' l  G
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
7 p* {' i8 b. e  k" h% H5 c1 x4 v7 _3 N' g& J' p" ~) l/ X

! X% {5 V+ a; Z5 Z4 H下载OWASP规则并生成配置文件7 W1 p- A, P  S2 r
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git/ t  A5 L) ?* c" k
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
1 V  U8 i; ~6 p6 ^* x, M9 q% i$ e$ cd /usr/local/nginx/conf/owasp-modsecurity-crs7 i: D" l5 ~$ |" Q
$ cp crs-setup.conf.example  crs-setup.conf; {$ m( _% G; _* e
配置OWASP规则
# I1 x2 D, ^3 U/ h+ b8 X$ X编辑crs-setup.conf文件6 j+ n  Y& F( |! |% c  A, }% ?

" w; A# v" a# _* l
6 M* \9 J! u/ {2 O6 {6 I, T& U; S. ^$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf- h. M1 O  |4 ?
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
  k8 r9 A* |! Q* g$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf" b; [" B' f" P% h0 ]8 ]3 m
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
+ z. V1 `6 k# p默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。# b% i4 O" o2 z: x
" G: |5 J  f1 D" H
. O, a" A( {7 m
启用ModSecurity模块和CRS规则  v) ]+ J# d- F4 P( o
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
6 D; M/ o: v, z  O
8 C1 ^; w$ j9 ]6 V5 C" \$ L" i7 I" ~0 y# K9 q- t0 J
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
2 d$ Y- G0 i+ k4 U* K2 v6 L* r
! S- F6 `) n7 J3 l. @7 Z6 H/ u, H. b  s. E. {) W
$ cd /root/modsecurity-2.9.1/! e8 m4 G! x5 [5 |) I" P
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  ( V$ r' c9 [9 ]/ g; s" A
$ cp unicode.mapping  /usr/local/nginx/conf/# o8 N* k. O& T
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。7 }( q$ k1 A# Q# i5 \
" q. p% V) }. Z6 D
" ]* W4 B( |" t1 @
$ vim /usr/local/nginx/conf/modsecurity.conf
* f& c  |8 W3 L% NSecRuleEngine On
+ ^5 H1 I7 i# M; Z" Z/ \ModSecurity中几个常用配置说明:+ k/ J; z% I! L  N" N
/ ^* S+ T3 G; Y3 R: C) U
1 l2 M8 Z) B4 q# I7 B8 H
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
/ g+ h/ h* E3 @# S, C" w# j
1 m1 s. b& T( y: u* Q1 J  T1 u% l$ Q8 ^( Q. t
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
9 m0 {& j- m: E- y) _* R  F/ @" s% @# r, L

  J0 d+ y! |7 w4 G6 f$ @* X/ n3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
( j' g9 L5 ~8 V) l5 S9 O% B" A" L( }  A# R/ ~7 s4 e
: W" G. E/ }7 x2 {1 l; C
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
/ @) s3 F+ i5 S$ ^# q% \) k- b6 A9 p; M: i- {; A

3 l+ a( @6 O" y( |, A在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
0 }, ]) g- A0 v% i5 N/ k& W& I' L3 G: ^( g" d- q0 }

) j4 {/ \9 O7 r( W" x% E5 _3.x版本CRS" G/ N0 o0 W. w% _5 E2 G3 G
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs2 a; y" K+ T  m( C2 C& v
# 生成例外排除请求的配置文件5 e! D. h+ v( e/ [) @9 _
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
' w1 n* I$ Z! Y& |. S3 v$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf7 H6 N/ h1 A; ?$ s9 U1 W
$ cp rules/*.data /usr/local/nginx/conf2 a" @7 }; X+ ~* s+ r
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
' S$ L+ z8 x0 d  Z, \' p! }9 ]/ }# T& R# A- l/ M
% `, }% G2 U) R* u: M, `# B% V
$ vim /usr/local/nginx/conf/modsec_includes.conf
2 n% ^- y7 l$ g' ?- O0 h4 y7 M9 m& Q& u% j  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
! R' A4 Y! a5 r5 i5 ~6 v

4 ~$ o3 e) \( r7 M3 k) G  o! @) j注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
7 Z5 e! @6 _+ w. |4 D* p- w  Q* F& Q

3 }3 g* `' b- |& x4 a& ]! m' v配置Nginx支持Modsecurity& `; x9 E0 X9 @; K5 O/ {2 O# `) ]
启用Modsecurity& o& j& R% G& g; p
使用静态模块加载的配置方法
$ e! F4 L8 i4 A# R  G  i在需要启用Modsecurity的主机的location下面加入下面两行即可:
5 F/ j* n8 G% X  H* k8 k2 q6 O
2 j, e: K( v5 P
3 O: n) O4 a0 s" x: t- P3 H3 X# yModSecurityEnabled on;1 s# }! n! Z  Q1 }" E& |' @
ModSecurityConfig modsec_includes.conf;# @( h/ }# }- `- t1 X1 U0 s; R
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。, ~8 U: g3 t' T7 g
# h8 r% \6 |9 i0 @
1 y; h; G/ U& f- s; y
$ vim /usr/local/nginx/conf/nginx.conf8 U1 j8 h0 d. c
8 A! u9 h. L. H( p% X
6 ~$ l8 k$ Q0 a6 ]* a3 @
server {, r& G! Z% e+ C1 t
  listen       80;
: f. T. K- ~4 U7 r' W; t  server_name  example.com;
$ {" f$ |. z# S& u0 r' }. J* F% m" r5 m+ l

- g& r2 s- f6 D) ]  location / {
' o  L4 O* r0 i# ^" r    ModSecurityEnabled on;5 x. ~- c8 s% g! O  t. h
    ModSecurityConfig modsec_includes.conf;4 Q$ |( V. o' J% i9 m' V
    root   html;( C) c7 ^5 h8 ?; O
    index  index.html index.htm;# V9 F# D  y; w2 b4 {; `
  }
; g  \3 O. q  n1 n' G" t# p5 }$ p}" x0 i4 g1 x! E
使用动态模块加载的配置方法6 H" f, H0 b4 X  a6 S% u5 m
在需要启用Modsecurity的主机的location下面加入下面两行即可:' T2 }7 d$ o7 u8 B% G" n9 D  K
. `; w4 \; o' e. @+ l

9 |; L; \. V3 G' W$ R9 x% z$ H$ ymodsecurity on;) D7 f. c% S8 c$ L
modsecurity_rules_file modsec_includes.conf;
6 R, P) B, M  H. k, w5 o修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
; A; F) i1 V+ n4 R, ~9 ]( w3 b) P+ J  K  u* s& B( C. H& I
* ^* x+ ]* ^! E, {) B  {0 N
$ vim /usr/local/nginx/conf/nginx.conf4 }8 K7 U. b' X6 s( |1 p  X
. c! m' r* S5 O7 k( V( Q

0 n7 `0 V# [8 {0 cserver {% M7 K  E8 X" Y
  listen  80;, k$ E; k0 K1 R" h3 J" e6 q
  server_name localhost mike.hi-linux.com;9 A+ B1 z6 a/ j0 A
  access_log /var/log/nginx/yourdomain.log;8 Y5 d5 O) Z. Q  |& z* D6 U8 v; X

6 z$ F* d! M/ a) Z
- [5 x7 u2 n. A# j5 C( A: l  location / {
2 u* {  i8 w- {! ]) G
, X- Z! G# b- d- F( p2 l1 H; P3 Q2 @
  modsecurity on;
, ^* V" C. V. T0 Y1 P' p  l4 w; T9 P! r  modsecurity_rules_file modsec_includes.conf;+ t: |$ o$ a, s: K5 \
  root   html;6 j# U+ i7 H$ i" \. l% w1 B3 T& j# G
  index  index.html index.htm;
9 A( \' q/ L" |! i& N; D, U$ C" y}( s5 V# b1 v9 f. [" O
}
6 T9 k+ Q+ k! B' X  K验证Nginx配置文件
3 P( w5 |$ w3 W$ /usr/local/nginx/sbin/nginx -t
# |0 f8 a5 v  S: W/ T- P. rnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok. S( }) n% f. V/ T8 z2 @  t
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful  x  `. c  O3 z, X1 |
启动Nginx
  D# g, U9 C: o& f( q8 d" ~$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
7 H' B  S( d4 G, C) y5 _
; K0 C: E% d; w

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


6 D. Z* Q9 T, x/ c

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-8-6 01:35 , Processed in 0.063494 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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