找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 8825|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。; a/ R  r# j+ i6 n$ @
. J5 }0 d5 k: O6 y' K. w

( g6 W# \) f+ E7 [2 z2 S# h1 N7 U在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。  ~1 F( f, x7 `+ \
  a7 v& S/ u: n2 l4 \* J+ q" F4 K

; f: c; N1 f4 |5 [& y什么是ModSecurity0 v! a% V8 `) D* ]( Y8 _
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
; t8 g- O! A# X" G
1 K( H& X" F4 u) U) ~; H% g) j! B5 n0 F) s8 p4 H: M8 A
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。( {9 K' J& @9 O7 K$ I" H
- J% x6 L# S7 r: r
! E1 m! M" ~: g- G" |: `0 y7 M
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。3 R3 M4 F' Y6 e: O$ L, C
$ X) R4 U6 g, q' K1 V
0 d" z% b+ ^" |  n3 `2 E
官网: https://www.modsecurity.org/# i4 Z5 N; v% }; G; ~3 r( y* y% ?

  Y  T7 J5 j" _! ]$ k" m
3 f  K" ~- E, S5 \: {8 T( q4 \什么是OWASP CRS. S6 O/ T0 ^+ a" p. ?/ w
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。; U( E7 P# S3 H
, f. A; _1 b, A$ a2 j6 ?

; f; I1 M3 G9 X. T0 I& l& gModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。0 z% m5 ]% w$ C! l
7 M  a9 _/ r- G- m
6 T4 g( _7 d3 Q+ ~
HTTP Protection(HTTP防御)
& I- o/ ^6 _& {+ l' e# V$ Q+ yHTTP协议和本地定义使用的detectsviolations策略。- n! P6 X$ ]+ n' e

& G0 d& ]* x' E6 O
; p/ m/ b) d' N% t8 a  S: e3 j& i7 l4 AReal-time Blacklist Lookups(实时黑名单查询)* x6 u2 `2 n: ^/ Z4 |) j9 n  G  Q
利用第三方IP名单。
& T* V" c% o8 K9 k4 c: i
7 f, y9 B3 w, L7 w' X
0 O' Y: P3 o! `HTTP Denial of Service Protections(HTTP的拒绝服务保护)% R2 O4 o$ T% E( y! E2 ?
防御HTTP的洪水攻击和HTTP Dos攻击。
: O: I  ?6 u0 q1 R  I" Y: \% U- r9 S* P' t9 j+ a

