找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12879|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
2 X: Q8 i; L& O2 o
- \$ Z4 p+ }) G6 w: w" l
/ \. G- S8 ]( B- K) @+ T$ q# S在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
( N7 c+ p1 t; O, \+ g$ j: L# [  A3 ~4 z/ s3 l' l1 p

3 G! w  h5 i. f1 G什么是ModSecurity# R* T3 D8 ], n1 @4 o/ t$ s
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。1 n# c$ ~, ?; C2 Y9 ^( V. t8 D6 k

' R1 Y; M; d5 [6 t2 k! K: m  I7 F
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。, C4 ]! H% s1 r

0 g3 n4 M) A8 _6 ?
% D: O9 r. T" j: GModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。1 _. ~' ~. ~7 q2 r
' j% C6 T2 r/ a# p1 J, B

. f( a1 P$ y0 z0 r1 r7 W官网: https://www.modsecurity.org// I$ u4 h! n; E. I! K# I

( l3 i; ~9 H/ A: q+ l+ ?1 @/ E+ @
什么是OWASP CRS$ J6 `8 |3 Y' U+ F0 Z4 B/ v" L& U1 w
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
3 V: s6 s& a3 ?- |7 M1 p0 v) r- D; D' Q/ P
0 v5 v( X" i- m8 S1 e  Z: F
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。5 F+ U7 D  x- N! [8 I3 G
  A7 w5 _1 q5 R/ G9 l! x

8 G' j$ V4 S8 C1 U' s* }' F# VHTTP Protection(HTTP防御)
1 T, v+ F! u: i. KHTTP协议和本地定义使用的detectsviolations策略。
$ o2 n1 C) ^! H, G) N8 T- J
& e; G) p: c$ o1 W9 c' K% X% O8 w# @0 h; i" z
Real-time Blacklist Lookups(实时黑名单查询): M- Q5 ]9 U% ]$ O9 |
利用第三方IP名单。
! H* w  s) k6 r8 h8 \2 {0 b5 t1 M3 _' ^' \$ ]0 M+ ~6 b

5 ^) t  w8 {& _9 NHTTP Denial of Service Protections(HTTP的拒绝服务保护)
+ `4 _0 i9 _3 n1 E6 Y$ v防御HTTP的洪水攻击和HTTP Dos攻击。
- b1 s, T8 d1 P/ V7 y' T1 v3 O7 A- ]8 \8 }; L# V; e
; b3 v3 f! |1 @9 @! n  c( R
Common Web Attacks Protection(常见的Web攻击防护)
& V! ^1 z0 p& y& n+ k7 b检测常见的Web应用程序的安全攻击。
' Y8 ^6 H2 z% `3 [* F+ z3 f2 S! d( c6 g! }* l4 @: `: P
, ^( |* p/ w! v: O
Automation Detection(自动化检测)0 a0 X$ J1 z: }1 |
检测机器人,爬虫,扫描仪和其他表面恶意活动。* m. z+ b1 o' k- p

+ g3 ]" X+ F8 C, ~
: S. x$ p2 h* `( n/ Z- B; H0 @Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
. X  R5 ]4 y& U9 M. N, w9 n+ f  W4 ^检测通过Web应用程序上传的恶意文件。
3 Z7 o0 S5 F+ ]8 c6 v. F! Q9 C( {. `9 d1 L% f7 u5 Q

8 y; h  D. t7 C5 E5 u+ l( }5 hTracking Sensitive Data(跟踪敏感数据)
$ o" p& o# q) y4 o9 d1 d( L信用卡通道的使用,并阻止泄漏。( A* d8 `# F( Z! o
; |2 _. R! {0 R
% S5 b$ u; _' _( o3 [
Trojan Protection(木马防护). A# t. K8 A1 I  H% Q
检测访问木马。/ M! V& x* Z- B4 ~
" w/ o7 _* u4 b5 p1 J

, E" C1 x. P& x+ y! xIdentification of Application Defects(应用程序缺陷的鉴定)
( t! _3 l0 Y% e; f7 F- c检测应用程序的错误配置警报。+ H9 {4 `6 ~& g- W$ E( |% U8 ]+ @/ ?

8 A' S+ G1 M3 I
: e8 L5 @! @0 `. h4 _: n5 c  Q: _4 GError Detection and Hiding(错误检测和隐藏)
) L! ^1 ?3 m7 C1 T  y! c检测伪装服务器发送错误消息。
8 N5 A/ h, }. k7 a6 q" d1 e
8 s# f. t) s/ c: p  w5 O3 `% Q  G& D: c+ m
安装ModSecurity7 R7 V9 q- B+ I. @0 b4 Y- P
软件基础环境准备
, [+ S( K5 r6 |4 N- k& z5 B; E下载对应软件包
- d& m3 T3 T5 Z+ R5 A* C# E0 q$ cd /root
# N1 e; B3 f" Y3 o" _$ ~. X$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
! a7 m( K. O. i+ e; v/ _# O$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz0 l0 V* A3 G3 e8 S6 b0 D
安装Nginx和ModSecurity依赖包) M* F2 t1 i8 h  a
Centos/RHEL- Y: J- o  x4 D9 W" F) K- B$ x
1 a! b6 m9 ~' r$ }4 O$ {

  B- X2 c% g* ^$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel  f& L9 T# X4 O
Ubuntu/Debian
& n* ]( l! x% {/ J$ G3 R9 d, x; n
0 ]. E$ ~! v2 h0 R( s) i
' K* Y7 n) o' q9 d& 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++-dev
$ C% G) c+ }( h# h编译安装ModSecurity3 y0 s2 b2 Q3 s, X$ q
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。5 ~: s7 y" v& F. B* C0 V
3 _0 P7 i, u+ P, H1 S% h
$ D2 O" x7 I% f: Y9 S: e- o2 v; J
方法一:编译为Nginx静态模块
& x  {3 u% @- o, h$ y  a: o. q& r3 e. c

. A; G, f- F; A' w/ F9 h( w) Z编译为独立模块(modsecurity-2.9.1)% v3 j. b- v9 w2 n3 j
$ tar xzvf modsecurity-2.9.1.tar.gz
, Q3 L0 i4 S7 q( h% O( S$ z$ cd modsecurity-2.9.1/$ c9 w4 }' x: c* s) h+ R
$ ./autogen.sh
2 u. V9 O  p! |% \, n$ ./configure --enable-standalone-module --disable-mlogc6 @; O$ v7 O5 [: G
$ make0 h1 X) f$ C, T- t( e7 o
编译安装Nginx并添加ModSecurity模块3 d; Y  B* ?& A. t2 O
$ tar xzvf nginx-1.9.2.tar.gz  }% P; X9 I, w' i
$ cd nginx-1.9.24 ^' l, w; u: S0 [, q+ F5 v- ~9 k
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/+ e3 x: c7 A* K4 V& V) M! r( y% A
$ make && make install
9 v. }; h- c7 B/ ]2 V  N/ _方法二:编译通过ModSecurity-Nginx Connector加载的动态模块  s' ]9 J2 b& P5 I7 \% P1 ^7 U
  k/ p8 ]( F  w" g) E  V! m  W

5 s3 {3 P1 [( G' U5 s编译LibModSecurity(modsecurity-3.0)
% s/ o( w0 O' e* f/ m# ~3 y$ cd /root
, X# _/ c0 l7 h; L" ]2 `$ git clone https://github.com/SpiderLabs/ModSecurity# s( Y& M3 b$ }9 _
$ cd ModSecurity0 z* ~* C' m. H: }' G! z
$ git checkout -b v3/master origin/v3/master
) Y2 v* y) m- x) {3 i& Q& b4 m6 G; d$ sh build.sh
; ^( o8 A5 N$ I! g$ git submodule init! y. b: `5 z* \9 C4 M
$ git submodule update* A; P/ E, q- L" P
$ ./configure, L; V. n  R1 _% s& ~# G
$ make% A4 x; ~* N- t3 V
$ make install
, g/ D: l4 d3 w5 RLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
, d/ Q6 t5 U8 T3 w! c! P6 H# r, f5 l, }5 B) I: D9 z* y
  A# b% j$ m0 \2 b* V4 o% W
$ ls /usr/local/modsecurity/lib
$ m/ S$ I' \  Q1 l- o' h0 R, `9 alibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
' G/ a% l" \2 [2 {) j' Z- p编译安装Nginx并添加ModSecurity-Nginx Connector模块2 i  x0 v: p8 G" N4 X0 k3 h7 j' e
使用ModSecurity-Nginx模块来连接LibModSecurity$ d! ?! L8 V2 E5 E3 l

9 N) h+ _7 i4 J8 {
0 M6 E5 ^/ H% ^9 @( }$ cd /root" Y$ |) l! J3 h$ r- ]
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx) h  T" h( L/ x
$ tar xzvf nginx-1.9.2.tar.gz
% T! u" h! _5 o7 Y6 ]$ cd nginx-1.9.2/ A, L* d: H  t/ j
$ ./configure --add-module=/root/modsecurity-nginx
7 O8 c5 S/ p1 T/ S, n/ d$ make
: V; c5 j  a; @' A6 ^$ b  z$ make && make install" h0 {9 E, a; ^9 k' S
添加OWASP规则
% ~( i9 Q1 d4 U7 _: n- K) lModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。. Q) J/ J# s/ {

" [9 D1 ~& K" w/ k+ \. e, P  V$ [4 e8 \
下载OWASP规则并生成配置文件9 T* _/ f; ~: A! L/ i4 c5 d
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git" D+ s) p1 |! l- ~( d
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
2 V+ ]' u; C( t; |. ^$ cd /usr/local/nginx/conf/owasp-modsecurity-crs; S5 r( O6 b# c
$ cp crs-setup.conf.example  crs-setup.conf+ q- K6 r  J, s& t" v
配置OWASP规则
8 K' W9 c4 x/ w/ c+ h: m编辑crs-setup.conf文件
; _" b" t3 S, [( q; t8 Z/ c4 ~- W6 q! u: r! w/ y: w6 ~6 K4 K+ t! a. G

1 s/ D( o( z; _5 J- F$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
6 i* C+ u6 a& B& _  {$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf. t, G* N8 }9 Q
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
# T3 C& x& p. v1 m4 K: A- v5 |$ Q$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf) Z& H+ \# ]4 j' d
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。% Y* E9 {9 ]/ v5 T) i

* k5 {; I8 F* C6 x3 m/ K
- F# Q, o2 |& u; }2 ~$ u: `5 N启用ModSecurity模块和CRS规则
$ Y3 K. Q  N7 J  |3 m. _" J复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
/ |) |. _7 C) k# n; U4 e, R4 T3 A. F+ t- \. {1 Z

# z$ y# N9 Q# Y7 Q1 \. E* ~modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。, o, T; m5 f  ~! I+ I
0 r3 x' t* r  l: o  L$ N7 T/ O
: y3 z1 G5 H! W2 V, j" M. z1 ?
$ cd /root/modsecurity-2.9.1/4 J# \( U( i  O
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  ! j7 T7 n" n3 {$ k
$ cp unicode.mapping  /usr/local/nginx/conf/. b: `$ {% v0 F$ L& E2 l' B9 N) Y
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。2 s; r( M+ w0 t0 I

) ^& x5 ]: P1 [
3 o" w5 C* N8 n0 [0 h$ vim /usr/local/nginx/conf/modsecurity.conf
3 @7 \( d( k4 X$ {' U+ K( E; DSecRuleEngine On3 Q) r5 R6 E0 L, _# E2 q+ h5 v
ModSecurity中几个常用配置说明:
9 R( I/ x8 c& Z/ o. c
$ k7 g2 K3 l+ C3 E1 g) m' F( n+ {4 v
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。4 n+ ^, Q! p. Z3 }' R. g

& D, ^- K! Y' M4 M  U! @( c* X& H
- |9 S# a! c; }) W; D# G+ v2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
  o1 E' Q; O' R4 ?3 Z
* W) V7 o, r- u- ~$ s" u' h& v8 F" }# o3 y1 D' A5 F
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。( p# b6 ^8 J. _" d) E

1 v. w  i5 s6 h* Z3 D+ Y
6 X* @' k0 R( Q9 P4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。* j2 O6 g6 A% Z9 i

, O; b9 M% Z5 G7 c8 x1 V! Y/ T0 G& l& Y
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
* o5 Y- K' Y' d+ R% z
- F, `# Z4 c( d* G* r, r* w. {( v' o2 v
8 f! V4 E, \% F( q3.x版本CRS
0 [! M3 x  ~: S$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
2 `3 l" z4 M0 v9 a5 P- @# 生成例外排除请求的配置文件
8 T0 V' F% X" c: m& S, ~$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
% H& F8 o8 ~! X' h+ {$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf* q7 L% w+ z' K6 H& h8 I
$ cp rules/*.data /usr/local/nginx/conf
2 a( [8 O9 Z3 c" p5 R! M, K为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。6 s- I2 b1 B  W
* ~- q2 N# K- f; M/ v
* w/ A1 A. E; S) x! H
$ vim /usr/local/nginx/conf/modsec_includes.conf( \" d9 h: w$ I: G9 E  ?

' b8 G6 P: X( d9 n! k* x8 r  N
[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: U: q0 H' F, S4 A( K" X8 j
% \$ n7 V5 _9 ^7 s- t) J" z
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。0 B# O( o; H2 ]. o. n- x; Q' y
& s" j% x' E: Q; B  P
9 g0 {0 p9 l6 e0 @, _# f
配置Nginx支持Modsecurity
- K9 n* i6 }2 j: K6 \- _/ s; |启用Modsecurity
) u! v9 [) a+ o8 s! G+ h+ i  S5 `使用静态模块加载的配置方法! ?5 E, O9 h' K
在需要启用Modsecurity的主机的location下面加入下面两行即可:  j9 m* ]  b! s* ]8 p8 ~5 v1 L
: Z% G7 _) `% H$ [! ?  b: Y8 t  n

; Q2 Y" D: v1 A2 e/ ^2 o1 F& lModSecurityEnabled on;* |4 C  O: z+ K( h4 O) ^( V
ModSecurityConfig modsec_includes.conf;
5 \6 b& n( b9 }- w修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。2 N! y% \; O6 Y+ Y8 [, I7 I
4 O6 x6 p$ p7 m9 ~6 [

! Y3 K- s) L; G* c# Y0 o5 N% {. X$ vim /usr/local/nginx/conf/nginx.conf0 H8 }& D1 y: c/ [5 B1 F% O

$ y" U8 x9 _+ j% ?7 E& {7 z  u9 r5 T/ u$ I' c
server {8 g) `' ]+ L. C0 W1 u4 v9 V! n
  listen       80;- d$ O9 X. u; i9 [8 }* z- C$ k' Q
  server_name  example.com;
6 L0 X" j+ w, F0 k! c& f/ V2 b9 D2 u8 M0 d) B$ f
7 i. m3 l/ R9 D$ D! M$ g  `
  location / {
) A) ?2 n5 o" H6 s9 g    ModSecurityEnabled on;$ e+ P: r9 |. F- p( `
    ModSecurityConfig modsec_includes.conf;7 b5 G! W! |" S$ B2 R6 W
    root   html;
( ?  H4 H/ S% v% S    index  index.html index.htm;
9 ]9 I3 K4 w) f  }( X7 ]) \- H: ?  k: W; Z# L, e, i
}
2 ]1 K: ?' H2 r6 z/ @使用动态模块加载的配置方法" \' }7 h& P, P: Q: ]: @9 R+ N$ `
在需要启用Modsecurity的主机的location下面加入下面两行即可:
0 {/ `3 f7 V& Y& t' R
8 y' c6 ~& _" ~; [& W( |) g9 b# K6 z- C  n4 E
modsecurity on;  p$ {( A  a" y7 `6 \( R, g
modsecurity_rules_file modsec_includes.conf;
  I, ~: D/ o( Q* X修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。  p3 u% b0 Z" f( ^# g3 }" {6 I
3 @6 G& v0 ~) I! E* |8 _- `) i

' `. D0 X; O9 w5 O5 S$ vim /usr/local/nginx/conf/nginx.conf
) J2 p- n0 x2 e7 d- L4 j5 a3 d- }9 O
  @% k* y- o8 z+ G$ q2 r& D
server {
! C6 J6 ^  c. r9 A4 ?  listen  80;2 I6 ~9 S% ]" ], T! T* P' ~
  server_name localhost mike.hi-linux.com;' p% I) H3 p1 N5 `: |' `( v+ l
  access_log /var/log/nginx/yourdomain.log;
6 `" Z3 t& ^" S% ~/ t0 l1 @  N1 r( f, u3 w1 F4 K

' Z( o, C! B4 V: i0 ?: @  location / {3 x' i9 V/ E0 ^; F# A& [5 u
- D) D% j, X* ?" w0 Y' e
4 j& h- ?& A$ \" v+ y: p
  modsecurity on;# n3 K6 `8 f# @( W$ f9 V
  modsecurity_rules_file modsec_includes.conf;- P: t- [" U0 x8 e, d0 C; ]
  root   html;. q) S- C  H' n, X; g
  index  index.html index.htm;
  Q9 {6 ]6 l3 |+ H}6 P# T. u+ z  k
}
) _3 l4 \4 M, X& k2 c6 I' J验证Nginx配置文件
! N! {& X1 A* S$ /usr/local/nginx/sbin/nginx -t
% `/ j8 V. ?2 J4 V5 C; R0 @nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok" n4 r7 T7 a0 w4 ]
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
' o8 q2 Z9 w: k! z" A* W$ Z启动Nginx$ b( \1 q/ X# z
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
2 U: P/ l+ n0 G4 t* a- p+ ^5 R! T$ y, k6 R- A# w' @

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

2 S, i4 J9 n# m* L/ j% L+ Q- y# w

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-11-26 06:18 , Processed in 0.075116 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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