找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12561|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。. X  ]! ]2 s. Y, [( U$ N8 c
0 D2 ^+ e# P5 h- U/ q  ^" N

  X, ^" i8 V0 ?7 `/ Q% u6 }; c在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
4 U4 q* r4 ^( s$ }% q- s* z. n/ n2 A4 v* f. v* q

# P# k/ n% A( j4 v% Q% \6 J什么是ModSecurity; \6 e. _/ I. G* L, c9 u# U# N* ^
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。* j. S1 V5 P' I3 X! b" I
  T2 J; X: h, b9 C: a) A% @+ G
+ g- h# K5 ^' U7 ^6 k5 z
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
5 m3 e4 ?7 Q' u0 F+ G: d$ J
  E1 C" N: a" g
9 v7 @2 h( b4 {1 K9 wModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
1 f7 J; X+ |* \; B4 M( T& m9 ~
8 m9 e$ x3 N0 f0 e! I7 p, G1 L2 O( b+ m4 I% Z, {' B
官网: https://www.modsecurity.org/& M# a; T3 @9 }

/ S' m6 x0 y4 R, ^
. L4 W  J) ~4 ]: J什么是OWASP CRS+ U/ X( W( `5 B" C) P/ ^$ K
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
# K" C' T$ t/ `( a/ Q
  z9 ~. j* ?: Z8 i9 z  B" P$ w  W
. c# H1 {4 F) Y$ wModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。4 B- l( N" M9 E. v2 A/ q( M

# x# _4 \, b( A& x1 }+ Y* E5 l0 r
HTTP Protection(HTTP防御)7 p8 w. D) X0 i8 f
HTTP协议和本地定义使用的detectsviolations策略。: ^: x0 L7 g$ v* E7 t1 U

5 s2 j# V! R0 q* ]
) p4 p& c7 O4 p. g& [/ i/ cReal-time Blacklist Lookups(实时黑名单查询)8 C$ @2 x% K' {' p
利用第三方IP名单。& Q( o! u, j( _' [) M

; B6 W; C8 E: W0 J, M; p
5 z8 |* U4 ~6 D% c. b4 b. QHTTP Denial of Service Protections(HTTP的拒绝服务保护)2 Q4 s) K# N: [$ s; u
防御HTTP的洪水攻击和HTTP Dos攻击。
$ g" q1 x: G$ |( R' M/ i2 B+ M1 I7 ]+ e$ l0 t
' o' W# {2 ~+ D* W; ?4 O, Q
Common Web Attacks Protection(常见的Web攻击防护)
7 R# i# Q- ^6 Z& P& `; {' `1 J检测常见的Web应用程序的安全攻击。% v, M' h% j2 p
' y# _6 f4 b( j/ _0 A& b$ D* u

5 B9 s) r7 }5 f  _# }3 pAutomation Detection(自动化检测)
2 P1 Z! T2 @4 U; z检测机器人,爬虫,扫描仪和其他表面恶意活动。
6 H3 w" x  f4 A4 m/ R0 i
. L7 z$ v9 E+ G" C" T8 x( ]& w8 D& H- `  n5 z- g
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)9 m! I. M5 ]! V7 K& O
检测通过Web应用程序上传的恶意文件。+ `: Z( X$ e. K
* R4 F7 c8 D( @/ q& t$ B
4 ]) C* i* O* V# E& l- A, N" g
Tracking Sensitive Data(跟踪敏感数据)# h$ [5 f6 F9 z% y+ }
信用卡通道的使用,并阻止泄漏。& V% f" r& s  N2 N0 d$ K
1 {! I8 P( Z# q4 u
0 @3 D5 X1 l9 m& d) {0 ?
Trojan Protection(木马防护)6 [) T) y; V) U( z% J5 C6 }
检测访问木马。
5 T& K7 x; ^* ]& ]+ K
/ l' D) L# A. F# {8 t# V$ h# e! p
% P* K6 d4 Z) K( C( _+ s: F$ hIdentification of Application Defects(应用程序缺陷的鉴定)" [, B3 K* }3 X1 ], @1 t4 Q6 |3 n
检测应用程序的错误配置警报。
( S$ C8 C* o1 Y& |. d3 L$ e8 u" c0 U- s0 Y* m7 ~' B. v
( I9 I( ~, Y4 ]1 C2 u
Error Detection and Hiding(错误检测和隐藏)4 O0 ?. K2 t2 |# h6 L3 S5 G. i4 v
检测伪装服务器发送错误消息。
% m; H, T5 M/ Q/ L
1 |; ]" U/ [9 d2 n% g1 r, l- e
1 z" o& ^$ r- R: U安装ModSecurity
0 n7 C% X, }3 K+ h5 @软件基础环境准备
7 D4 ]8 F: J% E3 U下载对应软件包
* O* u& [/ w+ }, Q# C$ cd /root
5 _8 N1 t5 K- g0 A$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
2 g1 g% {# D& p% d. r4 ~$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz1 A+ |+ m" S+ g# ~0 c' x% l4 x3 [
安装Nginx和ModSecurity依赖包
7 y9 m* }0 G' k+ R: WCentos/RHEL
2 F' s/ d4 p: h* _* A- R3 H0 W: P$ d6 @# Y6 w

! r! F: w& G% x2 Q  c$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel/ |1 X) K( j6 {/ ^) p
Ubuntu/Debian4 S; a" z: |7 o5 B8 s5 L* B0 M
& [9 t: S& k* v! x) `# H2 U

- V) ]( L8 G  M$ 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. g3 }; u. e$ M$ @
编译安装ModSecurity
  L% q6 R4 N- `& MNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。9 @- i. o6 h: ]9 @' o
2 n7 w# M) d1 f, R2 p/ }

3 V4 q' Z* g0 G7 v* E0 ?5 S方法一:编译为Nginx静态模块
! H2 Y4 U5 ]  e+ w
, g* U  Q* q5 o# ?2 _" P: r; N5 K  q& G' L8 [  w% K0 V
编译为独立模块(modsecurity-2.9.1)
2 G0 Y8 j7 j) i6 v9 h! h$ q; K; L# E$ tar xzvf modsecurity-2.9.1.tar.gz6 {" ?7 _/ j7 L" ]
$ cd modsecurity-2.9.1/
4 K. f% Z; a) J: G5 P$ ./autogen.sh
, M" K' E. ]6 k/ [, m; Y0 ^9 G$ ./configure --enable-standalone-module --disable-mlogc
% F6 F; P3 w* M; z, L+ k5 Z$ make
  ^3 @" g8 c4 g# S; N* O+ `% d编译安装Nginx并添加ModSecurity模块( ~/ }$ P6 U, I/ h% i# u% W$ F
