找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12322|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
1 x; E. z% S& U$ n2 _6 \' r4 R$ T- v1 y6 S9 F% ?* T

9 i# ?; ^0 I7 o! x8 T, u在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
( K0 r% [6 A* z5 O: }) D& ]+ l) F' Z

+ p: q% Z' x- `( `; [! n6 F8 N什么是ModSecurity
* c. ^$ G# R! w! O5 Q' K6 cModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。2 C5 q0 E( u7 v) V6 B
) t$ [" y$ y7 q1 b

1 e4 }9 Y: s$ F  LModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
& H) _: h, G/ j: D5 f
8 p0 l7 u/ B" Z- v. ^/ ^# \, u; v1 T+ b3 N
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
8 }. q* ^& O6 [+ w+ A' e: B
6 \+ K& Y' e  g# [# k* Q$ f% a) {6 L) u# r( h
官网: https://www.modsecurity.org/
- b& i% C9 \2 U$ W% E  S5 \5 c8 e" y' Q
  O1 x5 Y/ }4 D4 K% z" E
什么是OWASP CRS
6 a( s  w) ~& J' f0 s1 gOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。' _3 C% H0 g8 Z  z2 j5 ?

9 K$ ]$ E# {6 S! B3 u' N. P  X5 K' U2 k0 p. [3 P
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。2 Q: t% [$ q% ^" x4 _
( g% o- A9 F, C' f
4 Z' ?5 D8 s7 b  i" {; K
HTTP Protection(HTTP防御)1 Z! T! g% q5 Q7 p( ~
HTTP协议和本地定义使用的detectsviolations策略。
* Y3 K% u# n' S0 |& ~7 i! z
- v+ N1 r7 _2 g& M# q! ^- ~: x* h9 `: V/ |
Real-time Blacklist Lookups(实时黑名单查询)
- g! _5 m4 ?* e4 c利用第三方IP名单。
7 A- H; s1 ~) L7 E
1 o$ q* `9 h0 k" U- W+ p" H9 C3 _$ b% ^1 [" s4 Q
HTTP Denial of Service Protections(HTTP的拒绝服务保护)" u* ?4 L. X7 \  ]
防御HTTP的洪水攻击和HTTP Dos攻击。. M4 N- O- Z( l0 L
9 ^2 R. r. z3 u

+ ?5 k6 |: v6 z2 SCommon Web Attacks Protection(常见的Web攻击防护)2 z" o! I- j% U3 t3 M& ]
检测常见的Web应用程序的安全攻击。0 J9 T0 u$ l# |: r& D' n

5 x8 ?$ W  E  a7 I& {9 D
6 ^/ m. h: d% E4 W  wAutomation Detection(自动化检测)
9 j3 v9 w5 o' z$ @+ @检测机器人,爬虫,扫描仪和其他表面恶意活动。3 D, }; m# ]( \  t7 I3 k- `
/ d4 V3 A, L" O; c7 G
7 g, E- _/ R% U3 N. x* P
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
- R6 u2 z0 o$ o- G, Q1 v3 E7 R5 L检测通过Web应用程序上传的恶意文件。  P6 |" h! j8 x8 `/ C1 s

% J3 A. k$ Y) V. r5 S" }1 z7 m* g& _& v0 E. S
Tracking Sensitive Data(跟踪敏感数据)& Z- b: i8 m% t3 P7 {
信用卡通道的使用,并阻止泄漏。2 Z# W: t0 k3 k
9 Y6 c$ O' J" n) o4 V
8 M4 P4 Z$ r2 S
Trojan Protection(木马防护)
' U! t, X$ ?0 \! H6 J检测访问木马。/ r- q; v6 ^! m  |

9 s/ k+ `, d3 ^( ~5 @
5 K6 `6 b7 _3 ~& c4 b" bIdentification of Application Defects(应用程序缺陷的鉴定)4 i+ ?! h& Y4 U& e+ E! E6 x" ?
检测应用程序的错误配置警报。
/ h1 I1 U" G3 |$ C! g  U- X8 e5 p
" p" c$ D: a1 ?2 i- U+ G6 P! r; J1 U4 g
Error Detection and Hiding(错误检测和隐藏)
3 O1 D/ `7 f! ?9 C1 ~$ k1 e检测伪装服务器发送错误消息。
' I* C6 N8 C8 [9 s" Z( i
5 P5 b6 t: G' r; d( N$ o: ?# x+ `" _8 s  ?6 e% z( F
安装ModSecurity) _# o. b1 h* P( r# Y  M  E
软件基础环境准备
4 V/ B1 L, ?- q下载对应软件包
0 c0 R$ @( t+ p' e$ Z$ cd /root9 m2 L6 ?+ ?: i$ J' _" n( `9 R
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'2 ^: I6 S# n1 Q' P
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz# E) |9 ?3 H8 k* k4 Y
安装Nginx和ModSecurity依赖包
$ k# j: f1 {# D2 XCentos/RHEL
- G# G  t2 v; V/ V
  y$ Y5 D& b5 i, [- [- _4 f, b. c- s% v& Q
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
) d5 H3 k* c# c( D/ y4 |. ~Ubuntu/Debian: w" E/ M8 A3 v# N1 R% c! K
# N+ ~/ ~' z+ _. C% V" _

6 K9 q9 y: {4 N/ O* ]. U. a# E$ 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
* q5 o4 a; S6 i) W& }0 @; S编译安装ModSecurity
8 U  t0 c$ g  kNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
, P6 h6 r$ x( {4 x7 Z  y; P/ U2 x& V
, {/ q3 \% V- J% Y' @) x* o# J; `
方法一:编译为Nginx静态模块
3 h$ O% Y4 U4 q& Y, t# L
3 N( G/ `' r# I: o! F7 q, q3 c0 w1 Q1 c1 |0 |! J) z3 P3 z
编译为独立模块(modsecurity-2.9.1)1 L9 k. m/ K( w4 Q
$ tar xzvf modsecurity-2.9.1.tar.gz1 T7 W, X3 D' R5 ?
$ cd modsecurity-2.9.1/
0 H1 l" Z9 a2 y- F$ ./autogen.sh* k" o( a) W" W& m: @
$ ./configure --enable-standalone-module --disable-mlogc
5 X( w* J$ N# Y9 i$ make7 q6 o' a8 t: i5 T$ m$ I! D
编译安装Nginx并添加ModSecurity模块; x1 _! u* ]- C$ J6 k
$ tar xzvf nginx-1.9.2.tar.gz
0 N. m1 F6 v2 y6 x6 D- U$ cd nginx-1.9.2  |$ }; A9 w5 g
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
! \/ V$ x/ {3 y! G! c1 y$ make && make install
* r; ^  t# B- I3 r1 n1 X方法二:编译通过ModSecurity-Nginx Connector加载的动态模块* J3 A6 w: P  |. a' W& h4 ?+ m
# F" R$ U  Q" a, k# e& ?

) @* Y8 \& S+ Q- L6 v. F编译LibModSecurity(modsecurity-3.0)
8 f; w# Q$ @' \; r! w+ A$ {$ G$ cd /root
3 v! i: s% B) }5 M! r: J& |$ git clone https://github.com/SpiderLabs/ModSecurity# T* \  d' Y# K
$ cd ModSecurity
; r9 T, E' I# c$ git checkout -b v3/master origin/v3/master
7 b1 A* y( F" H8 N5 j5 k$ sh build.sh
# X& R3 w4 p* E! t. B8 \& s( q$ git submodule init
- J# a5 r  ]& l. Q( W% _$ git submodule update
- ^  W2 R5 u2 T# d$ ./configure1 c1 i# B; X) W: y* C
$ make
9 U2 O  S' v  ]0 i9 u$ make install/ o- S8 m$ l' u, X6 p4 Q- L6 h
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
  I# k  Z% ?4 x( f9 t5 f1 T! F- K1 U8 L7 q

, J- A4 A6 T  P- W6 t' P$ ls /usr/local/modsecurity/lib! e2 `7 y3 ~/ a
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0! R# m- o0 {- L/ f3 R
编译安装Nginx并添加ModSecurity-Nginx Connector模块
9 ^+ a" H  Q/ S- x8 V: y% L8 ~使用ModSecurity-Nginx模块来连接LibModSecurity
% v( b5 D3 n- \* d9 _9 g+ F& X8 y* S7 `& t

4 Z( g$ M- M  ]) `  T$ cd /root
+ V8 S# G7 Q( i& S  d3 {% W0 A$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx" c2 {* t* m, Z! R9 y/ L" I% o" Q& j
$ tar xzvf nginx-1.9.2.tar.gz
" B" a- ~% N5 K0 j# X( e# I$ cd nginx-1.9.2
& F, s; R1 B) v* R$ ./configure --add-module=/root/modsecurity-nginx3 B! x$ B' T; a/ v" r/ |# p1 j5 c
$ make& {5 M' O0 i4 \0 ]/ D
$ make && make install
5 D# g% R7 z  V4 C. R添加OWASP规则
: d+ C2 l  h" `. z7 f. B( FModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
; M/ P3 t: c: P6 B0 ^- l3 c3 a0 n2 U* t: I" Z
: Q& x3 X% C. L
下载OWASP规则并生成配置文件
. o" y$ W# w4 _5 N1 a  y$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git/ }8 p1 q! s0 _5 y, C5 [' C* H/ t
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
  l' T- e1 s$ P2 N4 o% L- Z4 a+ Y0 v$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
- T8 H; G. L3 c' W+ w, l. R/ |8 k$ cp crs-setup.conf.example  crs-setup.conf
0 |" |0 \1 W: ^  M( V. U) d配置OWASP规则
& L. j7 a* J4 R  F; T5 i& s编辑crs-setup.conf文件
: L" r1 o4 Q: f  t
  L, Q; o  T2 H
0 _, O/ ?( l$ h$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf  y* U# J9 N& F: a" N, X
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
0 u( m/ U8 u  K- e6 I$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
( Y8 l3 c( o+ z; u# w$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf5 m1 c7 M( G7 l! g
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
* D& ?3 {% S, ^- B! q& Z
7 Z/ S: x+ ?% W- p5 d2 F$ q4 f1 L6 g9 C  ~% o7 N2 f4 Y. ~
启用ModSecurity模块和CRS规则7 Y, n- ?& B! E' s+ d4 @0 x
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& h7 ]8 \# M: U" m
8 R( K$ S( G! m
2 Z) u0 @5 E8 Z( q! m( ?8 w
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。- S0 X" A% G6 q+ f% h8 j: ?& }
8 ]7 C( @9 r- n  z$ C

  w8 P) h( D! I! t& v$ cd /root/modsecurity-2.9.1/
- v2 u& u6 |$ Q( @$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf    m$ i5 ]  _" j/ Z3 E
$ cp unicode.mapping  /usr/local/nginx/conf/: }2 v! `: E8 h+ Y: k5 D  C
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。6 k3 v7 `- A4 X( m3 @

# b& C8 M1 L( C7 L4 A  B7 d* Y2 |& D+ ]3 L
$ vim /usr/local/nginx/conf/modsecurity.conf
6 y. j$ S8 K, a. z1 r! L4 j% {8 BSecRuleEngine On2 r( K$ R; z+ u8 p& G
ModSecurity中几个常用配置说明:
6 @" y' \& u9 _6 y! c8 k! }( i; w$ E( k

4 Q# n# r2 N% I; z/ g: \1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。5 ]' M. s2 G) B

( w$ V( P$ L* B+ C5 v- k4 \
5 K& r3 h, [$ Z! X2 w2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
$ E* Z$ e4 s1 X% A3 [# V9 }) p
/ Q' z+ C7 ~* ~0 X8 ^
2 t0 a9 ~  D6 G3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。; b7 ?& u3 p9 C3 |

! X8 `; Z- u6 z6 g9 }0 W: _
7 i' K2 ]7 c: ~; H4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
; ~6 ]! i9 j: B- s2 K5 Z" b6 ?! X) s1 U" @6 m

. g+ N6 u4 E8 V) l) o. _$ p; C在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
# z1 L- I: Y( _1 T8 F+ U. h
6 t* P, k- A* f+ c/ l1 M9 D
0 a/ |) w5 S; t' C3.x版本CRS
. O& B3 m6 s/ p3 f2 q' m$ cd /usr/local/nginx/conf/owasp-modsecurity-crs. \9 c3 R$ ?, O
# 生成例外排除请求的配置文件8 L1 l, w& \9 y3 p; b
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
" s" Z. Z1 H( g9 `/ _$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf8 b5 f' R4 [( K7 u1 i2 j3 J- z
$ cp rules/*.data /usr/local/nginx/conf7 b0 H1 I; m: a
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
+ ^# V- _6 \- a) ]  w5 E
! W2 D( u. B+ \
7 d; |3 V8 T. N* J$ vim /usr/local/nginx/conf/modsec_includes.conf$ D4 X- ?* v: d: j

0 u6 E4 c9 R1 k
[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

2 ^( h! X4 S; N4 K1 w; {, _3 t+ P8 b) I! G' q" J
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。6 B/ F' S: l1 X# V

' g' w# F( y/ U' Q+ m* G' D
7 u$ o: p5 r5 Y1 Z配置Nginx支持Modsecurity2 f7 _# e* m" R% k
启用Modsecurity- c/ i1 Z! F6 {+ ^4 W; O
使用静态模块加载的配置方法+ D( J) E0 B" k# n$ u
在需要启用Modsecurity的主机的location下面加入下面两行即可:% q0 N+ H$ U+ _3 I

+ P- }5 [- h( C4 [* m% N2 {$ ], S4 Z7 S6 j" u9 J: u
ModSecurityEnabled on;
0 A2 m8 K2 y% o$ c( Y3 aModSecurityConfig modsec_includes.conf;! ~( R3 u7 x7 P1 b
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
- j: |/ j; k8 \7 v$ Z/ o
0 e! {/ }! G1 q0 N
! \" F2 `6 ]- q+ r# A$ vim /usr/local/nginx/conf/nginx.conf4 I* J, P9 W& N6 `8 c8 J0 h# s* o

( y9 B2 {) N. {0 d+ q
5 y, k% a2 J8 d8 Jserver {. e0 j7 }9 v7 A0 X# D$ C
  listen       80;
  Y. g/ R5 g1 H  server_name  example.com;( l+ h/ F0 R; o4 w

0 Y# c+ p: i  @9 _
' Y5 n+ c4 b" T' L. ^& I7 ~. K  location / {7 ~" q: L8 }. |% J
    ModSecurityEnabled on;( \) l3 A/ }. O5 i
    ModSecurityConfig modsec_includes.conf;
3 ]4 R9 R# m! |' X! g  l    root   html;) R1 h' `, k5 A8 ]4 w
    index  index.html index.htm;& w* z( W) e0 Z  X, W& ?$ G
  }
8 V5 q8 g$ ~" }! c' h5 W$ V3 t* A}
9 H( E) q1 [& j7 r% n. N使用动态模块加载的配置方法' s! L: E* @3 C' r( y* g1 f
在需要启用Modsecurity的主机的location下面加入下面两行即可:
: S3 b( Y0 d. g+ R% g% T' }" b6 `4 N8 G5 s, ?! r6 A2 k8 ]

* t) X; C/ t- Z4 s' f% Wmodsecurity on;
0 c9 I4 M- n6 G! a7 ?modsecurity_rules_file modsec_includes.conf;$ b& f* [% Y9 m
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。$ j! G  |4 m6 q; B* @+ U

6 X% V4 h: i  f# v1 D. v) l  o% H! ~, y+ i! Q( d! ]& t
$ vim /usr/local/nginx/conf/nginx.conf* f* Z3 A! r/ m% `

2 h" Q0 j0 ^9 Y0 x% U7 N
1 T- K/ C; s9 z2 Q5 iserver {
2 D+ S( B' x. {  listen  80;
) _$ d6 A  L, K9 t9 J  server_name localhost mike.hi-linux.com;& Z9 c* v( {! d
  access_log /var/log/nginx/yourdomain.log;
( _" E, {. s2 S* C& p9 y( A9 j+ C  X) p( P) E9 Z( B- Z6 i) ?: a2 K

/ z7 T6 Q3 Q4 M  d* F) v) j9 P  location / {9 L. E" ?: f9 _% k. u
( b% L( y: X, s" a  T2 `$ i$ p4 \

/ Q8 F/ \* R. ?8 T9 K  modsecurity on;- g; ?7 ?9 q) z+ e' u' q- D  e6 {; E6 n* M
  modsecurity_rules_file modsec_includes.conf;
. ?* r* z- a! a" h  root   html;
$ s( @8 R& n/ J- L! q( `5 a  index  index.html index.htm;
8 \& T3 P5 W. k}9 s* A9 `, m2 N5 R% y5 t; S3 }# e
}5 \) ^& O' y, B4 |2 f& D0 ]
验证Nginx配置文件
& V7 d$ z$ @' ^2 H2 G) s( m/ q$ /usr/local/nginx/sbin/nginx -t: C) _. L8 A: `% d
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok+ N' }7 P3 ^' ~: G$ d: ]
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful# y, Q" K/ z% F' P0 Y
启动Nginx( `3 d; ]' _. c6 Q' e. q' i
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf; c  {, S. u4 E5 @

1 l  B% [: i5 i8 ^

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

' g0 t3 `: [, A2 Q% x- ~; R( g

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-9-10 08:21 , Processed in 0.081946 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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