找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 6473|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
: J) v5 n$ \0 `8 `0 Y
: V, H8 ~; r6 L" Y- z$ n$ x1 P6 z) e" S
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
3 a  m5 N* \0 F0 L
  h) ~% f& x3 N- Q- j
, |; t% a( G+ }8 ^- b什么是ModSecurity9 P4 z* u( {3 c* t" g) T
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
" F$ w4 k( P7 z; ~) r# Y7 u- s
) p$ F+ t3 m4 O4 D, ^; Y- j; g6 V* a  w/ ]+ k
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
, D& G; R# H, O6 k) \6 A2 x% f4 |1 b( t3 S9 A$ M
& e+ t$ t6 T: m8 h7 `) X
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
! E& q7 M+ V6 q4 j6 v. e  U/ a. _

( I& @) p! \$ n" t" K官网: https://www.modsecurity.org/4 k) `+ {$ e4 v5 P" C

7 F: X0 A! e8 _6 w) o1 G+ N. E9 k, n- s) x" B! \( R( V
什么是OWASP CRS
2 O* l1 L5 {4 z/ w& D' S* z' }  P# @/ hOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。: M1 \* W$ K5 ~3 |# ^

9 Q- o" j. z4 e3 r0 r* \2 H( A1 M$ e8 Q5 S7 L4 j" S
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
2 N' i& m. Q" f; q, y- {! b
$ |+ v+ q) U  u' c7 o4 f& f8 B! d: k8 I
HTTP Protection(HTTP防御)3 i) Q  X# Z5 g+ X3 k, |& k7 C7 ]
HTTP协议和本地定义使用的detectsviolations策略。
9 d  d$ N- x6 g, g# U# T  n# {* p5 w6 d
! B( \# z* P6 X9 y
Real-time Blacklist Lookups(实时黑名单查询)
, L6 T: R; ^; V. i. D利用第三方IP名单。! N$ N8 g6 B: _' o- u2 K
! F7 t6 m% e& d
/ u* q( ~) r; E" H- I
HTTP Denial of Service Protections(HTTP的拒绝服务保护)2 m( l+ F. X  @# E9 ]
防御HTTP的洪水攻击和HTTP Dos攻击。
* }  H& s& K8 ^+ G5 C
0 ~: V/ D( n5 _1 i, S! C7 f& I0 s: X0 m5 k
Common Web Attacks Protection(常见的Web攻击防护)
: |6 ~( ]. W$ M% z: ]3 Z5 N& J检测常见的Web应用程序的安全攻击。
' N) K. w5 Y0 [0 v* C2 Z! s3 t, k1 R% r8 I1 [
* N% V7 @' S& d2 w* W$ W2 |5 I
Automation Detection(自动化检测)
$ v  g. {/ ?- R) k% ?: B检测机器人,爬虫,扫描仪和其他表面恶意活动。
/ T  t5 o* P* L4 p7 g9 V9 [6 ^" w4 z( [9 s) H6 x# n
. J4 N* x" X% g
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
3 z6 N, @- Y, K% S检测通过Web应用程序上传的恶意文件。
/ _. I$ i! O$ h8 T
3 \/ T" a: @7 P, ]$ E+ u5 s3 I1 t* k3 i, ~- J+ X. U: r7 I
Tracking Sensitive Data(跟踪敏感数据)0 O: r/ j8 |8 w8 r1 c5 b
信用卡通道的使用,并阻止泄漏。
6 V! M8 c9 k$ R$ q1 A0 z
/ S& Y) `3 F( R: u, J0 H# D3 g* p8 k. M  V- g
Trojan Protection(木马防护)0 T2 t8 a" U: ~4 o5 d3 M2 y
检测访问木马。, K- \9 @- r. E2 W

1 d& p1 T) D2 q7 Q
6 Z; J" m- ~9 jIdentification of Application Defects(应用程序缺陷的鉴定)- n4 b9 ~6 W7 P* R" F
检测应用程序的错误配置警报。
) u& Q$ [3 c  ]6 V% \( x. M
8 j' D2 a( I% {5 h& f2 A+ W
6 g$ Z6 p. ?8 g# m$ k4 {4 {Error Detection and Hiding(错误检测和隐藏)) B2 l7 @8 o0 L! A6 o. M
检测伪装服务器发送错误消息。
! k/ h9 d- W, Q1 c7 x8 P+ z! W; r( _$ J' C
! G- r: W' i. n' H" f& s5 [; f4 H
安装ModSecurity1 x- |0 x7 b) m- u* |
软件基础环境准备
* }* d0 P' n7 h9 V; Q7 q, \( y9 w下载对应软件包
/ S3 K! D) T$ Y. P; ^- }( s$ c$ cd /root
. X0 `9 U1 ~1 @$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
0 z) q) N' Z) E$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
) p6 \7 {" }; t) C/ ]# p安装Nginx和ModSecurity依赖包* Q0 ^; K2 G2 R- d5 I$ x, C
Centos/RHEL
6 P, e4 ^  p% q2 _* z; ^) `7 G  O# v# y+ f# t5 x

" ?7 @/ Y8 y1 Z: Q& n$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel" P1 X5 c, c+ t2 q: D
Ubuntu/Debian
8 a1 A& X' ?8 b; R! v: B$ H( v+ M( p7 x; E0 W
# [3 ?) H) r: s9 i% Z5 V
$ 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
% I: x& A! n9 J7 g: j4 O编译安装ModSecurity
; J  e5 [" U* g& |( v. t+ YNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
" i. a7 a  k, U! k# X5 ?; ]7 \9 g0 W, q* g; c2 R# J7 H
# S" T/ d' f# J8 N( P- |# t
方法一:编译为Nginx静态模块
( N! X6 i( h7 q& I+ X4 o5 S. C& P6 i8 y
: J3 j9 c( ?; I" q3 v3 W' a
编译为独立模块(modsecurity-2.9.1)
! A6 \  Q* C* @/ G4 X3 p$ tar xzvf modsecurity-2.9.1.tar.gz
& N" Y/ X( G3 n; z! C$ cd modsecurity-2.9.1/
" @! B3 V: Z/ b. o) x$ ./autogen.sh
  G, x' w3 t  A/ U$ ./configure --enable-standalone-module --disable-mlogc
; w. j3 E3 O. Z- i* d$ make
* F. I: f2 w( A, P编译安装Nginx并添加ModSecurity模块* A3 r: Z' Z- ~
$ tar xzvf nginx-1.9.2.tar.gz
% ^6 A4 Q! s$ v! S+ v: {$ {4 x$ cd nginx-1.9.2& C5 t- J5 s( U9 b4 o4 Q4 `
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/7 ^+ X8 f- R1 o$ ?/ ]
$ make && make install
2 `) @! |, N2 s' ?方法二:编译通过ModSecurity-Nginx Connector加载的动态模块' H, m; Q' [2 H* L) S& H
: e% s4 z0 U" V$ M( a; D" w. K
8 W4 E0 w7 e1 f; T4 N# I
编译LibModSecurity(modsecurity-3.0)
6 c, i, D# f+ a" ?! z" A! ^4 c$ cd /root, J* Q+ n2 c" D# ]7 u
$ git clone https://github.com/SpiderLabs/ModSecurity
4 b- ^5 u! y8 ?/ k- l1 H/ v% s$ cd ModSecurity# t( ^, R: m, S+ M; j  J
$ git checkout -b v3/master origin/v3/master# ^% c6 }$ [: U% ?7 K& i# F
$ sh build.sh
% Q2 {3 r" e- R0 H! J6 n$ git submodule init
8 [* j1 m( v0 N' w, m$ git submodule update' L2 Z7 B- f; B( [5 d1 J9 k4 r2 C
$ ./configure5 {( ?% f2 v! F0 E9 x
$ make
. h) V  `4 O" I; U+ y+ d0 E+ {$ make install
+ V, x4 W% @) E2 MLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。# W, e. a( v# P5 v: w) I
7 r2 R& n1 r1 n' e4 N% G

