找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9695|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。6 ]" J9 }+ n" i6 r' d9 r+ [

3 R( {9 c; k9 |6 |" J6 {5 f* ~
, W; ~$ ?$ `7 u% c0 u在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
* g8 [- H% U/ ^' D! }6 @
! F! m; r6 N7 M, _) Y. m8 n4 [! S7 I9 Z* g" u1 h( Q4 g: F
什么是ModSecurity
/ K# f; P6 k- m3 |3 W4 dModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。2 _- I# {) w" T2 |8 [
! G/ |) g5 n# r
! o+ @$ J0 O6 R
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
! _8 @: w. O+ s8 E
& g# ~$ d9 D5 K1 [' A8 ]0 t6 S" O* M. h% A& e, z
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。6 z7 l- x  i3 r0 w5 ]0 _
, j9 r, f4 p* m
2 X& p) d, W9 N; A$ ]% T% x) B
官网: https://www.modsecurity.org/
9 L6 u5 B/ i: A* S+ h, v, |! {/ z) J

% \7 l& x3 E/ d" w, D什么是OWASP CRS
4 e( i+ g0 q) I5 F4 P) C: h. _$ }1 _OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。  \' U/ e$ j" j' h% W- y7 K
0 l+ c- [# {0 P& }- h; n4 g

! S6 {. E+ V4 |  e  e( EModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。* e  [# R. W4 c
6 c7 O  [1 n) {' j: s- b

# f, N# c7 E3 J7 A0 C1 `% }HTTP Protection(HTTP防御), D' {# q) h0 e' j
HTTP协议和本地定义使用的detectsviolations策略。# j- r. F! C& v3 @4 [5 A

: f, _- }9 w' q" m& M5 r1 ?1 Y2 }% r7 G& t- i
Real-time Blacklist Lookups(实时黑名单查询), y9 q4 G$ J1 ]1 J/ D7 Y  p$ T
利用第三方IP名单。7 U3 U/ X5 h0 I7 X& T8 ]

6 p8 G. P" A7 X4 b* P0 ^0 Q
8 A- O0 s, w" [4 e) K( `/ eHTTP Denial of Service Protections(HTTP的拒绝服务保护)
1 P* r6 ~/ ?: c9 s9 n0 @防御HTTP的洪水攻击和HTTP Dos攻击。# b1 Z  z9 H3 C9 B5 J3 p
4 t7 p" Z4 }+ C4 `7 M. z

9 k9 a7 N( i# M/ K  P" V% OCommon Web Attacks Protection(常见的Web攻击防护)
- ]# s' m3 K/ I检测常见的Web应用程序的安全攻击。
# R2 r  `8 \; C; B: E" \* K; Y& E4 `' d; O6 T- s
% @! g$ C1 `5 s2 u5 {
Automation Detection(自动化检测)
$ r$ v8 [. X; [* A检测机器人,爬虫,扫描仪和其他表面恶意活动。' _( ]! K& D, k4 W/ J( {4 _
& R& y- B% q! E, D. L, M: R: B! s9 M! k  l

- O# M" h) [3 N- S- b4 bIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
9 K8 j2 k' d6 `检测通过Web应用程序上传的恶意文件。
) U  V- o$ J7 |0 R9 C7 S4 W8 v4 j( j0 }6 w

