找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10919|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。7 V( d* o! c3 v$ l* ~  |* l# e
/ n8 j; S6 ^4 z/ D& z% s& a. n6 ]+ ]
, u8 x; T0 o3 d5 u: w
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。) U+ v6 v5 g* P& d
+ ]; r- }$ [5 G- |

8 r6 w2 `+ U/ n6 P5 b* r) c什么是ModSecurity$ a6 _! u8 D, Z: n
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
0 w7 h6 _; f. _4 d8 f5 C
7 i/ u* ^% I# t; {4 Q. O* ]
- ^6 L; B# l6 k- V$ k: u* U2 j0 dModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。* W* I+ |- s2 [; d8 i

6 {/ U+ O2 ~% Y+ K2 E! {
* f7 U* |+ Y" V/ s& b/ p. \4 p7 pModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。; A. u% T6 }) s3 C1 x
) n- q7 ~7 u. X% _( h

( g  J$ a: n/ _5 U官网: https://www.modsecurity.org/
+ l8 E+ {% U* x; f3 f: ]( q- H' q% j1 T

/ Y  y+ f; l% I9 F什么是OWASP CRS
8 g& o/ ]& H+ ?! zOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
4 B. }4 Z9 X* {; X; ?
3 C7 j. ?+ U: X. q
' e* o/ r* p$ a* b4 b* @4 tModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
5 o  ]# w  Q4 p! a" N$ D7 I( T
7 R  S3 N. P1 h* i* t1 Q4 r& e( `6 m2 C+ \) b$ [" }
HTTP Protection(HTTP防御)
% u1 U3 M. d) t) E. v6 bHTTP协议和本地定义使用的detectsviolations策略。
7 u" \; C$ m" N8 `, I: n$ R  W$ U2 B$ R9 O$ T9 `) O* }

4 @. Q/ a; V. A* u9 wReal-time Blacklist Lookups(实时黑名单查询)* X' ^' u4 h% I; ?; j6 P  {
利用第三方IP名单。5 K: v. f6 r: J  r

+ j* y; }  G" G8 S3 M7 R& z+ B+ X1 b- z" M0 A
HTTP Denial of Service Protections(HTTP的拒绝服务保护)# L5 I; w* D: n+ P& F% ~) |% I
防御HTTP的洪水攻击和HTTP Dos攻击。% _6 X3 o! ]& C8 O# Q6 O
  ?8 X" b7 Y$ g5 N1 ]

