找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 8841|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。/ ^, ^. \5 y$ K  l8 j$ p
9 T3 j' L$ X3 o0 ?$ q
5 q& F1 ]: C& K) }# d4 `3 I
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
. _0 Z6 I5 T2 a* u' }) y  G
" }# i9 @3 n6 N* Q1 V( M1 p* b0 v6 H4 k$ J
什么是ModSecurity7 U( X9 V; N) j' n6 o' e( Q
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
: J% O8 J3 x6 B. ]8 o3 V$ _, f3 Q; G, J
; h- H9 G0 j; z9 n1 ~( G+ H7 c
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。$ Z) s+ @8 H2 r6 B6 b4 H, A
! Y8 F  k4 j9 w% j6 f6 ^

7 L9 E, ^' R! D% [ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。" m% J. X, p2 p2 r) u

* {# B# D) v3 @$ P, {+ A
' ~2 H: R5 ~6 a: g# _) K3 s官网: https://www.modsecurity.org/
, j! n# I2 g; O
5 P3 H9 Z, Z* s6 E1 m, d" `6 s1 E5 p/ Z
什么是OWASP CRS0 g5 ~3 j  Q9 f* T+ F
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
& ?5 g% a* s; i; D+ }
# j; n* C' l( `7 P6 V' B( `# u
9 I8 U, G7 w$ \  eModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
. H/ g2 T" Y- g6 H+ ?* t" d8 ?7 Z: L7 f) {8 S8 m4 b
7 Z" D5 ^- G% P+ v1 q# ~! F
HTTP Protection(HTTP防御)( n& c  n( P: {+ ^. b! i/ u5 y/ v; f
HTTP协议和本地定义使用的detectsviolations策略。
4 [- _8 M( W- j: n& ~4 E
" K" n0 |' B/ d. l5 W# `8 p+ Z$ X! S. w; G% q
Real-time Blacklist Lookups(实时黑名单查询)
) J0 z0 H; g1 s) t; `7 @利用第三方IP名单。6 L/ Q* f5 I, {5 G; _

( X) t7 R+ a" `' D) |+ E
( r. p/ |" S, {HTTP Denial of Service Protections(HTTP的拒绝服务保护)  F3 j# S; R  t. p* s1 }
防御HTTP的洪水攻击和HTTP Dos攻击。
6 A& r7 T* D2 c: d
1 z0 N7 v: W% j( m* u/ S) E! Z; n3 v$ v& S( p: O0 h
Common Web Attacks Protection(常见的Web攻击防护)
8 Y1 u: |& O! A: K* r7 a- Q检测常见的Web应用程序的安全攻击。
4 R1 K: T! t! b$ |& N9 E' C$ T; i
# w; }0 i+ u, K1 _% x
2 H- G6 D  |% ]" F+ ~& Y% L5 C# iAutomation Detection(自动化检测)3 u3 P3 I  M1 v( p
检测机器人,爬虫,扫描仪和其他表面恶意活动。
0 _" y/ Y6 q5 }7 v$ Z
3 ?! m, }- p3 E' a& r% H2 K" n7 H7 Z% X
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
* s& z2 D2 K1 H: P检测通过Web应用程序上传的恶意文件。  e/ ~0 P) o; t; r; H# ]' k/ q
: t3 e7 T+ C) _1 a' h5 Z; V5 C9 v
: p! I1 b$ }$ ]& w: {3 ?6 N" n# G
Tracking Sensitive Data(跟踪敏感数据)
4 I& k- o* w0 k8 d' O+ e$ M/ H0 L信用卡通道的使用,并阻止泄漏。3 n/ x! k2 g$ U

9 B1 W  `. L. K) k; c5 h6 a8 s
+ N, f8 E# X. G. c4 w. Z' B7 h; nTrojan Protection(木马防护)
  ]# p( T& d) r7 p2 I. l检测访问木马。
2 V* b0 C" x+ Z4 \% h% o- Q: z$ L
0 I# H" ^( x  O+ x! \! @& M3 C' ?: {$ C8 c6 l+ x! |' w
Identification of Application Defects(应用程序缺陷的鉴定)3 n. I2 q) U! ~" z! c
检测应用程序的错误配置警报。
/ L+ L8 z3 Q% f+ R
% q0 P6 T8 S; X
' c* q% \: A( z- y+ L0 |Error Detection and Hiding(错误检测和隐藏)
- n# Y  K7 d0 K, I检测伪装服务器发送错误消息。$ \/ e7 H5 y7 m( O2 _

" W/ ~0 Y# {9 v1 b- ]6 ~% s# S' k8 t0 w
安装ModSecurity
/ Y6 E2 {3 m3 c$ B, |. G0 S% n软件基础环境准备3 D+ C  w& M: G- S( \
下载对应软件包
4 w6 w  U7 b# Z7 o$ cd /root
7 ~# n+ D  Z1 W0 V( v0 a$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
1 y( E3 P& T' ^& V3 p" Y; Z. h% g$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
* E4 U, C8 |" W0 m& ^. Z+ o安装Nginx和ModSecurity依赖包
. [# N: ?; _5 s9 H$ Q+ WCentos/RHEL
' R6 ^! K5 `8 U- d; \* w) f6 i; @' N
  W# d" E, l+ z% i- B6 f5 r; B
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
4 O6 k. j5 Q5 e" \( L: z" oUbuntu/Debian
* u, ~* a1 r$ m& i% r" _* N* s  \7 S4 C* [( a9 Z6 H" P% m

