找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10664|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。" J! Y* Y  ]: l+ \3 y4 \8 b4 r
# l  m! l4 q7 m5 T& y" `

5 j% u, L$ s* _1 }3 ]& T在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。: ^- N2 L" o; n% w

0 {3 o, r% J- {, u
1 P1 ~. C) D% L5 [( [+ t什么是ModSecurity
9 G1 A/ K# K, ^. A0 _+ zModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
7 c: }8 F% h/ O( N/ @, \  {& u9 N! X
/ M+ S' P- m$ `$ k: |3 K
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
6 Z5 l# x( h. {7 j6 s: B' e$ D9 a" }8 D7 ~6 B
- X0 D  T$ R3 A' b! j# l
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。- C, P: R% W3 w! w
! k2 {3 r2 K) C- ~* q' |- u8 J! ~

2 A2 R2 i! ~5 t0 L官网: https://www.modsecurity.org// ]" f9 p/ U  C, x
% [& B/ u1 l; u8 |2 ?  ~8 J! M

) {/ G$ r; f+ ~% ^什么是OWASP CRS( Z% o+ V8 L7 B9 ~9 V
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
9 u5 n! B* Y9 K; n" E
9 l1 u" k- S# c* {  X' [$ O8 _- N0 Q
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
' G2 {7 E. f7 r+ b1 C1 p/ G  o) b
! R3 F8 G8 x* i% Z( h5 U0 a$ S& \7 v" t8 j
HTTP Protection(HTTP防御)/ l4 f0 [7 J5 x; n/ W/ F4 g
HTTP协议和本地定义使用的detectsviolations策略。2 i, X, \% L' \* {4 e& ]

( Z2 _0 N2 F& m8 T. }9 D' h  @7 b- r) t# u. [
Real-time Blacklist Lookups(实时黑名单查询)7 Q7 d' p8 o& Z% C- L
利用第三方IP名单。
6 T/ O2 |' h0 q4 ?9 ?+ z7 l2 G2 ?
7 g9 |5 \* y( Q) C- v7 W$ p$ u3 [0 l* L& ?: g- ?9 R
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
- f( A, s9 q9 x防御HTTP的洪水攻击和HTTP Dos攻击。
* y5 u- s; }+ s3 J, p. M3 Q% u' P
% g5 b7 `% c  Z, ~: K2 M! {* ]1 J
Common Web Attacks Protection(常见的Web攻击防护). _; P# s- R1 f
检测常见的Web应用程序的安全攻击。3 f: @% P0 e; W* j0 I- K
5 M6 B. F. p) q$ \
% ~" q% `+ i7 J
Automation Detection(自动化检测)
" G! S% r2 |3 j" u# ], `检测机器人,爬虫,扫描仪和其他表面恶意活动。
: o# q1 d& Q. C; {; e
  E, U  V- _; T, |* T4 S( ]. o3 e4 {2 d8 [
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)6 x. V; v# G/ `, ?: [  C& Z1 S
检测通过Web应用程序上传的恶意文件。
+ J7 O, e7 Z/ U
& h1 D; M8 R" T; a4 m' m
/ ^6 {6 n1 J1 {+ H0 x6 _Tracking Sensitive Data(跟踪敏感数据)
7 q) d+ J4 K& U9 |) U( t0 g/ s信用卡通道的使用,并阻止泄漏。
, z! U3 l6 n% [7 x( D
' F7 R2 D3 v: v( c7 e
9 k/ i& C% z0 Y0 DTrojan Protection(木马防护)* u* z2 Y/ b2 p2 A
检测访问木马。& W; h: r, h9 l0 o7 T4 b% F
/ h5 |: Y0 O$ B* v; h, _  ^+ T
& l- @- @9 @5 G3 s
Identification of Application Defects(应用程序缺陷的鉴定)% f) ^( q% i) {2 e/ a7 B
检测应用程序的错误配置警报。
+ X$ e6 P4 m1 I& @) E/ `7 X+ U+ q; K8 ?0 Y7 B# h) I
. o# O  z( F, @% Z! z$ h
Error Detection and Hiding(错误检测和隐藏)
' ?+ h2 a  F! U: f' N8 s检测伪装服务器发送错误消息。
" M4 o4 E. w8 m; N( y: a7 S% I# B# e8 t: h) f4 V" _, E2 c
) w4 {* |# |5 g' C* E. V0 Q" I
安装ModSecurity
/ J# U( ^: Z( |4 l+ o! P2 I! L' x! {软件基础环境准备4 X9 r- C+ `- W: g1 l
下载对应软件包
  c- Y. ]* g0 Z" }6 u$ cd /root
& a4 R# Q; p( b$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'* P5 X  Z9 ~0 R  Y3 x
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz9 K- u- ^4 V1 Q/ W, `
安装Nginx和ModSecurity依赖包+ l3 s: e' A; D' W& ^4 X7 t
Centos/RHEL+ S3 ~+ C2 ^: }6 A6 Q* f$ j9 n; _
. g# W+ f, Y6 F, Z
! \3 r! b/ F. k9 t$ R
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel* I3 ~+ M9 n9 I4 G2 l; u& h2 ~  H* U
Ubuntu/Debian
& j6 |6 t$ |8 P5 \
) ~0 u" R7 `4 y- C
: D% X0 Y3 z" S$ 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
  [9 I" t+ |; ^' D/ w  D: r编译安装ModSecurity
' q1 D8 D* m/ D( u" u( INginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
! e. m+ k! Q: {+ l& s
  s% h: J4 P8 q$ o& I* o& `* P! x5 v& E% I' y
方法一:编译为Nginx静态模块
3 Y9 W- ^7 e; ?: K* ]7 s  A) ~2 H! Z& g" y" U7 x5 x

/ c4 Q1 I, k9 V0 m" o编译为独立模块(modsecurity-2.9.1)* a+ W+ u8 Q9 @4 r2 N2 Q1 z
$ tar xzvf modsecurity-2.9.1.tar.gz: e2 \+ q. q5 @8 a2 ~* |4 c0 L
$ cd modsecurity-2.9.1/
% |1 v1 t! Y2 m# ^* q/ L$ ./autogen.sh7 R, d" G6 A. m' K. m# Y* H# H
$ ./configure --enable-standalone-module --disable-mlogc& b% F8 x& \; r# X) G9 f( o  [
$ make/ C3 u; y. P. n3 s# f1 L5 O
编译安装Nginx并添加ModSecurity模块6 g: T" ?, o- U: w  j! Z" G& ~. M) k
$ tar xzvf nginx-1.9.2.tar.gz7 O; G, L* z$ l- p& ~( s- v2 R
$ cd nginx-1.9.2( d. U) B% `% N4 [
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/7 Q/ t- W: h5 z4 c- _0 s, a5 C* F
$ make && make install( B$ y7 }2 F8 e& L( ~
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块. I) W' |" c$ G% i8 M% _) g* _! `

" W$ |% x' M) S1 \" m3 r' ~5 P5 X- D& L$ V
编译LibModSecurity(modsecurity-3.0)
3 u) s+ K! V- R& _, S1 ?$ cd /root: J( e7 O1 n# u8 Y
$ git clone https://github.com/SpiderLabs/ModSecurity
* a. `7 v  U3 w$ l$ cd ModSecurity
/ m( Z4 K6 L8 e3 C/ A$ git checkout -b v3/master origin/v3/master+ b$ R% Q. s( Z* k% }6 f
$ sh build.sh* z; p+ n/ i& T$ S% o
$ git submodule init
# C: K; B7 D3 t( m5 Q$ k6 c' D6 D! W$ git submodule update
8 L; S7 f) v! ?$ ./configure1 v) R! N  g* j9 K# D
$ make/ q/ a$ _& N! I
$ make install8 J- C" a* W- Q8 Q2 Z( [2 \9 b7 ~
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
; l0 v/ O. O% e5 y4 P# x2 f! h! ^
$ F' N" K% f% M4 f- H: N3 ]
( R' ^9 v, T% `, x+ O$ ls /usr/local/modsecurity/lib
6 E+ A! }0 {: D! g1 llibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
3 O3 \! @7 a  `/ m4 I# Q& _4 k/ ~7 R; @编译安装Nginx并添加ModSecurity-Nginx Connector模块
- o' ?/ Z! z2 L使用ModSecurity-Nginx模块来连接LibModSecurity
' T7 z' B* w, n9 \! R' c' y0 _# |( X1 W' _; u9 X5 D: l; ~" y6 ?$ C$ h

8 O# ^1 L; Q5 N9 j+ _  j9 Y$ cd /root
8 U& X; J5 n$ l$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
- @! c; i+ q& w3 s$ tar xzvf nginx-1.9.2.tar.gz
1 r& a0 r" P4 Y, p" ~* V* R$ cd nginx-1.9.2
- w0 `& s+ R$ g" \3 w5 B$ ./configure --add-module=/root/modsecurity-nginx  f1 |+ C7 o* z  @4 Y& ~
$ make
, B" Y: p& i$ I. v+ ^$ make && make install" Z, k( Y! K# @! t
添加OWASP规则* J: h7 R% E+ n2 _1 f
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。* m% r, q2 H. u9 s8 K' T
6 a; e2 \; b! i* e. m
3 j' T  e" k5 m: z- A
下载OWASP规则并生成配置文件
+ Y- F0 h9 T) L& T! e) x$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
  o$ h  L/ M& C8 _, G1 M$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
/ ?+ `  x/ M$ @* _! P, V+ E$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
" A% T3 E# |  u$ cp crs-setup.conf.example  crs-setup.conf
; x. a: {" n$ ^配置OWASP规则
' O7 G. X  K& Q  [编辑crs-setup.conf文件
+ c- v' e' m1 f( Z! g) b8 G, J% g! O2 F' f  \. d5 F+ E
) r9 `) `( O0 ~- c: @; t' r( m
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf4 W. O  g. j' f6 n" ^, u8 T
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
. t& q* i' r/ h' l/ n+ h$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf, q! n0 z$ T  i
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
6 g) C  i9 ^2 ~( u默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。0 i. x2 b7 Y9 W0 i5 e, J9 k
3 ~8 r& c6 q# ^$ r  l3 f& X0 p

+ `' y( U( |7 A: Z8 a, e0 f+ U启用ModSecurity模块和CRS规则
0 u9 f& l+ L2 @( B* `8 j复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
5 I% ~' F' ?8 V* h
9 k0 I! F# ?2 l5 A- I1 Z& {! u) v5 n  _$ r3 u
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。. e5 R8 E" T2 n$ A
+ }! x# B. Y; c- h

( u, m  W* X/ Y# @7 b8 n9 C4 x$ cd /root/modsecurity-2.9.1/
% T) U# p0 L% k1 |. F) \$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  $ o; u2 Q/ `- P* V3 w# C+ V) w
$ cp unicode.mapping  /usr/local/nginx/conf/
5 {7 G  w. \; E* J将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
" U4 a* s5 u; u% l; x
- o0 |% S: ^6 |' T2 u6 M" n3 S
& m3 D2 s  Z$ N! W$ vim /usr/local/nginx/conf/modsecurity.conf- m- f5 x% e) M4 C8 `; G. }
SecRuleEngine On; K. O3 l+ x" [& x" R9 e$ S0 p
ModSecurity中几个常用配置说明:# q8 z+ E  ~0 t% Q- }# L' {% ?7 n9 l
6 J' p2 Z3 R  j, }, Z& T! C
- B, p. a) m: f& V% ]' g. c4 W. r& A
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。5 l, [* ?& K' N8 X# A
( Z2 S( }( v7 {# n, `! g

" Z4 C9 C1 H1 o2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
3 N' L7 B1 h. j! p' V" x1 v2 W3 p+ M! ^. }* u  I& f
- x! P* R# C0 j+ _  `
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
! G/ m1 }! {" X5 \, x% [* i( O9 m* b/ D
* e1 {  Y! Y+ {! J8 h1 i$ |
2 }& M, A$ Q, b3 z4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
5 z  W6 |* V4 i. ~+ O2 c) z9 P- i+ n* g1 l5 Z7 O( s5 M
3 S/ S' s8 g( a3 g% f, Z* N
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。# Q2 }& P: n, e. r8 ~5 C: N
3 t8 ?5 }& f6 o  c8 }8 q
4 |5 }2 J0 B# C& p) B
3.x版本CRS# q1 ~5 e3 ~* T% k7 G- a# ]
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs8 H  @8 q' L" v, E9 v7 H5 v
# 生成例外排除请求的配置文件
0 Q3 O6 a. S* l/ g8 E& m+ B& e$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf/ A4 [8 {( k# @8 b9 }" E
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf% R( V+ I- ?9 U+ t' S  n
$ cp rules/*.data /usr/local/nginx/conf
1 h, [# w6 |1 X为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。5 L: U' D5 H1 G3 u8 J

8 y* N4 f- \* @6 e  k
( {. Q; `9 Q" V* S+ l! z+ s# G$ vim /usr/local/nginx/conf/modsec_includes.conf+ r5 ~7 b' R# {

9 Z# `' e. y. M8 u
[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+ o9 F; \1 O6 B. c/ z! @. O2 v7 u6 j  n
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
$ n( _! g' Z8 t9 q7 V9 i% I9 ]. O
5 Q2 a$ B5 Z9 Z; Y4 D  R$ h" J/ ]; H5 H; s
配置Nginx支持Modsecurity; G2 G6 b' x1 t
启用Modsecurity
. _4 A: K6 o; s- U, b: K! c使用静态模块加载的配置方法
3 M6 N) s9 S1 g% v在需要启用Modsecurity的主机的location下面加入下面两行即可:
2 \1 u; r/ R& t/ M/ i9 R7 p: r1 o7 n; |& ~- R

& I( x2 t, @; n9 u7 T: pModSecurityEnabled on;
, ?9 M* _5 b+ X; `2 gModSecurityConfig modsec_includes.conf;
) O4 a7 ^) }8 \! O. J# \修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
( r7 [* T/ R, O* @' H+ c# c) |9 c" s7 ~+ P# M
4 X- K7 x" V9 ~9 y% {
$ vim /usr/local/nginx/conf/nginx.conf
- d" n( ~# [- u4 v9 Z& d; v, }, @$ }( M0 x7 |* r
; a$ N) M9 S0 p, j2 S
server {8 a  o7 |. K% K# s+ N- E. s
  listen       80;
: P+ |& U( `5 ]! W6 y9 p' b  server_name  example.com;
5 }  F( n5 \! u! g) ~! K7 B3 L7 V" z2 I8 v
, _* `( r* _4 v% w$ H/ r
  location / {
% r# }7 Y0 M" N    ModSecurityEnabled on;
. x6 `+ G' [( t( `' z    ModSecurityConfig modsec_includes.conf;. ]8 p% w3 o) ~( N6 U0 `9 ]
    root   html;
& c6 U& i. f! j; g- @1 \; E    index  index.html index.htm;
  A& Q7 g& J! y; {: [: s/ O  }0 C3 W% a4 _# [+ {
}
# J! g; A6 U/ D8 C& T& i3 g7 \5 F使用动态模块加载的配置方法
' j6 L  Y& z3 K$ `3 V在需要启用Modsecurity的主机的location下面加入下面两行即可:
/ s+ s  _( k' v4 V- f/ T1 h4 g
" U. g9 e7 p1 s/ d- E% _& E- p5 C& B6 [' X8 o0 h/ {6 U- x; R) `! i. F* _
modsecurity on;  c5 {; d7 [* U+ P
modsecurity_rules_file modsec_includes.conf;
8 Z% `2 R; g- y4 G. ?2 r# W) L! R% Q修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。  p3 r5 c# f, N$ j, j0 W8 N1 ?

+ C# j; G" L; h2 [" C4 e0 ~. F$ H) Y4 W, _/ S/ e6 y( a  ~: S
$ vim /usr/local/nginx/conf/nginx.conf; J. `8 `* k) a9 @2 ?

" n# D  \4 i& a/ B- n, B
& i/ B) r- E- \. eserver {6 \+ U& t9 j7 c$ G+ p
  listen  80;
/ V% E; ~: `7 L7 h. k" D  server_name localhost mike.hi-linux.com;
1 _- ~) J2 H/ w; x& i  access_log /var/log/nginx/yourdomain.log;7 l, A1 N6 g6 u7 N/ O! s

# [$ S! p6 ?# J0 g& z1 q
# s; q9 T4 Z$ d5 Z2 f1 d6 @" [  location / {& \# y' K$ e, s4 L
% W/ i* B: e0 c$ X: Q& e

4 m6 V: ]. r4 r9 d6 T  modsecurity on;- Q% ^' @- ~# H1 t& J0 y: W5 I
  modsecurity_rules_file modsec_includes.conf;
" b; e7 z8 U2 E; l! o- s4 }5 o# p  root   html;
) E! I4 U) V% ~0 d  index  index.html index.htm;' y3 k1 O% k3 L: n
}3 m/ q' q& d- F' }3 L9 f. s( U# g' z
}) m5 B# \" [* u5 J! _" R# f. w
验证Nginx配置文件- E( G3 ^" h3 C
$ /usr/local/nginx/sbin/nginx -t
0 t8 S1 V, w7 M4 _9 K6 bnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok$ @2 f6 C! l: _7 p* b2 H+ _' c0 E
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# ?/ S% O" P! U3 ?启动Nginx
' g, p, R* c, k; z  L$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
, Y( ?# m  M, Z+ L$ K8 T
- Z- Q6 D" g2 u+ h6 e6 j/ ~( T

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


2 i% J& v) \! _* L# r0 V5 _

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-3-29 20:21 , Processed in 0.085716 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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