|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
. F. I2 F. {" U
6 e( I# T: F6 W% J- H( `" I: l一.准备工作" X. N0 F" ^5 U
8 c8 ? b; l5 K6 R+ j* {& k" m2 Z: d
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.01 T2 G' f% ?* o. H6 ^& P, h
- B2 N e" F+ c5 P
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
1 D& z* Y. s' [: U$ D \
+ E; [: I" J5 J6 s2 |modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz* T$ Y+ U% s% P7 L) J
5 z0 I6 M- n B F# O
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
2 Y t: m6 b0 S. p4 x9 ~, w8 x% |6 E% P4 Q+ j% \0 B2 Z ?" ]
依赖关系:5 v1 H5 H. b( L$ c) E0 J& P- ?
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:( `: H1 q6 b+ M n+ e8 b
- M7 o+ X0 y; r9 j" k% Y/ zyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel& ~. T' e$ p* `2 c, M0 Z
modsecurty依赖的包:pcre httpd-devel libxml2 apr2 z6 _3 P# Z# Y; B' N8 N2 h* c5 s
* i' u2 J* l, V: p* _9 h$ m' \ k3 i
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
4 q2 f5 r& Q; ? Q# x: \) I" G二.启用standalone模块并编译 \5 `& |# v1 C2 ], F4 s! G
0 F7 a5 `1 B( Z6 \下载modsecurity for nginx 解压,进入解压后目录执行: z! }. X8 F# t3 V% Y( h
* b1 F2 c3 o$ T3 g0 X./autogen.sh4 E+ r, c9 I2 }$ `; k. y
./configure --enable-standalone-module --disable-mlogc% e- u7 [0 l$ w' @8 Q
make
( F7 a) z$ h4 q5 {7 {$ d% k三.nginx添加modsecurity模块
- B& o$ {% W. ]+ a
) m) K7 f7 y6 R7 _$ Y! ~. m在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:5 r8 _/ X3 V$ @4 _, y! b% D' n
) Y5 H% `, ^- q7 R9 ^' |./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
: ~8 j$ R/ R) @make && make install" m( ~# q& x m" ] u! m- T
四.添加规则
- A% I- H/ a1 W' v
# S$ F6 R6 V$ W K/ @modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。5 y: ^/ D, x" Q9 p
* a+ ~1 B M+ ~/ ?% v9 m1.下载OWASP规则:
9 s, f6 d+ ^; N! S. @
# \9 [! ~. T4 V3 G% z: A9 rgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
- _7 c: p; x; h
4 x! ~. c; v) ]mv owasp-modsecurity-crs /opt/tengine/conf/
6 U- g m F( w# o' L- g* ]
) N+ y0 t; h5 dcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf/ S: n. Q, L5 I
2.启用OWASP规则:
1 c* o* k" v: r6 t7 \) f. @7 d2 n$ v& @8 X
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
8 i9 u7 `* H" ~* G% R6 i L8 L5 N$ t" X. V; f" V: Z
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on: E- {2 _8 R3 n8 d8 D/ Q9 T0 G5 `
* t$ Z R' U# U1 O1 J- X0 _owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。! g3 l3 _2 I" d" @9 i
: i; v# y4 ], r) |$ s# J5 q+ qInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf5 @$ c: w# w) G& ^4 \! y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
) q0 e* J- z3 D( Z& j9 PInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
/ F1 v% k5 ~5 \( k( mInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
* g2 `9 Z* l- F3 j. M+ j. V8 aInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf; }6 K7 G5 e$ X i& v k7 v8 o
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf; e4 [+ N# g0 b6 \' Z \
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
g. w0 [9 ?+ i7 B6 Y9 U五.配置nginx0 Y/ Y" ^4 m& u
: {: B. O' O) z. q. K3 Z在需要启用modsecurity的主机的location下面加入下面两行即可:! d+ u2 S. q9 I
4 V/ Z7 ~2 ]8 P* [1 M) a
ModSecurityEnabled on;
& n# e7 e: l) C. \; \ModSecurityConfig modsecurity.conf;
1 u6 {- Z+ V3 ]* ]6 k8 }, c下面是两个示例配置,php虚拟主机:7 c% I% v$ c% {# ^
1 E7 W3 D S* W( x, o- lserver {
! h+ _* }- }$ D$ X6 j listen 80;
3 o" D" g$ A+ @) o8 B. n server_name 52os.net www.52os.net;0 Y, M5 x e3 L2 U' f
( ~7 N# O. p7 W7 t& i* G location ~ \.php$ {9 V) u' C+ S Z4 X8 ?
ModSecurityEnabled on; 2 t0 L0 ~3 x# U1 p2 b. O/ @
ModSecurityConfig modsecurity.conf;
7 `3 y P% w7 S& e, O3 f% q+ j! p4 F$ u+ j" H4 C
root /web/wordpress;/ p: c! T( H3 {. [, K; S
index index.php index.html index.htm;& Z( l A5 E! ~) S; s
0 e4 y- |. O' E0 D# E/ A: ]: H
fastcgi_pass 127.0.0.1:9000;! i$ H3 t, n' t! o7 h1 _0 V
fastcgi_index index.php;2 m8 Q4 ~+ S2 J0 W& N. X
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;# W9 Q7 h: Z; e4 y8 ?, R
include fastcgi_params;
4 Z4 l& e4 D. e( y r! g* z }
2 |$ k/ I5 R! ~# S- P1 F }/ }# F. K! \+ Z9 y1 _# M a. u
upstream负载均衡:
! L5 l/ m+ C( t9 t; H
2 m" ?8 a1 k6 m D# g$ R7 [: dupstream 52os.net {
& {, j0 C) c2 p! u, E server 192.168.1.100:8080;
1 N$ k2 U, {: V0 N5 p$ j; W% B server 192.168.1.101:8080 backup;
% z* p8 w. k1 b6 l} O0 k1 G7 S v- @; q
) f9 X% X3 d) W4 \
server {
( X/ Y, N5 q% Y6 ?) e% C* Rlisten 80;
' r! z5 H6 A. _/ Kserver_name 52os.net www.52os.net;+ T" ~9 a9 c5 j" v. F
5 A0 g8 u1 F: z
location / {
1 _, w; N' i( J* w ModSecurityEnabled on;
5 V1 f5 J0 _) T" }- B. y3 Q ModSecurityConfig modsecurity.conf; : V4 h% M0 O9 O( a, E% M5 E- [
; X! r' `, G& n/ o3 j1 J2 l proxy_pass http://online;% y ?- G8 X& l C
proxy_redirect off;+ h5 M: F( @7 g8 t. W( Z
proxy_set_header Host $host;
\; [" b9 @0 t# ?. R0 F proxy_set_header X-Real-IP $remote_addr;
w1 _) `- m7 C# o( n5 d$ f' W proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;- o: F) w) ^, c
}7 A" X0 ?! `2 I- t" Y- h
}9 Z2 n; e% r. k9 a/ ?
六.测试8 P* }: A b3 j
! H ^9 D! P- r) W. [我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:: U9 m- f9 i6 r! W3 Q% @
6 D" ^0 _, X0 {4 t) P# z' M<?php( {0 k" u9 \1 I; o! D" H
phpinfo();
& z7 v5 f+ f; k/ A6 X U?>! @+ V9 l8 C1 v
在浏览器中访问:0 h& h5 `/ e; Y) P% T
7 Y G0 r& m c6 G7 z
http://www.52os.net/phpinfo.php?id=1 正常显示。
. Z# R0 h5 q7 q# Y& _( [http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。' k( i% |7 l2 R/ Y O& p& J
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。0 U1 s2 v) R; Z2 c( w6 w: x% e
说明sql注入和xss已经被过滤了
2 {+ T' Z+ c; {% V/ J3 O6 e* C! L' w1 w# {5 h$ s
七、安装过程中排错& r) B2 ?! c$ R, u7 h
) i H3 [4 i0 ]1.缺少APXS会报错5 E! ]( `8 Y- s1 Y1 Z
8 O- @9 Q2 d7 v. e
configure: looking for Apache module support via DSO through APXS
( s+ T/ v& J* l* Gconfigure: error: couldn't find APXS
- K( @$ I" u0 m9 @, A7 n$ J Xapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。+ p. u. }$ i7 z7 s( F: w' m
解决方法:7 E: H x# I0 R6 I6 k7 [7 c8 C
# O( K- `- R! v; I4 H! M
yum install httpd-devel$ G- y8 t8 I( {: R+ s3 {$ y' X% {
2.没有pcre2 n; y$ O& n8 C
( `0 B* w$ ~, I, O4 f6 q; O
configure: *** pcre library not found.
% x1 H8 }( y$ Iconfigure: error: pcre library is required
1 K& H% B( w- [+ X2 z解决方法:
# _% A3 M( O+ G- f, P" O/ l0 ]5 r0 X1 a: X! K
yum install pcre pcre-devel
- W9 k( p0 z' W. Z' `$ c3.没有libxml2
6 P2 J3 l* ^. E) o0 g, K
( O" a* f5 N0 R
$ t3 g( o" Z9 j" c% X Q4 @configure: *** xml library not found.
5 G6 v% D- b4 ~ ], _% c4 pconfigure: error: libxml2 is required
5 Q* p( c( y+ a" ^7 F解决方法:
8 p- [9 E; v! z2 Y7 K+ l l
; N3 p$ h7 } h6 L9 tyum install libxml2 libxml2-devel v/ K# I- E7 [: N9 y. u) h E
4.执行 /opt/tengine/sbin/nginx -m 时有警告
# w7 n0 l0 K) A5 |
4 z; b& R# M- tTengine version: Tengine/2.1.0 (nginx/1.6.2)0 {: w) b* I8 B0 t
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!8 _8 G+ f7 F3 k8 O1 q0 g* P) v
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
* w2 s& D* ^8 H
, V+ |5 `$ C/ a9 H0 b& v2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.. f. f9 W; |7 W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"5 M: _3 s U4 c- ^6 C% N$ z
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!( Z( Y L$ y9 k
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"( Y$ l3 N% X# o4 c/ s0 x M0 Q4 V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
4 S1 h3 K. u6 m3 |3 R2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
( H! P7 Y# S0 _9 N解决方法,移除低版本的APR (1.3.9)
: F9 x( k- a% c$ l# K0 C6 i9 |$ i
yum remove apr# P$ q- C$ i2 X% P5 u. S2 Y1 x
5.Error.log中有: Audit log: Failed to lock global mutex
( F" ~, ]( i# o! ]" s: Z
9 c. G5 }! N; C4 _8 P/ r0 q2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock % F7 t+ e2 @1 w8 `; F- s
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]% \9 n. S' M1 f W; Z
解决方法:
; }, `6 t- c5 W% G) Z8 Y编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
8 I: t& p6 M, y9 | [6 U4 Y) z- ^ F X# L# p4 P9 m
SecAuditLogDirMode 0777) e/ A7 k6 t1 r/ L: w$ O1 R) {, q
SecAuditLogFileMode 0550
/ K# i" x! y8 Z! g9 K. E6 q, bSecAuditLogStorageDir /var/log/modsecurity
+ H4 ?" T# q6 A$ N1 ?SecAuditLogType Concurrent
8 I, v( n$ F6 @3 ^& j; U/ v" [参考文章:8 u$ B/ ]) ]2 p& H5 }8 u
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
' z9 @1 ?3 S5 e" \& ]1 Bhttp://drops.wooyun.org/tips/2614 |
|