找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10918|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
! b( Y6 A4 i+ H3 ?0 `0 X3 j! @$ m4 |0 w/ T" N

9 G3 h& A2 B* N. Z( E0 O9 ~在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。2 Z8 S* R" ^. M& f& r5 R) K0 c

2 K. C. @( x% Q, L& v% o. W
# i- C# a: a5 e0 f什么是ModSecurity# y/ Z$ p) R+ A
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
4 H' `& B9 V! ]* b3 m. B+ i2 F2 \/ a& ]

- W- q* a, [. G4 o1 g! u/ nModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。; w/ K4 _8 z+ h9 J- f6 Y) |% K$ k
  g" j8 _) F" B' ^
) o2 l) X8 s) T: B8 h
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。  g# ?$ A6 X) Q" f1 n

* U: M, k/ ^% q  B+ O$ E$ H$ V6 V. J, Q# r# S- m3 M% ]0 n
官网: https://www.modsecurity.org/, A8 x; A# `9 f- X6 O- R) d
9 @" @" k1 \" f6 L6 I/ _
( k) @9 r" `! `0 F3 ~1 x5 G7 O) G
什么是OWASP CRS$ L. O3 c0 L" p! ?- K& t
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。# L* B/ ~( @  M) e! C* ^

1 d# ?4 {$ d, T$ [. u. j( q6 @( A* d
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。& d# y1 w7 \+ r5 C/ i& I' S

