找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10706|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
) _1 f) [3 e- q# d5 o2 E4 e! K3 X9 x" e1 V7 i# [" M7 U

5 I( V3 q* k* B: b7 W  F在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。5 E9 _, A3 ^$ J

) d2 ~$ E! d: H9 \! H, z/ Y: f( j' G  P1 i( ~- F. b/ {$ _( |
什么是ModSecurity8 K* J; J9 M. l0 A& F. d
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
0 Z- `; B' ?# x. `) M7 h1 W* ^6 B# f/ ?( w& f
3 e4 a. A5 v" \, z
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
5 H  L$ \4 I0 i
0 e( Q" d2 W4 n& |: }4 h9 n# }: j1 Q6 Z) f9 h4 ~8 F7 W- ]0 T) W
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。  W  ?! p/ [6 o

/ B! \) J! X- ^  _; E# a; X) `* I" T; O% a  m- P  A# y. I
官网: https://www.modsecurity.org/
& a. z+ i+ G9 k: a' z
" O/ {6 t" \7 r3 R
' n8 u- P% z2 b& O$ R' T- ?6 h什么是OWASP CRS
8 B- }5 i5 Q: [$ S$ M( ]OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
6 D  Y5 G! C5 @2 o' z! f& _) o
( R( b8 f  u7 `2 O
3 s9 T8 I, }& J& BModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。0 H2 a0 W: u! y3 Z9 I

- w7 I4 n1 v0 p$ u* U1 L$ Y! f/ k+ t# Q/ f# Y4 Y, @6 c* w
HTTP Protection(HTTP防御)
1 G1 D" a0 G9 r6 Z- i, a* uHTTP协议和本地定义使用的detectsviolations策略。* E$ R0 Z, k; U

! s) Q% Q: a  M1 R9 M# d
. E$ e( V$ ?! D6 LReal-time Blacklist Lookups(实时黑名单查询). a( s' X& ?( \9 o* H6 _: G' v
利用第三方IP名单。
/ Q8 I" I% h& \3 d8 ?& \& h' C" {9 p7 q- z1 G; e, D. Y
! O5 q. S' \7 S# ]. p0 b8 k# h
HTTP Denial of Service Protections(HTTP的拒绝服务保护). n9 O9 G/ ?3 {3 n; r: U. y
防御HTTP的洪水攻击和HTTP Dos攻击。" c' i2 `& n- j6 h6 u' N/ _( c
( e7 R5 P8 t# F

. C4 ~( j0 P$ H* \Common Web Attacks Protection(常见的Web攻击防护)
) M0 K- v, n+ C: u( I* j9 r: ~1 s检测常见的Web应用程序的安全攻击。
3 j/ H, G/ ?' u, F) O/ ^' C2 u# V. O3 u5 q% @
# c8 m( ], D. ^; `4 F# ^
Automation Detection(自动化检测)  s- n; _) c9 }' y+ M8 \9 B
检测机器人,爬虫,扫描仪和其他表面恶意活动。" g, a+ ?' c" d5 p

6 c: F; K7 \6 i* I( ^& z( T1 z. h; u) G+ Q$ b& ?5 _5 b
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
  b4 Q0 W7 o6 E" u3 H检测通过Web应用程序上传的恶意文件。8 q+ |8 k7 N9 |  M' {+ Y( q; l$ ?" L
; u2 d1 b: V8 T: ^3 f3 ]# X6 `/ f
  g$ a" p2 V" V$ e( O
Tracking Sensitive Data(跟踪敏感数据)
% m* Z$ |$ {1 l! j. I; g+ [$ C信用卡通道的使用,并阻止泄漏。7 W4 c* ^  X; Q. T

$ n/ l/ m6 ^% R
. x/ ~; Z* ]3 a7 NTrojan Protection(木马防护)
) C4 T) M; g8 ]8 o) O检测访问木马。1 g1 s! H, T8 A/ q0 L$ L: u7 n

/ L! ]* j% r3 ^% |: k* g
0 j+ O# j) g4 b, B% ]* l7 XIdentification of Application Defects(应用程序缺陷的鉴定)
7 ~7 d; W+ p4 T6 ~- r/ _: V$ N检测应用程序的错误配置警报。- k) S! w# ?/ n! M2 f1 \' f, ]' [
% \: `2 ]8 d6 s  A: G# g

7 c5 y- i8 y. {4 ~Error Detection and Hiding(错误检测和隐藏)
5 u" L8 }; p" ]8 e% `; F- ]( P+ C! l检测伪装服务器发送错误消息。4 u5 D, u- I; v# {. X" V, @: g' [

0 E: ~- S6 S% _1 s5 Y
; e" H7 H/ ~3 P0 ~9 d( V安装ModSecurity* \4 N5 g# P& k9 Q) q- C% E$ e
软件基础环境准备
0 A! j: U6 f, q下载对应软件包
, ]# a. B+ o) O4 I' u4 Z$ cd /root: F/ {& w4 Y; L( H# `0 P  U
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
& O8 A: V( o. V: M: S$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
9 }2 [0 O- O* z9 ~+ t- B安装Nginx和ModSecurity依赖包* I( K  \( s7 B+ ~  t' d, z) `; x0 R
Centos/RHEL* W) ~) ^1 f. y+ L* o2 \& y

) L' a2 _2 d2 M; L( ?7 P4 V; _* _# O9 Q5 a' T- z
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel0 u. a# e% W/ u: z* g: i( |
Ubuntu/Debian5 |6 W# x9 J7 K2 @" I2 Q' G

