ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
- K5 U( \2 I/ b1 b/ r) A
1 o3 u* x" ?: ?# t$ \, V; l( x2 h3 I4 s# ]: b
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。, k+ l3 |$ @8 f# u0 s: T: ]
6 V% |7 k9 L+ U" X+ v6 D8 r
9 x6 a+ a$ s" G! w什么是ModSecurity, }/ q# b% ?0 A! {
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。8 n0 h6 G+ P/ p; u2 D1 \3 N
0 n, w( @4 f2 w1 @" A& Y$ P6 B. x
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。" G) N; `2 b! D* Z+ J
, |3 R6 L2 P7 ?
- N9 R2 ^2 M1 x; j% @4 Y/ E7 {ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
1 P F& l1 Z8 B B) j' O# I, w; m' [
) h& P' U: Y1 t4 R" `$ k! E2 c9 a. J3 e* f+ x: v! x/ G% j1 |* N
官网: https://www.modsecurity.org/, [5 y1 f( H1 s& n; e
; D4 K, ]# l9 \9 J* f
; o# F2 I$ b0 C" ?/ Y什么是OWASP CRS
2 x+ ~7 I: W# J: T5 O0 eOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。 O! l, z7 t& V# K7 k% |' L& g
( r5 I: n% u5 b% b3 b. v
& y9 a6 r( v v; x: z) ~
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
3 g. q8 v- M7 G) c% [
$ F" L( |$ m$ ~2 F1 Q9 p0 c) J) S" u* ]$ D8 e5 ?8 O
HTTP Protection(HTTP防御)
8 s7 p" S1 K1 s4 }/ v' D. v- nHTTP协议和本地定义使用的detectsviolations策略。
- R; o( U* h# f' T0 I
4 D1 d) U9 s0 }6 X" p% x x2 F s$ a9 @. Z) X
Real-time Blacklist Lookups(实时黑名单查询). ^/ p4 i8 c3 q6 M0 c
利用第三方IP名单。- f7 s9 o% m. U4 X+ j" o$ {& G% ^& [
& Z( o$ l$ S& j5 M4 Z
5 [7 H% w6 q" m2 i& KHTTP Denial of Service Protections(HTTP的拒绝服务保护)) A$ B& r1 ^) B5 p, v9 W
防御HTTP的洪水攻击和HTTP Dos攻击。# o6 Q; x+ i0 M
1 I# r2 H$ O) p9 h3 D9 D! `
* c4 |. e4 c) `4 h" ~0 A6 [: {Common Web Attacks Protection(常见的Web攻击防护)8 V- |- |: N: s5 O
检测常见的Web应用程序的安全攻击。
, o [9 Q, c. W! e
) g: R) A4 K3 X7 S% K5 x5 u* i) @8 y3 O8 I
Automation Detection(自动化检测)) w) k2 j2 K! [; Q' T: l
检测机器人,爬虫,扫描仪和其他表面恶意活动。9 U, X! z1 M- |2 {
z7 x1 v8 V$ e: O% r9 n0 T
3 f, u9 }3 T$ M# V" F4 b, VIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)0 x' U9 R" Q* L5 ~
检测通过Web应用程序上传的恶意文件。" _8 D/ f5 ]) _) h, r8 i7 \* Q
. k! p6 Q( C% v, a1 w
) k" G* D% x/ E' E% T; s
Tracking Sensitive Data(跟踪敏感数据)
( A9 u7 X/ ?" e& ]! m, [信用卡通道的使用,并阻止泄漏。
; P0 x- C! a% p0 d5 i1 S) _5 P/ }& j* z* t9 \! O5 v, B5 ~5 G
1 D* T$ }3 a3 l$ _; ^5 ?1 PTrojan Protection(木马防护)" a6 j5 ]; W6 g0 Q( M2 n% X: `
检测访问木马。! t& Y3 ]" C) [) }! {
% m4 @! q5 w" x0 \" L' i
: E$ @7 k& ~( \/ O) x2 F3 cIdentification of Application Defects(应用程序缺陷的鉴定)
2 h* j4 I, q, W" e! f! Q# Q; ?检测应用程序的错误配置警报。3 f- g. J0 p1 V5 k
1 D7 l. N/ f/ U4 G4 z
. h$ F) t: ]( e; PError Detection and Hiding(错误检测和隐藏)
+ i# J2 y4 I; x$ e4 X3 `检测伪装服务器发送错误消息。# @) l& v9 |& |3 ?: f6 k1 v
+ p' Q {" r- S2 _7 P5 f# J% u
! K. |+ }* m3 f$ R# x- j
安装ModSecurity
& j& v6 n" l1 s* }; V! o/ x软件基础环境准备
% ?7 F h9 Z! k* P% ]下载对应软件包
2 o, l/ b4 q1 n, d. l$ cd /root: s0 Z' V0 n+ s$ }. `7 E. I
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
$ S0 d5 A& A0 I, R3 e$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz5 |' h4 f# a! |( \1 b8 Q
安装Nginx和ModSecurity依赖包' F6 j1 R8 N% `8 K7 ?6 j% T2 Z
Centos/RHEL
) j0 J# e5 q# q5 y- z3 }) F( S$ v# P# b* \ I
6 [- ?/ A& E) B9 ?; B. G! }2 Y5 _. g$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
) t7 M, ?3 |8 F# D" |Ubuntu/Debian0 M' _7 r- {/ n1 G2 [( t# n
2 A( y: i7 i" I3 C
% D0 \3 r; i2 d+ J. d5 k# G1 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
( T! W4 x& q# A编译安装ModSecurity
* K1 ~& W4 R3 F. \8 pNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。; h9 f& U- N) g5 c
; G+ h# a2 M" P4 |
& c& y- z* f2 |& a. L方法一:编译为Nginx静态模块! v4 A5 j+ y3 N7 l9 _$ |' E. e
5 k) ^; _, H& V% O. c; M: @- ~
$ J9 m& c- {0 u! m编译为独立模块(modsecurity-2.9.1)/ l, T" C8 i5 J) A9 \6 A0 F! [
$ tar xzvf modsecurity-2.9.1.tar.gz. X, r; Y+ i) j ^1 m
$ cd modsecurity-2.9.1/ C* b+ I! G+ P; G7 A; o: j
$ ./autogen.sh
6 H" E5 D) v8 e8 C$ ./configure --enable-standalone-module --disable-mlogc% u+ X6 Q6 J1 K0 ?4 t+ d
$ make
: p* p; Q" ?- [. i编译安装Nginx并添加ModSecurity模块4 M, u. n3 [7 O7 y$ Q
$ tar xzvf nginx-1.9.2.tar.gz
# l: T. t# y$ H0 X/ j& e. s4 ?- ^$ cd nginx-1.9.2
6 k$ s, Z6 @, _: ~$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
: V* u8 i1 a3 r& x9 d$ make && make install
6 e4 `/ b! Y& V6 x+ u- @$ U方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
& `# T) ^( |& a* _- j7 h5 m. n4 ^ g- d2 w2 V1 r7 d* O6 S2 u
& N% y- c8 O9 W8 I编译LibModSecurity(modsecurity-3.0)( ?2 o: j" J3 u# h$ Q
$ cd /root* L5 g: i$ o- S9 }
$ git clone https://github.com/SpiderLabs/ModSecurity
- M2 Q1 ~ C7 q% V; z/ f2 [, U6 U$ cd ModSecurity" Z+ h @7 F4 l( q
$ git checkout -b v3/master origin/v3/master$ a1 P4 u8 ^: a# S, d
$ sh build.sh
( y5 _3 |. z, Z2 m$ git submodule init
2 Q I: V" m9 b$ git submodule update& N1 {% A' R. B9 Q6 y8 F6 h$ [
$ ./configure
3 e8 u6 U5 n, I- j" M$ make' }& h7 f% o. q5 |. [3 Q: p4 b5 B
$ make install
) e/ O! m% [3 p1 y$ q. ^LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。; g5 k, z* F5 K6 ?, E% i/ I9 @
5 w" A3 V# A* S! A& d
9 S/ X9 A& o# K: f3 n$ ls /usr/local/modsecurity/lib
( _2 ?4 d1 P9 ?; z9 Rlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
7 N* t# w1 Z3 K7 @( o4 Z7 g编译安装Nginx并添加ModSecurity-Nginx Connector模块* B4 U5 K5 _, E+ O$ n' V
使用ModSecurity-Nginx模块来连接LibModSecurity
& o- s' u/ {2 G* f
: g, \9 R8 T5 c! O) n( c' j! n+ E
8 E. F" M6 d' b! x6 `$ cd /root! {: U3 E a6 j
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx1 v7 ^) E( h2 U' m& N! H5 |1 W
$ tar xzvf nginx-1.9.2.tar.gz' P. L" u( K7 K; H. p s
$ cd nginx-1.9.27 b: ]) a# M$ u; O( P6 @2 t. Z& b- ?0 \
$ ./configure --add-module=/root/modsecurity-nginx: \7 x6 S' D7 x
$ make
) C0 ]! d, Y4 P( ], E% E$ make && make install% b: H6 u2 Z9 w a
添加OWASP规则& \# }# Z3 d# r4 y
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。" X9 a" ^8 D, J, S
# W8 s9 m7 F" y4 G7 g
( Z: c8 s4 a) f0 d- d6 x下载OWASP规则并生成配置文件
5 c" E) i9 [8 @# o: F- @% |7 U5 {$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git9 |9 i% d$ b* G; X- }) p( q" V
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
% i9 E2 F0 T7 f6 K$ k5 G" I6 R$ cd /usr/local/nginx/conf/owasp-modsecurity-crs8 d: K& B( u3 q3 z
$ cp crs-setup.conf.example crs-setup.conf
8 b; Z N* r# |5 U3 ?* M# v配置OWASP规则& i" d! @9 T1 A; ? C+ x8 M
编辑crs-setup.conf文件
% c) X6 p# t4 J; `7 f- t8 C
% g8 P4 J6 m- c' f! m8 J: ]+ U$ Z
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
0 A+ `" b7 [6 {6 Z$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf1 Z1 u; |+ @: u$ Y' h8 C
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf* ~9 O6 B' i7 F' s2 R! X7 [2 E
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf. ]9 q, [! U) t7 L- o: ~2 i
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
3 j* C* b; B r% P) ~1 k0 C) _- K* ^% B4 _' v
- `% b9 w D7 x
启用ModSecurity模块和CRS规则! N, k$ ^4 ~1 y$ i, K; a( C9 D
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
% j7 `' K( f/ i$ r+ W x7 W& b \- P' f
* t5 W Y8 f; E4 \
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
- E' X- |4 D" j8 }7 l9 O H- _2 y" A) Y* E( o% B
2 `! h9 z8 m, e" u# S w; f/ f
$ cd /root/modsecurity-2.9.1/6 d6 I& B( x6 z2 V" u' K
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf & N; ^8 j2 `8 E, e3 e t9 |
$ cp unicode.mapping /usr/local/nginx/conf/
9 S8 {4 }/ \: l, [7 w7 j" f. @将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
9 x2 z0 Z* x! G; ^! H
# S, \# w3 ^3 s6 u. h2 l
# k9 a B1 ?$ V. x" O$ vim /usr/local/nginx/conf/modsecurity.conf* |% f. g. \9 \
SecRuleEngine On% c8 e, O2 b( q" ]3 S* w
ModSecurity中几个常用配置说明: q: {) ~& K( m/ O/ h
; p- {' s. I8 W, k5 s* x4 h6 x' ~! o, B/ z2 k
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
8 {0 H' ~5 V F3 u9 f6 L
7 k" s! |, g6 |( {1 M
! [( i4 Y4 d V2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
' h, G; v$ J8 W% {9 {* V
9 d7 ?% c5 F; \/ v/ q0 V$ S- b' _, z. G) T8 {4 b- s- g, x1 ?
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
D- Y1 h H# P8 e3 S
& H8 p. n( X- O9 o' v5 H- D& x! |, B, ?9 |9 A1 Y
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
- a0 M" j8 s0 |. Q' S9 s1 w
- T2 G- d" e3 A% ]) a+ N( |: w' ^
) Z2 t* V9 n% ?- L! F, d$ {- ]5 S在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
0 T1 D* [" ^6 a* d3 o6 O' E* b- w# N+ t$ i
/ ?" G2 ^3 ^1 `
3.x版本CRS7 Q6 t1 K, Q. q; ^
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
2 b) m# F" T c, A# ]4 b' J" F# 生成例外排除请求的配置文件
6 X- c5 v; \( L7 ?6 C$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf. ~ q. R# r: j) |8 @- O/ ?
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf4 a1 [1 z# n, C( T! J0 q' o
$ cp rules/*.data /usr/local/nginx/conf
: _+ H; X% b) f/ g, P为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
$ n1 |4 O n B( u+ K) m% {- I/ p# O/ ~- R* X; w
2 S/ [& o! F; y4 |! @
$ vim /usr/local/nginx/conf/modsec_includes.conf. k) n8 x3 W# ^4 n5 h4 I5 w
) z8 z0 p# B) m% d
[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
( h6 j# m4 m" c2 i8 Y1 P5 r5 Z6 {
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。; B0 s( w- d7 C* v; o$ ?
, C5 p# V9 C" n. _/ s
' b3 {/ k, C5 F配置Nginx支持Modsecurity" F6 }+ t& t0 n
启用Modsecurity
( b& e4 d! ^6 \0 q使用静态模块加载的配置方法. M# P* O$ e. n' r- d
在需要启用Modsecurity的主机的location下面加入下面两行即可:6 W9 b: y" S+ q- _" z3 Z$ l
: u) F! E. O) C# G+ B
$ z0 S; n, n$ I1 I% ]/ J
ModSecurityEnabled on;
) t) V z; L) OModSecurityConfig modsec_includes.conf;1 l% l" b u/ k. l9 f% [' w
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
" H$ l1 i1 Y' x6 S: t! ~; K- w7 K1 B# a) s5 v. t; v
& Y. g1 p% K* Y! g1 @6 H
$ vim /usr/local/nginx/conf/nginx.conf* a) W1 I+ s9 o4 ~
3 l/ X9 M7 N5 A1 w% k" H
+ _$ X# K8 Q2 w
server {0 k3 S" E# v4 Y# h
listen 80;: p S" \: k$ ]- Z4 x
server_name example.com;/ l+ F1 |$ \( ~3 h% i4 J* [* G/ L
" p) Q7 u! O5 C" L! S
' z( M$ K( e8 u1 B; ]) I$ q
location / {
! n0 W9 h+ |9 K; T' ~ ModSecurityEnabled on;
5 o/ ]) k8 W1 {6 {( f. C% V+ y ModSecurityConfig modsec_includes.conf;
, G, `- r/ L4 {7 ?: v# z7 B- d' q/ F) k root html;
- H, q9 [% Z& U% Q0 | index index.html index.htm;
( k6 d s K" V3 o }+ j) a9 x& k5 e8 h1 t
}
/ Z+ X1 Y% M! _/ M( u' ?$ L, A使用动态模块加载的配置方法
6 m4 h" p0 I9 B9 n! t4 ?在需要启用Modsecurity的主机的location下面加入下面两行即可:. h$ Q1 w) h. j0 `- h6 X4 ~
) }& X$ k! t1 R$ ~- M
0 p1 a8 L4 z a) G9 z- e+ [3 X+ A# ~/ vmodsecurity on;9 ~6 v$ l' z* j, w7 {
modsecurity_rules_file modsec_includes.conf;- K( W* j2 j/ @# z
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。7 u; m3 ?- P w: M/ c: p3 e6 _
1 t8 N' Q; k* D. R2 F2 l9 M! [+ o
+ p& M) [* l- P! P9 w) z
$ vim /usr/local/nginx/conf/nginx.conf
$ _" x$ O1 M2 ^4 q/ m
+ _/ u2 r; Z2 U
- {5 v; O) \' z# U. J5 d* nserver {
@8 D! m2 q, h listen 80;
0 ?4 s) z+ Q8 e; A4 n server_name localhost mike.hi-linux.com;
4 g+ b* Z7 q+ G ? access_log /var/log/nginx/yourdomain.log;8 i, j( k- r6 W; T; w$ k# Y
( S! |7 ]. l% M4 Y. j* x2 y3 {- ^. h/ \# j T) c( m+ N8 H
location / {
' V0 X) H5 n! j2 v% N0 c. H% j, q6 c3 b9 b' x9 F
* W1 N! e. K M' j$ {0 m5 m6 k; H
modsecurity on;1 U) _4 q. N4 @
modsecurity_rules_file modsec_includes.conf;& g: w! v! l( `
root html;/ \9 ?0 y' I7 c2 C, Z, g& p F
index index.html index.htm;/ f, q0 m; o# W3 ?+ a5 ~7 S- Z- y
}
" w8 C' } t) i/ N6 i9 H6 ?$ F}2 A f* I2 {. c
验证Nginx配置文件9 b7 G. v* t0 ]% ]
$ /usr/local/nginx/sbin/nginx -t* P/ A! X4 ]9 y4 T1 G
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok _3 n; B0 w6 \( B7 h4 V4 S! Y/ L9 T
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful) R3 e! @# C* G, p
启动Nginx; X/ X" E* h5 i& h1 B2 a
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
7 K1 D" G2 u! A/ g0 {! J) T+ r; z: S& e; ~! D- `3 y5 d+ j
测试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能够战胜更多复杂/未知/混淆的攻击模式。 9 a4 L' w" l# E' d X7 g% u
|