找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12365|回复: 0

利用ModSecurity在Nginx上构建WAF

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

5 }' I' i, |( _- q8 I+ ~6 ^& S, o, E1 J2 N
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。5 j5 W: Z/ s; _* l9 Y! D% l- \

0 P' _. f4 N* @" W/ S0 B- ^2 D
. c! ^1 P; Y4 c- H什么是ModSecurity
, l, |( e, m* ]2 SModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
1 P! }. A5 p. @* V& Q" j% A
' H6 Y) r: i) D9 X. l: t* @  X; B: r3 ~
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
# D0 w. `1 f$ C8 `8 e. _: M
6 L, S; }- T8 O  C8 b, A9 X# w2 y4 K. _
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。( O' Y# t" A, K. P* y
5 B+ e( c3 U1 ]! Z5 Z: M! t

- G8 s* p5 O5 X! [8 ^6 W. s官网: https://www.modsecurity.org/; h- x# p, Q+ I! J- t" r& K
1 i2 p+ ]  u: L( p& k

* q! }$ O' f  |- T+ \/ g1 x什么是OWASP CRS. f4 Y# Y- J" g; q" j/ v
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
" J- M; e, k# e1 J( W6 e) C4 I
+ B  U* w, {! X# o" U; D  ]" v
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
9 t) c0 Q& f9 u$ }9 T2 H
! a8 }* y7 b0 v0 S( [
# f/ R) R* \: l$ EHTTP Protection(HTTP防御)9 X, Y; x2 V" X9 w
HTTP协议和本地定义使用的detectsviolations策略。4 v) x+ W) l7 [$ ]& ~

- w' W8 ~2 G) H$ b) J( k6 R. H8 J# z/ h, i4 p1 B
Real-time Blacklist Lookups(实时黑名单查询)- I; h1 v& ?4 G
利用第三方IP名单。+ p7 ^5 p+ o7 l( L/ ?3 F1 \
, @& T$ |7 \# l7 v
. M* l* K9 C+ L; U; y1 t. S; i& U
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
  b) U6 R5 i5 K2 ~4 H$ e* q9 B% a防御HTTP的洪水攻击和HTTP Dos攻击。8 k; [  g" B4 [! Q. @' E  u

' p7 p: M$ V$ J( q3 X  w8 d3 v  b6 L8 B  N# J
Common Web Attacks Protection(常见的Web攻击防护)
7 w! x$ m& r2 j: n8 ~. s  O检测常见的Web应用程序的安全攻击。
* e" J: c. Q. \6 [4 u- G
/ n# I3 J% @! |3 O( {; F+ N3 T+ J0 M- ?& b' V
Automation Detection(自动化检测)
3 o$ k! c3 N; I4 f* z检测机器人,爬虫,扫描仪和其他表面恶意活动。6 R3 a6 }6 a- y; V: O, ?. a! E  k
" n) K& i' n; N9 M

- E  ]  I9 u" v) x5 ?  HIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
8 r) }" w' S0 i' M2 u5 s2 g检测通过Web应用程序上传的恶意文件。  n! T0 k: k9 c( ], {
2 r1 f5 f  E8 E" @1 {

+ n" `0 T1 e4 N( r' k8 y# m, d: i3 \Tracking Sensitive Data(跟踪敏感数据)& e8 D& y% j. u5 I! x. n* ?
信用卡通道的使用,并阻止泄漏。0 s  S+ X. Z$ N* t5 P
# O9 Q" u# B* M( t0 B
# Y, `0 p. I2 M5 K& X8 i  ~. V6 T
Trojan Protection(木马防护)
( K1 o( m9 n0 y检测访问木马。4 ~0 A/ s* y5 r
* N* W: k  k4 b8 n8 x- Z

: M3 O4 ~9 A$ q; L" x- X4 rIdentification of Application Defects(应用程序缺陷的鉴定)
$ L+ y3 n7 v+ \4 V检测应用程序的错误配置警报。
5 V+ I9 t% G) O7 `7 y- g( a+ _' }' \  M' S
$ I" K  M& h; A- N. r
Error Detection and Hiding(错误检测和隐藏)
; e7 Z  U- s( X$ u检测伪装服务器发送错误消息。' z. I3 l9 E& l0 B
% u# J/ ^) s; Y( r, U

$ g7 R9 Y) B7 _3 `4 _安装ModSecurity2 \9 S$ ]6 O& o' s) u
软件基础环境准备$ C) B- a$ u/ L3 H
下载对应软件包
4 N* I  a1 O% P7 i- @$ cd /root* p1 R8 X5 I2 j6 n0 T- h/ a  @) C7 b
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'( z; A. {) U0 `. t6 r( {
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz3 ~+ |! @# R% _$ ^
安装Nginx和ModSecurity依赖包
# R6 C- l9 f+ pCentos/RHEL
& h0 F$ U  t. W  |, U% [* i) ]- a7 q

