|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。5 g9 j$ Z+ ^5 A
, V5 M p/ C o C
一.准备工作
% u; U! i$ M4 L( o9 f
! `8 |7 o& M% Y4 t) i) Z! p系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0) a; q! ?3 C/ Y: M$ E4 ^) H0 B
: u. \0 K! ^6 C9 Btengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz% s5 B' o* k( G! f/ N6 Q8 ]( X! |6 K
" O" E8 @& A$ @+ @- cmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
) Z' f$ ?# a/ r) d( z
- b2 I R) x# }) o* UOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
7 @% [5 T' S: k5 F; M& |' }0 Y# R$ z7 |# Y" b0 d# M
依赖关系:
% t+ _4 a2 w T* P* e! {tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
j7 B. o O6 N6 h
0 l9 `& o' }0 F% E) kyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel! j# |' ^% p4 |% c
modsecurty依赖的包:pcre httpd-devel libxml2 apr
' @& K# q+ L0 N0 @$ A# R3 z& Y* Q' i! B: ~# K4 u) O3 p
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
$ O }2 L. P1 ~- |' W二.启用standalone模块并编译0 z; o8 l, B! h% i. p% F V# H; z' S
, o4 ~( R3 G* ^ I
下载modsecurity for nginx 解压,进入解压后目录执行:* k' X; i; [) Z- ~6 i' e. ]$ ]
* H4 y9 d5 `* G! j0 n8 P* U, [
./autogen.sh
' z) E R8 b7 l; N0 V./configure --enable-standalone-module --disable-mlogc
. I$ i2 f/ z8 Q& u# K. imake
4 o) F0 z3 |' s a# G) ^2 k8 i三.nginx添加modsecurity模块 l8 M! D6 o# E* v% T7 z
/ L$ t7 B7 d- S: I4 ^; {' K
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:8 i: ^/ [& [- X
3 ^8 M( c. P4 W2 J5 S6 [7 r./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine2 ]- r$ F: Y. {& h) B2 F4 G) Z
make && make install( v1 x1 M D+ N$ P8 i& r
四.添加规则
# R# c, l: s/ z. `0 s& o) K; ^5 S: D
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
^9 l ~. n" l' P9 \% _! [* z
) c" P7 t0 I- q- f1.下载OWASP规则:
6 \3 v4 u8 a, n( o0 M7 ^
2 ^) L0 [5 _0 _5 @/ t8 {$ X3 J4 w8 F9 j, ?git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
/ ], F/ w6 j) K/ {) R$ G( J/ p7 w( p' M" ]3 r
mv owasp-modsecurity-crs /opt/tengine/conf/( \/ w2 q& O* V4 P: Z- M
% H- O$ n" V6 X' F, wcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf( O9 g# }+ \5 p6 e( F
2.启用OWASP规则:" ^' ^; s L# N. E
4 K+ T% [# m' i; Z$ M& |+ @复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。7 V$ |& k! ?* [
; Z" B- U6 i& b
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on1 ~0 e& S. \9 M% C* S" p
) @8 D; d: B& ~owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
6 V: S0 t6 w* l( ^+ C
6 ^ Z- b% |) t1 |Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf% r. s1 }/ f' P0 N# h6 H( c
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf! g1 M& @$ V+ q4 i% U7 ^& n+ _8 C# p; V
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
% I: N" ?: H" f+ c4 X& M% w1 QInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
7 }8 i I L1 s' A5 j( O2 {Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
/ V' D5 _. ]- H' U! C1 a, x8 H( n. KInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf$ O; y9 j+ }; s
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf2 s$ q; t" d5 y0 j# |
五.配置nginx
6 O) x# Q8 r* o, M! Y0 q' v2 Y$ f& E9 ?' P. }* \: B- X; ]7 ?
在需要启用modsecurity的主机的location下面加入下面两行即可:! `! w' U( B! b" a# _
$ M( k6 U; N& u+ O ]
ModSecurityEnabled on; 1 z3 a, ?/ s/ j* [% R
ModSecurityConfig modsecurity.conf;
, _# L, A; e/ j- P( |下面是两个示例配置,php虚拟主机:
. n& q) x4 X- m$ x; r" r
$ L7 O& ^' e8 m3 N8 pserver {
, n2 j* J. N9 p3 } listen 80;
6 \( S0 v. A- j server_name 52os.net www.52os.net; i1 I) h; S( Z8 ]: ~2 p2 O
7 ^3 d7 r! ~* w* z, v' e4 _2 x
location ~ \.php$ {
5 o2 R1 N$ r Q9 ]9 m. Q0 t ModSecurityEnabled on;
1 v1 H4 c8 |9 u; i8 D0 s ModSecurityConfig modsecurity.conf;
4 i/ g- n: u6 d. ~& d6 {# [' H \, T# c
root /web/wordpress;
: Y$ R0 B2 |1 N" T% e6 ` index index.php index.html index.htm;' }) K7 S) D. ?5 n0 ~% i* l
3 r' T, Z1 g! o2 R+ ^" m; ] fastcgi_pass 127.0.0.1:9000;# W4 o8 n( v& W+ Z
fastcgi_index index.php;
E: |! h: g! {2 Y$ | fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
) b6 V; l% I% n8 d% M5 z& a L include fastcgi_params;
+ d, z1 B" _9 X3 Z* m }3 ]$ J9 P" A7 L9 N& a
}8 p' J8 |! s* U8 T& W+ i
upstream负载均衡:- u+ J$ I F/ W; x: q. w8 p
' @( u. s p) |6 o! |! k; oupstream 52os.net {: b2 C0 b, M* s* M
server 192.168.1.100:8080;
: r5 V7 |4 A8 H0 n4 S server 192.168.1.101:8080 backup;
9 ?! v! \, R K* ]! Q+ h5 z}- r4 O: B9 l$ K R1 F
9 ^( e6 A4 V& a8 y! C
server {
7 e- _9 T8 }* P- P) [listen 80;2 G6 z( E# s, r! Q# ?2 C. {
server_name 52os.net www.52os.net;
: ]7 v7 t3 p4 u! [3 S& X5 z* H; M4 b$ O
location / {
: M& _! x; N8 f' | ModSecurityEnabled on;
9 y5 G8 C! Q8 e, K" E' r* l; e ModSecurityConfig modsecurity.conf; 6 K: w3 s4 X- m! i8 |* e
- Z2 S! u: H" \% T: v" _ proxy_pass http://online;5 m2 D: _1 V! r, q2 f, d
proxy_redirect off;) L# {; j6 t7 S2 ?1 q
proxy_set_header Host $host;6 B; ? i7 j5 h) W
proxy_set_header X-Real-IP $remote_addr;; |+ c% q% J' ~
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;: c0 ?. C4 Y; M# L# \1 W4 J7 U. _
}6 b& f, `! k3 T" W: ~
}& B; ~. t1 `2 ~+ ]- V
六.测试5 N! W* U- I8 j% s- X
/ X3 o; N4 f: M c) \0 c) @" z我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:; K Z: C/ W% l
8 U! B1 z% ^1 i- K3 i' u8 F<?php' X+ Y& N0 X$ J9 a3 T
phpinfo(); & W" Y1 Q( l7 r3 g# |4 }+ [
?>1 u3 Q# I" i7 N7 V
在浏览器中访问:
: u1 G$ n" }& z2 R. k) A! N3 F* a$ J4 O' I4 }: f, \; }
http://www.52os.net/phpinfo.php?id=1 正常显示。
! U' u5 P5 C# e9 I- Mhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
* s! W& a& @9 c5 ]# g, j7 chttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。% o( q8 z5 A/ O% v* ]! h& T- w
说明sql注入和xss已经被过滤了. F) K+ t& i8 G) g: h3 t* B3 a
3 E, n- A$ H* W6 [% ~
七、安装过程中排错
4 e) W& [& {" _6 ^9 x* H$ Z- C5 ~. M1 C7 c- R; C
1.缺少APXS会报错5 b2 f+ a) w+ @7 S, g3 x2 G6 \
+ p5 ?! ~ M8 K; {# e; d' m
configure: looking for Apache module support via DSO through APXS
/ V, Y/ ]9 x, T1 z( G! n0 qconfigure: error: couldn't find APXS* E' r# t6 z2 T
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。- \9 c6 a0 I: [: Y* \
解决方法:
! d2 q1 E) f# o6 O: u9 F% `' P: ?" R9 G
yum install httpd-devel; c" z% w* n' p# ~! F
2.没有pcre
4 ?! Q R; g% \& j; i8 O' \" y, l: Q: c
configure: *** pcre library not found.' m; Z: Z T/ y0 [6 H
configure: error: pcre library is required
, C' E8 l3 `* \3 ^8 }5 `解决方法:
! M- Y* P( Y+ i' f; }1 Y
( f# U) f9 t+ J9 M# ?yum install pcre pcre-devel
8 L+ D: ?; t8 g. U" n2 ~. X0 e3.没有libxml28 Z' m6 b* i8 a- b" `6 u
V3 y8 ]; H" {& |/ g
: `. G: F, V: Aconfigure: *** xml library not found.+ ]# {' S& I6 `7 ~; G! s3 }
configure: error: libxml2 is required
, L) k, \! g; n7 O, F2 H解决方法:
! z- C7 g) O* W
, F) d9 h( z" cyum install libxml2 libxml2-devel
- h$ t. c7 J5 e& q4.执行 /opt/tengine/sbin/nginx -m 时有警告
4 Q9 n6 t8 Z4 W! T7 a d- H N3 G5 ]- k/ g
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
, D+ n) N9 a2 B7 U% Bnginx: [warn] ModSecurity: Loaded APR do not match with compiled!, \: o7 b6 I ]5 _9 p% V# O
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
, c- [8 ^$ W' y+ K1 y- Z4 a) q1 E) Y: l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.) H/ X: F, e# Z! v: d
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
- I2 |% ?1 Z7 d2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
" F+ N ? g4 K) Y$ I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
$ r2 J" E4 x- ^9 ^4 m, N2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
( {1 ^. Y5 P8 O7 H3 r) G& ]+ A2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
P/ P. A* B# l, a解决方法,移除低版本的APR (1.3.9)
, w% U- ~/ C- T8 U0 I$ V& _- @, k9 k1 J* [
yum remove apr) m6 S. @* c- b
5.Error.log中有: Audit log: Failed to lock global mutex# W" Y1 K6 C6 c3 |
! I# D* O. d5 A4 v- V- t. s2 Z
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ( f5 f- ?- ^/ \0 B. Q- Z
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]' F2 N2 e" J8 q* _5 c% t% c0 {0 M! O W0 V
解决方法:
% h( d+ T. ]& D0 P! H# P编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:' V3 a$ E2 {& C
1 j5 s4 t0 x: \1 }; n4 L
SecAuditLogDirMode 0777
+ E7 o5 }5 q) @4 i* }+ d5 U( F( QSecAuditLogFileMode 05505 G5 `( @3 ~0 G8 ^) W
SecAuditLogStorageDir /var/log/modsecurity! X" O: q4 A+ a, ? u" a
SecAuditLogType Concurrent7 b/ j) U: v# e Q0 T' E; K9 Z9 I' a
参考文章:
" s2 l6 S2 V M- Z4 _https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
7 R) e3 R1 k) z% |& f& bhttp://drops.wooyun.org/tips/2614 |
|