|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。# e0 r0 S3 R( B4 Y" ?: I7 V7 H- r
$ r* W% Q$ W1 o+ l% V3 p
一.准备工作
% ~$ {; s9 L8 B3 D. b0 w( ^8 v& w$ l9 @* O5 }! V6 V% [
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
% j& s; w5 h( B3 O5 V3 r7 k% k( v' {* D0 d/ h6 |* _, V/ W
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
( q' w/ X$ x7 t0 Q% M. Q/ }8 ~
( e4 p+ ]% e" |2 a$ R1 Amodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz5 d+ v5 s. p* H
/ i3 [/ N% R: g2 I W. l
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
7 @1 o: N9 E3 b% T1 j1 D; J/ \7 \6 t4 k% y* T* T: @
依赖关系:1 e7 D1 Y! ^0 N# r" M( d+ l
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
9 b6 C1 _. X& R6 N2 M: q! I% M
: U, x( W+ `" V9 ~yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel+ V8 q3 }4 N# f& U6 h( h
modsecurty依赖的包:pcre httpd-devel libxml2 apr2 `% n Z2 ~) ~( p" }9 U: X/ N
* }1 o; }; e. V: w, V. d+ O( L/ Q
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
7 |8 J/ @. ?( R7 N: K1 I( R9 J( ?二.启用standalone模块并编译
1 s9 k: M R/ E
1 \: s* Y6 K- U2 w下载modsecurity for nginx 解压,进入解压后目录执行:# P2 p; {! @0 T+ e. K
- [0 k) }+ B- [' @) S! P& w
./autogen.sh6 x+ u3 D( k* s- r- T6 a, f# B0 _
./configure --enable-standalone-module --disable-mlogc
8 A7 Y' d) k! k s9 Imake 5 b: Z2 r5 m% q' n* l
三.nginx添加modsecurity模块% x5 B: [$ T9 @% l8 ~3 C! `
3 \/ v: T; s% Z
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
4 D/ J- \& U4 |" s% f+ w6 C, G5 e& {% f# ?, J3 Y
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
" t6 O9 ?, x" Z3 h: m6 _* _. Gmake && make install* Y1 `: I+ z& j/ D% n% N+ C
四.添加规则! }& s4 Y3 i0 B k1 {. i
# ~6 T- V+ B" F6 X; N* {
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。5 X% [) F4 K* @7 e3 ~2 D' y
: r0 C. |3 z9 z! Q( ~- O! n
1.下载OWASP规则:2 Z9 T1 G, p. ]' A- u
9 `* j5 ^) E ]3 W
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs% |' V3 ~& g( l* m1 z1 ~
$ ?/ B# A) F; {8 c
mv owasp-modsecurity-crs /opt/tengine/conf/
6 g E; d. C' w* ]9 J, v
) k K: N! f, H% _1 p( vcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
9 ~" A$ ?* Z2 V& T2 c& @* C0 M" e+ f, n2.启用OWASP规则:' P0 N! v% H+ M! h: g
' z) j" Z" s0 M) K# L4 n+ o1 j复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& n) ^# T( c3 P, U: s
2 G" E# H/ S! w- A% z编辑modsecurity.conf 文件,将SecRuleEngine设置为 on4 E9 r; n( P: X9 \
& V, X, u8 B6 [& ] @owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。2 L! l0 l; H1 Q" L; p" t
5 L# l9 j* R$ j7 n3 e5 p3 Z# FInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
I: J+ ]) b" a7 IInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf3 D+ k8 ]3 @2 ~4 S8 O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf/ r/ h! C- L! T% ~
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf' v2 W: _4 K1 m. d
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf* i, K D7 P4 v+ u
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf1 d8 e$ f$ C8 a4 c5 c' g
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf' g! I7 Y+ R- {) V! X
五.配置nginx
( J8 I5 K3 P9 J6 x
3 _: ?. A! T, g7 I在需要启用modsecurity的主机的location下面加入下面两行即可:+ _) x- f k4 ]0 H$ m; e! b9 y
' W7 m- D. r; F' c
ModSecurityEnabled on;
* R5 R) f3 J UModSecurityConfig modsecurity.conf;' ^) o# m$ g! z) {5 |, s* s& h
下面是两个示例配置,php虚拟主机:2 {* l( ~# R8 S2 O! w
- \+ j+ L4 d xserver {
7 P( D; @/ [2 G, q listen 80;
) V& e7 ] H) B+ j* E9 ^& O4 J server_name 52os.net www.52os.net;4 {" n# d/ g$ {% c5 Y
' [2 l. X# w4 Z. S8 |+ R) C* B
location ~ \.php$ {# r- ]; T) M- f' }0 L- g! i
ModSecurityEnabled on;
+ F$ W/ J C( J) v0 w1 U ModSecurityConfig modsecurity.conf;; i, W$ ~$ k$ S" @: Q
, q) g2 J& d( H0 f T# H+ B! c' O
root /web/wordpress;& O1 [& p8 G9 _* X. ]
index index.php index.html index.htm;
$ K$ o2 |7 Z, ]# h5 ~- a' N) B
: H9 i8 v9 m: T5 a: _5 Z fastcgi_pass 127.0.0.1:9000;
/ t& w- ^. f+ A2 W5 m- h( i fastcgi_index index.php;! y: c+ `1 s2 l6 p
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name; ?8 H. G2 U: F9 w1 c! Y
include fastcgi_params;* f) W- E, S' L6 \6 a
}
; {9 i0 J* e2 u+ m5 ?$ r }
# _8 T: n; T: a8 l3 @% s" e, Kupstream负载均衡:! S; D+ b$ ?* z6 F. Y
7 u$ I4 b( j- V( ~( H3 O* H
upstream 52os.net {
1 O3 M( W$ H/ T8 v) x server 192.168.1.100:8080;
/ {, T0 ~# N5 g. l# k. ? server 192.168.1.101:8080 backup;9 w2 J3 B2 l% z
}5 N- R8 i0 r4 N" V* V
6 [, Y1 U4 j9 y3 `2 w o& Pserver {* ]1 c j/ {9 I, C
listen 80;/ }6 p2 i0 o4 U( m. ~* ^/ C
server_name 52os.net www.52os.net;
2 A5 A# I7 A, f6 X- z( i" g7 X# g" S; L- D( w
location / {
6 G% x! w+ X" D; u4 w4 y) s ModSecurityEnabled on;
( e' f G7 n# f, V( {2 o, z% d ModSecurityConfig modsecurity.conf;
k- v" y5 ~ G" j2 @2 q
4 P; k& @5 h3 A- {: F! S proxy_pass http://online;
! h% o/ k" I5 I9 R6 h, m: m proxy_redirect off;
4 z( a1 M0 `4 n$ R6 M$ R+ \1 T proxy_set_header Host $host;5 q$ {1 c4 P5 `, L, Z
proxy_set_header X-Real-IP $remote_addr;
/ M/ d" M# |' s8 F' d proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;0 Z) C6 s/ G5 t: F. Y* }
}7 G/ j m+ g+ K1 X2 o% P
}- f$ A& i3 W# d1 {; P6 |
六.测试2 |* n, W$ v9 Q: G; n
) V- `9 S- B4 o' G我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:; f" I& G+ f3 @1 b
5 X& L- s# U; F7 Q( p6 Y0 e
<?php
7 M% l. O' h0 g5 [) a+ { phpinfo(); 5 n. V# I1 C- K9 S4 Y8 i
?>) B+ b5 S/ P8 w+ E
在浏览器中访问:
% l) U8 r% C. Z* V6 o' @7 T% e' W) L
http://www.52os.net/phpinfo.php?id=1 正常显示。1 T: Z- _: A; G- w6 T6 a
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。6 d" p' I5 J" i' U9 W1 E
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
4 x" Z: L& \& i) _3 Q. `6 Q说明sql注入和xss已经被过滤了
9 D5 Z) H1 g4 ~" [7 D
( @! w/ @* O( W* W七、安装过程中排错
) ]7 q: f1 n, X2 N" J* @8 H4 g8 Z* g0 u' Y( x+ y" r& z" b* s
1.缺少APXS会报错7 T8 U. _9 F3 c$ K5 q! Y) ^
; v+ i$ L4 V! d# K/ |
configure: looking for Apache module support via DSO through APXS
9 g$ S1 i: H3 Z) _configure: error: couldn't find APXS [' `, s) E. }: ?* u
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。 u {' Y! q& P, b( y' ~" G9 v
解决方法:
7 [0 k. `- X S6 R+ O
/ d% Y$ I$ I3 ^( I/ K9 W1 Qyum install httpd-devel
* {" `( c! ]; _0 } n8 M+ m2.没有pcre
3 ]4 |9 Y, J% c" Q( [/ l$ q7 T- H
configure: *** pcre library not found.
8 V, E* j T4 N5 l2 v Tconfigure: error: pcre library is required
: r( Y6 X$ [" P3 ?9 I3 W. t解决方法:' q3 U6 p! Q5 \
, m: |$ P3 l% n! e; d9 ^yum install pcre pcre-devel
( |& ~/ C2 e/ n& @' f) B1 q" t3.没有libxml2
( ]4 p- }. s n: ^2 @6 |
! ~1 y3 g" ]6 R
: T. v/ k6 S( s- Q7 R$ f$ m+ }' dconfigure: *** xml library not found.. x- D9 @! h6 H0 j
configure: error: libxml2 is required
/ J7 s" |" ]' V# I; v解决方法:- U7 a4 f8 O; k$ g- R
$ x, V! W/ j2 j8 n. d
yum install libxml2 libxml2-devel
S) k& p( n0 J6 S' p9 A6 J" X4.执行 /opt/tengine/sbin/nginx -m 时有警告, t2 U1 }# ^ F1 w# g0 c
. v1 O$ \; C( }- e6 y w2 f5 Z
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
5 s f) E( Q5 nnginx: [warn] ModSecurity: Loaded APR do not match with compiled!% v) X1 Z# K1 G& |4 W8 ^. {5 y G! @
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log1 G+ K4 ~; w: `7 s; a
# o0 q+ N9 K$ E# Q% [
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
+ R E" H A, U2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
) P( h* ]+ f( m# O2 ^9 q) ?2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!5 W& `$ R7 A% f+ o1 N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"& Z! P m2 O+ r Z0 {: F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
1 P; O; x3 x* [2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
4 }. K, M( r. C1 t, r! z$ w/ L解决方法,移除低版本的APR (1.3.9)
8 @! y0 b( o/ H. }8 U- v; h
4 K* {' W, d( V( V# G* P" G+ jyum remove apr
, m4 G( R7 D! O/ x7 n- ]3 g5.Error.log中有: Audit log: Failed to lock global mutex9 ?5 V' k0 ]- F9 X5 R5 n5 Q
; a) B) U: I$ C0 G( ]* G6 n* l0 q8 j
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ; P6 H9 i. B0 x# P! k
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
; @2 A# K8 v' W7 o解决方法:% J/ y, n) s! t/ s
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:+ j! w" N4 _. h- h9 B) U7 y
# N( R3 ]$ L2 U( Y! c- h
SecAuditLogDirMode 0777/ l/ R2 j( P' d; P$ \* r
SecAuditLogFileMode 0550
3 ^" b; u! k5 |! v. ^/ X/ h. ISecAuditLogStorageDir /var/log/modsecurity
' K8 t/ [! ? s8 w3 @' {SecAuditLogType Concurrent$ p: j5 b! | m1 F6 }# L5 a' h
参考文章:
# n$ |# \6 W4 u1 [' e0 ghttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX9 n6 t& Y/ S8 q$ v3 l
http://drops.wooyun.org/tips/2614 |
|