4 K4 E3 p- E: L4 z- T' e8 Y7 S7 g0 o( O+ A: |
HTTP Protection(HTTP防御), w. A# h9 R- a* F
HTTP协议和本地定义使用的detectsviolations策略。4 }) _+ i( D  j

% D( r0 y6 e) l" C; E) b8 u
1 m7 u( ^4 t8 i# E; f1 RReal-time Blacklist Lookups(实时黑名单查询)
& S  B: X/ P% f8 L4 g! l利用第三方IP名单。% {9 e, R% C8 m* z& \

8 u# U* C# B* s4 E. Z- K
- W) x5 s5 Y0 r. lHTTP Denial of Service Protections(HTTP的拒绝服务保护)
. U( ^, ]. m* U4 r; k8 _+ U- x( D防御HTTP的洪水攻击和HTTP Dos攻击。
$ u1 o. H& K% g: Z+ [3 f+ A
$ D+ I4 O- A5 j
+ H, v5 r' q; H& U8 M& H# SCommon Web Attacks Protection(常见的Web攻击防护)
+ v5 U( w2 t( S  D检测常见的Web应用程序的安全攻击。
. G" v  L3 Y2 P, R
) }$ R1 b, V5 X- b9 D- Q
- |9 @3 e! ^6 J, e6 {" O" lAutomation Detection(自动化检测)
0 m4 ?4 `3 k+ i& q$ H- B! ^检测机器人,爬虫,扫描仪和其他表面恶意活动。$ k+ S$ f4 }% T6 }0 g+ W5 }; |

6 G, @& m& H) p; q
) l( d5 C2 k, K5 h6 T1 tIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
3 ^3 Q% ^( P( }* p7 M5 r检测通过Web应用程序上传的恶意文件。9 {9 v- G* `3 i% `8 L5 D

; v  f7 D5 s5 P! ^; x9 n1 t; r. _( O8 \2 z( J7 I  `7 D: c
Tracking Sensitive Data(跟踪敏感数据)
3 i' U9 P* T8 i* w  T3 {信用卡通道的使用,并阻止泄漏。  M, p) U/ P, D# U; n% x' N
# z4 A+ W# I  j4 d, M; Y6 s
' z2 p3 y. a# t8 N* a
Trojan Protection(木马防护)- _' D8 G: F: L  \- n8 H
检测访问木马。
7 E& A$ o! R0 H
  U. X# d1 z# A- |' M$ N' P  o3 Z* y8 q; Q0 p0 h* m
Identification of Application Defects(应用程序缺陷的鉴定)) ]5 @. b& b* _: j" s, F4 [
检测应用程序的错误配置警报。
5 a4 R4 _. X8 j/ X( K/ b
6 g, H7 [- z9 D* y1 S, j. M/ _: J5 U0 A3 X0 f. s4 R
Error Detection and Hiding(错误检测和隐藏)
3 c5 M4 t! k1 `- f/ c检测伪装服务器发送错误消息。
7 n9 Y' c. N+ }* Y
  }) S, ]3 ?. C+ @* `/ ]' ^. c
) |4 k; ]1 {, G' g. [安装ModSecurity& X) Q7 w6 l/ o3 j* }7 w
软件基础环境准备) ~+ m/ B3 o: x! N
下载对应软件包7 f1 z8 j: R* X. m8 S2 o4 {! Y: v; A, k
$ cd /root: s8 \$ J) v  m7 T
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
' b0 g0 v/ v7 L' ^7 o$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz" T3 {( e# K! p: {
安装Nginx和ModSecurity依赖包. N; m. ~% F1 X
Centos/RHEL: O$ B  z; o  N
) N4 Y7 C9 S8 a1 q1 S: O0 a: x, X+ C
3 |" e) J- P& K
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
2 R* D/ \$ C9 ?' q$ tUbuntu/Debian
8 q. S) F: @% z8 e
- [* D6 r* g) j  c9 q/ Y; G0 @% L5 b7 f) p% J" Y4 ^
$ 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++-dev3 S6 b+ Q+ s* m4 g6 s
编译安装ModSecurity: D* R9 P& Y' t8 h& P
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
0 l! Z& c; L8 ^) q- U
( U' J, I. T# d+ n7 l* h3 V2 |  C
8 S5 X  @, Y# L8 E方法一:编译为Nginx静态模块
. F* W! h" d. i, a7 ?
2 F, i. a6 o4 O8 P( E( T: ~) M, |+ v5 g$ s) ^5 X& a
编译为独立模块(modsecurity-2.9.1)* B3 L! g" g) Q  a
$ tar xzvf modsecurity-2.9.1.tar.gz( }5 V, x# t6 B1 X9 N# j
$ cd modsecurity-2.9.1/
4 [7 M7 t9 N, D3 f4 d# E( F% Z$ ./autogen.sh! v! V) w0 b: G! g$ u* C) ]
$ ./configure --enable-standalone-module --disable-mlogc! B9 ^7 A1 N5 U8 ~' [" K; J" ]
$ make1 f' F& {& w3 `( z8 `
编译安装Nginx并添加ModSecurity模块+ p5 `2 f2 T' k: ^) U: T
$ tar xzvf nginx-1.9.2.tar.gz
- ?, t6 @8 U* S3 C$ cd nginx-1.9.22 y& c( c6 h3 |+ n* X9 W
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/- B5 l9 u; ^; U. f3 z
$ make && make install% t0 S+ U( o+ b- X4 j
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块6 |1 k/ G9 o0 Q2 r8 }* F

% K. V7 Z7 s% r4 W4 L/ R. O# g- Q0 _3 a* P6 @" [* K
编译LibModSecurity(modsecurity-3.0)
, t' ~6 i! j1 A' Z) R+ f$ cd /root
3 x* @6 T( E- x# o/ ?$ git clone https://github.com/SpiderLabs/ModSecurity
0 I# ~! d; _& a* d1 f4 [: c) s$ cd ModSecurity8 i# U7 }' C; c/ Z
$ git checkout -b v3/master origin/v3/master% n3 G/ P/ F' D7 z, J5 D# N4 ^  _
$ sh build.sh
4 z. S3 D0 E: F) h5 g# Y, j! G" i$ git submodule init
% E8 K. n& Z. P5 h- j, }$ git submodule update
; c! E6 G  A, m$ ./configure. C/ H; L( W/ y8 s3 T% s2 {5 v
$ make
2 W1 l3 U3 w# T& X+ L$ make install
, a( l3 `7 X! K. |, K# v+ |$ U( zLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
0 T( L- c3 A% @+ Q1 m# h  r' N6 c7 b* l, n; O2 D6 q
" M  y* C+ @4 O4 ?! x
$ ls /usr/local/modsecurity/lib
9 O4 y% Y) y% @/ Mlibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
  B, P3 B8 n& Q" d! M编译安装Nginx并添加ModSecurity-Nginx Connector模块
- w4 E. G# y5 A, q$ E0 ?- p) X9 p0 m使用ModSecurity-Nginx模块来连接LibModSecurity
) |; E9 ~; ^- l6 Y0 V
- [6 H, t" g  y& U  |' T4 y* T( b4 \5 x- H
$ cd /root7 V3 q( e+ U- c1 L; ]4 u9 g1 {6 J
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
' Z; Q$ J9 d1 ]# }6 ^/ Z! P( p$ tar xzvf nginx-1.9.2.tar.gz
7 ?3 S8 b8 M, n5 m  q0 B' ?1 c0 j$ cd nginx-1.9.21 E$ f% X, X' I% I  d% S
$ ./configure --add-module=/root/modsecurity-nginx2 _1 _2 i  f0 g
$ make& n, ^8 j$ j$ G" w
$ make && make install
7 E; Y% x0 v- S; k添加OWASP规则
6 C8 x- k* w, z8 ?, Y( uModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。4 d4 T9 v+ e9 r9 X' D& L" ?; P

( r7 O7 W) e; W; y- O# L+ X; _1 R, O0 F
下载OWASP规则并生成配置文件
% G" H. h+ @- }- a' a; j$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git0 G8 v  L: D4 ~% a: ^6 s+ z% B
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
3 s1 H; v4 v9 i0 f( N$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
+ F: d- f9 y, t, O4 }+ Z7 Z0 N$ cp crs-setup.conf.example  crs-setup.conf
# x% r& f' p2 d. e. ]( n配置OWASP规则% T2 K/ x% Q+ R& b# a" G; w2 I4 }
编辑crs-setup.conf文件
  o: d/ \9 r( a* u' F
0 g$ M0 p; {, K$ M' d) E7 h4 k; \( V* Y, j
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
; Y" J  y; k+ ^6 J$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf4 b- R* i& K3 y
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf$ h# V& q2 D7 y
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
1 F# K  {# ~, d% q/ E) ?1 q6 X0 ?默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
* p0 o0 a' A/ L& @
% r6 y% j! ^2 {5 ]5 u6 o! k2 D6 h
* H8 ^& t% q* \4 g: `( }" Z启用ModSecurity模块和CRS规则
+ q/ I4 {" d3 z( n! ~/ a5 [5 P, K复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
4 @7 m, t  {, z' {4 L. K& T1 V5 Z6 l# h. B
8 ^3 _* k4 W0 @( ?/ W
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。6 @( Z- j" P' f; V
2 p) [: q$ o/ v/ e8 {: J) J

) ]) i% z4 `* y$ cd /root/modsecurity-2.9.1/
6 l5 J% ]/ M" E$ ^& x$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
- c3 b: Z7 q  [: H$ cp unicode.mapping  /usr/local/nginx/conf/9 G  J* H7 e2 L0 b5 D
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
3 y/ M& [6 B+ \3 _& O5 W0 K2 k" a0 V' {/ \' V

9 E) T8 u7 d/ I! s8 X9 w$ vim /usr/local/nginx/conf/modsecurity.conf& D  O% w" x7 Z: m
SecRuleEngine On) X7 H& x% i# Z6 I6 F& W+ Y
ModSecurity中几个常用配置说明:5 {) y! _2 F8 b2 T4 h

. I, ~" ^9 t) Z. s: p
" z( Q8 F# [2 x# r& c1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
/ }+ {9 M1 u0 I0 b* K
* x* P" }+ g) m- _
. T# }7 i5 Z! l: R/ Z2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。5 x& Y  U4 u1 d# P1 j
1 A" }/ n. X: g' @) G# b' I
% o, }$ c8 A+ R' b% G5 O$ P. M8 L) c8 I
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。2 f6 D: W) N8 D( c+ ~- t5 |" S& U
) K2 y" q$ K$ e& P
# B  `- P0 G( F
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
% @: S! i  b( y; f" D$ \! u
/ h. Q( i, h7 }/ D3 ^$ L* ]+ i5 {0 b' N" T- a/ V6 L2 d/ W
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
% Q" X1 @6 s& W7 B- Y2 Z0 V' n8 f; A4 B8 f: v* {
  x. F8 @  Q. s! w% p/ [- a
3.x版本CRS
9 O. K0 K0 J" P% j# P. p$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
1 @+ |( m" O3 Q$ w) ~# 生成例外排除请求的配置文件
- h* i$ I) c8 z- S" Q5 v3 L* Z( w  G$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf0 M; s) E1 |$ v" {
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
# `# v/ H; |/ r$ cp rules/*.data /usr/local/nginx/conf
7 M: R. M: d; [+ n为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
: Z, i+ \: f  P0 s$ O8 a/ V: E1 `. o9 a/ V- r5 e) a4 B0 L, P
, E* B5 K+ c0 J3 A; A. W/ a6 G
$ vim /usr/local/nginx/conf/modsec_includes.conf! a. W' A/ ~. M+ u# m

- p; T4 p8 l  r
[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

/ V0 X4 w1 S3 T0 B6 }2 T! G; x' u' e- l1 X2 s
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。+ a- X; x* Q( R" Z/ A& L& `

' f$ I# a3 o, }* e" R
: O: J# r0 `/ O$ I+ z& R! T* ~配置Nginx支持Modsecurity# T/ o1 @& Z0 N( d
启用Modsecurity
2 H2 V: w1 Z7 @1 c使用静态模块加载的配置方法
  L2 @* @8 l, G7 I2 p; R7 Q- T% T7 e! x在需要启用Modsecurity的主机的location下面加入下面两行即可:
0 q/ v! r+ p0 p
" Z8 c/ |1 ?4 Q* G8 K: X2 g/ O" L3 r& a* d  f
ModSecurityEnabled on;- Z: a, b& z/ t
ModSecurityConfig modsec_includes.conf;
9 j( n! I- m7 Y7 z" F" E修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。' q- @$ G' r/ X

( B. L% F+ y& d" G6 I1 D% u' B  W0 \7 f# Q. ^/ O, G. g
$ vim /usr/local/nginx/conf/nginx.conf9 G  S2 _; ^( S4 e; f, d3 p9 z

1 `' Q1 j) i  }/ l; `/ {/ f8 i5 I; T' }9 t* _: @8 h
server {
& {& z7 }+ K0 h/ x# ?  listen       80;
' \8 K. y" P$ I, |  server_name  example.com;
& V$ t6 K/ L- ~0 k' Y+ k% \' E! I- {- U- [0 ?

1 O$ d5 u- g7 F: }3 a  location / {
# @' q# e% Q# u9 j9 w    ModSecurityEnabled on;8 D3 S8 x# e* L2 y0 U
    ModSecurityConfig modsec_includes.conf;
. V$ }. [  C0 w4 O8 j8 `% P    root   html;
# w) f2 w. X. ^& W$ C4 m$ @    index  index.html index.htm;
! n. A. t- z4 A8 z4 H4 F" C  }$ P6 N7 k) K" C7 x. r5 [
}1 }; F* g, b- S1 t  s; t) _
使用动态模块加载的配置方法4 G7 u; X8 z6 B2 a" z
在需要启用Modsecurity的主机的location下面加入下面两行即可:
$ q" t. {1 D. \; S; y9 U! C) o4 a2 v9 {
, q- K+ z3 L1 @9 I0 Z* u
modsecurity on;
1 ^% e) @6 Y9 k) mmodsecurity_rules_file modsec_includes.conf;
  `3 O1 B2 u) Z0 f+ N, ~修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
( q  K2 b' j& R  |6 l/ a( g. ^+ G% j$ X. ~

  R0 R' X( e/ l* k! L$ vim /usr/local/nginx/conf/nginx.conf4 D4 V: j9 G; }: x. n: O7 a$ V1 I2 w/ }

4 m. e" }+ ]& p3 x3 w. j) W3 U
: v  X8 L4 G- G7 k, ]" A$ |server {
6 e" h8 e9 n1 z6 ~+ R! @3 a, z' U7 b  listen  80;
$ `" G) f% F6 W5 N2 Q$ R% [  server_name localhost mike.hi-linux.com;
1 ]# b; Q, f% C- E# E  access_log /var/log/nginx/yourdomain.log;2 M7 N2 t" g+ C' U

/ H2 ?$ j4 A: E( N
8 v. y- W# F; d  location / {: S+ x6 f  d) M+ q$ l3 q) Q

5 q1 e% D# K+ U0 X: F* \8 A* [, g
3 }- {1 @) b$ i6 Z  U; x. x  modsecurity on;
# n4 o7 Q8 e0 A) ~' c/ J; K3 @9 U* S  modsecurity_rules_file modsec_includes.conf;: x% D6 Q/ r/ N: |. K$ r+ m% \+ R
  root   html;
" u" t0 v2 l9 W5 t9 v  index  index.html index.htm;( U  c3 }+ C4 x( Z. \1 J) P
}
/ Z" N9 N; c2 p: R& {+ L}
. E0 t; w7 v& m验证Nginx配置文件$ Q+ N+ |1 U& B0 x% p
$ /usr/local/nginx/sbin/nginx -t7 T4 d  Q# N: V9 v% D0 p3 Y8 @" {
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
( a* S3 n4 u; f) @5 Qnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful4 ?3 o( s  b/ o- C- }2 [, h3 I
启动Nginx2 L8 x4 f5 V: T/ O
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf  N) `: q) Y  z! r0 [0 K
1 M3 \8 u" s! D6 u- u

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


+ p# S8 L; ^6 P5 Y' h. d' c; L# B

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-4-23 17:05 , Processed in 0.061566 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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