% Z) ?; V! N8 o7 [+ l+ j9 K1 [) g2 m, }7 B
$ 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
6 O# u+ Q( [$ N# L  |' T+ c! x编译安装ModSecurity2 Y9 K8 _2 ]% a  o# k% ~
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。$ ]6 `: b( P  K

; e, M& R& `1 J% ^' r0 k2 J0 e7 C" g. e- ?; L9 e# a
方法一:编译为Nginx静态模块
4 I2 [$ z( b8 n8 k; Q7 a) \* d; f  L' H: E8 I; q2 `

1 ^, X* @, n7 _, R# O+ I7 S, Z编译为独立模块(modsecurity-2.9.1)
" Y4 m. G9 h/ l! X8 p6 R$ tar xzvf modsecurity-2.9.1.tar.gz  e) R8 {  j# l* q2 Y
$ cd modsecurity-2.9.1/$ Q$ q3 d* q# m! y8 T
$ ./autogen.sh
4 p/ R: ?8 g# e- l7 m7 ]$ ./configure --enable-standalone-module --disable-mlogc% l, q. z' ]  k; {, {' @+ G
$ make
6 g6 Q. c" I2 }! G4 G7 c7 \6 P编译安装Nginx并添加ModSecurity模块
7 O6 ^" k  b  w$ tar xzvf nginx-1.9.2.tar.gz
. b+ w1 n0 B7 ^  Q; V- O6 ?$ cd nginx-1.9.28 K# y6 }/ P* y3 H
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
  }* e5 v! w3 [$ make && make install# i* t5 m7 K# U$ t8 U& H0 \$ a
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
4 i) k! m/ @6 W: V4 z1 t" v2 B9 T: h/ f8 ]) X- e% I. z

3 n# ?1 P& ]1 c4 z编译LibModSecurity(modsecurity-3.0)7 p# N4 V8 X* U5 J, s) z& h# ]1 ^
$ cd /root
) L- Z# C+ a  H9 L$ git clone https://github.com/SpiderLabs/ModSecurity" _8 y  P* |/ i$ g
$ cd ModSecurity
1 J( E6 @" ?$ x. t  D- Z, G$ git checkout -b v3/master origin/v3/master
3 W. ]; I" g2 T7 d) I  `; P$ sh build.sh
+ M  N$ j; A: j* ~2 L  A- |3 P$ git submodule init
( n8 Z- D3 d; y$ git submodule update
6 y5 J* F1 P- Z9 J- n: T  j$ ./configure% {7 X# R7 W! \4 b( T9 D; V
$ make
8 }# \/ a9 Y) G8 p. n8 E; B# m3 }$ make install
0 z  S; E2 I' v6 b) }LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
& ~6 ]* q' E: e% s# p# K* s* v& |" `/ V& r

' S+ i1 r* S* [$ q: ~$ ls /usr/local/modsecurity/lib* ^/ R* B- S- l" \) z, O
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0. l! o" S  C& T8 e: \
编译安装Nginx并添加ModSecurity-Nginx Connector模块
: d0 n  I7 ^+ v  U  }2 z# G使用ModSecurity-Nginx模块来连接LibModSecurity- [. w; F& f! D

