找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9938|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。. A6 [/ q, R5 t' G$ y5 Z& V( }
, \, R" r1 `& s

0 g8 @2 c( k+ o7 E在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。% N# a, ~, x9 q1 J- a

* N0 V+ S6 m' [8 M, q2 L2 i! _% Y! g6 A: F! E: s# k' R
什么是ModSecurity
# e3 @5 d8 l' VModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
7 r- o+ S+ l* N5 Z5 G, i2 M0 Q# ^2 }# N* @# O  ~
( v$ L$ e3 h2 ]& G; ~$ s" O" M
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
% |8 h& D. D' H$ j3 n, E) e( a8 x- ~% ]2 C

1 v5 o0 P0 @5 D! L* z( I& W2 |ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。: I' s; C  e; P0 p! @
% w' O1 I) X# ]) \( w

! D# L% H) c! b6 Z3 h+ B官网: https://www.modsecurity.org/
# q( {0 ?! d8 M" K
& E0 ?: Q- u. A; V+ a- _4 x. x7 h! C" C; V- g" R
什么是OWASP CRS
3 P7 P4 u. `" ^) T. Z1 ~' r0 |OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。* ?. g& q# O- C3 p% P7 g: R, F  r9 h
% [* O$ g! N3 S

/ \) w+ V$ X; X3 N2 P5 L( IModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
. w6 x3 X& |; p) u; m2 k! j+ }2 [% K, ?, H" e# p6 `7 E
) Q' ?$ O* U. j* L
HTTP Protection(HTTP防御)! |8 s1 m3 R% F! b1 Y" k! g' s
HTTP协议和本地定义使用的detectsviolations策略。  {& s$ T+ y# e

( r- m8 M  L: o6 m6 [
% i4 S* a/ \8 m: a# {8 OReal-time Blacklist Lookups(实时黑名单查询)4 r2 d& h2 \) @; f& c5 V% N$ I
利用第三方IP名单。7 f8 k4 \) p6 S% u6 r1 V4 k- j
: O. a* C& j' V' ]* ^+ D* f# u4 J

3 H* x; `8 b  h6 Q8 `* g/ CHTTP Denial of Service Protections(HTTP的拒绝服务保护)4 |5 o* Y$ ?2 B5 |; w
防御HTTP的洪水攻击和HTTP Dos攻击。1 J; H) K' `( t/ Y+ A
3 `$ L2 x7 ~! X/ N1 t' X; x

1 ~% |. J: O4 w; n: a) u& P$ GCommon Web Attacks Protection(常见的Web攻击防护)7 H" i$ z* T4 n! [+ n! X. M
检测常见的Web应用程序的安全攻击。) K0 U; |6 c3 J4 x& @

' @. R- |+ A, p/ k( p- ?% }$ ]$ w) U; U( \8 {- r
Automation Detection(自动化检测)
" x% `" l: Q7 p$ D9 g8 Q' h, [检测机器人,爬虫,扫描仪和其他表面恶意活动。
0 o  O7 n+ S0 T4 d$ l  V: Y; t2 `5 U$ n; E) B  W; C" X8 |
  S; a) L1 u8 P: R( v; K
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
! f' ^2 x* y! S( H4 K检测通过Web应用程序上传的恶意文件。$ ~; R5 V1 G# M# m0 G6 N* J. Z# E; [
2 ?3 H/ z: B0 l( [& {

! i. C& `0 a9 C! XTracking Sensitive Data(跟踪敏感数据)
1 w' W( ]# E7 V% a1 a, Q" S信用卡通道的使用,并阻止泄漏。7 ~6 ~. m' M# K
( `; @; D1 ^7 L; M" e) [
! P/ H! K0 B. O7 Q$ O
Trojan Protection(木马防护)
& o" V' g9 E( c9 o3 s" a' m# ^检测访问木马。# d7 q6 M4 G. [, e
3 r* E' H. m8 Z, q$ {' G
4 E* c5 D6 c# b/ F  C
Identification of Application Defects(应用程序缺陷的鉴定)
. i6 H$ @# J! S检测应用程序的错误配置警报。# F# d$ P" ]- R: T; w

  p6 O+ g* h# F( }
! ^! [8 x& Y% Z: M, wError Detection and Hiding(错误检测和隐藏)
1 f: \5 t: W8 N# ?6 Z0 r3 q检测伪装服务器发送错误消息。. Q* M' i  a, O& h3 m, A# V
1 L' F* F7 [9 Q! z
/ Q. o9 [0 Z6 W/ O+ M5 L3 I5 f
安装ModSecurity5 T! Z4 ~" G1 D) @* @
软件基础环境准备# g- @1 ?# k$ d' z
下载对应软件包; Q9 y1 b, U  l, M9 r, d5 h
$ cd /root8 I' E8 G, p5 c: k3 x/ X
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz', C3 y% d6 _5 P! }6 p! 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
3 v9 p5 l. F3 t' @4 B/ R6 Y安装Nginx和ModSecurity依赖包
1 b0 j1 V7 J& F* j6 m; i! o% YCentos/RHEL( Y; t' n' }7 ^3 [# ~0 f5 h' t  x
! s% w2 P" s& q9 }; a

" M  i5 x9 q% V6 p# Y6 K- A% @; W7 E$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel8 D9 g/ j1 R8 A; i
Ubuntu/Debian
! L2 |. Z# z' C/ h& N! z8 v
7 R" V3 q' s1 I& R  |( n, j0 k, s$ E+ K* n% _* @) c4 M
$ 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/ ^9 a4 m$ W1 }  n) E: W( Y) C
编译安装ModSecurity1 Y7 q* x* Z" O$ q9 s* x* b
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。2 j) r" G" R. x( d
; U6 @# F7 D4 A
+ U7 N8 A! d: V
方法一:编译为Nginx静态模块
+ N1 b3 T* k$ s; C% T5 p6 k3 G
7 U/ U; S  M; G/ Z! X, B) V8 g7 t- L; s
编译为独立模块(modsecurity-2.9.1)
$ T) y; j4 s# p. @& E8 b" h& h. v$ tar xzvf modsecurity-2.9.1.tar.gz+ j  k! r5 j) |0 \, W
$ cd modsecurity-2.9.1/
9 \: U; d+ x9 ?* N) B' a$ ./autogen.sh
( z9 X: f- J  X& ^$ ./configure --enable-standalone-module --disable-mlogc
8 A5 @+ z) m+ G4 C# ?$ make  L6 W* l* R' z+ E; J! N8 \4 N
编译安装Nginx并添加ModSecurity模块
. b  q- i1 {9 t# Q$ tar xzvf nginx-1.9.2.tar.gz
2 r+ q; X8 d- v$ cd nginx-1.9.2* N/ J  E& x+ J* U4 [6 y
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/" b8 N  x# A! B: H9 Z
$ make && make install( O7 ]% J4 j7 j3 M1 n+ o
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块1 w- O& g- h9 B3 s5 u  w9 D) r
9 u/ f5 m: Z* x: F) ?( \. n

; x  V1 ]+ X& [编译LibModSecurity(modsecurity-3.0)# X$ e0 g0 H' O6 O5 @
$ cd /root
, x6 E1 T% K; Q3 f1 ~8 L0 i% L$ git clone https://github.com/SpiderLabs/ModSecurity
9 l3 o! g% z2 ?( m9 T9 v( N4 Y: D$ cd ModSecurity! h7 b# V5 G! b2 L$ Q
$ git checkout -b v3/master origin/v3/master  ~( r# ?' ~* C- a; q  K" {
$ sh build.sh
" V+ S4 J+ M2 z2 S4 t& i$ git submodule init
3 M% ^' e/ E3 t9 a$ git submodule update
5 n: j# `# j* V$ ./configure
! S4 Y( {; i! M$ make3 ?* m2 t" S+ q; }0 F1 O
$ make install
+ V4 c. w5 ~4 i' @( ~2 FLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。5 D) L4 K$ o1 W
0 i: p7 h5 T  W6 i" D. ]8 w* `& m) |

% _( ~0 F+ ?" L/ s3 @! u$ ls /usr/local/modsecurity/lib
  N' R. \( P2 t  B- K8 @libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.00 G/ e' F5 F4 A# i0 A
编译安装Nginx并添加ModSecurity-Nginx Connector模块, D3 M9 A/ K" r( G$ Y3 r5 z
使用ModSecurity-Nginx模块来连接LibModSecurity
) [. j9 a. \+ u1 x$ [$ a
, g7 M: D& B8 D# k7 M8 P5 T# b0 k5 ?0 s5 a+ B1 R4 C
$ cd /root6 b) Q8 b2 Z1 n% Q
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx4 c$ M% e( V( T5 j8 g
$ tar xzvf nginx-1.9.2.tar.gz
* N# D2 S/ O. t5 i& b  @$ cd nginx-1.9.2" `" W( m4 J$ h0 q& Y
$ ./configure --add-module=/root/modsecurity-nginx
0 ^$ H: ^, T/ o$ n0 |$ make  M0 h4 H8 V1 q6 M
$ make && make install0 S' H% v$ x0 s* y- P9 M, _5 J
添加OWASP规则
- J( }" e( l0 h, `ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。, R0 j$ x  f% e0 z$ Z. l) ]6 _
, F/ `7 I' \3 h4 J! @5 L

$ V# y6 p% T! B% n1 T下载OWASP规则并生成配置文件3 r6 {9 i( x; \
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git  v: I  v4 @* B7 c7 r6 B# f
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/! V5 H3 d9 |% `' ~$ e6 n
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
" @8 x, e2 [5 E$ cp crs-setup.conf.example  crs-setup.conf
. Q% \1 x* c  `/ P3 F" B. E配置OWASP规则1 x/ o9 q  }0 f' }4 j- q" h) S" H  ?
编辑crs-setup.conf文件9 q1 p+ F, j, S6 l# u6 C& k, `9 g

) Z; [& m) t& E2 N+ {8 C1 E, t! n8 q5 S
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
  _6 c/ }: b4 R" c3 ?- b$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
; E9 g% i  b7 O# H$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
, u$ C7 i4 w  n, |  q9 o$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf5 L* F$ e: H+ x" l1 }7 n7 Q/ o
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。$ G, L% v, o+ c" j3 \4 D, ~' e
5 Z) `) u4 q/ D$ Q! a* G
* D: Q+ f; L- O, s7 h' f) N
启用ModSecurity模块和CRS规则* P$ Q4 ]! p& u# n1 ]
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
: F7 J# j! A( c
8 q7 s* K* d6 E1 c0 P' k
; T$ K# p1 e; B. Xmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
  f" U6 c2 V# V1 K% o$ y
+ `1 o" H& ?' D; Y
: |! d! g* L3 g$ cd /root/modsecurity-2.9.1/
$ Y! S, I& I: ]7 s- l$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  ; E9 l, E* ~8 H0 r- ^  T
$ cp unicode.mapping  /usr/local/nginx/conf/! u$ L% G# }) ~" ^- T
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。( O; \; b1 e9 v* t( Y* K
! q( o; [' U3 j
1 p) S5 @8 Z9 i8 z, r
$ vim /usr/local/nginx/conf/modsecurity.conf$ {# j1 y- X; g6 y- f: q8 W$ x
SecRuleEngine On2 z% O- Q9 F  g' E5 K4 @
ModSecurity中几个常用配置说明:
  m% d' S! ^9 e" x
8 @8 v# B4 g; }0 c! q) R& V0 T; w9 u
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。+ p' ?8 J' Q' K% |' e

7 N. A' F9 C# Q" m5 x9 ~2 P) _6 _! S* H, G- O: [# I
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。" o5 Q. n7 E4 Z6 S4 @% X# S

+ ~( e  r' x5 I, B7 l8 P
+ f' \, f$ T( W4 I" I; Y$ m3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。9 _) n3 X/ J! P5 p
- b! X, f, j' P/ i
) m" F, ~' \: z' T) I4 f+ x
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
4 W5 e- q5 `9 p+ v4 E
4 [+ H6 V7 |  \. Q# \- Q0 q  k( G; p* }
  W9 T- O! D" X" m8 M3 Y在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。0 p9 ^2 G5 v( l/ d' Q0 w7 j

# F4 q, M- f- G7 v5 K, Y% _2 }0 k6 {) f( u( ]
3.x版本CRS. v/ K1 Y8 y& ]3 O
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
1 C- L- M" W$ e* H  y6 b# 生成例外排除请求的配置文件
' U1 `4 f  X7 p5 c: `( M$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
, f: E, A( n' W8 v3 S# [& b7 i$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf5 t+ Y/ E. e( D" P! A
$ cp rules/*.data /usr/local/nginx/conf* v) U8 Y0 Q) B# Z+ B2 J' ]
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
1 ]5 Q* G8 g- H) n. K1 Z; @/ b6 v4 I) C- |3 G. l, T" y' Q7 R- [+ T

- l' g% a- a- G$ E; q$ vim /usr/local/nginx/conf/modsec_includes.conf: Q* i6 a, b+ U6 o* \- |/ a! P1 u# P

& a8 E* D0 ?& n2 b8 B* K7 b' L" ^
[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

, Z  m; X9 O: k6 D  j" T- f! v4 Y, b# A7 H% d# o4 N7 V
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。3 i( b' Q. {* t& E

  w  Y2 N6 Q# r& K& u7 t4 O6 o0 R8 ~$ R% X5 `/ J3 t6 N
配置Nginx支持Modsecurity
( y: v% H, k  [, J& J: }2 j! G启用Modsecurity: E6 t) |& s  K
使用静态模块加载的配置方法+ ?* b& R; h0 U4 ~8 Y
在需要启用Modsecurity的主机的location下面加入下面两行即可:* b1 G% O+ K& l& C+ _# @" m

. L; z7 p7 i, @1 ^' ?" c" \4 [3 I- t' I! l: V$ ]
ModSecurityEnabled on;
" B1 T2 f6 Z+ W# r+ @: RModSecurityConfig modsec_includes.conf;
$ J& J* F+ i0 y1 M修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
& I: ~( z$ H0 a( v  ]) E6 F8 Y/ b1 F

/ R- t) y$ j! J0 n4 h( A1 Z$ vim /usr/local/nginx/conf/nginx.conf
1 H: J7 v% Z& C- n  b" u7 F8 p
0 I% n! K1 Z: \( W5 ?* b! b% F* J. B$ `7 i. H. V7 {
server {% C/ D& F4 p+ J
  listen       80;8 D2 E' \, C0 p/ B
  server_name  example.com;
. k* H( H+ Z' I7 z  E, b
' c% S" w& |/ I% _. L  V
. x4 Q* e5 B6 z  location / {0 n: `9 }' K6 F
    ModSecurityEnabled on;
" C! b- I7 {: `. a/ \) i! `7 j5 g    ModSecurityConfig modsec_includes.conf;# ^4 O5 I0 M& I( E) q9 }- e: l$ G2 v# V
    root   html;
4 u) h$ `( J" S' w, C! F* H    index  index.html index.htm;5 x. {! l: A% U" ^# e
  }; v& P' j& e, _. a9 }4 F. F( o
}* j0 T: ^7 g, w& Y' [, `, }
使用动态模块加载的配置方法8 X; @2 H+ W' g1 k4 \
在需要启用Modsecurity的主机的location下面加入下面两行即可:
' n9 ]% m, L+ e( \  f1 ?! F5 j, D9 G4 p

# o" h# M2 r! c6 I0 ?1 E2 \- Imodsecurity on;! C4 N( J. Q! S4 k. }
modsecurity_rules_file modsec_includes.conf;
  |: _/ P# Y1 \/ P& o, _6 x! |修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
( m8 }' t5 W- H* j
0 v2 I: M- S& |7 U# \5 x( \: L/ W
" m: w$ V+ V2 i, r3 {$ vim /usr/local/nginx/conf/nginx.conf
+ F+ ^# i# D/ m# h# C  J+ i! s
4 V( U. i8 ]0 O
, N5 n; [! E( Y& U* m3 Dserver {1 e1 F+ q$ j6 l
  listen  80;
7 r! }0 ?# x: V  server_name localhost mike.hi-linux.com;
2 g: L* _, ^# p# c( _- J$ w( ?2 p  access_log /var/log/nginx/yourdomain.log;# g( L9 c, ]" P# ~0 H8 q+ g% k

1 R6 A0 }" c* k8 u& B- y* G
/ A, w1 r# f. \6 j0 v9 c0 H9 N3 @  location / {+ O$ J% O; R- y) F, g$ [7 S! J
6 k7 a' s# Q: V6 K4 v
9 Y* r. J: U& L
  modsecurity on;% f% ^0 b4 N8 W) ~
  modsecurity_rules_file modsec_includes.conf;9 U9 Z4 g- O9 o9 s2 S/ _% i
  root   html;
- r) E; a! k+ r$ v5 L  index  index.html index.htm;) I$ E+ A: Z6 L$ L8 Q
}# l% r7 L$ _( X
}
* W, ?- i  l7 j验证Nginx配置文件1 d- Z2 S  L# d. n# S  i$ q
$ /usr/local/nginx/sbin/nginx -t
7 V9 Q8 U1 Q0 W  m( Anginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok- _- f7 w! |0 h& V9 X4 _0 q  s8 [
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful6 P( v0 U/ M# }2 p: ^
启动Nginx9 k: y, x" g" ^$ p. n7 f$ I
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
2 P/ Y5 `0 J7 g$ a
6 T; n; f9 Q3 b7 ?$ 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能够战胜更多复杂/未知/混淆的攻击模式。

  g/ W; ~5 ]6 E. A3 Z3 i, J

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-1-13 01:25 , Processed in 0.049448 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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