找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12184|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
# u! ~" X1 S; {  v9 c. P. h5 v# k  x, ^1 k' B0 q8 z; b- s
. ]+ ~/ X9 O1 Z0 M* }
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。$ @% C0 r! {( T2 j2 J) [
- |" g  i3 [7 V: w+ }

0 f% p- q: M- x; C0 q什么是ModSecurity
% _, y: r- q. I; R, PModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。" s8 p3 c4 h8 U+ o: s3 X3 r
' M' Y9 p# k* p/ L2 Z/ p- P

8 \0 K% |1 t/ s) xModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
" Q) {# Z/ }7 W' }: ~1 s7 p
9 T& o% t$ b; B4 S( I  i' j
# w& _8 b) ~6 ]" MModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。% g# w' T; r  I7 T' V7 H

  \4 _, m7 g5 w. H9 N6 C3 a0 V6 G% |$ o* u+ Z) |
官网: https://www.modsecurity.org/! I# @( C3 t+ z  J
) o1 ?, }2 G$ G5 a* s( w

, R6 T% }+ O0 e3 {; x( \什么是OWASP CRS, O( Q6 ?* k3 {5 y1 H1 o
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。6 v. O$ T0 @" i+ x" S2 m! z
# d8 e: c. p# i

' ^0 B9 f/ ?0 Y& _; @ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。8 I" o! b3 c# O4 @( [9 y9 p! |
% v; O3 ]) O' I3 `+ \
* [; a$ f% m8 D/ s) k
HTTP Protection(HTTP防御)9 K% y% ~2 q9 d) a" C: J
HTTP协议和本地定义使用的detectsviolations策略。
' S7 }& x" }% W9 G) m# D- P5 o' j8 `4 K* z
3 g5 Y0 ]* Y! P5 P
Real-time Blacklist Lookups(实时黑名单查询). p! O, {6 J& ~
利用第三方IP名单。0 E1 w% [- T  J% }; g) R

' x) \) E& y) _% z
7 x$ ~$ u' n1 l. H6 P1 V4 J" mHTTP Denial of Service Protections(HTTP的拒绝服务保护)
, S% L. V) w$ T) g, z防御HTTP的洪水攻击和HTTP Dos攻击。1 f# O$ m! h( M8 \2 H1 _, {

: P; k6 n# Y9 ~3 r9 n/ Q& f/ ]# Z/ M; C- D( V" T- u8 J
Common Web Attacks Protection(常见的Web攻击防护)
, C* k$ s6 _+ \, k) d$ ]4 k' }检测常见的Web应用程序的安全攻击。5 t& v3 m# W. O  B0 H. D

6 S% q0 x  t7 d
4 @+ \' [4 j  \8 ZAutomation Detection(自动化检测)
# H) f7 V. N( u& n" o( b# y检测机器人,爬虫,扫描仪和其他表面恶意活动。
% z2 s- D/ Z9 Y
) n" M; f1 O' ]9 h" ], M* ^4 K7 p, k* [/ v) a' W
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
8 B1 E& ^9 e' i0 K1 I: ~( f检测通过Web应用程序上传的恶意文件。
: _* Z9 I" a  h+ @
+ l5 L) A1 ~3 |6 o% r4 z5 j7 G7 _5 N+ w$ ?
Tracking Sensitive Data(跟踪敏感数据)
# j* Q0 b( V) R, U$ R信用卡通道的使用,并阻止泄漏。
8 p' W8 N: C- p" O, W
& [/ e) `# b' D- i7 P& V! w& d/ m2 H( s1 R% x. ]) @7 {3 G+ K
Trojan Protection(木马防护)
2 ]% Z6 B; c9 e" z检测访问木马。' S+ O. F+ K% l; a# O" |  \- M
" n! P0 o9 r* Q0 N
2 L% ?+ T; Z$ i/ ^1 v
Identification of Application Defects(应用程序缺陷的鉴定)) C; O# x3 y( X0 Q3 g; k  C
检测应用程序的错误配置警报。6 Z3 M& `4 v- j1 A4 |% v" }
0 z0 H. R9 Q! k

' `0 H5 w7 F; U( ]Error Detection and Hiding(错误检测和隐藏)  c3 x$ r0 h; `7 v; g. k6 B( F
检测伪装服务器发送错误消息。$ ]% D* V: e5 H. F" |3 _3 r2 w
$ Z0 i& o7 M  ^8 H
- t0 p! T0 o8 l  n8 Z1 T
安装ModSecurity0 C% ?0 t5 \, o# W
软件基础环境准备' I( x2 w( T' s. j
下载对应软件包
* ^  m1 }8 ^7 g: p6 f$ cd /root
% U  u! [5 D, [4 o: _- _6 R$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'8 s7 `, L$ y+ e) m
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
) E0 \) \9 ~/ L: ]! P+ I安装Nginx和ModSecurity依赖包9 i. |8 p  J( a* k( ^1 O2 ]
Centos/RHEL
, a5 e9 g1 y- p0 ?- g8 g7 b& I2 D, `, x
9 s# D$ m) f1 ^: F/ J4 Y
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
# Q2 h1 o3 R* nUbuntu/Debian
* g# J* @9 a  V- ~% R% o# f2 y$ n( C1 t$ a

2 B2 k  X, z2 u; ~$ 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
/ O, w/ s6 N0 C  C) E0 g( q* [编译安装ModSecurity
4 f; V9 o% ]/ y7 eNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。  D% V) {  Q; L8 S% y* O3 e
- D7 y; b. M0 {4 s
& i) e) R. a$ Y8 J1 F
方法一:编译为Nginx静态模块
" `$ Y/ T: v8 g8 S# C# K! D' Q- |$ P2 f) q3 a# q% Q3 o! L
8 X' L8 |* z, t6 y# t' C2 g
编译为独立模块(modsecurity-2.9.1)
+ e$ ]5 g8 j3 N  H$ tar xzvf modsecurity-2.9.1.tar.gz, u2 K. t) ?6 y0 K; q
$ cd modsecurity-2.9.1/
+ G( {2 q; ?! D) o* j" ^$ ./autogen.sh3 b5 o. |; l% y, ?4 \5 c/ C
$ ./configure --enable-standalone-module --disable-mlogc
4 n; p+ v! I* `# r$ make% s  I- j9 X; u
编译安装Nginx并添加ModSecurity模块
3 @* g- `' q7 \; J& |# o+ }$ tar xzvf nginx-1.9.2.tar.gz. b9 D7 i4 @% l# f- D7 P; a
$ cd nginx-1.9.2; ?; Q" y6 N# \' X  D1 V
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/1 a. y  j4 Z) A2 Z
$ make && make install
" s% l6 g* h9 [) {  x% _方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
& H8 z8 v1 e, Y' h4 \3 ^; I: ?! S* N  [5 J

