找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10219|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
, C# T. M. ]$ A2 ~5 \! A' ^
7 e- }, N: c/ m/ g
; _5 W! f4 b- Y: A: m在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
7 q. q$ E0 n3 Z" \5 J6 k0 g- Y$ Y2 d* L
. \  F( T4 r3 \5 j0 H* |
什么是ModSecurity
, k4 C, J3 [3 m/ k# sModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。( e  D( y8 [) `4 [  Y6 S/ r& M
% Z( ]. {) v: a& S& S/ {! G
" G' E9 A& B% X( J  J0 _
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
& j2 D$ P1 R, S, T$ ~( c" ]! i

% _. m) x  ]  R9 sModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。0 y7 ]5 Z( R2 \( U3 [8 W0 x
2 ]3 k2 q; q6 R8 F
3 }9 v: Q5 w; q% s- u0 V
官网: https://www.modsecurity.org/
( F( W2 H+ d" f5 e; {* V
& S& V' H" w+ Z, Y
! N) i# r* B3 S; K" a什么是OWASP CRS
; f$ `  m3 q( z( ~( H. {9 B% AOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
3 s0 Q5 w6 \5 f
& ~" P$ x+ B# x& k4 [; R: ~  I4 _% t0 j" C% ^
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
4 S' W' Q* k1 n, d$ Z; o+ m$ l% f2 g# F0 E( f" ]: j' z

' T6 j9 u4 g: a& z% J3 OHTTP Protection(HTTP防御)+ m) n" M* r2 n/ c
HTTP协议和本地定义使用的detectsviolations策略。
! ]7 y8 p. V2 g6 {: n# a4 ~+ `/ u& B7 ?# ~, G5 A+ a. n

4 Z! h  [& \, ?& ]Real-time Blacklist Lookups(实时黑名单查询)# y  q* ?9 y) E+ A# B$ E
利用第三方IP名单。
  s! G" o2 c' s/ d! v
. z5 {% W( H2 ^# h. q! s+ G( q/ a* Q( [1 E# H
HTTP Denial of Service Protections(HTTP的拒绝服务保护)+ y( A: N  _; ^; M; y! S- S
防御HTTP的洪水攻击和HTTP Dos攻击。' Z; n4 R( p0 I; U$ i- r
+ ^3 E' P7 J# I1 ^) S: G

) d, d8 I6 r9 z( d+ F0 M5 R+ hCommon Web Attacks Protection(常见的Web攻击防护)5 E$ v' x' q9 |7 C
检测常见的Web应用程序的安全攻击。
1 ?& F% E! I( ^  d. O/ p, e  V  K2 o7 }) Y- R8 J% l2 I
$ @3 n6 I- D. X- D% T" P" v
Automation Detection(自动化检测)
* i) Y. Z6 J' u2 G. g) h- x检测机器人,爬虫,扫描仪和其他表面恶意活动。/ b% z+ \" b) }/ ~6 `* P
% s- j' M" a- C. T1 r1 r
' |8 G1 K4 v" u- @& g
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
) }8 @' K% \& [9 G检测通过Web应用程序上传的恶意文件。! v0 d: C5 N% y
9 B- g4 B9 W& n$ L' b2 o4 y4 u
- W- W5 E) c( O& K
Tracking Sensitive Data(跟踪敏感数据)
, f- e" o: e: I信用卡通道的使用,并阻止泄漏。
) ?: |" S/ K  G. b- C5 u
. j# |2 c7 x3 U$ O. @
' X) c/ O) T; @2 ~! v7 k; n2 LTrojan Protection(木马防护)
* i& Q% i% a+ _- ^; i检测访问木马。* |/ T% S+ e: x( t6 V! s2 \
( n# N2 p% X* a% J
! u( y; I+ N2 I8 A% `
Identification of Application Defects(应用程序缺陷的鉴定)& n8 C8 \( I0 G* r
检测应用程序的错误配置警报。. T/ [( V8 p) z, X# b! S' z; s* A
  [+ S- }0 i2 |& d/ b' d1 T
+ X9 d! r7 @) t  a
Error Detection and Hiding(错误检测和隐藏)  A, P  N  O( M$ n* r
检测伪装服务器发送错误消息。
# J& h/ V, G% r. @% y! H
7 S% G" }2 X! q
( B% m/ @, l- S) W" c, x安装ModSecurity, u# |3 G# p/ r& e
软件基础环境准备
; {% i, |( _% U; i: l6 r% [下载对应软件包
( d! d$ r* L) l5 \5 v. s0 l$ cd /root
5 u/ j' |4 ~/ v* H& f# Y/ Z$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
) N, U$ W0 ]; _+ q7 d( w$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
' X9 \% _+ G: R7 B- o安装Nginx和ModSecurity依赖包' g2 Z3 P. w/ `9 e5 {& [1 Y
Centos/RHEL; R' A6 @, Q/ e/ c% O
( J. v( u  L0 R8 f6 q, H

$ r" T  e$ P+ w, Y2 w: [$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
9 t. T& }, A' e4 L& E# w6 u7 AUbuntu/Debian& i6 o  }. r+ C' P* a( s/ |

9 R9 ^0 _" Y7 {+ a. [$ c
% S- h# [& C* P, E$ 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++-dev6 V" ?" a7 s- Y
编译安装ModSecurity+ m& F" D2 w  e1 ]1 Y
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。9 B& e8 l( ?. C8 A! Z2 k
/ f* o' \3 c- o7 y  j
+ V7 `0 v8 @5 |- m0 a5 G4 ^6 _
方法一:编译为Nginx静态模块
! U, A) u" z4 ]- [9 |2 x1 x; q. }* P0 R- E

7 |6 f0 r! j, C5 k" v0 [1 M- h编译为独立模块(modsecurity-2.9.1)
+ }7 N2 r! ~0 o/ Q  e( Z: x! A& }$ tar xzvf modsecurity-2.9.1.tar.gz1 n2 }& g0 Q2 Q7 I6 b" Y
$ cd modsecurity-2.9.1/; Y( O1 K* s2 f. u( a
$ ./autogen.sh) H: n# Z3 V* ~3 j# l0 S& }! b- o
$ ./configure --enable-standalone-module --disable-mlogc
8 A0 ^0 x6 \6 v  y* v# B$ make
- |' n* h# {6 T3 s- B8 ]编译安装Nginx并添加ModSecurity模块( p4 I1 y/ V! Q$ ~3 U! p
$ tar xzvf nginx-1.9.2.tar.gz1 V) o: W8 d1 ?3 B: c6 B& D9 z
$ cd nginx-1.9.2
/ e" J  f% T" m$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
" X& D! T, m. i* `$ make && make install
$ u6 i  [3 o  \1 l8 T& }方法二:编译通过ModSecurity-Nginx Connector加载的动态模块8 i1 f* R( v/ @- G* f  |

/ r9 ]8 M. o/ \: t; B9 W4 v. q! y' k
3 |3 s6 W$ ?3 P3 Y2 \  I8 }0 _编译LibModSecurity(modsecurity-3.0)! r0 R5 _' T, P$ q. H8 R
$ cd /root4 _( d& Y& _, y/ |  e; r
$ git clone https://github.com/SpiderLabs/ModSecurity
& V& |1 l: w  b- h: ]$ cd ModSecurity
  e6 Z1 d) g. t- h! t% i$ git checkout -b v3/master origin/v3/master5 B: c) o# c" P* P+ J& k8 o6 b
$ sh build.sh' B6 ~7 H: d8 k5 r6 W8 c
$ git submodule init1 g5 q/ a! S& Z
$ git submodule update
; y6 d. ^" S' b1 Q1 B! Y8 `: \  ?2 `1 w$ ./configure" a: c; o" k6 \
$ make
$ A3 e* a6 m9 P- M# @$ make install' {8 F. Y6 z! j) e% m$ J
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。: n1 x* s3 }$ M5 A' D, ?; H
) d5 h7 q9 z+ |1 e

  `1 R4 l) k' s9 R! X! Y$ ls /usr/local/modsecurity/lib
4 R/ y2 ^3 c3 W0 l9 \; zlibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.06 n- v5 r) p; b: j& U+ d! ]0 |/ T& _* o
编译安装Nginx并添加ModSecurity-Nginx Connector模块9 K; |8 K+ i: `6 B; I
使用ModSecurity-Nginx模块来连接LibModSecurity+ H8 C5 n- p( j- \5 V4 C* x$ A0 e

2 K% `6 P5 x; d. |: T& W* B3 n( T2 n- p0 U: k/ i, S' x
$ cd /root6 }( ^4 \5 _. c4 d2 k, M9 _
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
2 z8 v9 e# \+ t3 b3 }' G$ tar xzvf nginx-1.9.2.tar.gz
/ C* y: ?  `1 C% w" G' v$ cd nginx-1.9.21 q0 M# k# |6 U, m' G+ X
$ ./configure --add-module=/root/modsecurity-nginx
3 b" u& T& l+ R& o% D- g) R$ make# q( K, n. G) F+ N- B- o
$ make && make install, K* Q* Y9 C1 B# Y9 ]
添加OWASP规则' C" H" N; N0 [
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。; I# W$ i# A8 e3 ?# Q# P6 m' }
  H; s$ W; q# {" n7 k

' j1 h, h: @2 T& }" I6 u& M) c8 k下载OWASP规则并生成配置文件, _& L3 \) R9 j1 f: A
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
" l2 p, |2 b7 [1 d2 x  M8 x$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/* c! a- R2 l5 M# N4 i- V7 D3 J2 ~
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
. Y# ^  L+ h! V! `$ cp crs-setup.conf.example  crs-setup.conf
1 G( o0 y0 |+ p配置OWASP规则
5 _$ B/ i7 U6 x9 y' \编辑crs-setup.conf文件
8 K, D" f8 J; @* v) x$ m0 ^1 d! S6 d; ~$ u( u2 \

2 S' a6 W8 ?( D1 z8 ^8 o$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
; s7 G1 h& E" n; e7 h4 N# O- J$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf1 w5 s3 y& H3 g' D4 q9 T
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
; \& r8 {3 W  b8 M8 T$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
3 a3 A  i/ C- Y; @0 N# Z, ^9 E( F默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。, r1 i: t/ D. |# V2 V! P) ^& q
, ^7 p- B1 m+ x& A. p. q, }' O
& m4 W$ K: I& {. n
启用ModSecurity模块和CRS规则
7 Z. Y9 @$ f: M5 u复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。3 V' c  i) W8 J% p' T. ^; ]3 i, x

8 I1 R! j% W" k3 u: K
( o( j% S7 Q0 {. S& W* ^# p2 Kmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
/ B  w# H6 T' ^7 Z/ |
. T  B* _4 R' Z( M* N. F
" l7 U5 h% m5 w6 X, e$ cd /root/modsecurity-2.9.1/; O7 ?7 l& T# W2 f6 a" u8 v
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
4 D, `; _$ w$ B$ S& q$ cp unicode.mapping  /usr/local/nginx/conf/
  I& x$ v7 e9 ?9 M将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。  E: u! l6 K( @  m
; L( r+ _. u' s2 J. T4 J3 B# ^

* ?  M0 C# [- y$ vim /usr/local/nginx/conf/modsecurity.conf: R' ?# t0 {8 ]  U
SecRuleEngine On9 A7 G& q( e1 K, @% G) `
ModSecurity中几个常用配置说明:
, m5 G/ |/ p0 B0 ]
$ d$ F' ~1 T1 t9 j& A6 z1 W
- M" P; Q) |! K% `4 @1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
$ e# d. Z& x6 Q5 z: j4 K5 Z' p" `5 |" z7 n
" f& K5 t. s% _  g( _
8 l3 |2 |, g7 c4 k) X) @! X2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
5 [  O# ?7 P' J7 _$ h9 n$ [5 V  {; P
& `8 C  {# B$ q& u5 j
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
* ?& V  k) q$ G: J: N  n, K0 t" z# |) B* j
* K+ K/ O/ o7 D. S# u, z
5 x8 B- k, n, U4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
* B$ k) V" m: s2 {; g% x) D3 x6 @5 q7 \: |1 A# Y) N5 E# ?

8 Q& E! ^/ v! M" W2 E在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。% y2 L, [0 }. f' I
: {$ h  B" |4 D0 w7 A

+ I; c+ Y) I- A4 ^3.x版本CRS' M8 P/ q+ R, n
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
  ]  o1 ?1 N& q% N6 S4 E: ^5 M# 生成例外排除请求的配置文件/ K; E" W/ p* ]0 g1 c& D' [
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf, f: }1 l5 A# i! T0 j2 k9 S
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf/ e: [5 V& R: H7 r6 V* Q9 G; m
$ cp rules/*.data /usr/local/nginx/conf
5 R& t9 {( g2 z* }2 u; k/ L为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
  \, C" n7 [1 c; e! _( E' t
" f; d& \* }" s- B: Q3 s, I; s* d7 n  ]0 q1 B0 Z# W
$ vim /usr/local/nginx/conf/modsec_includes.conf  q! Y/ m, Z$ x# w9 A; x

9 G5 q- i  v% K3 n8 ]( P* l' F7 t
[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

, d6 \' o% f5 @1 y; x! N7 k5 |; }2 h& B1 V) Y2 f+ N
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。: Y* \' k: r' i/ Q% X8 R
  A) G6 \: @+ D; o) W: U! P

* I8 u8 D1 ?  O* I3 U7 H, B配置Nginx支持Modsecurity
8 [3 a; h5 F2 ^5 M8 l6 ~启用Modsecurity7 z7 R! ?/ s% x  Q8 s0 K) H
使用静态模块加载的配置方法. M  h( ]' v1 y' g9 f' C8 d2 ^& f
在需要启用Modsecurity的主机的location下面加入下面两行即可:' i4 o5 g) X; u) P4 H

) a+ }' o. [% X' Q  E/ G7 a0 ?* S- @( H( ]1 W6 D* f; c3 f
ModSecurityEnabled on;
+ B: S- @1 e0 [& p% w/ ~$ sModSecurityConfig modsec_includes.conf;
4 e. A/ [  a" l修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。4 b6 e# q4 B) N% J+ m+ V! U

5 [1 I. E: B" v
0 o9 w" |" t, K/ ^# d) ~$ vim /usr/local/nginx/conf/nginx.conf
4 Y: M3 ^5 o6 r7 q, D& }2 x* k4 H; l, \3 U' ~5 z5 g' ~# x
5 A0 y7 n; c/ C; ^  E+ J
server {. ?- z/ s, [& i8 Y% Q. {
  listen       80;
5 X% X- L7 {! \  Y5 l  server_name  example.com;0 p; B2 y) O$ R/ \. k2 h9 I

" j5 J( ]7 w/ e5 d7 V6 H
2 W% B# o  _4 n6 k( G  location / {7 \; w! @$ R" Z6 s% f$ N4 s, T
    ModSecurityEnabled on;3 b6 [3 X! f$ b# ]
    ModSecurityConfig modsec_includes.conf;) H4 {$ A5 H/ ^7 ^/ C% o
    root   html;2 o9 L: B$ G* [% `
    index  index.html index.htm;
1 f( o9 ?- {6 g1 @4 v+ D# }( ?$ h: \  }5 Z% r% ~, p# |0 W0 ^
}$ }1 n9 T1 y7 l& [8 w
使用动态模块加载的配置方法0 y2 _4 e9 O3 X" @: t
在需要启用Modsecurity的主机的location下面加入下面两行即可:9 B: Q" B* w) Q* b% _

' Q  K% S* a- n
/ j; c) D$ u$ n9 P6 k; d) zmodsecurity on;3 m2 f( |8 C2 J8 y/ _. L
modsecurity_rules_file modsec_includes.conf;1 e2 c' f  h: @& J7 G5 j5 X% w
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
( ]/ F2 O" e$ p* H5 c4 t
+ p% m  i" ~: H0 U
: G4 z6 \6 d3 E$ O: c, n2 p$ vim /usr/local/nginx/conf/nginx.conf
( e$ |" P& _0 L  p( h7 Q  h3 V4 r% H
/ A/ S, ^1 B# n& W; o9 O7 v2 I9 D
; n7 Z$ Y+ a/ h6 b( L" Qserver {
* }5 b* N4 _( L( Q8 ^! i  listen  80;) g+ o, H- h; `5 S# j* t: M) s
  server_name localhost mike.hi-linux.com;; ~' U* y1 O( O6 r5 O1 L
  access_log /var/log/nginx/yourdomain.log;
& o/ `% X7 w9 }
' Y" R  X% ]/ z9 c
7 J% S7 {0 }  D6 O7 C  location / {  n6 t0 L3 |2 _5 u
; b/ f/ _5 _3 ^

9 S9 I8 M2 |" Q1 W- [$ L) W9 N! U0 I  modsecurity on;  H; ]' R- ^+ Z
  modsecurity_rules_file modsec_includes.conf;0 m) f* {8 R: Z6 V4 Z0 w
  root   html;) q2 b% {$ T* a! a
  index  index.html index.htm;
5 c* u% G# t  S  a0 A' m% @}9 J: P+ u2 T, y( `
}% o  w* H0 W& G
验证Nginx配置文件
5 u! S, K: L8 a5 R# V) R4 i9 S' k$ /usr/local/nginx/sbin/nginx -t
, N% K$ Z9 N  I' mnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok. `9 C3 y* ^$ g6 f% D3 j, ~6 }/ B$ H
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
) ?1 ~1 Q* ^" A0 i8 R" r: o启动Nginx
6 V/ ^( b2 k' y$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf+ I) N" g: `* D0 K2 n1 W

# ?9 `0 e7 S: s9 k* M

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

, B7 Z  K6 O+ J5 g, B

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-2-12 23:41 , Processed in 0.081355 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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