找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12782|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。" U. c" f0 }  Q  ]! }9 i$ i9 c3 p$ \

* }. l1 K7 g; P( T6 ?3 I, N4 h
2 i# p3 p% `, e) b在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。5 n$ E+ N6 M7 u- J) y, p

9 m' s2 I  Z; o& k0 a( M7 X
3 Q7 z6 ]! q- N0 P0 L) u9 E, H什么是ModSecurity
. a9 A) g) ^/ v: b! AModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
7 \4 [% R9 e1 ?+ b; U6 l2 D+ d% i
2 t4 j/ l0 `' R) j: E
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
" g; D9 V: ^- @5 X% Y/ S4 L/ t9 s1 e) k0 l4 w! F0 ?3 u
  s8 S- C3 M; \& M" L
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。- N+ E/ j$ `# g7 a8 e

, X( ]) x8 p; Q! S  n# X- ~0 r' A
7 S! E) q% n+ g5 ]1 c- |官网: https://www.modsecurity.org/
% q3 V/ s, G3 b( {1 v, X- F
* r. T* q% ^/ o; m) C
, y8 h7 N3 o5 h2 J什么是OWASP CRS2 V$ o2 G1 U1 [) r% m2 w: p
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。5 @# C4 N- z" J4 T% u

2 y# x$ T; ~4 M% ]5 j+ K  n) ?: O0 o1 p; L
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。% l3 O* m, T; m
1 {6 n+ F# c1 a0 g
1 c- q: ], P( b* N/ h: W
HTTP Protection(HTTP防御)
  m% T* _( S2 u( i3 S* ZHTTP协议和本地定义使用的detectsviolations策略。% \1 V7 Y) k9 M& J. j4 u

! {) b' L3 n+ n0 m! s* ?; N# f
' o7 f1 k. ^1 A1 c& l" kReal-time Blacklist Lookups(实时黑名单查询)) o* G" G) L+ p
利用第三方IP名单。
4 \" V4 _" h5 `. H: q, g: {8 B$ ~( v- k7 P3 r* M

2 w9 d! k, l4 H5 gHTTP Denial of Service Protections(HTTP的拒绝服务保护)
& |) u2 A8 I& U  u0 k6 [9 m$ L1 E防御HTTP的洪水攻击和HTTP Dos攻击。6 K3 c, x  E, ~4 {5 j3 Y0 K
" P8 w! ^' a* w9 U+ l5 F

2 U- ?' [9 P. ^Common Web Attacks Protection(常见的Web攻击防护)
6 @5 X0 V9 l$ s" u- B1 A/ g检测常见的Web应用程序的安全攻击。
$ i- q6 l: F. E- b( R+ s+ W  S
: b! ], F* P6 Q. b+ W
! X: Y  N: B, v! M6 x! nAutomation Detection(自动化检测)
. Y4 I9 O# ^. o* \- ?3 `6 w检测机器人,爬虫,扫描仪和其他表面恶意活动。
/ Y) U! z- P8 x: h2 x& M1 |3 m3 D) S# D# I' z: J6 N6 \

0 e: H, l$ S5 q) C1 U% u, k3 }, |Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
3 l! v* I. N2 x# {检测通过Web应用程序上传的恶意文件。
$ E* X$ @% W, L) i& ~( N; l9 i9 ~7 v! W+ t) ^4 }# T& v* j% e
# d  ^; c! W8 z  }4 @7 n
Tracking Sensitive Data(跟踪敏感数据)) ^8 c& x3 X$ n# H) k. s3 G& u6 z
信用卡通道的使用,并阻止泄漏。
! o  @6 h; Y- [* M' h$ {( C, C" B- x6 h$ ?6 y

% N* v! k  y  G# g2 L6 e6 _6 ]Trojan Protection(木马防护)5 I# B( I  f1 G4 w) V( [5 y3 g, Y
检测访问木马。" ^" [6 w0 c$ Q6 O" |3 X! ?! }2 S

# V; n2 R- N: X5 g- c$ q4 B9 p; R3 D' Z0 I* |9 S0 L( t
Identification of Application Defects(应用程序缺陷的鉴定)
6 |9 l9 J: G- y, o检测应用程序的错误配置警报。
' k6 D# s/ q2 I; D
+ ^1 }8 {* B4 I3 {! C$ }- }* K! ]
Error Detection and Hiding(错误检测和隐藏)
/ o) J2 r1 `  a9 v检测伪装服务器发送错误消息。/ u; A1 I4 V/ y$ }2 J

9 b# [& Z) k) `8 H
# \, w- Y' W. r# F安装ModSecurity
: d; e5 a# [% m5 W% ?+ N: W' N软件基础环境准备
* r2 c# L5 e$ @5 M+ J! J( @, S下载对应软件包' w- j' Q' R- k  S; s. {
$ cd /root8 b7 A9 ?+ r! c) w/ f  A* G
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
6 w6 j/ |9 h* J/ `; O  f$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
/ X3 v' g. T; @. d  _! K. F% s安装Nginx和ModSecurity依赖包' X2 A5 V. g% f! L3 ~
Centos/RHEL
! `5 @/ |( u- Z- ?- d  W  Q3 f$ l1 v0 n% ~" P3 [- `

, [+ @; u; C; C% b2 w" F4 K$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
3 A" Y8 I% L& a7 Z3 H7 wUbuntu/Debian( |; d1 E8 _7 \$ j& Y

; W: b" j" R( P
2 A. U7 y+ K/ z/ c7 J$ 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% W; Q9 |( r; [9 }
编译安装ModSecurity9 l1 t) \$ A0 g7 h; |! m4 z  b& _
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
: r8 {# k' }. s& l! `/ H4 c2 Q
3 L) B5 Q6 m# H% O
方法一:编译为Nginx静态模块
& R- H8 x" [4 `" g2 J: j9 S; |
: B; R- J# s' p4 i% o0 |. U2 @: [4 P4 g, m) ?& G( E; o$ E8 _
编译为独立模块(modsecurity-2.9.1)1 s$ S$ F3 I- v
$ tar xzvf modsecurity-2.9.1.tar.gz, Y# i7 D8 q7 A/ \0 d* J  O
$ cd modsecurity-2.9.1/
( |' I9 Y, q: K$ ./autogen.sh
* K( s' S  M& ?, k' y# d1 F$ ./configure --enable-standalone-module --disable-mlogc# l5 @; D3 B& X
$ make
$ {& L( T  A( s0 V  z编译安装Nginx并添加ModSecurity模块$ x( \6 q5 K- H% \. U, Y6 D8 w/ Y
$ tar xzvf nginx-1.9.2.tar.gz$ G) l7 K, t! g) n% c7 j4 x
$ cd nginx-1.9.2: r% F7 }/ @' b, g# u2 A- `3 o
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/! W: Y3 Y& L, }+ F8 o& u$ d& d
$ make && make install9 d) g9 U, B+ |) ~, c( ^
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
9 _; f0 C' v" T! g7 O9 `% p3 h) @9 w0 U! v

% ^! f5 X8 Y0 y* S6 K( I; z$ T编译LibModSecurity(modsecurity-3.0); Q- s0 n( f4 p3 }4 C) _9 b
$ cd /root' Z6 v% c$ C0 Z
$ git clone https://github.com/SpiderLabs/ModSecurity& k$ g6 L0 D' Q+ M7 W
$ cd ModSecurity+ b+ b3 ^/ f; x5 {
$ git checkout -b v3/master origin/v3/master  X& D. d% P: E: e# P7 O, b
$ sh build.sh! ]4 `9 L2 W3 h, {4 w
$ git submodule init
( @3 K" ]) q5 c3 o$ git submodule update
6 \( P4 v5 e$ h2 c) W5 V. c' {$ ./configure
& W0 T! n6 x% }6 I/ k! b/ N$ make
- I4 v' ~3 n3 M( j' ?$ make install
6 {) B* z1 E( s! k+ o2 c3 U0 ZLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。: H% O  ~. k+ B2 B) c

3 w, G) g/ p+ L
' D0 k, x# x  o7 T+ {$ ls /usr/local/modsecurity/lib
* X' N: `9 E6 t: nlibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
( E# Q5 F/ c; |' i* r5 a+ ~编译安装Nginx并添加ModSecurity-Nginx Connector模块2 R8 y2 a4 [$ d! m1 K
使用ModSecurity-Nginx模块来连接LibModSecurity
# v9 _" L7 E, F5 E& ^
/ {% G" s  w8 i. d2 Z0 ^
' s* B# v9 o! {# _/ I3 @% t9 Y# T/ X$ cd /root: x& ^2 C: l: g9 Y
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx( g9 T0 X* ~2 C2 Z
$ tar xzvf nginx-1.9.2.tar.gz& }7 R' f% c6 J5 q0 ^
$ cd nginx-1.9.2. b9 d7 L# J7 Z7 i. C( N: u
$ ./configure --add-module=/root/modsecurity-nginx
% O# l0 Z1 Z  T: ~% G8 @& d2 N  P7 A$ make$ G$ @& d' L1 b+ U- {" H; A
$ make && make install
! T1 {, f! M5 [- n  o( E添加OWASP规则
9 \, y8 @' n% XModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。( Y6 o% e- U3 a' L& i3 s- r: d- e1 c

! S: I4 W9 ^7 s& m6 ^
: E1 Z4 |- b9 x- V/ F. p' Y5 `下载OWASP规则并生成配置文件
9 E  [. O5 ]: g  U! p/ A, k$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
; H) t6 \2 u3 `- \; T  P$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
8 x5 ?. _2 L" O$ cd /usr/local/nginx/conf/owasp-modsecurity-crs% Z& j" p4 K$ \* @
$ cp crs-setup.conf.example  crs-setup.conf
+ c# P- t! @  |8 W; ?7 R配置OWASP规则8 W3 @* ]1 F. a. C5 w. ^- K
编辑crs-setup.conf文件
" n( @( o$ q. J0 h9 O8 O& A' ~* i9 @" G7 \" t

0 {* u6 V5 e* C$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
/ u% G( \3 ~' n; O0 _$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
) |. M/ g/ u9 ~6 @. x$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf/ u- @# o# D$ M
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
  t% h9 M+ [9 S6 N5 \默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。% {( v/ d0 U9 Y1 ~
" z' R0 E7 Z+ {' P

7 ^" V' `! k  i+ G# P' [2 v1 `  y启用ModSecurity模块和CRS规则! }& q9 A+ e* k8 n# w
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
5 m# O" {* F& x+ ~% f) }' Y  r$ h# W- r9 a

4 e( U4 I- [2 j! xmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。% P# ]1 i0 l% G, ?8 e! j. B4 S

6 P1 a1 S! J, r/ F$ @( Z3 t& F
- P2 A+ W% l6 k' e7 n$ cd /root/modsecurity-2.9.1/
: _+ K% E4 _, l1 F1 t7 F$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
2 Q- G* v9 N- E" e) `$ cp unicode.mapping  /usr/local/nginx/conf/
4 B! [) j2 [, Q0 h$ E7 m. ^. t7 s( h6 m将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。6 A5 j4 H% ?/ |0 S

/ j+ _8 s; `  D0 p8 L( ]2 w2 G" R9 J8 d* X
$ vim /usr/local/nginx/conf/modsecurity.conf
, J% V, [  A7 f- U3 BSecRuleEngine On$ f% c: y& z- M( K1 N2 i
ModSecurity中几个常用配置说明:
' H: k3 S+ I: q) }% ]# J% K$ B( B, L! d

+ z5 ?5 h' @5 _5 f; o+ d1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。) b7 C* [- g9 |$ ^6 s
1 ?5 t$ D! o4 u# ]& \- |
7 Z8 _; }# }4 j5 N- t
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。5 v3 d% Z! r8 h/ X) T" \* }4 b& ]

# w9 p! x  L( x$ i
0 M3 V9 A7 }$ @2 c3 U, C9 ^- M! A3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。7 ?5 o- b, ^% b: n

* k1 X2 c/ m  j+ @
  ?" H+ R+ P$ ^4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。7 {5 l2 z2 R) g* o0 ^" s- S7 ]
7 @- v+ ~% I( z: c/ h5 V" ]5 [3 C
" f% ?6 |" v. t2 V
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
0 t3 Y% P( u8 [6 ]% {* n3 L: h' p+ l
3 @8 o' }  |! T6 D  \6 W5 C
3.x版本CRS9 b1 f7 a' U& f
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs0 }) a+ `" n0 d* B3 j7 p
# 生成例外排除请求的配置文件% c2 k" K2 J# B8 o* F( \; d. w
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf$ c  p, C( n" I3 y
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf9 |& N6 U2 s7 b$ X" z
$ cp rules/*.data /usr/local/nginx/conf3 [3 S- A1 C4 l0 J3 V; |  }' [
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。) Z# s) k4 \  T) e: x6 d+ n! l
7 P$ r) P2 w1 [0 b2 n' j3 i/ p' U

- q; i0 [! f: S4 B$ vim /usr/local/nginx/conf/modsec_includes.conf4 M+ `( M0 D0 [) |% C# t( P

+ ^) r/ y% M! R; u( H
[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

( I6 A2 q: I3 p% `
/ U- v" H: W$ q" T: N注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。) F% P% o0 x! i9 K8 x2 i

4 T1 H* d1 a/ s" m0 Q) j; Z" s  S4 V& L2 x( n
配置Nginx支持Modsecurity- m1 G3 E  h/ f# y
启用Modsecurity
( n& q, F$ w6 F' W# J9 Q4 b6 l使用静态模块加载的配置方法
1 v# L! v$ H2 Y4 k- n" q& e0 _在需要启用Modsecurity的主机的location下面加入下面两行即可:
* j# y" |4 f2 x8 S, g1 D3 N
) L+ C2 I$ X  M
+ ?5 D& u& y  M& T4 JModSecurityEnabled on;$ J, N4 z: {6 }( h7 |1 M. K
ModSecurityConfig modsec_includes.conf;
7 H' v0 r" r* i修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
+ v# I- I+ U" I  h& P; O! u4 E3 L  I' \1 |8 ~) @
6 Y% o1 t7 }7 z- L& f4 U
$ vim /usr/local/nginx/conf/nginx.conf$ c' m3 L; x+ y6 d

0 e! W4 _; y' B0 a9 c) q7 W3 ~' f* j" j: x
server {3 E  i2 ?) n2 P0 S
  listen       80;
7 W7 _& S! Z5 y, c) J- z  server_name  example.com;! S* ~2 j" l- A+ t0 Y

: F+ v: ]$ e: k2 B5 E& }7 Z
: ~4 Z$ K5 z$ Y6 a  location / {
  v8 f  G5 v3 u  m7 T3 N/ Z% s! i    ModSecurityEnabled on;' P- ?( S$ o9 b7 `/ A! |+ h
    ModSecurityConfig modsec_includes.conf;
, `2 V& P7 j/ `7 ]% F$ M0 f( y; n    root   html;
% ]0 i' B! b" Z0 u    index  index.html index.htm;
5 Z( T' C/ _% s* A: m2 M5 ?; ]  }
4 a2 _+ D) T) y" k}
# g9 j5 w4 C( @6 a# H/ g% x使用动态模块加载的配置方法; a3 a  a) A: ~1 D9 l
在需要启用Modsecurity的主机的location下面加入下面两行即可:1 ^1 R6 V4 x- Y! K5 V5 N

* D3 Z  ?7 k9 ^* h! p8 r7 |  P% L3 @# i  [2 N3 f
modsecurity on;8 Q& b2 U8 G1 R  g# G
modsecurity_rules_file modsec_includes.conf;
7 Q. t0 E! R! q8 z, I& Y修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
. f* _6 ^6 J& z  w3 T) y- ?" n- U. W7 o3 q+ N4 {$ M" f
7 d1 r5 D6 b0 @8 R+ Z! n$ G
$ vim /usr/local/nginx/conf/nginx.conf
8 g! K8 C6 A& s9 D" j3 a( M/ o) C, g# v
' E: [- G, X. H9 t
server {# T' ^% @! p/ ^" n
  listen  80;* H/ X8 d% N  h
  server_name localhost mike.hi-linux.com;
) @, v* g/ y8 O& O  access_log /var/log/nginx/yourdomain.log;
$ Q9 m: p2 Z, g- W& c, H
; c/ u; e* Y' d6 y8 A  E! q2 X2 C0 @4 S7 W
  location / {
' ~& t( I& V3 r2 s- ~- q. E8 e# Q- i  \

8 O- S: e9 i2 v5 o  modsecurity on;$ i" Q% ?% @$ _
  modsecurity_rules_file modsec_includes.conf;2 S# H# H( h8 g5 V
  root   html;
: [8 p" F; i" s6 X1 \) [3 n  index  index.html index.htm;' ?# }0 I: Q2 p, \2 ]( F. b; w
}& S% J! x* G5 S/ b/ |( n. M  c
}" `6 q( L+ G* d7 i: X6 r5 j, E" X
验证Nginx配置文件
8 M# y( s& X5 ?, }7 t# q9 }$ /usr/local/nginx/sbin/nginx -t: x& b8 B0 e5 S& \
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
/ L" R8 c% R8 c6 d) Q9 ]/ Hnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful$ r! O$ l" R. g8 M
启动Nginx. H& o# c( ~2 V" C8 q5 q
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf0 I8 r( I1 W2 ^# b. c; f3 \: \/ j
9 r9 X2 c: N. @. f' {* ?

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

. K! _6 J  L( L5 k; R

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-11-14 17:52 , Processed in 0.090105 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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