|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
& J; @8 b0 T0 s2 r
, y w' S3 P# c% E q( \7 p一.准备工作; |% U4 l! N7 z4 G
' I8 ~, @% f+ B8 C6 Z* ~# J, O8 c" R系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.03 c8 ~5 @7 n9 e& r6 N! }) \
$ e g5 D2 W, d2 ]! x1 Ttengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
- J$ s) e& w" O' X+ C2 ]5 _
9 k8 o3 y- R- X* y& ^6 R9 nmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz* k% O) [+ f6 d6 \0 T m- {5 m
0 c% b* `4 ^. ^/ g' w3 ~' z5 oOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
9 ^/ R( E" ^# K
0 x/ o" \7 l: w依赖关系:
. ~3 [$ q* b6 X: t5 }* `tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:9 w0 m4 ~9 ? s6 I4 b
, G/ |5 k. o/ ~$ x8 S2 R4 H# U) eyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
4 c/ X; \1 o' B; R3 `modsecurty依赖的包:pcre httpd-devel libxml2 apr
- k. v1 ^/ K- c, [! O# Q4 Z9 S* }( z" h) [9 J& @* v4 T/ r8 f0 o
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
4 h; V9 {6 J. N: z3 E二.启用standalone模块并编译
/ l F- T9 n/ I% p5 g, u5 G( b1 M I# y0 i. ]* S/ }$ a6 \4 p3 V
下载modsecurity for nginx 解压,进入解压后目录执行:, ]. k6 D# o- I% f) z1 ]. _
+ H+ b" o" ]! y6 d8 x./autogen.sh% i0 o0 O) ? @* Q5 _; ?
./configure --enable-standalone-module --disable-mlogc' M; R# Z7 [8 l$ x0 V# P1 ?* q* I
make
4 G, ^6 u; C5 n1 G3 B三.nginx添加modsecurity模块
3 \0 S5 s$ Q- f& n) p/ u3 S4 f2 s8 D/ p5 D8 @3 F
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:9 |; F: d4 j' L+ B' ]! E
. N M: ^3 v0 f" l./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
\& Z" k% H$ `2 [- g; emake && make install
4 \9 Q5 G7 Z- G% C2 n四.添加规则- ~% V6 b& R; v% ~4 D- S% L( s
% t( n0 f4 w& b Lmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
0 C) t# E9 |# J( V; y& R% ?
; K$ F+ Z4 ]+ o1.下载OWASP规则:% S( G0 j: f9 [! D, u
" Y( J: m! O( M4 ]! q, xgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
+ s: K+ ^5 ?$ G. O; M
/ w" P7 B. D& o7 A& L. tmv owasp-modsecurity-crs /opt/tengine/conf/
( T7 s g7 ]' @3 U# L# r, d0 M- g5 ~1 a( [2 h
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
# }( c* \. v7 E2.启用OWASP规则:+ f3 X/ A1 P( g4 Y' ^% K
4 G$ w* r# O1 m8 m" G. D8 V
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。3 Y5 s# j Z- [
) W3 y" o$ ^% A. C/ e( v. u编辑modsecurity.conf 文件,将SecRuleEngine设置为 on+ O3 P9 N! Y' v5 {% H
' K% z! k- y5 Qowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。: ~9 j- M$ k; A3 ?' Z
6 W- W' I. ^ a% n/ gInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf3 T* f6 {% S) q/ k6 ?5 w0 b5 F7 _
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
^0 Z* X" k d( D5 pInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf8 k% j+ U8 O8 z$ O- w8 ^0 ]+ O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf% m/ O M! d: l: [1 d; n' d
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf1 m* Z/ X# z, m( R
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
2 [" k. @$ d: D6 q; w& fInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf/ a. R; r8 H7 g
五.配置nginx
! M/ o# k" r% @ W( n
) N% R9 B& ?! H8 ~, u在需要启用modsecurity的主机的location下面加入下面两行即可:" V% T& |* J+ a" u) f
2 q& O: K+ _" `5 @1 W' V- L- E! ~
ModSecurityEnabled on;
2 x: x1 Q7 ]; }1 J2 q N9 vModSecurityConfig modsecurity.conf;: ~( C! Q1 Q; B( \
下面是两个示例配置,php虚拟主机:/ i6 y! M- k H M6 [
+ }% |5 X2 q% a/ v( c
server {
4 J8 O& @, {$ V' n2 `) [ listen 80;9 Y. q1 X( H/ _2 l+ ]8 c- ?
server_name 52os.net www.52os.net;
& D. J2 y7 V: x; @$ m( Z - s: g, c4 a& ]# o5 A: t. L
location ~ \.php$ {
7 W; E- v/ v p9 q- D9 h ModSecurityEnabled on; . r* B2 Q# r: p7 b2 o' K
ModSecurityConfig modsecurity.conf;% J7 Z. s' h' H6 w: Y7 n K' k# o
. `+ m1 Y% Y4 U$ t( C
root /web/wordpress;
7 R7 a. X3 e+ M. ~ index index.php index.html index.htm;
+ C* R/ @) }% x
7 e8 J9 K% [6 A! f y, h fastcgi_pass 127.0.0.1:9000;' T2 i1 g* }: [& B) Q( b- {
fastcgi_index index.php;
% k- K. |* k' o1 } fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
5 a7 d1 F b- d$ U1 ?' c include fastcgi_params;
4 s K0 s: ]5 {' h* ` }
) Q) h9 _. g) o2 @6 Q- B }
; W w. T& _; U8 _+ h" t& E7 r$ }upstream负载均衡:3 F l* Q; _1 e
' u4 s* R4 Z" f% N2 m* v9 X
upstream 52os.net {" P+ {0 t' J. M# w0 [' t
server 192.168.1.100:8080;
& \. ~; ~5 L5 `$ u8 a8 A server 192.168.1.101:8080 backup;
2 V2 Y4 O! @8 \# L}
2 r% ?+ M) r9 s% X2 M9 P9 {' m! Q4 T0 U( N4 X
server {8 F# G" @0 X& G( a1 V* }6 p' I/ {# l
listen 80;1 @0 C; _3 O# F" z5 D- P z) v6 }$ v
server_name 52os.net www.52os.net;! U; g! Z% W- L$ B. ~% v
- s5 ~! z7 x* c4 O# i
location / {
/ k: Q9 g( N" Q5 i ModSecurityEnabled on; ' k7 o/ F0 ]. u/ @! w* K( d
ModSecurityConfig modsecurity.conf;
- i0 a7 D4 `5 K; M5 h) Q! u( I0 h V/ `; t
proxy_pass http://online;' [3 j' g! `0 ]
proxy_redirect off;
2 ^' ^6 z( }- Q, k C: M proxy_set_header Host $host;" y' i; A- j4 a0 G; u6 U
proxy_set_header X-Real-IP $remote_addr;3 M) L: f+ h. v/ u% W; G
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# t! e! D A) R( V! p1 p3 ]$ M7 J }
9 s: Q; k1 h0 d2 I) p0 q}
3 _; C* }7 l$ q5 G* T; |六.测试0 Q0 O; l0 f H) I: z* c
5 E Z9 S5 t1 M, _) Z$ A: |我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:+ @1 J( @8 t& h
! m& m5 W, W8 b7 `& |
<?php
/ d4 o, O# T$ P0 ]% O& c2 i' h phpinfo();
% s* |/ i2 V; O: S0 u/ z. E; F?>
8 a6 Q" u, ` n* Y9 _9 Q% n1 v在浏览器中访问:& {. ^1 d1 x% q/ s3 s; e
6 Q1 { B1 _! Y. \! d
http://www.52os.net/phpinfo.php?id=1 正常显示。1 s4 m. I" N! |7 i% q
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。/ j/ ^$ B1 C: j; o# g( t' q# j
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。% o: v+ o( ~& J: B1 m
说明sql注入和xss已经被过滤了
- r. q! J$ p$ B5 B# U; I' M) H u* h! S& O' n) W' x' P
七、安装过程中排错
' R* c7 |( Q# }: P& |; }
9 O: K& x: [9 j1 H# P1.缺少APXS会报错) E7 D" E/ h# d6 w- M
" m( @: { |% K6 |; ~3 c5 ~% {
configure: looking for Apache module support via DSO through APXS& s8 r* i: w( ~8 _% x/ F6 q
configure: error: couldn't find APXS
& W. k; l# e0 w0 r7 {apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。; c- A0 r# {! `) N" J8 \
解决方法:
- ~' Y% c6 B4 \0 O: l" c: S* Q0 I: B9 ?: j0 M$ {
yum install httpd-devel3 k$ ^7 M U* p( a# w
2.没有pcre
4 V+ c! b/ A3 n7 T
; q3 _: g1 A/ r) ^6 \% k* i( zconfigure: *** pcre library not found.
# l. i( Z% U! F6 Econfigure: error: pcre library is required% {( v# s2 L2 G4 \
解决方法: b) i( z S& ~0 V1 |( \( ^4 w
# N4 \& P3 r+ F8 A# L7 Lyum install pcre pcre-devel
& i2 M: |& Z1 W+ l% L/ g' {3.没有libxml2
. }" ~' y5 g$ R4 y( N9 _$ C: [9 b) ?0 `1 k% q/ B
& z2 l E6 B3 Econfigure: *** xml library not found.
) l# A- d9 _. G* R& |configure: error: libxml2 is required! g* z) @( H" B( Z
解决方法:
) N* y9 W- C$ f) y8 Q' P1 J3 j
5 w# w# O" Y9 V" K) [$ Yyum install libxml2 libxml2-devel
- K6 W9 C$ a$ g1 s3 d4.执行 /opt/tengine/sbin/nginx -m 时有警告% R; Y; q1 ^4 T- }2 K
8 Z) X6 t" P! W2 _6 vTengine version: Tengine/2.1.0 (nginx/1.6.2)
, Y8 a7 U3 f* s, Z4 gnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
1 R4 _+ L, o: Z: q8 ~* b原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log( u3 p- ~3 Y) R4 v# u6 k. r) I
; x4 f* Y* \7 L: s1 W3 `/ O* R( d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
\, s; p& K" T' N, ~2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
. U; z- e, j: l4 q+ _- w: C) S2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!( a" Q- x ?5 T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05". z: w# |, s& u% ~" [
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
! [0 ]& N1 {3 S2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
+ u& R$ g7 J x& f" x% }解决方法,移除低版本的APR (1.3.9)
6 \3 d( @1 j) w2 O9 G P
# q; Q0 o- u; Q3 w4 Wyum remove apr
% x, U9 y. _7 q4 n U5 S5.Error.log中有: Audit log: Failed to lock global mutex9 v7 O2 \. u2 u% R- P2 q: M
; ?" n+ @3 ~$ R$ X& g. m9 C
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
/ P$ H4 V. D+ {global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
7 ^& ?3 Z6 w* m* b5 M解决方法:
6 f+ A+ R: n, Z; m编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
* v: m& s0 O3 i- x/ `8 r7 [5 E6 u% Q: J: [$ x9 k, ~
SecAuditLogDirMode 07777 I' B! b/ b' s" L: H
SecAuditLogFileMode 0550" ]/ \" f% k% F: D
SecAuditLogStorageDir /var/log/modsecurity
- k6 [( A# T9 N; D* sSecAuditLogType Concurrent2 J7 h. u0 p K) o6 m
参考文章:' K+ d* I; I8 q6 j: ^. S2 _
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
6 d0 K8 o# U+ |: Hhttp://drops.wooyun.org/tips/2614 |
|