找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12745|回复: 0

利用ModSecurity在Nginx上构建WAF

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

9 t% g5 W) {6 m% e3 G) h' A在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
2 H5 \, H- a. v! N* b
# V; d1 R7 }; Q! y! U! `7 F" V3 B# k) G$ s8 }' w
什么是ModSecurity+ O) i+ p9 ^/ y
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。- N; r8 r* f' [% o

! o6 B; j! A8 p; ]1 N& t" ^  T0 `' }9 p; [
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。! K) P( {# k2 N/ j, L2 }

6 h! k- H' b9 p% w' \
: e0 r# j- L7 \% m2 Q: O9 `" tModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
# |4 W6 _# C- o. r5 I  J. T
- |) T0 R9 h- g5 {# s# t4 K8 \$ l; J' F0 W( |2 a
官网: https://www.modsecurity.org/
6 T9 R3 P2 W: |7 {2 C4 ?8 v* I' h- t* K9 y: r
4 ~0 B8 H5 B5 W# Q6 u
什么是OWASP CRS! |( a+ K' P! N6 X
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。3 I0 j3 h( `2 z5 J/ N) R

  T4 Z  x% q9 |" }8 \4 k& I1 i2 \0 s  d( t
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
1 n2 t! L) `$ d$ E% ?
7 z- P7 J& a  f* A3 a6 y/ \7 E" A8 a' k1 \' ?' L% Q% F/ ~
HTTP Protection(HTTP防御)
  s1 g4 K* w& T; J  V( R+ P# c& IHTTP协议和本地定义使用的detectsviolations策略。# v/ P. o! X2 _3 u& z( P+ }
, h& l  ]7 O- U$ k' n( ?& y# x
" z6 v+ M5 A. E1 @
Real-time Blacklist Lookups(实时黑名单查询)
( C2 _, v: L/ O# G: W  i# e利用第三方IP名单。' z+ i4 c1 T: S+ K+ ~: Q

% G: E7 b8 {7 v* H4 \& H% W( \- ]. l
5 m8 l7 f! i6 D1 k7 ]HTTP Denial of Service Protections(HTTP的拒绝服务保护)
" H2 S& h/ b# c) O防御HTTP的洪水攻击和HTTP Dos攻击。6 S" P9 r2 i" P& c

" j1 E& I$ c" c2 f) r0 `: |8 ~( P* z) K: S4 B$ H! l+ b) |9 Z3 i' o
Common Web Attacks Protection(常见的Web攻击防护)
7 F' ^) ]' v( Y7 p检测常见的Web应用程序的安全攻击。
( ~9 P9 H( d. e7 R- n, U3 P
0 t  H, X4 e- z$ K" b' Y7 U$ }. p) R, R5 f0 \9 Z
Automation Detection(自动化检测)2 B+ _$ H7 a3 b4 \( U; _
检测机器人,爬虫,扫描仪和其他表面恶意活动。
  L7 _3 V4 A. J6 j5 i& D
9 F' j5 u1 ?6 Z& w* R( p; l6 O; j  m9 ?* ?
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)" L6 c* W; q" n
检测通过Web应用程序上传的恶意文件。
9 Y' o8 D% Z4 ]$ h
0 y. L: I8 ?# z% K, `$ L" k+ J3 _$ Z: \3 l( s8 Q; W2 x0 K# I6 V
Tracking Sensitive Data(跟踪敏感数据)# i, q  K  E3 n' p
信用卡通道的使用,并阻止泄漏。! `" p9 X/ R! Z' ]8 q( f  I
- ]' d4 o1 A* @6 `( W! N2 S
3 h0 Z2 `1 t3 I; @6 H' X
Trojan Protection(木马防护)
2 T& ?+ o  u, A& G检测访问木马。
0 e# T4 L, V  D/ g. l
: N( n: K6 L1 Q3 y# D! E+ G0 ?+ A& U. F
Identification of Application Defects(应用程序缺陷的鉴定)
1 w8 X( y$ u% ~5 J' G- z检测应用程序的错误配置警报。, t$ w, C/ R2 L  k/ [! P

! _' r* m# i- M& [* R( i
- P' E! o! V4 [8 y/ T5 fError Detection and Hiding(错误检测和隐藏)# C* w; Z, k: ?7 @# N# w
检测伪装服务器发送错误消息。5 U9 f' I. r8 l6 ~& t* u; X4 o# }

: u" Z. {, o7 u. _& w: u) b5 G2 i0 U, L
安装ModSecurity) w2 U$ K0 o. g% ^  ~# z4 H
软件基础环境准备
- ?  o' g* w. K4 ]( z下载对应软件包( y+ a4 D: z+ p, X. J
$ cd /root1 _; _/ e$ b$ `/ Q, T8 v3 w4 i9 {, A
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
/ M: l$ V; f5 ]5 i. j4 T: p$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz6 h) @5 v  B& I4 Y' f# A+ C' o- D1 R$ C3 Z
安装Nginx和ModSecurity依赖包
/ \* T6 b2 d; @* y, iCentos/RHEL; j5 }4 O) i8 x+ g$ I

; F$ z8 g6 S6 A; L
) r% Q. J/ C1 m( T: i1 D$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel8 Y* F" H6 s* S! k3 G( D
Ubuntu/Debian% r& I& G* w) W+ X/ r

  b8 \3 U: D) E  c- |/ |# t3 G& K  h, v& e4 o6 Z
$ 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
( [4 d; r3 R& g: _* E编译安装ModSecurity# ?( ^6 V9 R) m( Y% b9 `6 W% u
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。6 P# \) T( c7 J1 w9 [
' e; L- \9 L9 |: t& F5 c

- h; |& I" O: }( P方法一:编译为Nginx静态模块2 S* F' r; n2 P, n/ `3 w' s

/ Q3 X. N& b/ m: |. f% }6 a5 n' d. {
编译为独立模块(modsecurity-2.9.1)  ], ~1 P) ^$ u
$ tar xzvf modsecurity-2.9.1.tar.gz/ K. y' I8 D% z: B- U$ ^
$ cd modsecurity-2.9.1// K, l7 Z! h: E* M- U) W. n1 k  ^! O
$ ./autogen.sh% f6 ?5 c; r  b* y- _% D. U2 Q1 v
$ ./configure --enable-standalone-module --disable-mlogc' u3 {1 p* b# x
$ make
* H+ \7 Y7 r- Y3 R9 d编译安装Nginx并添加ModSecurity模块. A5 I9 e$ [, s
$ tar xzvf nginx-1.9.2.tar.gz. T0 q* V5 \' ?4 u/ @) |
$ cd nginx-1.9.23 [% G+ X! P8 b9 R
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
: k5 @: q, S3 w9 p9 `2 t$ make && make install% p0 S6 [6 q" U  G# f
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块, o3 I: E) }5 J% D# r' h  i
+ ~& l; N: C1 \* B4 w+ J/ |
4 }2 P$ J3 }; G" G: h$ V6 I+ Y' X8 g
编译LibModSecurity(modsecurity-3.0): K" G- d/ J9 O- f
$ cd /root
1 Q  ]! L" h% B- w3 S1 v$ git clone https://github.com/SpiderLabs/ModSecurity+ w6 ~( D; a2 O
$ cd ModSecurity' q, z# B) z6 l" [# b, ]
$ git checkout -b v3/master origin/v3/master! C; z! f  ?& i% f  w" f7 y4 ?
$ sh build.sh
; g! Q8 {6 d+ Q$ git submodule init
3 v3 d* N2 P2 H2 d2 m$ h* {0 Y$ _$ git submodule update
* w% y% _; V  }1 R4 ]9 m3 z2 j$ ./configure
6 F3 Z6 E. y. |$ make3 {  t. T2 R9 N$ _- A6 n, H1 A+ E1 C
$ make install
' v( @$ p* l" G0 x( ALibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。- [5 {0 X8 v8 @; {3 s. ]
* T, d0 P+ T! V- o1 P0 s7 \
0 I! R# F' L! e9 G& d( U
$ ls /usr/local/modsecurity/lib
' W& y+ J2 D. Z$ [. B- S/ A+ Nlibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
; a; C* @( V# |( K5 g编译安装Nginx并添加ModSecurity-Nginx Connector模块
8 T' \7 ]5 s; D0 {2 J使用ModSecurity-Nginx模块来连接LibModSecurity- ~9 w, ]8 P6 Z0 C% l

9 y% |' J/ |# B" N6 Q
  Y7 {( X4 S1 I$ cd /root  {* x  C0 e( Z1 G; F
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
9 _; ]% r4 [% ]# f- O: J$ tar xzvf nginx-1.9.2.tar.gz/ J; `3 }, n9 ]  s
$ cd nginx-1.9.2/ J- e* t' {% H- U$ U" F3 _
$ ./configure --add-module=/root/modsecurity-nginx) c. ~* z' p& M. f% T
$ make% ^. k# k7 d9 ~  `* Z
$ make && make install0 n4 i" u$ `7 U% @5 d; g4 T
添加OWASP规则# d; P* w# P9 U5 \8 Z" G
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
( k/ I  X. z) x3 O8 s5 |9 R3 M6 }; N5 a8 K0 |
3 H- }! ^, {3 G9 J/ f
下载OWASP规则并生成配置文件
3 j" l4 a! i2 h$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
% G0 o* C2 w+ t  ^$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/2 |0 T; u" V3 D% M: v( s% n9 x
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs$ o+ p4 n2 L1 Q4 Y$ \
$ cp crs-setup.conf.example  crs-setup.conf' {9 x3 w+ f+ m8 a5 \# G* O
配置OWASP规则$ Z* s* x/ |6 K, Z
编辑crs-setup.conf文件" n: B. R% g. M4 ?8 j, }$ p
& x. `% L. w, q# E# p' B

2 k4 q! F+ ^/ y! B, I( I3 Z$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
  O6 p& i( P+ `6 M& x  p$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
) }7 L9 ]( t, U0 o$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf0 R" W1 H7 H# @7 C# \" ]
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf5 W8 b5 H/ u; z8 C3 r
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
4 n% N3 b8 O0 C- a- r+ [5 P- z( Q( n1 F. i& d0 b
$ N8 w- V( c8 m/ s( u9 i
启用ModSecurity模块和CRS规则
( p: e) }$ _2 [. J4 {! F复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。  |  d& G4 [$ w5 s
' _, k- m3 V5 g& l* n, ^6 \% }" o

9 c, \, V$ W* [modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。% F5 o# v4 W9 G# ]  D) G; Q
# D3 o' B5 b, Y, h9 p$ F
1 {) _( k' ~* K' j
$ cd /root/modsecurity-2.9.1/* }) E# `# e: K) a# m5 X9 x
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  * S) q  s6 D: [' ]- n! p, l
$ cp unicode.mapping  /usr/local/nginx/conf/) t# P( S  S, _& Y4 g3 Q& |
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
: ^: g7 H0 i& h# p8 G' ^* p
& v4 L$ Q# d% E* C$ Z) |9 K% L. O1 V8 \1 s$ K; z; m& P
$ vim /usr/local/nginx/conf/modsecurity.conf6 g% r: B* `) j% E- r
SecRuleEngine On4 c7 u( {$ X& Y% A. e0 k
ModSecurity中几个常用配置说明:
: |: [- |4 u1 q3 m
2 K/ r5 G7 Z* r3 T( z* t+ [9 Y' n/ x) V
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
. v6 L$ S3 d) W' e
! D0 W0 G! ]0 L; P+ {$ w6 w( n5 o$ Q/ u+ ^7 e
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
! Z/ r! F& p: w5 i. m; `
  l9 T! r# k/ {1 O% `( x2 a  h( Y8 k) u9 r8 g( I
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
* {6 n9 v* ~/ t' |9 f
: `% J' F5 b% F- l& Y
& a& e. D- S' _9 h4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
' [' ^& C. r# k' U7 U; u+ z: U0 N8 T* n! ~) a, s  a

  v+ m' k0 w" M在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。$ @* M3 B/ ]) z$ `' q% ]
) ^- L3 B8 ^. v- @& M
+ q; k) L5 t. W5 r& I. K
3.x版本CRS  y: f% M8 L; Q, g- j
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
9 K& G, E. _" |4 H! U( {4 j# 生成例外排除请求的配置文件
* I3 {; I3 J# q! e" Y$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
' H( v/ {2 }9 w  K) A/ l0 P9 ~7 s$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf& g3 ]+ R! @4 H3 g0 d3 y! a6 f. K
$ cp rules/*.data /usr/local/nginx/conf
9 P8 T5 l. }) m# X4 d1 j* v为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。* E! Q& f. l6 B' O8 K* V

6 S+ J  D) p  Q% c* W* B' c
" f3 q$ G; c4 z! s+ g4 w& E$ vim /usr/local/nginx/conf/modsec_includes.conf! F6 W4 }) }9 h: `( r" h) b
: x' R2 S5 C1 X4 ?4 }  W, D
[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

7 y( T% J& {+ m, l, A( l& E: C8 a. x: ]" N- X
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。  m5 H, y7 v* L0 ^3 W4 J
! ~7 s1 Y, x3 s1 X" y5 I

$ J% p. F" X4 Q配置Nginx支持Modsecurity& o) U) {" ?. B% f: Z$ r
启用Modsecurity, j* K0 f; G/ O& f) i  D! ^$ ?+ `
使用静态模块加载的配置方法
* L6 G$ n9 |5 E6 f; e在需要启用Modsecurity的主机的location下面加入下面两行即可:/ {5 v. T) ]8 ?( W8 d
9 ~3 H% j9 m0 ]. K4 N6 g

' J/ m6 v  m7 p5 z9 {" L; P8 `ModSecurityEnabled on;/ }- m2 P7 d$ t' e3 h/ q+ H
ModSecurityConfig modsec_includes.conf;
) E7 ^+ {) K+ P( F) j5 {/ o修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。/ O& x& H3 \  H$ w$ f2 H9 M; U

, |  l  g  x" e1 o% i% X" ]1 N3 n4 Y4 m3 _
$ vim /usr/local/nginx/conf/nginx.conf
1 J% L5 T  q8 Z/ Z. i4 N# y( \" }

3 {6 ?& r0 I9 q  m2 jserver {
2 s; q2 \* l' ~3 w  listen       80;
" W1 S  P$ \3 g. ~& {+ a  server_name  example.com;
7 Y5 _0 [, m8 k$ x% y
# d' d% S# M& [6 r( [0 h3 u
. s' _, G! r. `6 |  location / {
# N) m2 _0 |6 `5 h    ModSecurityEnabled on;+ \( h  D' Q0 n+ Q: F) Z
    ModSecurityConfig modsec_includes.conf;; R2 h0 M$ w1 X5 m" o
    root   html;1 {- d9 T7 A4 C0 R% t. m3 B! j
    index  index.html index.htm;. d6 X/ g( {3 ~- c
  }
( L: X: m  C4 L# M( W, `}1 j% T3 d' r8 |9 h
使用动态模块加载的配置方法9 D8 b4 d( d3 s
在需要启用Modsecurity的主机的location下面加入下面两行即可:
. p8 c. Z* A1 \  s( F1 E# ]3 L2 v) q5 ~6 C* V; ?2 X7 d8 {

9 [  w2 m# v' V+ Omodsecurity on;
- W' Q* `; s; d, N/ f3 Smodsecurity_rules_file modsec_includes.conf;
1 E0 M5 p; ~0 L8 n" l  C) r. B" H+ \修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。; l% A+ ~6 r3 i" E

9 i$ B% ?; n; I2 X+ O1 }0 }
5 G! ]9 ]/ Z% Z: D$ vim /usr/local/nginx/conf/nginx.conf
. k# K6 d* Y& }! n7 h* w/ |
3 [7 i8 Z. s9 x  f( s
, P) z0 s% a/ X6 b4 b. mserver {  g# Q8 c+ G3 x' s0 K2 q! T
  listen  80;
2 x8 u* ~! @3 M9 \2 \( k' V( _  server_name localhost mike.hi-linux.com;0 {/ t( x; D: }: X' S. D& P9 x
  access_log /var/log/nginx/yourdomain.log;
5 {& H- I$ }# V8 M! m& _7 u6 W, \  c2 @

9 j0 F, e  V; b: I+ L  M  location / {6 j7 Q9 b* m. N0 V
3 ]8 r" G) T! b% I

, ?; }4 V# `! y) f* v! n  modsecurity on;
; J! j7 k. F8 }  N# ]  c- q3 R2 i  modsecurity_rules_file modsec_includes.conf;" X, F  T* k: X0 x, {9 n4 b0 N
  root   html;
, z5 @6 {  M& e( H7 W  M6 I  index  index.html index.htm;" H$ G7 [& P" D" s  P. R
}
! m( l5 |2 Z! O' O}+ ?) _  U- P; ]; }% Y
验证Nginx配置文件. o5 B" `  ~  V, ^; D6 K4 ~+ P
$ /usr/local/nginx/sbin/nginx -t
- [/ h7 X) e& k8 j! H! ?nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  v2 r; Y$ M9 V  o# k6 E1 anginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful2 F, p; a, u0 e2 M
启动Nginx- P) o$ h" W( D: N
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
4 b5 P$ W: O6 X1 C4 r, G/ ~2 b" @8 d  [3 o

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

% V. n: Z- l: e6 o! f0 W$ S' X

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-11-7 15:48 , Processed in 0.081986 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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