' ?4 k% h* O2 o2 [. L编译LibModSecurity(modsecurity-3.0)
  w( a) C( Y+ \$ cd /root4 }; |+ ]$ R6 D/ r2 o. @/ u6 X. ]
$ git clone https://github.com/SpiderLabs/ModSecurity
" \: m6 f4 h  X+ k( `1 \$ cd ModSecurity8 J# _4 o0 P, m, G
$ git checkout -b v3/master origin/v3/master
0 i& q' k: O! U4 [$ sh build.sh0 a. h. |+ l  ^. `% ?, }
$ git submodule init
9 _4 s5 r& ]0 q* V+ y9 b$ git submodule update
+ l# D4 K/ D6 ?/ n" ^$ ./configure
6 ^$ C* G4 d- T9 Y' {$ make8 `/ L" U4 `" _
$ make install0 x0 f5 v$ G9 h- p- ?
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。" w9 g7 Y- N- Z) w- w) d2 }$ O
1 \5 o! i% _0 _/ V! r. |% J2 V
" k# `8 V+ N% D4 ^' A- J
$ ls /usr/local/modsecurity/lib, D5 D+ Y$ S1 B; P# N) I$ u' w+ I* O
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.05 }7 g8 W7 o7 [7 S. B, u! j/ P: S
编译安装Nginx并添加ModSecurity-Nginx Connector模块
" n& d* t  D8 O# I4 ?使用ModSecurity-Nginx模块来连接LibModSecurity
0 P, u* `* v0 o  u3 i6 D, P8 S! t1 X1 y
  @( k% e3 I# _! Z+ Q6 ], Z
$ cd /root+ v% O; p& {& w1 O* o' k8 u) {
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx4 M0 H/ R* u! l- M8 b$ _
$ tar xzvf nginx-1.9.2.tar.gz
) m" r' t" y% Y8 r9 V- U% b$ cd nginx-1.9.25 ^2 B1 h, p; v
$ ./configure --add-module=/root/modsecurity-nginx
: D/ O. y, o. Y5 X0 l& ?/ W$ make
8 }9 {% E  w: M# z, d$ make && make install/ ~/ B; g7 M) S. }) C3 \
添加OWASP规则
3 C! g& J8 x2 o6 a# aModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
9 \" V" B$ o2 ?% z% D& Q
. J% J# O; s  V5 o1 @. R  s2 i7 ?  k3 U0 A$ S8 C- E
下载OWASP规则并生成配置文件( }6 Q- U- x5 J7 Z! `# f# m. y
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
/ N2 o) s  M5 E3 @- D" V9 N$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/" O2 ~! r; |/ g9 h9 J9 E$ L
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs7 ^% i0 H9 n, c4 |4 H5 f6 q
$ cp crs-setup.conf.example  crs-setup.conf0 B1 h0 l# O4 Q( @
配置OWASP规则
% t8 R8 r. X# S) Y' ], T' D( `编辑crs-setup.conf文件# H- Z; d3 f: r5 X) D; A
: m6 H- g4 k9 g7 Z

