找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10324|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
" Y* t: o4 V8 c7 m3 @: {& x( J) {: f8 g, R% F7 ]  c9 L2 f1 H7 a7 C* h

5 a8 B# s. z! J+ Z9 Z在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。+ i) k8 ~# D; }; Q! X6 ]0 p+ p' {

& @  V* [& Z2 o/ M4 b5 p7 B2 Z: j7 }
什么是ModSecurity; M2 u' R: U4 H$ f, r- D
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
3 T# o0 J2 }6 I& @( g7 a
( B1 ?* y- w7 B2 u9 \: Y& H* S+ J7 q/ W( F6 ^
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。! G$ \2 [5 c( G6 {2 s1 w

9 g5 R5 U0 h. j& U; ^9 Q# D- G
$ P- ]3 s9 o* k9 G4 T* V3 KModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。( d7 O  s- t" e. H7 B- r9 v
- |- H' x* B8 H; u% {0 L8 j
9 Q7 G5 i+ g' Y& u' p- s0 E
官网: https://www.modsecurity.org/9 V5 V5 A0 _( _6 S
# s3 v* Q$ G/ B0 _! N# ]
( Z: x6 B$ o. w" K$ t0 p/ z
什么是OWASP CRS
4 d, i2 G6 ]& `+ Z+ A0 kOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
$ K$ O  r7 d$ A9 Y
, z5 A) G1 ^2 r6 r/ i) E+ {( Q2 L  ]* l5 x# K6 t
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
& }) `2 |7 |  D5 T$ v" c" @8 w6 w

9 b+ z" U+ b  Q! B$ X9 \2 O4 dHTTP Protection(HTTP防御)+ V4 u2 c) E# T/ R% d0 d% _
HTTP协议和本地定义使用的detectsviolations策略。/ s! N( z! x( T8 T, ~2 m, [

8 ?7 P: v# w( [% z  @: z; W& S. S  W- \/ `6 x
Real-time Blacklist Lookups(实时黑名单查询)4 q& n5 B" x* U! J) ?( @; S
利用第三方IP名单。" i# E0 }9 ?2 d, ]1 B9 ?# |  k

, C- o6 W& w. d8 H. D1 e4 ]* C& Z2 K, ?" c/ \' Y
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
3 R2 T* x4 G/ k2 o+ b防御HTTP的洪水攻击和HTTP Dos攻击。
5 K9 B4 U7 W6 n( Q6 B8 ]# J
9 m. D/ o  z! \% c( X& C
8 Q( U  j$ N6 s; P9 I0 ECommon Web Attacks Protection(常见的Web攻击防护)
! j  v% C( V# E0 i: M0 ^检测常见的Web应用程序的安全攻击。
( X' G& Y9 k3 r9 D0 R
( A1 `1 m; g8 M0 P4 l" a& K9 H9 f1 B$ b; m# u/ [8 E
Automation Detection(自动化检测)
3 L# W# @( _9 t  A/ d$ I' L检测机器人,爬虫,扫描仪和其他表面恶意活动。
1 G/ Z4 V1 G6 G2 E$ B- K! d0 |
- I8 F  K7 ]8 s. j* X4 H& h( R/ U- L% P2 ^
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)# ^/ A) M& T, l1 z/ I
检测通过Web应用程序上传的恶意文件。2 ^  W$ y8 W  R) U, ^5 _% b- n, I
2 U5 F% t. l% {: j" u

; L; ~% W4 ?9 bTracking Sensitive Data(跟踪敏感数据)  v! S  Q* \3 m
信用卡通道的使用,并阻止泄漏。
& y3 x% r' X, e
3 V9 V: f# `! a: E0 i) Y. `0 v0 L: b! G
Trojan Protection(木马防护)6 M& w' R& t7 l! Y1 M2 j
检测访问木马。. s9 F( }1 x0 o: C& T# C0 h! S% [

; c* G, ?3 P6 h& b' {" G
. w$ ]! y2 g5 gIdentification of Application Defects(应用程序缺陷的鉴定)* ^! j6 n2 A& a0 @! c. ~% P
检测应用程序的错误配置警报。" a; y" t- v! H
( W7 t  b8 E! d3 S9 e+ }: Y

! f) B7 d4 e' \, b, p' X# K" z. l0 GError Detection and Hiding(错误检测和隐藏)
4 Q* k7 X' O, `; \检测伪装服务器发送错误消息。
  \' w7 W' X4 m
4 m. d& ]- M$ p  r
0 I) ?8 b" G& [3 a6 |( ?安装ModSecurity& M. h, a& `$ {6 K: O5 R
软件基础环境准备
  H9 c. w0 O( l" q下载对应软件包
7 O( V% E) d% u/ a; M7 J$ cd /root9 F" j+ G5 D7 v2 ?8 k# R. W
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
) z8 f  y* H4 \: @- Q$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
2 ?/ Y$ X: {- w2 k0 x; j安装Nginx和ModSecurity依赖包
. C' D% [$ J9 L$ KCentos/RHEL9 v9 ^4 Q: }# C; l8 R2 m
4 [4 K( E0 I: N9 K. s$ G
& r% t5 j' |; j
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
5 x( t% ?- x- x* _Ubuntu/Debian' E/ A1 H: Q" C/ i7 S* D, T) N9 L

- u/ l$ h$ I% i7 f
/ J0 w( u$ {" B/ g& H$ 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; O, W2 e4 L+ V9 x! s8 U8 a
编译安装ModSecurity( L6 M, _" f" _1 X$ r: W6 e) h9 R
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。. k. K' O- ?0 }+ }* a6 j6 A, v

% _# R5 P& F! U" m
' Z1 `* @( @: M/ G% k方法一:编译为Nginx静态模块, o  A( \, F2 ?' i

) @# R1 O0 N9 G+ @/ L1 h5 e9 f7 d4 ]! [1 n) ]) [" N& X+ t2 _* x
编译为独立模块(modsecurity-2.9.1)
* x7 ]6 b1 D! Z; W, C$ tar xzvf modsecurity-2.9.1.tar.gz0 Z; ^5 V$ u3 F* f! F- r
$ cd modsecurity-2.9.1/" Q. p' \' k) B- c' ^  @
$ ./autogen.sh% Y: p: e: E7 Z8 C
$ ./configure --enable-standalone-module --disable-mlogc- L$ k5 s9 L1 D& N* r" J
$ make6 f5 f8 f6 E+ y- P% {6 n( h
编译安装Nginx并添加ModSecurity模块* K/ m! m3 e' p* ?$ x
$ tar xzvf nginx-1.9.2.tar.gz+ E8 [, \; A  f7 @, ]% M
$ cd nginx-1.9.27 w9 _9 o1 s. Q' k3 u4 V5 b1 X/ \3 l
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/" I9 S8 S1 y& C% ~
$ make && make install  W$ m" Q" s1 k* @+ q' u# K, V
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块2 E: j3 G- c) @: @0 _9 _* O
6 l1 V( [) ~  T

7 n) ~3 z3 k! @$ m# a编译LibModSecurity(modsecurity-3.0)  [: Y0 \8 N5 q) P( m
$ cd /root
0 H! ]* R; O  a5 b! i$ git clone https://github.com/SpiderLabs/ModSecurity
( h, Z, j0 D3 x( U$ cd ModSecurity! n$ ?4 C0 p3 [( b' x
$ git checkout -b v3/master origin/v3/master
* m' L/ @  m7 o* ~0 z: Y6 U$ sh build.sh
, K) F- m  P5 F8 ^1 F: V- V$ git submodule init- J3 J: V0 [1 B; e$ P
$ git submodule update
2 j" Y: r0 j  z2 {  G. y$ ./configure3 {0 V% c: f" I, {( R" h. h% L
$ make7 K. K% X" W7 e& w% A. A
$ make install
* \- X$ ]9 N8 ~% L" N) n3 F2 iLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。5 z( k( @& n) B* z
% X2 E/ G: n% l! {- B
+ r7 @5 O0 U6 c) H# _- ]
$ ls /usr/local/modsecurity/lib8 a( \' V+ E* A1 x5 i& b- \: s
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0  ~+ @1 P( V9 e% U/ w# g
编译安装Nginx并添加ModSecurity-Nginx Connector模块
( b: _- n) S; |; }' n; n$ G/ c6 w使用ModSecurity-Nginx模块来连接LibModSecurity- n0 g0 L+ W- G" o

/ J* J$ B- I; a: x  L% O! l- s, O. b; N
$ cd /root* ?% F9 U6 G6 M7 B2 q
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
* h9 g" K1 ~, d5 \$ tar xzvf nginx-1.9.2.tar.gz
+ D% [6 D. \9 ^3 w. @$ cd nginx-1.9.2
3 N; n+ D# R. d/ S8 j$ ./configure --add-module=/root/modsecurity-nginx
, F3 N$ w% Y# n$ make' L1 H' y; @: h9 n
$ make && make install9 r; I* w) a, y# ]1 d
添加OWASP规则" U# [/ ?% v* M/ t8 m$ l& F' B3 G8 ]
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。9 K  q/ `# Q9 x9 s. O& ]  M. H
" t0 x+ q2 A4 {! Y) O0 F( ]1 U4 P
  {8 i0 o" i3 e4 z+ A
下载OWASP规则并生成配置文件  a2 K- z' e* X
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git% x; |# v3 p5 g6 C" Y3 z: w! B
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/, _2 N! B! N( @" z" L
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs2 {) p: k4 t" r* T7 v4 t
$ cp crs-setup.conf.example  crs-setup.conf& e; m+ p' s; ^7 ~7 x! t
配置OWASP规则
, w$ K' X+ v9 H5 X编辑crs-setup.conf文件
' V, y- ~: I  v/ |( a; v: X4 E7 U8 [5 A; w6 E
5 T. D# W" ~9 D- f- N
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
, B7 I" E1 B5 y& [3 N9 D$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf5 c, e5 i5 v1 w$ ?; k
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
. Q5 Y0 [3 t( \+ B( s+ c$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
, S# ?1 S& z* i默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
8 j( B5 I, A/ m6 p- [: q( g# C2 `& y9 ?, O

2 E$ F- _% G8 e& c9 \启用ModSecurity模块和CRS规则$ d9 D: A/ _$ m  r2 R+ n1 i9 R
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。( t& v* o6 [$ T7 F  K* Z) c8 b

7 R, ~# f+ j4 c; S6 e. u% K! C; n% t8 ]$ E6 j( @% t8 Y
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。, Z( [+ f5 H; t+ d8 d

1 Q0 D9 N* t: _  s# {- y% Z
& O" `7 E# D" E8 @7 ^$ cd /root/modsecurity-2.9.1/2 n. w8 r% B9 a) ~0 _
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  / ]% x) w  y4 v& U$ ^/ A% P' |$ l
$ cp unicode.mapping  /usr/local/nginx/conf/
- t7 R/ g2 w& f* b7 \将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。1 l2 d4 q% w; \8 P- z7 C
1 M: u9 y" d! U) U

( K9 z0 r' P( w  F$ L$ vim /usr/local/nginx/conf/modsecurity.conf9 \( E5 J% L2 C( x7 s( }) f
SecRuleEngine On" Q! i* \3 U; c/ J% l, g
ModSecurity中几个常用配置说明:
7 f! s! O5 L/ ]8 i" ^; K, S( i, y( `4 ?$ P9 O' @4 M
9 S1 y* o! o$ v+ |6 u
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
) R  U- B0 y  ~3 i6 ^# |! |, u, Z& r. k! W+ c' E

$ j: n) O/ g' k6 ^8 d* }2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
# b- n3 ]# E7 M+ F9 `- t  f# F& o

. a  N+ t& C( G; f9 Y3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
  L% m. e+ k7 E5 }
# Z4 y- Q  P1 G- k3 z) N4 Z
6 d3 m& F+ b# L, s. l4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。- }! q( B9 Q& R1 q

; p2 N( H4 h& A0 h& Z7 Y& A' _/ I  Z! R" U- q0 K& k: N' y
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。- h4 K$ o2 I! O  o

) f, X& _2 _: M5 g6 R( K
  Z) x/ x) K& Y3.x版本CRS: k% f) X$ ~; H* D) V
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
3 @; L. Q8 b) i# 生成例外排除请求的配置文件0 b( c1 \" a! z9 n9 ]" N
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
- I, u$ t4 K# I. |# C: c+ ^: q$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf  J$ U/ k+ o  z8 `7 }5 m
$ cp rules/*.data /usr/local/nginx/conf
5 G  U) M" c7 K. J, f! Z为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
6 e( w5 V  }/ z3 ~+ I  e$ x
- |  q% u& y5 h; m" d7 B: [
" a2 c* Y# ~+ J8 L# L' e) [$ vim /usr/local/nginx/conf/modsec_includes.conf
( m  L- [2 B' ]$ g  w, B# M* p
9 ?- P. L7 A3 d' `
[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

0 r, ?5 _2 {* n
/ Q. w* Y* G, L) c注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
/ s. R; B, H+ W3 c! `
: Z; w1 H  o$ `
% J( l' Y8 e) A% ?配置Nginx支持Modsecurity9 g" h% d8 u% `! o: v* l
启用Modsecurity8 w* ~8 [# q. q. T' ^$ G8 J4 j
使用静态模块加载的配置方法
( K8 s! L3 @7 a9 f在需要启用Modsecurity的主机的location下面加入下面两行即可:
8 a* p5 x7 @: ?2 H# z0 o, z( o/ I0 p
! _5 m+ i6 Q2 @9 Z& |  A* H
ModSecurityEnabled on;
* |; F; w% b2 m- CModSecurityConfig modsec_includes.conf;
, d0 M, F& m/ K4 B修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
- Q( ]% k. m# C& @7 `* Q1 d3 i" K3 g9 u& D) K9 p' e
( l0 o. x  o% w" b8 S. ~
$ vim /usr/local/nginx/conf/nginx.conf! b- H; {# s7 J

7 Q8 R' H) n2 i4 |& N
6 ^" x+ V+ o9 t# n1 kserver {
. }( o. p9 `* N  listen       80;
* q, K5 m. M6 k, O0 h  server_name  example.com;! k, W6 e: k1 B

) |: g3 b  E, Q  `- `4 x; U6 w9 ~6 }6 v# q
  location / {) U' e+ p) U  w9 O) m! m9 F1 k/ g
    ModSecurityEnabled on;/ E+ _8 k6 j/ ?1 t) c7 }2 z
    ModSecurityConfig modsec_includes.conf;- x" L- [$ g- u5 g: f
    root   html;
* h$ l* c* y. k# d  J    index  index.html index.htm;" \' P" O# |* [% O' M
  }3 {* O1 O2 y' W1 n2 O; V
}
. e- H- B  l, ^2 E  y3 ~使用动态模块加载的配置方法7 |  e9 a" h9 X3 n
在需要启用Modsecurity的主机的location下面加入下面两行即可:9 O- b+ O5 N' c' A# p

, d1 ~# o3 [: `6 B) Z0 x; W& b9 ~* G5 g+ ?) R- q
modsecurity on;
0 @* E/ O, X9 C7 s, O" jmodsecurity_rules_file modsec_includes.conf;$ C2 t, A9 V  [
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
6 Q4 m3 p, H4 e, }0 D8 P
2 l, p6 c! c# p, B; x) z7 z8 p& }# Q  z0 X
$ vim /usr/local/nginx/conf/nginx.conf+ R: h( U3 ^: O0 w3 y( q. b! K( p
8 e6 z& f0 N  M

' N# }  d& e1 Z' Z- Z) Z- kserver {
/ W; X) I5 B6 f9 C0 h  listen  80;
& d! l! \1 Q, W% U( d# @0 \  server_name localhost mike.hi-linux.com;) F8 J" c! c/ O$ G
  access_log /var/log/nginx/yourdomain.log;
+ k1 e& W# J/ x% Z/ Q" {% {7 S" f# V6 F& f" V* Q: K  O5 W6 ]$ U

6 B$ p1 h( d; R( q- g  location / {
8 Q8 c7 i% t: y$ M
; ?2 y2 ^" {0 |( C! ?; g  p/ U3 W' b2 ?% l, O- z6 K! Q
  modsecurity on;+ T) R% h( G$ d* E( V. x
  modsecurity_rules_file modsec_includes.conf;
7 ]# U7 U' H) P$ F% v& Z. B  root   html;, E5 ]; G% z1 p0 _
  index  index.html index.htm;
4 C7 S) U: M' ]  u5 U1 ^' ?}7 E! w5 T( N3 t- ^4 s' n- D+ r  a
}
* p2 X  D; r7 t* {验证Nginx配置文件5 a: |* B% q0 r+ y( {( I! G
$ /usr/local/nginx/sbin/nginx -t3 T) N* c! b! J) S; d1 I( F: H8 ^
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok& p+ N9 P' i! e% J
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
+ m9 z: E+ [( J. K* \; |启动Nginx4 b% C3 M- J" A
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf8 }. E9 L( E: a) Q& ?( a

) s! h4 o" V) {: `* D' s( C$ ^

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


$ `) Y" [) M: p+ c+ r# ^

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-2-25 01:40 , Processed in 0.086262 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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