" c6 a7 o- D- J- V; C% _Tracking Sensitive Data(跟踪敏感数据)$ S; W1 j1 _, G7 \
信用卡通道的使用,并阻止泄漏。
+ J6 i9 O( B$ Q0 [$ @. L  u8 `: g6 D

* p6 z1 N" B6 v0 mTrojan Protection(木马防护)
5 e2 H" x; K) y( k  ]8 k3 r6 n检测访问木马。
: j/ @  F2 Y9 c! Z* l( l
1 [# _$ z$ m; R* ]2 U8 s" j1 d, j9 \; W& p, O+ O
Identification of Application Defects(应用程序缺陷的鉴定)9 L) ?& B9 ?3 w) L7 l% p
检测应用程序的错误配置警报。" f! }* x+ Q  [) l

% \6 M" l& c* Y5 U. h& g  O2 Y# q! I* H! G- F
Error Detection and Hiding(错误检测和隐藏)
8 V/ H- V8 x1 o) D检测伪装服务器发送错误消息。
: \8 O+ a' M, Q0 X6 C4 P
6 ~7 e+ ?- ~5 P# p9 u" ?! P
  ]/ V/ F, w6 |8 |安装ModSecurity
2 z( F8 B6 q+ t软件基础环境准备% Y3 U8 N# ^( R. t6 w6 @# Z
下载对应软件包- C) L( ~8 n- q6 O; i$ P$ s
$ cd /root+ c5 E3 P5 b0 }
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'2 L! }% R7 G+ c6 q8 H& ~- u
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
; C# V& Q9 c* [3 b( o6 p安装Nginx和ModSecurity依赖包
+ B* C4 |; k% u# b) PCentos/RHEL9 y/ q6 ?2 ~9 W. G0 V- M
6 k1 v$ K; J; Y% Q
3 [( l0 |  Y. `2 a
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel2 [3 J' E& E  z  G8 t+ J+ s
Ubuntu/Debian
6 C6 p4 Y' w- V! o5 y# Y: Z9 M9 V
3 b. I3 _4 i, t- p. T! e: q& e' u+ l5 J6 g, I! s* @! ]! H# m
$ 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
" B( q  T# r0 b4 O' h/ c: d# `编译安装ModSecurity
: |: i- }: c: e$ e5 WNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
6 o3 g) a1 w% W
6 W; Z3 }; J& `" {
' ~; }* l6 \1 j* e* T方法一:编译为Nginx静态模块$ \, {2 q7 T' C0 V. U

2 k" e5 U3 `  j: @* Y0 [5 W3 K. N3 r
编译为独立模块(modsecurity-2.9.1)
- d2 r* c0 W. V$ S8 B6 Z$ tar xzvf modsecurity-2.9.1.tar.gz
( ]* J0 O' ?0 ~4 p$ cd modsecurity-2.9.1/  F7 Z) q, O- {' [& ?
$ ./autogen.sh: L9 V" Z$ n; v
$ ./configure --enable-standalone-module --disable-mlogc
& ^# q- X4 m7 ~* x8 [8 U$ make. g. `7 a" Y# q/ _0 F
编译安装Nginx并添加ModSecurity模块
& u( k* L7 E( t, g# D# M$ tar xzvf nginx-1.9.2.tar.gz5 N: s; H( e2 y! O
$ cd nginx-1.9.28 P, d0 c8 _$ y
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
5 Z7 g+ K& M/ U$ make && make install* k5 `" e- a7 \9 w6 d- n; d
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
) P' H0 S  h# R. ]( f$ N" `  t$ Z5 G1 N3 \8 r

7 j" D  f  `) b) s1 E/ |, z编译LibModSecurity(modsecurity-3.0)
; H0 M2 z! X- [$ cd /root
) m+ Y: T$ p' r6 w& \1 d( R: H$ git clone https://github.com/SpiderLabs/ModSecurity! C2 J' z# Z* @$ ]" h
$ cd ModSecurity; M, }" P" N3 h6 L5 W$ i0 S$ o5 R
$ git checkout -b v3/master origin/v3/master/ M: t, H6 J8 M& T3 |9 v( |6 Q
$ sh build.sh
# [/ r3 s2 `: q9 q3 y) I$ git submodule init
# @3 X; n# q" K( A6 R$ git submodule update
8 F+ }! B8 Y- x  o$ ./configure% j0 U/ O) V/ e
$ make) Y) i. y" ]$ m; t7 l5 T
$ make install
1 b* G5 M/ f; P- e  f% W, W6 y9 M8 KLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。3 L# @7 y) L" y# t8 F! ^

0 [+ V& F7 ]' z- O9 H9 p) P8 [6 }6 E  L
$ ls /usr/local/modsecurity/lib$ e$ F5 H6 {2 W0 H- C# R2 B8 y4 a
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.07 _3 i5 l% P, H" w
编译安装Nginx并添加ModSecurity-Nginx Connector模块
8 y4 C& t! V6 q7 W9 p+ H使用ModSecurity-Nginx模块来连接LibModSecurity2 i2 e- T$ |. \* b( a

" {& W7 {5 Y" c) F) @) o# C# W! h4 e% ^3 L  \4 c4 a
$ cd /root4 U' j( f. C. @. _* ?  z
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx& G9 Q9 g' ^3 `' H' c
$ tar xzvf nginx-1.9.2.tar.gz* u$ r: S  U5 H* r
$ cd nginx-1.9.26 J7 X/ S- b0 w4 S' L  I0 T. ?+ o
$ ./configure --add-module=/root/modsecurity-nginx) @: e+ a% y) i: M7 ?  ?! [- z7 }; q
$ make3 T) b7 @: i) H9 P* K
$ make && make install% A& d. F& ?- C3 a. n, }$ g) j* \' M! |
添加OWASP规则
2 a4 q2 _" l5 L7 |ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。+ B) {! N# p' h8 F! v( r1 C

