找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11856|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
2 s; g9 y. ^8 W7 d
8 T3 S; ~, i3 z, \' T
7 Y4 g0 U! P* R, S! [在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
6 E6 x9 Q$ o$ x: k) I% n# Y
* \# Z* }  M9 h/ {0 R  K: z! [4 H
# E. L  y9 f: D9 |7 q8 a什么是ModSecurity
% k8 k7 s7 t! LModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。0 R4 Y& K9 T/ v6 r0 x" ~
  `9 |6 A4 i7 O
$ R% |" n& A8 v- W; r5 W6 E" S
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
0 h. n+ b! B( j  S7 a+ w* }6 Q# h; W4 x% g
! V. T4 h% ~3 N
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
3 {: M, C  e- t# V* _7 F+ C2 U9 n$ e2 k6 G2 F3 e1 s  ~' [7 c5 k0 j, y
5 D- F9 g# m3 c: e  V: a. x
官网: https://www.modsecurity.org/' h0 M8 E' e# t& g5 b+ j
& h& H8 ^+ q" w* ~8 {6 W2 q/ q
0 j2 b0 k) N& W. g: @1 \. o
什么是OWASP CRS; Z% n2 q& R" o
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。0 H9 G* Y" f" U+ {
) d$ m7 s5 X# v

0 z) U/ t9 n& a4 gModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
. v% h' _# u3 O6 k
, n8 {1 P4 d$ B: r# g3 H7 _; n% q) e& x' R& ?$ N, H6 F8 a4 X0 l' @: a4 j
HTTP Protection(HTTP防御)" U+ D7 F) ]* z% B5 l- s" ?+ c5 O
HTTP协议和本地定义使用的detectsviolations策略。4 F7 H; \5 M9 [) s' }! ~% s
; a2 |8 @+ `; }- p
' `# _$ m9 T9 f4 N) G! h
Real-time Blacklist Lookups(实时黑名单查询)
% L' T! \4 m: |  ?0 G利用第三方IP名单。
, ^6 ?0 [( D& z$ d! X: \/ G
5 K9 [6 v9 y& E1 K+ Z; L
2 n  J7 d! n' p: HHTTP Denial of Service Protections(HTTP的拒绝服务保护)( p. s' g9 S1 F- y+ v
防御HTTP的洪水攻击和HTTP Dos攻击。
+ u4 v4 @8 D1 v, M# A3 W1 w$ N7 I# h4 N6 N/ z) l
" R' q' E1 R6 D& L
Common Web Attacks Protection(常见的Web攻击防护)3 }, D5 ]& R6 Q& G% y0 ^
检测常见的Web应用程序的安全攻击。- k6 v* _' g8 j5 T$ P) Q* _
4 K0 F8 V1 ~+ |/ K

( h# ^; k/ b3 yAutomation Detection(自动化检测)
- D! ], l$ ^" X/ y9 {检测机器人,爬虫,扫描仪和其他表面恶意活动。3 R/ f! Y0 x& k' n7 S
, V( p  j: W+ S$ l3 c, Y& O6 J' z

- o+ u, W9 y& \0 b, K  KIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)7 L/ T4 {# R4 ]$ y+ y  n
检测通过Web应用程序上传的恶意文件。
- z  f. B7 Z& Z# X
+ Z: a* w/ L/ l5 r3 b5 y' @+ S2 g* C7 a" p
& j% o/ }( l$ J/ O) f3 mTracking Sensitive Data(跟踪敏感数据)0 `& z- G% U5 x
信用卡通道的使用,并阻止泄漏。
: |8 T& x* W0 }! y( t. `1 K% e3 `, e% ]" G
* m7 k/ q1 d1 M
Trojan Protection(木马防护)- e9 e: X: F* ?/ E0 ~! }
检测访问木马。
8 u& B2 ^( d% H" `+ P; S$ X% c+ q$ [% W( p  I
- X# Y& [9 P8 c' p! r7 X# u1 a
Identification of Application Defects(应用程序缺陷的鉴定)" y- f0 g' g! E; ^
检测应用程序的错误配置警报。2 O+ C6 ~8 j# q& u1 a; T7 y
7 `* F& O0 X/ w
3 C1 U! K. b  @; u( y* a( O5 P3 `
Error Detection and Hiding(错误检测和隐藏)9 a; _4 X, {; h+ D6 O
检测伪装服务器发送错误消息。! n5 R8 F# {  O! W/ u3 N4 N

' r* \1 I4 r: Y2 f1 w; M( G+ [0 C9 s# o% L: {# A. ?5 A' ~# {
安装ModSecurity
5 Y. X: o, U2 J! _0 t软件基础环境准备
, M0 Q0 F7 x$ y5 X) q3 P6 n下载对应软件包  ~3 M$ d5 F8 G. J1 `9 V+ Q5 h
$ cd /root
7 g  a8 b5 V4 k( z$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
7 \. Q5 q) q* m* o$ 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 L4 {; H; u5 Y" O安装Nginx和ModSecurity依赖包, y/ z. f2 C- q5 ]# [7 x) G
Centos/RHEL
/ v1 U6 Z2 p- q1 q/ v
, |- J2 _( I3 e! B! n
4 J: e# S9 a# d1 \: P5 ^9 d7 j$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
7 E" k; s; @: }( qUbuntu/Debian. h0 X/ W& J9 f7 O+ s0 B! M
/ @$ m7 z% \& V: @- M0 p( L

9 O$ w& x/ n: J+ c$ 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++-dev6 n6 i% h) @; }; I3 A+ G0 d5 E
编译安装ModSecurity. h2 Q4 Z  W: G8 g
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
, ~! ?: m) O0 E/ j, k' O7 t4 b# E# E3 y, e  i

+ t2 C6 P, g$ K$ U2 @" d9 t方法一:编译为Nginx静态模块0 W. o" \3 A; G; C/ L
2 y6 K! v! Z- b% g4 }

$ I0 {, N# J4 y编译为独立模块(modsecurity-2.9.1)
4 Q5 @# O2 }& g: I8 e, h% C8 Z+ K$ tar xzvf modsecurity-2.9.1.tar.gz
# A* g3 [6 ]5 ]$ i- L+ T8 H& b! U$ cd modsecurity-2.9.1/5 k) Q& o8 }$ q! T5 D# _' |
$ ./autogen.sh
1 S$ _) a3 E, q$ ./configure --enable-standalone-module --disable-mlogc7 Y5 h. d3 j3 ^1 P
$ make
. U5 A3 q% e3 _' |" q编译安装Nginx并添加ModSecurity模块
' J& u1 w4 s& z: V$ tar xzvf nginx-1.9.2.tar.gz% @* V! {8 k9 p8 A% x$ Y
$ cd nginx-1.9.23 A; _2 o. \4 K7 ?. V
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/1 P& v* s! q* b' g% j1 ^3 N, J
$ make && make install. r" i0 a! t; r+ u5 E
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块2 d/ b7 J  b) c: O3 ?6 k

& r8 z" P: j0 A4 ~* ]7 T8 ]  t  I' q. u5 {
编译LibModSecurity(modsecurity-3.0)1 W: B1 j, n: r
$ cd /root4 ?" {1 z" T# n# ^
$ git clone https://github.com/SpiderLabs/ModSecurity
9 G6 J! V, X0 i+ x0 m4 Y$ cd ModSecurity
/ g/ @- A& G( R9 e$ git checkout -b v3/master origin/v3/master
4 C8 k( s* S4 ~3 q4 K$ sh build.sh
  }3 z! g7 @4 I) v$ git submodule init" `/ U; z/ L3 S
$ git submodule update% \; i/ ]& @( E5 E  X1 ~, \7 T" G* U
$ ./configure- |2 r  V- M5 }$ H
$ make
- `! ?$ k% g$ j+ x$ make install
+ o4 v% M0 ?( tLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
& x) l. X. ?5 X0 q, M, _6 X/ |  c. S$ B, L* {% X" K
  m- b% P& X, c+ L" M& M0 p4 b
$ ls /usr/local/modsecurity/lib. c2 {4 S& o; ]( X" {) g4 d4 c
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
5 B5 T4 q0 v8 @1 i" ~: n编译安装Nginx并添加ModSecurity-Nginx Connector模块
* n$ v* ^& U3 D+ E! U' h使用ModSecurity-Nginx模块来连接LibModSecurity' B/ F& w& T$ c. A" i, I
$ D$ O. K0 R4 g/ h  \6 m
4 h+ R/ d* z9 V. A" m4 p% k
$ cd /root
( I! q2 `& A/ T: Y3 g  P0 v# n3 Z$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx7 I# K; x3 e; v* u7 w
$ tar xzvf nginx-1.9.2.tar.gz
5 u. v9 ]  z5 B& Z$ cd nginx-1.9.27 W- Z: u% d; x( b4 D3 y
$ ./configure --add-module=/root/modsecurity-nginx, V/ r: K* Y. Y/ ~
$ make
9 J$ x9 I$ J2 i  a5 e' x5 I# Q0 {$ make && make install
1 _: E  V, q8 ?- Q9 {* b添加OWASP规则, Z+ X3 V, K' A" ]
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
) F1 D1 \& x$ l' B: f: k* F
7 t6 I) u# o1 Y* h( I
, B  Z. M3 O. Q  J. U. `下载OWASP规则并生成配置文件4 ]5 y7 t/ G3 Q* Y- ?2 S1 q8 _+ Q& j
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
) i* m8 h, g% {  D$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
% c- g0 Y% x. N, C2 a/ H$ ?$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
- n7 s6 F5 _4 D  U' o! K$ cp crs-setup.conf.example  crs-setup.conf: K6 a6 m: u+ D! q; W
配置OWASP规则7 P# ]) k2 u  ~3 n' @; N
编辑crs-setup.conf文件
: H+ N, w! o; G/ c# O
+ ^, m* b3 H3 h; E6 S( _
! n: D2 T( x7 N$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf6 y. v5 H1 [& g4 E% A6 B
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
9 V& |% o- n& X. y* H4 C$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf! _; e5 l* T6 Y- K' m2 M
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
$ R6 ~$ {$ c8 ^4 k默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
& o3 B8 U0 [6 W% n! a
# A& v4 [, F9 P) O
  \) F- q' R+ }6 F$ v" Q* M' h7 {启用ModSecurity模块和CRS规则- I+ e4 \4 ~3 E/ W# R, @
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
5 P2 S) u) j. z! t( F
7 z" J/ o9 S6 ^1 [5 ?. A0 ?( C: d% N# w1 Q0 @3 N' F
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。' \& |7 ^/ r+ l1 _
9 t2 r: x- }2 u, H

1 f: V$ s: A6 @+ K$ cd /root/modsecurity-2.9.1/
. K% q% F+ N8 ?7 u$ ]$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  ) ^) ~+ f& _* _9 J/ H
$ cp unicode.mapping  /usr/local/nginx/conf/' p: Q% d! \) H+ c# y
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
9 J6 @# J6 r% F) n$ S3 A& B7 ~- J/ r( Q6 q3 y$ t( U

& v5 I" m' ?% \% H3 M+ `! U$ vim /usr/local/nginx/conf/modsecurity.conf7 ^8 s, Q' u, n( _$ p. Q/ G0 g4 b
SecRuleEngine On7 S1 Y2 u- o# S+ Y  D
ModSecurity中几个常用配置说明:
0 i' Z% I) V3 k# Y: s2 B
+ z% w+ ]4 }" l+ p3 }
! Z/ t6 [- T) ^% a2 z1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
1 J6 c6 q* _2 s2 j0 o2 {$ d. [! d" o" f+ J* H7 R/ g. ]/ N
/ t  e+ ], z. v& u5 k1 h
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。4 m7 l- q7 F6 M3 F' f3 k1 u( c" e