7 a1 ?/ F& C6 C+ sCommon Web Attacks Protection(常见的Web攻击防护)& @& P1 k' k; s  d8 v3 a4 Z
检测常见的Web应用程序的安全攻击。
! _, Z" W2 w, C% t% H. _; K+ e
" C0 L6 _) ?& y3 K) v" A$ h2 W2 U
Automation Detection(自动化检测)/ d% C" g" x% L6 L# N# v
检测机器人,爬虫,扫描仪和其他表面恶意活动。& P; q* g1 ~( [2 S5 O
! v2 L, H8 Q+ A  m0 V/ H

6 k- d" z$ T. b, b9 AIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
8 x# G" `4 G; g& V* I检测通过Web应用程序上传的恶意文件。7 ^% W7 C/ b- n5 P
# s; H- L7 G! l7 c

5 w4 H/ W8 {- [" v" ZTracking Sensitive Data(跟踪敏感数据): ~6 r- E6 x5 W' h
信用卡通道的使用,并阻止泄漏。
" k' h5 V0 Z6 X( e& ]. \  A. g
6 m  V. S2 t# k2 i" e* c/ }0 `* y& l" ]. w0 @& H/ e1 e
Trojan Protection(木马防护)* H" k+ Y5 ?/ p' w: t
检测访问木马。0 v/ r) J2 z+ g0 @. p8 w
& v+ F4 K9 F& ?7 L
/ W/ _# L2 X9 p4 @
Identification of Application Defects(应用程序缺陷的鉴定)
6 c1 q6 b  |9 K' a9 d检测应用程序的错误配置警报。) z  b7 z2 h6 z. U& q0 |. `

2 C+ h/ k- Y% s; u, S4 `% S2 y3 i9 s
Error Detection and Hiding(错误检测和隐藏)
# D/ S& B! Y* X5 ]检测伪装服务器发送错误消息。  h  A; s  l- J4 q, D

, i' O* f% Z( t& r. D$ d+ ?1 r
4 H  Y. Q  ?! X1 A0 ^安装ModSecurity% S- h7 C* ~  c- Y: @: m; H2 y, Y  u
软件基础环境准备/ A! R( M* X! y9 ^
下载对应软件包0 L# }8 r5 A5 l! |* E
$ cd /root* y( a* u  y0 K6 ?% j! `
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
$ w1 i# g- v, x$ o0 f. i$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
3 J1 i/ v* w, ?安装Nginx和ModSecurity依赖包
1 k+ t0 v1 T. HCentos/RHEL/ H& Z/ w4 b6 O, `3 R' c
" a. I1 y. {7 @
" V+ O2 T6 a) c/ V3 ~* g6 u3 V
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel  q; o# P( l* q& q' L
Ubuntu/Debian
% y$ C0 S9 C' h4 J" ?% u
* O: d$ T2 Q/ |/ y& v( ?8 C/ u/ f7 C
$ 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  f: w  C/ f, @: S0 v) J2 I9 t
编译安装ModSecurity) H8 x) @6 `; |, V' C
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。3 G$ G/ n8 y. z2 A+ w
, L1 L; d2 C6 n
3 d8 `: J  g+ h% N' {
方法一:编译为Nginx静态模块
+ r; \' X! @& B# h: w! N9 @& l! X. t& O- z

, v$ t( J0 q* n/ _  [. B编译为独立模块(modsecurity-2.9.1)2 ^3 Q  M9 e: f( f/ j/ r
$ tar xzvf modsecurity-2.9.1.tar.gz& k' k, ^/ {- L* m
$ cd modsecurity-2.9.1/4 G9 B  e$ _9 y4 K7 ~/ O; C
$ ./autogen.sh  ?2 e* p& q( z/ u4 [6 a
$ ./configure --enable-standalone-module --disable-mlogc/ A0 ?- h+ B7 z. ~0 H. e
$ make
% G' i( x1 v! }$ x$ N, ?, L编译安装Nginx并添加ModSecurity模块
  [' q+ Z2 R1 g% }3 u$ tar xzvf nginx-1.9.2.tar.gz, |" o( [: u+ A/ b) n7 ?7 |- [3 ~
$ cd nginx-1.9.2& l& d$ c6 z/ Q% l
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/9 m1 ]& e$ H" Z) u! E& O  b4 O
$ make && make install
! r* j) k; c9 y: R# X# I% p方法二:编译通过ModSecurity-Nginx Connector加载的动态模块) n* L& Z" ~* M3 e
' O# T; ^  g7 ]3 Q
# |+ V, [; X' d% j. P0 \: v
编译LibModSecurity(modsecurity-3.0)0 P6 c6 B, W" c& V( G
$ cd /root/ l: d1 q" P, B8 }
$ git clone https://github.com/SpiderLabs/ModSecurity! ^+ N+ _3 F, Z
$ cd ModSecurity& s9 [, q: P3 m+ N
$ git checkout -b v3/master origin/v3/master# S3 }% \6 f# m
$ sh build.sh
/ ^4 A; F# q- m/ o; [3 I$ git submodule init* ]  O6 O: C5 _8 B/ B4 `" v
$ git submodule update% O- c& S  I$ g  K* f
$ ./configure
7 l( D, M8 z% F$ n0 S" E* V$ make, q7 r, Q6 S) ^5 i+ J8 N
$ make install5 Q+ Q4 A( b- y' i: f9 k, L
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
% [0 a; v: h: n  t& b5 ?
4 y" C; M& z5 J+ J( t2 E/ o" H! v4 G, w8 M' @
$ ls /usr/local/modsecurity/lib
: S$ ?/ a- Q! c9 N$ ilibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
* \8 s* i# j- j% A6 g" J编译安装Nginx并添加ModSecurity-Nginx Connector模块$ m' w1 U/ X' l& {- s# x7 u
使用ModSecurity-Nginx模块来连接LibModSecurity8 ?1 J9 X4 M' i3 K8 f1 t
% S, T* q9 W! \1 {

4 K  b: U0 h2 o( e" P2 l4 I$ cd /root% {! i' Q5 S5 b
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
9 n4 U+ y  |- R6 _0 Z$ tar xzvf nginx-1.9.2.tar.gz; [6 S4 D# S  ]( p% V$ {
$ cd nginx-1.9.2. L, H% j  I1 |- ?" s/ a$ n. o
$ ./configure --add-module=/root/modsecurity-nginx9 S" ~2 Y6 W8 K$ X  p6 V. G4 M
$ make8 R8 K: m6 y2 E2 g0 v$ d
$ make && make install
. W5 F% f5 c, w& B& O添加OWASP规则
9 r! V1 Y( s$ ]: Z! ^" MModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
* ^  V7 x0 E1 S/ ]7 A) |8 w2 Y* |4 J( N. K, e

8 T5 z: K3 k& ^. W, M' J下载OWASP规则并生成配置文件
1 J1 J/ Q. Z9 I8 ]5 L$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git. k& w. b; H: v$ H* t
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/% ?- E: V7 b; N+ G7 P
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
6 Z+ ]- v7 B4 f. ^; Z$ m$ cp crs-setup.conf.example  crs-setup.conf! `# Y0 G* X8 a( G, |
配置OWASP规则
1 g2 r) E4 A/ x! P" G% ?编辑crs-setup.conf文件: C7 i3 W/ Z/ H, K+ I. O

. r. V6 t1 f( b, x+ D$ e# S$ `6 X0 L, v# s
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
0 H9 t: d  [( C2 X. O* F( ^$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf+ \+ d- n) `" O# B# m+ j# I
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
: @7 d" W4 N* _( O$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf* K0 a% i( g, B5 {
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
+ o( H( p3 F& @+ D( W3 R1 }4 e+ |- R4 v  a  z- s9 n0 g

+ }, r/ {; r8 [: T启用ModSecurity模块和CRS规则
6 }& ~6 p" o: I+ a' m. B0 s复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。6 Y& B/ M: f& y4 v

$ V4 U5 U/ E) D6 ^
6 @" B5 p6 N0 P7 }, nmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。+ l7 n$ b) J* _$ ^7 W

  q1 [! e3 q( ?& P
2 e- e, l6 E; K, R6 P* [8 V6 |/ D$ cd /root/modsecurity-2.9.1/6 g. Z8 ?2 G, L7 X. X
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
; y# W4 |2 j6 U  |: s/ _$ cp unicode.mapping  /usr/local/nginx/conf/0 V: o, U/ M% S. o; T, c
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
; Y0 D4 t5 w$ E' ]6 k
6 c/ {, \. D& g6 Q  p; X6 P! u" W$ u3 ^5 F/ w% f* B
$ vim /usr/local/nginx/conf/modsecurity.conf
& G* p( D& i+ e, M% wSecRuleEngine On
5 p4 N1 ?. L8 E/ `/ m' @' LModSecurity中几个常用配置说明:1 V, X& ?8 K" q' |" W
8 Y- |6 g3 L, P
8 o8 O& a2 j; K) H( r
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。' V  k, a& i# Q+ X9 |; W
; Y0 L! t" h- c4 |' ?) ^

9 @6 o% b: L8 [2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
2 @' L* d" y  P  K  b, O5 L0 p$ W- D

) w  k. D7 d9 X; u0 \# `4 @3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。1 I2 V# ?4 A2 F* m5 s# a0 v- I

" f% R- Z& v, u8 u. Z- A; B, _/ I) K- E; ~, f! f6 B  f' z
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
2 t& {& i$ v8 r
/ A6 V" I7 K; ~: r3 f6 t- f! a5 g5 G6 c7 z. i2 F
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。& M+ Q! k/ q9 N0 U8 z- K

: o8 W0 u  k4 y; t( S! Z; \
1 R* ?0 d& Y7 m1 A3.x版本CRS4 d! t9 B' g/ ?$ Y* r. Z3 }9 z5 q
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
! s$ u$ i6 {4 V# 生成例外排除请求的配置文件5 x( E2 c0 `2 C1 o, M7 t
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf2 j* b$ g" O/ _. x
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
4 [% f1 I* w2 a8 P0 g3 M$ cp rules/*.data /usr/local/nginx/conf
( X0 U! {' ]: Q% W2 Z为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。$ V1 g: C4 g. k, v
$ C- \$ x; D6 o& f+ V
3 J; K, g9 ]/ ^0 q$ \. y
$ vim /usr/local/nginx/conf/modsec_includes.conf  K' X% O6 |" z0 n

* |1 t2 [2 \% \" `
[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

8 p% H0 |/ z- r8 L3 h- q$ L; N( l0 x& Z5 [$ e: T! z4 |& i
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
* J4 W/ U+ ?( ~& {+ V$ N6 ~; \* |* [# s7 b- v8 i

4 w8 z( c: d3 y$ U' w: y3 ~配置Nginx支持Modsecurity  S- d& ~; s! _, e/ ^3 @8 E# Y2 U! x
启用Modsecurity1 h, a' o0 _6 j% Q, L2 `0 v
使用静态模块加载的配置方法
% u6 D( r  }  H2 X! a# x8 @2 H% V在需要启用Modsecurity的主机的location下面加入下面两行即可:3 D- ]5 C, h: P9 ]1 |. j8 X7 U; O
' Z' j# y2 D* I6 l
+ n1 O1 I% G9 H6 s- P! Q; b/ W( L
ModSecurityEnabled on;
+ J: ]1 Q, `/ L, ^3 TModSecurityConfig modsec_includes.conf;$ K! ~. K4 L1 J9 h9 r) d( ^: N' [/ U
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。6 i1 v$ w1 b, P
. i' o0 S2 }. i  e

+ R! `. a2 A  D* s4 H% T- H7 z' _$ vim /usr/local/nginx/conf/nginx.conf4 |% n7 d" U3 _' ^& N4 o

3 d1 d% n5 \9 n, [! @1 l/ v( ?4 {" T+ T$ s
server {4 b- L" K7 K4 o& S3 e
  listen       80;
% N3 U! T; J) E6 p( p. [% \$ ]$ V  server_name  example.com;
* s0 a* x& Q) [1 }0 K. y
* B' H9 ]0 p4 J7 P
6 D- Y, P1 h# g2 H) }  location / {' z0 v$ d: v# I! L& P, B1 Q) G
    ModSecurityEnabled on;
& }1 l; J) s1 ~: F    ModSecurityConfig modsec_includes.conf;
( g  Q, \( [- c( I# W# V- |) O    root   html;+ @  R- c2 a" w( x
    index  index.html index.htm;7 Y' D' r, T' b! ~, `
  }
3 i  M1 O8 |& z0 f- v) o}
8 G' Y6 o7 P2 Y3 y/ c; B7 @使用动态模块加载的配置方法
- L6 i1 @- E7 W9 [0 ]7 F1 r在需要启用Modsecurity的主机的location下面加入下面两行即可:
( G9 t: z8 ^5 r# V% Y2 P8 w# T
4 f7 m- u$ h0 U* {/ ]( S2 v
* H/ I, P; l* W# F' \modsecurity on;
" x, c3 y) Z8 O3 K; b# D5 s1 U# {/ G6 amodsecurity_rules_file modsec_includes.conf;; s& ~& u+ B7 E+ z2 p6 R
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。# q  g! \+ x, Z7 B
/ w6 n2 Y6 I* t) v
% m$ C; N$ d) \: `0 N6 @2 ]
$ vim /usr/local/nginx/conf/nginx.conf# |! c5 A; W1 K5 C4 G; }6 M
- l( @0 M7 b0 v4 [) p3 w8 T* P
; C* K; Y/ V! }' L4 Q
server {
, w& C4 `, P+ V  listen  80;! l  `' z( M' m8 Q0 A- S; G: b
  server_name localhost mike.hi-linux.com;
# l% R8 _4 C9 q1 _! a1 V  access_log /var/log/nginx/yourdomain.log;$ ?) Z. D/ }( A, M
1 f. p3 G7 z' f, j" I

' F- f7 v# i) z1 s% y: B  location / {& S6 L( }+ C6 @0 Y
8 P7 b' W/ @; R0 j, f8 c

0 A& ?1 g% |7 E/ c% g9 c- J) K  modsecurity on;
, e/ n. Y, g) _( U: W1 K  modsecurity_rules_file modsec_includes.conf;
8 m' y# U, f+ P/ W  root   html;
6 h& e$ k6 d* Y' Q  index  index.html index.htm;
/ p( j( R' B# i5 @: d4 `& }) R, d; N}
+ k( r6 `$ c. i* h, [5 U}
4 A% Q: N6 K6 i7 ]* c$ h9 A' V& F; `验证Nginx配置文件
) k4 f. p: T3 g6 m& x$ V$ /usr/local/nginx/sbin/nginx -t
( e! q: t; @0 D; }4 t# Qnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok" q4 d# Z% k. p# Y5 U6 r* n
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
/ j3 K7 {2 @2 Q$ O' |3 F" T启动Nginx
2 Y5 q, @, k6 {# \+ T$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
5 t+ Q9 I$ r2 `( E+ p, f% O  D+ J) Q
2 _9 S4 a+ C% G6 s# N; k

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

8 F7 s3 Y9 I$ K8 P3 P" w7 x

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-4-23 17:15 , Processed in 0.074929 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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