找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11397|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
* l" G; S! n2 b
% I) h( p7 S) J% f+ m0 q$ ?
) [8 O5 A- \9 z; H- f( g: r在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。2 d( w8 D: A6 p4 Z9 ^
$ P8 p7 w3 p2 m* b

4 \7 F; |( ~' ]8 i什么是ModSecurity! ]8 V* `- `" v9 Z
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
/ B6 d# Q; X: I' s; r5 g) @2 b7 ]* a

3 o7 k% t3 Q% k' qModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
: J: l2 z  z5 H9 ?$ M, y* A* ~" G/ J; z& z- G' O* Z2 J! {# e: o/ c' s
/ @) ]& ^  f% D* j2 H1 D% K
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。- F& z/ l4 x* O

# W- C  n7 w4 A9 Q1 ^( {! I* c
; x4 h! ~/ G% E3 \3 z官网: https://www.modsecurity.org/
9 B% X; Z2 j; U! R: Z+ h6 Q& L" H7 x
- f& v6 o$ T) n/ d1 c% u
什么是OWASP CRS* h8 b2 n. e) F" c
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。  G1 v/ K( I/ b

0 ?5 x* P* h9 f
5 u6 M. i3 F8 n* p8 |4 W: BModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
3 p, w9 X2 O5 |& M% Z8 F8 O- i1 O* H6 f& K0 L3 `( f

( X# |: T* u- EHTTP Protection(HTTP防御)0 u3 A9 j; z- |9 }" {1 \
HTTP协议和本地定义使用的detectsviolations策略。
0 S% P+ |: e$ Z. A& i; z9 u4 ^) d) U( r$ P: t$ ?" o( w

; E$ _0 W% Z3 C8 p! oReal-time Blacklist Lookups(实时黑名单查询)+ S7 p1 L8 z3 ~. J* h2 \" n8 L1 @; G
利用第三方IP名单。
7 b9 ]. Q& G! y* v5 I( s6 e, E6 G
; }( S, S5 Z) [3 F1 D: O& E, f3 h7 f+ Y& Q& S) k  v) o
HTTP Denial of Service Protections(HTTP的拒绝服务保护), W: n1 D# q/ _* b! A
防御HTTP的洪水攻击和HTTP Dos攻击。
# s6 A& v8 l$ H# B# B' D9 k' c4 U6 J0 L

0 ^+ [! b, F. ^) c7 Y- OCommon Web Attacks Protection(常见的Web攻击防护)  a4 y( G2 X- x3 A' o
检测常见的Web应用程序的安全攻击。
0 a4 I' j7 n8 ^
) t7 ~7 B+ ^1 B# R' ~, T  z8 ?
: [9 n4 Q9 _% G' W* R: sAutomation Detection(自动化检测)
# Z; t  D9 Q! {. f检测机器人,爬虫,扫描仪和其他表面恶意活动。
! i% j& E1 `' y1 i$ U& l) Z: i0 j, ?
# \' J4 W2 q4 D5 V7 d$ F! t; ^, w
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
) ^# T( n1 N5 ^! k检测通过Web应用程序上传的恶意文件。1 E3 }1 s4 s5 O+ t" V
1 x) Z6 E% U0 b+ _

