找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11139|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。2 ?/ B  ~* F) @% Q
: v% _& A0 B! e

7 K3 Q5 [: }; W在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。& o2 y4 E% H+ P4 s6 s

; V- b5 b- K  |- z6 I6 r; ?# N9 ~. O1 O  [/ J
什么是ModSecurity
0 x+ l- ~# Z) B0 q( JModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
) X: J$ |7 j- |7 `& x0 F  a3 ^( B

: l9 P/ f0 X" H3 nModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
$ c" l( w: F" C+ x/ T' C" S# I8 a& I* f4 e" o
7 I3 X+ l  ], t  ~! x0 \; f4 W
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。/ J) ?7 d, {- x: z: _
2 Y; n  W, F6 E4 I" f

5 T7 C4 _0 K; O: x官网: https://www.modsecurity.org/
( U1 m! j$ @4 X3 l- ^; ?0 i8 U7 a3 G5 l+ k, g, w2 ]7 e

, f0 f% c' E; X" v$ o什么是OWASP CRS
8 |; w0 R9 J' [3 dOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
" d- V, @8 v0 t2 v5 {
% Z1 j7 V' b# t4 s5 v. t3 Q
: {/ B, u- o* H  K  f  ^) JModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。8 `6 G  f7 M  h" @$ B9 b  p: m

& d+ E" O; z! ?* m  \1 s( _1 ~) o8 S2 w# a
HTTP Protection(HTTP防御)
' p3 M* I  A  U# U7 j+ s! MHTTP协议和本地定义使用的detectsviolations策略。: n" x1 A1 _) ?1 I6 s. h: s
& B3 K6 _% l) j# n) c
% A! N5 `& k9 o' I
Real-time Blacklist Lookups(实时黑名单查询)1 c1 f% }- F% I1 D. Y8 }  u% l
利用第三方IP名单。
7 @  J2 `( K8 K1 [7 C4 f0 i* c. v
& v" W0 P1 \" }! g) l: c% g3 B
0 b2 J: F/ ]& J0 ^  R, a; C) fHTTP Denial of Service Protections(HTTP的拒绝服务保护)
0 e9 d7 p" d" K* c. s- [7 }' b1 A防御HTTP的洪水攻击和HTTP Dos攻击。) u, V' t; D# V# G

. [$ s8 m* d- ]  _! }+ s
# f* v- k3 Z+ P( NCommon Web Attacks Protection(常见的Web攻击防护)
! V8 V3 E& j# O7 u5 F! @6 }检测常见的Web应用程序的安全攻击。* Q6 F9 b3 T- R6 v2 B# s