4 A' d- ~" j9 @( j( A$ 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++-dev5 G  s4 c0 J2 j4 |
编译安装ModSecurity6 J) M/ g2 y) t3 T* M
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
( f* E5 i/ d; i0 g. D  z3 v  o
. ]0 ~) v8 K( o$ i7 U" |
; l  D/ z7 \6 E6 J' j6 O方法一:编译为Nginx静态模块
8 w+ U' Z" l2 F- V0 A% P5 q% o! y9 E  Q7 a) E5 l/ F' v0 y" n5 v+ p1 }
1 |" Q+ M6 `0 E
编译为独立模块(modsecurity-2.9.1)9 q0 e5 G. t6 v5 `% `
$ tar xzvf modsecurity-2.9.1.tar.gz
% [+ `  F$ {, {& o) s0 r$ cd modsecurity-2.9.1/& M9 t$ M$ N$ b2 ^: l+ d
$ ./autogen.sh
. j9 i$ k. \! j( i8 g+ g9 @$ ./configure --enable-standalone-module --disable-mlogc
6 w/ G: r' w. T: U$ make
$ S9 H+ B8 B+ K' g- [编译安装Nginx并添加ModSecurity模块
/ {* v  k) W  h. Q$ tar xzvf nginx-1.9.2.tar.gz
3 [  v  @8 P* d9 C/ g) t$ cd nginx-1.9.2- X- c9 G3 ^# Z0 @/ {9 {) q+ t
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
' P) o  q# H$ J9 t) [: g$ make && make install
5 x! o! Z7 q* i1 [方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
! H) d* p& B& a$ I1 h
$ V2 e7 l' j+ c. `& m# W! ^! {% M- C" N" c: K
编译LibModSecurity(modsecurity-3.0)5 ]& c0 L4 b+ K/ v9 Y
$ cd /root% I, A3 Z' h: w7 r/ P) S
$ git clone https://github.com/SpiderLabs/ModSecurity
+ h/ d( N3 I. `' h+ w6 f$ cd ModSecurity
' q2 K3 R" h! [  R+ G$ git checkout -b v3/master origin/v3/master
" j# p! F; X- }1 l# s# a, V$ sh build.sh- e3 U+ I: I  p- z  F0 }' f9 \
$ git submodule init  L  J2 b1 k6 k! n! g5 J
$ git submodule update
  i3 k; H  f6 ?* a( z$ ./configure% P: b+ m$ m  I# E8 C7 K- ]( _
$ make# e! J- |/ O# |9 r: ?2 U2 A1 U
$ make install
4 x; T* e1 y  g7 D5 [LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
: V( G2 d& h# |) u  T4 H
; Q% r. l8 I' {1 Y! N9 i7 V2 M: C0 m% L
$ ls /usr/local/modsecurity/lib9 s2 S# t' h5 N% `! e
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
% x" p& i: Q6 C9 Z% u* ?! l# G% h编译安装Nginx并添加ModSecurity-Nginx Connector模块7 p/ t2 ]3 K4 ?6 b
使用ModSecurity-Nginx模块来连接LibModSecurity5 d8 U  n! p1 ]3 A
4 D7 ^" C' d- {) g# G' ?
# D3 f- Z) G7 u1 t  A7 @
$ cd /root
! d# ?' Z' U7 d6 b# t- Z, p$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx/ E- |- ]! o+ U
$ tar xzvf nginx-1.9.2.tar.gz
* k; t* g0 a" o, N* T2 f$ cd nginx-1.9.26 t. n9 m7 Y- O  }
$ ./configure --add-module=/root/modsecurity-nginx
. Q% {" [' M8 N/ A( L* q9 J$ make
7 v; C; U* J. D- M  C8 I$ make && make install! t8 j% Y, R" r8 a
添加OWASP规则0 C) U, \: ~  m' u) U, [
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。; b1 n, n! l3 `# r4 i  n" R
) `+ Z" Y/ R' ^' M; L; U

0 X6 D  |: D, q! m8 m4 P5 y下载OWASP规则并生成配置文件
! [; j! i  v. T  \: ^: [: R$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
  }8 ]0 E/ @& U" S, X, e% y$ M( P$ Y$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
5 n. W0 ], y. s7 j+ G$ cd /usr/local/nginx/conf/owasp-modsecurity-crs: x# B0 ]5 L/ V
$ cp crs-setup.conf.example  crs-setup.conf/ S7 U9 s, h, Z, T7 o# m$ k
配置OWASP规则, M7 u2 e% v+ E$ c
编辑crs-setup.conf文件
3 H8 J9 j2 j: K6 ?1 l. M0 h3 ]  \8 y) z6 v0 ?0 q2 M2 Y
5 L, R+ Q& X; Q" E* M: {# t3 T
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf+ A4 y' s7 h' k4 O6 n- H7 U
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
, C# q1 }, `& E2 J$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf0 _4 _, f% L* o
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf- k0 V4 z8 b2 e( n, L' D
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。& W2 _" \1 d: d

$ u( c/ J9 u( _/ X3 u, Z6 x" I
8 ]# }1 W/ n+ U1 F启用ModSecurity模块和CRS规则4 u& y' Z& H! D; B1 d! O
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。% |/ g) }+ P: Z7 r