' _7 b( V5 [5 Q; @0 b$ ls /usr/local/modsecurity/lib  y0 o7 {/ w. J6 A
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0- _6 W! m6 D# `
编译安装Nginx并添加ModSecurity-Nginx Connector模块
9 |& |/ Y& A# f, I使用ModSecurity-Nginx模块来连接LibModSecurity
; A3 c  d( \6 X: E
2 A" s4 [* e/ n" t7 e
( o! A6 L1 }. t7 C4 m/ L; Z$ cd /root# x) S+ M8 \5 o
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
" k% o0 }+ a2 W- z3 C$ tar xzvf nginx-1.9.2.tar.gz
' G5 r4 G7 d' z4 |4 x6 g$ cd nginx-1.9.2, S4 ?# n& m5 w0 m' J7 y/ \
$ ./configure --add-module=/root/modsecurity-nginx
' V  p( H/ P: S2 A+ ?$ make6 {1 I+ i8 R! O0 q% c8 H
$ make && make install
2 T" L8 T% K0 Y( e/ Y0 H* S5 d7 l$ h添加OWASP规则4 \7 U8 e5 |, n
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。. y! d3 B3 M7 _6 c
6 U( u) k8 ]+ Z& Y
3 q1 r, Y4 K" A/ x( [( @2 ^9 {
下载OWASP规则并生成配置文件. _0 R4 q' Z: {# {& F
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
+ G. j2 f4 Y. U' Q! C5 s$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/" q) V+ F& L! p' V6 A6 u
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs% ^3 |$ l8 j4 c. ]. F3 R1 z
$ cp crs-setup.conf.example  crs-setup.conf) O3 i/ w7 ~- `
配置OWASP规则
/ u# P. {! {; [1 v4 B编辑crs-setup.conf文件1 J5 z& K8 F4 D$ @% T4 X
$ m0 r8 z2 y* P! _4 g. z

. d. z6 Z- v) s2 B$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf5 f  T  m. T& d8 k3 L! A
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
5 J6 a' ]2 ]1 X1 w$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
8 V1 [2 r* Z7 [" ]! A$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf/ _8 M  l( i- c# z- o6 @6 n8 q. F
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。  x% q3 T! P4 O, {) o
' S  i5 x4 ?( ~3 z4 \, ]0 ?

7 f+ `% A- r  v) j' q启用ModSecurity模块和CRS规则
0 d( G1 T4 d! ]5 g复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
" X1 }5 p# s! ~
! H: {, e8 s# I; S) z$ \% U$ L8 {7 y8 Z/ c
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。9 D' N* c9 f  W. c2 O% e( ^& P

% P+ o5 S1 Q+ @: p  `( t2 F" q' ^% B6 j# h. z" K# ]2 Y
$ cd /root/modsecurity-2.9.1/- s" G) M6 B" c1 |" L* S
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  ' }& `$ w* n5 O, t+ L4 [3 A8 ]
$ cp unicode.mapping  /usr/local/nginx/conf/
7 y# q, M; k! H9 @将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
5 f3 j# d& {0 K' z4 M: y) r5 S
) W* [, t" Q9 E" H
$ vim /usr/local/nginx/conf/modsecurity.conf
  j8 _9 w5 \8 H4 B4 W% v: _4 CSecRuleEngine On* U/ p. v/ q/ E7 ?, T
ModSecurity中几个常用配置说明:
: B$ t( C& i" p. E  Z7 }1 M/ r9 R: n" n; W; U5 u
# t+ \, t/ e- w
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。* ~0 F* r3 J5 {6 _( q* s

' S8 t* C# o9 X1 o" R* s
# _9 R( r$ g0 x4 V% \: ^2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
2 d- z' N4 G  h) M7 ]  n4 e3 {: _" t9 K! h

+ ~0 a9 [# o, _& @, L7 v3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。. }: s, O# \1 ~: w6 e" O6 B( P+ U* a' X9 S; c

& P5 f! ?5 i3 u- t6 w& H
. A) Y- R' H: ?; @4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。& a6 \% L$ N1 z" {

6 v( S7 d; W. r& `  a1 P3 e8 e
) w0 E' g6 U' A, r. ]8 R$ R在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。5 g2 E, I' y; i: b) {2 ~- c
5 A% o' J) j5 a( ^" H$ F" F) s
- V& z" o3 ]' m% y/ O1 ^
3.x版本CRS, T# d, ~4 r3 @- v
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs* d2 m9 }- ?) s
# 生成例外排除请求的配置文件0 ]/ J0 r3 [! p- F; f. [0 d
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
4 J  Y9 Y2 d$ e0 J9 D8 E% a$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf- e) H1 m% I( ^$ S
$ cp rules/*.data /usr/local/nginx/conf
- m2 S9 n! y+ S; W( S% o为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。( B; N# A8 |0 }
) V& H! R7 d% k/ _- e

; K& p2 }  F5 D$ vim /usr/local/nginx/conf/modsec_includes.conf3 @$ U' c) l# G& }' t* t

! i$ w  N9 j7 p# f
[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 J, q/ x/ L2 J
" e% ^1 C$ r# o  j+ R1 L, }注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。6 T0 \* E1 D* t
: ~' |( L6 @* z, ^! |# g+ n
. U5 [/ ?; R0 x) P) @
配置Nginx支持Modsecurity/ G2 A& g6 D0 s
启用Modsecurity
  y+ F$ r" _! l# [+ P使用静态模块加载的配置方法' d" c& g' Y; V' ]
在需要启用Modsecurity的主机的location下面加入下面两行即可:. G, ?- W5 _$ g

& I+ N' J, X& i+ F# j4 k! z: z
/ Q) a; i/ h) S" ~# N: ?ModSecurityEnabled on;; S- W0 d  c6 u4 _: T
ModSecurityConfig modsec_includes.conf;- N  A  o: V. A/ ]! U& a
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
9 e3 L! ~3 i: V% B; G- [. N! @  ^: N% m4 s
& T' ~4 }: K; ]
$ vim /usr/local/nginx/conf/nginx.conf) `3 O7 y3 w8 Y; l0 T0 b1 d

5 h' P) _' R4 u% e1 H( N" U9 j; E% p
server {
5 M1 A2 Y: C" b1 e9 f  listen       80;. _# ^" |1 m$ Y- U" ?, I8 w
  server_name  example.com;
8 G: G' f+ a0 p' |- \2 O9 R+ D$ Z1 h8 k  W: k* I& W) \

( T7 A5 E6 |) ?1 n1 u- i# J5 F  location / {
0 B- e" O& p1 R9 A    ModSecurityEnabled on;
- {7 |4 e. L+ f, D% x* ^/ Q; B$ L9 A    ModSecurityConfig modsec_includes.conf;4 _' }$ m# R; ~0 F$ @8 {
    root   html;- A# F; W1 i; d" p: P9 y
    index  index.html index.htm;
' x" @% ?9 U3 B! D1 j. A# ^  }" x$ e/ i$ F/ r# `2 t
}: L' G) g+ X1 ?& c  J
使用动态模块加载的配置方法  ?3 M( M+ _* q; o( @, a
在需要启用Modsecurity的主机的location下面加入下面两行即可:5 e$ b; x  h, g7 n7 ^3 x( c6 e

. f( o( d7 D; S5 S/ }+ D3 a
9 R( f: j* J7 K8 f. ymodsecurity on;% ]  ~7 P* C* ^# f2 E3 C
modsecurity_rules_file modsec_includes.conf;1 `/ O5 g- ^6 [9 b3 U4 f
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
* V8 \( {3 ~! r  v, a  f+ i$ X8 `3 z1 K' I6 }1 u. V0 ~4 m3 i

: d' v7 Y1 H3 `5 K$ vim /usr/local/nginx/conf/nginx.conf' [7 l+ C; O( m7 j9 J
4 e1 d7 W/ Y) G3 p3 m) B9 D

" A7 R3 p  n  _. ?server {, l- j, p  K( s: v! m" {
  listen  80;
3 Z3 b  n1 \: M9 {9 U9 a9 \- ?& x8 f  server_name localhost mike.hi-linux.com;& ?/ Y9 x1 e- X2 J! f  q- l8 I  y
  access_log /var/log/nginx/yourdomain.log;
: c; y; A. `" v1 K
4 b- I# p& t  ]( _$ s( C1 S; u% V0 V4 ?" y
  location / {/ c1 w1 H. y$ Z% v7 y4 m7 S  U
0 k7 a8 V+ q) ?5 [1 T* P$ p

3 }# {/ a2 O# c# j  modsecurity on;) J; w! m* T4 ?
  modsecurity_rules_file modsec_includes.conf;  f$ O0 s6 B! Y2 U! a* O
  root   html;8 ^0 d' ]' C* o0 n3 O
  index  index.html index.htm;( L! A6 ?5 [6 u5 Y
}
8 i; D1 v4 r( u& \}8 T! a' k1 H6 T9 q& S7 U
验证Nginx配置文件1 S1 m4 W8 P5 @$ p- g0 s) Y0 L
$ /usr/local/nginx/sbin/nginx -t
6 L1 L+ p" ~) Rnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok% ^5 g6 t) X! ?1 j3 C% g' R
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful5 B  j/ I, r1 N
启动Nginx
; J5 {: R4 k1 X: j/ H3 Z$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
3 r$ `& g- i" c/ D1 q" R% Y" [9 g4 C+ _1 S8 h$ [; 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能够战胜更多复杂/未知/混淆的攻击模式。

, S9 X5 K6 J8 r/ g2 n5 p5 u* m

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2024-9-8 08:50 , Processed in 0.082999 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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