* U% ?+ v2 N6 {* j0 qCommon Web Attacks Protection(常见的Web攻击防护)+ [& c, n5 H6 x7 J! l6 J1 Z- H
检测常见的Web应用程序的安全攻击。$ e9 F4 J" @4 {- z% g

1 E  R7 P- v2 D( M) p+ Y% m' s" _9 t1 I1 ]% C
Automation Detection(自动化检测)
9 V( J% s5 y, H4 f& f% Q检测机器人,爬虫,扫描仪和其他表面恶意活动。
5 i+ N( D; d0 k4 C1 |( a8 r& [! u$ b1 _$ z1 }% w
9 ^, ]: M7 E: _; D) _
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)3 s, Q6 G/ C* _4 l& p5 X! p% u
检测通过Web应用程序上传的恶意文件。2 o6 X2 h1 y' D+ C% _$ n
$ W5 `2 D; n9 U3 |
1 w7 V) k& }, [; n0 Z  K
Tracking Sensitive Data(跟踪敏感数据)7 p- D6 L. Q! u5 i" V. x" w& f
信用卡通道的使用,并阻止泄漏。
5 M+ A, l" D6 N
$ |7 \9 g% Y# b# C: X" _$ W. ^# e! {# h9 X) J: x1 w
Trojan Protection(木马防护)
! W6 R7 _9 F9 H7 ?检测访问木马。- K' }5 h+ q( Z
! ?' z: }* D8 A7 p* p6 [
* @4 P+ N' X, s: z4 L9 u
Identification of Application Defects(应用程序缺陷的鉴定)
$ c: K2 k# y- `8 Z7 |) K检测应用程序的错误配置警报。
: {2 F% y9 e, @; F/ U$ F
/ _& t' n5 ~0 v! h- g' i) n
$ x: ?# ]; h: p) @( aError Detection and Hiding(错误检测和隐藏)/ t  v3 C& i1 [" k+ o1 K3 w* M
检测伪装服务器发送错误消息。
$ V) @- `1 A% C2 X9 B
( D. Y0 ^! P5 P% b: }6 k8 j1 F4 [$ s5 J
安装ModSecurity" ~$ g% t2 }( J3 ?4 a0 `; l
软件基础环境准备
0 [* @3 Q5 E2 M5 \& @. M, }: T1 X下载对应软件包
" G& O2 c7 B( y* j2 \6 p0 Z$ cd /root
2 t% r% e( s' M: _. A: Q6 I$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'" O) P" C% X# 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
6 p) S( n* e$ a; z- f安装Nginx和ModSecurity依赖包
, h7 l' A# F& x( v. ]) A( u- w2 TCentos/RHEL
6 Z+ ^, V. _; b3 {8 Y# r4 Y( n6 o
" H9 K5 v7 Q; o  r3 F# }
6 {! Y# j+ `! i$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
+ k% \4 j! Q: o$ V1 SUbuntu/Debian
' h& p$ P& O! _# d% f/ R9 S; d9 N2 }& i/ J# g
, W4 A1 z9 F; w# a' Y7 m& j# h
$ 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
5 y- r* y2 c$ d编译安装ModSecurity
" a5 n' D4 Y/ E8 \: VNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。% P6 _! H4 O% d# ], N+ s8 s
' B  V8 z/ i8 R

1 C: {$ S4 ]0 {. N方法一:编译为Nginx静态模块
+ J2 H  w; Q, I: u7 s" ?
* f! P! a; `/ H2 F3 @
5 D, ]6 f; A. F* G4 Q  c6 r; L9 N编译为独立模块(modsecurity-2.9.1)) \) v7 f; O/ S4 |/ ?. }: R+ f
$ tar xzvf modsecurity-2.9.1.tar.gz
! ~+ _. j- A% L' W$ cd modsecurity-2.9.1/; Q5 g: A4 I% g; G- w/ x
$ ./autogen.sh$ p- s1 f( l0 a7 }
$ ./configure --enable-standalone-module --disable-mlogc
; c7 `) Z# S, E8 y, L% f$ make6 E) q/ s1 p7 g5 v5 N, m' d2 H3 f, @
编译安装Nginx并添加ModSecurity模块
( R% y) i- ~& b, ?$ tar xzvf nginx-1.9.2.tar.gz' a6 z" R) J5 @; C3 g
$ cd nginx-1.9.2+ U- p2 q4 j5 R3 B  v- r
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
% W/ Q3 M5 C) t; E$ make && make install4 }) h2 K' Q- o) ^- `
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
0 D( B- k2 Q  o# R+ M- k; v
' k6 @; D5 e/ B+ W
* p" Y$ V4 i; s. ]编译LibModSecurity(modsecurity-3.0), ]5 [# z' P3 t7 ^4 a
$ cd /root
, [* M% a, ]2 |$ git clone https://github.com/SpiderLabs/ModSecurity* s4 T$ u% R) v
$ cd ModSecurity
7 m/ d5 g- M5 h, `6 U4 R$ git checkout -b v3/master origin/v3/master
# Q$ c, e% A- z$ sh build.sh" q3 ^% |2 q7 t; S  K
$ git submodule init
" Y/ E1 a) V0 E/ J$ git submodule update. A) }- ~, }5 I3 w$ M& P
$ ./configure
" G& ?# k6 m/ v$ make7 l6 a. w& O, R- u$ U
$ make install# ~1 p8 _4 X9 y" [& }
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。. I# U2 K0 E1 y* q* e7 X1 A

" Y+ G# E  ~9 ~  j3 N; v, p
7 d- w; f. ?3 F/ L: ^( G* L$ ls /usr/local/modsecurity/lib
6 D% J$ m* X4 A  ]5 o; M: alibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.03 g% j" j- Y- e
编译安装Nginx并添加ModSecurity-Nginx Connector模块, H* L" [7 Y% B6 G5 i
使用ModSecurity-Nginx模块来连接LibModSecurity
1 s: [2 u( g( R- D' s1 T- j% K; X, W7 ~' ^* {& P" ]

" a, X5 m( i* }$ cd /root! f- L4 p+ p9 R3 b3 r* n5 U; \
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
  O1 `! q) t6 G$ tar xzvf nginx-1.9.2.tar.gz* ?9 @" y* I* C- ]4 c
