|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
3 v3 y7 j- C0 \5 c7 ?& {3 _; s; q6 A
一.准备工作
6 u$ {. V n; V. }% L5 ~; t2 y; S0 ]+ C! }# Y5 ]) ]4 N
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.09 p4 w9 h' [( F( l
0 f2 a& m% x9 l2 I6 B1 A/ D. k. N/ P
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
: Z1 ]0 h8 Q6 C9 n9 w2 I# c2 k
* Z" M- I8 H/ d7 I+ T0 Cmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
7 @/ i! `+ y8 E0 q
# W w3 J9 j' D% ^OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
7 g* q- l. j0 U' ~7 g& d( c: N4 U% t9 \% B
依赖关系:) ?/ L0 h# D- r4 }
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:' [+ @+ s" y S. F8 Q$ o" v7 a; N0 w
w4 Z% q1 p s: myum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
' G9 L% v1 A5 I; f2 C. M! gmodsecurty依赖的包:pcre httpd-devel libxml2 apr! C6 g0 S! ~" W, a9 [
9 h* @) L) C3 Y
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
# i1 N/ U8 z- O: P二.启用standalone模块并编译2 o( r; C9 `, @( W$ C
$ a, I( d; `5 a ^* X6 v
下载modsecurity for nginx 解压,进入解压后目录执行:. b5 n" y1 _: z
# V B. _+ T: e6 B: h- o./autogen.sh
2 a. D+ }- r" k6 O./configure --enable-standalone-module --disable-mlogc9 l& B" I+ s& `+ H: d- j7 c p
make
p. \; s0 [! f( N$ M三.nginx添加modsecurity模块
1 `' H- ~9 I# x% T
6 }, u4 C$ R+ O3 W1 e5 G在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:1 k6 {# h( ^3 `0 E
& T* j, @7 f+ e+ o8 @8 I; i% u8 L
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine' p8 d& G, O; G1 v# i
make && make install
/ r, A2 ~' W; L! R四.添加规则
# k) G, o. V5 O* g/ {5 L0 L" A# E4 ]
6 `) `8 u7 w; Z2 [0 [, z. K# E7 kmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。% s# Z6 ~. ^$ A0 I6 k" H8 F
* l' w- i# G; u' x% {
1.下载OWASP规则:& Y. i# r* U$ X: C# m
: d/ l. k# @* ?+ T9 e( O; Y
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
$ F( S; l0 ?6 m" j2 [3 u
( P- E, ^9 R1 {$ }( Ymv owasp-modsecurity-crs /opt/tengine/conf/
+ q8 B; s; ~8 P( X2 K) F1 d8 ]% D: ?
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
" i% K H% ]4 B2.启用OWASP规则:0 \, w: H8 ?$ m: h, \ U1 Y
2 `; X5 ?! k. n1 j! d
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。: {) B( X) }7 W0 \6 g- @! k
. G/ ]1 Z# `& w4 U" R3 g7 Q
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
7 G, U# U( w1 E8 ^! G5 H! J0 r% z2 P/ j7 Y) ^
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
5 l/ h5 g* F$ e0 D4 F4 Y% x `% l# z) R; P& }) k; U) p
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
% y, u# [1 K4 I% [# E0 u3 SInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf% o- _; x- F& S$ o. N/ N
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf9 \) w R. G) Q. H* D6 K5 Z* U# C/ B# ?
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf$ s L2 C. d9 k
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
! _9 V; \- e% I( D( dInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
H7 X6 K) R( _9 f% ]Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
) j7 c8 d" s* X) C' a# s五.配置nginx$ A0 e9 J; ^+ j4 C( l" g! J3 l* |
( j- ]9 G( v" W1 z. z3 E0 G4 r
在需要启用modsecurity的主机的location下面加入下面两行即可:% C* z4 t% B# E9 x9 z: @- G
' x1 j, c$ P0 {3 C% N8 ^+ G
ModSecurityEnabled on;
4 Z4 X+ S& B% c9 aModSecurityConfig modsecurity.conf;
) U. c& c$ M% R; D, S下面是两个示例配置,php虚拟主机:
Q9 q: Q7 e3 y$ R% n( ~- Y6 b' S! }8 T9 ?( Z# ?1 b
server {
: U, c( D+ y8 Z6 K! [ listen 80;1 X% y0 c0 L- V4 u4 D% Y1 B
server_name 52os.net www.52os.net;
4 {6 @& }9 f/ V3 C
. `' f+ U( T8 n0 X location ~ \.php$ {
. G$ k) O3 b1 v/ }" | ModSecurityEnabled on;
: Q# ]0 I( V8 N$ P4 t# | ModSecurityConfig modsecurity.conf;* q6 U5 B" L! c5 d+ g' w" E) {+ m
# ^% o. ?2 _! ` root /web/wordpress;
0 c% s' J7 l9 S index index.php index.html index.htm;
' |" C/ V" D0 @1 p$ q3 r + P( \5 w9 |0 ]" m
fastcgi_pass 127.0.0.1:9000;& S, ]9 Z- W3 d4 W) g- H+ b
fastcgi_index index.php;
! ?+ ~! g4 o3 q# e' s, B fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;$ d. z6 e1 n* {% f" \
include fastcgi_params;
f2 s% X8 ^. g. E. a+ T }
- j4 _* D/ H; Q8 u }0 z8 J X1 V+ f' O# F, L
upstream负载均衡:
# T- B4 C( T9 A% {: h2 j9 W- [$ J, x, @3 v0 a7 ~ I+ \3 Y
upstream 52os.net {
) ?( f& h" o& n$ q; ]# [" s server 192.168.1.100:8080;
* I0 T! I' M: X2 m3 W5 W' B server 192.168.1.101:8080 backup;2 A9 d+ e5 k- t( z! }- N7 |; y
}
2 {8 U3 E# o1 W6 Z& p0 A* z8 v }) j% N
server {
$ J+ z& X s; Q2 U- [6 blisten 80;
% ~+ D" g& j3 |0 Z. P y( lserver_name 52os.net www.52os.net;/ J/ p4 |2 _; O$ }! R
9 R3 ]- W0 ?1 N
location / {& }9 \& g4 T# ?$ ^8 C+ B$ l
ModSecurityEnabled on; & R3 G, G) e5 E
ModSecurityConfig modsecurity.conf; ! F& t% |* Y0 D
0 Y( k( \5 R5 i proxy_pass http://online;7 x7 L3 w3 S9 j1 |$ h7 \
proxy_redirect off;3 ?% {7 R9 L. X
proxy_set_header Host $host;0 K) A% @ ^! b' C2 z9 L
proxy_set_header X-Real-IP $remote_addr;
$ [7 |" I j0 W( P& z proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Z1 X1 I( b8 j, X9 { }
( K/ }7 f# M F( G" R7 A}* |: P8 H2 d% F0 \6 L9 _4 n1 ~* B
六.测试
" T, l8 Y" |( U- Y+ x
9 q4 Z9 H2 Y% J: N! _我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
( b4 J4 t0 o1 I5 r. Z
" k- U/ a9 r" N# H1 F' I; h<?php& Q- ]6 c+ \* [, d x
phpinfo(); : R1 ?7 R1 l: F- Z3 U4 E" q% v
?>( m7 ?0 d+ [$ B+ l# O ~" G
在浏览器中访问:
) G% K4 \/ ~1 D" G6 l4 W! \( z2 {9 {, D2 p
http://www.52os.net/phpinfo.php?id=1 正常显示。3 u' ?/ Z: q7 m) S0 {$ Q' \
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。$ j* _% M" g: ?6 M; H- [3 \
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
2 t% h0 a! ~: i$ K6 {8 C" E# _说明sql注入和xss已经被过滤了
Y) ~; v- |# D1 J/ S& v0 }5 Q, O- Y! X- c: {( A! g: B
七、安装过程中排错
. W5 d3 m# I, O7 G% `) I0 M- e% f: n7 m9 G% C# P/ f- r. W% J }* T
1.缺少APXS会报错0 U; ~' e8 {9 ]: y$ V% I
1 }1 x' c7 v" p! ?+ x: D# S/ B
configure: looking for Apache module support via DSO through APXS
, R- `; I" o% [5 z$ Mconfigure: error: couldn't find APXS
6 y* P5 c5 D& q5 e! L$ u" lapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
9 e1 G6 E a; M' v解决方法:
% t: F+ l% s: M! U+ z
, F: _* M, @8 O; Ryum install httpd-devel
B9 |4 z9 z+ x2.没有pcre
$ l" W% T, Y7 l; |( F7 H' q
$ _! C# k$ s7 r& a9 econfigure: *** pcre library not found.
0 b7 s8 Z. o0 E& |6 ^, Sconfigure: error: pcre library is required! P% L; t$ p- K( F
解决方法:# \: W, A& A2 R' s0 Q+ }
% T+ b! i% X. ~2 P
yum install pcre pcre-devel
- y4 v& B- R9 v& i& h3.没有libxml2
9 K) j3 l/ @2 s) T* M' U( H& \- ?5 r7 d( _
- {1 u4 @5 ~/ G9 J% A7 X0 y
configure: *** xml library not found.( g3 a7 Q9 c- j" P% j) ]' i' C/ @4 T, K
configure: error: libxml2 is required
% G$ T2 O- j' C" b( _6 S3 R解决方法:
& M! R( ]3 C' J& Y' r T/ l% M, N
yum install libxml2 libxml2-devel& R: P: q% H3 B2 e3 a* z
4.执行 /opt/tengine/sbin/nginx -m 时有警告# p ]" q3 I1 W6 N1 v. ?' t
0 Y. b$ G+ f2 p" i* I/ xTengine version: Tengine/2.1.0 (nginx/1.6.2)
1 g8 q$ }( e* K7 t, @; b6 rnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
/ V+ N4 y4 O: j9 h/ h1 O7 H. `原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
8 H$ v/ c7 m* ?) K8 o; Q7 J, n7 w, L9 [0 R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
+ @# g- K- d( x+ [$ h2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9" g9 ^* w; K7 X `8 f' k, e
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!; o3 D0 Y4 l; n/ ]6 U' n' f
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"# N' O* R2 a- C( [0 q) ?
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"6 {* W; u) @1 O0 k7 \9 x
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
4 m+ O( u, f- n4 `( h) W5 e6 O解决方法,移除低版本的APR (1.3.9)3 @" P1 J L; u# ]8 l
5 @7 h8 ?9 Z. z4 G5 P, L0 v
yum remove apr
1 F/ S: r: S. \. J/ I& ^ x" {' a- S5.Error.log中有: Audit log: Failed to lock global mutex4 l' E- M1 f" p8 r( o
8 B. M9 a1 T) H3 f5 D) |! r2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock O$ r; c( I8 t$ x* p
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]/ N2 c ]0 m. Z2 g5 J
解决方法:: |% @! ]3 M4 N* _8 p7 N
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:- F* G) o8 _# _& l' Z: j
4 p% o8 [6 Y5 o& c: x. v% I: z3 ~
SecAuditLogDirMode 0777! s! B; g1 z: ?7 C2 l
SecAuditLogFileMode 05503 `. H- g( W& Y0 U* g/ E, A
SecAuditLogStorageDir /var/log/modsecurity
5 [( M& p r0 K. z6 E" _SecAuditLogType Concurrent
; [4 C: d; C6 T+ I* P" @7 O参考文章:
, b4 s5 t, [8 Vhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
" P8 f" P$ }: khttp://drops.wooyun.org/tips/2614 |
|