* u4 N. D4 o; M- J3 u' ^
& w. n6 }6 ]7 U' j: t6 y- Imodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。1 K9 u+ b& j! p: s6 A, H
! N2 j& i* f  F7 c+ l! l. D

/ L1 E- Q- d3 _6 n. L6 X4 H. D$ cd /root/modsecurity-2.9.1/1 h6 D- o( |) L, @  U, ~; e; X5 w
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
: p; Q; m% L/ O! r: M$ cp unicode.mapping  /usr/local/nginx/conf/
! M& z  \/ V: c( B  H将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
& Y1 Q0 D- i  q9 E5 z# i
$ Q6 Z7 k7 |, m+ j$ R1 `( L
. K% P& n9 a: U, X2 ?) y+ q$ vim /usr/local/nginx/conf/modsecurity.conf: O/ |/ I6 `/ e  l6 z! r1 x
SecRuleEngine On& p! q: ~- r5 Y. ]: d% A
ModSecurity中几个常用配置说明:8 G# j- u, S4 H1 ^( {( {- e# ~
, b0 r$ P/ `% k: `5 d, O  m
- _9 F- R7 {! s% h# s; X  O/ j
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。; `0 T! w% m2 S9 _, d2 {
' T3 y  W! x5 ^! c+ s

+ s7 b: [- m9 G2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。. ]4 H3 t' z9 O4 a3 h4 S

0 Z+ p! K% T& y) E
; X4 G  u, o, g" G" Z3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。0 H4 Y4 N2 j' X( h! u
) R+ z7 }! ~- @3 L1 b8 g! I
" S# F  i. g9 P) Y; v1 [: N2 m
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。2 K' h; v' s; @) s/ F% V
; n4 V. u3 C" C% Q4 z

; i! q$ D/ _+ t在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。& k" `; o# Q- A2 u
3 i4 o3 L6 L) Z& W2 w! L