( b/ ?( I; L+ z* Z. X8 h9 C+ c, \) g3 v5 Z" K  o
Automation Detection(自动化检测)5 @. c( H2 T* D: K! e$ f* X7 Z' L
检测机器人,爬虫,扫描仪和其他表面恶意活动。+ _* D; V& l. D7 K, _3 z1 ?" E& A

, U5 Y  s& P3 j8 k' M5 m% \' i: K) u# k0 k9 `  X
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)1 W& z/ R  g: I1 m( A
检测通过Web应用程序上传的恶意文件。' ?8 @/ ]1 W6 U

1 B. T$ S6 E1 {! w- u# [: m6 H; T" e( i. F7 H  U- n6 c0 z& @; }
Tracking Sensitive Data(跟踪敏感数据)
0 o" z! z* i7 V信用卡通道的使用,并阻止泄漏。
3 x2 Q" r! D6 u  s" q8 @3 T' d$ Q4 `) R% p5 k3 l" R7 W% h9 T

8 g8 ]0 b. v. z  W! KTrojan Protection(木马防护)  C" p  v% ?4 `0 N% ?4 z8 m' ^
检测访问木马。
8 t/ _8 b6 p: Y/ W5 q$ L
9 D" ]4 p  ?2 Q) Z; S9 K% F  r' e4 A' p1 o- t
Identification of Application Defects(应用程序缺陷的鉴定)
, M# s% R1 c* t' d! _检测应用程序的错误配置警报。
$ u5 J1 o6 y: H. b
, _4 m2 t9 L/ S; E4 y7 V. [: c8 E5 w( m
Error Detection and Hiding(错误检测和隐藏)8 z9 w9 p% D. v3 \4 `7 V7 z! y+ D
检测伪装服务器发送错误消息。
8 ~0 q: v5 p) {/ u# L. C
2 [* o1 Z; E4 u7 D) E5 T# o% ~' M3 M  N5 w, q6 I; U
安装ModSecurity% K: C  a; P+ \' A* e! o
软件基础环境准备3 i0 ~2 R# j; {6 ~4 H# d
下载对应软件包/ k. [+ T" u8 }+ O" e; q
$ cd /root- a; ~* w: d) r3 D5 L* p
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
- s! N: N. ]% _, n8 ?. G: 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) n3 }: h# j) Y$ m, \
安装Nginx和ModSecurity依赖包
% w! v; ]+ N7 ?) q2 c  ECentos/RHEL, w( y2 h( p) ^, f
* _. e/ E. o  E9 i2 l& Y

$ U  L% T+ R' U- ]" ?! G$ ^; L6 a$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
9 W8 L4 _# x+ k# R+ _9 TUbuntu/Debian
" c' d6 f) i* x8 M
7 R7 P% @4 ], f. w" m& C- [( v. g3 `2 C) p
$ 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
+ a; |$ k/ p$ x7 h8 h& s编译安装ModSecurity( _5 ]4 P! b, P2 ^' n
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。; Z2 c6 g9 @& U2 \

  Z# p  A3 Z1 }9 d( H& d
/ G+ j4 D4 A0 o& M1 z方法一:编译为Nginx静态模块2 q  l! G4 X1 W* x( q0 l2 S
7 x  ]% a$ |9 b' k. j4 }7 o% O6 E

# L; v. B# {  o编译为独立模块(modsecurity-2.9.1)7 L6 _/ y; b" O, N5 l
$ tar xzvf modsecurity-2.9.1.tar.gz. k$ S+ ]& q7 R) L3 b6 r9 t) W
$ cd modsecurity-2.9.1/! N8 F  S% j' V/ m
$ ./autogen.sh
6 H& r9 F1 N; I, Q: {1 T$ ./configure --enable-standalone-module --disable-mlogc
' ]) n6 j) N1 m* f7 l, w$ make  U# \( M; L& z5 U% W
编译安装Nginx并添加ModSecurity模块, w* z8 o/ ~0 O5 ?/ ?( J' w9 T
$ tar xzvf nginx-1.9.2.tar.gz6 r$ ^0 F1 _% b$ e. ?- ]$ @1 J) q
$ cd nginx-1.9.2
5 w5 `* _$ x: H$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
% l+ s% b" e- ?; K% z: G$ make && make install
9 {, q1 F/ `. @% {方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
3 S2 i7 Z5 T3 v* A8 [, f7 z" _' \. s+ L/ v0 B4 t4 q- I/ h+ T
) T& Q/ [8 @+ a
编译LibModSecurity(modsecurity-3.0)
6 Y, ^8 F/ T. u- T" R, g& ^  ~$ cd /root
% b+ |1 p# J% U$ git clone https://github.com/SpiderLabs/ModSecurity
& r& X4 G% N2 k( P. Y+ N$ cd ModSecurity+ I, ^4 Z. ]& x$ v8 {1 x- e6 x' u
$ git checkout -b v3/master origin/v3/master/ w3 p' i; t+ H! l( k: R; ]0 k
$ sh build.sh
* J: I4 B6 @. r6 Y$ git submodule init
9 ?9 ^& H' `4 q% b: c$ git submodule update3 w' O, \* Q8 o+ U# j
$ ./configure& j$ d$ j5 e8 J- \7 R7 p
$ make3 o  D4 S0 Q2 e, \
$ make install
6 o4 @0 j) G: w+ Y2 N6 yLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
% s1 l! m. e, E; l$ x7 h- l5 @% K% E9 c/ D" u
; V' J( V7 X- g* a: Z
$ ls /usr/local/modsecurity/lib7 D7 m/ u% ^: ^8 k% A+ K: l
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0  Y" G/ V' Q; |/ b( b
编译安装Nginx并添加ModSecurity-Nginx Connector模块  }$ x$ O4 q8 _) p. u: q$ C' Q
使用ModSecurity-Nginx模块来连接LibModSecurity2 Z- f2 d2 v7 i, q