! o) \) X$ |& b6 u4 D" X5 f
3 A/ f1 _& F' P( R下载OWASP规则并生成配置文件
+ e3 ^/ E0 l2 T5 o2 U$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
( f  {- X6 y+ N$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
, G9 ^9 r7 e2 U& N1 t4 [5 s( \$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
- H! k4 t. q/ B# A9 ]% u$ cp crs-setup.conf.example  crs-setup.conf5 f9 q1 M7 X6 L: Q) c- w) l
配置OWASP规则, t$ W# A: Q# W7 B7 w4 b
编辑crs-setup.conf文件
( s. f! c9 Z$ R3 Z) h1 _4 w) @8 b) x# m5 t
2 b; |& O9 Q" @$ F6 R  K2 Z- j4 W' p# }
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf" }5 p: G" Y* y
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf: E# I" u- A: `! ~/ W
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf7 x: b& E* x* e1 Y2 k4 i
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
4 {) ]7 I4 q, u! `默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。* ?' Q# S- T, |- u% ^, v9 e
& Z5 E) C8 Z3 g. E9 X& X

  K. h& A' j  Z6 h! W启用ModSecurity模块和CRS规则
) H  u" [& E; r( t( H复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
: w- T1 w5 p$ S9 @
5 ?- u- m- R, |# u, @. K; a
' U" I; j7 p9 q2 omodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
1 A- _. W( K. P4 S5 ]( B. a
6 D/ `3 b$ X+ l- q; U. i7 b4 ^
- L! h7 i0 i9 D. g' E- F$ cd /root/modsecurity-2.9.1/4 s2 G9 {6 F" j( t+ V4 g
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  4 P& c" D6 ^. o" N9 [5 j3 h$ z
$ cp unicode.mapping  /usr/local/nginx/conf/8 G' T  v& M. R  j' |
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
. d1 T* {% @9 w6 j* o
' j$ L# G, N/ g% @7 }$ v$ I2 L/ |$ e
$ vim /usr/local/nginx/conf/modsecurity.conf
& R8 Q# X1 j* [+ C. ^9 j/ BSecRuleEngine On! ^8 n* B$ l; h6 \8 x6 Q+ M
ModSecurity中几个常用配置说明:+ ~5 G7 q% f8 @/ F

6 g7 ^( q6 b7 L' T4 e2 U" G* z0 \. B0 G
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。( k( F( z0 h* j

. L3 N5 c7 H. T2 K9 m' O3 G; g7 V$ f8 v8 Q8 s( z. R& C( [
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
) e2 F* P" g0 j. W
) W! n( _$ {; }" E/ G6 B: @; J9 j6 ]8 b" I
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。3 [' y4 B6 N. ^7 y% i
( G1 u# c( D* i, Z( \3 y, \

; G9 `$ [; }/ L5 Y5 e: w6 G1 X9 y4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。# s; O" E+ p5 X8 U4 r
7 K4 i" p  X' X+ [. \2 o* p
2 ~# d* v: m3 C/ h; |4 r% F
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。% A$ [% I" Y! j, F
& u. D& M5 X: i, n

9 f0 G+ N& a- }1 U3.x版本CRS& g0 d5 q2 M( |% l
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs/ D* a# }3 }) V+ m6 `
# 生成例外排除请求的配置文件! _9 b# ?, F/ W6 z* V9 @
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
8 B! n" B  K  I5 K/ `4 |$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
* Z* d8 e% ?, _7 \$ cp rules/*.data /usr/local/nginx/conf6 M9 f4 S' c2 L- o. `0 L
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
, w; a  ~. Z0 n3 B+ i: }5 ?" \$ I& f9 W
9 p( n, f' j5 @* t
$ vim /usr/local/nginx/conf/modsec_includes.conf7 p/ N% ]$ q$ c2 z% a# K* ?) ~5 V

6 W( ^" K- z$ o* s- 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

/ H9 `' T3 ?/ V3 N1 l
8 ^' p0 ~' G1 L3 Z注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
9 x& g" F) e, f2 ?
1 h! H8 c* h# c6 F4 E, Q! }& O, P% D0 m; S! U
配置Nginx支持Modsecurity2 {& Q: `9 v: ~/ ^3 O
启用Modsecurity
( y; F% T' O6 G1 q* M使用静态模块加载的配置方法
9 _* f% D. M2 J, T9 b) e在需要启用Modsecurity的主机的location下面加入下面两行即可:
1 }! q5 ~1 o9 P# \+ t1 b+ k. n1 u" v6 u2 V0 @
& H/ G/ a" ?* M
ModSecurityEnabled on;
+ P1 }/ G& s: w  ]& M6 n. f7 j( lModSecurityConfig modsec_includes.conf;# E8 h6 s2 l" Q  C6 m
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。* y) r& v4 R6 w/ D$ W

( d5 \  q# N) |7 u1 \( g
$ B5 j4 @: x7 s3 |/ a* h/ C4 {$ vim /usr/local/nginx/conf/nginx.conf5 S: X) u2 Y' f/ V# R5 U

: t6 `; C" S6 r, z, {4 R% ]
( [0 _+ g; J. h, \3 E; ^server {
1 a! q* }0 Y! }' u- _  listen       80;" M# u- G. H0 j6 X) {
  server_name  example.com;7 ]9 h' r, l: L8 [' @
0 E: Q1 ?- C8 x
; d. j* z0 A; i0 j$ A) ?" c
  location / {
3 b5 b$ g1 V/ z  B6 Y6 Z5 ?    ModSecurityEnabled on;5 w  P& q* E; {$ v! F' j$ L7 v
    ModSecurityConfig modsec_includes.conf;
+ L) \3 V0 V- Q    root   html;
  X6 R- O6 b: r4 D    index  index.html index.htm;
' f' i2 p0 Q' V+ J8 h/ d. ~  q4 |& y  }& ]+ W  i4 d9 d+ L- \9 ?) C- {8 d2 U
}7 O3 X% W6 n: h; c
使用动态模块加载的配置方法
6 P. ?# c# `% E0 O在需要启用Modsecurity的主机的location下面加入下面两行即可:- C1 k* c/ G5 p2 Q- E5 q& k
) i- W+ v8 O9 q# `! |3 c4 c  r

4 X; T: E# h* N  f$ F; A/ qmodsecurity on;- Z. E  B5 U3 ~( ]# j: r+ d4 X! }
modsecurity_rules_file modsec_includes.conf;
: a& U7 k, {1 A9 D8 c# o修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。. @6 g9 L8 A2 @5 X( ?5 x$ ~, ?

: t2 \# Q4 m3 r) r) O( u5 X, }: i- J
$ vim /usr/local/nginx/conf/nginx.conf* h/ U/ |0 T1 u2 w$ s

* l# W) N  P0 n  o0 ]3 H3 ]( H+ j8 s7 A8 S" W! c1 n& F
server {
- r, J, g# V* o  U, H  listen  80;1 |6 ~) s5 T  i- Y8 m+ Z
  server_name localhost mike.hi-linux.com;
6 q: E+ U3 U  e% R( h  access_log /var/log/nginx/yourdomain.log;
# ^* m* b' K, j+ M. N  G9 A& y* W+ J5 g1 }% I

4 L+ ?3 j5 Y* g# S$ u% P: {( u  location / {
3 r1 _/ l' g% y+ L9 Z, i' D" m- @! }+ n8 U2 z
2 h/ w( t3 U3 @
  modsecurity on;
+ o0 m7 [$ [/ v' |: T6 S  modsecurity_rules_file modsec_includes.conf;
0 ~) o4 E+ c  z" [. C! a/ @  root   html;- I" z9 ?% V' {* u/ M! j- `5 y1 d
  index  index.html index.htm;
; Y8 G& b  U# E9 F2 f/ f( Z}
3 Z) e& S' }1 L1 l}8 Z5 g; m0 T( M! o: F
验证Nginx配置文件5 m& q( C% L( A# \" ^
$ /usr/local/nginx/sbin/nginx -t
7 |5 |; I9 S: x+ S* B" P  d8 Xnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
5 u9 y' l* g4 e3 fnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful* U, E+ ~9 s  A" i
启动Nginx& ]1 B" B! U' g; l) H6 N
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf& W# Z5 g: `; B5 I. L; I
7 t: Y# g% s/ }6 @- E

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

+ k2 t% N- |. O& [

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2025-12-8 22:44 , Processed in 0.049836 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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