找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10932|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。2 G) y9 O6 j# y: P  q! u
6 Z2 }$ D- J: l4 s0 J! m: o
, u1 s7 W8 b: X+ p0 Z
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。0 w9 M( p7 D! L/ ^0 L

1 R' m' _8 F, f7 h+ R) s
% g4 O$ a4 d4 v/ x' S! g9 D+ w9 D什么是ModSecurity
7 k# M9 b8 {* g  }  |5 a6 a, HModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
0 z) I: {. A3 j/ m' z* a
* I& h! H$ r( X7 x5 R7 B# y& v9 N$ F1 C9 e+ o: Y4 h) T! |( \
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
' l: i5 h. P. @7 C
: N% ^! g$ ^# n- [6 k- g% [5 Q
$ ]/ l7 p6 N, k# xModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
  G8 o) g3 N7 D, [
' ^, e: t) Z: A' y/ ?" k3 m9 h5 _$ X% N% m; ^
官网: https://www.modsecurity.org/
1 @4 {! L" {& n3 A8 f9 Q
+ o% R7 _* w% U+ B/ R
  N  o9 T2 h) f. J! E9 F什么是OWASP CRS, @+ i. ^; v2 Q$ V
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。6 H& A. S/ x9 L  V

) _4 ~, N0 s' \0 z! `7 ?1 C& d! m# J1 \
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。; r7 s* U# Q& n
& z9 @- z$ t8 k( n  i! U, @

. W, u3 b9 c3 o/ H* eHTTP Protection(HTTP防御)
$ A- L3 s0 ?0 ?1 Z5 lHTTP协议和本地定义使用的detectsviolations策略。
0 C) E; ^* w: q% ~% ^, F% Y" _: Z

7 L$ A( K/ u3 ^Real-time Blacklist Lookups(实时黑名单查询)& M9 q* `0 e/ ]4 T. x
利用第三方IP名单。
( _  E* v6 {* z  S. d6 e
8 \, ?6 c/ J4 o: n$ {) A
" i/ W4 p' X% sHTTP Denial of Service Protections(HTTP的拒绝服务保护)7 t1 n7 D2 [- M3 R+ x6 ]
防御HTTP的洪水攻击和HTTP Dos攻击。# L( }$ T$ v/ t) u1 I

% Q& Y4 A$ b$ Z( ]
0 w2 n8 F. H- O1 m/ I3 ~0 b/ \Common Web Attacks Protection(常见的Web攻击防护)  Z# B& u; c1 h
检测常见的Web应用程序的安全攻击。: ?+ t2 y) W7 t4 K! K1 [
8 ]) U. }4 w' e, a, W

6 Z; W: h* ~; f3 F9 G0 vAutomation Detection(自动化检测)
) W7 C3 o" d3 [, e  X. w检测机器人,爬虫,扫描仪和其他表面恶意活动。4 |4 q6 L. o9 U. f) G& Q0 `
/ o9 f! N" V$ V1 q. w: Q( f7 C
' l4 B8 B4 b7 h
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
% G9 K3 m; d( e9 J检测通过Web应用程序上传的恶意文件。
# D4 V4 b; J; X2 V3 l2 d& ^0 R2 l$ g& q( k. D/ u  r

' e) ]! d2 N" X8 j! H) tTracking Sensitive Data(跟踪敏感数据)& ?2 B6 \5 T0 O
信用卡通道的使用,并阻止泄漏。; T5 k0 e& \$ j3 [; S! \# ~6 B  R$ R
7 R5 T- s4 ~8 m7 Q4 q, Q

3 P4 |6 E" t0 Z* `$ B7 ^9 o* _# NTrojan Protection(木马防护)
; G" F2 [9 U. G检测访问木马。- H  {' C. a3 A8 A1 B  Q

$ K& K+ Q8 ~- u) d8 h( X2 M4 v3 E" A
" L$ @0 {$ J6 C: x; vIdentification of Application Defects(应用程序缺陷的鉴定)
0 s) |3 h7 o/ X  M4 Z' n2 C- A检测应用程序的错误配置警报。
* ~2 P1 f1 T; Y. ]: i' d: L& d6 X3 B* h# t
+ q1 A  S6 v7 l8 g8 f6 c1 [& M: _( R& S
Error Detection and Hiding(错误检测和隐藏)
& Y: |. a2 r/ X, @% I检测伪装服务器发送错误消息。
; D9 v, J2 z! U: `
/ {* d8 O# i) ^" T- f; t5 V% ]8 d8 ^* F3 k% ^
安装ModSecurity$ \3 t: [7 N1 m/ J% ^
软件基础环境准备
+ B0 b/ h% I/ T$ i% K+ B) V# }下载对应软件包
# p! l$ L0 b1 k$ \( P  o$ cd /root
/ ]4 A% D! ~% c2 y4 g$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'8 _: V1 ^! u; t# l
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz" O7 w) f4 M: T' [0 F
安装Nginx和ModSecurity依赖包
/ r9 J( h; J% B6 B5 h6 W3 aCentos/RHEL8 M/ {1 M6 [1 R* P) \

* j  z5 |+ }4 Y
1 w, t$ h3 f1 e. f! T* a  x$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
) M" l4 K, F8 y$ MUbuntu/Debian8 D4 m, Z0 v* `4 _# j: s, M: G5 u
9 f8 P5 ^7 m  a& ]& H" G/ c

+ |& S5 y! R4 T% m- s4 }$ 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
0 j7 y: e/ @, b$ Q  ?编译安装ModSecurity
' i  n( W, O5 k; o$ ]. T7 \6 GNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
. X" g/ m$ n& w6 V; @. s$ F: l, J# o. B
. P0 w- l" @& V; K1 t5 k
方法一:编译为Nginx静态模块
' S# `& a2 {. x' e7 v  f" r* j
+ L* b+ n* @) _7 j. M/ @9 Z2 y! N+ Q) r1 H$ q  K
编译为独立模块(modsecurity-2.9.1)
  F' A; {; k2 u( G$ tar xzvf modsecurity-2.9.1.tar.gz
