|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
6 `0 G/ e6 T( n, w! a) C5 P2 ~& S0 s' }5 N; j4 y, y
一.准备工作
3 i5 ~; S& x7 f6 Y2 g" {' L& ^2 W+ l1 I) }- h
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0" G; L2 P$ o2 e; ]
2 q* m' o9 H+ X7 R' u2 q) Z; I! z2 xtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz3 |. P- r2 f/ H6 P$ y4 [; u
+ w% _2 y0 A& ?; F7 Tmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
7 I0 G) G; m& W+ Y& b4 d$ A& W1 ~1 E7 Q3 [5 y/ w
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
# C5 s0 ]) c8 U4 v B: \
; V9 f, G; J/ i- ]$ {依赖关系:7 R/ m% Q1 i2 @5 L( V5 Y4 B
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:5 ]0 N2 y. L. C9 b1 [
( d. ~7 b6 y7 Z4 b' Y& D' L
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
0 n- w' e+ W& r/ Rmodsecurty依赖的包:pcre httpd-devel libxml2 apr1 w2 J- l% b8 |& K
& Z9 w) ]- T* n
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
% f" \5 S$ a+ s二.启用standalone模块并编译
/ V- U0 J9 I" A# n
9 k/ q- E5 e' x3 V: J' S4 M下载modsecurity for nginx 解压,进入解压后目录执行:
# p% g! e0 d! h/ Z. A% g9 ^6 s c) M5 u1 E2 l
./autogen.sh9 j9 o" W& r2 Z1 O
./configure --enable-standalone-module --disable-mlogc7 r: s/ c. Z+ _1 U9 ?
make # f9 k% b% K) y* \
三.nginx添加modsecurity模块$ i6 A% u( }7 `" w6 A6 v$ Y
1 T# Q' ~ V+ J' S. h4 g/ ~1 P
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:# h, x* t! S( B
4 s/ k# z( M) O# p. X./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine# n( |, a6 v! G* b% \1 f
make && make install
4 U0 _2 k s8 Q5 }2 G- u* C四.添加规则
( k- l8 c. c ~' y
& d9 |) t4 o" j0 hmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
0 w$ Q4 Z* k& T5 i) ^3 d6 Q9 B8 W5 e2 {5 r* h ?
1.下载OWASP规则:1 {9 `1 h9 }9 u* x- N2 ]
+ M) N5 k8 M) V$ c' I8 c# k6 a+ |9 Y2 F
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs; D' C- ~: }: {
6 w) X/ X3 p/ q. @mv owasp-modsecurity-crs /opt/tengine/conf/! p- {# ?5 ^9 ]4 j+ h
4 F z; v; w0 l; P [- v3 z% {" @0 vcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf/ Q& m) \& Y7 C7 G
2.启用OWASP规则:% n- v1 T$ M3 u( g: P$ ]
# f7 f7 w* ? O0 U+ C9 g5 V
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。( t+ U" a. }' @8 Y- E
$ Z- ?% f2 s& Z; y
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on& N! O: P' ?( p
0 X7 m0 n" }9 |
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。% T% e- F) t$ ^6 S3 {! q+ E1 ]7 k9 P
4 t* y/ h4 O3 b7 k) g) t; {Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf8 M+ h2 e8 z$ E# R
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
7 W5 x- g# @* d" q3 nInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf8 X0 l7 h( t6 L+ ^, K# h/ q
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf" x; l7 p2 _1 U) u
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
: g4 b# E2 y) R% l3 Q( P1 jInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
/ ]2 C3 v2 r6 [! I( n2 l5 bInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
; ?, e. W, O0 H' X五.配置nginx
( l+ Q" b, @/ S; i5 V# e* h/ s2 D6 W# i) f9 s) A7 l1 g
在需要启用modsecurity的主机的location下面加入下面两行即可:
8 C$ F6 x9 |; y" l' r8 R; E2 F3 f) {- X' M
ModSecurityEnabled on; ( f, P2 L- U1 E% ]7 \ r
ModSecurityConfig modsecurity.conf;
% ?* K$ a4 i7 M& U! w$ Z下面是两个示例配置,php虚拟主机:
7 X" I4 e' E- A4 I: N; ~ a' E2 C; }( ^8 a# a+ T1 ]
server {7 I4 x! x& \# h! G
listen 80;1 f! ?( W9 |: o* C& _
server_name 52os.net www.52os.net;: }+ f% z- Z) O0 G) Z6 i4 Q7 f
, P/ k0 ^8 m9 |. _) ^, ]& v location ~ \.php$ {+ _6 B' O" {' r9 e
ModSecurityEnabled on;
; M% s* _- c- d# K' R- K( z# ` ModSecurityConfig modsecurity.conf;
* E0 o* j) s6 f. z$ m. m$ S. Z
+ o; l X+ z! x M6 W% A6 ^ root /web/wordpress;
2 t2 H6 {) V( \ index index.php index.html index.htm;! e/ D2 `- i3 e: V% ?( d& S
4 t: v! t. x i: `+ Z/ \9 Z
fastcgi_pass 127.0.0.1:9000;
0 v9 Y/ F4 T+ i. d) x+ O* J fastcgi_index index.php;! q( S {* x( ?5 T) G3 J0 D0 |% p
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;' N9 T/ I% s4 @% B0 S
include fastcgi_params;6 P0 ~3 }' Q5 _2 V% s- `6 w3 b% H
}
" N, b5 U% {6 f2 }1 U; B }
2 h; f& \7 j H# l( \* o$ c& Aupstream负载均衡:* `$ ^: j$ L! }9 Y$ {( m
3 ]# S3 K$ T5 }upstream 52os.net {
5 H+ `6 |: g9 ^$ O v1 w& M server 192.168.1.100:8080;
3 r: ?# h) t1 |9 m server 192.168.1.101:8080 backup;
' H: v( S: p {" m( Z}" B7 C4 b* d/ W4 s% n9 z# B
3 j& ^/ Y! e! t: u8 [3 Z; Dserver {
; R' D2 [' U3 a( {; n2 T- c! ilisten 80;
8 }, u' \# Z4 [: P4 V& y0 x/ Nserver_name 52os.net www.52os.net;
" S% U" l) o( Y, a8 ^
% s7 n( n5 L2 g+ v ^$ klocation / { W" C6 ^4 I5 j( ^5 O* A
ModSecurityEnabled on;
0 U2 I0 ^ t: V" A ModSecurityConfig modsecurity.conf; 8 P8 B& r# b% y2 T
4 u8 |) a# E6 Q; W+ ^* \ proxy_pass http://online;
2 Y2 ^' p* \5 ?; D& n' u proxy_redirect off;' a0 }6 P. x' q$ d2 ?& t5 I& Y% F+ [
proxy_set_header Host $host;2 H, U, {6 M- |6 j. j4 J3 b# q) x
proxy_set_header X-Real-IP $remote_addr;
4 h* P8 M$ w# _3 W& O proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
: G# P& e/ x k# f }2 y3 @( Z9 L8 Z5 x+ V$ r
}
% \# f$ w( `5 [- \0 u0 Q六.测试; X' i d* D) U) S* \; o* t& q/ c
) o5 a, {* E* S" q' \* Z/ j7 u我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
$ g* K8 C( c! p/ r: B- C
) |3 J# [1 W) J8 _2 j<?php
- p- }* ~5 B% X' u1 h. ?! t phpinfo(); ! v7 E* \# O( k" [1 m
?>
4 a7 }! D* t5 }! D4 \% L. O" a在浏览器中访问:5 V; w5 I' k0 q: c- U
, }0 ^% j6 r, y. G% Jhttp://www.52os.net/phpinfo.php?id=1 正常显示。2 x+ v# X2 K+ i! r+ @' M
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。6 p& I0 R7 W4 H! y) U/ N" @* A
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。1 }$ F1 \/ C4 |3 z
说明sql注入和xss已经被过滤了
6 x O/ q2 ^/ j+ J
' T& S. w7 Y- C七、安装过程中排错: L* t* D. B9 l" @9 O
- y. {( c, `1 i$ t- c# s& Y1.缺少APXS会报错. E+ k7 _3 X" {- E7 K: l% H3 e
9 a& k8 _& W6 e# Tconfigure: looking for Apache module support via DSO through APXS
* Y$ Y: H( \$ t; f* K" w) Yconfigure: error: couldn't find APXS
' N# ~ j* P+ B: U, Napxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
* ^. {7 O. f: [: M5 E. I4 F- U% ^解决方法:9 _7 C6 ^: I. n2 g9 F6 y5 y
" b$ ]5 U& Q9 K* B6 v& r3 T$ {yum install httpd-devel3 K, z, M5 J5 ^, k
2.没有pcre U6 \7 U* B+ _9 v9 C
3 m) t: @ f) G; w
configure: *** pcre library not found.
' n: Y" E1 t% ^6 R$ W8 hconfigure: error: pcre library is required
2 p, A- ]1 u: F \8 h& Z& W解决方法:
& G3 L. I7 j6 Y( F3 t; W
9 ]7 q5 p, U2 n( b* O- {yum install pcre pcre-devel
K& O: U6 n; Q2 A3.没有libxml2" X' n' O: Y; \( c1 ]* M) t% X
3 ~/ C2 A- u( _- v6 t
2 e3 ]* z p0 k$ I8 Aconfigure: *** xml library not found.0 H( j+ \+ J# _& h2 S1 [+ O9 P
configure: error: libxml2 is required
5 L2 [1 A' a2 U+ V. X5 v解决方法:
2 M) b+ x* V' ]- O9 G3 [- Z( K& _% s
yum install libxml2 libxml2-devel; Y7 d% H8 y: L% e9 q5 _
4.执行 /opt/tengine/sbin/nginx -m 时有警告
- x& @! P% s) ]" U; T
. w4 R, T0 V" ZTengine version: Tengine/2.1.0 (nginx/1.6.2)
6 X b8 D0 M8 i' \5 N" a; \ E2 enginx: [warn] ModSecurity: Loaded APR do not match with compiled!' s6 F8 G" f/ y- n7 D0 b
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
; o2 v, l' ?* @; g# |" b
' U1 n2 k. ~3 U% K) Q/ V2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
: e7 n3 U" D; q! n2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
7 K0 G; W% a5 H1 F, z2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
$ p1 d, w- p9 C/ f2 _2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05" }! @2 K6 m2 |2 C
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"" G; V i* k4 _, L% J& Q7 {7 ^
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.0 F, R& P( k) Y0 t* r
解决方法,移除低版本的APR (1.3.9) n! Z) c; p( a' r, K" u0 I4 q
* |6 n3 F# R$ {1 `yum remove apr
' Q- I, l& e- X5.Error.log中有: Audit log: Failed to lock global mutex
' ?8 A% B8 L* D6 T6 q: d# v; g0 | h5 ]7 ]1 F( e
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 5 g+ ?1 h' J+ d
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
( e G9 P* p, ~+ K1 O/ h7 [解决方法:- I$ J/ G4 C7 n- [) H% l
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
( [% C0 j2 N; I. U3 }) o
; w8 s! h" B3 l: tSecAuditLogDirMode 0777
8 p ]$ u z$ W" T SSecAuditLogFileMode 0550
6 f& `4 ]* J, w5 CSecAuditLogStorageDir /var/log/modsecurity
0 D3 ~' L g* Y* J& K; B, SSecAuditLogType Concurrent
& w; Q1 p5 `) V( P参考文章:
$ l- v# m. t* l& a9 q2 S V/ {https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
- C8 @" q7 l; V' qhttp://drops.wooyun.org/tips/2614 |
|