! k# v/ T: L: K3 f( R) D
* G  F7 J1 \/ [3 i' M+ r; S+ {. f7 d3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
( `: G; ^5 h3 L3 t7 |' a2 k/ s. o- \

  C( g& t+ x# t8 ]9 E! b, x0 \) A4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
+ p: ^- ]0 j% k( V
7 ~1 B; C$ l/ r; y& d5 H! q8 d8 u) d# b3 Y4 i, B- n8 d
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。! E# O( i9 n5 n2 L

( q2 }2 }6 I  o. y& {, ]5 D& }* _3 {; P/ g0 ?( O
3.x版本CRS
- E/ \1 i# {3 q' y% O  U1 B9 k. B$ cd /usr/local/nginx/conf/owasp-modsecurity-crs& H! {6 [1 ]% V3 X6 z2 A! u
# 生成例外排除请求的配置文件
( n5 ]2 b4 y% \* @$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
: W$ `8 d! P6 Z9 p, A% N1 z' N. i; A$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf2 [2 x- V: s: @- R- N: M
$ cp rules/*.data /usr/local/nginx/conf
" c6 y2 s. R+ D1 ^' ?  Y( f为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。8 G8 U' k& x* P1 G9 W
% i' ^8 d9 R. m4 n9 W1 B; Y

: F1 O( d0 u) Z& W' t9 ~$ vim /usr/local/nginx/conf/modsec_includes.conf. ]1 `+ [5 c; v, L) X
2 G9 h8 c1 E) Y" Z) z
[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

8 n7 L# ]. ~3 A  O$ W8 F( q; i- y* \( W
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。; h  _) ]: y4 P) c5 q0 y( r

- c0 J8 R5 k( M, U, l3 Q6 f& z& G: y. S+ O! ~. L9 e0 j! E. }' i
配置Nginx支持Modsecurity
& {$ j; j9 Q, W/ j8 j- `启用Modsecurity
+ [# n- s  e- H, P; e使用静态模块加载的配置方法
( K8 n$ C0 R4 P3 }5 s: M! n$ b; t在需要启用Modsecurity的主机的location下面加入下面两行即可:' r8 A/ p( D1 U# f+ j7 X* y9 F
1 r  F' ^: T! C& h3 s( f1 k
! u( J, g3 U7 {6 j6 m
ModSecurityEnabled on;( o; d% X3 H! V; |/ x) j
ModSecurityConfig modsec_includes.conf;
- B/ ]" z! N. O, f* N/ i$ r' x修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
& D  M; B' e5 y' W
# E8 }  s& j1 j/ h, b7 e( m+ V9 N2 _3 r
$ vim /usr/local/nginx/conf/nginx.conf9 N+ U5 u8 d7 Q
3 \, I! X& E6 d2 R1 R+ Z0 ^" e
7 u8 M3 M% v. j% }
server {
- y0 j" m' I! ~) S1 o# x4 {  listen       80;! M6 a8 _' C6 g4 q5 v" l
  server_name  example.com;
) {: f1 h3 {+ Z" r6 h& G3 x5 v7 b1 u( r" ^  w* ?0 \8 |

3 Y1 q" i5 u, b) Q. t  location / {! F3 x9 B0 V- ~& l( C
    ModSecurityEnabled on;
/ O2 m+ X  |* H1 l    ModSecurityConfig modsec_includes.conf;- L' m! d8 T/ X
    root   html;! B2 |# [/ T! ~& y/ ~+ {$ q6 o
    index  index.html index.htm;
2 q  c8 R( p6 U' m' s% \3 e  }
: g7 B+ V1 y; n9 [- U& I4 h! O}
3 _. F/ x+ T3 b1 J5 T' v  S使用动态模块加载的配置方法: s9 a8 w; q6 K. ?. _; p. m
在需要启用Modsecurity的主机的location下面加入下面两行即可:
! h1 ^; p! W6 S4 [" p! q5 |" U3 j* {: @& [- {
8 V; c) i* ~4 P
modsecurity on;
& j8 m* v+ I% f: n4 F( Q0 {modsecurity_rules_file modsec_includes.conf;
( d& d9 V9 l# p( F! S8 _1 R修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
; T; @$ A3 Q7 u0 i
3 E" j) b; }7 _
  U2 n0 v) Z7 d+ o8 Y' O, V$ vim /usr/local/nginx/conf/nginx.conf6 e" G  ^: D6 T
5 C2 f8 a+ U8 f$ H" v
' o3 O! f- S- w* n* G
server {
$ O& n( ^2 o9 z: a3 x  Y  listen  80;# T+ n% r, T7 x6 b7 }: E$ x
  server_name localhost mike.hi-linux.com;
& R3 a' l$ n  E' D5 {  access_log /var/log/nginx/yourdomain.log;( p: q5 Q. X" h: L+ ]5 C

. x& g( e- Q  A
1 I& h0 Y9 C9 i! n8 J2 f- S, [  location / {9 J2 o1 L- f9 V0 F% A

" R( F# p6 _' C, u
0 D1 e2 }8 L2 j0 Y  modsecurity on;
7 r0 m% U% [3 S* L8 Q  modsecurity_rules_file modsec_includes.conf;. k; d" e: w: q" C% e
  root   html;# [# G2 e) I$ t+ g# Z6 y" U4 q
  index  index.html index.htm;& B. D  m7 e, f! i! U; x
}
+ `, U0 w7 Q) M, x% h5 Y) o}  a4 E0 |7 h5 K
验证Nginx配置文件! l# I6 u0 `4 q7 K$ x# N- i
$ /usr/local/nginx/sbin/nginx -t
1 T% l0 A- N8 V. w6 ~% o6 Ynginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok2 f# z) R2 ?/ y/ Z  r! P5 W, v
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful) D, e- {! q/ B  K5 h) f, \
启动Nginx* f: @- s2 \1 `7 g5 v4 }$ W" I& _
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf5 [# i/ c# \& E. G) t4 y( @! w! I

5 S" }6 S" c! T# S

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


4 [, w$ I. _: }

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-7-22 13:08 , Processed in 0.085511 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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