|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。( q& r, I" s V) P ?, U+ h
8 x' l+ L0 P( x; k/ W; U: p
一.准备工作
. ~) r ?1 p" ^+ _5 _) w) S: d0 [' C
: \* G! E( w( S& z6 b G系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.02 E( O" u3 [7 p" W- P' [
& M5 m3 B$ F, n: }- k) D7 }
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz, G' S8 k, |9 d7 V9 t3 O. p
( k, A$ V" p, {1 z! Pmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz- P- Z( ~- G) A" j O& r9 g
g6 u& n, h. ~# H) r9 ` X/ g
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs! D. u% S' E' i% S3 b
, ^! ?; ?# s b/ V+ Q# H依赖关系:- p. ^& g* P( g, A4 g* K0 B
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
+ w- P6 k' ^/ S4 R+ f6 m2 ?6 g+ j) p8 Z+ D8 e1 @
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel5 e. W. t$ J) I
modsecurty依赖的包:pcre httpd-devel libxml2 apr2 P% ?, `7 G7 K; B# s4 H
8 ?! s+ Z+ E1 r7 Tyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
) K' N- a1 x7 X9 Y二.启用standalone模块并编译
+ M. X; _0 W3 s* ?' [( ^$ T
9 V; W1 ^* s' i R+ a9 m: V下载modsecurity for nginx 解压,进入解压后目录执行:( i+ y: {* w# U' o% x5 h9 D
* c l2 C% }4 D' t0 e `! w; e& R
./autogen.sh- S8 `: n/ W* X t0 M" x, b
./configure --enable-standalone-module --disable-mlogc7 W L8 { C4 s# _7 p9 J0 u
make " M, L+ M. b8 [5 \) n9 w3 v; W2 T
三.nginx添加modsecurity模块 z/ g) ^$ p% p0 p" {# V& k
1 N. Q( ^4 h0 L1 m T2 J在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
- W: v( }, S" s) `
& U6 v2 C |! H$ {0 M7 @2 u./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
/ U6 D/ g$ G1 U" \make && make install6 o' F+ A4 y, u+ X
四.添加规则 T, F6 z2 V. w
$ W& ^3 b& n7 f; z* s* Jmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。. r( ~. [4 |8 j0 ]
' K$ B& p8 U. i; g8 t# I# r
1.下载OWASP规则:/ R& ~: ]' F4 k( ?. E8 t
- j& Y+ {1 X0 B) [/ K7 ]git clone https://github.com/SpiderLabs/owasp-modsecurity-crs- R# X$ G4 f( L, ?" Q" B
4 M* U, b; o" `3 {0 X$ ?9 hmv owasp-modsecurity-crs /opt/tengine/conf/9 h) a' Y2 C4 K: Y$ n2 z
1 b1 v$ |& w/ u# A- q
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf O) m6 e! \0 [* q/ M3 z
2.启用OWASP规则:7 O8 D {& [2 Z; ~* q' Z
: M* _, E; _2 ~4 C" `& Q
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
' K7 ^+ l* y* A7 E/ c: P0 V3 A
+ a, P9 [- l- t# l3 h4 x7 R4 z# J r编辑modsecurity.conf 文件,将SecRuleEngine设置为 on' h. E( u% c" n$ t+ B; S
2 @; z8 L4 j5 h5 l& N8 }) Oowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。& |( y5 F5 a' t9 `9 s0 a3 o Z! k
# i0 a3 {; w; t% n; LInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf/ a6 N2 \7 Z+ q L
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf6 r7 N# S) ]5 N4 n8 ?& ?* _. C- f/ e
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
8 d! o3 X/ j; OInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf/ }( E7 ?2 ~2 |- [8 K
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf$ @% r' { N9 X1 n: y' \- Z2 w3 V
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf+ ?7 [8 |) H+ t4 m
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf& H" v/ p7 @% G5 j: O: D! v+ b
五.配置nginx
; x6 g9 ~; ~3 l# e
/ e0 H& X7 u% d2 D, k* ^在需要启用modsecurity的主机的location下面加入下面两行即可:
2 F$ i+ S' a& M" k0 }" S. \% f3 ?- I5 T1 e2 Y1 L4 W
ModSecurityEnabled on; \/ n: E* _ u) @/ r3 n4 C
ModSecurityConfig modsecurity.conf;
6 v' c9 ?9 T- W% ~2 `下面是两个示例配置,php虚拟主机:
3 @" F( W+ z4 `( l# y2 W; a
, d/ `" N7 `# r% b7 Mserver {+ v8 Q, t) S5 Y; Y
listen 80;5 _1 {0 e1 g6 ~; S: V# F C5 w! m
server_name 52os.net www.52os.net;8 i" e1 F: }' ^
) f* r# K0 w6 G
location ~ \.php$ {# }+ A/ ]0 _5 N# N1 w/ ?; D: Y, b
ModSecurityEnabled on;
: C0 m. c0 A; M0 p+ N0 a ModSecurityConfig modsecurity.conf;
2 I9 b3 F9 v$ b4 x4 O( U3 A/ \1 t
root /web/wordpress;, J! d# Y) v; E* ^/ l
index index.php index.html index.htm;
" m4 A7 P4 @- Y( i0 Z8 G
9 [/ @5 a# o: a4 B, R% } fastcgi_pass 127.0.0.1:9000;
% i1 c5 P* E4 ]1 M) D0 u1 V fastcgi_index index.php;
9 S4 M4 U y" u. K8 X% B X9 o, I fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
; \2 C* g9 k' n5 r3 W include fastcgi_params;
+ [; K1 u1 R9 ] }
/ y2 X" V y6 y& S- B: L }
; ^* T, O3 ~+ |0 K- k" Lupstream负载均衡:4 M( E& R4 A/ Y
$ a& o: y1 B; F$ T8 Z/ z3 `9 U- g! N# Supstream 52os.net {
" C; I1 [. t4 F' P1 E server 192.168.1.100:8080;8 `0 K) A' N4 V+ R0 i" |" F1 q
server 192.168.1.101:8080 backup;
& p# K* s$ W# i) a% j}
$ t: N6 J3 D5 I$ Q* D# Q# }( h4 I/ |8 _% T& M1 ], L
server {
- V& W4 ~9 z. K! y* s* Ulisten 80;2 H3 L+ E2 t& R3 m9 h" J( [: v
server_name 52os.net www.52os.net;
5 K( u: X; b2 R7 |0 O
( Q" G; q, T9 O4 M/ |& r6 ?location / {
( G+ S& L: M. S. D5 w ModSecurityEnabled on; 6 o" e+ J! ?2 A8 \# s" R2 n
ModSecurityConfig modsecurity.conf; ! O4 o% R* I: |# n# G3 P$ i
# q9 z4 Z, K8 Z; g' U; J' E) p proxy_pass http://online;
2 }. N' Q2 z! G* C9 I: [1 `2 K7 y proxy_redirect off; j4 o6 |" `$ I1 Q* ?
proxy_set_header Host $host;
0 C0 G3 c: L" S# T proxy_set_header X-Real-IP $remote_addr;
8 @9 L6 N( L; H5 g( ~ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
2 d; K' R' c% j- c* ^- r& |7 G% } }
. j+ a5 x5 H! |2 P2 r2 p2 [6 C}
( l' R: o) B2 J: `' o" T" t六.测试
" b6 r, x+ i V5 ~, W# f: h/ z' }/ q& C% X
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:* ^# c' j: N I, c$ X/ Y6 _9 q
( p7 V" L! o1 s4 v. T6 u+ Z<?php
' z$ | [2 Y( Y; H2 ~) Q phpinfo(); # T8 @" C4 f- j
?>
1 K8 }6 R* ^: U& ~6 t( n在浏览器中访问:& J1 V# D7 H/ b; o6 ]9 N! R
. d7 \6 l% f/ Yhttp://www.52os.net/phpinfo.php?id=1 正常显示。' F. K/ j# ?" J# [
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
. q0 t3 V) @1 {/ h- A7 G! ahttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。 ]7 ?" [( ~- j/ Z* S3 ]6 F3 y
说明sql注入和xss已经被过滤了
# H; s3 z) [2 q# j# | `; K5 B
1 G$ f9 S/ Z w$ N七、安装过程中排错
0 u: N, v; d$ Q5 Y4 f$ |
9 D) m5 ?9 l, N& B0 Q1.缺少APXS会报错
9 \# a0 |0 F* k1 P* t. |+ z; A
( u; r" F, C9 ^! G1 zconfigure: looking for Apache module support via DSO through APXS
: C0 m F3 b: z* D0 uconfigure: error: couldn't find APXS$ d4 \* J4 ^8 n' u! R- i4 ~
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。: N( i5 ?4 T S* j3 G
解决方法:
* N2 y6 C' v9 M
" v6 `2 D, y: U1 @6 byum install httpd-devel: y$ Z- H( s# J2 u" M3 w" ~
2.没有pcre
; E! `3 j& C3 c9 n: E& S
5 K$ L+ a% d( a% k' ~# l3 f# f4 a& nconfigure: *** pcre library not found.' |" {5 t# a" Q8 |
configure: error: pcre library is required
- c6 A) a* f: h+ ~! \" e& q解决方法:
8 U# ?/ E3 Y6 q( z$ `
& f) T' U/ n8 K9 k5 R3 ]" C. Syum install pcre pcre-devel+ D, s- s$ j& S: ~) X% C
3.没有libxml2$ r+ W* n( V3 z
% X: r5 q/ O) Y8 l
) M; v; F4 G2 c% D# D, _configure: *** xml library not found.
4 ~5 c$ H0 `4 I$ p( l8 yconfigure: error: libxml2 is required
- g; s, m: r6 F6 \5 Y% R解决方法:
; n6 y( f) }% B7 T, c* x
! p$ i- ?7 j* byum install libxml2 libxml2-devel* {. C( y1 d3 m5 s3 G' S
4.执行 /opt/tengine/sbin/nginx -m 时有警告
( k" p! Y2 O6 E3 `* p
% m1 @4 ^" H3 H7 j9 qTengine version: Tengine/2.1.0 (nginx/1.6.2)
5 t2 Q9 B; i5 D- a2 Cnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
# t7 b, ?- ` ~9 K1 O) a( u原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
/ F- {# E/ O- A, Q! B4 F d
: L* V: K- [) e! Q) Z% e2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.2 ^: Y& h* p3 E! a4 p5 ], | O0 M
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"' z! H2 p+ n7 C0 Z
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!+ R" U2 W$ x( b, q5 F5 {
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
. n4 M) E6 a: y; M4 O! f) o: i! a6 b2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
! C, J0 q+ }9 [- `1 S1 s9 r2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.1 _ m6 @0 n6 @& j% D+ S
解决方法,移除低版本的APR (1.3.9)- \$ ?! M, x$ W1 \* @# j
3 D5 o H) s4 M8 H. Tyum remove apr# r! T6 w4 v. j8 c9 P( K0 E
5.Error.log中有: Audit log: Failed to lock global mutex. R: D. J- w( b$ ^6 x, p
3 F' t. j% K4 s
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
3 N7 H2 x6 z9 | b6 Dglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]3 s! m: u& F( V: A
解决方法:
; X; W4 }) b0 K% S+ q8 Z, y编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:) m& `7 n+ ~( b, _0 H& _
: s# B! B2 l2 N* g4 A" X" D: `SecAuditLogDirMode 0777, ~! K3 M7 t4 X1 x1 A2 G) ~
SecAuditLogFileMode 0550" T6 e: E, E8 Y( {7 I% e) z+ A
SecAuditLogStorageDir /var/log/modsecurity* m- F( D) o+ S5 w+ `0 C: F2 m
SecAuditLogType Concurrent
! B5 C' |1 }9 d* c/ x/ f8 x参考文章:
( A8 I+ \6 V; Z3 {6 f. ?& k: Ohttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX- W$ D. p6 r7 r
http://drops.wooyun.org/tips/2614 |
|