找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9181|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
0 k# a  f% \0 I) I1 f* t* W: q7 V  m, Y9 h. @- f# o5 `

4 s8 M& D1 v* E& O在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
$ b- U1 w/ p  K/ ]4 O: j# v
: |" a& s( _, N6 l- i6 s  m% x7 d2 Y. I3 d7 e
什么是ModSecurity
$ r) g9 T' j' d' G( lModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
2 y$ a" w! D0 }2 P# p0 A8 ^/ ?- D" Q

# v: }* l: }) U* k' `$ m% cModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
$ T2 Y7 ~% i8 U) {$ O) e; J1 k8 _0 z- ^* \8 I) ?3 p. @7 U, m

4 e0 L9 P8 I7 S# k  q/ `( sModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。; P4 l$ m4 |+ z( I
7 Z$ n( J  T( c* f# g7 s9 I9 r
# n6 L' f' f& E( e
官网: https://www.modsecurity.org/
, y* l; J3 [6 ]! Z. D7 b5 ^3 J0 m5 o! I/ m
! d3 n5 p2 l5 Q. y/ t4 f5 P0 g
什么是OWASP CRS, n2 H, |# c- p. t( S
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
4 w; s* X% I( G# {) Z' g4 _7 ~" a- S5 F

& j, t8 }& c0 [0 J6 D0 z/ FModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。# i5 n; i4 k, @0 K% w/ H+ m
1 s/ B$ }6 r% f1 m: o/ l

: L) F4 U0 N9 H# \8 v4 Y9 h! rHTTP Protection(HTTP防御)* }# c8 O6 B0 @# c) f, i
HTTP协议和本地定义使用的detectsviolations策略。
; _) ^1 @# M& a% X% @
" p4 k7 L+ i: |/ j
& V/ }+ q! b5 [  p$ |Real-time Blacklist Lookups(实时黑名单查询)# @' z  |& _6 O( n% ^' i0 x9 N
利用第三方IP名单。3 W! E4 n( i# i8 @2 r% Q0 H

9 G1 u9 |' [6 |% R: D
0 Z) l7 U6 w2 Y3 J& X! dHTTP Denial of Service Protections(HTTP的拒绝服务保护)0 S3 R- p; t# p/ i5 F
防御HTTP的洪水攻击和HTTP Dos攻击。
1 n* M, c  m, B1 f( \. A0 g* S9 U. [& g$ O! U+ T0 j% ]

7 t* e3 R: S$ m% fCommon Web Attacks Protection(常见的Web攻击防护)8 P6 k' R& i) U7 ^7 x; k
检测常见的Web应用程序的安全攻击。- N( ~( U; m" a5 w. e" {4 c
% v4 K5 Q- V4 {; C: o
, d4 B5 J- a# I+ V2 F
Automation Detection(自动化检测)8 h- t0 B# D' {" \
检测机器人,爬虫,扫描仪和其他表面恶意活动。! V5 d- m! F/ A% t, ?) G! j
: _' z/ w3 ], X- \5 u6 o# E* v
: }9 C7 [" j( E0 e
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)0 i; @! ]6 x- O* n
检测通过Web应用程序上传的恶意文件。. I( ?+ W' P( B! Y# v; ]7 Z

! j" Q1 a* c' ^  K+ a4 N! E7 K5 ~7 d8 X
Tracking Sensitive Data(跟踪敏感数据)" {& E9 x7 _, A' Q$ E- w. c7 `
信用卡通道的使用,并阻止泄漏。
1 l/ F& X: j9 e8 V, l* h3 S! w0 e
+ j( b- d- }. v5 x
6 |& R( {3 I6 OTrojan Protection(木马防护)1 w$ Z* J' F( q7 Z5 c+ d
检测访问木马。) u/ @7 @, a; z5 |! K3 \7 N
' C0 t4 l( s! n
- y: v( [+ t9 }5 z: @
Identification of Application Defects(应用程序缺陷的鉴定)6 ~6 F* j& ?) V- b7 }/ @
检测应用程序的错误配置警报。; }- X6 n- h$ o, P8 P. I
& m6 R0 @8 t4 _0 p  t+ Y
- y/ M. W/ Y& r& i8 ]- B
Error Detection and Hiding(错误检测和隐藏)( y" q% j7 |8 ]4 U
检测伪装服务器发送错误消息。3 J$ K" [6 `: R! N1 A$ u. n% q
% n4 H3 R1 ~  D* V; F: c- n1 }
& p; i, O, ^2 w$ f
安装ModSecurity
, d4 Q  y& F+ P  A软件基础环境准备* A6 @) o3 e8 N) f0 ?0 ]
下载对应软件包4 |1 }7 P& {. u3 X' w3 C
$ cd /root
& E' e  @. |: p) G; _6 k( Z5 v% n% y$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
, Y) k4 x7 M% ^- U$ 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 W: B9 m4 P# N& m安装Nginx和ModSecurity依赖包8 D% J8 Z- i4 A3 c1 \
Centos/RHEL" I' k, K7 ?* ~) {