" C7 {: N; j. F: d+ ~4 @: ?* c) ?  L6 g2 ^0 `
$ cd /root  m% R+ K+ q1 i3 y
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
: _. w0 ^0 P6 n5 G* J& Z9 S$ tar xzvf nginx-1.9.2.tar.gz
) Y6 Q; }/ L, g3 b4 b$ cd nginx-1.9.2$ g* c4 \" J  R6 S  l3 Y( z4 d/ X
$ ./configure --add-module=/root/modsecurity-nginx
. a& u- t5 F+ n$ make
! v5 b  [8 `, ?+ ?/ S3 s# `$ make && make install
3 S  J6 v/ `7 d) C9 F/ {7 U添加OWASP规则# K# ?' B5 r) A  X# Z
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
7 ~6 C/ V# o3 P, F* B& _. T0 E; C7 R
9 p  b7 U0 I; f6 f+ H1 d9 ^
下载OWASP规则并生成配置文件
$ C( B. ?  z3 p& ?& G$ v* t$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git- \2 v" Y6 N7 j' L9 O2 k
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
4 S# e6 w: E! G/ U8 Q; }7 E/ w3 `$ cd /usr/local/nginx/conf/owasp-modsecurity-crs- G' v, U1 n: S- E
$ cp crs-setup.conf.example  crs-setup.conf
9 b6 \% y4 ^" f8 g2 s配置OWASP规则# v7 p# f7 ]5 Q+ q3 w/ S
编辑crs-setup.conf文件, D( m7 U2 Q  A
% C* O% Y& v8 F4 y/ N
, j, s* n* M+ |- Q! U6 G
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf* A2 S0 `# ^  [$ C0 A* j) e; a9 B
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
5 |5 g& x/ Z: c" S/ z$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
, f: `# F! M# z' ~$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf; |4 D7 I* j. G" w  D
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。+ \# `( ^$ a% u& b: m
5 A2 r  h6 I. b  {# ]
+ |7 o+ V& {& \% M) M- v1 ]
启用ModSecurity模块和CRS规则9 u7 ?+ }) ~& T
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。+ h! C7 `1 _/ b+ j

( H! j% ^, J; _# d9 `/ L
$ j! A- J' z- R  w, b3 G0 E/ O4 xmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。2 D6 Y* ]8 s3 P3 Y: m2 ?
+ ^9 u# _% x3 U0 H7 B; H$ M! B

+ p& c2 t, D" G& |) O: b) u+ i5 j$ cd /root/modsecurity-2.9.1/0 C, b0 b. |3 j% C
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
4 K" i% s, r* y4 f$ cp unicode.mapping  /usr/local/nginx/conf/
  Q! n% `) @9 S  w2 W4 e8 z将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
5 H5 S( L: F" V- N7 g# `8 }5 z: C) u/ ]
8 }, R* d; N$ n  g. c" B  @0 G
$ vim /usr/local/nginx/conf/modsecurity.conf
& G% O1 @2 v0 }' c) T/ j/ D4 USecRuleEngine On
& B, e* V9 \0 U8 Y6 SModSecurity中几个常用配置说明:
2 T3 t. U2 ?* y6 g% A! t- G8 j  b) s& c. ~8 l

