找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11546|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。0 W0 z8 I, R. @
0 O1 g9 X9 \& ]& A3 \9 @
& G! b# V' T- W7 N, J$ p
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
* G+ h$ I# e, x8 r9 [6 L' L/ S8 |, l( h; N6 K  D1 F& B8 x

7 z' J' t  H+ r, O7 k  N什么是ModSecurity# T6 @; l6 Y, \# U0 ]: _
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
6 ]3 X; u) T8 V% h0 ~
% Z- j* o( a: k( b. r
) H8 Y, r/ ~; I8 M' TModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。, v0 K( A. G5 m, w2 _- t: B. ]
1 l6 X# o6 a) v* {& j, X* R

: Q0 C& }6 d7 d; [+ O2 H* xModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。* `; g4 H2 B5 R4 C5 }) R3 c

5 |( c: C7 j2 ?. z, z7 Z7 q1 X$ E0 O+ [# ^
官网: https://www.modsecurity.org/3 o6 X" e' ^! K
% [7 w7 v/ ~- c: v" e& L
2 ?& [* E! Z$ K5 d4 X/ H- c/ p
什么是OWASP CRS
* S3 n: p- q1 Y& N7 UOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。9 j: ]+ o5 U& a4 g( D, |8 @. v4 B5 [
% Z7 a9 i- G' X* ^: c6 J* z! a6 N
5 n9 A# X' v3 Z* K( I1 @. P3 O
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。) c. O( [8 w; W
8 A7 r2 b4 z& K8 x; ]% h
! ^4 A% D" P1 B9 _) A; A8 {; o
HTTP Protection(HTTP防御)
4 O" V* y; E; kHTTP协议和本地定义使用的detectsviolations策略。- @9 @: x% O! n2 U" F" M% V

' G- j- `; k2 X
0 x. {: K: c0 z: UReal-time Blacklist Lookups(实时黑名单查询)
% s4 ]* f$ M; c- ?利用第三方IP名单。* j5 x. h+ e/ D- k
, \: o1 W1 U3 {$ X5 p9 \

; a0 m3 l" r" {( p3 T4 y/ iHTTP Denial of Service Protections(HTTP的拒绝服务保护); X, {+ q* j) K- }% G. @* M
防御HTTP的洪水攻击和HTTP Dos攻击。+ R. @/ Q! F% Z; W6 _7 ~

# W/ c; k) b( D; B& v( i6 D8 w) Z5 Y% Q& K
3 `' q2 ?& W' Z: @0 M) P% qCommon Web Attacks Protection(常见的Web攻击防护)
, o9 ]5 t! U, n3 W3 q# K, ~; v检测常见的Web应用程序的安全攻击。1 e- r$ n5 M+ T1 v7 A% r
. @! ?" d4 }# [; v- t" N* A
& T/ b& S/ [1 u" Q3 p: |
Automation Detection(自动化检测)& i+ s1 k+ b' s7 m
检测机器人,爬虫,扫描仪和其他表面恶意活动。" @" F% K( g' d
) R. P, u7 n2 ?' d8 u, n
- J; V. M. U+ [6 N
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)5 {4 T5 K) t. b3 b: C" V
检测通过Web应用程序上传的恶意文件。: B6 S$ L, ]2 |6 z

2 i2 e- T" B) K% o. r1 Z9 v: K3 p% Q" h) Z/ Z+ o
Tracking Sensitive Data(跟踪敏感数据)
2 d# x( }: S& O6 m: M* M信用卡通道的使用,并阻止泄漏。3 U9 L4 w5 Y1 {/ s, A0 a) q
+ l1 e1 v7 {+ f8 C* j  \; {

1 E, Y. y( y: HTrojan Protection(木马防护)
8 k7 g. P5 r& R* _8 i7 \& E" T. p检测访问木马。( {: y  q8 g" P. H5 b3 a* }

& Y5 T4 E: H6 f8 V  |' w! V" r5 R9 |( h! H: X, Z5 E: Q4 Q2 f* D
Identification of Application Defects(应用程序缺陷的鉴定)/ }% V; n' C  J* [
检测应用程序的错误配置警报。; K3 j5 C( ?6 H- A2 m9 A
: u. u7 X9 D2 D& ~2 o* v

. `% P- S* u/ L+ B2 R+ v5 z9 _Error Detection and Hiding(错误检测和隐藏)3 a1 d4 U( Z4 \4 A# }) q
检测伪装服务器发送错误消息。5 x9 h" m% i. g! |0 f/ C3 u% N& s

$ O2 d( J8 S2 r+ k+ s- F8 z+ D3 i: u, U' z( u/ P) R) B; o
安装ModSecurity3 S: |" n: E) v8 Q  d4 p  l
软件基础环境准备
& v+ m! o5 @# V! K9 G* W5 v( A9 t4 Z下载对应软件包
/ b8 c" Y. Y( K8 z. u- X3 U$ cd /root) ?8 E! m  l5 D: t
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'* {+ y' r6 o7 E" g2 t/ v
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz; i. J9 s+ h1 ]& s
安装Nginx和ModSecurity依赖包( B# [* A# K7 S/ {3 X& N' v
Centos/RHEL$ @% c. h/ c7 [5 ?. G. V* B

/ C5 ]4 J6 n6 Q" v1 p4 o- |, ]- x1 G8 F) a% y
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel1 G( ~+ _& P) H
Ubuntu/Debian- x" u+ y7 q2 ]8 {6 O! N
9 u9 e! I" L) G1 L; k5 |
. N: D; G; n) O$ N
$ 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++-dev3 F% |( u5 `5 G9 f% ~8 f
编译安装ModSecurity4 l& v$ l6 G" e# }& O& ?& s
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
9 ~$ @' I7 k' y: c' G, d3 j) R9 h" `
  h# {* R0 w: e
方法一:编译为Nginx静态模块
4 a+ `  U. ]; R7 I6 n) m* c9 H$ w) J7 L/ `% [1 V
3 w# p- S2 S3 p& a
编译为独立模块(modsecurity-2.9.1)
. D: s7 T3 Z. m% W- H" O- k$ tar xzvf modsecurity-2.9.1.tar.gz. T+ i; n- P' F! s! t9 W
$ cd modsecurity-2.9.1/: }6 [4 O: {, l0 e8 F1 K
$ ./autogen.sh
9 P6 v0 H1 D9 T4 [$ ./configure --enable-standalone-module --disable-mlogc7 I" V' v9 h2 `- f3 S9 F
$ make
9 F- Y$ A. L- e/ p3 g编译安装Nginx并添加ModSecurity模块& |9 p3 z5 l( Y. c7 }! Y
$ tar xzvf nginx-1.9.2.tar.gz2 U3 O  p# v* d$ H( D  A, K
$ cd nginx-1.9.25 V( Y4 X& W9 I! H
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/; A# m4 V) s' P1 F) u
$ make && make install
8 L( Z% X. ^$ R1 X) Y/ k; z方法二:编译通过ModSecurity-Nginx Connector加载的动态模块6 u4 y* d, J5 R9 P7 u& S# y

  w2 g! H# [5 R/ J1 _
: k; P( @2 \  Z! [% }% s0 H% V1 F编译LibModSecurity(modsecurity-3.0)
9 }4 W; }7 Q/ ?/ ?8 w; e2 L, O! a$ cd /root
# H8 M4 C5 k9 t9 i$ x# s$ git clone https://github.com/SpiderLabs/ModSecurity
9 k" H  ]  p% H# t2 P* W$ cd ModSecurity
$ M; e! q  d) O/ r$ git checkout -b v3/master origin/v3/master! H) l5 W) `8 m- I! `) ^
$ sh build.sh
* N8 s/ Y; }, _! J! w7 l" V$ git submodule init
: ^; {; g; h1 y9 A- q$ git submodule update
! @# t- H3 z! ~" b/ s$ ./configure1 I0 o) i' ~3 d" g4 G: {4 n9 U' B
$ make$ W  H- j3 {5 e0 d3 E5 n
$ make install7 K, e" [/ x+ n( I+ l( |
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。3 u( D2 N) B1 h" B, V/ z2 F+ E

" W: X3 O% z) G2 Z9 x1 R
) ^: P- D' ~. s! \# v# C9 \* U  j$ ls /usr/local/modsecurity/lib
+ ~  y- \  z+ ]* O: C2 Q) P# p0 plibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0; d! p) m4 @- h! N* P0 o
编译安装Nginx并添加ModSecurity-Nginx Connector模块8 R8 y0 T3 W; H
使用ModSecurity-Nginx模块来连接LibModSecurity2 N' n5 V/ y7 |# S
% x$ `- r9 ]. d# m! t4 W

6 ]0 W3 v8 N5 _1 R) n* q7 c$ cd /root+ g6 C6 U) P$ e$ ?
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx# I0 t2 P$ P+ |: a3 F1 G- `
$ tar xzvf nginx-1.9.2.tar.gz
  X* N7 j2 s' x6 c$ y9 J+ h$ cd nginx-1.9.2! I! d4 ]) v: M4 q2 D
$ ./configure --add-module=/root/modsecurity-nginx
$ f1 l( v4 ]& [+ |- J. V$ make
9 X; d1 e1 b; C, a( d$ make && make install( j' ^( |: D1 A0 e# M
添加OWASP规则" b9 h# Z% j/ y' {* C
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。% O' Z# Q0 Q1 k: T  ~* a4 W
* p6 n3 ~% Y& C

' h0 @% \) j, [6 V+ K下载OWASP规则并生成配置文件
: E7 V0 {  {+ l3 n" M$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
; b* N0 s# `. x: q3 y" ^8 L5 j$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
: \) B0 v7 O8 q8 g/ C( d+ {, n$ cd /usr/local/nginx/conf/owasp-modsecurity-crs, w- {2 x, k8 U+ D# O" j2 ?# {
$ cp crs-setup.conf.example  crs-setup.conf, [" ]/ _! r) z. U
配置OWASP规则
# _0 J  Z" C' M5 w: }编辑crs-setup.conf文件8 C+ e+ U. j1 H! [/ t  d
4 O# U2 |* {( O# B0 h% B# |

& O4 K8 C+ N$ K$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
& [& d) z/ p4 R$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf' b9 |: u0 F3 E0 U& F2 ~  Q1 y
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf0 h- x6 C, ?% K( W# k9 H0 k+ `
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf. x7 M" p3 x! P* a" Z- q" H6 p
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。4 N0 W6 ~, e5 P& I
7 f5 ]% P' s; P1 e

0 W3 _$ r; z) t' w启用ModSecurity模块和CRS规则
& y( H" e' E3 r' U$ M% w复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
. L4 {) V5 j1 K+ E0 y! }. e
+ P, M( F! Q! @# p
1 B# K' G7 r+ u* U8 g0 \9 ~/ n$ Mmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
  F: o# Y- p- x- h, i8 |: L
  P7 p; ~3 W: i1 o- q: I* m& S" t8 {+ |  N, y/ P
$ cd /root/modsecurity-2.9.1/
1 [" k0 b4 w% l: J$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  2 M7 a  c+ P9 O: x) x/ Z& p
$ cp unicode.mapping  /usr/local/nginx/conf/1 }( ^( N+ _5 [9 K" F7 O6 n
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
& h# Q8 Z* ~% t( K0 U, S2 J6 ^1 g1 @4 R( V* c+ |. p9 I' p
( B6 ]$ t; r0 V8 h
$ vim /usr/local/nginx/conf/modsecurity.conf
, K. {( h! a8 r+ wSecRuleEngine On( A3 x# V5 F+ p+ s
ModSecurity中几个常用配置说明:' ^& h! N8 r. Q, q& n
9 p# A- J- C2 S! q. f. a; ^

+ R6 q7 f4 R& Y1 s" Z5 a1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。1 b2 h/ Q! n/ \2 q" M. u

6 y$ E3 H8 h& g  ~* w
# K5 w0 [, F( w" n2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
- P5 i& f6 z3 |6 T, C% m
: C, b6 T5 R) C, N# F$ ]! E3 Q
" o; ^  @5 w' ?6 \, t6 r3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。/ a  j( G3 Y' j+ x. _
2 H0 h( U+ @0 a( b' x9 Z

! F6 \! ^. e. [% W' p! J4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。" @( D1 c7 C: j9 R

' ?* x1 j  ^# P$ x6 e) o& L
+ v- b1 ]- J( P1 G在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。6 t4 `6 ]4 O; y8 }8 j) _& m+ l

9 b3 K% ?( Y/ A0 i) f7 L% m! ^! i1 M& h
3.x版本CRS9 E% T% l4 Y4 x6 q
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs8 u" j) X) _( N+ }* X
# 生成例外排除请求的配置文件
. `) C/ ?. T# W, s1 l+ L6 R% D$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf1 i  b# w  w& ~7 c
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf, t* y  [% T9 P# @) e
$ cp rules/*.data /usr/local/nginx/conf
; z- q) }& m; n+ X. J  Z& l为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
- G4 t' q; _# c0 I, i0 s* \1 p6 P; V7 m" T6 r2 q2 v# B$ [) w
, y1 p) V' G6 G3 V% X$ y& z6 t
$ vim /usr/local/nginx/conf/modsec_includes.conf" u; \' m- n. E% f5 |7 l) M
  r: Y' {6 ]7 r8 Y! t+ _& N
[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

- o3 c8 t& U/ C1 C+ x5 `  D% l$ T/ Q# f7 W  \; [5 o
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
" U6 D" o4 Q0 a6 z
, h2 a+ V* D7 ?  x5 S( o
, ]; Q+ u' B: S: a配置Nginx支持Modsecurity+ d, l+ v. T4 k$ ]
启用Modsecurity8 D5 q' v& {9 i' h5 P
使用静态模块加载的配置方法
/ V& [" `/ Y6 s+ R9 S在需要启用Modsecurity的主机的location下面加入下面两行即可:3 }) x& C! I3 g
8 J2 E1 T2 J4 s$ H
. y1 t/ V- ~% |" y- s* P
ModSecurityEnabled on;
" S! u2 U  ], W8 R& [ModSecurityConfig modsec_includes.conf;
# C( h& ]$ ?3 k3 r修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。: i% l" K# e5 W/ Q- t1 l& O

% Y3 A! W9 Y1 `& e0 L8 }& S1 }
  J$ y6 L3 S. ^6 p, o- K1 X$ vim /usr/local/nginx/conf/nginx.conf
2 \$ a; z% h' b; h* a. X( Q; k2 ~  v) |8 ^: D! h$ c
  C+ r3 g9 |1 R! M2 V- ~% ~6 n2 h, X
server {5 @# [. R# i3 t- R, l( g0 O- z8 s
  listen       80;
' h  a0 m* n4 O  R' m  server_name  example.com;/ |+ M1 S: Q4 ?9 k

! G: Z/ Z) L+ W6 Z- }1 s# z2 J5 Q+ ], u8 s" s6 v
  location / {
; Y: r1 z% G: {    ModSecurityEnabled on;% {; w5 ]3 }" ]! l
    ModSecurityConfig modsec_includes.conf;0 ?/ w6 T7 N5 [* V" k  b8 ]. H. c. x
    root   html;
6 S: J4 w4 M5 T. C; |+ ]: i    index  index.html index.htm;/ i- Z2 t" V& g+ R
  }
+ y1 W! c# v& |- ]}" {! z6 [  ]3 |0 R  Y' `
使用动态模块加载的配置方法
. A5 K8 J6 f! `% B8 O. T. ]在需要启用Modsecurity的主机的location下面加入下面两行即可:# e0 |; Q; C# P) T+ Z1 X
0 j0 |2 Q4 O' f/ n0 k3 f0 a/ ?
& h! _% x+ ]7 i
modsecurity on;, q# x. X' \9 f% }8 G7 s
modsecurity_rules_file modsec_includes.conf;
. m- C  ]$ Z: K修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。- W/ P( k* q# N5 l8 R7 S
- P5 e( R1 n% K, N/ r$ J, K
% S) _) D+ `6 @( a5 v
$ vim /usr/local/nginx/conf/nginx.conf
, G) M. Y# D6 j6 w
  g0 _" W5 Z- [; \: t/ h4 a( {/ ]& {2 A/ Q6 a
server {
8 e* o) s' v1 ]4 C, p# r  listen  80;4 ^6 L! A7 _( h. `
  server_name localhost mike.hi-linux.com;5 I7 u3 G8 I' w0 w) E' t' U( j
  access_log /var/log/nginx/yourdomain.log;
' @$ Q+ g0 u5 H9 f) M* e) ]( l3 t  \6 I

* a& r7 O) |3 I& v  location / {
) i8 A0 P( _3 Z- I! z* `
7 H5 I9 u" R0 h% @: U3 F+ E  f% ]( L* g* H7 G) g: h
  modsecurity on;
1 }7 l& \/ h6 ~3 d: K  modsecurity_rules_file modsec_includes.conf;: N0 w4 Z" [' P+ s
  root   html;
: A% p1 Q8 @; J, X; t  index  index.html index.htm;& Q9 @* p. T0 P) N; J
}& {9 H9 @% D1 e' b8 o) I' w6 @: N9 q
}
' o) U* z8 ^" U; R3 J2 h* \验证Nginx配置文件
, }" V# I: L/ C) v) h$ /usr/local/nginx/sbin/nginx -t
2 _4 I4 E( b, c1 ~4 H0 Unginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
* V2 d3 H/ Q: Q0 d6 `2 J, Bnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful! j' ?3 Q: `! A' [
启动Nginx
+ a2 b1 y  a2 B. S. i$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
" I( c$ @8 y/ E8 i; ^" A: }, t3 D  I: E6 G4 z* F4 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能够战胜更多复杂/未知/混淆的攻击模式。

/ r0 t$ {: @) v' c

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-6-23 06:20 , Processed in 0.078770 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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