$ cd nginx-1.9.2
* ^( D5 U0 ~- ?8 q0 P$ ./configure --add-module=/root/modsecurity-nginx% w- N: s- z' f, Q! v2 T
$ make
$ V% F4 w/ O( f- c$ make && make install
  i8 S0 y* p/ }( x6 g. J添加OWASP规则: S; m: n1 B% U
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。9 v9 M/ W) |# [( K& b: P

0 R# C4 v. I9 L
1 |6 a) M2 _- v" o2 I! Z6 _/ X下载OWASP规则并生成配置文件
) @$ O. V2 `( U) @) K. ~, |5 J$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git, z4 x* D- P3 @
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
) n# ^% F$ i; G: X- G- ?2 ]- X/ v/ ?/ h$ ~$ cd /usr/local/nginx/conf/owasp-modsecurity-crs7 c) a- B2 k8 G! U" x2 h+ }
$ cp crs-setup.conf.example  crs-setup.conf
+ M* y, c, E! |配置OWASP规则
* M# _5 X+ c! O  u' Q编辑crs-setup.conf文件
9 c+ Y" _4 P8 r7 W  q9 I/ ]( K$ W0 Q1 y4 J% k- V# w/ {
2 l; w" J$ t' R* ~& S
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
" Z% T, W4 A, [0 I8 b4 p) m$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
; {( r: f7 Q0 {, K. h1 H3 n$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
/ y. ^" q% o3 t  \$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
1 B* w( D/ D, }- K4 F默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。" |! o* M* B9 R4 O

- F8 ^6 j' `) ~, ?9 I0 i
/ P3 _9 ~! M/ M7 [: {启用ModSecurity模块和CRS规则
# r& N# v. [' Y: G- i1 i: F" f: ^9 ?复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
0 _- C8 f  R; E  {$ `/ E$ z8 W2 G" h, \' \- r* @% p' R

" g3 L  S) A. F! j& u9 ~modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。3 ~! u" I+ K* a8 d' |

" y2 h/ w. P! \1 J3 Q) s7 V
. s2 u) W' i$ B* H* [& G! C$ cd /root/modsecurity-2.9.1/
7 p6 H8 f$ ~& k* K$ ]1 H; w- z! N$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
, d% |% D+ z# E/ q. P- F$ cp unicode.mapping  /usr/local/nginx/conf/
) ~6 ^; b- ]+ Y: a9 l# O$ M+ A将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。: n3 q2 s" U* b5 C6 f3 @3 @

7 t# `" `- i' L- `8 A! D9 c5 f+ q* U% X  K! ]# s
$ vim /usr/local/nginx/conf/modsecurity.conf
  z+ F1 P$ ~& }  k9 i! r* cSecRuleEngine On
& X1 c2 D% d: h  bModSecurity中几个常用配置说明:3 q4 N3 _0 N+ y! x* g: p! g0 Y

  }+ w: a9 f2 w* L
& ^2 H# n. H- o6 y$ X6 ?9 X1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
+ @" k; \% ]% ?$ O) V, u: w; d# O2 R4 ~8 i0 z# D) v0 P

) o! W. g5 y2 A  V2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。+ Z! W5 ~2 q  D: r" B

