ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
/ a6 {# g/ y5 }: e" p$ `! \! P. O* [8 Q+ x
8 O" C6 S7 Q# e u( ~
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
1 n( z j9 k" {2 A# A- a% N* m2 ?% G- z- P( l* ^
/ w1 H3 `1 f- p- }# M, g什么是ModSecurity
/ }% |; O7 N0 k$ JModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。+ I P9 E3 P' l: l
( T1 w& K' L4 w3 L9 C% s
& ]. G: i: k' Q! a: a) o; L% LModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。! d9 ` e6 j9 e
+ k8 \+ o& |* X. E% |: _
; H3 E D# W; U! G. }1 \ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
3 S0 d# l% W- p% |, P3 D: x5 a! ?7 D$ N, i9 A. n
, Y9 I% \( q8 u. z, o
官网: https://www.modsecurity.org/
# c5 R3 q F1 F$ [7 O" u( z6 e0 A) G2 I. k- ^$ m
8 G- ?- g/ j; K& E. k; C+ ^1 j
什么是OWASP CRS
5 n# ?9 e$ M& H5 QOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
1 m! x& k+ e. l, Y8 }( f7 J) e$ k) H1 ?- t' ^( ~
; d9 g2 h' _: @6 Q$ p* k# K) YModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。2 H7 ^4 Z- r$ o6 ?- K( a
( J- a7 \6 \7 t0 j! U
# R$ W3 x5 i* w) @+ G7 CHTTP Protection(HTTP防御)
& D5 F6 D) P- X0 K8 {% IHTTP协议和本地定义使用的detectsviolations策略。4 u; y; i* a2 M1 R) C7 F w. e
. T$ a9 H! u5 J3 x" s5 a, y3 g6 s
7 v3 |, G1 e: d: FReal-time Blacklist Lookups(实时黑名单查询)
% Y- I+ l0 q! g/ d利用第三方IP名单。9 X! J8 q9 N$ \* G" Q: B1 d* p P
- D3 c% h5 }( l/ q% N1 r! a
* m9 \0 g# ]) t/ THTTP Denial of Service Protections(HTTP的拒绝服务保护)
1 }; s* i; M" y防御HTTP的洪水攻击和HTTP Dos攻击。
# |6 I. D5 Z0 J- ~- { s% y! U+ Z+ t( a" N9 j7 I N/ @
6 n' }) g# L* h6 l& v- xCommon Web Attacks Protection(常见的Web攻击防护)
8 x: c5 Z* @: M, r检测常见的Web应用程序的安全攻击。# Z2 v4 ^; T6 }7 p0 ^- h# j) T2 o
w: t, v" n" U; w( S/ o5 @
/ O* |( x& q" N5 a
Automation Detection(自动化检测)2 m! v8 T2 c" B$ {; Z5 Q
检测机器人,爬虫,扫描仪和其他表面恶意活动。
% N* n. Z" D4 X4 Q0 {3 d1 W, Y
* ~/ \: g- O9 t" A$ B4 y
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)$ z+ `% w8 m" E+ M
检测通过Web应用程序上传的恶意文件。 e& d- w! A3 ^
& _; d4 R$ Q# W6 Y2 x" _$ G- }- L) B% U
Tracking Sensitive Data(跟踪敏感数据)' s B: k! d6 d/ I6 \* ^+ U0 L
信用卡通道的使用,并阻止泄漏。8 Y" P" o Q8 o# ~0 l/ A" o! C
4 H7 h. h* u5 Q5 t- j- H
. v& A) _1 r& `2 K
Trojan Protection(木马防护)
' E4 t! y" X/ R检测访问木马。. W$ F0 U( F* [" e0 o
, }. x1 {, i n8 o3 _
4 u$ K6 m& O( SIdentification of Application Defects(应用程序缺陷的鉴定)3 j9 F- F; P& l k4 Y' p
检测应用程序的错误配置警报。2 Y8 J8 }9 C2 U. E- d
& T! [1 v7 U+ \
& u! x" O4 W" g L+ n% |1 n6 J" ?+ A8 UError Detection and Hiding(错误检测和隐藏), i+ w! E/ u# Z* t2 j
检测伪装服务器发送错误消息。2 n9 C* s+ j+ m9 N7 w2 `
; @- h; C4 X$ r2 e" }( [ X: I- t
J* c t) |6 E6 g安装ModSecurity8 V0 O/ _: z5 r# H {8 W
软件基础环境准备
2 q2 ?8 J3 A7 g1 v5 f/ o下载对应软件包) O0 o3 {0 a* N! f2 ~ }7 a* f
$ cd /root; v, Z$ s$ ]( y A+ }6 C
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'" j4 T( C5 v' S& P, l, E$ z8 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
' n" I2 d0 N6 O+ ?4 |安装Nginx和ModSecurity依赖包
# S, s" g5 f" @7 XCentos/RHEL* ], ?& Y. C" `
3 K3 h- t9 Z5 ]
4 `: a! _+ a5 \9 n. a& ^) {$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
& | x' J& g w EUbuntu/Debian. {, K6 f! t9 k/ d8 I( |& A( }, N) p
1 P. \4 I. j% y# G0 l
" m# W8 l; I5 A# C/ g+ G" f W$ 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) I8 u9 j+ v/ K+ S! v% J, a
编译安装ModSecurity5 Z0 O( J1 q$ `/ R" M
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
1 F- D3 a) B- z8 f# R4 V5 }
# u3 ~1 f! V2 G& u% O6 _
) N1 H5 M. o0 P+ L5 J5 I方法一:编译为Nginx静态模块' U+ B4 l$ U m: S1 H) K! {
/ h% _8 Z$ W" S8 F' z% R1 G d0 n8 q' k2 @
编译为独立模块(modsecurity-2.9.1)
% p+ j. D2 `# I6 m$ tar xzvf modsecurity-2.9.1.tar.gz0 n( @/ C0 F! s" g7 j9 r6 y
$ cd modsecurity-2.9.1/
9 F3 V! }2 @4 ^& F$ u- M! X$ ./autogen.sh6 v1 [0 g. `9 Y3 i8 q
$ ./configure --enable-standalone-module --disable-mlogc
# G R/ m7 y* P2 Y+ L" D0 [" ^1 c$ make
- ?6 d- w4 L9 L编译安装Nginx并添加ModSecurity模块5 A5 _7 c# f* Z: Q0 a: g4 N
$ tar xzvf nginx-1.9.2.tar.gz% ~7 P/ t7 f: y# ~. L1 |0 z- l
$ cd nginx-1.9.2
; R1 w. I }% s5 `6 E8 \) c$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/" ?5 F* z2 r9 A& X! G# ?, t
$ make && make install4 K- Y/ m2 V' j% C
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
, B% n# U. i8 u. p
) W W: C0 ?3 E2 f! w
5 }& D. b# {. P+ |% J& v编译LibModSecurity(modsecurity-3.0)
7 Y k! [, v5 J; S) T7 d. f$ cd /root8 `' T. h; O# S+ G% V; e
$ git clone https://github.com/SpiderLabs/ModSecurity
4 ?( |; f$ G F: l$ z$ cd ModSecurity7 [. N- z1 X0 l- K3 d
$ git checkout -b v3/master origin/v3/master0 }7 a G+ Z1 w* ^, E
$ sh build.sh! H @( h* w% u( V2 `7 ]6 ]
$ git submodule init
9 {/ S! }, @( h+ k1 w& n9 E+ l$ git submodule update/ t8 E' c* k8 z$ [) C
$ ./configure
2 C# ]% W9 r/ u) w$ make. o4 ]+ ]7 z) C7 g& w* z( t
$ make install/ A( J* v% H9 G7 m, A' a3 {- H
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
( f3 _: M2 o0 j) [/ H2 W& @' `
2 L6 h. P4 b6 L% j& W
9 w0 i7 v0 L7 w7 z( ^% e8 K$ ls /usr/local/modsecurity/lib. X/ |& H3 a; k/ X2 h
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
4 C; X6 T) u% I6 W( x编译安装Nginx并添加ModSecurity-Nginx Connector模块1 m5 S9 l# J" r/ C: J
使用ModSecurity-Nginx模块来连接LibModSecurity
. L- ^9 p9 G7 Y) T2 W+ ]- I: T$ k* V" P# r O) G
: N5 t$ m* W9 D$ cd /root+ p+ z1 }3 G$ R+ e, h
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
$ }3 ?: h3 d: w* O$ tar xzvf nginx-1.9.2.tar.gz
7 P' a4 I) h! O9 J$ cd nginx-1.9.27 u2 Z \1 c9 k4 a _$ S5 w$ h$ b
$ ./configure --add-module=/root/modsecurity-nginx, t0 S9 l. K! T7 y
$ make8 k# E( F! \9 u! G% L5 h
$ make && make install( x5 x- G) x9 J5 }) j( `
添加OWASP规则
0 W( {/ x3 w6 c- ]ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。* U6 Y( u% z8 O6 V
$ H2 I$ e& H. T2 f6 f
4 h+ N4 G7 _ t6 ~5 F$ b5 G, B下载OWASP规则并生成配置文件0 S) t a7 |$ o) z( |( o/ t
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git+ B2 p% B, P& y! s* T. e. t$ h
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/9 [$ }7 F1 {0 w5 N; O
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs5 J# u @# p/ Z( a2 U5 Q8 b8 F
$ cp crs-setup.conf.example crs-setup.conf
3 v6 ^5 D) T x H+ ?8 \3 X. B8 R) a配置OWASP规则- l: A) j; }8 i
编辑crs-setup.conf文件
- o5 q) X& Q( A" |% n% b* H; L
7 z' S5 e& X4 [; |' \/ z0 w; k1 n W8 ]3 \/ L7 t0 F5 A
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
5 f+ j5 M: Z: f6 C$ q. E7 Q$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf0 K3 k5 T3 w8 K2 e
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
2 e! A4 i1 U8 F. C# p$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf# |: ~0 ~: p* `! @. J2 ?
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
; X# e' k/ Y: T4 s% x2 E9 I8 {. U U _. H8 E4 Y. X
( L5 K. q; f/ D5 l6 q! `% L
启用ModSecurity模块和CRS规则
9 b9 K( M1 z6 H复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。+ B: m. e; B" y! t$ t* f0 ~
/ P3 J: X% V; S
: m2 G% `; {% k4 k& cmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
1 `9 U3 Q. j% X. A4 c4 z" {- Q/ e# H$ F8 @9 { r5 m _0 ^( Y
g$ G. s9 R' k4 S$ cd /root/modsecurity-2.9.1/, ]4 ~ T+ k& Q
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf ' U. P# Q' S5 Q; g
$ cp unicode.mapping /usr/local/nginx/conf/
6 i2 D( N( o7 R/ {将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
0 F5 w, {! j/ a& V" u; e' F( b$ l4 U" }% ? ~7 ^
* \1 ^$ m* x9 N. @( h% [
$ vim /usr/local/nginx/conf/modsecurity.conf. E6 K+ o* D- ?6 o- D+ ^2 z% U6 G
SecRuleEngine On* [- Y X; \. Q {4 @- ^
ModSecurity中几个常用配置说明:
4 x- D+ F3 I: Y$ t
" S+ g1 z% C+ ~, m! ]. t. L1 f: H' `2 \2 ]3 c- H# h9 B- {0 \
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
3 i$ m5 ^* A/ Q) h/ s
9 A- l& ^$ h2 y+ w- g+ E! F6 R. J$ S" m
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
) j( u; k+ V: Q* |( a1 i( b7 W5 |& ?7 T
9 s @3 V( f5 V$ T0 a) v4 `4 M2 e8 s* B
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
3 r7 i2 c5 \: X8 M( d) {8 |
( j; b2 ^) ^5 E
4 ^: d6 ]; t ?) m4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。 P0 n5 D* h. S' S, Q9 ]
1 k; o/ \7 a2 o3 h: x% I2 t7 v( I/ l9 @: j9 |
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
8 w. s% M; Q% v# y5 Y2 o: y5 v1 n+ Z! B
; B; e. }' u" h3 F) r# G3.x版本CRS
2 r9 R) v5 H# Z' Q1 ?$ cd /usr/local/nginx/conf/owasp-modsecurity-crs7 ~- E* ~7 \- h& s, y- ]$ B
# 生成例外排除请求的配置文件
- |$ b$ N6 w0 k) r! @' p$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf4 x9 c% r# M# E4 l
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf5 Y3 Z' h5 h' r6 [2 C6 {
$ cp rules/*.data /usr/local/nginx/conf
" g$ l; ]3 y9 [, F. ]为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
) u {1 U7 e( q8 [- V* O4 ?) }
, \, y9 E8 ^/ `1 N& u
" c7 n$ W4 o, h, F$ vim /usr/local/nginx/conf/modsec_includes.conf% V) n3 q1 E" B. L- [
7 L8 C% o& G& F k7 p2 y* a[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 / [( ^, {, y T) V- e @
, v0 Z" K) F( a& G& A9 T) f
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
) {& z! T8 m5 Y. V; Q9 F' | }3 V" Q: Z: Z e7 N
" b8 W- }- r. K* k: V1 \7 J配置Nginx支持Modsecurity9 j: {& k5 @2 x9 t4 c- K' A9 }
启用Modsecurity
; o$ t- o _/ d' ?2 t使用静态模块加载的配置方法
* m; I2 q. y# t% ?在需要启用Modsecurity的主机的location下面加入下面两行即可:" B8 j: D' M6 N& s
n- z K) F5 `0 Y
! T$ J( a0 p, {. T+ }/ EModSecurityEnabled on;
1 b0 W; {8 D7 x6 b* `$ yModSecurityConfig modsec_includes.conf;" f, Z$ Z: v$ t6 l+ d/ E
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。& v4 W: M/ X9 M$ R" O1 P
# a1 Z& h# P3 x- @# i* W. ]9 A% b/ @, `! n1 n7 m
$ vim /usr/local/nginx/conf/nginx.conf( T2 C) v; a4 }! B7 N& r C: U2 L
# g2 c% {9 d @8 A c3 }
' E* \; a+ G9 @. k! U) V
server { o! y. J) m$ H3 ^* ?
listen 80;
" a7 Q$ W! G: H6 c, z# L! ^. X0 l server_name example.com;3 |( T# U) M0 P8 R0 N
% C4 G- j! z7 r
5 u& m4 U% ]) O* k7 y
location / {
P3 g y3 c! b ModSecurityEnabled on;
& r, o9 _) |9 J ModSecurityConfig modsec_includes.conf;1 [4 O: `$ g @" A9 H9 F( B% A
root html;
7 \& }" ~' D! F& B8 d6 t index index.html index.htm;6 ]7 s" X, Q; y S2 D( r
}
# n/ Y$ ]3 H8 c$ E' v: ?7 Y}
; n2 N% G& ^, b使用动态模块加载的配置方法
0 }+ S# q9 u" Y7 i! r0 H在需要启用Modsecurity的主机的location下面加入下面两行即可:, q" U- B5 Z. L/ R
4 T* q; \6 B' t: ]% t: `1 R- K6 L2 s2 r
modsecurity on;3 _ y5 U# s# e6 r* g
modsecurity_rules_file modsec_includes.conf;
: }) h D0 I1 M, j' E3 X1 D8 `修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。/ O3 g" @7 l3 G! l0 x& W, O; j
5 m: G" l% T3 Y4 P2 Y4 S, q% g6 s% M8 W
$ vim /usr/local/nginx/conf/nginx.conf
" R- w. X/ M! d* N& L0 ^
, ]! Q1 ?3 J( |
4 k! ?) Y3 y% Eserver {
; G Q! K, v0 b: _) N, h listen 80;$ A* M8 M" l! U" u9 m" ?" z+ Z N
server_name localhost mike.hi-linux.com;. R2 h3 p( x2 V- B
access_log /var/log/nginx/yourdomain.log;
" o; X' V ?0 s0 _: F1 \0 g' B9 F( a2 o8 o
( o6 `8 T( e% N3 I5 W
location / {
* E8 H9 f& ^; P" k( V) e
! l" v$ \7 T2 _! V& ^7 i: r/ U/ h
' }1 G1 ]# z3 l8 F modsecurity on;
1 r" c- V! c, l ~5 g modsecurity_rules_file modsec_includes.conf;
: f% [& T( b# h! P% m root html;
0 J$ C* M1 a( }6 x1 ^ index index.html index.htm;+ @8 W8 N4 ]/ O& u4 {( |1 e( U1 E
}, s" k! D" g& k
}
" [8 m" A' g& x0 s' v4 m+ _( ~验证Nginx配置文件
- o+ `1 G- ^. g4 z3 M1 c$ /usr/local/nginx/sbin/nginx -t' A% U6 H* h4 z4 _3 d( h. c; z
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok1 D% D: D! `9 ^6 h- g3 ~0 B: n
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful* W( h* u9 V5 {2 L: f5 t9 |
启动Nginx
/ e. `& j* W" R$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
2 c h" n/ a" C" r$ m4 v; E
6 q8 P$ |3 I7 T7 f1 [( v测试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.logModsecurity主要是规则验证(验证已知漏洞),Nginx下还有另一个功能强大的WAF模块Naxsi。Naxsi最大特点是可以设置学习模式,抓取您的网站产生必要的白名单,以避免误报!Naxsi不依赖于预先定义的签名,Naxsi能够战胜更多复杂/未知/混淆的攻击模式。
# A! q6 d0 J3 W1 l0 u; a r9 c/ d |