ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
( s2 k' } [2 F$ K D/ n
$ @! z9 k# I# S2 E; x J% M+ t; n# r0 [: w0 N1 T4 }
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。" \/ r% X$ R9 E( W7 m3 @
( s, U2 _( [% f2 }7 }* t/ b
" [& m1 D0 M% X' X& Z: q什么是ModSecurity
( m/ X9 e6 B w$ V% DModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
# F$ v9 B8 v. q# v
! k& Q/ D6 Z4 G) J9 o3 i( p8 _ X8 s" [& a
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
) @. g4 u I/ J4 ^ o
! n8 ~/ g7 \; b c1 I, P. i; Z5 n
% s( t! V" J5 D# K4 C) [ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。+ f; D. {3 y- H' w' n& `* k
' _. I& o, S' G. j8 A' j
: [; Q: r% s& T! h/ D: e# b, f# ~4 y官网: https://www.modsecurity.org/; J5 a7 H: |4 @" X4 u" ?
S( V8 ]/ z: Q* v( u3 k7 Q+ c6 o2 Y+ @
什么是OWASP CRS4 ]" ?/ ?+ a' c. B% Z$ W
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。3 h1 I: y; A. i2 \
! c+ N% C8 ?, a% O* _
% O# v C& R, ~/ r% ~ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
1 i7 x, v) Q) }* e; L4 A# O
2 }0 |$ ~5 [) N, t C- i0 M$ C6 ]1 K, W1 {
HTTP Protection(HTTP防御)! i% O9 x, q4 }0 f9 _
HTTP协议和本地定义使用的detectsviolations策略。& t% h, K; f; ]' i& D" Q5 m
, f( b4 v6 {& i j! }$ K
; S# o* U( S; A) n- ^
Real-time Blacklist Lookups(实时黑名单查询)
* A1 ^: u4 V9 R# F/ l利用第三方IP名单。
" Q1 t) e1 p# X# ]7 f* T( t8 z& t
1 D& B v. T4 @3 ~" F, I9 b0 THTTP Denial of Service Protections(HTTP的拒绝服务保护)1 B* c7 b) P4 h/ B: F' r5 h7 M. n' U) J
防御HTTP的洪水攻击和HTTP Dos攻击。
0 i/ F. K- d8 Y8 d9 [) q
9 ~4 @) \8 V7 V7 A; Y+ n% u q% R" m% }) E; \2 h/ s
Common Web Attacks Protection(常见的Web攻击防护)
p: _( X. L8 e) I. K检测常见的Web应用程序的安全攻击。
- p9 e5 U7 f2 N, m( c# a( @3 P) A0 n7 M$ T, d
) \7 [; n6 ]9 P2 ]6 r u" a U% T
Automation Detection(自动化检测)5 |6 M! @! @4 t; m# o
检测机器人,爬虫,扫描仪和其他表面恶意活动。7 V/ J$ C+ b. X
Y7 r9 t% c/ \9 v1 d! m) f5 {
& H- A7 v& f4 M( ]7 _0 a) D
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
( B8 y# t' R; C9 J& w- t# [, M检测通过Web应用程序上传的恶意文件。
5 w6 s5 V& [) q# o& e* @) R2 x$ @, `. Q& }6 M1 ]( g) x: _
3 [- g5 p' w' O* k9 Q# ~8 p. b
Tracking Sensitive Data(跟踪敏感数据)( L+ f, w* r* o; V, k
信用卡通道的使用,并阻止泄漏。, s; Q% g9 H" n
5 M. G9 a* Q* m, N' O5 _! g1 R' {0 h9 N
Trojan Protection(木马防护)0 u( t+ n& i3 M
检测访问木马。
* q- Q. }) v2 w* ~4 x
1 C; H0 W6 M4 b5 D q% [# b- t4 {( ~ ]; y" y$ e- e3 @: a
Identification of Application Defects(应用程序缺陷的鉴定)
& y2 A a6 u" ]. U, n- r检测应用程序的错误配置警报。0 ~. _. b. R- _3 H6 C" Z- |
) c# |6 b% u3 H8 C: h& A8 l
0 ]& E) t" X: R+ v, c
Error Detection and Hiding(错误检测和隐藏)& l$ B+ g; M0 s3 W* t! H6 ~
检测伪装服务器发送错误消息。1 M9 g& i+ G- b [# Y9 t2 Q$ I: c
' N8 y+ D. y; R6 Y7 v) C
6 N, m. G. @% ^1 z$ w安装ModSecurity/ ]6 G4 C0 ^" Y0 X
软件基础环境准备4 G* c, X( n& ~- e
下载对应软件包
, \ V3 e6 D% R8 m1 i5 @$ cd /root
" u1 V2 e# e/ v% k0 `% r$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz') @- J0 H& A9 q7 b8 P
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
* b$ ^1 s$ W' G, r安装Nginx和ModSecurity依赖包# @2 n% D* u3 p9 c* I" b8 \
Centos/RHEL
7 k- @$ j6 P# g+ D5 y3 R' \! ]' W4 }, e2 [
u6 |, _+ m- e- F; H; O$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel$ X+ c" I5 E1 ]" d2 W0 ?
Ubuntu/Debian& W1 T+ n- e# w6 k% a1 R* L/ |
2 g" x; p- `% D1 n- D+ `& P: j* [
% \: C4 |# u* ?) Z8 _/ g8 k
$ 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% Q0 p" ], ]) w& ?' _3 m! ~
编译安装ModSecurity7 s% ^* X: ?& L( p5 E' h) c
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。1 _2 o+ P# D p3 K, c0 Z) @$ L* m) i+ s
$ k' Y0 }; o) Q* L! L3 P9 F% r
V1 m& N. E. F8 b8 ?- b, k
方法一:编译为Nginx静态模块4 a3 a; _# N' w$ r1 ~4 B! U
5 C3 Q* S9 D- p: [' [: h- M( i" T2 w7 f+ K# Q2 ]' G+ _
编译为独立模块(modsecurity-2.9.1)0 b! C- u) L2 }7 H& g+ G3 F a
$ tar xzvf modsecurity-2.9.1.tar.gz
0 C6 t4 N3 s9 }* m$ cd modsecurity-2.9.1/+ I' C8 ?1 N/ A( h
$ ./autogen.sh
6 b( A# x+ y+ r0 G$ ./configure --enable-standalone-module --disable-mlogc' X. w" y" Q; j6 C5 s
$ make0 |! E c0 p3 ?+ N, v4 X/ I2 z
编译安装Nginx并添加ModSecurity模块
M+ y/ z: ^# ?' F0 ~$ tar xzvf nginx-1.9.2.tar.gz5 `4 ]7 I! j, g6 |
$ cd nginx-1.9.2, u8 N! W* f% D4 [
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
/ T) g+ x0 ?# K/ o T$ make && make install
# a! c: |$ e( P5 i; |* P方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
4 J+ X$ l8 L% Q3 {! f
* z. u9 O' f% p y+ r- ^
/ N6 K3 X" \ W% c编译LibModSecurity(modsecurity-3.0)
8 Q! `6 }* q2 A$ cd /root. F1 P; Z5 N1 u7 e! x% P y9 M
$ git clone https://github.com/SpiderLabs/ModSecurity
2 L4 G3 ?2 u: t. H w/ ^: V' R$ cd ModSecurity" t3 g) I; i! q# A
$ git checkout -b v3/master origin/v3/master
2 ]9 D$ r- N' R4 J: W: ]$ f, _+ ]$ sh build.sh. c0 ?; d( o' r1 r7 `: {
$ git submodule init
6 v) s, F8 _: [5 D3 U9 i$ git submodule update
# P) ?% j) d# \" }3 s" a9 v$ ./configure8 b# S; t# O; V& X0 A8 }
$ make9 ?9 J' a4 w5 F: J7 ^" I& ^
$ make install
: M+ e5 G1 n3 b7 s) n/ rLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
7 k* Z9 [9 K' R0 k/ g9 q% I. R1 L% D- q
" h3 D+ V' K7 x: e6 }$ ls /usr/local/modsecurity/lib' x3 k' \" L# I' D& y
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0% n! s6 G! k0 _# E* n
编译安装Nginx并添加ModSecurity-Nginx Connector模块
E8 L. f) X! s+ U& E使用ModSecurity-Nginx模块来连接LibModSecurity
1 o4 j1 ^0 j" \# J0 x8 P7 Z/ X) h- \1 L6 t2 R- `
" \$ _( i6 I# d$ ?: H$ cd /root) H& A6 }( \7 \8 a$ c
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx! W, y& T/ H( {" M5 L
$ tar xzvf nginx-1.9.2.tar.gz
% u9 i& s' u) g* { x# V0 D/ ?' R$ cd nginx-1.9.24 `; ~9 `8 [( a3 g. P
$ ./configure --add-module=/root/modsecurity-nginx
, u8 y5 M( |1 H2 n$ make
/ l( E5 v8 a# p1 g( P" }$ make && make install: x# q- [, R0 J' }2 a: e
添加OWASP规则
) g+ g/ S( C$ r+ T) J- b! \: `ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
' d+ p% a) g# Q
5 s+ |) A4 ]. ?5 W! S9 e
7 T8 d5 Q, G: d% v2 K0 V+ ^下载OWASP规则并生成配置文件
* [8 R6 }2 R* T$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git& M3 d( x' Q# S
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/" E1 Y; L! c( P' W5 Y+ h! X: a
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs) j& C1 Y2 a# k: ^; x$ ^5 O4 C3 `' y4 K
$ cp crs-setup.conf.example crs-setup.conf8 D! |# f# ^8 f* c
配置OWASP规则
# c$ _) ~5 p3 O0 w4 V" \1 \/ c" f编辑crs-setup.conf文件; F) N7 E. p+ y) _: d
+ }, a( l' K' v8 l5 Z) B
& b( S4 e( G) O( q$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
1 K4 K+ c8 w9 u4 a/ g$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
. A% D* Y+ K* F& C5 Z& k- j1 k6 L$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf) E- ?5 s$ C4 H# o
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf2 I; T* ~- B0 _# \' M* E) w1 u
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
+ X' k0 K. g8 x6 K) y4 b9 G' b% h6 J/ @+ c7 L
" S$ z0 w; M. O: W启用ModSecurity模块和CRS规则
5 }* }5 H4 q! c& A复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。* P! }2 q5 o) g% y
3 z) S9 Z0 H6 g2 P1 T& G% x3 ]& s7 X ^2 c, `- C6 E/ \( k" d C, M
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
5 H( \8 ~ z. h6 W* y9 K9 P5 g' k# s1 i2 z3 @9 d1 M
0 ^% t. Q7 g$ Q8 F7 N( p) l$ cd /root/modsecurity-2.9.1/
) e# b2 F9 f$ A. o$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf . n$ Y2 F6 d$ a" `' y7 q1 p$ z
$ cp unicode.mapping /usr/local/nginx/conf/
. Y8 I+ [; Q2 E将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
* I" Y2 Z3 S: b+ J0 Y# H. ^& e
6 ?+ w' \& y$ y4 ~: p B
+ Y3 H4 Z4 r! l$ vim /usr/local/nginx/conf/modsecurity.conf
. I O& q4 A* k" d# @+ ~SecRuleEngine On
$ V) j1 M+ o# p$ A* J `# P# KModSecurity中几个常用配置说明:. G3 S. v/ Z$ ?3 S
; K) s+ ?7 A% j! }/ H+ P! i( M7 f8 W8 t0 ?
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。& [$ i" x2 b# `8 m( Y# r5 {0 A
+ A8 X* R% C1 X+ w6 k+ D1 P T
1 P, A. z7 z$ J9 J9 ]( c: z+ V2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
( Z8 {0 [9 l3 c& u7 z9 h
6 |. g* E/ J: s1 U! x
. w7 Q0 R' J8 G3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。4 K0 B/ ~- g) A1 l3 V$ k
# L9 Y7 u( K0 E9 D9 K# W p* v( R' L3 G# r0 o$ p2 m6 H
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。2 H+ ?3 G( T5 ?% ~+ ~6 M
$ q/ `" r1 ?4 ? q3 A0 s1 m
( C; ?$ g: D9 c$ Y( G6 B在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
) E' K9 K: G! C; v( Q I) Z9 V- f g- R8 @
9 X9 D( h( T G h: l, ]+ H9 {7 z- H
3.x版本CRS
8 L3 @* j4 T3 ?* [$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
5 S4 x9 ]4 E+ @6 A1 \# 生成例外排除请求的配置文件
+ h) }+ Q- Q% y$ r/ b5 X$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
% b8 \" v1 n, \# D% ^4 F! ]$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
& w* Q( {1 g4 z: f1 p5 f8 F$ cp rules/*.data /usr/local/nginx/conf
1 ]6 M1 H/ v$ u( o+ W为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
$ l0 e0 ~; r( M' C% K2 v9 w4 t& [1 X7 f
3 \" T R3 t/ n. F. ?
$ vim /usr/local/nginx/conf/modsec_includes.conf& G7 u% Z- z- x' @0 B0 Q7 ^
# e! k! E3 t! U! C/ ][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
F2 M+ c" B+ g+ g' [7 x
$ e. m- L! s! F/ v; \1 C注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
# D0 Y- F) x6 \7 s9 N6 b: E i0 U9 }# @3 ~, M; N
$ d0 e7 E. o# r# ]* O/ f
配置Nginx支持Modsecurity
0 @% {3 w3 [% p, t- H启用Modsecurity( ~! m D( P) x# f, P
使用静态模块加载的配置方法
$ q5 |" R g+ F! _9 d, L在需要启用Modsecurity的主机的location下面加入下面两行即可:
) F( z7 P% n; R2 B9 I+ W' B
$ u% V- s2 t! |/ e+ n7 {+ f7 [: R* B/ `; @/ Y( w4 ?2 V
ModSecurityEnabled on;$ v _, \/ v. y3 g; Y) o, j3 Y
ModSecurityConfig modsec_includes.conf;* c/ D' G' T1 w! E& ?; i
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。* \" X# `; t* j0 V3 k8 n y
$ M8 l5 C2 `0 r0 W: R* o
f" X1 @7 b( d& y$ L7 `$ c7 p
$ vim /usr/local/nginx/conf/nginx.conf$ b* A/ T5 N- @$ j' i! T: x0 F
$ e" [5 a5 @& x/ c0 y6 e9 h9 w2 j/ ^% f
2 u9 V3 ^* Y% {0 ^' C* F- W
server {8 x9 m7 c0 l% A3 ^ O" l' l+ a
listen 80;
* N4 n; Q+ z- |; F8 c server_name example.com;
# Z1 e; ~) ^5 u( ]2 E' t: t7 _. q( C! v4 R1 [. v
* {1 d- u* ~8 R location / {
, _, s3 R% j! t% M- k ModSecurityEnabled on;
2 @+ m6 u* h D1 K ModSecurityConfig modsec_includes.conf;
7 |+ p; X3 [* F root html;
0 j3 _. I2 H5 u index index.html index.htm;7 y& Z" z. _! X4 F& z
}- ]. C& a& [! r; e, z3 }: w0 B" r
}
. ?+ `, d! r4 {$ C( n: t使用动态模块加载的配置方法, k$ b5 U1 C# I, n+ Z$ u
在需要启用Modsecurity的主机的location下面加入下面两行即可:) q3 i0 B: E+ y& q8 g
5 U3 E3 W/ `. Y; N$ p4 z
0 j0 v% q7 e: c, a9 y
modsecurity on;( F" W7 x* W/ O$ Z: G* L
modsecurity_rules_file modsec_includes.conf;
# O# ^$ Y8 Y; B- S) y) A r2 E4 a2 P修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。' i) o8 g+ F3 V2 Y* c
: R8 @* u- S2 W4 y; x. Q- m$ p0 P" _4 v7 m8 D* M( W f. f% _/ @; f8 \( B
$ vim /usr/local/nginx/conf/nginx.conf
- ^; y% U" `( v/ V* s1 W+ z( o8 K, q
* b4 Y, K* z, X# K5 oserver {8 k5 f4 ?2 {( D. V! `) Q' W
listen 80;
4 n) F7 q3 @4 @4 \4 B server_name localhost mike.hi-linux.com;
5 h6 M! b0 U5 G5 g% S: m$ ?5 x1 g+ M, b access_log /var/log/nginx/yourdomain.log;
$ l9 l$ s2 O0 C( N4 q/ A: Q+ ~3 h( u1 y4 U! z' A
# x. v3 R* ]$ a1 D
location / {$ g$ s2 l* ^. K9 l8 X8 n
! U" H0 p9 R2 V& j$ s# `+ n
" [8 [2 y" C+ r/ A! ? modsecurity on;
% C: {+ T9 F$ ?9 N1 U modsecurity_rules_file modsec_includes.conf;0 e8 Z, Q1 C+ `
root html;
+ J9 F% d" r6 {: z% @ index index.html index.htm;
* s. m. [- u) ^6 x6 v}
2 Z- I; s% g8 t& L3 H" Q}
) V1 }# ]' M4 m3 c验证Nginx配置文件 L% ^! k* e8 C, q: o
$ /usr/local/nginx/sbin/nginx -t
' V6 E8 H, g( S, J+ a3 W( ?! r# Vnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
) t$ S5 {0 [8 a; H1 O3 V7 Anginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful, L, m- m1 w( ]4 Y* F5 ?
启动Nginx
' f/ W5 F4 ~! O! g( w2 i$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf& @, {8 [5 N3 g/ T) a
. H- n; `; c+ r. C测试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能够战胜更多复杂/未知/混淆的攻击模式。
- j: V0 g! ]# N Y8 w, a5 F. s/ e |