|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。3 Y8 A5 e6 O e$ \
, I. ]% B- J4 h" X; z" w0 L一.准备工作
5 L+ `; K( ^ K' N0 L
" Y$ A; _* u5 t h系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
# T$ N! m+ n' p: |
5 x6 z+ w2 g, Dtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
3 E, n& e- R9 \; B \
* K" S- u6 B! M; \; |. c+ m4 E& |3 gmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz1 {6 T, x; ?$ o) M& I) [8 u# z2 X' y
1 }# `3 i8 A; @" U5 ]% u$ k2 YOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs) y" Y% A$ G; ?! w' B8 o
- Z' [3 T" h- S8 u依赖关系:# D8 G: k6 ~0 ]% H% j- [
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
" p& L, D& g. g+ E& _6 Z0 k" v
- H3 t \$ l( k( Qyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
7 g1 y( y9 { ^) umodsecurty依赖的包:pcre httpd-devel libxml2 apr1 f# N l+ r5 }! x' m, @$ {
# o; N0 C { V: y5 _
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
! R4 X% a9 a8 l8 R! b' k! F二.启用standalone模块并编译 P) R4 L0 b9 ^* Y
# ?3 z; ~' [0 q$ H X; o" E5 I( b5 y
下载modsecurity for nginx 解压,进入解压后目录执行:
$ }+ C0 x5 d5 b- M u& n6 ^
# K+ B) f9 y# q0 d3 {$ n2 N8 C./autogen.sh% o2 X3 H- i* i
./configure --enable-standalone-module --disable-mlogc& @2 x) L. ]1 {. G. I. a7 R
make 8 L( e. r( T) u/ G ^# J, @; L2 b e
三.nginx添加modsecurity模块
) e5 q+ r- x" g) |' q
$ z* ?2 I/ }# _5 |# n) K/ y在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:) C& y/ K% I* y4 A- k! J
$ f& r0 N; m3 M( h; s./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
6 {7 ?7 u5 ]! L- o7 nmake && make install
% g1 F2 U% A8 T) ?. B四.添加规则4 r. t: Y- t( v: F2 T; ~" E
7 B2 O9 Z# w) e0 Z: L0 mmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。: b/ l t/ l1 A, x& @& q, X
; U( J' n. e. ?# c
1.下载OWASP规则:
- r- W+ \! q1 n% C1 N
" ^# _0 s+ W0 V; D. Ngit clone https://github.com/SpiderLabs/owasp-modsecurity-crs1 R- M) K( |# s" f2 S
6 j, i+ c: Y1 O1 ^2 d) o" e) c2 nmv owasp-modsecurity-crs /opt/tengine/conf/
V! Y$ J8 X# U; m# b$ c0 E$ s5 M/ ~; V# e6 n
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
' ]# B; w/ `1 Z; E9 [/ L2.启用OWASP规则:
0 b1 \# H; W! _, L. Z3 g- k, D$ E
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
9 k, u! t6 O n) ~. }+ b
5 H$ ?% P# S7 T% ^( E, {9 S编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
W" z5 Q4 x" z* ^# N8 T+ B
8 V6 L& S! e7 q1 Z5 b8 Powasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
! o3 ?& z% I; q+ N" F3 g: @# I8 ~! \3 m
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf: S9 \' W8 G! j& h( U9 p" b+ L k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf3 V1 ]0 ]0 C5 q% G( }: i! A
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
& k! U* |$ a0 |7 @% \Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf# _& e! l2 t) z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
! M" z2 M% _9 R' p5 O8 }- HInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf- z8 t0 W w: C+ M' W, l
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
" v2 O. y% Y) y$ D五.配置nginx
, p3 R' B8 R( N3 K0 Y' t* T" k! {1 u3 n. X
在需要启用modsecurity的主机的location下面加入下面两行即可:; R0 l1 ]% B# E4 g. |
4 G5 _6 b4 D! K) s) w; M2 X X: PModSecurityEnabled on;
0 f; X5 H; x6 s3 n& nModSecurityConfig modsecurity.conf;
7 [3 P0 i" X* ]) N% V0 `下面是两个示例配置,php虚拟主机:
3 e6 E& n+ \: o0 I0 a5 P" Y; _1 U2 Z
server {+ q6 v5 Y- L7 Y; j
listen 80;
! b* B H: y& u7 h" E/ y3 g server_name 52os.net www.52os.net;
f5 @0 G6 {2 v. i2 T8 J/ _ & A; o9 b+ C3 L0 @4 a2 b6 s- a) I
location ~ \.php$ {! [1 ]8 k3 _- F0 a# C4 G$ D$ }- Y
ModSecurityEnabled on; 6 g3 X" h: Q8 n! |
ModSecurityConfig modsecurity.conf;
4 K, s' P5 L' p- O$ [" \4 e0 C# r
0 k0 I1 c+ [# y( @% B8 D1 u0 t root /web/wordpress;8 N' S7 G( A, j
index index.php index.html index.htm;
8 {3 D1 [2 _2 y# K9 y7 q9 x( l; L
: |. S1 f A7 R6 f/ D fastcgi_pass 127.0.0.1:9000;% F: p0 l' m" a: {1 j+ w# e4 W
fastcgi_index index.php;# o4 r: G9 l* m% q" Y
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
& G% s/ w- `: _+ r3 E include fastcgi_params;3 ^5 n0 h* `7 o0 V- Q x& o
}
( L, \: i8 M4 T+ }6 U X2 `! R }+ a7 H9 b! ]& r5 z+ g# Y& L
upstream负载均衡:8 Y/ P; h8 L9 w6 Y" [# i: n
, |# ~- [: k& y+ p+ p5 i0 y7 f
upstream 52os.net {
" d: _* r+ y/ n! E server 192.168.1.100:8080;
6 L$ S! e+ P7 n% g+ S$ B server 192.168.1.101:8080 backup;2 \$ m3 m- O5 B3 c+ l3 @
}9 D+ W2 h* l5 |' s# @3 |6 j
' R1 v2 e! l6 |% M' o7 xserver {
+ B. g6 g) Z0 @# {listen 80;, [1 D& x/ ~9 L- U2 V/ c! l; b
server_name 52os.net www.52os.net;) U8 {1 D3 F" t
0 \4 R+ m0 u. e. `+ l
location / {9 h( Z0 C1 a9 h, G! _
ModSecurityEnabled on;
7 ]2 m8 C8 t( u: D V3 {: X ModSecurityConfig modsecurity.conf;
2 P2 `! ?, v9 K! m1 E, T) f g& q: i9 k6 U' Y- \, b% \1 N
proxy_pass http://online;
4 m4 ^& ]3 n0 Y5 s6 k/ W1 D proxy_redirect off;( E$ |! @# I* Z# ]& h
proxy_set_header Host $host;
. Y. V+ J# X& ^ proxy_set_header X-Real-IP $remote_addr;3 T( m T3 m& m, [3 }
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
2 B5 n3 V. u5 n9 X }4 L/ i9 n" u$ U$ g' |+ z
}. E0 W3 q5 |8 p9 m( G s
六.测试
* i- h; K* B7 q
: K$ U; R" s O k( L我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:0 g5 I2 L: [: P- K0 o/ p5 G
3 F" a* `+ ?0 o. L- K4 P7 b
<?php
/ S, K, H) ~! \4 e# p phpinfo(); . s# m) _ [; b6 Z$ ^
?>1 A' [5 B$ {' R+ u/ Y- q
在浏览器中访问:: y( O e0 G3 Z8 T% {- J
, V( L+ L2 c0 v* Bhttp://www.52os.net/phpinfo.php?id=1 正常显示。
6 n6 E& M3 P/ g1 ?8 f5 P/ Yhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
1 x6 g# S! M+ [* I+ Uhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。' @( s# I+ X% I5 V9 P6 A8 I+ V1 g# w
说明sql注入和xss已经被过滤了# g# n* {" w$ V3 @& D! @
, k( o7 K m: K* d) p
七、安装过程中排错) S( z3 k& q3 `% j
6 G* ^' x! `6 T/ {4 d. P! M' o: c. G1.缺少APXS会报错
4 D2 d2 p4 h, l, f3 g; W/ T/ v: I2 G. Y( b
configure: looking for Apache module support via DSO through APXS
- I6 N) m) s% G( sconfigure: error: couldn't find APXS
5 Z- A2 n/ j' p3 |: K. P; eapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
; t: U' |% {; L9 U6 f3 f解决方法:; b/ [0 c4 t$ G* O
! Z6 }! Y p0 Q. k- ~1 Cyum install httpd-devel
+ E! M/ o6 X- y* x ? o# o& E2.没有pcre
/ z5 b' r$ ?0 S% M+ |1 @+ v9 I: e3 r; ]% y7 }- ~4 T
configure: *** pcre library not found./ T5 B+ S! N G
configure: error: pcre library is required+ `6 O. C D. ?' d! O' x4 M; O
解决方法:. V$ w/ X" k" r, p; ^2 H- w
' {3 L7 t. J% j" t) a+ }yum install pcre pcre-devel7 d6 E) I* r. @1 p, R- p
3.没有libxml2' O- o# M+ l- e& d6 K* X; O; n
' [ C) p) G- Q+ N- ]/ ~
# [) h+ w' ~ r. K
configure: *** xml library not found.& Z R4 S& d( ?' X
configure: error: libxml2 is required; i6 e3 R/ n) p" Q
解决方法:
8 u( V+ u# F* ?8 x* W2 }3 h: [( ]3 W) ?7 _% m! [; a- x& ]5 e5 u
yum install libxml2 libxml2-devel/ V! Y$ V) c' M( W% \* q1 e7 W+ U' L
4.执行 /opt/tengine/sbin/nginx -m 时有警告
/ d- E5 G9 ?/ {) L% T; t) i) }) O! G9 E: A8 u6 @7 R6 K0 D
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
* F& F3 V. k. T4 @8 I3 inginx: [warn] ModSecurity: Loaded APR do not match with compiled!
1 a) o; k0 \4 B) @+ z! }原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
- g( u$ J/ p" e$ Y9 Q
6 y! A; P( Z" G9 ~2 i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
1 V! N. d( [3 F3 J# [( \! I L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
e6 c9 H# A% F" h C: g8 ?& d2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!. A `; Y- c! s0 F1 Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"+ r' b2 t0 \$ d/ r& e: s) Y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
2 }0 `( I1 _6 u% I8 }( N5 e2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
9 {( `8 ^/ j! t/ J) @3 B; C解决方法,移除低版本的APR (1.3.9)
7 ~3 @4 k! A' Y' z: I
" e& A: j0 k5 ]& s+ }yum remove apr# h) f) O. @0 m( ~
5.Error.log中有: Audit log: Failed to lock global mutex
4 D1 O8 J2 r4 g0 |4 M& z' Q" U- w+ \% J, K8 B+ s- L
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock % \$ T7 @; A2 e! J
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]( d' r3 E9 [4 ~4 T3 M
解决方法:7 e; V, L2 r# P) A3 e3 c
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
+ \$ j1 @3 _8 Y0 A
7 w! o- l& ~& }2 ~SecAuditLogDirMode 0777! j, j/ k( b4 C. j5 g
SecAuditLogFileMode 0550' B$ w; P1 \# H( i$ |2 i Z) B
SecAuditLogStorageDir /var/log/modsecurity" u1 u( G4 H0 l& {. v) ?
SecAuditLogType Concurrent
6 ]) I- R. Y* a& n2 u6 @) N5 E7 e参考文章:
' e9 P% N% I3 w* S* Q: G; ?https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
7 Y% m6 R) }+ H& z) fhttp://drops.wooyun.org/tips/2614 |
|