" _6 j' o% i6 C' G1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。1 c) e  |7 A) f/ O1 u
7 H% e8 p9 G% j/ h$ \0 F& X, M
. s1 x& @5 B( b2 U
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。  H) n& H4 c% p( f+ d% u
8 w; [& R" I, K; q+ Z
+ q! H( V" @2 {3 g6 U
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。$ @+ Q6 F& w0 w6 w* x2 C1 k1 x
* I- s; k# s2 C0 b; e

7 I/ ^; i, t% Y+ O8 Z: d4 Q1 b4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。7 g) D, |5 c- m# ~5 [7 a4 U: e
) {: p! E+ @1 Y
# E% M: k9 G6 q; ~
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。+ B6 ~- I$ B' F- z4 B/ v

1 x* {* G3 B" [  r
( x# @1 n2 {$ p0 ~3.x版本CRS
, u3 `' _, _( q& a- r2 T& Q$ cd /usr/local/nginx/conf/owasp-modsecurity-crs) {* L6 S5 D; L3 H
# 生成例外排除请求的配置文件
4 R# s" E$ K2 k2 c& j* u$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf  o' h4 `3 n) v( s
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf4 N# t# G- W3 {8 v. B0 X3 K
$ cp rules/*.data /usr/local/nginx/conf3 A, e! q9 u1 `8 O
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。# s- ~. Z. _" v: Q. ]; Y  q
8 r7 Y& j  m- {) i$ ^. F+ p

9 {- h& }' V+ ?$ vim /usr/local/nginx/conf/modsec_includes.conf8 b9 N# M+ p/ h+ L, v/ c
5 c1 K% o/ I" K; C0 C- [% J/ }
[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
! L4 n0 z7 j& A. u2 I5 I, G
! N, ]# {$ d# A
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。6 f) v% H* A6 d- Z  O- D

( |/ i; x5 h* u6 q) A& W0 Q2 ]2 u9 ~2 B0 f
配置Nginx支持Modsecurity
& o9 ?+ [7 d& k% T% Q启用Modsecurity% g8 u% I/ x+ m/ J& Q, u' |
使用静态模块加载的配置方法+ R+ P& J* F! u2 H) {" ~9 K
在需要启用Modsecurity的主机的location下面加入下面两行即可:5 }, [7 G( G) F8 ]

0 g0 u# `: i. S7 U/ C0 Y' [  [/ N1 }2 ?* h( y
ModSecurityEnabled on;7 c0 k! h. M% x4 `8 `+ z% z9 n5 r
ModSecurityConfig modsec_includes.conf;
3 Z3 S/ u2 `4 _5 A; R9 J# H修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。. g8 P' s' j) Q- K
3 o5 o+ x1 g9 V# H0 D9 J
! f5 G$ ]/ {% F
$ vim /usr/local/nginx/conf/nginx.conf$ r  S+ k6 |- i

) E6 U3 _1 ~: X% O, P- k3 E9 b1 E6 s7 S
server {0 j$ B, w! i& ~4 f# c
  listen       80;
. a8 t. b# g. Y/ F$ e  server_name  example.com;5 p9 I/ I4 d$ K2 F/ x
2 D/ U5 R  [/ Y

* S' [, u3 e' h/ S( n2 Z! G6 [( p0 m  location / {; x( g* [" S% a
    ModSecurityEnabled on;
3 s5 }; p- t0 N0 N    ModSecurityConfig modsec_includes.conf;9 N$ Q  L$ h# J$ s) r0 ~
    root   html;
+ {- n4 B% e: k' ?    index  index.html index.htm;
" x" v# Q" t+ j0 s5 f2 v/ q/ `  }  i7 c$ X- K$ }4 n
}/ t; W5 O5 }" Z/ G& k5 R, Q
使用动态模块加载的配置方法" g- q  w/ E3 }$ G) ?5 X
在需要启用Modsecurity的主机的location下面加入下面两行即可:0 L& h) l/ t$ b5 k$ A* L2 H
3 a" I! e! o' o1 p# w( K5 v# H2 p

3 ?+ R6 J/ `3 ?0 y4 p% T0 \modsecurity on;
2 K; E( w/ q* P9 _2 lmodsecurity_rules_file modsec_includes.conf;4 S' c, }. w' t$ C3 e& c1 |$ p
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
& e/ Y1 J! S6 V+ e4 a( k1 e" D0 x: {# [7 d  _# Q: g: u6 m0 x! J  F
8 j0 E' P% e) `! b
$ vim /usr/local/nginx/conf/nginx.conf0 _- t1 h4 P* K1 k
1 E. Z5 n; B( p) J
- d* d0 K8 }5 h5 o, R
server {1 H9 i2 V% j6 j+ K# _. G
  listen  80;
' h! s) e# W' G/ b$ }1 C3 L  server_name localhost mike.hi-linux.com;
% E$ {; z: ^! ^  access_log /var/log/nginx/yourdomain.log;
1 k2 ]" K/ F- y) U6 J6 M% |
. H4 U" a, I5 ^8 J% h/ Z/ L* H' M, N+ i+ `
  location / {2 r5 a; o3 S% _- b8 W+ o$ |& g5 J
3 w" l2 }+ u! _
* h4 [5 b/ o$ E$ S) P
  modsecurity on;
" J7 l4 P# `4 f' l1 h  modsecurity_rules_file modsec_includes.conf;; Y0 G" \/ |& s! c
  root   html;
8 t! l* g- ~( \, s+ {+ D$ i  index  index.html index.htm;- y% {1 \3 e  i( N
}9 J8 X3 A9 D: X9 f0 P/ p
}
) S1 n1 W4 A  s! D! H' e! K2 U/ s验证Nginx配置文件
# u. P) v$ v. K" k2 O4 J( f" K$ /usr/local/nginx/sbin/nginx -t* u% b& A0 S! F" E$ J
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok) T* g4 y" t1 }  C4 c* m1 [+ e0 Y
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  X& f( [- ^) F7 }3 }' }, G启动Nginx
! ~0 z& T# a# R" w1 x$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf( s+ L, `) e& C+ U& ?( J
& D# M) ~( c7 n) [& 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能够战胜更多复杂/未知/混淆的攻击模式。


: f7 v, h' U5 z$ |! V2 g0 s

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-5-11 05:15 , Processed in 0.077944 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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