7 Y8 b' c" \; ?1 Z! X, w) G/ g- l4 V& F9 O6 U* e
$ cd /root/ \2 z7 S# ]2 C; g/ {9 A+ \0 {0 n
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
# g5 }5 S2 S1 d: A* z& l$ tar xzvf nginx-1.9.2.tar.gz
- c1 a8 a5 l! y7 ~' U$ cd nginx-1.9.25 A7 S$ r5 t1 ]
$ ./configure --add-module=/root/modsecurity-nginx
* x2 _( ?8 L" x( o, {& U2 ^" V$ make( Z- {  e5 C% x. b" s
$ make && make install
9 S% r  l3 w9 h3 S. ]添加OWASP规则$ ~' w: q' F7 W& U% A1 w/ ?* ~! D
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。. q$ W& Q* ]5 J# A$ R

5 Q- Y& z' L5 f
  I4 S7 r3 c3 i1 z下载OWASP规则并生成配置文件( F1 O/ D) Y' H* V( c* _$ G. E
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git; F# H2 o* w# \& V2 ?
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
* [7 G2 h# e9 x; J# w% L$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
$ t1 }( V0 z: Q$ y4 k2 ^$ cp crs-setup.conf.example  crs-setup.conf) W* {& D6 ?5 _1 s) S
配置OWASP规则
! f, I% I8 M& n/ _编辑crs-setup.conf文件& ?7 R' H8 B6 G" Q
% Z7 i- }$ x! y9 u( M0 K" ]
0 l) {4 @1 {" t) b
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
" o5 o* N% k( `' ]' a0 A$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
' b0 X' A' P/ ]6 p+ h$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf3 v2 Y( ^2 w9 p* L/ G7 X2 `! R9 e: D
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
( s: K# @( |3 u$ V9 \: v默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
0 ~, j8 S5 |  o2 B: z7 m9 n3 A$ Q. T7 E0 Y" N# Q% ~7 l9 U
) Z+ b; ?" d& B/ v* S' a" l7 H/ L' }$ v
启用ModSecurity模块和CRS规则
" I- Z2 _: [+ L7 w9 Z0 P9 j( a复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。! b; s3 g2 e0 ^$ S3 E+ }

+ R1 G- X. h! i. x! J, @4 R+ |# y2 Z  Y: i7 `% \5 g( b
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。$ b2 [' s8 j" p1 U5 ]9 {$ i
# U7 t+ v4 p+ p, g1 i% U
3 {" U5 [; `4 I5 K
$ cd /root/modsecurity-2.9.1/) x8 f' ~+ q* a8 v2 p: }' ^
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  ' z$ R3 O7 }2 o* L
$ cp unicode.mapping  /usr/local/nginx/conf/
/ x% `8 j+ w8 s1 S将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
; O! E1 a% z  a! ~* `+ c6 ~3 X9 z1 A6 B- ^0 u
4 \$ v+ H% E, A4 h" g
$ vim /usr/local/nginx/conf/modsecurity.conf
; E. H9 R* k5 V- M7 N5 D" j+ [SecRuleEngine On. k: J. {: o/ `
ModSecurity中几个常用配置说明:
) T: N, [2 X- r- d6 v& g: d7 v6 a2 m% T3 P; K1 s

7 k+ A8 e4 V. o1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
7 J6 I7 F) _! y; D7 z- g+ I! a, r4 H  ^/ F9 R: ?0 Z# C4 b" ]
, M1 B1 W" L" G, u
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
+ o, o. B& ^( v) C5 D7 n
' @1 g3 l  D; h+ ~2 _
  m7 L3 O; c( j/ P6 d5 q: i, H% E3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。9 O! m- u  E3 F1 m
8 v* q4 ^; {; y. m0 }4 d; O6 J

0 g3 e/ g: V2 o0 v5 }2 J4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
- J& U2 ~, H2 C  S: h- ]' m
/ `- o0 ~6 R+ W; Z' ?( J# ~) H  A  K( l
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。& a2 @/ A) u3 q1 \6 [
+ K$ l$ k; x  Y  V  Z) [
1 N5 A) D- k! f! h: x" e
3.x版本CRS
2 v# S4 X8 x4 S9 r+ Z5 x$ cd /usr/local/nginx/conf/owasp-modsecurity-crs* w: j' s9 y' E0 m
# 生成例外排除请求的配置文件, F7 z. Y$ t3 x# ~, r
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf8 a- O& C- |6 N' }
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf, ]' X1 Y7 W& F! n# e
$ cp rules/*.data /usr/local/nginx/conf
+ D4 o; R" f9 z7 S: A为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。7 Y$ w3 q% A  G2 }  l8 x" W  k* |
/ R0 B; r# Z7 U0 A8 W0 y- x) d

" D) V$ x+ O1 U; L. @$ vim /usr/local/nginx/conf/modsec_includes.conf
+ Z) w! X- H% L; ~2 F8 J9 Z8 G/ x/ z3 o( w
[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
, `9 p! F, p4 p& V0 X' t' O/ k
- I; Q# F5 [4 O  O' p( ]
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
2 C6 U+ y" o8 k
2 \) ?, d3 F( s9 ~6 x% S! H/ A* h9 I2 _/ @( N7 C4 X: e( {
配置Nginx支持Modsecurity
  ]* m, P: K: U% u) S! a0 @6 S启用Modsecurity
9 x0 }+ b" w9 y$ ~7 |4 y8 |使用静态模块加载的配置方法! d; e; h2 w$ o7 q6 r" o
在需要启用Modsecurity的主机的location下面加入下面两行即可:
# q$ l* F9 W; r& K& o, `7 [* M( h9 Q  j7 b/ O% Q1 |
4 s' `+ v0 _7 g/ w
ModSecurityEnabled on;
6 t/ T4 B( ~( M/ j9 G8 yModSecurityConfig modsec_includes.conf;" k: Y: P% O3 @2 C  H& \# t
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
' X' \0 |% w" w
; J8 J$ P- I; p  \- T) u3 Z) |  S$ t1 t% O/ ^
$ vim /usr/local/nginx/conf/nginx.conf
  ]9 {. n& X- V1 i# z8 }0 w/ o$ R' w; z: G5 L( c
: O" V  V* Y6 f( y$ n5 {& i
server {
3 G. }+ o  G( h, {9 z. \' S0 K3 t4 S  listen       80;
  [( u$ B" Q* _2 H! G1 D  server_name  example.com;
. n' T4 j: [" N& c+ r, n% A  P7 u- L/ J9 |. f4 x
5 V# s* ^2 C$ d4 J2 o' I2 c8 r
  location / {4 G, X( h: m2 I& t7 d/ [
    ModSecurityEnabled on;
6 t0 F# L! a3 |& G4 n$ L) _    ModSecurityConfig modsec_includes.conf;! H0 [3 k3 `- h
    root   html;
6 w$ m0 R* c  A3 t    index  index.html index.htm;7 R8 s: X  W  N+ J7 N9 g! }) P
  }$ x# T1 p' c. O7 n8 c8 S, \- a
}
! c' O( L) N, h% |# C  D使用动态模块加载的配置方法- e3 A. b  {% A4 {$ v3 q! R  u0 p& [9 D
在需要启用Modsecurity的主机的location下面加入下面两行即可:
" O5 Y! y4 B; V0 \( D
1 r; A; r, q/ R3 N2 |; D8 `3 D" P  ~. g5 |7 @4 n
modsecurity on;7 w) K# B6 H2 ~, |' W! K9 n/ g
modsecurity_rules_file modsec_includes.conf;7 A) K& [. x8 j
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
$ _+ P" F9 [& w" U7 r: y7 C& V' i# Y3 A/ F

* Y! O; N9 y' @3 o2 |$ vim /usr/local/nginx/conf/nginx.conf! f- N$ s% Y" @6 K# S) w- r% v

7 U% v7 w/ Y0 S  B  y9 y! ]" F) ?8 ~! V4 F! g$ {0 J# k6 h
server {" g/ b- @" c5 K7 \" x# m# H
  listen  80;7 j4 U7 t! l5 I: f& `
  server_name localhost mike.hi-linux.com;
" [- p+ g# A7 {9 J/ w  p. k  access_log /var/log/nginx/yourdomain.log;. Z& Y  C4 g$ k* F2 v$ |

7 q+ x- e' H0 {* g
" H& u, n: `- n" b/ c  `7 f# H  location / {
, a, N; _0 S8 L6 `- `6 B1 V" C2 \: v) J3 o$ u0 E) E0 H# j; K
4 \6 p3 n2 M5 D+ K7 p- n! h
  modsecurity on;
, q- N# B# I9 r, Z. q2 A+ C  modsecurity_rules_file modsec_includes.conf;
0 j" D2 l2 j1 L, v9 b9 {  root   html;8 h! a6 U9 L! T3 X7 r$ ~( G% N
  index  index.html index.htm;% X  b8 A* @6 m* ]7 e0 W6 ~$ _& c- O4 Z, d
}
) x/ H; q& o* B$ `}; ]- Q. a7 X! g9 v9 l; N, C
验证Nginx配置文件+ L9 M. b) T6 e$ m
$ /usr/local/nginx/sbin/nginx -t) j& J$ b3 b/ c; q
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
" T' {1 L2 q5 B! ~' I9 l: l/ Bnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
9 O3 l% _' C5 b- ]. ]- U- O启动Nginx" @( v3 g! b" I6 v( y
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
* G7 W" O6 k  S8 }3 ?' Q; d1 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能够战胜更多复杂/未知/混淆的攻击模式。

9 F( w! i: [6 z, `

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-4-3 09:22 , Processed in 0.112442 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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