# O' V7 [4 {- F0 XTracking Sensitive Data(跟踪敏感数据)! o0 y- z6 b* S4 l
信用卡通道的使用,并阻止泄漏。
" }& X" a- Y. x) p4 K; A: K9 `# ^; G" G4 N; W
/ z; t( A' G3 l+ u+ u
Trojan Protection(木马防护)
. x/ `3 @/ ]3 }; A% Q; m4 D检测访问木马。% H; d% \5 K2 \
& S, B( y* R( V/ l6 J

4 D( j. b( ^' W( z$ g; D; XIdentification of Application Defects(应用程序缺陷的鉴定)2 Q* h0 l/ E' Q; n! n
检测应用程序的错误配置警报。* `; E+ N# {% r) k" Y
% h. }9 F% X7 h. @0 M* ^. ~

) f; D9 j% K+ g" c3 h& gError Detection and Hiding(错误检测和隐藏)! K3 f. e, K4 ~6 K
检测伪装服务器发送错误消息。1 p4 l% U* n  b8 B

6 I, d" A" c* t- D2 ~5 x
. o' N6 Z- ?8 L, O5 @7 s安装ModSecurity
3 B% m9 S+ C4 P# F+ m' f# X+ ]软件基础环境准备
% w" _9 A6 [) h, G' E下载对应软件包
- j2 u' E3 f. \9 D+ T$ cd /root
4 ^2 g% m0 a( K2 r$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'9 |# C+ P4 z" l: f9 N
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz* K+ N7 s, q! T5 T& ~8 c
安装Nginx和ModSecurity依赖包
, i1 w7 N# @' l! ?; ^  tCentos/RHEL9 c& S6 @3 I% S% p: Z4 g

7 Q+ Q! C! \5 j$ y
' @5 O. s! Q- [. J" V0 ~2 ?$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel* E% C! h6 h1 X7 c+ q( H# Z3 N
Ubuntu/Debian$ v4 L, U$ X  Y& H6 h

, ^1 j. k5 A' b6 ?2 W
: H6 a+ P# Z/ 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++-dev) {, G% n1 c% s
编译安装ModSecurity7 E# t8 [1 R$ `: O: l( r. m6 |
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。1 K4 `3 X4 w/ P& f* c6 w, j

4 \) S& @& s% w  G' |+ y- M
  r7 F9 [! \2 ]0 P% |方法一:编译为Nginx静态模块
( z6 }- u4 c; t- t* F7 K
; r* K# e" c3 d
, |% N6 x$ s9 d2 N1 [" A编译为独立模块(modsecurity-2.9.1)8 S& ?, _* A: ~* ?" [: o# u! C
$ tar xzvf modsecurity-2.9.1.tar.gz. H7 D* q5 r8 R" Q* t+ q. W3 V
$ cd modsecurity-2.9.1/
4 @/ D; z/ w( s0 \3 L+ W, l$ ./autogen.sh; P/ E9 }7 f6 ]8 X+ y
$ ./configure --enable-standalone-module --disable-mlogc
. u+ ^, Y1 ~" W1 h4 A6 v$ make/ O0 f  @6 A9 l4 Z
编译安装Nginx并添加ModSecurity模块( M9 J" v8 D* c* h7 |" i! N8 h
$ tar xzvf nginx-1.9.2.tar.gz% z2 ?+ J, a% |$ R9 i& T
$ cd nginx-1.9.2
2 ]7 b7 ~- n1 T$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/' f) A: V, |1 A* k) ?; {
$ make && make install
! l& S" e4 n/ u' \方法二:编译通过ModSecurity-Nginx Connector加载的动态模块, v0 k  v( W; R% R! [: l9 Y- B

3 V- O3 ~3 s/ k$ S3 Q) C9 O
9 l9 l9 l) I" W4 M编译LibModSecurity(modsecurity-3.0)
$ o, p! \& e5 O$ cd /root
5 u: z8 D) U) ~+ z$ i$ git clone https://github.com/SpiderLabs/ModSecurity# g0 i5 F2 W3 L/ u
$ cd ModSecurity
2 m! q, r" ~% J0 Q4 `/ P$ git checkout -b v3/master origin/v3/master4 M  L9 K9 U$ F# ^& n$ P
$ sh build.sh
& Q% i  o# W- S$ git submodule init1 m" z  H* |& s& b; b
$ git submodule update
) }" }/ p1 E9 I6 [, _$ ./configure
3 L; ^1 [6 h3 {7 E$ make+ F( D; T, a2 \2 ^* k1 m4 \5 v
$ make install
6 W' s3 h3 I  }* k: P4 r; Q) GLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。. O4 E9 S5 ^0 q% p

* I( w6 S4 Y8 T! |- l4 w5 T6 B( L# S" r' f6 _; C+ W+ }
$ ls /usr/local/modsecurity/lib
& k; Z2 |% P  U$ \* ulibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.09 D! ^) k, [( ^
编译安装Nginx并添加ModSecurity-Nginx Connector模块- O. g  o& r8 m
使用ModSecurity-Nginx模块来连接LibModSecurity
( o. W' ]! A) K' {- Q3 E$ s5 }. r3 Q; Y3 e1 _+ p! F5 p
' z5 ?) ~( w% n; d
$ cd /root
/ _  \: d9 t! C1 `$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx: f" l7 a0 E# M: r! w
$ tar xzvf nginx-1.9.2.tar.gz
* E- ^; I/ y8 A$ cd nginx-1.9.2
7 C8 q7 P" `/ p8 G  v3 z5 l$ ^8 q$ ./configure --add-module=/root/modsecurity-nginx3 g$ Q; w# ~) o% X( W
$ make/ @( W9 N" L5 r4 S5 A9 Y6 O2 @9 @
$ make && make install, o+ c4 a% S* x- [
添加OWASP规则
, `% ~9 v6 U- tModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
6 n$ G! ]7 v3 \. e
) D8 h, e- Q# {8 \
1 s1 ?8 t4 r8 C8 h# y) S2 \1 \1 g+ L下载OWASP规则并生成配置文件
, T( T) r' O' T; r5 E  P% Y7 t$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
1 Y( }( u6 P+ y6 n, V, H" N) q$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/  U, H& Z/ L2 C6 o9 l! h: \
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
+ D. x) l' e# j  b" Q3 `$ cp crs-setup.conf.example  crs-setup.conf
' S+ V2 w5 o. P: ~9 |, C% m配置OWASP规则
( b& Z1 A) _, a( a* d编辑crs-setup.conf文件5 O+ ?8 k+ h2 f
) K$ }& u" a2 }; \$ m- J  J) @! c
! N8 L' Z& Y% M. Y9 Z" o  c) {1 _
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
7 |$ a$ p2 n- i$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
9 S# t2 q9 F1 D; e. d$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf9 A  M( S" g) M: J& a1 c
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf" d5 x+ k5 }0 |# f/ {' k/ ^
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
6 A% r3 ^6 Y2 {% N9 n( S. C
7 ^. U3 a/ @; d) k- \6 z
# ]' ^4 u$ j+ z5 e' _4 A( O3 Z+ r启用ModSecurity模块和CRS规则
; v8 g, s( D  C复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
* H( q+ u! v, R! _( W8 ]
6 z; r; j9 g  F
. z8 {# \5 o8 F, f5 u( Q8 f  Xmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
! P" K  w, c" e% q
- i$ T4 A% s. j& `6 L: k- P3 y, i  J  c, P9 ]- D. Z7 Z* o
$ cd /root/modsecurity-2.9.1/( e4 F/ g2 ~' `) _4 D
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  ' B; l2 R+ R! y
$ cp unicode.mapping  /usr/local/nginx/conf/" L4 o2 A1 z% i- T: O( W
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
/ x4 \7 i$ W  W1 F
. ]$ P5 K8 w. i) H+ }$ }* K* d# P3 z0 F
$ vim /usr/local/nginx/conf/modsecurity.conf% m( B, }$ z" x4 u+ B- @
SecRuleEngine On
7 j' m! ^* Y& LModSecurity中几个常用配置说明:% h$ J- s  l' _* Y# X
8 x9 |; M6 o6 Q! ^; d* ^  ?4 P
* G* k2 Z8 N7 |# P0 J
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。. a3 V3 h. A) n: _5 Y* e
+ \, t9 |; D& S" F2 E

" S9 |, T* {2 X9 R6 N7 Z2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
  q' n2 p# f9 d2 d' Y& |  ^5 R0 l3 ~

" I9 _+ H; _# }% n. p0 F( X3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
+ D* x$ E7 x2 O- b  `5 A
% }' f4 L* }) n5 L( u0 @
2 R% x) Q" W' f% K. T4 V4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。! L" m- W; X, o/ ]3 i
& v( A2 T3 |2 l% x- r9 I1 A4 {& Y
9 s; d  z! B3 o
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
; @5 ~' H$ Y4 C! X
6 j/ u; ?& x+ \6 m9 f4 y
: v) `  @9 z' p0 @, J6 P! U3.x版本CRS! _1 e% ?' o) y; R5 W2 E. T2 W: A
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
6 t  b% ~2 a6 s1 U: I- r7 F# 生成例外排除请求的配置文件
6 t  M4 g( A; B  N0 X$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
* [* W6 d  i2 {5 n; z5 G* r$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf2 L& i; T* o. G8 V" |5 h4 N6 g
$ cp rules/*.data /usr/local/nginx/conf( ~3 {' ]0 X3 w6 o5 j& A* Z
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
3 v/ c8 z0 {7 w' J: v9 C4 l
. m/ e2 @- M) n8 P
) `- ]) m2 [6 @% M% a$ vim /usr/local/nginx/conf/modsec_includes.conf* I9 P. |2 T) {5 P2 c/ W( _5 [- X
' x& P; R+ z. r" \- h! Q5 o7 F8 A2 J
[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
/ i. ]1 L2 E: ~; y$ V) |0 [" H
. |$ |; m  i  N7 D9 i
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
) a2 g1 ?6 O5 ?5 `
7 D- d6 a! B1 G' X  C
9 o1 |0 u0 A& y4 I* j配置Nginx支持Modsecurity2 a7 q% N1 @$ ]3 j! K7 ]
启用Modsecurity
! u5 G- W, @+ N1 d% L; W8 _使用静态模块加载的配置方法
* q$ g8 H" B3 y, V在需要启用Modsecurity的主机的location下面加入下面两行即可:
" c( F3 ^+ S1 c- y1 U+ c2 z* X8 J  Q

0 [9 x8 g# S, p( _' W; C% o! PModSecurityEnabled on;
  c7 P& O3 I. O0 c, b- G3 @8 W4 VModSecurityConfig modsec_includes.conf;
  t/ F/ \- x' Q8 g修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
) M& T/ N4 K2 n0 s  ?0 n- Z, I+ T# Z
- h. s1 }$ }/ _
$ vim /usr/local/nginx/conf/nginx.conf: Q, v6 X3 r+ V; r. _8 ^; w

0 y2 ]% W; K% H5 A3 B
" Z% Q+ S1 J' r# c* j4 e7 xserver {/ y2 A; j/ x, e: u: n
  listen       80;
) ?- }" ?( Q6 i1 }, T* b  server_name  example.com;
/ i$ c: ?  {5 e* E3 l9 w! j2 D3 {: f+ e- C) H  b

( l+ w+ e* R* A" o  location / {
0 k6 F4 i2 A  c) J- w) e& Z    ModSecurityEnabled on;. m. O7 y# M$ Y4 L
    ModSecurityConfig modsec_includes.conf;
1 P# b# j9 `! \& |+ m    root   html;2 s+ q9 j* H# Q9 N7 H
    index  index.html index.htm;
# G* `* ^' q8 t9 H/ L  }$ J7 N; _1 M- B( q& D' o
}/ w% y# o4 c/ n# n: H# L  c
使用动态模块加载的配置方法
5 M, U( ]  U1 t; N4 `, W在需要启用Modsecurity的主机的location下面加入下面两行即可:) j) ?" `1 S6 n( v
" N; j% b1 ^& M! U! b- T; S
1 r( d0 k. L2 r% O& \: ]1 ^% O$ b
modsecurity on;
; X+ f( }6 e0 E7 Umodsecurity_rules_file modsec_includes.conf;1 N8 l6 Z- m5 Q. m5 a
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。) N$ B8 X: {: L  Y; P, E4 f) H

+ R" n: n# Y% ~1 Z: K$ c; `' w% o
$ vim /usr/local/nginx/conf/nginx.conf. S* Z1 X) H0 H9 q

" s$ F+ l0 n, x0 g7 j2 A) g+ C7 V& Y$ d
server {2 l  \$ E; G1 S/ ]) ~; Y9 i$ Z4 H
  listen  80;
' R" v, J! Q3 f* H  server_name localhost mike.hi-linux.com;
1 d/ N/ p* w; v, ^3 F( h1 A8 I  access_log /var/log/nginx/yourdomain.log;0 C# S% @0 M) T! f# G9 @; Z. I
/ o1 M5 }7 c0 M. h" m

' t, u# B% N. U8 E. A  location / {7 z4 \5 f" `( J- q! _) `; ]$ U
$ E/ x7 `( |+ ?8 u* L2 Y

8 d  b( b0 ^4 T* ^- w7 a  modsecurity on;
1 B; H. F& A6 o5 ~9 o# l  modsecurity_rules_file modsec_includes.conf;6 o+ W9 ^5 l- u. U% _- s
  root   html;
+ p) S2 N( b, P; f  index  index.html index.htm;
2 }& O. m3 `+ \& l4 o4 {0 N}
  F: N: u( Z2 S* F% h; s2 l}
) R8 ?" Z  l+ u! }2 Y0 l- ~验证Nginx配置文件
: i$ W+ n; _. A' S- U$ /usr/local/nginx/sbin/nginx -t, X; A' R( e2 B2 ?: `
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok5 H. M8 T5 K$ }' N( m# R
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
6 k  ]. Z! L8 }/ r% e) E8 h4 `启动Nginx
  E  F4 Z+ k8 t1 h- `$ b) e/ b$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf  s1 n$ p- Y$ O$ H% S6 x

# c9 R8 {! ~9 K5 N" j4 g

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

1 ~6 x) F/ _$ k3 W# _* J: {% ^

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-6-7 04:16 , Processed in 0.075617 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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