|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
3 a! c9 W( D8 u# C% o
6 X" e8 x( K, O5 C" [6 i% {一.准备工作0 m# Q0 s/ c" N9 Y. ?
) X* o) l, F" k6 T6 w" {系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
! P: _: i/ C4 j G% I! h1 [, T3 x3 M& O) W9 P
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
5 T+ g3 F0 I( @6 W! w
3 {1 X8 Y6 |( l" d& j: gmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
/ @2 z1 B. ]1 w( f0 Q, R# n" Y' `" ^6 W
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs T7 Q! a) \0 _# p, d
5 X' F: S# l% k7 {依赖关系:# a5 _$ p$ b7 G8 h: u/ g
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
c! W! k3 j4 g- b' E* ~) M+ j+ s# I/ d: g. i- |6 s
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
N. C# d6 W6 S O J! Q' j3 b: M* omodsecurty依赖的包:pcre httpd-devel libxml2 apr2 N2 b0 R. y9 e8 U: x9 O. |. H0 S
* X! V, l- f4 l3 U8 h/ u) {
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel8 {1 E. b$ W: w/ A9 b
二.启用standalone模块并编译
/ h- j3 K' S& F* S1 N( k& y$ `6 v& b! `
下载modsecurity for nginx 解压,进入解压后目录执行:; I! v5 F: X& p* I6 [% s: l7 g
f& m" R5 q% z$ k" E3 m0 g./autogen.sh6 b; x1 C3 R S- \3 B
./configure --enable-standalone-module --disable-mlogc
C z* q2 h) R* pmake
$ R8 F: U2 y1 t三.nginx添加modsecurity模块; E8 d% ~8 e! m9 ]9 k! {" M$ [
0 k7 D5 p Y" a# d; z" F# h在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:- z* p% ?3 N5 L- {
- _. A1 B; @+ L
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
, j( x+ {* ~# z. \, y8 X' g, D4 nmake && make install. |6 j# q& Z3 j5 k. F/ H# c
四.添加规则# y4 v3 {0 F6 N% ?" i
, Y) P' E" o* f' }/ ~modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。8 c5 p# h+ g' f# o
5 b, ~/ S' f$ _. m- P1.下载OWASP规则:
7 f/ K7 F6 p0 ?3 T
% h* x: W' z6 C/ v! Ggit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
0 f5 T, v2 Y4 l2 q% m" P
! H: u$ N8 s4 X5 P/ U( ]7 F1 nmv owasp-modsecurity-crs /opt/tengine/conf/3 V% `3 t& }1 [9 y) P
* n' U6 X( W+ `* {3 G' \
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
+ y/ g3 K3 q" t# `+ ?2.启用OWASP规则:: t! ^1 P8 E k: m |
( e! a0 s" ?4 L9 P1 T8 |复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
8 `* M/ m- Z9 D( j5 K+ u( u8 _- F! _: z5 |0 s9 `9 A5 E u
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on' ]2 v% n4 E! E( ?
+ M1 ~9 H, M& l xowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。9 _" q: z- d2 f2 j% S: h# T5 l
3 [& X2 H* M3 E+ r* w" Q& v
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf% T6 B/ h/ B, N+ j. }
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf4 e0 Q; r4 X) U6 ?
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
; O' `2 m& Z" B1 u& `. bInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
$ z& H8 q, v" R- XInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
0 }. Q; S* Z) t# _1 \" SInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf1 p* A8 c0 A' ?* L
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf, Q3 i7 p2 g y9 P: H3 `; O; J
五.配置nginx7 x$ d6 Z' R( S7 B6 Q$ E5 ?; q& a' o. q
4 d$ D: E5 ^+ A9 B+ {4 q在需要启用modsecurity的主机的location下面加入下面两行即可:0 e0 O6 s4 l# `. T
3 N; Z' y" H ]) n! Q+ XModSecurityEnabled on;
0 d& S$ @' l/ ]5 i7 O, \/ Z5 yModSecurityConfig modsecurity.conf;
3 } A2 V: [5 v' b- z' P下面是两个示例配置,php虚拟主机:
- ~ B: V6 D1 g: ?# [. x9 I0 ]# j# c4 M; H2 _% P
server {- \7 R& k) J- I k0 q) s& e! o& E
listen 80;0 Y S+ O- X2 H% F! c5 r, {
server_name 52os.net www.52os.net;
; Y5 F2 {. N0 w# G
% b& F$ @ c, P1 t5 g9 }! O% t location ~ \.php$ {! W* [) B& ?" U
ModSecurityEnabled on; 9 J* A" Q D# H) Y9 W; ~
ModSecurityConfig modsecurity.conf;
) Q% E+ l# B }: e5 P' l/ u$ ~& t! s0 Z
root /web/wordpress;
/ O% B% t# F! B/ F9 G6 I9 H5 g# l4 f index index.php index.html index.htm;
3 f- w- p ^( N+ X
& D! N3 A# O/ K. P: w; i6 ~) \) c fastcgi_pass 127.0.0.1:9000;
' {! {% W/ I$ ?8 l2 t fastcgi_index index.php;
w% M) j/ C5 ]7 o8 ]8 [& b7 l fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;5 W1 D0 ^; ]2 }
include fastcgi_params;# H; h A8 X1 b' h# {0 b
}
1 [# D5 d( F: q$ C$ e; k }! Y' B; Z! d( d* H7 J
upstream负载均衡:7 c1 E0 m8 U7 j2 u3 O$ b
" k) _. L C8 [3 W8 O; Z- }1 D' N
upstream 52os.net {
2 _% w$ Y% ?! } server 192.168.1.100:8080;! a& g8 J+ P9 f. |, G
server 192.168.1.101:8080 backup;7 ?, e5 t' k6 I/ c5 d8 m
}
- V, C6 J5 v: v" j8 z3 H- I" ~3 D" [- i) g
server {
% }0 @5 [! S/ V! o9 y& o7 x1 y, Jlisten 80;
& b+ q# \) L1 ~0 A/ Xserver_name 52os.net www.52os.net;
2 }6 m3 S( C2 n" w$ S3 o4 c2 r6 g4 `- Y) a2 k
location / {0 [) P W& \- n: Y* G
ModSecurityEnabled on;
$ T3 l$ a2 i0 a0 }& j ModSecurityConfig modsecurity.conf;
& F- x4 a% H1 O" z. Z" o; s$ c' l1 y% A8 j5 ^/ n
proxy_pass http://online;7 r# H: Q+ g2 K7 v2 ~
proxy_redirect off;
$ w* y( c' c: E: X proxy_set_header Host $host;
+ T8 ~8 B: p* X0 y) I! L proxy_set_header X-Real-IP $remote_addr;
- j/ f( h/ P% u" a% g proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- D3 u0 V2 F; X" ^9 D }! Q& f! x6 ~8 ?( x! x9 N# l
}/ z, e6 l3 |$ Q6 ?, T
六.测试
, ^" t4 q/ y, K+ J/ J
8 r4 a/ U9 P2 n我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:. S, b( g4 C0 u8 s, E0 P( C
6 N- N" T7 | U7 p<?php" H$ h; T2 _0 s; i$ ~% b
phpinfo(); 3 a% B+ r0 E1 j d6 Y
?>
% E. N) X! I( R1 b: w8 ^* J) ]在浏览器中访问:. E4 V0 v1 S. p v$ P; q
6 u( Z H( a% l4 {& j1 ]/ ihttp://www.52os.net/phpinfo.php?id=1 正常显示。
- P- s; S* @" ]' z! Ahttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。. R6 @* q1 n( O3 D
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
# F* \1 w% q, p9 \说明sql注入和xss已经被过滤了
* X0 i; r8 M+ I6 O. `$ `7 w# I, S5 A3 }5 ?9 R- u% ~. p3 N/ L
七、安装过程中排错' `7 Y% s$ ~) M5 ?/ P( X+ \
- \; g1 u& D/ d! W& h: s1 H! j1.缺少APXS会报错, b9 i' m' |. j/ V) M( J6 r4 V
/ K( ? ^ l: j& J: O; Bconfigure: looking for Apache module support via DSO through APXS& @& Z$ C; }7 t; c o
configure: error: couldn't find APXS
, S8 n3 `7 y5 q+ @+ z6 j& rapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。9 R M& ?; `& _! Y* B
解决方法:' V2 b, |+ c" C/ g3 y4 U* a
0 Z3 d5 S$ R3 i J( _9 p' D1 a
yum install httpd-devel d Q( H& K5 K1 A, p: S
2.没有pcre
S" e1 Q9 z8 Q7 } [! S& V2 B" t, x( O& i* U8 Q
configure: *** pcre library not found." C5 u8 s# L% k& j
configure: error: pcre library is required
+ t9 ?8 ^. \% a! z! w' A9 ]2 A! g解决方法:
8 r% ~$ u- ?# X2 ]
( y" }8 X: C/ q6 M/ W* f" O0 Tyum install pcre pcre-devel
0 q+ n2 q, f% e* a, l3.没有libxml2
& X2 Q( W) A8 B# G: J" b* H4 Y
/ m1 [% x0 H! i8 v( q1 L' b/ c; A1 _% g1 y A
configure: *** xml library not found.* b& {. Y; [3 H, w- R
configure: error: libxml2 is required
7 M" q( u' g! J5 N: I* S# U解决方法:1 r& I' x/ U7 Y! a8 R
& G$ u- ~0 G8 a' Wyum install libxml2 libxml2-devel
& d. _1 A) Y( T, ]3 E9 O. ?4.执行 /opt/tengine/sbin/nginx -m 时有警告$ Q _* S. ?1 u4 @5 A& r* K
7 w2 B0 q! I6 ?- N" G
Tengine version: Tengine/2.1.0 (nginx/1.6.2)3 z; o9 U) l4 F" m. L7 Z% O
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
' H4 s/ t0 v7 p% X/ B G原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log- _. g1 r7 x& a9 j
0 l7 E* V& d* K; `
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.4 L! n% F) U, R6 ~& N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
! _6 N. ~% U" m) S0 D4 p2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
2 S \9 ?; F$ M7 H2 t/ j* E8 h2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"5 W6 q+ G9 l0 |6 W. R; n: P
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
" b A; G! {( ]8 s2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
% w% W$ j& o, P4 i; x解决方法,移除低版本的APR (1.3.9)
' L8 T. o4 L6 U9 {/ ]2 t2 T1 K9 @6 G- T3 n2 |/ H+ N# d
yum remove apr
. g; ]8 r; u3 l. x( q5.Error.log中有: Audit log: Failed to lock global mutex
: G& c9 [3 R0 E3 f) O) |: w+ o4 v" w% m, V% r) ?
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
* w: x' D1 Q d9 ]: f6 Eglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
; e: m7 q7 F. w5 N解决方法:
. u% Z3 X9 I' g" A7 y编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
8 z* F- }4 a0 y& L
% y7 C9 C- Y5 ^& Q4 d; oSecAuditLogDirMode 0777& ?3 q" ~. [% j9 e
SecAuditLogFileMode 0550
' N; C/ F& t$ n0 u0 QSecAuditLogStorageDir /var/log/modsecurity
# p$ v N# D+ R. xSecAuditLogType Concurrent& ?: ?. ]7 J6 I) W9 |' p
参考文章:
7 _. A3 M; V- j2 `9 F% ^) \& p8 U4 L2 Lhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX+ p ]; t3 Y r8 c P( ?
http://drops.wooyun.org/tips/2614 |
|