找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9751|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
( z5 U; Y& p: N* o1 h0 r
( t, Y# Q8 ]5 Q- \" U! G1 ]) j, r- d# T" x+ N$ _. Y6 p
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。/ Z# o5 L! A: a/ V# l3 S

0 ^$ g+ S0 U0 {$ w+ `  U3 L% t9 Z; z$ m) O
什么是ModSecurity
( S" |7 D7 r* m& b; gModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
5 K8 h  Y5 @! k
1 g- s/ ?3 A8 D7 |, M
/ Z. ~' i3 `8 hModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。0 A: T  g: m' C

" t' f* `+ l  I
; j0 c  `' @, ?0 ~% Z- \+ C+ ^9 QModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。+ X# U6 r8 X6 L
0 C9 u  Y+ \8 `% Q% N* H7 [1 x

; z! \' r8 d# P5 Z2 d5 e: i官网: https://www.modsecurity.org/$ b" a: @7 X/ z3 X* ~

, s  v" o/ m7 @/ c/ H
# K# x0 v/ G- y: L7 a  c什么是OWASP CRS
% z5 a0 n4 A& XOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
0 t$ d! ^9 S' j. P. s0 p/ b: {1 E3 w
2 L! H- ~! }) A: f
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。8 x6 f6 G3 M+ r9 K/ t8 @! P

% k  W: f! Q( A0 C1 H0 a
8 J! X8 M- P4 `, ?( Q/ ^: m* lHTTP Protection(HTTP防御)% l+ h# m' N& l( v: K
HTTP协议和本地定义使用的detectsviolations策略。# ~; K3 ?  T; t2 d

& y& i8 ~4 G: E1 m( `1 y2 b7 {3 C7 Z% B3 }
Real-time Blacklist Lookups(实时黑名单查询)
2 i; R; n6 |, o6 c5 k) ~利用第三方IP名单。
& q) x5 d6 o( ^$ t$ q
! i6 }- M' |6 h1 a4 |
: Y! h& g9 `2 lHTTP Denial of Service Protections(HTTP的拒绝服务保护)( r7 R4 h) F( |2 c% F
防御HTTP的洪水攻击和HTTP Dos攻击。% l9 w' {/ v8 T# k+ J9 x( y

" g5 {* Z: _! ?/ D: U% B- i7 r) o, V
Common Web Attacks Protection(常见的Web攻击防护)) `, p3 f9 v: Q9 t* G2 U) [8 l
检测常见的Web应用程序的安全攻击。
  ^5 B1 q) a* Y, U6 I/ t3 p
6 B4 u% {* ^% Y6 ~; O; Z
  X# o, P& ^2 G: b1 QAutomation Detection(自动化检测)$ K% P' k. M$ f/ W7 D5 j6 g: q
检测机器人,爬虫,扫描仪和其他表面恶意活动。
. T2 ]* c; z8 M' w7 u" @+ w! O. v" }2 ?% S: r  w

, b- ^0 k7 ?5 ]$ w; qIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)1 i' \& m7 b, P4 {$ p5 w
检测通过Web应用程序上传的恶意文件。) O9 ?7 \% A9 c9 g( X& i8 O9 h  ~' M

* a2 ^# y4 i6 O% p2 K1 y
- ]0 n3 M4 \/ y" _' NTracking Sensitive Data(跟踪敏感数据)
. |* I% |% F" N& T信用卡通道的使用,并阻止泄漏。; h# ^+ z: J7 N' A

. e) |: A+ `. P3 f, g2 B+ I# I: j: F
Trojan Protection(木马防护)
! V4 P) |$ T( d检测访问木马。
& p9 \# g, x  |2 L# N& p8 c$ k
2 [& A+ y" s) x( J3 V6 M4 H) f& e- [' s" M3 g. R" I  n" e1 Q* K- Y
Identification of Application Defects(应用程序缺陷的鉴定)5 v3 T; w) j+ I8 D( k7 c  O
检测应用程序的错误配置警报。7 \, T1 V+ e# \3 H7 f7 f
; ]- J3 |8 V8 L! }" O" ^: E8 V3 X
; u1 _: N3 l  E# X+ [
Error Detection and Hiding(错误检测和隐藏)
0 d7 W( s4 k5 g) d7 H检测伪装服务器发送错误消息。4 @) @" g& W- k6 @

; M% D% }6 L: I$ P8 P& [; J5 I+ n
3 o1 a4 _+ b0 |2 I, L安装ModSecurity
6 B5 l8 p! N0 a2 a软件基础环境准备2 C# x/ Y; C; m
下载对应软件包
5 u+ f7 `- h, @& }$ cd /root" y3 C5 P6 z3 a7 q8 k
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz') o  M+ Z1 m" A) Q/ [& }; z& A
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz# G& o/ @. t5 V5 \+ b9 V
安装Nginx和ModSecurity依赖包
4 M% k5 A. R  q" o; ^7 KCentos/RHEL: u  ?# D2 S1 @& W0 \

