|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。9 ], C6 _1 `0 ~' r! M: B
" b8 V3 n4 t/ |$ J7 z2 s# l6 \% c一.准备工作4 R ~# T& w7 U4 D, \& N5 p
2 W" a. G( K$ }系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
0 C4 t' I/ _4 X8 @ H0 O( O1 P! R3 r- f& H" \
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz- d4 N3 }4 _9 _
: m1 T& a A2 j3 Q6 k
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
: _+ c; V( _3 n3 W* S" e- I1 s2 l. H: ?9 v5 P9 Q
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs6 N) M! G7 v4 e! q
* ]7 C! q/ A S* ?6 H X依赖关系:) T! N! |: k: F$ D' h- Z2 `9 W
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:7 I! o" H! ~, v
( Y8 k4 S) g6 w3 D
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
K; @; l/ l. Y' o3 pmodsecurty依赖的包:pcre httpd-devel libxml2 apr1 h: D4 V, I7 D4 p2 T
! d, L) j, E7 C9 ?
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel. f2 S( f% r l1 o/ q6 V
二.启用standalone模块并编译7 t2 `( I% D3 ^# @( b0 r
+ s; w3 b6 w: R0 U
下载modsecurity for nginx 解压,进入解压后目录执行: e1 G6 [# \6 L1 j t4 \2 O
( @! R% O! o' C0 f
./autogen.sh9 ~" }' R0 f1 K5 s6 V! B
./configure --enable-standalone-module --disable-mlogc
" a- B# U) k/ ]+ S, Y) b% l2 M+ Umake
P* f D# a6 _# y三.nginx添加modsecurity模块 T' _% Q% M1 _0 I3 Z# j9 o9 `
; Q/ t6 R- M. _: i: m. |8 I$ U
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
% [( D+ U% A/ l0 K, {, U* E. m8 Y7 p8 ~8 M" I/ k, w
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine* V F1 A5 i$ G% I7 G1 |* T
make && make install- {4 |! E! a) u) D8 e; @3 r) F3 |
四.添加规则
, c% X4 x. O$ W1 e3 I- | I
/ r9 H; E( G! z$ W2 u) W, R+ h) Tmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
, w5 e# v% `+ M( n2 J* [9 C7 z" c9 f- U
1.下载OWASP规则:
+ e% `8 |; C' G6 u. i# S2 `
* I- |$ `) d, w$ w1 |. D) Wgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs/ V" Y& m9 w! z( n9 `2 L
p! r' f0 c( @# \( amv owasp-modsecurity-crs /opt/tengine/conf/
( `+ J9 b9 @) @: P3 m N3 j9 n0 `) ?; C, f/ S/ V
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf" Y" k( S" L' \& p) v' \
2.启用OWASP规则:. Y; ]% _: j5 {$ `& v
" Y2 s' C3 y* r! A* F
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
' U' }2 H$ _) w+ p/ d! t/ p4 y2 z1 U7 v) A( c5 o+ ~. K7 M
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
9 Y( e Y; |8 O' x* B% r; P+ {4 Y* s
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。 D" z ~$ ^1 y, |
" ^( Q* N6 |1 o- L& w2 g, I! c1 X
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf+ H( O! U1 U! P4 k2 Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
; v9 |2 Y$ Q8 N9 o$ G4 J5 ]Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf6 p+ c- g2 Z4 S- {* e# V
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf. X3 {. O9 m3 x# p
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
2 J& C# a8 ^' l% Q0 sInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf2 c0 C9 P& x) o/ Q0 d/ e
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf& l3 n" A j3 C9 g ~7 T3 ~3 b; I
五.配置nginx
) n4 x) x8 j- |) M2 g& R9 B1 l
在需要启用modsecurity的主机的location下面加入下面两行即可:
; W1 `# K1 @3 Q( N l0 B; U- a& B8 G. a4 o/ J0 X1 ~
ModSecurityEnabled on; 8 @" @- p- K& Y1 U
ModSecurityConfig modsecurity.conf;
$ s/ R+ P& l9 N下面是两个示例配置,php虚拟主机:1 [1 w! ^! \- k; f/ x
- O6 ?, K* L% w8 j8 l- {server {
/ E7 g& A0 W- F6 ^4 I+ m0 ~) z7 m3 T2 `8 L listen 80;3 k4 z4 { `" G- G, v1 B
server_name 52os.net www.52os.net;
, d9 t u. d+ H$ i: n
8 `# u# }. W( @5 p location ~ \.php$ {
5 h) f. u5 d- o3 v! p9 W/ i; J ModSecurityEnabled on;
1 B1 s4 B) E+ A3 f; ^ ModSecurityConfig modsecurity.conf;9 U" J5 E; |/ X! _
1 R9 p% b) i8 t
root /web/wordpress;; p4 d; a0 S0 X& c- Q% R, N S
index index.php index.html index.htm;
) F$ J4 D" m1 Q, Z , G" M, ]8 r6 z9 g1 [. c
fastcgi_pass 127.0.0.1:9000;$ }9 ]$ A! w+ I9 o1 n$ F: b! J
fastcgi_index index.php;
1 z- R) A+ G0 `5 {0 [; A6 R! O2 T fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;* K! V" n& o# n( M0 K" J
include fastcgi_params;. r0 j2 w! X1 D4 a4 J+ ]+ g
}
7 v; H% o# u% t0 d& L b& F }
: h9 g+ c: `9 ~$ G9 w& c! ]upstream负载均衡:) r6 e7 U8 Q4 b6 G0 |& u
: {2 M) W" `# {upstream 52os.net {/ T* M5 F% P. O5 m
server 192.168.1.100:8080;; v& P4 y Q* z7 p4 f
server 192.168.1.101:8080 backup;
% u) Z8 R% \ }! l}
1 _; n, X; g" ^' I0 x. d
* P) r1 \3 N' |server {
2 z3 ~8 H6 ?( F3 Z* Z" qlisten 80;
8 c8 w% B" c _: r$ xserver_name 52os.net www.52os.net;
x# n! x" G5 d) a; Z! F5 R0 T2 x; A6 v
location / {
2 a3 p% B) r7 M% o% | ModSecurityEnabled on;
, W, [4 N: ] \: t ModSecurityConfig modsecurity.conf;
: K- {2 w9 G, s" X0 ]
# w/ e$ H- f) [+ I3 f3 y proxy_pass http://online;
0 Z0 B9 d6 l, ]6 R; `* N. Z proxy_redirect off;2 c- f& m8 T# A9 D3 p5 G& t
proxy_set_header Host $host;
/ h+ \) ~4 G5 L! v$ ~ proxy_set_header X-Real-IP $remote_addr;* `( d5 c: |2 N/ W; i" s
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
( b) ?. K: K- I3 u4 s% P9 _ }
, y$ T# n+ _* O9 A}
6 }6 b' K6 k# R: f: p4 D/ f六.测试, B/ c/ m' I X! B' {( E* ?! I9 X
- v: O0 r: E5 A+ d! ]
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:0 s+ F& d( n0 t! a. S; m
0 i3 `4 M8 L4 P0 X
<?php5 k' f# G; D* Q! F
phpinfo(); : i0 a# t/ a4 D$ y: M
?>
5 O8 o" P) w, T$ T$ w. o* j& x在浏览器中访问:5 t( s% \4 \% b) @" B
2 z$ p/ @' C; Z& N0 L
http://www.52os.net/phpinfo.php?id=1 正常显示。& j$ e; \& h0 s; h% I
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。4 v$ O2 r! H# [; A' X/ F
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。6 [" Y3 ~" w* n
说明sql注入和xss已经被过滤了7 c; B. B8 m% N \! ~4 m
L$ z: I2 \+ S& `; Z% G七、安装过程中排错
* n0 D& v6 I e
1 ?3 Q6 l( V2 w1.缺少APXS会报错" t" R9 M0 w% u8 w0 w* d
4 i4 @$ r5 e6 D
configure: looking for Apache module support via DSO through APXS
; B+ }2 |0 Y) ?# qconfigure: error: couldn't find APXS
" m2 K+ y0 v' g! t/ p, W- \apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。2 G, S% q: L# a+ l* K+ q% X
解决方法:
( m) u3 I, U9 [6 I1 d$ X% F( J
. H) q7 h' O6 H( _6 Wyum install httpd-devel: @* Z6 B. o% z: j. _4 ? s# l! N# ]1 {
2.没有pcre
; Z" H q) I+ |$ V. I
, r- c5 i7 F6 J' {* Z3 o& Econfigure: *** pcre library not found.3 Z0 d# o7 U8 _) b) k0 K ?
configure: error: pcre library is required
8 B' B9 X, t4 T% @, _解决方法:
* E! {' K, ]( d8 J% h, Y* P" ^2 Q* r. L7 v5 O5 ?. h4 e' [6 `% ^9 {
yum install pcre pcre-devel9 U6 c# S$ a; G; |
3.没有libxml2# L, v; V; `8 G% c) g6 V& X$ ? ~5 z- a
~( a ^9 k: g& K, Z, A7 E; l$ P5 |
configure: *** xml library not found.
' Y4 x3 W9 d7 }& nconfigure: error: libxml2 is required
- C$ Z3 M+ h5 |; B4 P! A% `2 S1 \解决方法:
2 d3 {0 D$ k4 y' L% b2 T% a. H8 `% ^
yum install libxml2 libxml2-devel5 K$ X- ]9 t4 H5 d
4.执行 /opt/tengine/sbin/nginx -m 时有警告
* [$ J. ]( W9 ` T" n' P
& K( h+ @$ V! t7 C* [4 d9 ?4 Q+ NTengine version: Tengine/2.1.0 (nginx/1.6.2)
: _6 w' V, E/ H4 x7 E# H$ |# b1 p# r4 Onginx: [warn] ModSecurity: Loaded APR do not match with compiled!; t4 Y( t6 b6 q4 E
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log( z G/ i$ X O# W+ Q4 f1 J+ l( K
5 t6 N: s- V; K3 o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.0 c k d- O5 M6 B$ R7 o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
4 r: V( W" W0 v( e2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!8 R, P7 n% j6 Y' @' o0 S! {) o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
' G9 A$ H1 {: V B2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6") n' k p: q+ n/ j' ^7 x7 s
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.3 M& a! p" W, E
解决方法,移除低版本的APR (1.3.9)6 Z6 d) g( |! r3 W6 X6 i( c E
9 f$ Q; Y! @; h' E7 T6 J
yum remove apr
. o }' ?8 e9 [3 G0 m0 K5.Error.log中有: Audit log: Failed to lock global mutex+ C% B2 A! d# o% T9 O7 ^4 x
: B q* O( h; }% H* u
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ( H! N7 P8 d$ Y' c) v6 A2 i
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
y) P; e2 R. H" q4 K解决方法:
- I ^" X' z {6 I编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:7 b1 U D- o& S' g% u
% |0 ^& j- P$ P' s0 Q6 O
SecAuditLogDirMode 0777$ x1 J$ U7 t: C) I) ^
SecAuditLogFileMode 0550. ^9 Z4 W- A; j |( S W9 k4 G3 [
SecAuditLogStorageDir /var/log/modsecurity
n$ Q' [# T+ r# g# qSecAuditLogType Concurrent/ E* T9 S0 V- X# H
参考文章:' ^5 y$ Z. D/ A) L
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX" I H, @0 B5 i- ^# c C6 V
http://drops.wooyun.org/tips/2614 |
|