找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11282|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。  ?1 |5 ^* j5 m8 U, z- F

4 m7 p; f1 @  u1 }+ L5 \1 k3 {) U+ ~' d3 A0 w0 z# M, M
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。, ~; S) p+ {: x  L4 p

# q0 J% b2 t1 `, J& [4 M2 N1 _2 U% }* t) b% p1 L2 m
什么是ModSecurity, s1 f  x- Y0 p/ L0 c& d; C3 r% G
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。; W' X" \' Y, y/ B/ D

4 V, }, B' R: s$ x+ f# e
5 ]7 f% _% q4 x' L" K: O' s* ^ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
$ w, e( G5 V# C' q  B& G0 s: S
/ C: C+ O8 e: }
& H- j/ }) s) |% T5 e2 \; \ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。4 E; t/ v2 f8 Y% F( J

! Y& d7 a. X- R3 y
* k4 b/ W+ ~* C; l* _% j! |官网: https://www.modsecurity.org/
& C/ O6 a1 P; O8 {& w+ ^" d
; z% k3 i! \& i; P- s# U1 Y( |- ?' T& L6 f% l. Q4 E/ y& C1 R3 T
什么是OWASP CRS
! |/ J6 f% S8 T0 lOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
% `/ w% z" i6 v4 }) A$ M; w/ z, C8 Z8 H" i% R9 _

# \5 h5 n; n4 A! k% {8 WModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。( t( v/ w9 q' x. z" I0 U
( @1 G  t3 V7 X( |
% T1 Z, k: e9 M( V* f; G
HTTP Protection(HTTP防御)5 b' Q1 d5 _7 c5 h3 O
HTTP协议和本地定义使用的detectsviolations策略。2 O; }9 Y' Z  ?5 b; P* g3 @+ v+ m

* E5 x. ^9 ~9 L4 v8 x  [* R
7 H  e* [3 `* n3 D( k& L) @Real-time Blacklist Lookups(实时黑名单查询)" B- G5 W! f# i/ M
利用第三方IP名单。
1 D4 L& R# k6 d) \
/ |$ ]+ ?, p& ?6 [. W; ~( M
; m/ W! d' V1 M- n) @% L6 RHTTP Denial of Service Protections(HTTP的拒绝服务保护)5 n1 I9 n  a( F8 u6 |# a" W# k* k' r
防御HTTP的洪水攻击和HTTP Dos攻击。1 ]7 m+ B' Q% q8 r' [4 m4 q7 q; F2 Y
  v' j9 f) P) _! ~3 l) Q

; ~" O% K& T. W) R% b. @Common Web Attacks Protection(常见的Web攻击防护)- u7 {2 {  h& d
检测常见的Web应用程序的安全攻击。
& Z8 z0 o7 o6 x% `1 H
" G* B' v% }9 w& J( P, A$ U& [
2 ~% f! [7 [/ o* h( sAutomation Detection(自动化检测)
$ E; M% `0 H. u检测机器人,爬虫,扫描仪和其他表面恶意活动。
3 Z8 _1 Z8 E! Y  m7 R& G* Y7 A# ]  _8 l( b

' o/ u5 m2 d4 O% tIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)9 M9 i1 A$ ?7 p; M! [
检测通过Web应用程序上传的恶意文件。
; L1 {4 J( N& t( h- Y) t3 Q! U* u
# |& ?0 x# _0 B7 @+ o* T! K( L
* k1 A4 k8 e" V0 i" h2 mTracking Sensitive Data(跟踪敏感数据)
+ ?$ z' N4 ]% ]3 b" ~信用卡通道的使用,并阻止泄漏。
. W6 _: c" V% i( o, ]
) j+ _% t2 |* C% J( B- V
  ?" m1 I- _7 q) ?Trojan Protection(木马防护)- e7 u  S! J5 S$ O
检测访问木马。
# `" m5 f8 R) p/ W7 x; b" J, ]/ X0 c: Z8 u, f1 e+ |
' l: q( {- o+ I" u! \. Z* J
Identification of Application Defects(应用程序缺陷的鉴定)
' Q4 S2 Z; J$ L检测应用程序的错误配置警报。
1 y& @6 @1 c* ^5 }# m4 z- e# m
' v1 Y: G, \$ ]2 {# R3 W
Error Detection and Hiding(错误检测和隐藏)2 |$ w# @1 N- j0 N: L% C
检测伪装服务器发送错误消息。
0 d/ z5 p" n* z5 B& Q7 l6 X
: |: K" d4 I7 \4 U. z1 L5 h# w7 i* ]  q: G9 |4 N" Q
安装ModSecurity" P1 ~) x$ z. Y  A- G
软件基础环境准备
% R8 n$ U4 b9 t5 w2 i. ^下载对应软件包
$ ]% z. n% x/ `5 U& V+ |$ cd /root: `! R) T" `, ]# z* ]
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'+ n: r/ f* V/ @" q7 }* e
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
$ M: a; _7 ~2 x" Y8 i$ Y安装Nginx和ModSecurity依赖包% H; N6 c) h4 ~7 q4 L
Centos/RHEL6 I' y5 M% C0 D9 `" X3 u
: E' Q* |( p/ Y
" w& ?: G2 j5 K0 o' c9 y
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel: W2 e( v& F  T1 Y9 h6 s
Ubuntu/Debian
; L. G$ k" M2 B( I- q4 C  w) n5 l! g* z5 U$ r/ d
: x  L( L. h9 g
$ 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++-dev8 B* d. K# l2 Q5 J* ]
编译安装ModSecurity% a9 g+ j0 c" p1 H* ]: T6 h
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
; T0 q, p# I0 r6 w+ y5 p+ n; |- d, ^0 F1 m0 K' D5 [; p# Y

1 C6 V  C' ]" ~- U7 y7 t方法一:编译为Nginx静态模块
7 y# b- C) U3 y4 T& H
8 ~" F, q( R) o! h2 L0 S. f( J% P
编译为独立模块(modsecurity-2.9.1)- q$ |' Q# I+ A6 O* B
$ tar xzvf modsecurity-2.9.1.tar.gz
1 W1 e( n  s. W$ cd modsecurity-2.9.1/  A$ [8 D( o+ o* B+ |, {  q" A
$ ./autogen.sh* O& F2 q" S4 v/ ~, w% p
$ ./configure --enable-standalone-module --disable-mlogc
" z0 ?: H/ f: U8 S% c$ make  S3 G" @1 V) W- Z& A- s
编译安装Nginx并添加ModSecurity模块+ k* r* l, u9 g+ i! w  H/ m3 Y
$ tar xzvf nginx-1.9.2.tar.gz( C0 F, v, L  m
$ cd nginx-1.9.2
6 |4 [7 T* t  F1 _7 P$ `& T$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
9 f/ @6 [9 g. A. {9 \/ L( U* P+ W3 `$ make && make install
$ t* @% n2 ]; v, p! B+ G  T方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
" o: d) p3 I9 n3 }1 @  Y2 Q' ~/ B
/ f4 b7 d0 F$ r4 i4 p2 e& c) R( ~$ S: B
编译LibModSecurity(modsecurity-3.0)
. I1 I  W: c( d9 E- z$ cd /root
, y3 x+ Z" D  i$ F$ git clone https://github.com/SpiderLabs/ModSecurity# v! u1 f0 K  q* d- M
$ cd ModSecurity( x: r! z* u$ I& r, f; x
$ git checkout -b v3/master origin/v3/master
5 }: S, _1 j5 h9 b$ sh build.sh% |8 b% L5 x% ?: T
$ git submodule init  \! a5 ]$ J6 T/ t
$ git submodule update
9 I* a" l- `- [% d8 j; b  \+ Z$ ./configure" v* h1 H- v8 k- i, |
$ make- E, C1 P& a" D& c" `, @) l( v
$ make install
. a3 W7 b9 C2 KLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
3 Q) O( V5 \" x1 h& {6 E' ]( Q9 e& C" o4 |7 ^; v9 h0 O7 B0 |4 x
* C/ l$ c) T0 ?/ k* I; V
$ ls /usr/local/modsecurity/lib
4 ]+ O3 \% I0 n( t0 \* @libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0/ `7 }" E" J! L- \5 Q4 p' w' N1 w
编译安装Nginx并添加ModSecurity-Nginx Connector模块
/ i, v/ Y2 v3 Z, y" H, D1 ]2 v使用ModSecurity-Nginx模块来连接LibModSecurity
# j& C  i. W7 S  X  v6 o. n6 g6 b7 N, U' O  i# R. s8 Y

! ?9 g. x+ K, z! [+ S% e$ cd /root
$ e" Y6 c* i7 b, ]$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
4 i4 Q" Z) g/ C" ?( \$ tar xzvf nginx-1.9.2.tar.gz3 Y0 S6 D/ |' f- B  G4 i
$ cd nginx-1.9.2
: g( r2 Y8 b$ ]/ h. U7 C$ ./configure --add-module=/root/modsecurity-nginx) ^1 \- r# {) @* d( O3 p1 Y
$ make2 T7 T" `# {' N, p8 k
$ make && make install) `% t* |5 C. d3 V5 A4 ^9 ], m2 n
添加OWASP规则
/ E7 V. a$ \- iModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
0 i9 O2 }. s8 ?1 ^' T+ _1 C5 r1 p; b4 G" l- W" @
# m- Z1 u6 A; d) p/ N; g
下载OWASP规则并生成配置文件& Q/ s' {5 G, `
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git; Q* I4 m5 O9 d% Z. M! s) j
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
: S3 `+ d1 n0 C5 g1 B$ ?  b$ cd /usr/local/nginx/conf/owasp-modsecurity-crs* Z& F0 g* O/ c/ V6 h& h, S6 v9 W
$ cp crs-setup.conf.example  crs-setup.conf3 c, I& H" u, ^7 g, \' ]
配置OWASP规则( Q6 W9 m- E8 p: C' A* Z3 \$ \
编辑crs-setup.conf文件
: R$ _/ x5 X, T( N) x3 X) C6 T6 j% n( X4 F

* M1 n- o' ?  Z6 D7 I8 O7 R3 }$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf8 V/ D4 h+ \0 p6 x) \* `7 _" A1 R  ~
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
2 q/ v3 r3 j* Y  n$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf7 c7 N8 v, J2 }5 s) H- F4 Z7 l
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
# d& @0 \% c5 R+ |3 b  e默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。$ Y+ d! X4 K7 \: g; b8 a( u
/ ]) _9 v% q7 \
0 |2 C1 f" a* O3 Z
启用ModSecurity模块和CRS规则
1 q' ]3 p! S  x7 V0 h$ w复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。* Y! e0 ^8 ]; q% l

! X' _- q+ A5 ^5 \( q( `* {
" q+ C  X; s6 u# z# Z2 H3 K- W5 smodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
. z+ \4 K, Z# A
- r6 u! E; G* z6 ~4 v5 A7 o0 {$ N
; g( w/ o: u- e3 h3 ?- {$ cd /root/modsecurity-2.9.1/
/ u: L, ?) H1 t# E4 P$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  6 ]0 b. U- A2 A" L" K! d
$ cp unicode.mapping  /usr/local/nginx/conf/
$ ^& A3 W) _6 B- M' i' X- ?将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
! q. P' p- ?0 J( ?7 N- C" t8 c& T( C  q" J1 Z! W

& }1 i) p$ T4 ]$ vim /usr/local/nginx/conf/modsecurity.conf
5 _+ n3 _  N+ o+ `) WSecRuleEngine On  p7 d! W% v- r
ModSecurity中几个常用配置说明:+ f& H1 k0 r6 L5 N5 n
) m$ v6 ^  F7 B, w
- K* V1 ^. ?* r) l( r2 `, s" F
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
$ K) N8 f" L8 i" o
# d- Q* b4 p1 a( }' o
! P+ o% K7 ~3 e$ g4 W5 W4 @6 y2 K2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。) _( C- z" j' V* i5 W. X: S0 u

3 I2 z3 |0 L6 Y. o4 O$ W& u1 d
+ V; ], B! R/ `" E2 [$ y3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。$ B" j/ @. D: R

, e) O. f5 G& D5 Z5 r- _
# c' c3 ]6 f4 e& |  F" A4 Z9 I/ L: F4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
( O- P2 Y2 X+ o% {. q
. H+ A" t6 r- ], P7 Q  o0 {3 Z
5 Z; F6 Q0 J, v2 p! E4 i8 B, q在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
' L! _' I; x/ E# e! H2 E, s- y+ H! O1 [9 y! A

; u1 A; P) }  {4 a# d5 J& p3.x版本CRS1 v6 ?9 P5 T6 ~# |: M- [3 r% R
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs/ f/ z% ^8 P$ H8 D% c) L
# 生成例外排除请求的配置文件4 d( X& S1 f, l8 K7 K
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
+ e5 V( I  q1 C) _$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
3 R% }! `1 F0 I% V$ cp rules/*.data /usr/local/nginx/conf' j) ^( w- Y6 R4 a
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
& w8 c, t5 }5 o9 C2 B# D6 b
9 n# S% i4 B5 M& N+ F
. E' r  B+ z- B' D$ v$ vim /usr/local/nginx/conf/modsec_includes.conf% P" ], T5 n* a4 @" n) G  J

1 ?. V% T1 |) Y$ _/ 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

. P' c3 [$ U/ y( z/ ~/ Z& d. k2 d  T  Z" V
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
, X$ I/ m1 ]7 x. ~' w/ V/ z" Y7 F% H& W4 p2 q

  G8 H; L2 f; k配置Nginx支持Modsecurity
! T0 P) t: g; ~3 S3 ^* j启用Modsecurity; o& o2 w  O( }
使用静态模块加载的配置方法& B- k4 C4 Y. G0 R& Y' E4 @1 t
在需要启用Modsecurity的主机的location下面加入下面两行即可:9 Z) F; M( @: j" n+ \7 y- d7 K1 h( R

  X! q! |, W% D1 O1 p
  l9 u7 |. P5 M. f, t1 a8 ~ModSecurityEnabled on;
' c# t- h8 U+ i+ _7 }- H6 h' KModSecurityConfig modsec_includes.conf;. q2 R4 ?/ a0 t2 C3 F, p
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
) y8 U% G$ a( o/ T# b1 K( l. q1 X  c( z7 |! R6 O) ?& P

7 |' A7 f7 x0 ^, M# d) j# Q( [& h2 \$ vim /usr/local/nginx/conf/nginx.conf
/ {  l6 S, M6 t; i- d4 f* R  I0 ?+ t* u0 E

; k7 w( N5 k# J$ y. _- ~+ W9 sserver {
4 M# l% b0 Y, f5 x& H0 W6 y  listen       80;
+ G  [* u) k, r; V8 E  server_name  example.com;
3 I/ T1 C& Q) e) N8 b. @+ V) S+ o: ~, e5 o) C
8 w- I6 T- Q: K/ a4 c- f3 E
  location / {  t5 w0 F7 s9 i( }  f
    ModSecurityEnabled on;& r  O8 Y  o* P/ ]& t
    ModSecurityConfig modsec_includes.conf;: Z3 J  l5 `" D6 q1 p
    root   html;
) r4 l+ j/ e8 S- J1 [$ H% i9 u    index  index.html index.htm;& W/ d8 d  F) G# _1 q; X6 ~% \
  }- s- J/ l# d! W: a
}0 f  B! w9 X/ E2 Y; Y
使用动态模块加载的配置方法& C9 H3 W( e1 d2 n9 m
在需要启用Modsecurity的主机的location下面加入下面两行即可:
! }# [/ Z) B7 h9 ~$ f) x* P4 P
& m6 m" d, A" N) a( {2 O0 C
" ^( N6 r2 i7 ~9 {modsecurity on;- z3 @: {. ~3 C; z( o
modsecurity_rules_file modsec_includes.conf;
) C0 w6 @- a2 v9 n4 _修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
, ?* }: j6 g3 o* g2 n/ }9 d  N7 O# v) x5 ]% M

4 ?/ u2 f8 g1 U, \$ vim /usr/local/nginx/conf/nginx.conf
  j) h1 R- U# }0 s+ ?; K
2 A1 Y) w0 @& E
$ }/ J) W  y$ B2 E/ n+ |! n1 ?7 Fserver {4 W" {; M7 {4 c) D" z
  listen  80;' v. t0 b7 `( C' \2 s
  server_name localhost mike.hi-linux.com;
5 }) k1 r( Q5 @) {1 a  access_log /var/log/nginx/yourdomain.log;- G4 x8 r+ d/ h$ ~! _+ F$ D

; w1 f$ ~8 ?& x) ]& G; |, z8 `- @2 d; K. K* z9 Z
  location / {
6 u* n% i& x+ K9 Q- j! e* n8 C" \5 ^+ P) I9 z
  O& Z2 B: k& L9 b2 R/ H! f+ |' ^8 B
  modsecurity on;4 A, O! L9 G3 v) G" N9 C
  modsecurity_rules_file modsec_includes.conf;% o4 h3 v& f/ [7 S
  root   html;& A: X/ f5 T- u( T. H7 [5 n4 Y
  index  index.html index.htm;
! X  j  {/ \& r- F" e. ^6 C/ Q}/ K8 L" Y& u0 G( T! i3 q) |
}
, E6 E$ }% D  [: b  h2 z4 @# {7 \, \验证Nginx配置文件
1 O5 [7 M6 {9 R% B& x$ u* {3 k* H$ /usr/local/nginx/sbin/nginx -t
+ K2 t3 U' f/ V& a, M# ^* Rnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
9 z$ Q, [/ `/ |; |4 b; e$ `nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful% Q1 a( U' J  M% D$ b0 a
启动Nginx
; i% t/ t# U1 ?% l& ^6 _) `$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf9 G+ Z0 l5 m% f9 c5 I

$ f& O" e( a) Z5 [. t1 ]# F

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


6 H2 U& J  n6 n# D/ ?: {7 M+ `

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-5-24 17:22 , Processed in 0.070794 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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