: s. `% g5 I4 b% G3 m: f- F$ cd modsecurity-2.9.1/
2 v2 r, u; H- `5 \2 E$ ./autogen.sh
9 d5 r' S# ]1 ^# l! j; W, t+ K; k$ ./configure --enable-standalone-module --disable-mlogc  i( ~7 T) W3 y0 j* B) e
$ make, u) t. a: s' T: _% h
编译安装Nginx并添加ModSecurity模块/ @2 X* X+ Q% W) t
$ tar xzvf nginx-1.9.2.tar.gz  M9 U6 ~. r5 N
$ cd nginx-1.9.2. z0 y6 _; r- }' u. n
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/+ v7 e4 v# f( q$ n0 N& D3 q6 m
$ make && make install
# E8 R8 `/ S0 C# P  g方法二:编译通过ModSecurity-Nginx Connector加载的动态模块8 {( V: y! }. l0 N: E
" m# {3 h. y; ]: F# w! D' E
4 T5 {$ D4 N1 |5 ?; @- L; n8 F6 u1 j3 M
编译LibModSecurity(modsecurity-3.0)3 {, B0 ?+ w% ]. a4 P
$ cd /root
1 p/ _' j9 b% z, L$ git clone https://github.com/SpiderLabs/ModSecurity
/ G" |) g# {! Q$ cd ModSecurity
- o7 Y6 T' `: |7 W5 J# ~. v' f$ git checkout -b v3/master origin/v3/master9 `6 t7 p+ Z2 o) _/ [$ s* Q
$ sh build.sh
! z3 ~1 m, P1 }: O+ C$ git submodule init# k- }% L6 \/ |5 `
$ git submodule update+ g5 U: n0 f% s9 _* u0 Q  H
$ ./configure
7 H( t5 b  F$ g$ make4 |5 D8 O) w& p% T0 n
$ make install5 e9 @$ M/ ^( j: m, v0 a
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。) X4 _7 J" M9 Q
5 S; H% J0 y$ }/ m8 g: ], C