$ tar xzvf nginx-1.9.2.tar.gz
) g4 h; f! g: t- l$ cd nginx-1.9.2
1 Q. I. `$ ]6 L1 I9 k$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
4 k* D2 K% M* b! i. l+ r5 L$ make && make install( B8 j: A4 {8 |7 I
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
6 t9 V+ {9 O* }! J0 u6 d! V2 n  Z2 E
! D# @1 E4 o% M+ B6 p1 C
编译LibModSecurity(modsecurity-3.0)- k9 V/ e% u. j1 L
$ cd /root& a2 ^2 N. `& {
$ git clone https://github.com/SpiderLabs/ModSecurity7 q7 W! D! ?' b! ^5 {
$ cd ModSecurity# D& Y- L' M6 d% D1 ]- _. J
$ git checkout -b v3/master origin/v3/master' a. I& t$ m, _5 q8 [/ T- h
$ sh build.sh5 o# d- x, r& d% O% O
$ git submodule init
1 r8 T0 U+ d$ _$ G0 O& y& q$ git submodule update
. P: k/ L# @3 \6 h, O2 R0 B! A$ ./configure( Y- p( r% V, W7 q- U$ F
$ make
- U2 L+ O- `2 s9 n8 C; d+ l  {5 g# I$ make install
& ?+ {$ w9 A3 N: r8 S1 w1 R( XLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
, G5 O9 ?' g* R: N9 N! g  f8 m8 N, u3 @& f  T0 g9 |

( J" v2 l7 `2 }1 w1 [* g$ ls /usr/local/modsecurity/lib
& O' d' G& {5 }7 W( e2 }' ulibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.00 m, w  q8 T" ]: p
编译安装Nginx并添加ModSecurity-Nginx Connector模块
$ k) {  a- r( {* s使用ModSecurity-Nginx模块来连接LibModSecurity7 x7 f. }7 o2 Q+ i. G

: J* B2 j* T4 ], [
" V; G; V5 G- ~: c1 U$ cd /root
# I: p8 ?7 E( O5 Q1 G# a7 o, E$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx' w. @4 ]# G! z& P) H
$ tar xzvf nginx-1.9.2.tar.gz8 D/ {1 g% l( Z( V1 ?. d- T
$ cd nginx-1.9.2
; `) n5 u7 f1 ~. V6 L' d% k$ ./configure --add-module=/root/modsecurity-nginx
* n& B; Y# Y  O; e6 ^- F$ make4 H1 K8 u4 ?8 r: w
$ make && make install- p, e* J$ {5 C' m
添加OWASP规则
' ]$ J1 y, [* \9 \8 t: x' g% pModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
3 l/ ^! b, G1 H
9 n3 U' T, `  n5 g9 L& K; Z" Z2 F, H# g: C5 y* }. E
下载OWASP规则并生成配置文件
4 ?  [0 }) }  J4 v: A& P2 [) \$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
/ s- O8 x7 i+ Z  o$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
9 v7 i: w6 p0 A$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
3 n, b: c" A1 h% o$ cp crs-setup.conf.example  crs-setup.conf1 A( Q( p( h  `5 m, B; |! \
配置OWASP规则+ [1 l; K6 f* x7 Q
编辑crs-setup.conf文件& f4 D" x; P6 D
. G) @! c5 K# k& q4 z* @. L

$ z0 A6 C9 U2 e6 X' E4 q$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
  [3 r4 z5 w6 e$ y$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
, b2 M6 X( w6 p* l3 ?$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf. V1 w5 {5 o5 l! e- R' F
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
4 C& T  Z4 z! c0 v默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。8 K0 A7 \' i2 y

* V- y  P# ~5 A: P$ C" c) E6 X( R3 a6 ~7 S! E8 l. M" {
启用ModSecurity模块和CRS规则
8 Q$ q% I7 V* j6 [# f复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。5 l( }3 C' n! K0 U
' B4 x) ^; \+ ]
. A" X% Z$ n. F
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
  n- t$ E5 P2 [  M1 P* V4 Q! q5 k/ n4 E9 ]

/ C2 R3 f% x6 D8 U% c6 I: E$ cd /root/modsecurity-2.9.1/
  g" j6 x& d/ h5 f$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
" i2 m/ L3 Y( r3 z; I: k: ?' X8 `$ cp unicode.mapping  /usr/local/nginx/conf/
9 b: u1 c$ z: l8 |& U8 Y% R7 z0 u3 g将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
/ v! l' m3 V& Z( B  R7 {5 r8 d( g
! m( p$ s3 R" ~/ {' l" B5 P4 l9 L  h3 y: w4 C: `
$ vim /usr/local/nginx/conf/modsecurity.conf2 D0 _9 b. R' Z* j2 U1 o
SecRuleEngine On
( u; X! t0 Q/ |5 PModSecurity中几个常用配置说明:
1 d5 g, j* C& R: G# C5 ]8 {
" U- _2 A* }: e9 F
5 \  h& ?# \2 y1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。# A  Q% v# W& C. e8 ~2 }7 Q

7 H$ \% V5 M4 ?' n8 e8 I6 p* w- n" P/ L0 y; J4 z& g+ e
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。; @5 R1 s3 Q6 c
* ]* Q% }2 b- x& h; a' W

! _5 ~  _) e8 Q; O4 U; W3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。0 Q4 q8 W5 n9 C3 g/ s! p

+ Z- a$ b% s! E+ r. {' |5 r- \6 }( U, m  c& c' ]2 e4 d
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。0 O; S: a1 F- _9 t( J

- K% H; q1 \+ V5 y4 s+ S8 E1 ]# C) a* b& n
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。! K4 s0 R) `9 L

$ z( W4 P* q& A% q: ]" g9 a! N. f4 K1 n9 U& m0 ]! K
3.x版本CRS2 |- P' s3 H5 Z7 N: M! C
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs2 B; O( x# H% A& u
# 生成例外排除请求的配置文件, _. D6 ?% R! j5 v( e4 A
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
7 d+ T8 n* i% V/ d; W6 {$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf& p. C0 R* L: ?4 x2 X3 i1 w" p  F
$ cp rules/*.data /usr/local/nginx/conf
8 }7 T* G: J4 {" @* i+ j0 K# t为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
7 C# W% t- f1 \- y, S: s( Q/ ]9 D  V5 D2 X' _
$ X* C. O+ N) n! Q' @- a
$ vim /usr/local/nginx/conf/modsec_includes.conf: n( l" s6 y! i2 d/ j! c0 s+ n5 ?

6 d/ C4 k  L( y- r  R$ M+ ]
[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

, k( E8 ~  `8 a& g
# R' d9 i' F7 d8 O1 c* n0 ^注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
# C  k* G+ b0 K/ \
; l2 M0 F) g+ ^& x* \) o# h) h& ?  l. D* u* a
配置Nginx支持Modsecurity) S0 t; ]8 C6 E1 o! Y
启用Modsecurity
2 l8 k: }& Y- u- s, S# D+ Z使用静态模块加载的配置方法: D7 X3 V; }2 X$ ^( @
在需要启用Modsecurity的主机的location下面加入下面两行即可:
+ Q: G  M- R. `7 M' p  N
: b5 [0 ^/ H. @, e; W: g5 F: U) [- j, Q9 @1 j! o- e  j
ModSecurityEnabled on;) N" H9 ~( z* N- R
ModSecurityConfig modsec_includes.conf;( h% F  ?( m$ J8 l3 r1 @5 k8 ]
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
7 {: B  R- y+ o  E8 ?- V2 ?) T' c- I7 P( E8 E

4 Y0 u0 `5 j& d- i- F  J& n$ vim /usr/local/nginx/conf/nginx.conf8 P" p; k% C* ~4 X( Y6 y
" N4 P* ?1 s  S" Y3 t

) p# T7 S1 J, G" F$ Cserver {
. V1 X9 ]! |% ?6 x' [  listen       80;
2 V. @% }1 e. U; M  server_name  example.com;0 z" D- [) M: n: m% w7 Y
$ u3 z2 I7 z& w1 h1 w  K9 C. E
' F- ^, D: k7 Q4 a* j* @" e0 r2 i
  location / {( a8 s! ^1 _# C2 `
    ModSecurityEnabled on;
) C( m+ V+ Y8 Y( o    ModSecurityConfig modsec_includes.conf;5 ?3 Q2 Z& `* Q/ ~2 [' @* |# @
    root   html;7 [" M' Q8 L& f2 D8 S+ s
    index  index.html index.htm;
# U7 o% P# m8 l6 H2 l8 c3 `  }
3 U# `6 j/ [/ B( h  I) g. X}
7 U3 `" D! T/ u+ ^) }使用动态模块加载的配置方法; w1 W, c2 ^/ a3 y" \
在需要启用Modsecurity的主机的location下面加入下面两行即可:2 C1 V! \& l' O4 H! H

6 J" F$ P& r2 X. i
& }3 Y! n0 K2 W5 d+ l4 g1 jmodsecurity on;
/ K7 {$ P9 V3 C5 b4 {modsecurity_rules_file modsec_includes.conf;+ l' P9 z) T2 h7 _; H
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。5 V5 w. M: {) ?/ |1 C2 `8 Z* j6 r
9 W7 n! C6 u2 z% g
) R& W9 s# M9 e
$ vim /usr/local/nginx/conf/nginx.conf
/ G: S1 `1 y) h. L8 \
4 X5 k4 n6 b5 j4 H. P* n5 ]" T. M$ w4 s2 \/ t! }! }' h* i
server {
. p! w6 r2 o' k- O" x6 V  listen  80;
7 K  I6 D" _0 N  server_name localhost mike.hi-linux.com;( `0 k1 F  \9 M& e
  access_log /var/log/nginx/yourdomain.log;
' ~. c) U) v% o' E7 X
+ Z4 T6 M9 M7 w, O3 Z" O4 _1 P; m/ V8 Z! Q6 E
  location / {
5 V+ B) j5 e7 l+ s
+ |7 B  u4 F7 |7 y+ ^; H( g- \" W* w
# `& x) C: {  U/ d/ Q$ R  modsecurity on;
. k* p0 H7 E3 J7 B  i3 {/ v% j# E) D  modsecurity_rules_file modsec_includes.conf;/ I. j, z4 c. S! t$ u
  root   html;" g# d; \6 d6 K$ [( ^
  index  index.html index.htm;
) z. x; w# C9 J- H# ]  E1 B}
  Z  V$ r4 d: @+ h- `; w4 \  w}
, K3 x4 P- J; G" H验证Nginx配置文件  N( w1 r5 f  z6 J" i  ]
$ /usr/local/nginx/sbin/nginx -t. a1 I- W8 w' m/ B! ~+ n& j( t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
) D: ?  m/ z, _" F1 @( inginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful& {* S! i0 ~+ c& E/ G! A' D
启动Nginx
, l- t( E7 B# Y$ K: P) t: ^$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf' F9 Q- w: E0 {8 d$ }$ g, I! m1 Y, s
8 V2 h" [5 O: n' l  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能够战胜更多复杂/未知/混淆的攻击模式。


  ~: G& w$ u! ]: ^

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-10-12 21:19 , Processed in 0.110750 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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