找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11053|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
: f! l4 E" }( r9 _7 A- D; V" ]  d* f/ |4 L9 \! P

6 k. o6 v8 B8 m8 T在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
' z8 h  D% w5 b. P5 S2 R- ~* K! z
9 Y, W  Y- L5 q3 y$ O9 w5 q) ]5 x9 _" z
什么是ModSecurity* ]; G6 T9 o" @+ }- ~
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。8 ^" W* t4 G1 T0 B$ t$ b0 N

( `* `1 S+ y8 F
% }0 t3 S8 c' k) A  QModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。. h! o; @: [8 E9 O0 {
* ^1 U( |+ w  n* d

, I0 q" ^6 x! _' [! ]  U, GModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
7 M# K( Q$ V* \8 s) g
6 X2 z6 b( g+ f+ Z. b! |% k
7 K8 p& b5 X6 b4 V6 d官网: https://www.modsecurity.org/
0 R. k: n. k* W3 B
3 K9 F8 V8 A" a' j8 k$ i- R% r; w' P+ o1 `4 p, z
什么是OWASP CRS! D7 ^2 T) p) E
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
1 [3 V: m$ I& X- M
4 ~. M8 x$ o) g% I' T# D
5 R- [! b) D/ l, R; @6 JModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
3 a3 \, e: d: v9 B0 A0 b7 F% ^4 S! w( N- C# y7 K& u( ?) v- _1 l
$ S! n: w. e0 r% F; w* d: {$ O# W' X
HTTP Protection(HTTP防御). L! o1 q7 X2 ], c
HTTP协议和本地定义使用的detectsviolations策略。7 i: f+ b0 }  B; o' X
, @1 _- `. ^8 l, r+ [* N& R

( P" T: @1 H2 s! q' \2 PReal-time Blacklist Lookups(实时黑名单查询)* O& e: @6 j& Y% g: x# b
利用第三方IP名单。
. J. q7 r7 _6 s8 v% i  S5 J: }6 O% b/ X
4 q0 O9 p( o8 M* L2 s
HTTP Denial of Service Protections(HTTP的拒绝服务保护)+ D7 S) b+ p" A- w5 U
防御HTTP的洪水攻击和HTTP Dos攻击。/ b' J( o$ n  ]- \

9 p( y+ _1 x4 B3 ~$ ]; O6 H5 H0 U4 k7 b. c
Common Web Attacks Protection(常见的Web攻击防护)
  j$ U+ @* t" K' c& a2 p1 }检测常见的Web应用程序的安全攻击。+ k: o  G+ l. s* e8 ]# Q

. B* D9 C3 S6 o0 T. _% V& x4 K/ O0 z
Automation Detection(自动化检测)
6 N) q( i- X# {# }+ d2 |检测机器人,爬虫,扫描仪和其他表面恶意活动。
. N: ^/ L, t* o7 O8 j7 S; m$ q- b' \0 _

6 E: a6 Z# U: ~  y4 n1 QIntegration with AV Scanning for File Uploads(文件上传防病毒扫描), Y! w( G; _' z, A: f
检测通过Web应用程序上传的恶意文件。
- Q  g, y! r' b7 Y& I
& S/ {4 j% ]- m* q6 e4 ~' [; r  D9 I5 t$ k, W2 a
Tracking Sensitive Data(跟踪敏感数据)" ^/ d* Y  n) K3 E  g
信用卡通道的使用,并阻止泄漏。
2 a6 {8 W, T7 A( Y: }# y" _1 g$ K$ j& H

, p6 S5 Y3 R4 F. S/ H2 P7 N2 nTrojan Protection(木马防护)' W0 u: t! @2 D6 _
检测访问木马。
% V, x" t: S: _0 P5 u# Y
- @/ q1 P1 R( h5 t* _. V/ v: [5 H/ M+ m6 K( @# m" N9 A
Identification of Application Defects(应用程序缺陷的鉴定)8 c5 u* T2 I4 ?1 T: K$ _: Y
检测应用程序的错误配置警报。' }' w4 x- B0 F. u7 w  c, E* O
; B( t/ _. e5 P1 v) Z7 l

; m* e7 c, H+ z; M7 f: u- ^Error Detection and Hiding(错误检测和隐藏)6 E# M& R/ o- W) O0 W/ E# w, z8 N
检测伪装服务器发送错误消息。
7 d  ]5 e- f& }  R1 X4 S/ z( [) J, R* j) k' l0 H. t

, }0 Y* @7 n5 }2 V4 M安装ModSecurity6 I6 h' a: o$ ~% p. L2 O
软件基础环境准备
5 u& t" i& a/ P3 R下载对应软件包% }0 s, j+ N( Z2 o+ [7 m4 l
$ cd /root# I2 ~' N: H4 \" V. t& b# K
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'; X8 _5 v! ^  v& l
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
* }: C8 [% x1 M% l安装Nginx和ModSecurity依赖包5 B; o# u  y! `* I
Centos/RHEL
$ F& e+ L9 I; h+ l
& R; p4 x2 F: s4 A- g3 a* w
4 d2 X- P8 {7 T7 C$ P- r$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
7 V; Y9 H" d: a% g4 eUbuntu/Debian
+ ^! M1 T1 R1 \
! D% D. i8 z4 ^0 q& U2 y; K! v/ |6 d) H% Q, b4 X
$ 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
/ n& C2 j0 y# S  W+ Y6 C) E: Q编译安装ModSecurity# \9 i& s6 u5 i" d0 d( t& z# H
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
0 C+ w% b+ U+ b  \* D) Q3 b! [0 ]- k% w) R  G, D