3 c# h4 x0 R5 M$ p* U9 V
: c9 S# u% t. H6 p+ X# b$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
0 Y- o# T% o$ u8 q. dUbuntu/Debian: q) d8 [. T/ }. ~7 f& H' V$ V
2 s1 J7 x, U+ d1 z  s2 Z3 ~
0 H- |3 p" U* H' q0 a, 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- l* ~7 }6 x# }3 c8 }3 e  x
编译安装ModSecurity
& @& K8 _& r3 B$ sNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
% L5 R/ x6 _0 a. L
, }! [, V' _8 V$ L6 I- N5 ~" g, u. C& z
方法一:编译为Nginx静态模块/ Y8 A8 B5 J9 k! l8 D' \* v

1 _/ n% p) Z: e9 X* w: H  C0 Q4 R! q5 d! L, P3 M3 d# D. M
编译为独立模块(modsecurity-2.9.1)( U6 H3 S" W. V; Y* d" P; r- ?* t5 e
$ tar xzvf modsecurity-2.9.1.tar.gz
) Q. s' C: @  r( I# w5 |+ ]+ `. r! C$ cd modsecurity-2.9.1/
6 W) n% f; c9 \+ U: q$ ./autogen.sh
: V2 E# F4 n# }+ t& [$ ./configure --enable-standalone-module --disable-mlogc
% A9 ~; z  T- z2 B0 f5 _6 ^$ make
9 y% n2 N" @( f( y* D& a& s! U编译安装Nginx并添加ModSecurity模块% Z" V& B5 h/ b' s
$ tar xzvf nginx-1.9.2.tar.gz
6 V, d5 k$ ^" M' k$ cd nginx-1.9.2
3 a% C, E/ z5 v5 W$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
; G! N7 X3 [5 h5 o3 V( a+ p- {  ]$ make && make install
1 a! Z3 W4 k+ c: v1 \+ P9 D方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
8 C* i7 ]6 X  m2 s) ~/ ]; T# R% w% T- @% b
7 @& m+ J9 ^! h4 q' c
编译LibModSecurity(modsecurity-3.0)8 m  m. T  A7 G9 ~
$ cd /root6 v* I- d) a+ H3 S3 R2 T
$ git clone https://github.com/SpiderLabs/ModSecurity
& E$ L+ c( H: i0 {$ cd ModSecurity
5 c$ v/ C) @) ?  h* T4 z$ git checkout -b v3/master origin/v3/master+ m* }7 F8 p2 D
$ sh build.sh
4 O2 ?2 D0 r9 C- {/ R! g' j# e$ git submodule init
! g4 D" H& Z# k, I, \3 c2 |- D# D$ git submodule update0 D8 [# t5 F: f+ o/ h
$ ./configure, e1 G; S, \* F4 c2 P
$ make) \  d$ g, S5 L
$ make install
7 Z6 k, n/ v+ k0 ]2 ALibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
! q8 [  B# c2 ~3 {9 {( J2 H5 t" c5 A3 }

8 W5 m( t$ t9 `$ ls /usr/local/modsecurity/lib
& ]# f7 `  B. W% a* M) ]libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
# L3 P, ^- H* ?编译安装Nginx并添加ModSecurity-Nginx Connector模块
1 ]6 E8 B) o/ @5 B使用ModSecurity-Nginx模块来连接LibModSecurity
$ r) [2 ~4 e% a6 h
. `5 m, I* j6 S6 V) |3 f/ h- @4 |* U! t: G: `. e
$ cd /root0 m* @/ p* ?" ]* _
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
4 U' |/ W% E# Z$ tar xzvf nginx-1.9.2.tar.gz6 L2 Q0 z; X5 ^6 ?1 O
$ cd nginx-1.9.2
4 n6 d1 Q6 j6 @/ A$ ./configure --add-module=/root/modsecurity-nginx
' B+ y0 m( ]! o3 ^- ?$ make
$ t6 p! j0 P5 J% V. {$ make && make install; U' ^+ i% a/ ]( {* C- F7 h
添加OWASP规则
6 F9 Y3 Y% m: z# c$ A* p$ U! zModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。7 ~0 q) q5 I- t6 l/ \0 F% m/ O
8 F4 k* u8 s; V2 f/ {3 p/ N
% p; o! g  Q3 j
下载OWASP规则并生成配置文件( m. d2 f3 `/ k& I* B; L2 y
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git9 h1 G& {; T, O2 }
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
, ^% ?' E4 y. j. ^8 W9 W$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
9 Z! g6 A% N# \. Z% W$ cp crs-setup.conf.example  crs-setup.conf; N+ O8 c& j9 e- \1 p
配置OWASP规则1 q: J4 z) h% s  i" A
编辑crs-setup.conf文件/ S7 S- r6 `2 B& P1 l# j

, u  m" \  o2 j( V% p$ x" E
+ i! T/ U8 x- B5 g7 X1 b6 m4 {# R$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
* j1 j0 M. G% A- u7 t) U) B$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
  I& s2 `4 L2 ^2 f% g! T$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf6 l# X2 m! y6 G3 W
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf" z) Y# m7 Z" O# I
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。% V* g: D' q* f# r
4 n- a' e. V; f1 Y) r8 K, z

. `8 m( Z" K5 Y) X  R启用ModSecurity模块和CRS规则
5 e8 |* H2 }0 A% `6 R复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
6 T2 ]; T$ W9 \* A5 a
; y" C( G2 p$ k2 I$ {' c& b3 _6 v. i; t8 x! Y4 g3 F
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。" |8 O5 l7 L0 H# Z2 G1 R
% k( ^5 g2 J  f, U) o- e

0 a% \6 ]" B, Q/ j9 N$ c$ cd /root/modsecurity-2.9.1/" S- T- e% ?9 P8 P0 X6 q4 ]
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  + c! i) v. P* B; o$ o" ]# e: J
$ cp unicode.mapping  /usr/local/nginx/conf/( k$ p6 L8 p6 P
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
6 V& s/ \# }; a4 Y8 ~
' r+ {1 M: F; N- X$ ?4 F; i( _% S4 i+ X; q- ^3 M% u
$ vim /usr/local/nginx/conf/modsecurity.conf
& u, E+ K. c" E1 G- }8 mSecRuleEngine On
' V; @/ R' J2 r# U/ [ModSecurity中几个常用配置说明:
7 r) o3 }5 B- P3 k2 |& n! r8 s. X6 \
6 Q8 B" d  U0 x% c- a& V* d
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。8 T5 ?2 t& s5 k3 v) r
, q$ D9 U: n" I: Z
" q) p' t$ \: G# v: [
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。7 j8 p8 z7 [. i
5 W% N2 Z5 a$ S9 S5 a+ k- s* p6 R2 _
/ K% D9 u* E0 J8 u* t* m, E0 Q. i8 I
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。. H. e) c- _4 h0 @6 q3 ~

' g8 u) [2 p/ R' W) d8 H* g' k! _: ?
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
8 h, ^4 O7 x* N* @* k6 J* ^, \# Y& w, O* c0 ?
" z" C) W) ~# Z
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
$ e% A/ _2 Z! K6 g% a2 c' m3 E& a* I# Z( t9 {; ?7 h4 Q

+ Q9 t( X. S( q& a  O+ A3.x版本CRS
" f! I, u3 \6 R8 M$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
/ K% y/ m6 c2 |9 u# 生成例外排除请求的配置文件0 e& @7 K8 b1 ^  N! \
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf1 f+ @4 ^3 Q& ?# A
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
$ d  b! \: D5 U6 s: X' O7 b$ cp rules/*.data /usr/local/nginx/conf1 A) S# [, e8 h; _6 v
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
9 m; f" M/ r! k0 H7 @0 I/ R, ]% P  I
6 h9 X% H( I8 f  v9 T
$ vim /usr/local/nginx/conf/modsec_includes.conf
6 N5 V  H% s4 m" \& S5 @8 A+ [0 @, j, f) W
[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
. P3 \5 n! s- K3 s! q' Q+ F

* y# D9 |1 P; I- O注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。) t7 K  N5 U  I& {' l

2 R1 l  A8 V5 ]0 D- H5 B0 [, X( W
配置Nginx支持Modsecurity9 c0 w. m$ N7 ?& }) _
启用Modsecurity' e: j% D# ]$ E8 r9 y: h! P
使用静态模块加载的配置方法
  m8 |3 J- t, |8 B在需要启用Modsecurity的主机的location下面加入下面两行即可:- I0 q1 G& c1 R3 K/ z3 ]: w: h

$ X; W' K1 G) X3 Z2 V
! U" `0 R7 z) @/ DModSecurityEnabled on;, }5 |4 u4 F# O8 S+ }
ModSecurityConfig modsec_includes.conf;
- X# O: s, h( X3 H" ]修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
! B9 J* [: M2 {, R5 E  s9 q
) o# }  F/ [) `# ~7 x
  f6 b' @3 a+ Q, E% G2 e& R$ vim /usr/local/nginx/conf/nginx.conf" E9 ]$ V4 `( J  P4 {/ y- v

, b" E! j+ A7 `/ A: T  s* @! P8 i7 E: s" o1 z( q2 V
server {
5 S3 A4 C' J3 Q* O  listen       80;
" ~) w5 u0 j+ M9 {1 |) _  server_name  example.com;
9 Q( ^% C9 E3 h: @# w# B) x8 @# t" s% S$ ~& k

! V' s5 v. X: i  location / {. v! a7 ~7 k- b5 w& x
    ModSecurityEnabled on;: }* x+ I; K' c( K; @1 D) ~" U6 c7 \
    ModSecurityConfig modsec_includes.conf;' t/ D, g; P3 R& s: h
    root   html;) B9 M0 x6 U; l+ n* G
    index  index.html index.htm;6 [/ M) @6 O! j1 t' b5 d
  }
* l5 x" g, S2 X7 P1 C, \$ W  u}' Z  p  s0 j3 ~: m$ [- }5 p- v
使用动态模块加载的配置方法
. H- o, c, }1 j9 [在需要启用Modsecurity的主机的location下面加入下面两行即可:: k5 H9 O2 M0 F1 f

, G  S# N' a) m% h+ z! H, N  \6 Y: C* p9 ^
modsecurity on;. n8 |, \5 |8 U0 B) z3 g1 P( }
modsecurity_rules_file modsec_includes.conf;
% a; g6 B4 |# q" I% H1 u7 Q修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。% O! Q$ F) j$ @+ C% \* U
7 @. R2 n: D; Q. B* g
! m* L5 @! X+ P: o# m# `
$ vim /usr/local/nginx/conf/nginx.conf; {: ~$ _; S: o3 n$ h8 c( Z

/ t" R9 _; y! j+ E
3 e, h& `. O( \4 N8 lserver {2 R9 \: Z3 M' j8 Y, F
  listen  80;
/ X# x) V% d( k$ [1 Y6 ^- {  server_name localhost mike.hi-linux.com;
2 E1 v$ u/ w4 T  access_log /var/log/nginx/yourdomain.log;
- Q4 J- F: F, s/ X3 B  c& I
6 M5 c, c' i' p5 \6 d+ D/ O( q+ h" q; Y  s7 U: g5 y% [- R
  location / {
* p/ o: e' ]5 i- y
6 A  y1 D# }+ `: W- r
& f+ N/ o3 u: J0 L/ r  modsecurity on;4 W4 ?0 K5 G* d& d1 l& d
  modsecurity_rules_file modsec_includes.conf;1 D. M: U9 C% r$ a& v
  root   html;
5 K1 e7 X: Y5 B( U  index  index.html index.htm;
9 m7 Z" u" L$ p* s}  V1 m$ `  {' b( H
}8 r( q+ m: y( z  j4 g9 R
验证Nginx配置文件
4 M) `" t# ~! d6 |: }; b0 ?$ /usr/local/nginx/sbin/nginx -t5 m9 z$ Q/ M& n5 j% b6 Q3 G* v
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok) c0 |* u* m$ \, W
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful. S" x/ e' q$ y  y* R
启动Nginx
6 H: ~0 D' k) o# U) j- e  H' Q$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf/ ]# Z6 m" g7 v  K+ S9 V

& ~  Q- @7 ^* H! M8 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能够战胜更多复杂/未知/混淆的攻击模式。


- m  h6 d6 {9 j6 u, Q7 H, i

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2025-12-16 20:54 , Processed in 0.059821 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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