找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10554|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。1 T. J8 c# K' }& R/ y

* ^3 R$ q  m- K" M' o2 R9 e! J" j5 _( |/ [  v6 C) W
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
0 Q1 a& v- @! w" p
4 G: p& _5 u" \1 K8 f7 C& W" S' X4 C
9 I% l9 l8 w+ F. Q! s1 [什么是ModSecurity
6 A7 e# T" C+ f- @) c8 W" YModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。' c  Q7 ?: M& O4 j

* C/ ^3 e% _# b! W5 ]& e- t4 b5 ~6 a2 q8 `+ E
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。5 e. T; w# [/ U) `3 _( ~  h8 t

  }, F, I. T0 x( V2 a+ j5 V- C/ z$ i
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。0 A+ P( K: ~4 S) \
( P3 I; j6 o+ A2 C

+ y5 @4 h! `5 U官网: https://www.modsecurity.org/
" {* x" t, m& W. W; c' ?1 j: ^1 t# ]9 J
, g, C5 A. _8 U2 N
什么是OWASP CRS) a2 I: z  E+ Q
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。- V. N8 J* [) z  A! Y& `

8 A9 e8 P; W% T3 n) e( v/ F/ d& ?- X1 v! s0 ?! n
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
  t8 X: m9 i( T! K. ?3 P& b6 E9 F0 A9 U2 x; k
  h2 g1 X7 M. `; M( P
HTTP Protection(HTTP防御). G8 X9 p4 o! d9 `
HTTP协议和本地定义使用的detectsviolations策略。
/ D! G$ _7 C6 j* c
1 ~: i! L: h4 I  Y5 q- v& D1 E9 C# h+ h! E# S: r
Real-time Blacklist Lookups(实时黑名单查询)
2 `2 k" w. m: @4 i& x4 Q: ?利用第三方IP名单。) z. w. k9 V3 d/ s1 v0 S

- N. N+ K7 `" T  d# J# \( V. B" c
3 U  S/ N% @4 k6 e' `0 ~5 ?: O0 KHTTP Denial of Service Protections(HTTP的拒绝服务保护)
/ }  a2 L; X8 i- F! d防御HTTP的洪水攻击和HTTP Dos攻击。
% x, S; s/ o% r1 j; F* s6 r6 G

, M! n& K9 `, H& K" kCommon Web Attacks Protection(常见的Web攻击防护)5 H1 B6 b' g# ^8 }3 |0 T
检测常见的Web应用程序的安全攻击。$ Y. c& ~& E) \7 ]- p8 W
3 _# E2 F5 g- Z

  M- h) X: F: w& gAutomation Detection(自动化检测)
2 m/ |* {% z/ v* L: D: g检测机器人,爬虫,扫描仪和其他表面恶意活动。' W5 x4 R9 P6 D  E# e/ u0 T
/ }* Y; ~6 w* q3 r6 M( Q: q

( R2 ~3 B2 |% h0 _# }; MIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
2 F7 k- X- \0 h- q2 T9 Q; w5 N检测通过Web应用程序上传的恶意文件。' [  n) ^' F. Z% k2 J

3 P$ u4 U9 `$ T% e! p* f5 E. u, d  D% v$ N: q# w# E" T9 z
Tracking Sensitive Data(跟踪敏感数据)0 J! z. x& m8 T, o
信用卡通道的使用,并阻止泄漏。
4 J( g" B& v1 V/ |" t9 a# n- S: Z; J. \! b3 D- p, h" R  g8 g2 n
, c* o! n2 Y  z* x
Trojan Protection(木马防护)
% `2 A. |. E( ^6 H$ `% H! f. h检测访问木马。) K1 k; P5 t$ ^5 t" H$ ^% |

' l; X- R3 S. f- }; j& k. h; N5 k# y# \/ p  z8 Q! d+ s
Identification of Application Defects(应用程序缺陷的鉴定)
$ b+ I" s! j) q" a! ~4 y. _检测应用程序的错误配置警报。- z4 e9 X- E$ {. Y7 I6 T

  r9 I5 e1 z7 p5 j
& I( b* y6 ?+ @: @: n, \: {Error Detection and Hiding(错误检测和隐藏)3 u- R* ?4 P  |% {- u& T. w
检测伪装服务器发送错误消息。1 y" K1 U5 ?( t1 Y3 g

1 T* G: Z  q4 u/ {+ {' f9 w2 C. E) b5 F  y
安装ModSecurity
! e+ d9 e+ I. J7 ~软件基础环境准备
0 r- E+ i+ F0 f7 f- ]6 `: b下载对应软件包
7 F. d$ b3 U1 m# H$ cd /root
  e" I; T. g/ x$ S: d' U$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'+ I3 F9 R' P+ y) J8 f
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz; |4 G6 V% p: k9 H1 U
安装Nginx和ModSecurity依赖包5 u% ~0 i, b! I" L: i& t
Centos/RHEL
. o# S" }! K$ x) p1 V+ S8 Y) h; C3 u% l1 j
6 }. N$ m" s4 M, Z
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
; _( g/ \: u. Z% TUbuntu/Debian
( h/ m7 D8 w7 _# i* {. s8 l, @
+ Q3 O- V2 ?. |. h9 j- k' P" }! [! k. k" g7 O& ^9 j
$ 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* G1 W% }$ }$ R. |! d, x编译安装ModSecurity
+ b0 f% S* T! z; A. @- Y  D8 aNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
8 y& i& s8 z+ Z+ }1 b0 v  |
+ {, N( t1 S! n2 E! u3 P6 @! _% T. R& Q" _! g; d+ V) W
方法一:编译为Nginx静态模块( Y2 M; k7 ]6 n. c# p" o2 ?6 i
; d  @; J8 w1 N/ i% q
! s+ K" A5 S. Z' H5 T% _/ w4 ^
编译为独立模块(modsecurity-2.9.1); C3 s# Z% r' y- t2 E3 p
$ tar xzvf modsecurity-2.9.1.tar.gz
3 l0 Z: Y/ T6 n/ d) j" q9 A, S$ cd modsecurity-2.9.1/
4 W$ {" _( B9 p. `/ _1 U$ ./autogen.sh" E4 ~3 \. n" `) b4 {
$ ./configure --enable-standalone-module --disable-mlogc
" m/ U6 l, m( ~  [; r! Q$ make+ H0 P( R  K! M! V
编译安装Nginx并添加ModSecurity模块0 _0 i$ h( I# s, R, E$ S* `
$ tar xzvf nginx-1.9.2.tar.gz* b- E3 A  k6 O& K
$ cd nginx-1.9.2  @/ r$ v, p6 x4 w0 E0 h) K( O
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/& t. R' y/ r7 {0 O/ ~
$ make && make install
- |0 b* f5 R5 [+ |9 h% @方法二:编译通过ModSecurity-Nginx Connector加载的动态模块# [- d, W+ `+ l: l3 g
, b0 L9 u( g3 D
* l' m+ C/ @+ K; G
编译LibModSecurity(modsecurity-3.0)# o( ?) s/ q; l! m
$ cd /root
' u5 b1 {% x0 A$ O+ N* g. }. y( Z$ git clone https://github.com/SpiderLabs/ModSecurity
! l, L& X! G* N: i9 X; p9 J, B$ cd ModSecurity
- G2 O' t: q0 ~- |$ j5 b. K" v$ git checkout -b v3/master origin/v3/master4 W* i, @& W; c7 S* @. k; j
$ sh build.sh2 B* a( y4 w6 a5 y$ |
$ git submodule init+ E) f1 R8 n' D3 \& h- y: F$ f
$ git submodule update4 u7 ~" C% L, J7 Z' b
$ ./configure! D% `, p! U' I
$ make: T1 \4 v: [5 Y7 I. a- r( ^
$ make install
; a2 n( S2 D  ?3 nLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。8 }8 E, {- r' f
' X# |7 v2 w  v  _2 ^1 A

. ~# m/ }& g$ x$ ls /usr/local/modsecurity/lib
0 M8 v7 ?; i& O% q( E* klibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0  e: u1 g6 L) d( Z6 U) U
编译安装Nginx并添加ModSecurity-Nginx Connector模块
# i' M0 H! Z' T, e使用ModSecurity-Nginx模块来连接LibModSecurity( k0 K" q5 z1 E/ l" u5 O" X

+ y/ E! z0 F) t4 F4 m4 f; g
/ Y3 c4 A7 c2 g+ p/ R% i! g) A$ cd /root0 v4 r1 o; F1 ~8 v- Y+ i: y
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
/ b$ K0 j; a$ u$ tar xzvf nginx-1.9.2.tar.gz
; e6 i+ Z+ \4 n& E3 B# f( V3 q' O$ cd nginx-1.9.2+ }2 E, d1 E0 q8 R9 h* V
$ ./configure --add-module=/root/modsecurity-nginx1 m9 r7 [; u/ q4 H5 E
$ make
+ j8 _3 T, V+ n; ]- C3 E6 A% j$ make && make install
# M: [8 R; x$ H1 v2 E- s/ O添加OWASP规则
" m0 ?) H, |* E  H4 [9 t" C7 IModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
5 @$ V0 p; y1 L. {2 x' K- O" J$ J
1 P$ b( }, R' ?8 u  L( k2 l8 O9 h! y( S& b" V9 ^
下载OWASP规则并生成配置文件
' o  f3 p& R, _$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
3 x" ]# S/ Q2 i/ |8 ?! p6 q$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/: I' g8 b3 j/ E* `
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
6 Z1 j( u8 L- Z+ A1 N/ c$ cp crs-setup.conf.example  crs-setup.conf' S& C; [5 F$ g& Q
配置OWASP规则
" x1 d1 [" W1 O5 S8 P& T编辑crs-setup.conf文件
# e8 Y$ Q4 h; X; Q0 N! g; U/ M" G) t) d
. c% ?# y- G! Z  x4 m/ S  t/ e1 w+ Q& I$ [. `
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf: j1 [" k+ k* M4 Q) a
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
/ P$ w/ g5 O& w$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
/ j! r7 Y7 I1 N$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf! G$ v8 i6 _4 S: y5 y
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。1 f! j/ p) _! k6 M7 p9 b' M
) ], d7 Q9 g: v) \5 n

% H  C0 i; h- l3 |启用ModSecurity模块和CRS规则
. o8 L  @- A2 ?* q, A* E复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
/ z* C$ C* F. s3 D: q6 d- I7 c# o+ G% o% C. F
. [( a! U3 B7 _1 a0 j
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。+ ?: h4 T6 S. r4 |' W5 m

5 l3 f" w2 U- ?. [8 |+ o6 R$ S& M4 J$ h' a: t
$ cd /root/modsecurity-2.9.1/- b) ^+ o( G) s5 U4 P" j
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
( N( J( T% g  H& L$ cp unicode.mapping  /usr/local/nginx/conf/8 w& p: D/ T5 C4 z/ M4 z$ O& ~
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。( g: N. }4 }" i3 M# C0 j# J

, O8 j7 r) x. z, k/ L: A; f" e9 @$ ]
4 X) O5 O1 b  j' t+ E- e$ vim /usr/local/nginx/conf/modsecurity.conf
8 v9 N# g9 @  k5 k: i$ D8 D* wSecRuleEngine On
7 d. W& y1 [4 VModSecurity中几个常用配置说明:
0 e8 F/ H; @0 E
9 k0 p8 ]' x2 M- k: n$ X: ]+ H: V6 D/ _. F. z
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。& l3 i% z/ }; S
: a) H( v$ Q! ~. Z6 r
4 a0 w2 N" e) I1 [
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
" p- ?, X0 v9 L" ]8 V- J
- \7 N0 {( }# x3 u; Z6 _/ c  D
# K! j6 n' V8 [: p: v3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。: {8 R% x& ^4 A
- h3 \# A! I6 O3 K5 |

4 U! ?, Y) A0 B. M4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。# k3 ~) E; @6 ]0 h! f2 L, ^  l

. x+ l0 F" C3 W5 ^* |" v; k# L) l' c: l. ^, O( e* s8 k1 N5 }
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。1 j* U+ G1 @" n1 w- N5 D

, x7 _, a0 b; _& r) W. u$ z9 d0 J
  S( Y+ r2 b5 Y% ~# A3 I; Y( D/ ?3.x版本CRS
9 U/ U6 E& L- P7 i) ~7 ?$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
- H- T! [, ^2 D) _: |, Y# 生成例外排除请求的配置文件1 M' e+ C! U: J. j7 P4 L2 H6 ]8 f1 C
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf8 |. E. A% t2 q/ Q& V
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
3 ]- \0 c) ~  R8 q* I7 K0 j$ cp rules/*.data /usr/local/nginx/conf
5 y: c* N) m' k为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。! L* X8 A7 X. U. ]) z
1 K0 M0 n; x. _; R/ W' l# o/ d% r
4 L# y! d0 g" U
$ vim /usr/local/nginx/conf/modsec_includes.conf9 F0 W, k  E$ V

  P  s# O3 A8 {. {: Q3 b! G
[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
, [+ X9 B( |$ A8 A4 A5 ~
7 V$ j, m* C* H" n
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。' \6 O( c9 U$ @' ]/ z

& u) ]+ W$ \2 a  J" `7 N0 H2 m2 F' ^9 T' O) C
配置Nginx支持Modsecurity  k( g6 U- ^' j5 l( Z6 j+ |+ ]
启用Modsecurity- t* C& u+ q) \5 q: S# Z8 F) L
使用静态模块加载的配置方法
  O2 |7 W6 s; o* |在需要启用Modsecurity的主机的location下面加入下面两行即可:$ C+ c5 m( J8 w; v

5 N0 Y! E9 Q. K  }) U
. ?- [  n* T- y8 oModSecurityEnabled on;" ^# P; q3 y& X2 ]
ModSecurityConfig modsec_includes.conf;* ^2 ~: L4 j; ^& n0 i. y8 O
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。" a8 u7 D# L6 c$ j! T+ b& k

( t, Y& n3 y, _  `  }. g# b
3 m" q6 {# G- w$ vim /usr/local/nginx/conf/nginx.conf
. [: E4 f- Q' P3 [* Z7 P; u
$ c3 \# x( H# p- B/ x/ [1 p+ ?9 H& B
! i+ a: O2 x# ]server {
# S* x- I" x, A) v; s  listen       80;
- M: f% h1 R6 F3 Q  server_name  example.com;
% V: Y# d  f$ V/ ]- U
! D5 _# q: [* `2 i; w
% \3 J( }; x* q( A' _  location / {( M6 l5 K# _% I' O+ r
    ModSecurityEnabled on;& }- s4 Z2 P; g8 u( t' q
    ModSecurityConfig modsec_includes.conf;
9 J3 a  |6 d3 E/ |8 u* L9 r- C    root   html;
# _" L6 a: |, S3 Z* ^& t4 X    index  index.html index.htm;
& t: f1 }* ^' D9 ]" }, ^2 D( Q  }. u8 R* o, e+ C8 x! N- ]0 f# Z
}
( v: m' X# F/ b使用动态模块加载的配置方法
# n2 N+ W* Y' w* k6 z% {9 H4 B在需要启用Modsecurity的主机的location下面加入下面两行即可:2 J8 t( t0 V2 X* t4 }

- }* q3 c+ n1 c  f5 x; a
7 q7 w- B: e8 t* Smodsecurity on;! q+ }5 n$ X8 u( Q8 ^6 W0 P
modsecurity_rules_file modsec_includes.conf;
% S, n; E& Z1 e9 H' S. u4 Y修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。& o$ {! X6 a# P. P! H& w

4 x4 O; v. R: Y, k# B4 k1 k8 ~- R" N0 Z) |5 g, S
$ vim /usr/local/nginx/conf/nginx.conf( w6 P" [! o/ l5 h' t0 U4 {

3 v8 T8 s! G7 T+ R1 ], ^, g, g5 S) G4 w* h/ o8 o( Z+ W# `
server {
7 x. r% H0 ]3 k- d# `  listen  80;3 }1 p% [9 _( _$ S! `; w% R9 p
  server_name localhost mike.hi-linux.com;
% F, n4 c4 @! C- v( Z" Z" O0 v  access_log /var/log/nginx/yourdomain.log;( C, Y$ I5 S+ E) o* B
" a+ d( t0 _4 c" U8 n+ v
) M# u* g  H( l  b* k% z5 i* @
  location / {( A% U( F' s% X- h3 \# o& b
# o; N6 y1 s7 X
+ Y- |. b' f9 \$ K! n
  modsecurity on;) o" ]" t9 P6 j
  modsecurity_rules_file modsec_includes.conf;; ?5 i, f/ V: Z+ h) f
  root   html;
* o& C) F1 k' b  _7 f  index  index.html index.htm;/ C: p& x' O* }& S3 ~& f9 e+ P
}0 [3 L! f( B$ q0 s& ?  G
}
, R4 N8 _  A$ k0 `验证Nginx配置文件
$ L9 U) }( v3 S$ C3 I1 {5 `2 b& w" ]$ /usr/local/nginx/sbin/nginx -t2 d. B8 _2 d# o, |: H8 r
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
# r, c# f9 ]5 Cnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
0 b9 {& O0 X. c" V) z' X6 D# r启动Nginx
( m( P; V' \( V7 i$ Z$ w$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  w6 U' V0 e  [- m$ I! D, k- @7 K( d

测试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 l1 E0 E, @+ W1 R4 Y

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-3-19 21:49 , Processed in 0.075602 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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