找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10509|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。, ^' m; ?- h5 U8 }& j
2 |) o6 p# e( @! v6 D

8 D! {1 z) c  f! s" C/ c- T& w在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。, R) l: n! t0 }& l9 B- G+ X' `  ^7 \0 X

+ M) ]/ \5 [' Y0 O
3 r. W" l; r+ {什么是ModSecurity! C7 U. H0 i% S: O% D9 S  ]
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
- y' C/ i8 ~6 |+ n. s+ V2 E
' C- @! F- v, `* \$ o
6 d# i* p" O9 kModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
# X; l$ R6 L5 C( |- r  a7 w, e) i; ]' P) V
9 B( ?9 O$ l( _5 B, x, A% ^, P/ y
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
) U. e4 C( }7 a! E9 \6 w& w) _5 `3 J9 ~! L# }* I1 l
5 y% P7 H5 N8 G9 q
官网: https://www.modsecurity.org/
6 R! w4 e+ d6 i( ~5 p% b! o! a- ]) v6 Q. n
* M- e/ t9 h6 h" C
什么是OWASP CRS: k( d5 @; x1 f( r4 z
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。) T# ^3 C3 q) j# ?) J% _$ @
1 i# w8 H2 d# B; c" ~! E# Y

" i6 H3 m& J: g0 M0 lModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
9 i* R! X" ?6 N0 d$ k" T$ |# x( \% Z8 j( |2 ^& H  y
1 f- W6 z; N$ b" j- U, t
HTTP Protection(HTTP防御)
( l4 j- e! |* u5 ?- xHTTP协议和本地定义使用的detectsviolations策略。" Y9 n% J: ?2 v7 L& l) A

$ j# C  g) u1 k# f% c9 f
* e- ?' l- F0 O7 P3 [4 X  `# BReal-time Blacklist Lookups(实时黑名单查询)# w/ u. A; y+ {, r6 X% h; H6 K
利用第三方IP名单。, m' y" A' M0 w4 {9 R! Y6 s* r- }3 D
5 S# c$ l' j9 A* b

4 n) s+ b' w) EHTTP Denial of Service Protections(HTTP的拒绝服务保护)
) H  K4 x" f& k( U9 y. I防御HTTP的洪水攻击和HTTP Dos攻击。
0 J$ e8 o' m1 L: \# i6 u  m! Z7 L* y