1 @( ^  C' X% t3.x版本CRS8 g& y, Y$ w8 d
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
5 X" k, u% ^# o# 生成例外排除请求的配置文件. Q/ Z% t, L% Q* H3 ?% d" b2 [
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf2 d1 ?# k: S1 F0 e* W* v
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf3 D1 Q  _1 [8 w# a! f2 M+ L5 H  z2 n
$ cp rules/*.data /usr/local/nginx/conf
: T0 s1 }" j: J- J, A# Y7 `. ^为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
8 `9 e6 F4 q) G
( m) J6 r+ J) ~3 \* W
* ?: C* f  F9 i. `& E8 f" E+ n# A6 D( v$ vim /usr/local/nginx/conf/modsec_includes.conf
% [$ X5 b; @8 v" q: d1 }: m! o, Y5 ?0 @6 N" X: c" L6 ^
[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

2 w3 D* P8 W% A
7 |# Z+ C& Z0 P# c# f' d) t, l$ c注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
5 c# Q5 e( z. `( D2 a7 d
5 v  g- @3 W- k  }9 G: E( R
; X! X) w+ V2 {+ t. n配置Nginx支持Modsecurity7 w' }; H% {- c3 m
启用Modsecurity
+ H) }& j* F6 v" T使用静态模块加载的配置方法5 o; N9 O% g5 D1 r/ g; y( r
在需要启用Modsecurity的主机的location下面加入下面两行即可:
; k4 A# e) P0 L! g% V1 U5 ?
% P$ D3 S% M# T2 j, m
9 B/ r6 _" u9 q1 D' g8 JModSecurityEnabled on;
, J) J* |" ^4 h& l( c4 o& T) Z! nModSecurityConfig modsec_includes.conf;: j# K! p3 S! x" w! b
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。* i. F% E3 v5 S8 l

+ y. |% a$ Y2 w/ f) a, W0 x7 }6 K. ]1 V' Q1 u
$ vim /usr/local/nginx/conf/nginx.conf
( U0 Y1 i& A7 h6 y1 }: ~) _& J
0 V$ F( ?8 E; o8 j( R, B0 n% b# {( B# x& ~. `1 |6 m
server {
+ A% Q( B. W/ a  listen       80;9 _8 W. g, |& w* b0 @
  server_name  example.com;
' B) E! l  K% _/ [! n) W
0 \! ]" f) s' ?
" J) R3 b, x4 A( k4 h0 G  location / {1 ~* [5 D! O3 i' i8 l
    ModSecurityEnabled on;* @3 L6 W( j# e1 y% V1 |
    ModSecurityConfig modsec_includes.conf;$ W4 v% H. j6 b1 P" B
    root   html;0 C  L# F$ j& j* Y0 \
    index  index.html index.htm;! `, q/ }( S. L  n: ]
  }* T, M/ I# B# _, N0 w$ k
}$ t' ?0 N5 Z) ]3 J  P9 c0 C
使用动态模块加载的配置方法* n& N) R/ J: E0 t' n2 d
在需要启用Modsecurity的主机的location下面加入下面两行即可:6 {$ [7 [& D9 w% e" ]& }. ^

$ N5 e+ G- L: h- d, p1 V5 t
) H/ N! ^/ w* X: W9 K( Qmodsecurity on;
% t8 P2 q' o% H# ?) y8 Ymodsecurity_rules_file modsec_includes.conf;) H+ G- {, E8 j* k
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。1 r$ A3 d+ ?8 s5 U& d
- D  p2 v( n5 m7 Q+ Y. r
8 G4 \/ a$ F  I* k  C
$ vim /usr/local/nginx/conf/nginx.conf
% {' z7 f5 ]8 J, Q) L: q
$ g# w8 R3 b  \
9 K* ]7 }' i: H$ S& Y! [server {
# t+ N0 c9 ?" |. K% ?6 T8 {- v  listen  80;
6 a. d) h8 c+ m5 ?/ E$ U: D" g5 S  server_name localhost mike.hi-linux.com;: n3 l1 r/ L6 ~: n! t
  access_log /var/log/nginx/yourdomain.log;
4 R' b# ]" n6 R8 U* N& i3 R# l% E1 B  I3 u1 F

/ L! }8 x2 e2 @) e7 G6 x  location / {
  W) W) d* Y7 q3 j
) m" t. f  X2 b  {7 n, a. x( A3 P- E) p$ F
  modsecurity on;
9 V8 U+ {0 S" e8 m  modsecurity_rules_file modsec_includes.conf;
0 N; y' R* w1 ?8 _+ o# h  root   html;6 Z% U: X5 j# h$ u* Z( i
  index  index.html index.htm;
4 d. d% U' g) f4 r2 K}* _, h' P2 i, _5 w
}1 i+ }* s1 u5 T1 ~& q
验证Nginx配置文件
% \+ f/ C! X+ a# p, K$ /usr/local/nginx/sbin/nginx -t. D, k! b! d" Q7 Z. h0 ~# F
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
- {% W7 J" R1 R7 K, ?0 t$ Hnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
; R2 |( \; {: L( q/ F启动Nginx
9 s  X6 m/ P' [/ R$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf, s0 L3 A7 `. ?0 ~. U2 d  h2 Q  N

" i. K: w$ e0 r. h

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

" q$ Z' A$ ?9 y8 s$ J! F" _

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2025-7-7 09:30 , Processed in 0.082962 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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