找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11536|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。- @! r- J( e+ X: Y
$ G! d6 s% v" p9 K; s7 U
( h0 D) D9 ]1 S. N
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。) E4 \- J: ~3 X0 {# Q2 K+ N

9 X, I& c6 S9 f' ]8 Q! h4 T
* k3 M5 I7 d$ {& i) D- B% y什么是ModSecurity
! N9 I2 Q4 d' Q, e* b4 b. DModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。+ [% n8 e# C3 K* x* g0 u, @. k
2 o" m$ z) D# `% \! [& j* P" E

0 o# z3 o  g6 O! h7 C: t9 RModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
% x2 a( d( S5 d' C# Z& V
* ?" q2 e; G0 `' W& \2 y7 y, N& s  @6 x
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
1 N" \. S) v( c9 v' U2 G* c, p. T# A2 m; c; a9 x
+ U; {0 Y6 _- ~& D
官网: https://www.modsecurity.org/6 t7 Z& j4 P$ y: F6 b

6 X; w% `" e8 N( n7 ^4 c) l' K# M2 q5 I; |
什么是OWASP CRS! D: p1 C3 d, w$ J- b
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。, S3 J- l* b3 h5 i2 E

" {( K2 `9 \% f6 n6 `+ P- c( a$ `) A
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
3 G1 U/ D6 h$ H' M8 U, n) b7 A, Z) d+ A4 Q1 o9 n

# X: L- m9 o3 X3 Q( j& [HTTP Protection(HTTP防御)2 K4 ]0 e: ?3 c& x
HTTP协议和本地定义使用的detectsviolations策略。. h. O0 f, E3 O% R; c: J

, S2 J- H; B& l% l% \! G; Y) n9 `: p! F2 `# M$ L
Real-time Blacklist Lookups(实时黑名单查询)
9 a4 Q! P1 Q: _9 P6 f利用第三方IP名单。' D0 x" k' j( j  r
7 F0 w0 C, v% s1 ]; |5 G, V

+ i9 T  e- O' l0 wHTTP Denial of Service Protections(HTTP的拒绝服务保护)
* N- ~- J0 L9 l+ u. I防御HTTP的洪水攻击和HTTP Dos攻击。
0 U' n, D. I' \; R* l; i! L% l/ l  Q( w- L. W& @8 k) F0 P

5 _( z0 I8 w5 g+ \3 h7 Q, ]$ }Common Web Attacks Protection(常见的Web攻击防护)
8 b  `* Y% e, F4 d' h; e检测常见的Web应用程序的安全攻击。
: m2 Q8 z. D( h. Q# n& m( U
8 L' d' Z3 X3 C; x9 |3 ]6 `1 x9 o. p4 K- {
Automation Detection(自动化检测): K+ f0 w: ~; O# T2 V; q
检测机器人,爬虫,扫描仪和其他表面恶意活动。3 X- i; ^' _% J6 P0 i6 ]
0 ?4 {( D  y% @$ j& W2 B8 q3 |+ h# D
. L6 I1 M' e0 `; w
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)( a3 p+ u0 K* z& H
检测通过Web应用程序上传的恶意文件。: r+ D2 k: z' X1 y
$ ^. `& W: A* Q  F( p% H0 z

" @0 y# y( p% H9 ^0 G" FTracking Sensitive Data(跟踪敏感数据)
6 L, a  ]8 F! l6 ~& h. D信用卡通道的使用,并阻止泄漏。: V. M) R* ^" o, ~# h

$ d5 b! _: ^* r/ e- v9 R/ Q  [+ n5 r: k$ }$ K9 l' m
Trojan Protection(木马防护)9 p1 s5 k9 D& A( b
检测访问木马。! P9 t' B- V4 `* m

( u( j1 f7 ~! W, p% c9 ?, n8 _0 O2 C3 G4 u4 j7 |& X7 r1 [  J
Identification of Application Defects(应用程序缺陷的鉴定)
1 _) l; n" l6 w9 B  G* T) b检测应用程序的错误配置警报。& W% A/ w! X8 t) J( g% ?% v

. z( u7 p( B. ~& @8 P" X  h- M, h1 ?/ h2 w! X
Error Detection and Hiding(错误检测和隐藏), {/ W' A1 r; |! J7 e
检测伪装服务器发送错误消息。$ J$ a+ s# e7 [0 n3 f9 M$ J8 D

/ n' V+ k6 `1 w5 Z1 r
$ i# e6 `5 c' l. o1 y9 f安装ModSecurity
7 D6 D0 r6 h5 N0 R. l, m) s软件基础环境准备
- f0 ]" m9 f, `- j7 b4 \- i+ ]下载对应软件包3 o. J9 X1 z: i1 z/ X
$ cd /root3 O+ `- W! X& O& K
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'0 ?. A  i  ~  i4 N* S3 B. `9 S
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz! r9 U* l" D4 Y4 O- w& X: i! D; g
安装Nginx和ModSecurity依赖包
* |! G& r/ y3 d; ~- @Centos/RHEL
3 Y! ~9 u, @7 K( H! }" V7 j" E! P6 V# r
. K( B5 {# }: d4 H) M
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
) }0 v$ ^+ P6 {Ubuntu/Debian
" W0 R0 E/ B7 R$ l3 i9 U8 V8 K5 c
( y' ~( w1 V6 F3 w" J5 `  p0 A0 y/ O+ h$ P- 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; K& ]( f/ B( g# [1 d7 j
编译安装ModSecurity
$ l( f: l1 s! J- o  X, w: wNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。& }; b9 L8 ~: p% Y
. L0 k5 Y( @' _
& y$ V" X! ~6 T
方法一:编译为Nginx静态模块
8 {% F7 _5 f7 Q5 R
0 K% N( p( e) `
$ a  W8 G  i0 w/ ~2 J编译为独立模块(modsecurity-2.9.1)$ Z% c% i! W# G
$ tar xzvf modsecurity-2.9.1.tar.gz
5 e7 \' F+ b) P& x/ |" |0 j$ ~$ cd modsecurity-2.9.1/
1 B- @$ s8 E$ t, w" Q: T$ ./autogen.sh) ?- B3 ~5 v- P3 z/ ?" ^) O& u
$ ./configure --enable-standalone-module --disable-mlogc6 H% f( c2 a" x" W' f
$ make
/ e: d; |7 G8 m  S; Z6 H编译安装Nginx并添加ModSecurity模块4 ?8 l( L3 ?" J/ f; u  k& r2 m
$ tar xzvf nginx-1.9.2.tar.gz
: ?- ?2 L# R! k+ v/ n$ cd nginx-1.9.28 A# ?/ a5 _/ D( {( L6 G
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
8 P9 d: C" |. W6 Z$ make && make install
$ _! J0 r. y1 J% u. q  m4 z& @方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
8 |0 |. x! @  r  T3 E% a
$ G1 Z6 S3 T. X  l" ]4 E0 w. @* N5 d% t; E$ s
编译LibModSecurity(modsecurity-3.0)- u4 O& R+ V7 Z3 v& m/ K% H
$ cd /root, d; h$ Q( ]3 f0 U+ ~! m% U
$ git clone https://github.com/SpiderLabs/ModSecurity6 s$ s* U  B2 s# y7 _) j
$ cd ModSecurity
9 h" Y$ d) _; g/ `/ |% s$ git checkout -b v3/master origin/v3/master' q5 S# K9 y0 L9 D. J7 N
$ sh build.sh) a8 ^6 K9 w* G$ p4 v# \8 d, D7 @% B7 C
$ git submodule init
' @3 c' `+ f% P" f  p$ git submodule update
4 M& A! O: e; a4 i) t$ v$ ./configure* M( \2 V* X" s
$ make- i9 Y) Y0 s, z
$ make install
' K* y3 _8 d% S% G; Q- Y1 ]: x2 ELibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。: ~2 H- C; M' f4 e2 i
/ {. |" Z1 `* f5 b% Z! y
- ]5 u7 z5 ?! y8 e+ N
$ ls /usr/local/modsecurity/lib, K/ \4 U5 n! C$ k8 C
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
+ Q/ z; K9 w% e: R) I7 C编译安装Nginx并添加ModSecurity-Nginx Connector模块! i8 f, X8 n% w4 z) ]. L6 _
使用ModSecurity-Nginx模块来连接LibModSecurity/ l; d& R8 G- P; `' J7 E
8 U4 v4 K0 {- |2 f# }

8 n/ l2 M- P$ p) A4 k3 z$ P$ cd /root2 U- ?. h8 i. }. I) ^+ Z/ c. h, A& J
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
6 b! R4 k, G$ q$ tar xzvf nginx-1.9.2.tar.gz
0 ~: N8 R  z; ^$ cd nginx-1.9.2
6 Y; _  e$ Q. m: L9 `  |$ ./configure --add-module=/root/modsecurity-nginx8 p! R; b# \& _
$ make  m4 |( X! u. T* k+ ^4 B: m$ h7 \" G
$ make && make install3 Q3 H4 `2 Y. Q3 H0 g
添加OWASP规则
& ~1 e" N; e" T6 o$ P$ l- v: \& XModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
4 k3 }3 v$ H; _/ A/ [; w
2 k) o: [8 A% G6 X
# w" D2 A8 {$ t! D! ^& h下载OWASP规则并生成配置文件
8 O0 W) t, l; Q4 U$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
" R9 b( y+ i' o" T. T; L$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/! D1 ]* [9 V' k" G9 u  N- X
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
; |: n' k2 X8 g( w  y- Q$ cp crs-setup.conf.example  crs-setup.conf
0 [" J2 C" n% T3 e% ^配置OWASP规则
" o, i  z. Z4 Y编辑crs-setup.conf文件/ f4 x8 Q2 u( U7 D- V5 K" `- K9 [
4 n- l+ l3 g% E# X7 I

+ J- |+ x. y/ \& r/ ?) |$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
+ |& a6 H" @7 Y3 P! R$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
8 v; r+ m5 A' W: X$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf5 {- W6 w6 O( U; l0 V9 x# u0 {
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
$ p# a* w& m8 u8 z默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
) d( l7 C5 o; a% C8 d7 k3 R1 n0 m0 `5 g( S+ b* q

% q9 z! @3 \% x( S$ R7 |启用ModSecurity模块和CRS规则
2 j- j: O, k, G+ ]" Q$ c复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。8 w( t8 @0 [/ {4 C3 G
$ p, `# H0 Z% e/ ~$ P+ M) H
) @7 |! I% t! p1 ]" U: A" L
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
3 \! j$ Y9 F* w& l5 V' W: Q
! L+ e/ W5 ~1 g5 n: o! Y# y2 P- U, s0 W3 o# Q& p# n/ i+ p. ]* H
$ cd /root/modsecurity-2.9.1/
! K/ t6 w$ N2 C' K8 t0 p- R$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
6 R6 k( I# V9 I, h+ Z7 \6 `$ cp unicode.mapping  /usr/local/nginx/conf/; I# W" s7 O- L4 I
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。1 W7 w. z+ d& i9 W+ G& g
' |1 C# t5 f+ z$ ^

) a) z# U, a: ]$ vim /usr/local/nginx/conf/modsecurity.conf3 n' N: E- K# h4 P$ F5 \; C$ a7 l" Z
SecRuleEngine On; n; s* X7 q! {" d
ModSecurity中几个常用配置说明:$ w; V! j6 |2 O6 S

1 a- E- t" {5 M' |9 Y. n4 }/ ]' j
# [$ Q* H7 ?& F6 j( W1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
2 h/ K6 _# G% p5 `) R& G: y7 c$ o0 ^! r8 V* }; t& \

! S6 x/ |2 C7 _0 p0 ^  D& |6 C2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。7 ^( \, P( J5 S7 h
) w1 ?# Y+ \. X

: r8 e- R; E% S' L& \3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
: u" {0 `( h! I
% [8 f, u0 a- l( X
& i1 u0 C  F: W) T$ Q4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。  X* l+ M) O- C! Y2 }( i
( d( O  ^' U6 ]: l: E! g9 }; H3 S
- Q1 R/ E* o/ a
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。6 r0 q; p) }9 U& ^

8 ]/ w5 s$ P, M
, z- ~6 r/ S: p# ~$ J3.x版本CRS
/ L8 B: m$ g) t$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
9 ^1 W9 x8 D* A9 N3 ~4 g! w# 生成例外排除请求的配置文件
2 g8 ]8 c( o; A$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf% D6 _- S  r3 p5 r1 w$ [6 V
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf& G5 |8 c9 r% B0 V* a
$ cp rules/*.data /usr/local/nginx/conf
  s% R' c- g' k7 |为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。4 g% Y; B6 ~2 G/ f0 ^* p
8 [8 ?+ q/ c2 s: ]" ]% z
' r! o! u5 R4 I8 b
$ vim /usr/local/nginx/conf/modsec_includes.conf
8 T' _0 p* h( F2 \( F. U; p$ E0 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

/ G, q8 F! e# L$ D, q1 o" p6 W% P+ L0 {* c0 J# M+ M
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。7 [4 z! S# O2 a

$ p) F% Q# ]; `( y4 {# h
- I  S- G; S6 Z( c7 t* N: K  }* }配置Nginx支持Modsecurity! S1 E- Y; N: _/ |! }* L2 V
启用Modsecurity3 F4 t6 j3 r# l, n* u$ t
使用静态模块加载的配置方法' b. H& l+ ~# V; o
在需要启用Modsecurity的主机的location下面加入下面两行即可:( E! A- h0 z9 W7 h  T6 y
, H4 r0 P$ I2 l

! {2 O# f/ Y6 k8 @3 v6 J) |ModSecurityEnabled on;# `4 c/ m3 I8 y# o* j
ModSecurityConfig modsec_includes.conf;- d* @* v5 T/ h( _! ~
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
0 W7 M5 P3 J. ]
3 T) Q0 M8 @6 X: Q4 e( p" h: I! `
; p# B+ S9 n5 o; J5 U$ vim /usr/local/nginx/conf/nginx.conf
) b( i5 V+ F7 k# Q  c8 ]
6 t/ W% T  ~6 X! c
; @# j0 {& D+ a3 mserver {4 M% h2 ~  r) z  B& Q7 e) A( S
  listen       80;
8 M4 }; z% Y/ `  server_name  example.com;; r' O+ M; o( Y

) S- F. I. g: A' ^( |" D# g, v9 Y5 ]
  location / {2 K" D! e  o( V! K, [' H$ S* y4 C
    ModSecurityEnabled on;( `( u# k! g! C# H7 a
    ModSecurityConfig modsec_includes.conf;
7 F2 f. u& ^6 l# {  x" M: p: w    root   html;* ]1 e4 E8 m# g9 Q8 \
    index  index.html index.htm;* O; \  u/ x$ z2 f+ h4 P5 Q6 j
  }
1 S; e/ y# V2 F( A) [}8 g& w1 o8 ^" r8 V+ ]0 J
使用动态模块加载的配置方法
7 D) o; X! o" X& I9 K在需要启用Modsecurity的主机的location下面加入下面两行即可:
! }6 E& y9 ~) E, a; |' J& P5 ?: Z7 u) M9 e
8 I+ R& {0 v1 c; R' z
modsecurity on;
2 A  ?3 h( ~- \- s2 |3 vmodsecurity_rules_file modsec_includes.conf;
9 s( v4 V4 h1 _- Y3 M修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
! [4 X# x# S) L& o$ s  w0 H4 x
1 C# Z' L. Y8 y9 w7 q4 f0 E* C
% `( t- O! Z  C5 r. f% }$ vim /usr/local/nginx/conf/nginx.conf
  u. c( {; [7 Q6 V3 A3 W7 p* J% K- Q
2 g# ?' s+ q3 S( J- O
server {3 A* j1 d2 _( [
  listen  80;
2 B3 V4 K6 B+ K& W  server_name localhost mike.hi-linux.com;
. T* D/ u* h: _' n8 j- w/ R& L3 B% A  access_log /var/log/nginx/yourdomain.log;! e  C& j% t$ V
+ _) O% [0 M/ a% l4 W7 N
$ W9 ^) Y$ y2 _
  location / {! {6 f! z1 u2 m% u7 ~
0 E" _7 h/ R' \7 ]/ w% d' o

* E8 _& R: N' u2 I3 k: Y1 l  modsecurity on;
3 r& \5 U& `" ?; S7 z  modsecurity_rules_file modsec_includes.conf;
' c% s/ w  M8 T% G, }' p, ^2 E. h1 H  root   html;
- |: U( M3 u2 i7 \  index  index.html index.htm;& C4 J7 S3 A" O! `1 n
}
) F  u8 h% o$ Z( I$ d. y}
, a$ I0 P# O6 a2 S* l5 q+ A' B" N验证Nginx配置文件" h- L6 Y( j% A& U
$ /usr/local/nginx/sbin/nginx -t
6 I6 a" d  ]  y5 f$ z! n9 M3 onginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
9 F- g, j4 l% R8 K7 E9 V: Qnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
: Q$ I3 k0 ?" n  {启动Nginx
2 V3 |1 [% u: b' V$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
: D( Q5 m' u, z7 p: b
0 S# V! ~  @4 m2 [/ L7 h4 a2 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能够战胜更多复杂/未知/混淆的攻击模式。


7 W6 ?% s1 M* {( R7 ]( x0 h

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-6-22 05:57 , Processed in 0.075754 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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