2 o/ y- ]) F' s! Q+ n$ ls /usr/local/modsecurity/lib
7 ?) V3 u! \- w3 C* @$ K7 e2 Rlibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0  \6 n+ \* q; }3 |: `
编译安装Nginx并添加ModSecurity-Nginx Connector模块* i( j: O/ X% X. t/ S9 {: s
使用ModSecurity-Nginx模块来连接LibModSecurity) u( S7 L# u9 h. n; J

# a& {- T7 u5 g" h1 b, M+ I% H9 n- m1 [
$ cd /root
# @# O" h6 v9 F$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
5 T: P* e5 S8 C% p. ~8 [; ?$ tar xzvf nginx-1.9.2.tar.gz
9 z- d. W. E1 E* I* J$ cd nginx-1.9.2
! e+ U& m0 Y2 {# O& L1 w* O$ ./configure --add-module=/root/modsecurity-nginx, y2 D* O) |0 H- e. }2 F
$ make
0 g. s4 @+ A, Q7 r$ make && make install  F, n" F4 ]( s3 @0 a
添加OWASP规则
) ^+ F) Y: ^7 [ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
: o7 V" E* g) b( I+ E* k
' W8 Q6 [7 M4 ]% z7 f: y9 M" ]
* k  n: G6 r4 ]  x5 H, Z下载OWASP规则并生成配置文件
# r2 v2 _5 i0 t8 @  ^, V$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git% C- S5 Y( k1 h. r1 b# C5 r! ^
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/! s+ y1 p, C! w9 Q7 B1 q
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
1 o; }8 C* C9 M" V: _" N$ cp crs-setup.conf.example  crs-setup.conf
7 _7 w$ f# w5 Q2 R配置OWASP规则
- s7 ~; J8 p% r+ ~" X0 I; W7 [编辑crs-setup.conf文件
& \8 K. }) T0 s% U3 k
# M  b' x2 y9 u* @- ^0 ~8 E1 l6 L. v. [$ D$ a3 h) {: P
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf& i! d$ h) o) r! l# I# Y( {
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf" I( \0 _7 w# p$ R# p; y  I- }+ ~
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf  K8 b! U- A, l4 v1 T% x& X( e" M
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf4 A" b& n4 i$ @
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
& }  O+ s  E; c9 D
) v& G4 I- A6 _9 m( U3 q6 H1 K* L4 |0 \7 {! j. G
启用ModSecurity模块和CRS规则
0 q8 n6 r5 ]6 |! d复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
- c& f+ r, a8 e1 z6 \
6 s: b3 q: B* J9 J# @) A6 W2 x6 P" \, K# m5 o1 s
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。5 [5 r, `6 K& F  U2 m) y