3 D. W8 t4 ?' D5 J$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
  K$ V8 S: W  m$ ], }; `Ubuntu/Debian
/ M1 U" v+ p3 ]
" `; A( ]+ t6 F  q6 w& y. v  w$ K0 q3 ?
$ 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/ y* U# w8 T  z
编译安装ModSecurity
  X/ U# M3 r( t) ^! d7 \+ r( bNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。3 l# O$ n$ J% X* c9 W) }
/ O* M) u' r. L* y# S
6 L& ?0 K# |* r" U$ \' ]) I
方法一:编译为Nginx静态模块# V0 I$ m5 g2 ^6 V$ A* q4 W
0 W, X4 }! k1 q, ]

+ M1 D  q8 O9 Y) m编译为独立模块(modsecurity-2.9.1)8 K: X0 z% U9 [5 F) {( b, ^
$ tar xzvf modsecurity-2.9.1.tar.gz! w" c! _: T" Z( _4 `! ^0 c. O
$ cd modsecurity-2.9.1/- z) X+ E6 |' H+ @
$ ./autogen.sh
1 J  g' o! r) }+ w* m" j' b$ ./configure --enable-standalone-module --disable-mlogc
/ `0 O3 Y( ^, D. v+ N$ make0 Y7 N4 U1 t  g. ?7 {9 }9 R
编译安装Nginx并添加ModSecurity模块0 {7 b8 ~( J8 J: H' c
$ tar xzvf nginx-1.9.2.tar.gz
# X; w' u" Q. S/ |0 Q$ cd nginx-1.9.26 Q, |5 w* e* b8 l3 S' {3 D
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
2 R7 z1 Z# V1 n- b) a. g( x/ _3 V1 L9 N$ make && make install( d; L5 V* B2 W
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块1 Q6 r+ ^3 l+ {. x; k5 M

- ^( x5 v( ]4 ?- Q8 R$ C5 f" Y- F) ?( o
编译LibModSecurity(modsecurity-3.0)
' a% n6 \# n& [2 w! V$ cd /root7 f/ y  h5 T9 j
$ git clone https://github.com/SpiderLabs/ModSecurity9 r0 ?' ~/ ]( c; I$ m. N9 `
$ cd ModSecurity
& Y7 W( x4 \' l$ K9 [2 k" B$ git checkout -b v3/master origin/v3/master) k7 Y" p7 y* F$ N
$ sh build.sh$ t+ k$ ^) R+ `4 o; M- w& V9 u
$ git submodule init
8 V% s0 J5 K. ~' M2 G2 K$ git submodule update
8 @6 v1 q0 B- y- H8 M  R# g# Y$ ./configure- f3 Q& P! `% z3 U8 J$ k! V- G1 l
$ make0 `7 F6 ?" d6 y1 Q" }" z: \
$ make install
6 \0 n) ^0 _  Q& T( LLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
; P6 O" r* p: v% \0 ?4 f; {, T
) p8 |! P3 H0 S. }; v' m0 Y5 V& ]( o) P. |! p& u! C2 K- o' ^
$ ls /usr/local/modsecurity/lib
& X- Z" @5 [; V7 M( olibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0/ r4 R3 L* g; u- p% e: o- C
编译安装Nginx并添加ModSecurity-Nginx Connector模块1 f! n7 o3 h7 G! H- p( f7 y# _
使用ModSecurity-Nginx模块来连接LibModSecurity7 I) y# ^/ o( y6 v" }: `# g
4 M3 s% \* [2 z0 y( V; S! H, N8 ~
6 N4 d+ O6 w& S8 a7 e
$ cd /root
& L6 N2 m* F2 p5 z' q( E4 x$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx3 n  `  T. w3 ~# a' D1 O. `% H& k
$ tar xzvf nginx-1.9.2.tar.gz
" w3 [. z, B' x) l+ l1 w, I- `$ cd nginx-1.9.2
, D7 o' l4 R2 |* e% ?$ W9 M1 q$ ./configure --add-module=/root/modsecurity-nginx5 |, g- J, g8 Z0 g
$ make+ v3 U, [& E# L- G3 A
$ make && make install
! m9 r$ N8 {* q/ R" O1 Q$ f添加OWASP规则" s; u6 }$ b8 K: M3 m
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。( F' [6 ]* J! v

; S) m" ?0 Q. f: a& i5 x7 q3 }0 _# b% V
下载OWASP规则并生成配置文件+ v5 _+ v! u0 e. K4 D- h7 H
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git# ]. a+ N7 C, N( c  J
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
- W% m. I+ q, y% C5 K$ cd /usr/local/nginx/conf/owasp-modsecurity-crs* @0 H' _! \4 j. ?+ B  l
$ cp crs-setup.conf.example  crs-setup.conf
5 V% v( B9 o3 |, J8 I$ K( X/ c配置OWASP规则/ b  Q* i8 @2 h8 q
编辑crs-setup.conf文件9 b8 l6 S: I$ _# k7 L2 ]
4 o( b% I, {+ y5 x5 M8 j; e( X& \! K

9 ^5 q$ X) v4 D' Z& [& r4 I$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
, D$ N, _. S( T- \$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
" U/ B' j: c" M# F0 t  p: [# U$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
# j* d: d2 H( t" ^$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
! L0 I' |6 K( R. z! T默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。4 `- w) b) Y$ N, S% U9 R

5 Y/ x5 |) L' r' Q( H* B5 q4 x1 K- J" j& b1 t
启用ModSecurity模块和CRS规则
2 v2 C! |5 q5 _  F2 [8 e复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
% Y% J- [; v5 C( h# g. ^( |" O5 p
: T5 h9 h9 i) _# U. k/ e3 |+ p* U9 {$ c5 ]
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。) F9 C4 S3 N6 W# w$ {! x
' ?4 a3 v: M& y0 f( `

1 P' X8 n% E: ^* d1 ?( K- Z9 y$ cd /root/modsecurity-2.9.1/9 p8 ~/ @  d- K5 W. h2 l" g7 J
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  2 u& M% T1 J+ l0 z
$ cp unicode.mapping  /usr/local/nginx/conf/
8 l7 a, P* s2 O( W将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
0 K; ~! l6 m. H7 z0 f- O
% b9 L. n$ }5 s2 Y9 q' i+ M$ A* n% Y) }' n
$ vim /usr/local/nginx/conf/modsecurity.conf
0 [1 s# m0 i$ I% W6 ?SecRuleEngine On
# [  O  ~1 |  \) _ModSecurity中几个常用配置说明:
! z- ~0 |+ M! d/ `( g% f9 N4 @+ m3 G4 S: J- j( o; w6 o- U4 k. `
  k4 z+ @" f0 W% |
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。# B8 T7 E6 c3 U& P# P  l

% N9 e! W1 [& d& t( S# g/ m/ X
/ k+ A2 f+ @3 N& A. n: e6 x- H2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。! W: \9 Q; X; i% U& q/ K

0 e& {0 E6 m. T. v0 S( l: v% a# S5 k, c8 g) S
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
" O+ }) c9 L/ R' |5 l7 i) d0 Y- B; M
! H7 N$ T2 D7 I
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。2 J& b" E% T$ W
0 v; \5 D. e2 R9 w8 Y. ~; f  Z

5 q; o4 `1 O7 ]& {1 J8 z! {在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
- s% a, C4 M& d$ q8 `  q  t( D$ n: K2 j7 E0 X% O  i7 i3 v. P' Q/ o
7 i( g# }0 K0 B4 ]5 O% C1 T- [
3.x版本CRS& A' u! G* f! Z% F+ D( _
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs# H  s4 j. @: c' v- h, D0 v: r
# 生成例外排除请求的配置文件
. u5 [+ D) j; O2 B$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf+ B" z& B1 U! [  a/ c
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
2 q+ _/ c* r8 }2 i. s0 h$ e$ cp rules/*.data /usr/local/nginx/conf
* N1 ]$ L! w4 e为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
# \3 O2 t# X  P0 Z8 D. w+ F# r
3 [5 z8 m! c5 v7 M; @# v, g  m9 I+ J* E9 v+ b' V
$ vim /usr/local/nginx/conf/modsec_includes.conf9 r  `$ b- A" ^& G, O( o) b
% [! ?' b0 N( B5 |0 x& I/ g1 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

9 h# i- ~  S: Y' X- O% B! o- a  c! w6 C
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
+ @& ]$ w9 a+ |2 ~& B7 R8 y( h; f9 {$ }9 \( w' T

, B6 g8 ?9 k- j% N- b配置Nginx支持Modsecurity$ r2 E9 B' U4 i+ G, b2 j
启用Modsecurity( j# Y. z- @% Q- _4 d6 p
使用静态模块加载的配置方法
  @0 n0 O. I, a5 ~- [在需要启用Modsecurity的主机的location下面加入下面两行即可:
: \+ G% P5 E; l6 T0 ~& ~+ f3 \. v2 ~0 h; p, y# k8 v
* d- P0 j- M  z7 W
ModSecurityEnabled on;. }4 a$ s, V% |, f; E4 }( _
ModSecurityConfig modsec_includes.conf;6 ^& s4 Y3 Y6 L+ N6 G- ^' u0 ~  u
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
1 Z, A8 Y+ g; |- }& [/ w: \
+ ^* _, }  K; O, N1 Z- k: s
2 B" j/ a& t" t  A- j% ~, O7 K9 ^$ vim /usr/local/nginx/conf/nginx.conf( V# ]( @# t( J  Z1 ?; x2 l

& @" M6 _2 W$ u
5 h( k: q* V" ^3 _server {
8 E2 X, u$ i% _; _$ g+ i2 l  listen       80;0 K' t- [. w6 y# l5 H9 `6 t
  server_name  example.com;/ K- d8 I' @% D6 V

8 M% M$ Q/ e+ O; H0 b0 A4 J$ `) J2 T! v+ r; J
  location / {
* R# w% L/ J  ^. Z" d    ModSecurityEnabled on;, N' J+ l" l: {7 M* i, N6 C
    ModSecurityConfig modsec_includes.conf;
5 k% u3 Z* p# @+ e    root   html;% ~/ t1 ^4 |, x6 C2 b
    index  index.html index.htm;
& y) }- J0 k1 ~% K. g5 R  }
4 h* [" d$ ~) ~! Y( R& z" r6 R( x}
+ g% M" k' c6 L使用动态模块加载的配置方法, ]! J" ~7 y) X8 M8 F/ ]/ i% N
在需要启用Modsecurity的主机的location下面加入下面两行即可:4 i; V2 w  T1 _  e( A  O% w( S  r. I
! x* H% d4 |" f( r/ h
1 ^) f/ c2 b. j% @
modsecurity on;
* K& @' ]5 I! v) f9 ~( }modsecurity_rules_file modsec_includes.conf;
7 E  X* t  c. b! E+ o修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。9 v0 V% _8 {0 x/ @7 Z8 @6 c

2 Z5 U8 \: c7 K' ^' M) R) R+ W5 P  F
$ vim /usr/local/nginx/conf/nginx.conf8 G. M' T: V: N+ J  |
. k" m2 j3 |) m  c% H! b
+ n" G* j( ^8 f
server {
& q) j: b2 W& G  listen  80;0 D0 U2 E" F' {6 g$ w, c( C2 I% @) @
  server_name localhost mike.hi-linux.com;
, v3 `$ d3 L7 r; X+ T3 u- R3 v$ l  access_log /var/log/nginx/yourdomain.log;" E, ~1 ~* g( h' k! I5 q  {: S
: ]) s' e; }7 i1 Q; e. ^8 p
! u8 {1 u/ T" J8 E6 C* ?
  location / {
6 j5 I7 B$ s. a  d8 P8 k- X8 t4 v8 D+ ]; g- _$ F

. o% v6 k& X, v- Z2 m  modsecurity on;
* {& z& R. o0 V0 F& \5 u* W: H  modsecurity_rules_file modsec_includes.conf;
* ]5 }/ ~. O  p, Z' A  root   html;4 P8 L* o0 i& e" F, k
  index  index.html index.htm;2 G* w. J* ]1 c) G
}  y. O- U) A9 {/ |
}3 \9 |) @( J( A! a+ u3 t9 C3 `
验证Nginx配置文件+ h' Q& @4 U# |& z' I, i
$ /usr/local/nginx/sbin/nginx -t
* D! \4 y% {$ P* p0 Z4 K( Anginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
3 B' f* B$ n- Q5 g/ o' Dnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
1 J; @, a( i  X启动Nginx
# d7 `) U; x1 q9 X6 Z* U* f$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf) I8 _% W9 ]0 [* Z  ~; o

9 {" _- L/ W5 D. r+ p" C' K

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


( p4 Z# l9 Q( O

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-9-15 12:06 , Processed in 0.053508 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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