找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10222|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
1 T: e# D; n/ H: w" n4 H
/ |( X# Z# T& V; U" B
3 X0 h% r# J/ {9 A7 J+ I3 d6 F在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
6 r4 Q; }. y  F: h$ _! _1 d
) `" }, C2 W' J9 ^
+ y1 W5 f1 ?. E3 ?2 r, x. M什么是ModSecurity
  p4 P; q2 t3 EModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。4 b1 z5 O! o# F+ `3 K* _

, D, R6 Y- l* o! @$ H. Q
" _( x: {5 N+ O4 v5 pModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
* _$ `# S& L6 ], `4 ~' k' h4 D# Y: C& E5 f- [& R
& _( j; {* b: l* N+ i3 V
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。6 Q3 l1 [% Z. K! d+ F

- u* p2 N, x5 [- b/ l# S
6 ?% B. s- l1 v  I1 B6 n; p3 `$ v官网: https://www.modsecurity.org/
. I5 C: A( |9 T5 n5 R+ q
! k* B: A+ Y0 }( t$ t* M2 Q
+ F$ [! Q# n8 c! v0 [) g什么是OWASP CRS2 e- u( ]  e0 N  c
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。. Y2 M, T' y( l

" {% y. G* Z) P: h& P; ^2 h
3 s% ]( S) Z" `+ t0 m; @ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
  t" i5 ]' |3 l% D0 P: \" B5 W8 m
. s& T4 j  ^3 `# [
: Q. H# d  F$ H8 M9 LHTTP Protection(HTTP防御)
! v! g4 V0 Y3 {* o: I/ ]9 Q  h- g2 dHTTP协议和本地定义使用的detectsviolations策略。! p, ]' }2 ~  T5 e% i) R- |
/ a& ]" v. {$ Y
5 H" A( j1 u: Y  B
Real-time Blacklist Lookups(实时黑名单查询)- F3 R9 y1 v. |4 r  s
利用第三方IP名单。! N7 N% `, f; U2 {* {2 r+ B2 t
: b, @5 n# L, ]" d+ i5 S( X

! y- l5 A& B' o) eHTTP Denial of Service Protections(HTTP的拒绝服务保护)
! Z; q: n  N- `4 b/ K  S防御HTTP的洪水攻击和HTTP Dos攻击。3 V$ F$ l8 K! [, I
! O/ C5 ?/ Z$ ^7 s% Y& r! e# _& U

; G6 l2 v- X; i2 u- Q+ lCommon Web Attacks Protection(常见的Web攻击防护)0 u% e0 \' R2 Q
检测常见的Web应用程序的安全攻击。. p) F; i9 u" ~9 G; K) Q% o; p, W

3 E  s7 I9 f/ M% K4 y, Y$ H! B
7 u% a3 w' k* \) d# d2 g& [3 GAutomation Detection(自动化检测)
6 n0 k0 s0 ^9 T1 T检测机器人,爬虫,扫描仪和其他表面恶意活动。! e& j+ Z8 F, W& h: l5 ~
  P7 L2 I3 Z6 U; z+ c
/ @  L2 A! x* N" }
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
6 l8 t( i7 S& V3 N$ ?) J, w9 h6 g检测通过Web应用程序上传的恶意文件。
% ?9 X- |3 @% q3 y) A: Q7 z/ S4 \! n: D

7 l0 t  z" |* D8 f* _, g0 S) VTracking Sensitive Data(跟踪敏感数据)8 ^: X* o" Y- c( a0 B8 c6 [
信用卡通道的使用,并阻止泄漏。
( l, Q, G4 @, c' t# P
" b5 Q: g9 ?1 `$ j  k: a  x
+ f. ^1 |8 y/ rTrojan Protection(木马防护)
( O6 k2 T5 |3 y" w3 G. d6 [/ [1 V检测访问木马。
6 H: z! [* }6 q# {# k
$ N" g0 Z4 w( L4 E2 K' `' [+ q" U) \* r9 C+ N- G" ]
Identification of Application Defects(应用程序缺陷的鉴定)
( m- _2 E5 `0 m$ E检测应用程序的错误配置警报。
% z8 \/ E5 p; `$ W9 P
3 {7 ]3 ~, ?* P1 w* ~! {
# q8 ?: u! I' [* }- KError Detection and Hiding(错误检测和隐藏)% }/ }9 S( A) H! Y4 U; o1 y
检测伪装服务器发送错误消息。
3 j' y. @: @9 R& f
" B7 w& `6 h- S' g
( V( K7 c/ |- ]9 D0 m# l安装ModSecurity
* w; d" b* P2 e4 ^( n$ B& `0 Y软件基础环境准备
1 w- h) k% x. y4 |( [8 O5 y下载对应软件包- F1 H$ ]/ W. K6 K# B
$ cd /root) [$ ~$ A5 E4 B& R- f* c, X( x
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
1 N  Q! k3 o: z! K2 R0 M4 r$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
5 c; v2 J$ Q6 `* r+ N/ B3 B; M安装Nginx和ModSecurity依赖包( a* ~& P) d+ q! o6 ?6 P/ }! D3 P$ k
Centos/RHEL
) `- R/ m  N- o8 m5 d% k
/ g5 g) L3 L5 i/ H$ _
& g" S  r3 y' c9 q5 b. R( }1 ?$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel3 B/ B# ]( w6 @
Ubuntu/Debian# y8 T. X% i, Z
) |& o7 F# w" F' z
4 B( _0 a% q/ A
$ 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 ?3 N3 l1 [! M+ ]* T9 @) J* [, t7 [
编译安装ModSecurity
/ c; P( \/ a- Y. t3 ~Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。' f( `  V! H& Q) ^8 L! Y$ i* L

; Q/ Y% v! c, V  [) b5 E! E
2 t5 ]* `1 ?, k9 H3 u方法一:编译为Nginx静态模块
( j- L% d) G8 i% \
0 @% L7 u) C) W/ m
( p, u- U  Q. s8 e; h编译为独立模块(modsecurity-2.9.1)3 g$ Z1 q2 C" i& ~, f0 h
$ tar xzvf modsecurity-2.9.1.tar.gz
  F; b" K6 M  \6 H& b, C. p$ cd modsecurity-2.9.1/9 E* V- p3 \6 H1 M$ a" s; j  K
$ ./autogen.sh& k% X7 `' O6 W- L8 c( P
$ ./configure --enable-standalone-module --disable-mlogc
1 k. i$ G$ v* H2 T% Z+ d( u$ make
1 V' K7 l/ k  h$ N0 j, b; P编译安装Nginx并添加ModSecurity模块
. }, ~2 B2 S9 G4 v) }( `* N- ?, c$ tar xzvf nginx-1.9.2.tar.gz
0 e' \5 \' o5 Z% U( M$ cd nginx-1.9.2: ^- k' W& g3 _0 y  _
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/* T( e' @- h9 m4 `- k/ ~
$ make && make install
: [: |1 S* r8 R方法二:编译通过ModSecurity-Nginx Connector加载的动态模块: D- _6 B" M5 F' X/ m7 W

# \' r& k, t' {  l) R4 n1 \# d- e( N
编译LibModSecurity(modsecurity-3.0)
1 v  v+ o$ Z9 h$ cd /root
% ?- V) h( M3 p/ K$ git clone https://github.com/SpiderLabs/ModSecurity
2 y6 x1 f' w4 ~! L/ s$ cd ModSecurity& X* a4 `; e1 N6 m) X2 s( v
$ git checkout -b v3/master origin/v3/master/ ]+ E. b. Q6 n
$ sh build.sh  @7 m$ Q' P3 j6 ]' w+ |' i8 S
$ git submodule init
) M2 e; Z) f0 T, B8 E$ git submodule update
/ y" h8 h1 L" }! y$ ./configure6 h% {  [- Z+ {4 ?( d6 w# N3 E, N
$ make1 r5 R$ A+ b& W* ]& F; w2 i$ c5 ^
$ make install0 D$ t3 \/ q3 f, N
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。6 f: ]8 A4 O, J* z6 Q

/ [7 }0 g- o$ t) A3 d! b- A2 h) e4 \3 \
$ ls /usr/local/modsecurity/lib3 ~" _" Z8 O9 L% u6 W( T% @1 y
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.00 G& B+ T: V) \2 u3 g' M2 M
编译安装Nginx并添加ModSecurity-Nginx Connector模块
+ o1 W+ t# X. p! R: w9 |使用ModSecurity-Nginx模块来连接LibModSecurity
& _. H9 a2 A& d' c
8 z$ u8 i- S( x
0 D0 P! u. P9 A& @; l; g$ cd /root
8 {' A- C" Y+ c$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
7 t' p. V1 L  \2 z) F7 m$ tar xzvf nginx-1.9.2.tar.gz6 d" j; M, S; l( h4 h
$ cd nginx-1.9.2, a! v! M4 d6 D0 {# i
$ ./configure --add-module=/root/modsecurity-nginx; u1 k- J( ]4 o
$ make/ \/ ^2 u! F9 Z2 \6 L; o
$ make && make install  @' a6 G" H! {9 j' u8 M
添加OWASP规则# O0 [& L7 Y( g; m4 ~* `
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。0 K" p  N+ s! W/ W) f# ^
: b6 V/ \- z* K- n, P3 [
/ w0 d& |4 z0 k# ]  X6 [7 r* U, N
下载OWASP规则并生成配置文件
4 D! P7 t, C$ d8 }$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git' S! F" ~; w$ A/ f" s. ~- x+ C
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/, f( ?! D# P9 A' q6 d
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
2 v6 A3 V. y, @$ cp crs-setup.conf.example  crs-setup.conf
4 O9 g( R1 E( E: @配置OWASP规则9 x5 G! t2 P1 p& N" Q7 K# F
编辑crs-setup.conf文件
6 u1 H4 \5 b/ J! K0 q
% o5 P, ~' m( T
; _4 P, y4 H9 C' D5 R$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf4 f9 G% q3 \$ N; x
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf. [' \. y+ [, w, z; X# g' Y
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf* S+ @; J7 a. o$ R' S% p
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf; V  |) I8 m2 ^" ?4 h
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。/ k/ z1 A$ Q+ ?" a; n& k
1 B2 q5 @- t; i+ M" a
8 Q, Q! {" u4 u
启用ModSecurity模块和CRS规则
! o( e4 y; H; }) k& x8 |复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。9 r9 }3 t; O: e, V8 l0 `
& F/ v; o8 V# _: F2 b
! C0 l' \# ?) u6 |3 D
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
' c' R7 @& l6 \3 m7 `
" i5 s- a+ f& V6 ]2 n6 Z+ H! D- D6 w% b8 h; e3 h  u
$ cd /root/modsecurity-2.9.1/( I4 \. d* w) \. L
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
( h0 |. g8 t/ j: w; U5 v! |$ cp unicode.mapping  /usr/local/nginx/conf/& b1 |/ r+ [5 Q( t" n4 F8 u5 a
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
* X& x- O) A# o5 I0 k
. P# _1 s9 q( ]2 B% K' t8 g( j' ~
' m( ~+ |0 h8 Z  x4 e) l$ vim /usr/local/nginx/conf/modsecurity.conf
* n3 W: e6 |7 O3 F+ i/ {SecRuleEngine On9 p! Y5 Q4 V/ H+ f1 t) S
ModSecurity中几个常用配置说明:
$ z' n- Y. O! _3 e5 F. d0 t% M& I/ M. J9 v+ _  d& I2 S' s1 I+ M
1 b) l% j! D4 z. r3 I- D
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
; j4 `( T: A8 r' F5 f  [
! T% e7 K) X5 z0 O: C3 f; e
8 O. Y( Y! G7 i: S0 {5 m1 y# F2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
3 d- n, g# [! U- t( o9 K2 r1 ]/ {: Z# x1 `, D" \0 q
3 {! }' t' Q+ s( n0 H& p3 R" f0 ]/ r
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。! v( J: G; E" r1 C0 r

/ L% d) j$ [7 k9 }: F
8 L) u; Y1 `( m; g/ @4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。& W1 ~" H/ B4 N4 }, a$ E6 I5 Z7 L. Z2 J
3 J; o3 \7 X% ]! E
. j) Q- W' Z$ B% ~
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。! j" t  l* ?" `* t* A! j' \9 R

  R1 n- `6 I, C8 I5 j( t* u
, G# {' e. v) S1 z; O1 [7 F8 F3.x版本CRS
, O" a5 u+ @' r; J/ u; Q$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
! j. K! P5 V5 w- r: s" x& j: G# 生成例外排除请求的配置文件
+ ~( ~) M' [/ @$ P/ F4 r: i. n" O$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
) x  N7 Z% H, A2 D' w& _, x) `$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf, F" m+ j- J; ?% e3 g- y
$ cp rules/*.data /usr/local/nginx/conf
) v1 S, z& j0 B为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
* I" B8 \3 M4 K
' m/ n0 f# h8 M. s3 X: t( O+ u3 w8 s2 l4 i% }% m: t+ M6 B6 ^
$ vim /usr/local/nginx/conf/modsec_includes.conf% W1 g/ [) |: z" G6 s

& F- r5 q* H; X" B/ Y4 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

4 F1 x$ ~8 V, h; o1 T# w7 \, _; G+ j6 L# L. A
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。- r# O" @. O7 S+ y

2 v- I! K) T3 Q& I7 X% i# ?2 M" b4 n
配置Nginx支持Modsecurity
9 r5 U8 q3 U9 ]7 ?3 ?+ \启用Modsecurity
( z$ b2 a- ?" w: K1 e. r. \使用静态模块加载的配置方法) J4 L  z" Y$ \9 J  E
在需要启用Modsecurity的主机的location下面加入下面两行即可:
$ ~; {  n  J1 r. t3 n
* z  C- t: u3 y2 Z6 M/ E/ n* ?9 s3 f! A7 s- T! |( N  P
ModSecurityEnabled on;+ I: X. B4 B+ S0 R4 I+ E% K$ k
ModSecurityConfig modsec_includes.conf;
8 b5 O. E' j3 o% y$ ^5 Z  E修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。4 w1 r' l% ~2 W! Y9 w. O

% `: ~5 o: e& h! Y% q2 J
; N5 s* J4 [! e; r7 [* \  X9 Q$ vim /usr/local/nginx/conf/nginx.conf% C! A7 L  c% o: I; B
1 u# Y5 ]! U, r, @8 c! _2 f
2 S( ?9 o5 p5 [0 O8 x6 m& i
server {1 w0 o/ [( Q) k2 ~! Q  A: p
  listen       80;
$ I" Q+ S% @4 X! f& S# i  server_name  example.com;9 C7 `. P" W4 E6 S* M

6 X) v" Q7 F( k, k5 Q& q6 C3 D( w+ \0 h% i! _5 }% W& t" `
  location / {
8 r5 `7 C+ |9 C    ModSecurityEnabled on;) V. T1 h+ ~0 F" |
    ModSecurityConfig modsec_includes.conf;- C" a% m$ k8 R0 W; k; [, _' d, a0 @; S
    root   html;
+ H( @% Z  v+ s) C    index  index.html index.htm;
5 Z& O$ y1 p: r) y6 U8 R5 Q  }, o8 n) g1 u" G9 [: x# r
}3 Z8 T% P7 Q3 }
使用动态模块加载的配置方法
1 s  q8 [" T! b& s; y* ?/ p在需要启用Modsecurity的主机的location下面加入下面两行即可:
" v8 W* X) \0 W2 v7 g7 K" R3 F& @( U% J

+ q$ W# @& h3 F% {) F/ ^3 Smodsecurity on;
9 {6 E; n9 Y: a' E$ t& J# Xmodsecurity_rules_file modsec_includes.conf;2 s5 z" \6 P/ ^3 q' c7 G9 I
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
  p1 [# d2 d+ G5 i  Q( v9 ]/ B" ~; S: _6 T

$ O* C, ]; R+ u6 K2 X" X% b$ vim /usr/local/nginx/conf/nginx.conf
8 V9 x( a/ t8 \! Q
# K1 l" ?& U+ ~# J/ f5 a. o, J- j. {. }/ t
server {
4 O, Y/ y# v6 j% {) r) [" ]6 O  listen  80;
8 {6 X3 I, M; t$ K# R  server_name localhost mike.hi-linux.com;
& ^8 F1 ?8 g! ~4 \5 _  access_log /var/log/nginx/yourdomain.log;
, L6 ~" m5 b  a' m
7 w) q$ m9 d* ?; G+ G( D( R2 b- \/ A% h$ Y2 G; G- W% y: ^
  location / {- a1 }6 c: G' o; j
, G: `4 B/ T6 h% C% K0 A7 b
9 G: f3 Y  {: d% Z" C5 ~0 v' ?
  modsecurity on;6 W( E) n- J. ~7 R% A; @; P% @, \
  modsecurity_rules_file modsec_includes.conf;9 |1 w0 B" I$ V1 X3 t3 E+ M% N- w% N
  root   html;
3 m% T: G' K& o/ u  index  index.html index.htm;0 u/ _; D4 c, p4 G9 e
}
. c1 C. L& N: r/ D5 \- |+ `7 j}3 G( O* ~/ V, m; n) |* K: o" b
验证Nginx配置文件
1 B% g, |: d5 |0 P8 _/ m$ /usr/local/nginx/sbin/nginx -t
, @+ `$ M9 x8 D/ m# |8 |nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
6 R" [0 Q/ I, b- n/ ynginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
) c" G3 j# h8 y" H$ r启动Nginx/ N2 L# a% K5 x; i% `+ ?
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
. t# j4 ^. ^$ q+ a4 ?2 m: y- H( v- _6 g0 Z; L8 W: q4 X

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

0 w% U7 u  A  ]" e* m

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-2-13 12:17 , Processed in 0.079560 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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