/ [2 e2 _2 h; b6 W3 w. F3 _! ^
2 o& Q0 q; v/ [- h3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。' S3 P/ D; M4 y0 R/ \! |9 M
5 T$ K4 Q) E" ?* N7 B
, p; B. _" l! H9 {' W8 ]
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。& g6 L- P2 R: {! E" |/ F
) y" r$ Z, Y5 P
( U7 h3 E; E+ \* c8 f  S$ x
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。$ j6 W. N4 _# W9 v: s! V8 {- P

" l7 b* ^* |/ H7 m6 ]3 J$ V# |  n; t* s: P
3.x版本CRS
1 E- u1 }/ R# n2 t$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
* m1 \. I+ ?9 Y* ?! O0 z# 生成例外排除请求的配置文件7 j3 F$ y, f8 C( |9 T/ I
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf" {6 b8 Q6 G1 z
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf/ _: q7 ^' M4 S) i9 @, H& `4 |
$ cp rules/*.data /usr/local/nginx/conf# n: {/ J6 _9 Y" S" D1 B4 N; @' T4 i
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。8 s( ^& z  i& W6 |
. C5 r/ K- |8 I2 n

' t7 o! I$ f( o" O9 L$ vim /usr/local/nginx/conf/modsec_includes.conf
5 Y8 x2 F4 `' r& F3 J* C' @/ X; r9 P* _/ g$ I  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

# O( K3 M. ^* F; g2 |6 Y/ C/ w
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
" L- k6 g" B. S4 E
& A% Q  {; @: C# x1 i- q
( j" _5 V$ A. |. Z- @9 P2 v配置Nginx支持Modsecurity0 c9 ?& j+ Q; B# {$ B
启用Modsecurity. {* H) }1 D) [8 d
使用静态模块加载的配置方法
7 C3 F. _) b: Z9 f; M在需要启用Modsecurity的主机的location下面加入下面两行即可:
2 ?- I; w) Z% c* L
1 M2 M# b' [9 Z# H) t$ o
9 i1 F' d$ P: |! L8 ?1 tModSecurityEnabled on;+ ?1 ^9 ?1 q8 Y5 q. G
ModSecurityConfig modsec_includes.conf;0 C9 P+ P9 ]: o/ p" a
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
4 t2 O$ b! |) P) u
+ \5 m" ^3 T0 m* Y1 J% X9 C% L& n2 O# |) \( Z( m# ~) ]2 F
$ vim /usr/local/nginx/conf/nginx.conf. i. F& E1 _2 H" z" z# l
+ a  B; G: l* Q6 F
; {: {8 e+ o! _" k" X
server {
; ?7 I' I, X2 B( F" T  e: }  listen       80;
% N% b' R( }& S9 K, X  server_name  example.com;
! S% g$ R* Y% V( x! l' Y1 o/ M4 N- [- j0 Q) g5 l+ U* }" \% Q

7 w& Y! b! c: R- b% I5 k  location / {. ~, {; L1 N' \# r/ P- X
    ModSecurityEnabled on;
( V4 D: `# U4 A' O. [( K    ModSecurityConfig modsec_includes.conf;
' @1 r5 [" L$ Z5 X* p; D$ T; b    root   html;
, P6 G0 y& W4 E  W# G2 g    index  index.html index.htm;
* Z- f9 t) w, H- S, F  }
9 C7 O4 I# O& u/ U4 {5 C}/ `2 C/ x( j7 Q$ o) B
使用动态模块加载的配置方法
$ K6 d  ]9 O' |. r在需要启用Modsecurity的主机的location下面加入下面两行即可:
! V. l1 p& g# |9 s/ l  J# A0 K! `) H7 V/ v0 X- H

$ W! `5 b" {6 a2 T! {4 Vmodsecurity on;! K+ C9 |& S; }. k/ U' H& \
modsecurity_rules_file modsec_includes.conf;
: B5 G' b; G0 K2 O4 I/ U: u( |  q% X修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。0 k3 z) `8 ^0 g3 p: u% O8 E
8 d3 w6 M$ P# R' E( L
' O* ~/ i8 y8 e# m: T9 N2 |
$ vim /usr/local/nginx/conf/nginx.conf3 X0 q1 I' a+ L! {

) \& \; B; M* T% z; a: X1 F5 |8 g6 D/ D; ?1 U1 Z' |
server {
: @+ M( _3 U3 m2 e/ K  listen  80;
5 }: B/ E# [5 D  server_name localhost mike.hi-linux.com;. T) s7 T% }! _' n8 `& ^1 L
  access_log /var/log/nginx/yourdomain.log;0 x& h& ?9 P8 a. m
3 A- o- g$ l/ u8 G$ s0 B2 i3 H

% e- d! O  J! \' `+ `% o  location / {/ g  v0 U3 f) Z" L  m1 m' w

* }( T; F7 i" ~2 f. m$ E5 ~; A8 U9 L$ L6 z8 \7 A
  modsecurity on;. ]- R& U9 t, [" E
  modsecurity_rules_file modsec_includes.conf;! W0 y- d" F9 N! ?! Z8 U1 w
  root   html;
1 y0 X4 f( E/ Q- G* b/ `/ U" ]  index  index.html index.htm;
, b4 y1 q* @6 s8 y2 s9 y}
  c8 o3 y0 i7 V7 P! O6 U}9 r7 t! k! b+ D3 r
验证Nginx配置文件, ~' B( J! S0 |0 u
$ /usr/local/nginx/sbin/nginx -t0 x) y5 `$ L5 ~' x/ ~* q' G
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok+ D6 x  ]+ p, c5 C) d
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful" z0 k# P3 H; s7 j  ~1 x
启动Nginx
3 ]. n/ e+ Y$ {; T3 Q- _$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf$ p: s  e! e# p7 a% ?

: f4 |- \- N  s' F8 F% h9 X$ y8 V

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

" S, [2 Q6 y! X

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2025-7-5 11:17 , Processed in 0.078606 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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