- [) s2 A# s  DCommon Web Attacks Protection(常见的Web攻击防护)# O9 o+ q  |: G" F
检测常见的Web应用程序的安全攻击。
. I" u: K. ^/ d8 ?! [5 O+ W: W& d4 t6 B
1 c& q' \/ s5 X1 Q
Automation Detection(自动化检测)8 p# l5 u& m$ R+ v8 n3 `* s
检测机器人,爬虫,扫描仪和其他表面恶意活动。
/ x9 ?7 A- [: d- }
# n2 ?; ?1 V7 x0 I; @' h4 _
* f9 W" {& s1 Y. ~Integration with AV Scanning for File Uploads(文件上传防病毒扫描)5 v' X! {3 |) D! U
检测通过Web应用程序上传的恶意文件。
5 i7 a- \  K; M6 l0 n
6 q7 r" @- S, O& Q/ Q  h
; ?4 N( Y: @& j* C& l. [Tracking Sensitive Data(跟踪敏感数据)
: W2 [4 i/ Q( v6 p, d; n信用卡通道的使用,并阻止泄漏。6 V) }, C* H% k8 C+ P

0 M" d7 E8 C% u# e! E' ?% D
! X( t7 j3 `  }& [) mTrojan Protection(木马防护), ]2 |# h8 i/ d- L; B
检测访问木马。0 m. U. V8 y  }5 d

) e3 V% V1 M* h9 r" p, H
% s0 @: k  l7 Y) \3 U) K/ mIdentification of Application Defects(应用程序缺陷的鉴定)
) d9 Y$ [: r& u: U' H; U检测应用程序的错误配置警报。& w* ^$ b, T  w$ l
, ]0 S: I4 W( g% n( `5 p' W
- ~/ b8 v8 v: N9 ]
Error Detection and Hiding(错误检测和隐藏)
0 L, s% ?. e" u" D$ _检测伪装服务器发送错误消息。9 b2 U3 @3 E: E% P, I  @

# X" Q! m0 C. A( V! _3 D, K" M" ?& B/ z$ M% |+ [* x% h, q8 I2 Q
安装ModSecurity  I  G3 O4 s+ i7 V# O
软件基础环境准备1 \8 ~$ A( {* U( j
下载对应软件包# c5 a3 {# `* u# F. m5 K2 e
$ cd /root
5 K* X6 q: p7 J- ^  ]& h1 E# W: J$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
2 w3 I( |2 c! I# w+ j) @9 L$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz6 K) p: }% B, ]( r" U
安装Nginx和ModSecurity依赖包
2 `0 f' `: A- y4 o( \/ Z1 tCentos/RHEL
* v! d: B1 u# ~7 l+ h# L  b3 B; j: i$ g# U6 F
: a2 I; w0 m3 A, k
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
- \0 ]3 c. o9 h8 [6 j2 VUbuntu/Debian
) F# R5 O7 s2 T8 N6 P- I5 f0 ^* ^/ C& I4 A1 I' [" Q

4 w& v8 u6 Z& S6 w! g  E$ 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
# M& \: K' X. @编译安装ModSecurity' ]/ ?: {, O* N3 {+ C$ a, V
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。( ^9 y3 n0 Q, {+ O# l% X, Y
& w: w  w/ [. R3 K) ]
& _) M- `) F5 m' U+ J+ ]% k
方法一:编译为Nginx静态模块" v9 |1 C3 T- ~* {( ?( m- V2 u
2 e% c) K* T5 v% [

4 B3 ^' r) X6 G. s3 @7 I编译为独立模块(modsecurity-2.9.1)
3 D0 B" y+ B# _  ?; {( Z1 K0 ^$ tar xzvf modsecurity-2.9.1.tar.gz7 w5 J) E+ D8 k& p6 |
$ cd modsecurity-2.9.1// \. ~: i- f, x1 u6 c( J
$ ./autogen.sh0 @& Q( h8 N9 Y
$ ./configure --enable-standalone-module --disable-mlogc, d# O; }* Y) r, N, L
$ make
; A+ S' z- X, l' p- J编译安装Nginx并添加ModSecurity模块
) X4 u9 o# C" t& d$ tar xzvf nginx-1.9.2.tar.gz
  \" V! w! u% y( ~  L5 Z# d$ cd nginx-1.9.2$ {' `. ?' m: Z0 L/ t6 u) U* S5 s
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
9 Z% y0 i: c: ~6 J$ make && make install7 S. T8 s# V! J# _( g) q
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
+ b, B3 O# c& ~4 B
9 M( R9 |& Y4 q& d
/ ?( ?3 N! F7 e; Z) Z1 P* I编译LibModSecurity(modsecurity-3.0)) u- s7 a( o& y+ A* d
$ cd /root
2 [3 T( m( a! R0 A$ git clone https://github.com/SpiderLabs/ModSecurity
* ~; E1 b- z9 j$ cd ModSecurity
: u8 W! G  Q+ U3 @% i$ git checkout -b v3/master origin/v3/master
. s, D2 v! l# }+ M* i$ sh build.sh: k' N3 `/ k7 j# n' @
$ git submodule init- U" F8 Y/ j, B" B% X2 F# \
$ git submodule update
9 \7 L" N# A6 Y: [: R6 P2 L0 }$ ./configure! V. g( R- }  ]  ~
$ make- z3 u! E; D5 ?* @3 c, u
$ make install' e1 `; N- ?2 v2 W; R
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。# `& q1 ^# K; [) L

# H5 T) B5 P8 B& }/ l, `0 @
) K1 w5 }2 y% O% K& s5 `1 y$ ls /usr/local/modsecurity/lib7 @2 f6 ~- c( e. ^/ A6 F
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
, T- H8 y+ W3 b# k4 O" q1 V编译安装Nginx并添加ModSecurity-Nginx Connector模块# q9 W- W( o  A6 p" Z
使用ModSecurity-Nginx模块来连接LibModSecurity: v: m, B" x7 y0 G

5 E6 z) o3 [7 C3 W: _. b4 B  y& s
$ cd /root& z) k- }) }, C4 Y* |
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
, {+ ~) z5 }* y1 |  B$ tar xzvf nginx-1.9.2.tar.gz
4 g; G0 p& t. S  \8 j. ]$ cd nginx-1.9.2
0 K& I1 y' \2 U  r1 m7 S$ ./configure --add-module=/root/modsecurity-nginx
5 }8 Y/ t% Z) I; N) @9 L" h$ make( |+ z2 [+ G0 p, A  o5 G
$ make && make install* `! M! g# X3 h3 Y  E& g
添加OWASP规则$ O( J9 }: h7 @; z$ |' k
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。' z, B3 P3 i: b4 l
* ~6 @2 I& c% q

- j/ q3 b/ Q' h9 y3 A下载OWASP规则并生成配置文件
5 H  x) g) ]; V) y# Y$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
* X# j* |/ }: |5 K5 E. ?, j$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
. Y- ~7 K9 N' S, {$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
1 e3 V" B% G  t, {4 S# e; B' A$ cp crs-setup.conf.example  crs-setup.conf( d0 G: S: M- ]9 ~
配置OWASP规则6 P, t$ j8 E8 L, L9 y# e# N( m
编辑crs-setup.conf文件
8 A" H8 M3 q6 y# o2 q5 u5 q( w
! G  P4 |; ~7 I* w& ]' _2 `" G/ V+ p2 c3 k4 T
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf8 c3 ^( C8 O/ F
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
% C! ?/ s$ `2 v2 I2 ^# c$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf7 L( G" J% V1 n8 |3 a6 p
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
2 Z& T6 C' F- Y+ A5 s默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。: L+ O) J3 U) D! I7 a* C& d- g

& |) ^4 S, C& W; D1 p3 v3 w5 q$ [# _* k* u2 V" Z$ J* f% R/ _) E
启用ModSecurity模块和CRS规则
/ s# `5 O3 `' z7 L复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
4 W* e, h$ G9 G9 q* H0 V
2 S2 Z6 k; C! `" ]0 ?2 Y) s
1 D: p- t9 z; q$ W: W. h0 K- y( `modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。. ?# N! |2 @, ^9 \

. n/ Z0 x$ l. N# d* Q% d- p% z
5 e5 m- S8 F0 x' ]& L. ~. \4 S$ cd /root/modsecurity-2.9.1/
. B0 l9 S0 l3 h3 I0 a' b0 H$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
- ?) k" b: E9 `$ cp unicode.mapping  /usr/local/nginx/conf/
3 j7 ~( G) N. @; |将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。; v' B% T3 ~4 F9 b! `7 P7 k+ {* P+ x" X
6 T7 \2 c& [: Z$ X

! G5 d! l+ D8 N; E$ vim /usr/local/nginx/conf/modsecurity.conf! t" O/ B% i. p
SecRuleEngine On- B8 l# I+ E6 n9 S& U
ModSecurity中几个常用配置说明:. e6 D; b& |5 O
# Z+ Q, K/ Z  M& T
* G- O  J$ _7 H+ p
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。- o$ X" ]( [7 k* u# C' D
* |  Y$ {3 [+ d! ~7 a& v9 X5 E

5 b+ b7 E0 `6 ~5 v/ g& ?2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
8 X+ f- b4 o" }! S: ~6 O, w. \$ T% S5 G. v
% R/ s# w' X3 q( E- @' z
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。8 |8 V. r& D: c; B

$ z  m5 S$ d) l' K; r! M: s: f# W+ p
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。0 N- W2 K% T- q0 l2 T

1 T) a# ]3 x8 i( N
1 Z. B' u7 D$ R- Z5 |% Y在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
) h3 A* a& {. s
2 J3 K- o6 L+ T2 i# @: v+ \6 v
& _5 K' C& p: ~: \- i3.x版本CRS6 I% ?# m1 D( D$ _6 v
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs2 A9 W6 u* b7 h2 p
# 生成例外排除请求的配置文件
# {1 w3 K( E# |- o$ b  A7 X$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
: V* H* N3 X% z5 \, p% Z7 j# R0 s$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf  Z3 p+ W5 K0 ?& Y
$ cp rules/*.data /usr/local/nginx/conf' }1 N5 A1 r, D2 f; H8 ~
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
8 S0 R7 T5 X( d# d1 s. C, B2 \' b) ]1 G6 x- e; O& E! P  @% L

# d+ O- I/ V4 z$ vim /usr/local/nginx/conf/modsec_includes.conf
) n; z6 o2 a% U, u. X9 s. {7 K9 }. B7 [3 q& ?$ X* e
[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; i& F3 C9 c* J
! c8 w( \7 t! D6 U1 M! M注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。0 c& b+ C8 U" p4 w

: p0 q6 ^4 i; c8 F
; _/ }  W. G3 s# ~配置Nginx支持Modsecurity% V$ O- D3 R3 u' [4 V6 ~
启用Modsecurity1 _1 |! C+ c; m$ H, K, |1 ]
使用静态模块加载的配置方法9 K$ f; `* g6 V1 u0 @
在需要启用Modsecurity的主机的location下面加入下面两行即可:9 Q5 B8 M5 D% t5 _8 c) ^9 z- x

: _0 O; M. i5 J* n3 r; ?* x; Q4 @8 |7 ^. ^& {0 F1 p7 S/ q3 e
ModSecurityEnabled on;# J( Z/ k8 l: r) I7 i4 X
ModSecurityConfig modsec_includes.conf;" }: T- q2 `) ~- H( p' D$ O: S5 T
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
2 G9 o) p" I1 S2 t/ y- i! f5 B; a
# A0 v' u, M# i. O! F  C* n3 k, h* o$ M/ E' U& F% v0 P; O" o1 ~
$ vim /usr/local/nginx/conf/nginx.conf. d% V& r$ y/ U: g4 E; P

* v# i) @$ X" u1 C; n( i! Q  D7 j! j! E" r
server {
. R; ~% H: S* ]6 g1 R* l  listen       80;
0 i8 z; L' k2 D+ z% Q  server_name  example.com;5 l; m  n4 t6 m/ j: W

# O4 X+ B$ [* |2 O& m% s9 F2 ?, L- o
$ y, ~; @7 P1 }  A/ ^2 Q7 {  location / {, [2 y, }& _/ ^0 N( w% O
    ModSecurityEnabled on;
3 I) s/ S- n: E2 a) U  i    ModSecurityConfig modsec_includes.conf;# o) a% ]# Z( o3 I
    root   html;+ B/ F' W% c3 f; y
    index  index.html index.htm;
8 a6 J8 V. M$ \# Y, s  }
3 |+ E, C. _; h9 [: T% r2 ~}; Q" [8 a. i% k; V8 f3 Y
使用动态模块加载的配置方法1 Q# V' S8 x- I* z) u( Q1 I
在需要启用Modsecurity的主机的location下面加入下面两行即可:$ g# e; W" v7 H/ V+ P# h' J

: c8 ]" s! o8 k2 n
+ X) M3 G+ R# k* _  C: y% f7 a$ emodsecurity on;
4 m) [2 Y1 H9 B! I" |modsecurity_rules_file modsec_includes.conf;
. u' C& ~7 z3 ~4 J9 _修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
2 n9 B1 j0 |. A
2 Q) L' W; x5 o) K) ?( N. F# ?; m4 p* E/ f
$ vim /usr/local/nginx/conf/nginx.conf
3 L  u6 z9 |% Q! X" u' b1 Q. q8 R  I" f
9 x" u2 x0 w  b  i! C
server {$ a8 H9 q% U: \% [  {$ \% N; z5 D
  listen  80;3 f, c/ H4 W; s% o! @; O% X, k
  server_name localhost mike.hi-linux.com;1 Y( h! ^' c# t; @1 P
  access_log /var/log/nginx/yourdomain.log;
5 C- E6 }0 k# ^% j$ K; ~2 }3 P" D$ K! i+ r7 z' a& D) O  u4 n
, N- {5 k: I% h* n/ F
  location / {
  V( I8 d2 y: s! d; g' r0 W+ f$ f5 j  L. M/ B4 K9 `

2 \7 T# h' ~+ R! d/ @2 a4 G  modsecurity on;9 p& S. g! J+ f6 a1 k
  modsecurity_rules_file modsec_includes.conf;
  U" x+ F7 d6 V5 [) G  root   html;
' Q) ?/ p/ @, \: R! X/ c' Q  t  index  index.html index.htm;/ e5 k# \0 O1 {8 Q
}
/ X  d3 v0 T! X' {2 y6 Z) y; K}
4 @: E, V/ v) J6 x6 D验证Nginx配置文件2 y# B8 O2 K4 {+ c5 p
$ /usr/local/nginx/sbin/nginx -t
' P" x1 g( }' `) f& ?3 X$ w2 jnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
' r4 L! X' l5 N% h8 `# b  Jnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful* ~2 j" o, d7 p" T: v
启动Nginx
' M/ M' D6 r* y/ S" {9 N1 U$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf/ c5 u5 F. l: k+ e( i/ D
8 }0 I: n4 V& c2 ^

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


) t/ |: ~( F" e3 N; j9 f

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-3-15 01:04 , Processed in 0.086498 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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