|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
[: y( t. i& b- G/ o) h3 V2 |( ^) Z- Q. I- E/ d
一.准备工作4 Q! `, }, y1 T0 d/ d/ r! S
7 v: T# a" L# q/ ]/ p
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
# f$ ~$ [3 ~) k: _/ _" s |; V3 @. }- q7 q- j
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz5 z( p, ] h6 z
1 s0 U# d; T2 Y3 B5 _5 j
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
/ n8 X1 s0 j& \5 f" b6 C
7 r# l7 J: Q# Q9 F! E' wOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
& _" s7 [+ W9 Q; `! x ^, e0 B3 h; V! b3 n( ~4 U& g% z5 ]4 ?
依赖关系:
& a0 `* |7 \* Ctengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:2 q+ F9 w+ T5 ]$ G; n' I3 s9 {# ^ Z
+ i; H4 }3 _! f1 H8 D2 n5 N4 d
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
! r" Z" R! S8 a( ?. v8 ~modsecurty依赖的包:pcre httpd-devel libxml2 apr4 @. B- H% s# V
* p+ \! l @ |/ ^, j" Wyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
1 v% G7 A& k% b9 @# d& M二.启用standalone模块并编译
! \/ R6 I1 k4 G: k6 ?1 k, ^$ z5 Z" u& W! _1 S. a5 V' W7 U
下载modsecurity for nginx 解压,进入解压后目录执行:2 g; p# a. Q/ L5 b) i8 s/ e
- G: I! U" y% O- E) Y* D! g0 c1 |
./autogen.sh8 H5 C7 _; M5 e* e# a
./configure --enable-standalone-module --disable-mlogc5 Q2 g8 O. h( M5 x; Y
make
6 [& b4 R# J0 b2 L2 j9 P8 t) M0 t三.nginx添加modsecurity模块
. {( U- v6 c/ d- A, C
; z5 K5 N1 g$ {# n O" e在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
8 p2 J' b* m# ~: P6 s
9 @1 {& d s/ k* G./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
3 ^0 D# ]( ^7 E" `make && make install
' \4 F5 \$ Z, X( l4 z3 O4 A四.添加规则
4 x. k P- }0 k! u l$ T; {1 M* u, @$ @- d2 D% f) w r
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。+ O8 I( a3 U8 A; K+ {
: z$ v" Y; d; x& B Y* _
1.下载OWASP规则:/ ?8 j2 y4 Y6 j i6 H
% }" n$ o& T. f. B8 a" p) r
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs6 c1 X; F+ h$ C) u4 ~+ y; j
" H& o5 q) x: {2 a Jmv owasp-modsecurity-crs /opt/tengine/conf/
# z. t. G. t; ]2 [5 w! }2 [2 u" r
+ G- ?; U; k3 X) k# @1 g3 rcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf" Q# \' G+ N9 p% F
2.启用OWASP规则:
/ x! |' o$ o( A- r% u9 s" T2 B3 E$ G3 j" R. Q
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
$ _3 p' J: S1 U) P1 Y8 \, c+ W0 X/ B( x1 [4 n% i; b" V& |
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on( b3 t2 x+ u# x H8 S; ^5 X
, U2 w- X* s6 }
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
; G, @: j7 b/ L! @
+ U: G; _$ D' g& YInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
2 ~- ~& J' B u9 oInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf! K9 X. G# U+ ~& y4 Z8 P6 Y/ ?9 P
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf1 B( T* c1 F+ X# S* R7 D- \- A
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
9 n q1 t y) KInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf# H( H3 L7 h: t5 _& s
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf& A- s/ ?/ v+ i
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
0 y4 _9 V3 c( X# p5 B五.配置nginx
" `. q3 U, w4 O- K( @5 S: ^5 V9 i6 d
在需要启用modsecurity的主机的location下面加入下面两行即可:2 R6 \" F, g: E, V1 x z* n m7 V
1 K) b7 ]3 v: n; k2 d, z$ [7 |, qModSecurityEnabled on;
6 P: i, W- D. ^0 L) T- K/ ZModSecurityConfig modsecurity.conf;% r$ b# Q/ r; j3 x
下面是两个示例配置,php虚拟主机:
; E# O; [& F, m$ N/ ~# Q, H# p" ?) P7 h
server {4 Y7 X' C4 u) J7 v+ A7 o: L
listen 80;
' R- g( c) V4 f8 [, o% k server_name 52os.net www.52os.net;
* m. v8 @( s* M " J) M6 R" P- Z/ {, |% o4 {
location ~ \.php$ {- m* P5 W! X$ c' H( D- m
ModSecurityEnabled on;
! N9 O0 S# _) T; u- C& _) f) d ModSecurityConfig modsecurity.conf;
! V8 m4 z5 r$ h3 A
" M6 }4 P& R$ x9 D root /web/wordpress;
9 ~8 j0 |8 j4 D; c. {% i index index.php index.html index.htm;
) d: k: a; V( P
1 T' K% f* G# g' x. F fastcgi_pass 127.0.0.1:9000;9 b1 N% o1 Q+ b1 @9 B, h5 U
fastcgi_index index.php;4 K- R& w4 @" j/ F4 @$ p# I
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
1 ? b/ N7 ?( F9 k include fastcgi_params;
: W+ q- \1 I* V }) b9 [( H5 M; ?3 t9 B% O/ R
}+ K& m2 e( ~+ g$ Z
upstream负载均衡:4 i0 J$ I D- v$ n- ^
+ H( C- e4 ]$ V* m4 {- z/ h
upstream 52os.net {
O! u6 ]9 M7 [ server 192.168.1.100:8080;
% N# W* B+ O; l# P. K5 @ server 192.168.1.101:8080 backup;5 ~5 E& H- f l& ~2 q5 X
}+ v, T4 t: ` ?
8 m' j! W ]. ~2 a) P+ v
server {
: j6 i: c0 Q( ^+ x- ~9 D0 r( Wlisten 80;+ s% J& H2 `$ h2 s
server_name 52os.net www.52os.net;
2 d2 C$ P g+ I0 n8 T/ W- I4 W {2 c
location / {* D+ B3 r+ D }+ U7 ~
ModSecurityEnabled on; : P1 t2 `/ S, \! k1 v& X! _
ModSecurityConfig modsecurity.conf; " v( J N) e" N0 v( g
% L. c* u' M! @. x% t proxy_pass http://online;& k6 J( }/ K" t( U
proxy_redirect off;, r; r9 A% K9 ~6 a! y" M$ X
proxy_set_header Host $host;! ^0 |/ W' y+ V! y+ [
proxy_set_header X-Real-IP $remote_addr;, ~$ ~# R- k8 x6 M: |1 H5 O1 D7 A
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
/ _* ?9 i& ^( n- o' J }2 C0 `3 U, Y5 d2 e- ^# c# i5 q `
}: c: M9 _, d4 z9 I2 l4 W
六.测试3 s3 c7 [3 w) Z7 e9 D m
# E* g" J( s0 {4 l) \) M ~2 G
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:5 U7 w8 C% m& f, A7 s1 a1 z6 {( u+ d
. O* g7 i7 @3 |3 p% z<?php) F6 c c" {/ ^; W' ?
phpinfo(); ; p M5 A( Y; a4 J1 Z7 U
?>. Z; ]5 {6 y+ ~
在浏览器中访问:% ^0 G6 ]4 ?+ C8 p
$ q N. K' ~( f" k6 C, Rhttp://www.52os.net/phpinfo.php?id=1 正常显示。( p( \# ]$ E* H8 I. l4 [
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
! z. A' L7 @3 W8 J" Qhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。; S/ q8 s, R4 w) Q J
说明sql注入和xss已经被过滤了
: d. H. H6 ^* {. y: m+ x, v; w8 u- m5 C' L; Q8 D
七、安装过程中排错
% h; r: e( Y# n! K$ ?) w- c' F, f
, s7 M% E; J1 t/ n1.缺少APXS会报错
2 t6 o) v1 u+ f+ h
+ j5 n1 {+ Z" I/ a# C6 Kconfigure: looking for Apache module support via DSO through APXS" X3 p" d( n! a6 x( C( y
configure: error: couldn't find APXS
7 ~' M/ U* ]2 P" m/ T' e, ~. Eapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
/ Q; V! V3 z) A# \0 I解决方法:6 Q9 ^' d F# B1 X1 A+ s
/ P/ p4 x r, u. X! t$ L) h" c2 U1 W
yum install httpd-devel* r2 B: G& @1 z r2 D9 t% a, i
2.没有pcre; Z. o0 X; R5 u4 U
& h3 Z: S8 }/ {2 S- V3 p$ K
configure: *** pcre library not found.0 ]9 ` [" ^4 A6 N
configure: error: pcre library is required
/ f5 A% v- L. Z s解决方法:; r+ j8 S7 D1 r4 Y
$ e) N8 a4 K7 x( @) n' ]% o
yum install pcre pcre-devel) G' J, ^0 i) j q
3.没有libxml2
4 i T. ^( W5 r( x# \; x1 |" P8 h9 ]) X ?2 Q$ X
! Q8 u' z% }( g- p' f
configure: *** xml library not found." C- s8 _4 N1 A6 C7 D- B, e' [
configure: error: libxml2 is required) n c6 f. z% d8 R# I5 \$ Z1 |- a- U
解决方法:
2 w) g7 r! E. r- h7 E* E, i
3 k. N( l* J( ^, T; M, m$ M( Myum install libxml2 libxml2-devel
" u: [ B$ b S4.执行 /opt/tengine/sbin/nginx -m 时有警告, v1 h/ G! B$ E+ n/ H
+ D9 I/ _3 B( ?; S$ G; U
Tengine version: Tengine/2.1.0 (nginx/1.6.2)0 q7 K& Q1 V2 x* ]) p
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
# X$ k( G$ y& Y2 G原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log. p# H' z( ~% Q% N' c" i
1 K7 O* C& ?1 P# B3 I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.* d8 G! T, }& l) j, g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9": q) a2 n3 e+ G3 g% L( t+ v4 O& O
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
7 j2 B7 v, C2 V2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"; J6 x, p3 }) V7 g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
+ T. _1 I$ f c/ J4 G4 g; J( S4 {2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.# x9 r6 g$ ]. D; I$ M, Q% T5 i
解决方法,移除低版本的APR (1.3.9)/ C- E5 ?$ u/ b
7 Y. W* `- \* ^; z4 _& ?
yum remove apr
8 x" j1 t# h0 a$ ?, \* b4 S! ?5.Error.log中有: Audit log: Failed to lock global mutex
$ x! P; ^" C1 G" b; h5 c
( p2 K! t1 Y( |2 y; u2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock E" @5 w% M- }3 J
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
* t; e) S: n1 a) y解决方法:2 M0 R$ t$ b8 K& M' I- V6 Y5 x% M
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
3 a! Q: Y/ V& [4 S: b2 C! e1 J0 M
/ E' A( ]3 W: p' P8 pSecAuditLogDirMode 0777
3 H# V' z1 b/ `& @$ C( dSecAuditLogFileMode 0550
+ k3 v4 P5 J y' tSecAuditLogStorageDir /var/log/modsecurity1 W* _# u. I! h p% B
SecAuditLogType Concurrent
1 u* b2 s, O# v3 C: P参考文章:& \+ H4 w( ~4 ^" ]7 j5 @2 t ^3 @7 K
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
3 e3 J& K) V8 rhttp://drops.wooyun.org/tips/2614 |
|