5 e$ d8 a3 s# S' U7 [. d$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
4 n, e4 u$ p" y: {) X$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf; o7 w/ N! \# l, R  {( }  ]
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
6 F0 ~$ B( r2 _# Z. a# u$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
9 D: V# g  o% Q, M默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
) {1 h7 C" ?. M7 t
6 K- d2 Q5 A" y) N7 r
# [% R( u4 ^2 _7 {: R+ _8 E启用ModSecurity模块和CRS规则
5 S( B0 J% B: ]# p' {复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
4 J6 {! o" q: J
2 x( x: P% [. b' [
# K( R7 w9 H- _3 D: wmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
$ F6 m" s4 ]) g  O! p/ M5 j
% B/ K2 b4 [5 D1 m/ W
/ E3 ]! _+ K6 @, A$ cd /root/modsecurity-2.9.1/
6 v0 I  r: \0 q. o* Y4 h$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  # C& [: k) ]% u, ]3 k3 N) U
$ cp unicode.mapping  /usr/local/nginx/conf/
" S  A6 f$ r6 O; D9 w将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
: y. D1 `1 x' v0 H$ f
/ h- j, o* q8 e- \% s7 g5 J) P
* ]2 [$ l2 H5 S5 F7 H$ vim /usr/local/nginx/conf/modsecurity.conf1 _: D  [, m- a. T: t+ w, X
SecRuleEngine On
4 i! A7 @1 _3 y0 K  gModSecurity中几个常用配置说明:
4 ^9 G7 `7 ?" o/ W: \
. r+ p& u" ?: _; [2 X: `/ E
  o! g6 }  L! r, s$ R. R$ R9 r1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
# V/ d, \- v1 O! o
* C; s0 a1 V3 I7 ]0 z% H
  i- J0 w; s* t8 v; ]* T3 P1 [2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
2 p8 G+ L7 w# w' A" p; }
0 z! i0 Z& y9 Y
. Z. O  Q( J1 |* |4 u: e! U3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。' ?# V: R/ z8 J4 [

0 c) l. _7 w) ^' u
& B# J9 G+ ~! [+ l( _2 F+ \7 W5 T7 L4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。% T$ k* w8 Y. M

( w% O1 ^1 X7 n$ Q
, {! ]4 m; s% g在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
* |! Q9 K) Q  r5 ]" P% R# i$ J# i  N

: \. I- s" I6 E3 g6 q' }3 c* P3.x版本CRS/ h3 z. e: h: {  W( Q) H
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
( l  p! G9 {9 H& p# 生成例外排除请求的配置文件0 U, ?5 ^( f- z7 O; Z: P
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
* X7 B- R" c# }) r9 R$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf2 b! |; K5 A) O) O
$ cp rules/*.data /usr/local/nginx/conf
6 o- v# E0 v( L1 O为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
9 ^; \- E3 `6 D
* a& P6 e% A$ o8 o& e& f$ @; t2 c
$ vim /usr/local/nginx/conf/modsec_includes.conf8 d) p6 W8 |4 X# n. Y+ Y

; N* {* p, o8 w; \8 x0 R  ?6 y
[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
7 i6 \4 \2 s' I. z
6 C  Y/ l/ i: M8 V8 N+ ?5 a
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。& l: \2 `' G( D# O& Z; P

$ E7 y: x6 |7 s- a4 m6 n. H/ F- R
配置Nginx支持Modsecurity; x2 ]( c3 r3 C, V( N% R; h, \
启用Modsecurity
0 R( Y6 E0 C! F使用静态模块加载的配置方法# }/ }8 j! H! O+ t& W8 }
在需要启用Modsecurity的主机的location下面加入下面两行即可:
6 |6 ~1 U7 i3 _4 P
/ t0 \& b3 }- d4 B& Z
9 I! x  s' ?% ~" G+ s* {) SModSecurityEnabled on;2 M* ]. k7 S6 B: [0 V; s; ?, N
ModSecurityConfig modsec_includes.conf;
3 E. \8 y0 F1 M* ], @' |0 D修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。. W$ u6 T. F- T' J+ M2 l
, Z& |8 @2 I  p* Y1 C3 e  Z* M- @
: t! Q# S4 M& C
$ vim /usr/local/nginx/conf/nginx.conf
' t' I! z% L9 ]1 d8 o% Q: Y. ~& t3 q+ J

5 S5 h, q  W8 R1 Y  \& Mserver {+ \; B1 V; X# _5 Q
  listen       80;
. D3 L; S6 a' j- W: l  server_name  example.com;5 ^- D+ f. j9 I0 X" n5 e
6 B0 k; I. t1 K! G: B) h0 w

1 Y  }: C( h( |  location / {
' C% }9 S' D7 ?  l4 n    ModSecurityEnabled on;
# u4 w. Z, t5 }! Z: {) Z    ModSecurityConfig modsec_includes.conf;
* c8 i' A/ Z" J0 M    root   html;* a* i8 d: W1 Q% x4 O& v
    index  index.html index.htm;
2 N& w! L0 m9 i3 D$ b  }
, `: I! s( o+ [: j7 t" D- S, S}
3 ^( K/ R. x* x; ?+ F; Z使用动态模块加载的配置方法
2 q8 W4 F1 I" p在需要启用Modsecurity的主机的location下面加入下面两行即可:
1 _6 `' Q; Z' C
9 m# e, @0 M6 m5 E9 f% f8 W! I4 k) g: U/ Y1 d
modsecurity on;
  H* X; C2 M2 dmodsecurity_rules_file modsec_includes.conf;
/ S3 t& f8 A1 ~+ v4 p修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
* d1 O. j4 \6 q2 d: F2 \% _/ \+ E+ u0 Q8 `1 O' R2 Z0 s

, [% I9 X9 e" S$ vim /usr/local/nginx/conf/nginx.conf# I: E9 n2 n( }9 ^% s1 e
' d  d! l" {! f, P' c' X3 @; _
. ]1 F/ r! K; l- L
server {
2 ?! B7 Y/ r8 y! K; ?, [6 a  listen  80;, |) i6 Y5 x8 v& [2 u, T( I+ c
  server_name localhost mike.hi-linux.com;, A5 y; i  c0 m' T' ]
  access_log /var/log/nginx/yourdomain.log;
( P: i# ]( H& w; C, M# X
. Q! ~+ l) S" A; C
# R/ U9 D  H1 H) m  location / {2 |: q# m) z% X" g! r
2 b: I( X3 H0 {$ C3 b9 E5 J' S
6 t8 A) V. Z7 y8 _4 P: R
  modsecurity on;
. n0 j. |1 U, d* m* g  modsecurity_rules_file modsec_includes.conf;
9 l# k. t6 q6 E8 g; n% z  root   html;/ m2 D: p2 P! ?, u7 Z' v
  index  index.html index.htm;
, Q: Y  i& e+ q) d/ v/ n}5 _% k, F8 L" p! ?/ H0 l4 N' |
}* D9 p9 m9 ]( L( r* y% p6 A9 k
验证Nginx配置文件
3 }! t+ v5 G1 f% u9 I$ /usr/local/nginx/sbin/nginx -t
5 ~3 o' J8 w$ @" A& z. I$ S# n/ znginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok* K7 N5 }0 C: D" L, x/ F# u( X+ ^
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
! c7 ]2 @2 U4 F启动Nginx+ `% l2 S/ D4 U) N( e1 c
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
8 _! l* N1 x9 ]7 r' |6 b# [7 u
. C3 }" r8 X3 R8 E

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

7 Y4 {& M: Z) O" P$ D

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-8-26 16:51 , Processed in 0.075958 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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