9 u1 S% `  ^# A- }6 A( a方法一:编译为Nginx静态模块
7 ?0 y) i9 W; w! U; I5 U# q' u6 x! c% T& v: D' v' A3 y  n

& C2 m, \" C8 \/ W; A! ?0 C编译为独立模块(modsecurity-2.9.1)
- s& v4 c* q9 U) x# Q& f2 d. d; |! f$ tar xzvf modsecurity-2.9.1.tar.gz9 _9 i$ V( U/ @1 Y) H% a
$ cd modsecurity-2.9.1/
/ @5 w0 V0 [7 P/ [0 a$ ./autogen.sh9 d0 l6 w% _* J: u; f7 p: g% Z
$ ./configure --enable-standalone-module --disable-mlogc' }+ a/ l, R3 }% d3 [) {
$ make
6 [% C- |3 r" C$ P编译安装Nginx并添加ModSecurity模块* p$ \- z: H9 X; B9 c8 {
$ tar xzvf nginx-1.9.2.tar.gz! ]' K& b3 J) G5 \" c
$ cd nginx-1.9.2# ?0 y. }; r4 I" m, h1 k
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/5 r7 ~  L0 C, D2 t& ]
$ make && make install3 |- y$ m. h8 v6 i  k! A0 |1 P
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
7 [* M* J/ s- c; k7 e: n) H7 Z+ s6 [+ I
( ?9 ]  @) q4 _1 Q# A4 I9 s
6 f5 ~0 @0 ^, g# [& K2 I2 _编译LibModSecurity(modsecurity-3.0)) {+ _/ Z  o' i4 h9 i% Z$ y5 n" O. g5 {
$ cd /root( M6 g9 [. f- @
$ git clone https://github.com/SpiderLabs/ModSecurity
, s6 l$ F( I9 N8 [( e$ cd ModSecurity
" }, x+ @6 H( o$ git checkout -b v3/master origin/v3/master0 l1 j! c$ ?) W
$ sh build.sh+ v" U) a  v3 M* z" h2 H5 b
$ git submodule init! B, V1 [5 D0 Q7 f0 Q1 j& n
$ git submodule update3 N  C, [+ o0 P4 T: y7 @
$ ./configure" u. k+ O! n# H$ C1 s4 r
$ make
' P1 c+ v% |4 s9 P, S1 [) v$ make install5 Y- }' c4 x; [' V+ G8 c5 M1 V
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。5 e& G  [! Z/ q) f$ A* V, U* S/ l

& T& t. W1 M8 C$ z: G, w
# \3 s; ?( W9 F( X( w7 l$ ls /usr/local/modsecurity/lib, _7 G9 H0 x2 Y" f& a
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.04 o8 L8 y$ T$ @" |
编译安装Nginx并添加ModSecurity-Nginx Connector模块
. U3 @# c7 S! x! N& I使用ModSecurity-Nginx模块来连接LibModSecurity- `4 K8 e$ N. y2 K2 R% G

