找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12268|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。( x2 r. w% u7 o! b

! @4 {; y- J4 r0 X& a1 ^
- S3 n6 b1 g# Y  {在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
/ a7 i/ j  A3 e4 [. z
7 k9 m- G' n" r6 l
! y" A* ~9 i0 }" B( O( O什么是ModSecurity
: p6 X. ~6 t$ t9 T% I6 {ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。8 g. O0 ]$ \. [
, n7 y3 c$ m3 T' O6 B3 Z
/ Q1 [* s4 G2 e1 E' n4 f4 G) v
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
& G7 f! r/ |$ `5 Z" ?7 m
+ t; d# q' t6 v8 O4 ~: z' b/ G  l1 `) ]9 K8 D
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。$ h5 M( s5 G+ d/ z* ^" u+ ]6 p# E1 u

# q, Q* z$ n5 @4 X
  x8 y( v7 l& g" W+ y( a官网: https://www.modsecurity.org/6 t" T7 o: E/ k/ s% [. `- O) l7 K, b

2 I7 u- L7 D, H9 y- ~( v+ \( n: ^* Q- ^  ^5 f% E+ \4 \
什么是OWASP CRS
# X- D7 m* N' U; J+ tOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。' E# A& [* m5 s! d

) L5 ?  S& b! L; [. g  a8 o) k/ o, H
" s# D, J0 b: c7 Y% s- `% V( w8 zModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
1 g, h/ d) E) s$ R; _: ~2 y2 i/ p
4 y; ?- M2 D$ i( D" B0 ?3 y+ u* u+ M2 e5 Z' d4 M; p7 U% P1 c" s7 b
HTTP Protection(HTTP防御)( N+ J1 E0 j, o% v: _% K
HTTP协议和本地定义使用的detectsviolations策略。
1 O% c5 @5 J% |4 v8 r8 {+ v5 G* {# Y

# }) H* }; a+ f9 R: X0 Z" b& c& H) CReal-time Blacklist Lookups(实时黑名单查询)
* u$ e4 ?& i# D/ Y6 t利用第三方IP名单。
3 g: Y" I0 v$ z9 n! A" T% N7 z, y9 P

7 u+ [+ x+ b3 m" iHTTP Denial of Service Protections(HTTP的拒绝服务保护)& a5 C0 d& ~. n
防御HTTP的洪水攻击和HTTP Dos攻击。7 W3 _' Q- A) T7 o3 ]% K" S
* l% a; j4 K& @: l$ n/ X: C
9 [) R  m3 n6 {
Common Web Attacks Protection(常见的Web攻击防护)
7 c: E0 r; A9 ]" E4 l( d; g6 q0 o检测常见的Web应用程序的安全攻击。" J5 @/ y; e3 u, P2 f4 Q
0 O* l" x9 I4 u! c0 ^- A
2 ?$ N. ]/ B+ `
Automation Detection(自动化检测)' f4 D" t3 f7 _3 _" ^+ }+ e
检测机器人,爬虫,扫描仪和其他表面恶意活动。
6 c6 {' I4 ^9 b/ ~3 \4 V
. f, z  M! z5 E# q+ k! r8 l0 a$ ^9 s( Q" h
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)& `- L) k1 e6 E( j+ F: s
检测通过Web应用程序上传的恶意文件。
5 G$ P6 X7 f# Z" w  d$ Z+ X
7 H9 |: o& m+ g! J( `" z) S2 f  I! {5 p' p
Tracking Sensitive Data(跟踪敏感数据)
* p. s. Q4 E9 T2 q/ y% c信用卡通道的使用,并阻止泄漏。* }8 j7 H, \# U( u& ]9 A% N3 }9 @

6 k+ J9 S  ?7 [6 V- R' J+ n5 V: K4 c$ C. B* s) P4 D* L6 a! B
Trojan Protection(木马防护); R9 c* G) a# u5 o+ V
检测访问木马。
) p: g4 {# [' h- h: f! y6 L3 F/ j% H; H, y. B- V' @/ D* W
3 Y" @  P" H2 p& o5 |7 p
Identification of Application Defects(应用程序缺陷的鉴定), |  e$ t# z) j+ p$ m
检测应用程序的错误配置警报。
; Q5 }$ ~& N0 f) i/ }1 c2 {5 ?9 M. Q  _6 l' T

$ w# a% Y* x6 Z/ T6 a& X( `, b$ X) qError Detection and Hiding(错误检测和隐藏)
( J: c# z4 G+ ]0 _2 o. \, Q检测伪装服务器发送错误消息。
# b5 M8 e  x( L$ J4 |
2 Z8 _8 e% S7 R. N
. X; @; {6 a0 J' t- o, f' t* ]% |# u安装ModSecurity6 Y" j& ]4 r) Y! j
软件基础环境准备% u4 I5 z' ^( w; y
下载对应软件包
; ?! \. j8 {" m$ C( S6 W$ cd /root: h4 _8 f2 M! L
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'! K6 X/ a2 v) ]" y' ?) I6 f8 G
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz' Q0 b0 ^; E" ?+ f; o4 P7 J
安装Nginx和ModSecurity依赖包
# z9 ]& f! R  G2 hCentos/RHEL
$ d: t: K' m& ?* E. ^+ n5 [* z2 @/ o1 n
8 c" r% x2 z% a- ?- w
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
9 H6 }! b" S  p' \3 e0 cUbuntu/Debian
/ c5 D1 A9 ?' x' s5 `- P8 w% J. X) S/ a7 z  A- A

- J0 P: Z0 \5 `1 I9 D, 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
" E$ j! B) W* R) K编译安装ModSecurity9 i  m  q7 ^7 i# J
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。# j; k8 N$ h0 C3 l
4 s+ K; [1 m, r4 l' Y* ^

2 P" ^; w9 x3 f8 x# j+ M( n方法一:编译为Nginx静态模块8 e# W, s, X8 {. w' d1 _
. c& Q9 I. ?; L" l6 G" J6 {# t0 C

" ?' r4 @1 r2 S% W* D' B( P" `3 f编译为独立模块(modsecurity-2.9.1)
. b# J/ P. H' e+ _$ tar xzvf modsecurity-2.9.1.tar.gz* B9 @. J& G$ n3 [7 ^6 E5 d5 a- `
$ cd modsecurity-2.9.1/
1 C1 b9 R, _  k$ ./autogen.sh
8 e7 \. J$ R. O; H1 ?) I$ ./configure --enable-standalone-module --disable-mlogc
- \3 n: ?8 @/ ~4 ]0 b0 J$ make
3 e+ `5 o5 |* e7 g编译安装Nginx并添加ModSecurity模块
$ k# h; N' ^2 Z/ ~2 T5 J$ tar xzvf nginx-1.9.2.tar.gz6 \5 a% A1 s2 A9 c" q4 S( |
$ cd nginx-1.9.2
0 r9 N% I/ G0 s- w; N- L4 Y3 @9 A5 U1 u$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/* Q. W, ~% y8 c
$ make && make install0 w& [3 ]# n# N9 Y+ u5 v
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
- m  R1 K0 y, z" P5 }% J9 p* [' n$ n5 g: l- ^

8 z; |+ T5 @7 H2 E0 K1 |编译LibModSecurity(modsecurity-3.0)& U6 \" L/ e, G' T5 q0 }
$ cd /root
8 Q/ M0 E$ t' E- ~% }$ git clone https://github.com/SpiderLabs/ModSecurity& L) a+ M& T, U
$ cd ModSecurity
& p; y; u' g/ ]8 C( g4 d) N$ git checkout -b v3/master origin/v3/master
) u. K+ |5 S3 i$ sh build.sh& Z1 [5 j1 O7 H* j
$ git submodule init8 p5 |4 F% E" y" C4 j: m6 w) e
$ git submodule update
' c- F/ v# |: u5 X$ ./configure+ I. r% e/ u* |
$ make% K0 N: k6 |3 t( j, l& n
$ make install$ |+ E) w( H2 ]1 @) u5 [
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。: w, d( u; S0 `3 q' ]5 Y2 {
1 H7 r/ Q* n( v" s( {: C+ e

8 L# Q+ m: f) [! c' T3 Z% a; z/ S7 l$ ls /usr/local/modsecurity/lib
9 X1 m% m: h1 t4 ^libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.05 t5 u' E# y; ?& X% Q$ I
编译安装Nginx并添加ModSecurity-Nginx Connector模块
6 r9 N9 Z% h/ [  A; m使用ModSecurity-Nginx模块来连接LibModSecurity9 O. b- o: D5 B; b  r5 u
- K4 W( t2 L. y# s7 n0 ^
; `# q& e6 i5 s% S0 b+ G
$ cd /root4 e- W& K5 y  [
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
5 X9 i, S9 }  e$ tar xzvf nginx-1.9.2.tar.gz5 D2 ]% B; C( @  b$ L3 p3 T
$ cd nginx-1.9.28 m3 C* ?9 z1 s! P. m$ R6 g
$ ./configure --add-module=/root/modsecurity-nginx
2 B) R6 Z/ F5 y' |- o$ make
  H/ _! L. K: c' p7 R# }. k$ make && make install
( Y' Y6 U8 w. Y. f6 ]添加OWASP规则
$ Q0 d9 D( }/ ]ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。2 R0 q7 `# {. p2 b+ {
6 w. k: }% E& u7 @7 z
& c& s" _6 g# Q- c6 D
下载OWASP规则并生成配置文件
6 z6 }6 _: F* M9 z' {$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
! \1 f3 \; _) }. a% a, I9 ?5 ~$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/! M. z; _# ?+ s9 D7 n2 E
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
/ p  b% q7 M7 J- {$ cp crs-setup.conf.example  crs-setup.conf% j/ a, X) x4 V3 T, V& v0 w7 O7 f
配置OWASP规则
& b' E" P! ^8 Z9 {6 J2 P  W编辑crs-setup.conf文件
. l8 W4 ?; L: }2 M0 |3 z; _, M
! H3 s* h# S9 y% D# v# n5 ?/ c0 }! W4 U5 c( r! ^* B9 l( F5 ^
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf/ ]7 ^2 X0 b" ]: N3 }
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
9 ?3 f0 c) [3 O( P  N( Z$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
' }) J+ B& i1 |% ?0 x+ A$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
, r( D, P- E9 y$ u  |  Y) c/ {默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
# v4 a, q8 I: h# D: Z. i  A( b9 D% y2 `
  _7 m# K. S7 [9 ]  g! n: D* o3 r
启用ModSecurity模块和CRS规则
, P4 v+ l! _: o( k复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
& }8 b+ \0 P8 G4 n: s5 C3 {6 `) E* [% g% A7 T; |
- Q, s3 ^* \& [9 P1 r8 I
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。% b: @2 f7 d% N3 l& X

& W7 Q$ H- }9 q* v: s; S
$ n2 Z( ^& `6 v$ cd /root/modsecurity-2.9.1/
9 `, ]2 g* S) R" Y; y% L$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  : Y' B+ Z1 C, }5 H
$ cp unicode.mapping  /usr/local/nginx/conf/
. t+ ^( l% d' R将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。" N; s+ @8 ]( S! G5 ~1 D$ i4 i5 i
9 A  ?0 \! b5 X, I; ^* G
' V" k& Y7 s( i# r5 H
$ vim /usr/local/nginx/conf/modsecurity.conf+ _1 v7 h6 |6 e) m
SecRuleEngine On
& D- }* ?7 `3 ]' a* `ModSecurity中几个常用配置说明:0 p) w' s- F5 b
) u! i0 |. S" L: V+ y) G, i
- a- R& H2 i( v+ d* _
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
% d$ r% k0 N8 J; h" P
* C' ^2 O8 u; u7 Z+ {. w' F4 G8 E! {7 C/ U8 y2 S! N8 z) l
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。. r- G& S: Q: S- L9 _/ q2 {
; Z. V. Y7 e. A
; n1 J0 B/ J; S3 A
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
. |5 n- ]3 q2 F- \' U* C: L
1 ^6 `3 z/ @% Q5 p0 P3 R& B4 O1 ]) O* |5 [
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
* B5 i, d! F8 r- u: l
+ A' c# o' l1 P$ G) W, {6 k1 M
, b! p* F5 \9 y) q9 k" O) g' k: ]在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
, }- {, u& Z1 ?5 ^, S8 P- f
9 @, u  z8 x& v% |9 r, H1 s0 K+ |2 ^$ i0 g. u! ~4 ~
3.x版本CRS9 D2 k" V' X, D. F) B" I
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs) @( T8 F* N8 _% N: o/ L
# 生成例外排除请求的配置文件, w2 u1 Z2 G+ e! o
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
6 j( D+ B; Y9 Y1 O8 n7 F6 U* O1 H+ P- V$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
/ M* S' ]* A* r* S0 c2 ]$ cp rules/*.data /usr/local/nginx/conf) p" \) `% K* g6 C0 J- W3 K
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。1 K' Q. r$ h: d% _" ]! m
2 v& F$ g6 w0 d' p. k4 M

; {8 }6 o5 y1 q3 n$ vim /usr/local/nginx/conf/modsec_includes.conf+ H  ]- }, T: E2 y
& R% b! G, L9 K
[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

/ U! s' o$ M, i/ o  F5 y) ]6 Q8 A4 q
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。. p. ?( X0 z+ L7 i

2 K" \1 d) R) m: ]0 w# }3 {
' G1 b/ L: H* r2 b+ i$ ]) v% x配置Nginx支持Modsecurity0 A5 Q1 a5 v' S  Y3 f1 w
启用Modsecurity
6 m% o- J1 a0 k" T5 E# P使用静态模块加载的配置方法
8 e7 e  u9 H" O7 B在需要启用Modsecurity的主机的location下面加入下面两行即可:
# E0 r7 ?- B  P7 b# @8 I4 i# X
! B' t  T  M! c% f6 `+ ]# G  T" W% \2 }, U- W0 S5 R* W
ModSecurityEnabled on;
$ m8 Q) E8 P+ G0 \& J, d- fModSecurityConfig modsec_includes.conf;
! |" m% ?2 w3 {# T6 j  J修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。4 C7 g8 ]6 O. I7 r8 q' x
/ x& D7 p( r% c5 I/ p
7 ~5 N8 q2 Q; l) B6 I2 s
$ vim /usr/local/nginx/conf/nginx.conf0 Q/ t3 |* J% ?% x7 B) j' e/ z
7 \* l% h7 z* E0 \$ O7 l

# |. p; x" C+ l; z- Pserver {; l1 D+ Y9 I" c+ p; ~4 c
  listen       80;( E* I3 G3 J& t$ N% J% y/ P
  server_name  example.com;
3 s$ O4 k  H+ p) H) m' e9 y) {0 J
# L( u( V4 C6 Q9 ^; u' j" d8 a' X' _+ g' \8 q1 [
  location / {
8 r$ a3 G- c8 ^: B% x3 d8 H    ModSecurityEnabled on;" }' j9 _, W: |# V0 V
    ModSecurityConfig modsec_includes.conf;
" R! d' \; J% ^& j; Y8 ~# v    root   html;  M% [/ X2 }6 Y3 q  Q  b) S
    index  index.html index.htm;
% ~, q" p% x" H8 Z. M7 z0 c  }
8 J' C) |9 Z2 g/ z9 y+ i1 b  r: @3 J# X; c}
( r- X# j  I, a$ L( T: M% Z使用动态模块加载的配置方法& X9 n6 X% z6 g; ^
在需要启用Modsecurity的主机的location下面加入下面两行即可:
7 l9 u2 N0 M  Y
) _; A: @, H' y; b$ {, [: r% H% x3 X/ J0 X* t! X
modsecurity on;. e5 }  N. U% d
modsecurity_rules_file modsec_includes.conf;
' u! J) E; X% P* g1 e4 Q修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。; `  \2 N: |& N' Y  O; s

% s+ p3 N* y) o3 L0 ?9 P
0 n2 V% B! K/ _) p) n  X/ o$ vim /usr/local/nginx/conf/nginx.conf
2 @& F" k3 V0 L, S% W1 R
) m# b1 I8 c: Q% \/ `
/ `4 q4 i$ ^2 Z1 t6 L/ iserver {
5 n6 F" G. f, s; {  listen  80;4 q" }' t1 r8 x6 |# S5 |  {: s
  server_name localhost mike.hi-linux.com;
( J7 @" a9 D  a$ ~2 Y4 }  access_log /var/log/nginx/yourdomain.log;
; _$ F! P% X# B/ n* L8 ~7 n: }+ ^  `" ?9 E7 q7 u0 z

7 w7 w: R5 d" a  location / {
: E5 a  t1 ~) s: ]; r
& @) }! S% c( j/ i8 i
, I) V7 V, h/ h  modsecurity on;
3 y! l( H$ D8 G3 P6 n. I  modsecurity_rules_file modsec_includes.conf;
8 ]6 F6 \# Q: u6 v( x, C  root   html;
: P; f. o4 ^* x" e, f4 i9 r  index  index.html index.htm;
$ }, ]9 C# N: Y2 {+ l}) d8 j% w9 U0 w5 N
}
2 H1 A! U) a% Y. R- h+ |/ R验证Nginx配置文件4 M; s' l2 ]+ w
$ /usr/local/nginx/sbin/nginx -t7 }5 y9 `9 }0 g3 q' D9 `  a0 V! |" i" @
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok9 l5 u( Q  P8 l) |, X
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful* [+ N2 C5 h. p4 `
启动Nginx$ V# N) l4 o7 [- r8 ?, g
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf8 y6 q" J+ S. s
0 p  }0 K: z8 s( B) R$ X

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


( C% U; X3 Z% o3 y9 I- D

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-9-6 02:06 , Processed in 0.090996 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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