ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
% `! O; \% r/ Z( N. x9 I
5 N+ T. y$ ]9 o* u* f& z P) v$ T; K# M7 K# p# x6 H0 Y5 E7 |
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。/ w% |' K' F# b
* I0 P' x# C( I
; I/ r6 B1 {" i+ s+ s5 E g什么是ModSecurity- s* u0 s( a& @# B
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
" }" x" z1 ?2 M: t
& f6 a( N* ~/ c9 j* S4 f
8 U Z- q8 B3 qModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。3 X# v* z+ U4 |5 ^5 i+ F
/ Z j- K6 m i0 d; j, D; r2 d
- l8 E5 J+ }# i9 xModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
/ I' o! G8 n5 [+ D u1 C2 D; K& z. P6 j, L
: n& r7 p* l" h- K7 O/ {
官网: https://www.modsecurity.org/
$ O, X6 P! _! Y& [) S# B/ @( S& H' l! |
: E0 W5 h/ [) t* [% f) Y! k# c什么是OWASP CRS
% B: s8 X' d) _9 {* O. iOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。& v9 p8 m. F3 S5 z) Z9 C
- E1 H& ^. E3 i1 W; T. N$ e
7 ~; X# B; y5 h$ I5 g" `' z0 F3 [ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
* d; y r L3 J/ q7 G$ k+ k6 _$ e5 K6 `8 d
+ |, e n4 j$ ^ H
HTTP Protection(HTTP防御)
- U$ a/ R- M9 T1 O+ \7 EHTTP协议和本地定义使用的detectsviolations策略。
# [) K+ _5 }4 E$ Q( t/ b$ e; o4 q
# i' W8 t" g% V% i+ }( h
3 P9 b: Q+ i( n1 b. S9 ?1 a1 c7 ?# RReal-time Blacklist Lookups(实时黑名单查询)
b: e7 I8 p/ W/ f1 o" E# L利用第三方IP名单。6 S5 f9 K$ A5 I. u7 y5 V
3 x4 I0 R4 _0 z/ F& G) m. S( `
' H% c0 j$ m# P
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
4 y) E( _, ^+ a# u( o7 @2 R2 u防御HTTP的洪水攻击和HTTP Dos攻击。7 k# l2 V$ Z. o
; `- u" \9 { o" m( D) [+ ]# }: c2 h! x
Common Web Attacks Protection(常见的Web攻击防护)
+ R0 [8 ^& w0 l% p6 \检测常见的Web应用程序的安全攻击。) `1 d; F. n0 N- G8 X
2 w( Y' |* j0 J7 B s
9 d- r7 F L9 n- G( iAutomation Detection(自动化检测)0 N1 X2 ?2 b, ^+ K1 C5 p
检测机器人,爬虫,扫描仪和其他表面恶意活动。
3 |7 \3 _5 ^$ b! h4 j% k: D# i- x/ q- I& `
% M& y' D( l8 v) U% H1 g7 p
Integration with AV Scanning for File Uploads(文件上传防病毒扫描). I! K2 i! J- W# [* \
检测通过Web应用程序上传的恶意文件。8 ^3 E4 T. x X' Z
1 U9 k6 z0 ~5 p7 P/ c: [
- P' @ Y3 F# ~1 hTracking Sensitive Data(跟踪敏感数据); I" W/ i9 ^! ~0 t9 G! ~$ f
信用卡通道的使用,并阻止泄漏。9 q% r( J8 G- m2 l6 O
- U$ \0 l, ^* P ]" u+ a) ] T& n! u: E6 w. F+ e
Trojan Protection(木马防护)
0 R; w% G9 x# Q9 ?检测访问木马。" w' ?4 ^9 W3 p3 E, J* n: e* g
3 H# n2 M0 c3 {7 X5 N4 L8 l
, n# h7 `6 R& {7 G+ v! CIdentification of Application Defects(应用程序缺陷的鉴定)
# Q8 {2 ^- U5 n! X# a' Z4 ~1 X检测应用程序的错误配置警报。/ _! p3 u! ~2 y, t
' p* r( F$ x( [. [0 b! ]1 I
& y/ ?9 V0 J. T+ YError Detection and Hiding(错误检测和隐藏)
, y7 I# M* Y" q0 i y3 _6 M+ F检测伪装服务器发送错误消息。$ i( t( _% m6 m. q# h |5 d
/ @$ ^' X+ }3 ]' A- G
* Z2 l2 x' Y/ F3 G$ T
安装ModSecurity% B$ A* ]0 q! ^8 z* f, J# u+ V% y* p
软件基础环境准备
8 ^- x' Q- S. O! v9 p下载对应软件包: v, c4 I" a- _, g* D
$ cd /root$ ^/ O" `( a5 m3 T, L" E& |5 S
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
% e6 R8 u: @) `4 k) Z$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz2 X* T3 k! q9 A* O
安装Nginx和ModSecurity依赖包
$ q/ I4 @6 K/ l. ECentos/RHEL
& J: k" z+ b5 ]' S' F. e: X+ m( J% g3 |
- f3 y: P+ W2 H4 D
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
2 P/ s) z6 C) h- hUbuntu/Debian& Y% I' t" k3 {, K8 I" G% Z
, \4 G& g% m( y
9 \, {8 b* Y, M2 t$ 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
: X/ p# x. m5 W& W" m8 R2 Y编译安装ModSecurity
8 M# V1 e: ]( ?, D. w& J% m8 VNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
/ W2 B1 s( \( e2 ?6 B! e6 o7 D1 M7 O$ A G" R
# t5 M B% m' v' i- W `方法一:编译为Nginx静态模块& J7 M0 F* p+ _; c
" ?; s Q' ~! y4 }! q7 Z
% N) f7 t1 }! t* k+ K8 w9 C编译为独立模块(modsecurity-2.9.1)
6 _4 g' f- ~! L2 c/ f0 B6 W$ tar xzvf modsecurity-2.9.1.tar.gz
: i) t/ \% j1 c9 E0 ~$ cd modsecurity-2.9.1/
( Q! u- h- q- J9 ~0 B' U% c$ ./autogen.sh9 Y& m+ f6 L8 g) M
$ ./configure --enable-standalone-module --disable-mlogc5 [# V. n3 k0 |
$ make
7 _6 P/ I) p" a0 [编译安装Nginx并添加ModSecurity模块% u H& s; K5 ?# ?: [
$ tar xzvf nginx-1.9.2.tar.gz4 D2 C A; J# B$ X
$ cd nginx-1.9.2& a9 k6 s k- n/ X! Y' [
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/0 }- D! u. q' K( p, F; U4 O( o6 v
$ make && make install
* c6 r5 I) ^$ ^# H- z% [3 X方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
( Q4 u/ R7 ~& V; M8 `- e
! M5 `8 d0 l# H7 @) R; _9 m% H# p# Y% f5 o
编译LibModSecurity(modsecurity-3.0)5 K/ u- A. s" D3 u" Y
$ cd /root3 o$ V" s9 x+ u
$ git clone https://github.com/SpiderLabs/ModSecurity
2 }" _3 P0 M7 G# p. k4 v$ cd ModSecurity
, v1 q- l! K/ |$ git checkout -b v3/master origin/v3/master5 \+ Q) J6 U# v7 n$ i& T
$ sh build.sh
! _, S0 ]3 C! w' s @$ git submodule init1 @4 g% s( ~, u5 l$ D; N
$ git submodule update
4 E( L6 x! I8 Y+ e% M/ o$ ./configure
" w1 u- E2 w5 Z' Z1 L) @8 h+ ^- V$ make
[. E6 H! \2 z! B$ make install
; @' h @" p. _7 P# t2 ]) b3 ]LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
4 N5 c6 H v3 ~# n0 G/ Q3 x6 P* H( U: b9 K8 Z5 P" o$ ]1 Q
) k U* T9 C' C# K2 x+ v
$ ls /usr/local/modsecurity/lib9 @- W8 [% Y) Z! m: a4 V
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
: Q8 x) }9 h& b+ K. N编译安装Nginx并添加ModSecurity-Nginx Connector模块* Z. F7 x+ c8 L" n$ f
使用ModSecurity-Nginx模块来连接LibModSecurity u8 P- i4 @5 ]$ P; v% Y
5 a- W1 C+ D9 ^% \& F) N4 T
% @3 ?. _8 T2 Z. Q1 a
$ cd /root7 b& U1 o: O9 x
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx# I/ p" a. k1 f; s/ V8 v- ?$ f
$ tar xzvf nginx-1.9.2.tar.gz9 E: r; v+ S& \2 ^! {/ |
$ cd nginx-1.9.2. D% j! _$ ? W- |/ K
$ ./configure --add-module=/root/modsecurity-nginx
O4 T6 J* w$ ?3 D$ make0 r& E3 D" [- ]9 n8 j1 \
$ make && make install
! U; A$ l+ `: c/ [+ V/ U' Y添加OWASP规则( k# r) s; j' m! h3 q9 M
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
. c# R& @7 p/ A$ p' H! r4 ^, @' J5 V) M7 [3 r+ q+ M+ q
7 x# j; w/ G. s0 B下载OWASP规则并生成配置文件& B- j9 m7 Y/ a- B( B s
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
* G. R9 E* N0 J0 O3 S$ f; z' f$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/! D1 I8 N3 z0 s2 U p9 Z
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
( s2 c# l0 q8 n$ cp crs-setup.conf.example crs-setup.conf
( k/ e/ [. }* D K* G" b8 c. ?, r, o配置OWASP规则
% t+ F4 Y, N8 p4 ~) {4 F编辑crs-setup.conf文件9 d$ N" }5 G/ m8 @4 p
: x( q* `: x9 h0 a' d3 U
4 q6 @- ]9 @2 o6 n) F4 ~$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf& e0 U% i3 N- r S0 ~ _' u1 X# }
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
; \7 x( h" g0 P; S. ]/ t$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf) I- u, N) [; \% R
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
( Q2 u5 q# j/ t [5 Z/ c4 d9 V默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
7 m9 ^7 F: P: S7 N& C' p& ?' T5 u7 P/ D
0 C: T! H, Q! c9 C8 Y启用ModSecurity模块和CRS规则
5 P) X( i- y7 O复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。! d8 t/ p N0 j. C9 r7 n/ N$ o
2 M) H, t% X# X& w8 a
P+ h3 e: {! h. wmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。7 b* E$ _( u7 u) ^
0 l: T' [* V8 g: F/ L* c
; b0 Y0 x' ~, b$ cd /root/modsecurity-2.9.1/: k* j% {% S/ m; C% ~. M
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
3 K/ k# G2 T1 d. ^8 A$ cp unicode.mapping /usr/local/nginx/conf/
8 B# ^% {5 ~* }; U0 B" t将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
5 z, R. Q4 p4 Z
2 j# y( S7 P7 n0 ~5 h) A" u- A, G+ o# Y! o. K/ `) Y
$ vim /usr/local/nginx/conf/modsecurity.conf# N: @: c6 S8 B8 X$ x/ z, \
SecRuleEngine On
+ o7 {* q( }# t8 n( y( J. e8 hModSecurity中几个常用配置说明:& n: E1 I6 [* n& I& ~1 F. L
9 V) m' ~& p6 _
- G/ d9 r* w) f c1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
! h/ \- }& a" H0 j' T" m3 T; R# ~" }( B% Q# b5 M# N7 B }
9 P8 T* ~3 Z0 a% ?
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。+ R3 \# \/ f/ h( f7 k& |" v: D5 r
4 s1 h& y6 X# H7 R4 z
) A+ v# A6 Z0 C, W$ T) Z* y0 t, W
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。3 k+ |1 k% l( [* _$ R% [
( g% U- G, i1 s. t( S4 `3 ?, z) i/ J
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
+ E3 u7 _) a7 p# _$ x1 l4 C% x5 Q" j) G: B; S5 u' |: c
* g, W2 Q* u! N0 ]+ T& f在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
7 G) V8 ^+ {1 P$ U( A/ `; s1 n+ D; b# y# l- J
8 a% q- a) G1 O- U$ }: G4 A3.x版本CRS% S% F1 |" h% f" ~0 t
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
& t2 h, \7 r+ }8 n" ?: F# 生成例外排除请求的配置文件
' {- j% X+ t0 Y* b5 q b6 @$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf" Z# o8 d* l8 T% p4 U9 `6 D
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf2 g! O6 M1 C9 P2 y- ~/ D% C1 h. V
$ cp rules/*.data /usr/local/nginx/conf
3 l- D7 ^5 U7 ^为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。 g: n7 n {2 ~4 v
3 g) s; o: q; x( s
% R+ C% Z, X+ C8 y$ vim /usr/local/nginx/conf/modsec_includes.conf2 q( R U6 [; J9 V; j7 P4 R; `
1 H! G% E; ]/ k$ \1 c8 }& N6 B[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
8 x" c& K, U' ~1 e
" |/ [0 E7 E0 |, Y5 j. g注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
- |9 S& v2 H' g# C& o* x8 d% S$ `- w0 ?
7 ?2 T* i3 y3 v- Y
配置Nginx支持Modsecurity
" Z7 ^0 q" F: e/ x* a1 H c启用Modsecurity) e* U; M; K1 y# d6 i
使用静态模块加载的配置方法
- {6 P: V8 n3 C0 v" T在需要启用Modsecurity的主机的location下面加入下面两行即可:% ?8 `$ r9 V" y7 I6 j
7 ^! `% h! m# ]$ J1 B) t
0 u9 ^& w W! Z8 x9 JModSecurityEnabled on;( N4 f7 a2 x7 }% Y+ n% A
ModSecurityConfig modsec_includes.conf;
! [0 T0 t& b' r, }0 d修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。; s1 j! S4 f- l8 \0 C; `& w x
+ ]! j* I1 M2 d6 ^) m
: v e3 ~5 _. H' i* Z
$ vim /usr/local/nginx/conf/nginx.conf9 H+ w6 U: W; E% w5 l/ i0 a
4 O9 H4 N" |) F
/ Y: ? ]7 B5 x* [9 Eserver {$ Q7 A6 @6 x, Q. X0 a9 ^
listen 80;" {& A3 a1 G; r" F
server_name example.com;
# r0 N) V, A- ~' |' Z
2 @4 B) U3 I% G. {- v9 L Y0 N. y- I# _6 V+ x8 u. T
location / {
1 x' }4 X W2 s6 `+ V ModSecurityEnabled on;! A* m. \, C. p( E. f( l
ModSecurityConfig modsec_includes.conf;8 X3 x9 O: Y, r. ^
root html;
2 Q8 D( b; p, s$ w+ h/ T5 T index index.html index.htm;4 m" Y" l( x; Y
}* b" r( o6 K" B
}
2 `$ k& u& @ h, w使用动态模块加载的配置方法
) w% M* g5 @1 f4 s8 \在需要启用Modsecurity的主机的location下面加入下面两行即可:
% g. h3 J6 {6 g7 v1 F7 N" F
+ Q+ w/ k! b& E0 q* e- I7 |# ^0 `( ?4 p* H
modsecurity on;4 x, Z5 u$ ~, d! n# C+ p, x
modsecurity_rules_file modsec_includes.conf;% s8 V i3 ^/ I
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。6 [3 s W% }% a! C+ ?
% b6 s: g1 ? H4 f
4 q5 J4 j: T( f ^; Y/ y& ]( c8 _- S
$ vim /usr/local/nginx/conf/nginx.conf7 G8 X! Q# N$ X2 a! K( P+ ^* ?
2 A4 I1 L! {! p6 f
! ?5 h' |+ i$ S7 w
server { y7 h. G# K7 _4 [7 @
listen 80;
% e( @2 G! Y# ^% n9 r' m+ y server_name localhost mike.hi-linux.com;
" k, k9 ]7 t# h7 [# T5 E! ^! S& T access_log /var/log/nginx/yourdomain.log;$ e2 {' l7 S5 ?( s& h
. q# O7 {# \ H. s T
( s G0 l. D5 G1 M location / {
: Y6 P+ u) y( f, P6 _$ S( o& m2 \- ^! }
2 g8 u1 H2 Y8 l ]) |3 [* x/ m
modsecurity on;; h; N( P- f# x! M
modsecurity_rules_file modsec_includes.conf;
6 Z; Z/ t' ]/ L% e root html;6 \2 T. A3 g4 m/ l% D$ p8 b
index index.html index.htm;$ H9 Z4 W% O; {( I! g1 ~
}
9 ^* e# H% J' I( [8 m# U* w' A}
j: V/ ~2 D: r j& v. A验证Nginx配置文件* |* ~$ P2 l+ q& u/ s$ J* `- `
$ /usr/local/nginx/sbin/nginx -t
% ^: G( q$ w0 L2 A9 tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
/ e# r1 r, |7 ~# h, i3 p/ [nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
. A5 e. V$ q% D0 W* E' x- e" ~启动Nginx
. D1 G! a* e s$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf+ k* L7 a; N0 p3 j9 j1 _6 E
" y# A4 G) l0 U; n% x% t7 A# r测试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能够战胜更多复杂/未知/混淆的攻击模式。 * ~/ E9 O; r/ [- b! u
|