$ Y0 d- V% I% r) h
9 N' b5 J- \# k( W* S5 ]$ cd /root& q7 E3 ~) W3 V: L+ x3 E' s
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
) \9 B1 {6 F8 l. u. ?8 J) v$ tar xzvf nginx-1.9.2.tar.gz2 H% J3 Z: H+ p/ I9 I5 j! S; m
$ cd nginx-1.9.2
1 o, x  v* V9 |3 s7 [+ l$ ./configure --add-module=/root/modsecurity-nginx3 N4 b9 X1 A( A( F0 L
$ make" `8 L3 {( c1 p5 y4 K+ M% z+ f
$ make && make install, `" [  K  R/ V' w/ c
添加OWASP规则
0 w4 @3 @8 j/ c  \9 dModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
7 `# y! ~8 }2 O! I1 J
$ k5 G; J( x/ q$ g) a0 @
7 J1 o. M( N4 d' D8 P: f9 Y下载OWASP规则并生成配置文件, w. \- [8 u9 X' v) }
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
- k, ]* i; s8 C, N9 |$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
4 k/ t8 r8 S* R# I$ cd /usr/local/nginx/conf/owasp-modsecurity-crs7 i& h% A: A+ K8 v- E' B) L8 F
$ cp crs-setup.conf.example  crs-setup.conf
$ M  |3 u) b+ \' z4 i# i配置OWASP规则6 K& E1 C" y! I
编辑crs-setup.conf文件
# ~0 H: \! G: b- N  t2 t
; O1 O/ P; v6 |$ U) l( |. t) [" r/ Q0 m1 D: ^
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf  t$ u& r4 s, [# }+ h) O
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
* T3 L. a. Q0 ~6 ]' Z$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
/ V$ [- C3 \0 i# Z+ l$ T; N5 y; K8 J$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
# S4 R9 |/ I' O6 Q0 Y' X8 v6 _默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。. N+ A7 v( t; B3 S

! |4 E0 A. z% h$ {. a. g
$ b5 O) Y4 w3 H& u1 c/ _; T启用ModSecurity模块和CRS规则
$ w" `1 x" D% v( ^7 p0 u9 {复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。/ N) I& O6 V$ W: S

6 z% X+ ?1 k$ n' r0 P& w* m7 L  e! S. ?6 }) T  j6 w
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。3 y% b  Z5 j9 b8 W) {
8 R6 J' w' `( \: C
4 ^3 ^6 P0 z# f# p& B
$ cd /root/modsecurity-2.9.1/$ b6 Y9 j" x. _+ T/ c6 w$ Z
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  $ q6 p2 Q. `, d. O+ P' l( i$ j
$ cp unicode.mapping  /usr/local/nginx/conf/
/ N4 s3 a: m4 a4 E# S将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。  m' m6 [% d" ~. f. |& h

- h2 }) C( S9 ?2 U
  U' n( p6 B, s  I) \3 ?$ vim /usr/local/nginx/conf/modsecurity.conf& a2 o3 C: D% C1 T) ]
SecRuleEngine On! S6 `6 b+ n8 [5 [, s, b8 x( E
ModSecurity中几个常用配置说明:' n7 C1 y4 F( M; W* Q- i
0 a; s& v5 s* v, S! U5 {% G  o
9 v( ^6 C% n! y# ~3 G
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
3 V3 M6 ]8 A/ c- ^. F( ~2 T! k) L; g+ [1 v5 v; E

" @/ j! ~( r2 r% l/ {% y+ P; H( ^2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。2 u4 H" O/ Y( ~, y4 F, z

' h* H# {+ ]/ p; a3 L
2 ?4 V$ a# z. [2 A# n. r2 \3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。& Q, I! q; x0 F8 K. |

* D4 {0 B* A2 h
) L0 s( K$ O. ?* \# b( h( x! n* v4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
; ?9 r  i: C/ H& e$ Z  j4 H* A  l7 \  p0 `5 A! {) i
, d7 t& _+ @1 `" t* c" }' J* p; E
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。" g/ I% X; U1 `) ^

7 H! C1 C( \: Q
+ f( z! W9 ~+ c3.x版本CRS
0 @: }; `4 w/ i' A& [% z$ cd /usr/local/nginx/conf/owasp-modsecurity-crs  q1 U3 |( G; I. E4 U
# 生成例外排除请求的配置文件
$ h" g7 O* K( c' y5 E2 {' i$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
% c. r0 z7 C7 k+ @; I) L/ t5 _' J) g; e$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf  D  ^3 G7 V, k
$ cp rules/*.data /usr/local/nginx/conf$ i, L1 D# Z" u$ ^- R3 ^5 v
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
! d5 S% v. q# A* |
% f7 d0 H/ ]* A( ]5 T3 I& m5 i8 v6 u- x3 t6 O+ d6 i
$ vim /usr/local/nginx/conf/modsec_includes.conf
& n; M4 R' F( E2 o% ^7 X- b9 c) 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
1 l" t) Z* u: p- ?& H1 m# D

  d0 e* a7 V: B# `* Z. [! v注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
9 j. X" q3 a* }. q. N/ N# z" A$ O% u' I+ @* [
$ N- l- ?4 B/ X' S6 i* D" t" j8 n
配置Nginx支持Modsecurity+ @; G  Y1 K! I( ?7 r8 X; M
启用Modsecurity/ B& @' Y- {# L; X$ l
使用静态模块加载的配置方法* L9 b: ^% u! b! z& I" W8 e
在需要启用Modsecurity的主机的location下面加入下面两行即可:, E+ E* r' S6 Q
" S6 Q" |, _# P. c" P
/ c6 n- |, b% ]1 j
ModSecurityEnabled on;& d4 ~! C* Q1 o2 x
ModSecurityConfig modsec_includes.conf;5 z. F, [/ P+ w- G8 Z& I6 b
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
* I+ m" z' R5 Q. G
% |( f% M' L# ]% h; Q: m+ l
: I7 `+ a- S, x( G# ]$ vim /usr/local/nginx/conf/nginx.conf9 x5 E2 o8 ?) a# v3 U+ C1 ^
0 a) l# E8 {; w

( z* J& x6 u% y. dserver {
+ }. y; Y  l5 s9 a  o  listen       80;
; g& e8 }8 m0 R% y/ q. ^% t! c; x  server_name  example.com;& p0 O3 L0 W8 {
6 F7 `& G, z( G- [. P% s5 w

1 C1 X3 ^; \9 T" t7 @# W  location / {
1 T: s( f4 f, {    ModSecurityEnabled on;
+ y1 |' n8 C3 _' |% D" V    ModSecurityConfig modsec_includes.conf;6 y& x5 M  e4 A/ T: i; M8 _
    root   html;) j  l) ^. {  B; h* |, R/ q
    index  index.html index.htm;6 }8 }0 s) P$ k- V. r, p8 m* h
  }( [# n1 P5 C- g. s" f
}1 A0 X6 x( q9 u  T  }$ `3 `" H
使用动态模块加载的配置方法
) r6 S  T* p! r+ u% G; [在需要启用Modsecurity的主机的location下面加入下面两行即可:; K6 A$ \' n% [1 Z1 T! z, W5 [+ T) b
) i7 Z! u8 ?( ]3 i

8 b  }' @: {$ I# @modsecurity on;3 K7 k5 R3 X" N" R2 P4 `) R
modsecurity_rules_file modsec_includes.conf;  O/ g6 Q4 u. `. u# s7 k. {1 V
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。3 t# g! ]  y9 ~1 H  M1 B5 i

; T0 h: J7 H- @0 _' H/ x# w6 f7 a0 _( Z
$ vim /usr/local/nginx/conf/nginx.conf; x% e7 R1 c! B& c

3 W7 ]" B1 ]# ~9 P4 q& e2 e
; E8 \; B7 z& K/ C2 J* n- lserver {
9 b2 J$ |3 A, H& S9 R  listen  80;
9 J- n% `9 x* z* }% o1 Z  server_name localhost mike.hi-linux.com;
3 H# b  ]* ^9 s( O2 h0 t  access_log /var/log/nginx/yourdomain.log;& x" v# m; P* r  O! m

3 d9 u9 L$ Y# e0 q9 U0 ^8 w$ G' R/ h3 A& c, l- E
  location / {, i- o1 q& `; ?7 D2 K1 r& y* J$ ?
0 x* H( q& E4 y
' ~9 Y6 s* R0 U8 ^# Y% k
  modsecurity on;5 M* ?& F# S% V( ^1 M# U+ N: x2 Q
  modsecurity_rules_file modsec_includes.conf;$ ?- p& f( Q, `% k8 }- q6 s
  root   html;: b# w$ v. o. \& ~- \
  index  index.html index.htm;
* N# r! Z7 E) L6 m1 B* W# h$ y9 H}
9 }' v/ h: @' s- P5 n}
$ ?6 J0 h1 ?8 T0 a# I验证Nginx配置文件! |  n% h( B/ m* h, s" Q1 e* R% S
$ /usr/local/nginx/sbin/nginx -t: {4 m& u! h5 w. I3 B: t, K, f
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
/ x5 R  {4 s5 n. I$ h; B3 _' Y/ X, W& ynginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
& d) r9 Z, G, j. f9 e$ N9 W启动Nginx
' A: Z( \8 G: D* h6 r$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
/ w5 d) m' ^0 h* t% o" b
& B; W+ ?$ z5 l  q' b$ V/ K9 c

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


! m( p) b* g1 t, Q" l! x9 c

本帖子中包含更多资源

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

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

本版积分规则

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

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

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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