- ?( M3 o' t3 C' u- Q- P( v2 m7 p" P* D, S/ }+ U
$ cd /root/modsecurity-2.9.1/7 c' v5 v$ M# c/ O6 f, _
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  $ Q. N1 s$ L  p3 f" ]; m/ I# j
$ cp unicode.mapping  /usr/local/nginx/conf/
+ n$ L+ U  X, `  F# x' g将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。1 `  m/ g; C9 h1 o
$ g, \# ^* }8 F3 V4 y% I  x
+ D& d8 o9 C' J7 n
$ vim /usr/local/nginx/conf/modsecurity.conf; ?5 p& o) D+ o( u& U1 b
SecRuleEngine On
- V6 W: X' N5 J  rModSecurity中几个常用配置说明:: R& g/ y% J/ Z% A! o
+ K$ S: C' T9 p& ^/ M
: O& t. X7 n. Q$ T* U
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
4 A5 o- v: _) f0 v  ]& A* E: C( g/ o0 x% B) {+ Z& L( i

9 c9 B' u' w; ^4 b# O6 W' j2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。9 q! D, }9 g/ B

+ h. ?0 f" U9 Q' N3 ^( x3 h. q: ?' l4 K& K) P
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
7 w3 i) h5 s; t+ C. a" [8 m9 r( s1 }1 W/ `/ N% D

' d/ d) D. d0 a1 i5 {% P4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。( E5 q7 ~/ h- O' l- Z! W
! U  r! }5 g/ [1 z1 @3 S( a

) p; d2 M2 U; M/ U在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
+ R( L% P  Y& N4 m* C: C' Q8 j, D# w

* c) t" t9 }3 k7 a, J3.x版本CRS
3 y. e* X9 P; n, i# q$ U$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
# x5 y' e! M7 s7 `- e# 生成例外排除请求的配置文件
; P2 S& u+ I& L$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf- k. K8 F- N, r0 K# X) ^
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf6 q$ f1 s9 i; F: Z; Z1 R- C  J  [
$ cp rules/*.data /usr/local/nginx/conf5 s. l* c% Q8 \3 Z  w: o5 Q
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。" a% Z- S% @  U, L6 _4 x* G0 z. n
7 k# ?  X( [: q# h$ G4 u) @

* q# i7 X9 ~& [4 X0 ^3 G) l$ vim /usr/local/nginx/conf/modsec_includes.conf
  A* w; e# d( P3 {7 `* {7 b+ A6 R( U$ x) t& F; w# d% p
[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
* k+ s6 B" r  t# ^8 u# k

0 m$ u; W" I- `9 Q. q) f注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
. s( a/ S, t6 [/ W$ |+ c
1 n. f1 l0 L$ d9 e$ N+ S7 |# F1 B; u7 R( {7 t! I1 ~$ g; [8 A
配置Nginx支持Modsecurity
! c& Q. x) N  ]( W+ y启用Modsecurity: ^. u9 _* @" P1 }7 B
使用静态模块加载的配置方法  E# M7 P# W' ~1 u
在需要启用Modsecurity的主机的location下面加入下面两行即可:5 X* Z0 ?- `" N  l  E+ i: J. s
: U: n% q6 q) C+ J0 C( h
( Y) [: ~" J$ E4 j6 R- q2 r3 k. k# `
ModSecurityEnabled on;
5 c2 T3 d7 l8 ^' mModSecurityConfig modsec_includes.conf;
: R' D: r) ^; X3 |修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。  _- ~3 K7 S+ @/ Z, B; m" A+ x" ?6 Z
& ~6 {% H) X& \6 \( Z& b. w
, `' A. N7 k+ Z
$ vim /usr/local/nginx/conf/nginx.conf
/ ^& Q! Y* q/ O5 e5 ?" t8 y' U! v+ Z& U7 b8 k" O

$ o  {9 z  W( [* yserver {
  n9 m6 C, M7 Q# v, R  listen       80;
, b4 n1 H7 z/ U  server_name  example.com;: c5 S; B& G$ ]
9 S- O2 U# |0 X) D2 Z/ u' E: C- o6 f' w; h

: C0 |; D) |8 r( T6 K* d3 p  location / {
* ^3 X/ z3 q# [* p    ModSecurityEnabled on;$ h. c% W# q+ q; m: C+ T4 S. k
    ModSecurityConfig modsec_includes.conf;1 l) I0 |' {: m5 R5 L+ }8 ~
    root   html;
, {* P* A, D3 X# Y4 t6 I: e    index  index.html index.htm;
: A( S9 J0 ?* T& t3 j$ [- B  }
% ^/ u4 f: c; l- a7 f3 g}
" M9 P& o9 g1 e: M) B; r使用动态模块加载的配置方法
6 q7 x" b& L: |7 E7 p0 q在需要启用Modsecurity的主机的location下面加入下面两行即可:9 W/ D+ t0 h. a7 L& Z

  L1 N7 k+ R, b0 _2 o! |; u( u: e$ s
modsecurity on;
% O, |- O2 I7 `& Zmodsecurity_rules_file modsec_includes.conf;
. l$ r; k- L/ j' A. \/ a- t+ ^  R修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
- h5 i  x  E. \1 x  a( Y
. J" G% t5 X  c# G. f. Y
, E7 ^$ @* t/ o9 l1 M4 g4 q/ }$ vim /usr/local/nginx/conf/nginx.conf
7 c, B* a  E: Y8 }3 a" W* R8 W8 v' K7 \9 N4 |$ |( }8 s: L( U: @

( }5 d% h5 P% l; ~, p' ]server {. e: ?3 o8 b2 k# H* S7 f. D3 Z" u
  listen  80;. j1 G* {- B/ a# [5 C9 {% S8 F
  server_name localhost mike.hi-linux.com;
; e  R) Y+ ~$ ^( r" y" Q  access_log /var/log/nginx/yourdomain.log;* A9 q. k. E3 F: e" D5 y

. x; M' w! N1 I: C+ g* ^+ \7 K
& }& g4 X# i8 f$ m' _2 o2 a  location / {; ~: b. Q: Y8 e* }! d& S5 |& z; N, o

) ^6 t! d" Q/ ?. R  v
" \% r, g3 D+ \9 P% x  modsecurity on;
/ }$ V  z* n& Q; e% H/ K  modsecurity_rules_file modsec_includes.conf;/ X3 d8 T3 k% x# S. p; b. j; A, t! l" \
  root   html;6 b8 C$ u! l; v# g& _: ]+ M
  index  index.html index.htm;, Q3 C( x7 b# ^5 l6 ]! f
}
- `& Q/ U- E( S1 l. Z' w7 k+ o; Q}; F/ p2 S% r3 L2 w& ]4 ?1 T* F
验证Nginx配置文件
4 L! \% c1 ]% l4 @( D. W9 ^$ z$ /usr/local/nginx/sbin/nginx -t! g- }, O) g  d( _  F% X
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok) {( i9 x2 @8 h( U* U
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful0 v+ x- g; J9 g4 F0 G9 p5 q
启动Nginx
* o) F% ?4 u; I- M$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
3 y6 S. `* s+ j! Z1 L" ?2 I( t6 h0 V2 q" n

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

! K- u& F9 J4 ^- z

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-4-25 08:51 , Processed in 0.064423 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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