/ q+ @: F# l0 H: o
+ Y/ G9 A( Y0 N. g# G$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel' P( L' V1 {& s% y1 b8 Z: H
Ubuntu/Debian
  t/ a/ I" G. r; {# E' h$ g0 r5 E& H, Q
2 G7 V: l  B4 a2 K" i* o
$ 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/ I9 R; {8 L/ W. h5 ^; l
编译安装ModSecurity
9 c2 }  c( Z' r% P8 M% tNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
! R. X  c+ }, q
: X( |: A* s3 g" ^  b/ W. Z. N. C. K
方法一:编译为Nginx静态模块
% A* G, Y# k( ^1 r
3 e& l! j/ ~3 s$ @4 i0 p$ K! A; A& c, W1 W
编译为独立模块(modsecurity-2.9.1)
- l  w) ^4 K& q  l9 r2 F% |# a8 \$ tar xzvf modsecurity-2.9.1.tar.gz
5 v$ T# l& L: y2 Y1 i- G$ cd modsecurity-2.9.1/
" d  t5 J: g) v4 z$ ./autogen.sh
% ~+ j- n) M9 S$ ./configure --enable-standalone-module --disable-mlogc2 \: w. D" _7 A% R
$ make
6 q6 a% P, Z. e& ]) H" C' k编译安装Nginx并添加ModSecurity模块' @& `9 Z$ ]) G/ x  D- B
$ tar xzvf nginx-1.9.2.tar.gz
3 V' U+ j6 ~. p9 I% R( E$ cd nginx-1.9.2: v9 n7 ~* A+ i, B5 V/ @/ F
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/7 W1 C6 y- G; k7 x6 Q1 T6 z' t* L
$ make && make install: G6 \6 a3 T# C5 Y& p" Q
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块* o0 w+ O7 Q- P9 Z% O) Z3 W( y

0 `! I5 J& K7 X8 {8 i# W8 a, \
: G7 P7 _* a* ^% t7 s编译LibModSecurity(modsecurity-3.0)
6 b: u# T( b) O+ {% u( E$ cd /root
+ C: z0 H$ S5 ~3 ^/ h, ^$ git clone https://github.com/SpiderLabs/ModSecurity* ^" l, ~/ B4 P
$ cd ModSecurity9 E/ v9 z" R# v& \3 T
$ git checkout -b v3/master origin/v3/master, c4 R1 ^4 c, \  `
$ sh build.sh
7 m) v, C/ _) D# V5 q! o$ git submodule init% I4 F- S1 u, F
$ git submodule update
& C$ Z' B$ q1 [$ ./configure( H  \- H1 l7 a' h
$ make
. K  [, ]5 {& _2 S! M$ make install$ k" E* U1 T; {0 p: V* K3 }
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
3 E. l# l  x7 x- T0 C4 l& Z3 O
0 O, _9 t9 g3 ~9 \' {, S/ e- A6 \+ p( ~1 i5 E7 e
$ ls /usr/local/modsecurity/lib; [9 b5 F& r$ b; y, S
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0% m% J& k% k/ H$ y; ]1 k
编译安装Nginx并添加ModSecurity-Nginx Connector模块
# o9 w1 L& r3 m/ |& G使用ModSecurity-Nginx模块来连接LibModSecurity
: n3 _. W7 c) G( Z# E1 F
. B: ?2 P1 g. m2 P' L6 S) {( n3 I' ?: @2 c" z
$ cd /root% e- L5 c/ z; i' \+ @
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
4 X) _1 h& L9 G$ x7 g2 A- d& a$ tar xzvf nginx-1.9.2.tar.gz
8 P' O- r2 {+ w# t$ cd nginx-1.9.24 [) X( z* P. y5 n
$ ./configure --add-module=/root/modsecurity-nginx. p( ]) C0 l9 v& L8 p
$ make
# O4 q  m$ c: Z) @6 w5 V$ [* ?/ c$ make && make install
2 }9 T, }3 T$ J# a6 O& i0 k4 R$ f! K添加OWASP规则8 Z- p" v! |. I. G" O- N! y: D: a
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。  l1 p) b4 q. I: P4 ?( u1 T8 X
7 P+ ^* J! l0 H  T$ c+ G

0 i3 {* L  e9 A: N下载OWASP规则并生成配置文件
8 \2 D0 [' v4 n+ Q4 _( k$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
) M* r! z0 L$ I7 d; Z& l2 O' e/ ]$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/4 u( [6 }0 [- a' M$ y2 }
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs+ v9 N6 C1 X4 t1 L1 A* h8 N7 e0 |
$ cp crs-setup.conf.example  crs-setup.conf
/ C/ |( c3 o$ ?; @8 T$ l配置OWASP规则/ o: t( D' i  G1 k6 ^
编辑crs-setup.conf文件7 W( q6 ^8 O) n2 L

: c& z7 J5 y3 H! L5 V0 _: T' {( K1 l
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf! z. T2 u6 K) q
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf# U- A& d6 E5 M( }' L/ ^, W$ g
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
% s8 q8 n4 s: M; t! N& D$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf" c- R, D6 k  L4 ]
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
6 n, F1 J0 E  Z; J& |" @/ Y& j: Z# m" H8 N
6 {6 R9 W7 ?" V1 {- e
启用ModSecurity模块和CRS规则
( o5 w6 y) L8 V复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
4 z6 D7 F7 F7 [% i% @+ e4 O3 {/ k0 Z0 Y3 P

6 _% s" u; k; I+ x- Fmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
' E6 R# y' e  [5 P
# i5 _: t2 }, r' W6 T( b  M, H& b9 J& h- L* p, h7 P# D" l
$ cd /root/modsecurity-2.9.1/3 |( P- W8 G  g+ P! b; B
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
% x) V, R: e/ ^+ r$ cp unicode.mapping  /usr/local/nginx/conf/& F0 x& J/ I/ W8 c' ^+ |
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。; R9 W5 b" X. d: s; |4 m
) J  c4 r" i8 h& Y1 u; B

4 g4 O- X: m; b$ vim /usr/local/nginx/conf/modsecurity.conf8 l+ Y# K0 N1 K  _" r! O( w
SecRuleEngine On2 C$ _2 F; l' x# A# ?. S
ModSecurity中几个常用配置说明:& L# A+ @: @' Y8 V5 t; `
+ X1 s3 K3 A" E2 w, A
! u: p# T# G: a7 H0 ]; ]# O! a0 f
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
+ ~3 s" J8 s6 i3 b( q
6 |& p8 L+ S8 r' ]
" T$ H/ {0 z' O" f2 \2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
3 ^  E" A+ D( q( k8 w6 S! Z$ g" t; t! ~% W: y
0 m/ Q$ O8 j$ D1 q3 y  Y# u
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。$ ^# |' l/ k2 `. x, g

6 {8 s' K: g1 w; X9 l; O" ^" {. ?
; d( a9 X& w6 ^4 S6 Q: i+ N4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
; L  P5 m; z3 [
! ?+ u  r$ D: C' m6 T4 s
  r4 h' A( q8 n  T7 C5 I9 B& k在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
7 x* E. b' {3 q/ B7 B: v$ w2 c. m3 y9 Z, M, |( x3 r

2 K1 w; y* c9 \5 O* p0 T* K3.x版本CRS2 F9 ~  f) G1 g
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs! ]. [, S8 J* a/ l" [3 y
# 生成例外排除请求的配置文件
& W1 V- J8 l3 y5 X; `$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf1 B" u/ Y9 Y2 c: a
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
/ Q: g: ~$ X  h: m( a! Q$ cp rules/*.data /usr/local/nginx/conf
, K. g- p+ H( W: M& @* I6 J为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。. T6 ~6 c3 Q& n2 v
4 V( _+ Z( Q4 r. F
. X: |1 V% Y+ T& t8 N4 R
$ vim /usr/local/nginx/conf/modsec_includes.conf6 M1 {  g9 ~3 d6 \) s- {5 G
! Y3 H. c, z" G, I: l, a
[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
' X" `, H# S$ i' r$ K1 Q2 L
7 o! `# G! C* l& U7 O0 |$ y! h
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。, H8 P4 r6 E$ ~* x3 ?, ]& D
3 g+ ]5 m% M* J: s7 ^; A& L
0 [1 K; d9 I& G
配置Nginx支持Modsecurity
5 p+ i; C4 y# ]1 k启用Modsecurity
; y5 @+ i, B- P  J( p7 d使用静态模块加载的配置方法3 F  L) h" S, [5 a! C. @4 G
在需要启用Modsecurity的主机的location下面加入下面两行即可:
7 e0 w; \- ~4 `8 ]* r2 Q3 F
! j& r6 f+ Q" {9 f! k; ]6 M# W5 b% l0 j' O2 D8 k0 A
ModSecurityEnabled on;
8 i: [% F+ L% N5 w& DModSecurityConfig modsec_includes.conf;
1 b3 u) }2 w: s8 M! d! S修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
& K: m+ k& Q( O6 v0 W( R! L0 @- k- w+ ~  n7 C6 a7 Z

3 V! r4 b% j6 V1 z3 z% w/ @' L$ vim /usr/local/nginx/conf/nginx.conf
* Z. \: U! I9 a8 E
5 k" h9 W7 r, \) J% l) p
, f" W  @- X6 a: R+ y- ~) w& qserver {% o' A0 u' B! a1 l3 t( F6 c4 B
  listen       80;
) K4 p# u; f( P8 E  server_name  example.com;" C- P8 W  }4 p

* x- U, a  B2 L: U- n$ i8 N- f( {  F% n/ t) [. B, R$ N3 j& E* j
  location / {; k7 d5 W2 L; V! R$ o" h* I
    ModSecurityEnabled on;" Z% R) Q5 |; s( d3 z
    ModSecurityConfig modsec_includes.conf;: E2 t' k2 `$ R! z" g) t
    root   html;1 t- ?! h6 D: K; }
    index  index.html index.htm;
* m* W5 x: m; Q% U1 w  }
+ ]0 X$ N4 \7 a  W}
. S: E, Q/ ]6 q. X  Y2 f7 O使用动态模块加载的配置方法
; h$ q& Y  [! p在需要启用Modsecurity的主机的location下面加入下面两行即可:
* ?3 E, M; h& a5 Y2 O( P  Y( W
; Y4 k& x5 U3 f
7 r; y" n8 _6 z0 Z! Lmodsecurity on;
/ r0 E; g2 X* v3 R: v# m+ Q0 emodsecurity_rules_file modsec_includes.conf;+ A- l6 K! f1 Z3 l
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。) Z! B' k- @1 L: I# j

2 C& [9 i. U( }- |+ V7 g, i' W
$ vim /usr/local/nginx/conf/nginx.conf
- t  D" ~8 r9 a/ k% T- p  G6 Z* T# L1 w) b8 h2 C, q7 s4 U/ J3 I* M
: W6 @8 {) B0 m7 l: R
server {
. y# @) Z% K8 t3 ?! Q* [5 Y  listen  80;
# M( `+ O1 a+ Z; Y  server_name localhost mike.hi-linux.com;7 L% \1 p+ ^( |6 J! ^/ e% u' R6 h
  access_log /var/log/nginx/yourdomain.log;
5 K; M8 @% O: p* L% w& H
  j$ W# o9 J2 \2 w% Q0 r9 K
" ?- j7 C2 X- I# L% V0 D! ?  location / {0 g) o! n4 K/ A
& ]9 P* K- S: p# v# \# c
/ G# {: b0 w& k" s2 u- s: i
  modsecurity on;
# H/ ^6 e, U* K1 m4 M  modsecurity_rules_file modsec_includes.conf;8 R* Q5 @; U& e+ m. M; [4 s# P% n8 d
  root   html;
; {  u% P2 n$ h' E1 E2 W  index  index.html index.htm;! J4 M+ ~; w7 z% H% p
}/ L3 ?! _; W  g' s5 e! `
}
1 i( I' G' ^2 t/ Q验证Nginx配置文件
8 L0 u/ c) S2 _5 {+ [) G$ /usr/local/nginx/sbin/nginx -t
2 ]0 P8 T1 J6 A1 z2 U" Knginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok6 @8 q) N& X0 X  D8 [, C) ]0 C9 m
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
9 e5 i5 S$ A! E( ]$ n( V启动Nginx
. m' S8 q7 m: b* t2 e3 e$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf7 C' [9 L3 Q- m  Q0 k- N  ~0 |9 C

1 g9 h$ M# P7 \" j9 k) z, 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能够战胜更多复杂/未知/混淆的攻击模式。

% P. I4 s5 j1 U" u+ N6 B% M* R

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2025-9-19 09:46 , Processed in 0.071535 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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