找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10988|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。, i) [' o5 r9 x: J1 e! y
5 ^$ C& E3 W+ n

, ~' d9 O* c2 y, {& E$ R. w在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
" Q- h4 c4 Y! G7 T  h  L& k4 Z+ H5 h& F7 S3 j5 x
0 w. [; [, S9 W! \  _
什么是ModSecurity2 S% ?  I4 X; x$ c
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
  s$ X1 S! }5 P& h, @6 d; G- e! M0 Z  ^' S. f$ T. X$ b5 P- N

. `( U0 Y2 U) l* N% AModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。: ]: ?9 W; U. n- U! W

( ?: D$ ~* C( Z) n
, Y5 ]$ E8 G( ~7 ^* qModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
* `* y2 ?) U/ C; ]7 m# \3 K/ R
. u8 f; r$ G' ~, s$ O1 k8 v  Y3 \+ \/ o+ N6 q
官网: https://www.modsecurity.org/! k9 g( q# R8 |% M, Z2 \& O! `

: m7 c; f0 Q: V& C
/ C' l, Z9 w0 ^& a. j. D什么是OWASP CRS( c+ U  D, p( Y9 t" i7 A3 p
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
7 x# K7 b( O  B% j1 z' X0 m
; w! ]8 G3 Z0 T# O* j1 r" b7 V, N, ?1 N5 L4 d+ {3 L( I$ ?
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。  S! f: x0 ]0 Z, {
% ^% l2 Q+ |  e, z' x" r1 b$ @

$ S' ~: v7 ]6 m" b0 A5 G1 }1 k/ Z4 CHTTP Protection(HTTP防御)
& H; I+ b. R5 u8 X+ p4 aHTTP协议和本地定义使用的detectsviolations策略。8 g- v( [* H0 U4 l1 t  a" y
3 f/ d& Z9 e& M

7 X" ?  c- P$ |! oReal-time Blacklist Lookups(实时黑名单查询)
, L7 K' o3 k* z1 h$ l利用第三方IP名单。
( @" c" @# X0 k, \
. `' X8 X+ h' j* L5 N3 f! t/ g) R& x( J
HTTP Denial of Service Protections(HTTP的拒绝服务保护)* g1 |3 E5 e1 i- G( T/ E' e. P
防御HTTP的洪水攻击和HTTP Dos攻击。
% b. s) }: e& l: R0 D
+ w, K0 N, w1 r% `" m% O& Z- p5 P' D: _: o, }
Common Web Attacks Protection(常见的Web攻击防护)1 Y& ?+ t! l  n% M7 H  A
检测常见的Web应用程序的安全攻击。
; R4 w- m: U. a# t. {# ^
/ Y+ E! v4 D6 [9 x' Q9 Y7 I
1 u" z7 Z% E6 A7 e* s* BAutomation Detection(自动化检测)
& }  [3 e1 A3 y7 X检测机器人,爬虫,扫描仪和其他表面恶意活动。
$ ~' E% S' s% I% [. }3 v! \9 r1 ?, c" m' z8 j+ g; f

2 S  D" D0 G0 p: FIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)# [$ c% c+ d: V& q
检测通过Web应用程序上传的恶意文件。
; E# }( k+ \: n% H5 h! r$ C$ d$ `6 \* \' G. O0 u
/ {/ u2 w- A* r+ e2 o
Tracking Sensitive Data(跟踪敏感数据)& e7 ~* v0 @4 j$ i
信用卡通道的使用,并阻止泄漏。
- e6 j7 _* L; \* |1 I0 e: B3 x# s4 }+ v/ f/ L6 S$ l3 s

/ j# t$ F# x% U( q- e7 a2 [/ v$ xTrojan Protection(木马防护)$ |# f: A5 _% X. r8 G3 j7 j2 @% J
检测访问木马。$ ~1 z) e- T. T: h) r  J
7 H/ ~  \* j" y0 _  b

9 z. U* v; c+ w! ^Identification of Application Defects(应用程序缺陷的鉴定)1 ?1 G8 H" g' k- f
检测应用程序的错误配置警报。
7 B4 e8 q- m8 n! l) {( Z) ?8 d& T8 N4 U

: K. D5 X/ K( I# A  wError Detection and Hiding(错误检测和隐藏)
2 \* `, ]% Y% h检测伪装服务器发送错误消息。$ Y0 q' c( A: h& y2 b4 J

1 W; U$ [' g- h
4 o5 \* V" R) k$ ]  g9 V( R( H安装ModSecurity
- e. Z$ `$ P& c" ]7 H4 g  q软件基础环境准备
2 N9 M9 N$ C  f8 x( z下载对应软件包
8 U1 ?  W, ?/ v& y" ?: Q$ cd /root3 i- U/ L' k/ e& |8 \( E
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'/ M2 A8 B* d: \
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
7 q5 p  N3 ^4 M3 W' P  M. T$ M安装Nginx和ModSecurity依赖包! a. j0 D8 R3 Q& b8 a
Centos/RHEL; o, v! i+ f) V

4 L" [5 H5 P3 P  Z# j, M% P& ?* t* c6 b, r/ Z
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
9 l8 |6 u9 ~2 F/ D1 tUbuntu/Debian
2 E" J- @+ w+ E. y3 B3 A
0 M. A; B2 u+ M. B0 j
, r% M- y5 {' H9 h) K3 C# b$ 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) w7 Y9 o7 g: |5 p
编译安装ModSecurity" i$ l! G5 I# U, k' f+ D% m! }$ I
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
9 `) E! v& w9 R" c# G
" t/ l- ?3 q. J8 A# i) Y  B
3 R+ J" g6 ^2 P3 z方法一:编译为Nginx静态模块
5 x) N) A% a. T0 `. @$ o$ p- m* A2 X/ ^+ C
% T  Q% D" P5 f" r
编译为独立模块(modsecurity-2.9.1)5 P, S% l. ?8 w  @
$ tar xzvf modsecurity-2.9.1.tar.gz
+ A* c* H2 n1 C  z! p$ cd modsecurity-2.9.1/7 l. \, Q7 _, o
$ ./autogen.sh
7 n5 {  y& K# W3 F  i/ B$ ./configure --enable-standalone-module --disable-mlogc& v7 L& G/ l$ N0 y9 }
$ make; E$ ]/ c; _$ @+ ^/ {; d
编译安装Nginx并添加ModSecurity模块9 w2 }: ?* k% T7 s
$ tar xzvf nginx-1.9.2.tar.gz4 |2 j0 {2 j4 `$ K5 D
$ cd nginx-1.9.2
" @+ l% W+ i( e2 |, U3 H$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
# q( Q* x  E8 D1 {$ V" e3 [% T$ make && make install
# ?! u0 G$ [+ z: I0 M4 y方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
9 ~! I) A3 b) K8 K, t$ f
. c2 Q+ v! y4 Q" ?4 E$ D! V, R1 s$ v" u
编译LibModSecurity(modsecurity-3.0)$ N7 y9 i  q5 c$ \6 a3 M
$ cd /root
2 U8 x$ M9 Q" o( `/ i7 z2 _' C$ git clone https://github.com/SpiderLabs/ModSecurity# J7 j4 z1 C2 z5 a* P# X1 j4 M: m! C
$ cd ModSecurity* j7 d8 _) Z% b! Y5 f+ L3 K# g
$ git checkout -b v3/master origin/v3/master9 H' T; i7 _. z* ]
$ sh build.sh! U9 l4 |8 \# z
$ git submodule init% T& z5 Z) w) D
$ git submodule update4 K  ?6 v! M9 p% E8 E
$ ./configure
+ q9 H# f+ n2 Y& |0 R( J5 Z$ make6 n& b; d' S4 L. a  H+ _* K5 K5 K
$ make install
$ T7 S% s6 @. o3 p" pLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
3 v# @4 i' g$ n7 R  z. C
' i5 M& P8 H6 |# o9 O* z' m0 h! m$ K4 C
$ ls /usr/local/modsecurity/lib
# h" C1 m7 m" J% ?libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0$ e1 H! V/ E$ Q1 ?$ k' k( I
编译安装Nginx并添加ModSecurity-Nginx Connector模块
+ Y! L1 Z$ W- z: a( o5 d7 f2 P使用ModSecurity-Nginx模块来连接LibModSecurity  Y6 Z% g% e1 n

- Q7 B7 N5 ?: A- p7 Y( S+ H2 t
) K% X2 k3 s9 R' s. w$ cd /root
6 h: S' @: c: O/ |% l5 D" X+ |$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx; z0 H! F4 p: s( a: o" ?/ B# y
$ tar xzvf nginx-1.9.2.tar.gz
& @9 Y5 z; |! O$ ^, k/ K5 Q8 Y4 Z$ cd nginx-1.9.2
- M' K; \: N4 W, x7 L$ ./configure --add-module=/root/modsecurity-nginx. \7 r+ Y$ I- V/ D0 L1 h4 L; n6 w; h  q
$ make% I; g0 `: F' |; A) t4 A# j
$ make && make install
+ Y2 |9 \( n" q$ P) b7 f( M: p添加OWASP规则
3 t% k$ g$ f6 d# l; }ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
7 k/ e$ w4 Q3 Q4 R8 c
* A  |. S( H8 {6 C: j  e6 B
" n: H) ^  g! u+ B' N; A, Y下载OWASP规则并生成配置文件8 X/ T6 X3 E9 G) l
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git4 e+ b' l. ^- l0 \& b
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
6 i) q+ |5 q& A; b; s* u! f) x$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
6 o) }# V7 J% w! q. ~$ cp crs-setup.conf.example  crs-setup.conf
# q, \3 `6 q5 H3 k" B7 L  E: z6 n配置OWASP规则
1 @( h; y: r4 h2 m编辑crs-setup.conf文件, W. a; r; [7 H6 D) n

6 d/ f1 Q. U  m* c" O; S& _/ q& W) P8 L# h& T5 X
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
7 l  ?8 F6 _0 s" C$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf5 @, ^6 a8 m+ C  k: W6 K) p% c; i
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf# ~9 l% {8 ?7 e7 }) \5 u
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf( {8 u0 l' f* K; \7 T
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。2 ]/ g# f9 V$ s/ f8 W3 D9 F

& c$ v- @5 V3 w' I
: U0 s+ a; z% S+ Q* b启用ModSecurity模块和CRS规则
, z7 \& E, S* H% g/ r1 m复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
. _1 h% ^9 b& B, y" i
) O# _0 F0 Q- ?# u$ e) T9 G. O: E4 x/ H- d
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
  I# c5 ^) J( A0 C* a/ [9 i% N
: [9 c7 v2 f+ P& _1 k% S1 u
# i; K) L4 ^2 ~$ cd /root/modsecurity-2.9.1/
4 ?5 S1 P! H7 N, k% L6 N$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf    s8 H! [) z7 i: T, F5 K. Z
$ cp unicode.mapping  /usr/local/nginx/conf/
1 s& k( N8 |/ F% f- g! f. z( ~将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
: m) P$ q: G  @4 x4 b) t: s2 Z+ d( v& \

, y, X2 o+ B4 o$ vim /usr/local/nginx/conf/modsecurity.conf1 W- K- |$ e6 m8 V5 @$ \
SecRuleEngine On/ l* v2 }$ t( O; a# T
ModSecurity中几个常用配置说明:+ p, i- F8 P/ e9 \2 y$ o% l0 L
5 X* n# v& a  x, D* _+ e( q; V
% q5 [2 r& p- Q( Z  T6 h
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。2 v# `5 l- U5 C: C
; |3 S) Q8 G! d# h

/ a0 i' j+ h: j2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
: X5 }2 A1 ^( C7 S, _( t+ p  n0 Z
1 [0 g5 t1 l: j2 U
7 N! ?' s% b! w& z5 c0 h3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。' f4 J3 o. H' b2 x) v4 v8 J* Z- @
+ J3 ~* ]+ l' O% y6 z0 n

0 A7 S7 q6 t) ~# b9 L3 c4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。" z! [* s& c* o
$ B/ y; J% G! o9 n5 {' j

& b3 O' d: J& p2 ~在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
8 K4 k3 C8 \' A  l& I3 ~0 S" |. D3 m+ h: M8 A- x( G

2 R8 I- V2 u+ a* z2 Y+ G" ^" U( J3.x版本CRS, Y$ @! W* l9 D$ k# n
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs. ?  _& b9 m0 `7 e* p3 b
# 生成例外排除请求的配置文件
+ f5 x" H% t1 [  b; v) b$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
$ Z2 b9 U/ }- }8 X9 N# ?$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf3 a0 }0 U7 P# h! K+ e
$ cp rules/*.data /usr/local/nginx/conf
0 K! ~: E" A! ^" b2 ^' p为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
2 v9 q7 w( ?; v, U" Y1 W
. @7 a7 s! \* ]. `- H, g: Q6 A
$ vim /usr/local/nginx/conf/modsec_includes.conf
1 I8 \  t, T% t. E
3 r& _9 k0 I9 L2 b' 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
& L3 J) L/ k  z" M, U( D5 S, Q7 l
) M5 ^$ N& `: g! q, F! S, o3 Q
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。$ Z; C5 l+ w* `
7 z; y7 D1 J7 `* U. {

5 P2 N7 D% j, \; H6 E# {1 D9 D配置Nginx支持Modsecurity
2 r' W1 o/ Z/ s" l4 v) y# b7 W启用Modsecurity
4 f+ I6 u2 Q' m+ C1 N6 C+ ^使用静态模块加载的配置方法$ F/ K8 Q: O9 J0 X# ^" N5 E( V
在需要启用Modsecurity的主机的location下面加入下面两行即可:
+ G/ ]* w* t6 \+ Z' O" o) Z6 q2 u+ \0 Q) C, x
- n+ I: ?; V+ @8 O' {9 o* N
ModSecurityEnabled on;$ E& J5 I/ s- U, L' b0 i
ModSecurityConfig modsec_includes.conf;
8 s- m$ |( u5 o9 ]; i7 p修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
* T. p) Z3 Q- o! r; @5 w9 S- x4 p5 r! i
0 L# f6 Z$ l8 X  R' ?$ U
$ vim /usr/local/nginx/conf/nginx.conf
/ K+ G, q0 S' {$ h
/ ]1 X: `% B4 ]
* g. D  z! Q1 Mserver {9 s  q& X5 b& t" e! X  C
  listen       80;
: J2 p+ M8 h8 o5 o1 B  server_name  example.com;* s/ h5 h$ ]1 o  y) f( P% Q5 g

/ x3 d- w& [8 u4 w# L/ Q8 @0 U) J
: A( S  k6 t# u! `5 M  location / {3 g. s4 b7 M4 J% k
    ModSecurityEnabled on;
# z, n( h" v( `  V    ModSecurityConfig modsec_includes.conf;
; E8 f. V  K- _6 k0 h4 j8 ]+ ^    root   html;
4 H. |7 l' G7 h, Z" H    index  index.html index.htm;' W* E) _2 Q) Q: M
  }
8 `% r5 X. ~  s9 [3 ~6 z7 [+ k}
* A7 |2 c- }" t0 N! x( R, G  w使用动态模块加载的配置方法/ K3 r$ M# Y/ }5 `0 l0 j
在需要启用Modsecurity的主机的location下面加入下面两行即可:4 y8 K! J7 v9 K0 I% M, k! W
+ Y* i" m# T' R- C/ d3 {- n

+ j, `4 I+ r. Q9 nmodsecurity on;8 D, ^0 c$ E2 u( ~" A  x) Z
modsecurity_rules_file modsec_includes.conf;
6 c4 o/ Y% ]( T8 H. I修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。' @* _$ h$ e- _' s

( T2 K) q5 m* E4 R; x4 R& O1 N8 I! W& _- u! j" B* |  }# A
$ vim /usr/local/nginx/conf/nginx.conf
' P# _4 [% @2 i9 C& v" c
6 a/ u3 m# ?' ?4 d* e" {/ n
6 I' ?' {* t! H% lserver {
1 n3 \6 [0 x6 W- F* n  listen  80;- G8 i1 S7 l  j; y( G
  server_name localhost mike.hi-linux.com;; t5 m0 Z" v) l4 ]1 }2 L8 g0 q+ V/ l
  access_log /var/log/nginx/yourdomain.log;
4 L- x2 s# m  u' {0 _& a# c
* [9 p4 g! ?# z) V+ I  }( f. W! i( f( f/ A: f/ h1 P  ?
  location / {9 A9 U! w9 L& x1 r' F* h4 v/ u

( i" ]" E& M5 P2 B* o; H: h) R
5 C- G( t4 [  h+ g$ u1 b5 ]  modsecurity on;
1 C" D0 Y$ S8 u  modsecurity_rules_file modsec_includes.conf;
0 K4 J, Q. `- q: n# w  root   html;
9 r; m+ X' y, g+ o8 c% v1 ~' b3 C  index  index.html index.htm;
9 O$ D5 f" Z* s$ H}
: Y. q2 W4 G% d$ W}% G4 j9 ?8 k! U& A* \8 {+ K
验证Nginx配置文件; v" b1 Z4 Z+ F$ V% j3 a
$ /usr/local/nginx/sbin/nginx -t  ]: r! E" S- g% ?; Z
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok+ s& ~6 i" T. j# ?
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful$ R$ U2 w' |2 f3 x$ ^
启动Nginx( T) {+ ]- p) s- ]: x* w" {+ S
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
: U$ r7 p  R: }' ~' b# Q1 Z
9 O) O9 j' c! R0 p

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


4 e! M) U  l2 i9 Z5 j+ T

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-4-29 15:55 , Processed in 0.079323 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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