|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
, g! Q, G6 ?4 n) j4 g- A$ u
2 U' q2 ~" z7 W+ K& A一.准备工作+ G% ~2 M |- ?! B
& @$ N- ]/ O3 [# B" K
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0) n3 s- Y4 `- |$ x
9 u2 B# M+ ^! j( @* f; p9 O& Z( Ctengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
4 V2 `/ t6 o- |8 L
% u" Z. S' f# }! w. y2 E1 emodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ D& }: a; n* C3 D n3 Q. Z* U5 `; _5 n' n+ _( k
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs0 o' W/ Y5 A: e" Q
& G, T, A# s! ~2 e2 L0 g
依赖关系:
@( ?3 E: Z4 w( D0 L" ?tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
7 E4 L5 ~# |; `: w" l4 }7 M& n* o3 n& L ~1 l% h
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
& u( S7 ]% B k3 W. `modsecurty依赖的包:pcre httpd-devel libxml2 apr4 x% Y" [" k2 X( o7 R
# u9 C. o j5 s+ R3 M
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel# p% l4 d. ~; d9 L
二.启用standalone模块并编译* e6 F' e, N4 [5 J3 Q
6 G4 |; o! g, y
下载modsecurity for nginx 解压,进入解压后目录执行:
2 ]3 \; z9 P! u' t, h v
^5 H9 H$ O Q1 s! {2 A: R./autogen.sh
/ i# i0 v3 b5 D; ~% M./configure --enable-standalone-module --disable-mlogc/ z& N$ |7 I' p' ?
make 3 X7 L6 t4 s8 e; B, Y
三.nginx添加modsecurity模块1 T, h: x& G1 C6 \" S
" J( ~) h( T2 [; M
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
. f& B0 a0 n8 T/ I9 S4 n4 M `7 I: }4 J& p4 |
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
# g; @& b1 C) R) v( w7 P# S1 Xmake && make install; V: @; K2 A/ P
四.添加规则
- R$ w; y- \8 I
% j+ W* k1 L$ w: B8 T/ ^3 f" e: ~! ^modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。$ A9 R: H+ e k l |' w
! B) r+ r# c9 k- K5 y1.下载OWASP规则:( i; V; V0 l# T
* W, @" E, p7 D8 t5 u1 a7 A6 u# m
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs- K, G* d- W5 @8 }/ _
, k0 F! b" L T0 u) H) V8 G4 L
mv owasp-modsecurity-crs /opt/tengine/conf/+ Q1 F0 G, ?+ c0 k1 T
& V! O& E3 t! i+ Fcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf0 P! P, {: J5 E _2 ^% [
2.启用OWASP规则:
# T- N0 ~/ l8 {; `2 P0 I/ q& j
3 z% Q- b4 Z9 i: e% ]' ?! D6 l; k复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。4 _" F( x {" t* Y4 q
3 ?$ k+ F/ m3 l$ E" V- Z9 c
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on3 Z* V1 a4 ?1 F1 m
3 I+ a+ U4 q! Y! ]9 Q l
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。) R0 b; A Z- I" T: k
& g- }4 ^0 ?) x- a5 [
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
z( T; e' u7 \. BInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
/ _( v7 `% P- p' X' l0 \) L0 lInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf! D) n5 x' z% `2 e' n% L6 b
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
* B4 H2 U T- ] c% ?Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf- W& T& ?' B+ D& q9 r x
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf& q* {! Q3 Q. L' F/ s$ W/ ]
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf/ s4 \: l$ Z% X- d! a3 t4 `3 k
五.配置nginx# S9 x% h1 u# X1 s7 C: y* P4 P9 [! {
: p; o" d- W9 m0 \% V
在需要启用modsecurity的主机的location下面加入下面两行即可:" h# q. C+ r) l7 z# d1 n
# v" S5 O7 J" k+ a& @ModSecurityEnabled on;
& h" U3 F8 `$ W# V6 c2 ~; GModSecurityConfig modsecurity.conf;7 T$ s7 V& |- w0 P+ j: y
下面是两个示例配置,php虚拟主机:
3 ^) N! a$ ?$ ~8 n
7 L5 Q2 h6 W% P" ]* H( gserver {
' s2 l# L9 Y% Y5 h* d4 V listen 80;
# H8 @/ C$ e- T c9 |0 |+ T" o$ ] server_name 52os.net www.52os.net;% X. F, ~6 K( f& U& O
+ a& o3 ~" d( ~; v/ O- {
location ~ \.php$ {
+ Q) |% Q( A2 Y; T1 h ModSecurityEnabled on; ( N4 e J" V' C) U! x$ W: d
ModSecurityConfig modsecurity.conf;8 j' `, L R# j7 T2 V
9 B; @8 ~0 }7 ~& n# A- O
root /web/wordpress;6 `8 r9 o* J) d- y& ^" g/ I- V
index index.php index.html index.htm;* m: @" E9 }# w: B- ]
9 `: T+ b- Z4 X" [- I fastcgi_pass 127.0.0.1:9000;
8 {! b1 w z. u- I fastcgi_index index.php;
. {8 S- U# U9 ~0 A8 { ^ fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
' a9 ^4 n. F8 H: H* {$ f; p- h% K8 J include fastcgi_params;4 ~+ }6 _9 n# |7 M" ] [3 l d
}
* ], Y8 T) I8 y/ [4 ^ }- R% v8 e1 c% {
upstream负载均衡:6 Z0 c* ~" {1 ~: v; |
! t6 ^4 x( ^# P: l3 Uupstream 52os.net {
/ t% u, t: ^1 {9 L# M! i5 V% ? server 192.168.1.100:8080;: a1 M9 P( _& ?
server 192.168.1.101:8080 backup; R* a* w0 E' m" o# Z) U5 L
}+ R* ~$ ~0 @1 ~4 r, q7 I" r+ M
$ t" M; ?' E8 w1 \$ }& z
server {
( w9 H; d0 V. F2 Y7 |listen 80;
! f; a( M% p9 O2 R q; iserver_name 52os.net www.52os.net;% Z& O8 C, {* q% B) E9 V
: u" W' o# }' F8 o2 `, `
location / {
3 F" w. U/ Z& K; r# ~ ModSecurityEnabled on;
# |" x7 X* j% @" s! M n( f ModSecurityConfig modsecurity.conf;
- h. v5 t6 h; a8 }
3 e4 S4 {: e( g' J proxy_pass http://online;: i5 |' J4 E7 k2 j
proxy_redirect off;& e w8 s1 S2 V7 `: E
proxy_set_header Host $host;( u' Y- ]. t1 _$ I# M
proxy_set_header X-Real-IP $remote_addr;
) }( S( u9 B q proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
" S8 f! [5 t0 s* m) p* V }
, N! K8 x/ n: X8 e: s}
5 f. I r" B/ f: M5 E$ C @7 z* X六.测试: X% Z+ p/ A: r2 c0 f
) O! @6 F1 c0 T/ D( w6 H% n我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
% B0 Z6 @1 @' {0 H+ r$ N; F, u9 t$ T6 H
<?php% m# N) L6 a# J
phpinfo(); ' `7 W. G2 ]" v2 h. {0 K) d, \
?>
5 Q0 ?+ z2 s0 h+ Q; E& \' V在浏览器中访问:& q- ?! t" \9 ?2 q; }( f1 b0 \
2 I9 l/ A4 ]8 `& f- t/ Vhttp://www.52os.net/phpinfo.php?id=1 正常显示。6 u# Q, E: S; K# @
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
! I' u3 f; E& Rhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。' K5 `( W! m3 k0 q! N* [0 e5 t
说明sql注入和xss已经被过滤了0 O7 I6 F: u$ f$ {$ `+ Q7 e
+ n! h# d6 W( t/ d" i2 ]5 E
七、安装过程中排错* S) D2 H7 j, Q/ w% D5 s
; G! i6 k/ J" M8 X- f% @+ B
1.缺少APXS会报错
( Q5 `* d+ j9 s
1 {/ Q. s9 S+ S: c Cconfigure: looking for Apache module support via DSO through APXS
% K$ a; p1 u2 w; A( Oconfigure: error: couldn't find APXS5 h" a# L& d8 ~3 X3 @8 W5 m1 P1 x
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
$ k5 N, K. M x7 \ c+ f解决方法:: V/ `% V( G7 b
7 K" o- `8 f0 W
yum install httpd-devel
9 Y- w& ] e9 z9 n- W) }2.没有pcre3 z% f$ S m) {* c9 `9 e& y
1 W c9 p( s' {; Q
configure: *** pcre library not found.
+ t+ L/ ?; U- A& d: m( A, a5 gconfigure: error: pcre library is required
3 @& R# @3 x H! `解决方法:1 s! n* q( B9 \# F6 n, B' O
; }5 h+ r% e4 A8 T! K W$ y7 xyum install pcre pcre-devel# d9 Z$ O6 f% X _+ I% c8 M
3.没有libxml2( u3 P2 c8 w6 {1 j: `. L: \8 t# Z( F
/ X7 p% [+ q r$ E& W0 g. a2 P( }4 U
7 f1 c% ^$ D1 o2 d' D8 _2 s7 i/ Cconfigure: *** xml library not found.0 ~. s' e5 e) s
configure: error: libxml2 is required& k. X d1 [; z& z
解决方法:
9 S5 O. H5 e6 ?4 Y4 F1 l. ]( Q! }; F U) @/ b3 C
yum install libxml2 libxml2-devel
! E3 I1 a' _7 A* M4.执行 /opt/tengine/sbin/nginx -m 时有警告) x) a: z) d; c2 G* m0 e
: R; J; |% c- S% r! ?) S, aTengine version: Tengine/2.1.0 (nginx/1.6.2)
+ ?9 n, N; c* n2 T6 a% Ynginx: [warn] ModSecurity: Loaded APR do not match with compiled!
1 Z/ x" F) w+ S2 v1 e- t原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log* Z) N: y1 f! @3 k( q
$ m1 h6 A5 P" P2 J2 t A) T1 U2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.; ~( Z! P, l& A' g* D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
, y; K, V/ L4 h) a; o2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!( _4 `1 `+ G5 _" _; V6 D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"! y" U/ W! n7 @" j) N4 o* B
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"/ Q3 `$ N1 n7 Y5 Q7 x: q% X K
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.7 \3 V2 r1 E4 E c2 M9 O ]/ J! K
解决方法,移除低版本的APR (1.3.9)
& w) @/ A( J7 |" J) c
: r$ t0 }+ g0 u+ T1 S/ J. e7 ^yum remove apr
0 z' Y1 r2 j* \( ]* h% A) M5.Error.log中有: Audit log: Failed to lock global mutex' `4 {, n0 n3 L( c! C9 `2 F' X8 o% C
& \4 i+ _" c0 r& J
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
. v. V3 f9 V, yglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
# Z) M6 Z R0 U# m: k8 e解决方法:5 I2 d1 m6 h: e4 w
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:' \/ l/ M8 n6 I* |5 s# [, h
, ?% k3 @! c" J3 K W# y! q
SecAuditLogDirMode 0777
( v0 X) ]( p% |SecAuditLogFileMode 0550* ?. z* c# W. Q' M% M
SecAuditLogStorageDir /var/log/modsecurity1 ~( Z1 @5 x& g F; A
SecAuditLogType Concurrent
& F1 F9 G( ^# {" c: O9 c) o' q# i参考文章:
+ \& l+ o t8 f, L9 dhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
( }) x" J' V5 j; T7 @5 Chttp://drops.wooyun.org/tips/2614 |
|