|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
7 s0 P$ S, M# A2 I6 R3 f4 t* E4 M# l1 j) ]8 G% c+ L( _+ q
一.准备工作
, i& K9 _7 f% M4 z8 l5 V2 @4 k9 A0 f; o" h7 }1 J
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.06 M2 Z1 Z. C* g% V( l% C F
3 g8 m2 x& o' m0 Q8 B2 {' Ttengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz" `6 H$ u# \0 a; \) l ]
: o! E( c5 ~( k" P$ w F
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz& v/ e" v+ q5 G& }2 Y
: z3 K! l5 }. z; S9 f
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
- Q K/ j0 x9 w+ m+ a- x: k8 c) P' I- j0 p" G3 ?
依赖关系:
, E. W8 _; \$ x8 C3 L) A' qtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:5 ?& {/ e I$ e( S
& C! _2 P1 P, O8 H
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
1 T% C2 y* _) Zmodsecurty依赖的包:pcre httpd-devel libxml2 apr/ ~5 r) W- v0 |0 F& Q, ]4 m
& a: G6 _: ^% ?. \yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel0 j4 O6 {' t- n h; t* W9 U
二.启用standalone模块并编译# S6 V0 p3 Y3 @5 Z3 q& J' }% s+ O
: {6 b. w( r& [' J9 t下载modsecurity for nginx 解压,进入解压后目录执行:
! `% B% ~2 \ A: [# P; o8 Q8 _0 L8 ?7 q! A3 g% ^; [
./autogen.sh
6 ?# e& n( M- S& b./configure --enable-standalone-module --disable-mlogc
9 {9 s D6 v) Q n8 S! N: u) {/ Omake
3 ^7 T' K! ?" B% O三.nginx添加modsecurity模块
4 }6 z i8 B5 ?8 b5 q
) J: ?0 |: b2 G2 h在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
& e7 d+ R) k( p3 Z5 G& p+ \7 [8 [4 L" w5 e( |0 g( K! c
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
# X' n: ?: k% i0 f8 A+ d' Rmake && make install9 x. r: z: {7 w8 {% ]
四.添加规则
6 I" W+ d- Q3 g- P* N0 }9 p. g( w" Q0 E2 W$ A U7 M0 f
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。8 Q! W T8 \* O& w* c, i8 ]
3 {+ F7 p8 J3 g4 o! i0 i/ G1.下载OWASP规则:4 ~* ?. j/ x& `+ L, y' \. H- V e: X$ }% c
* [2 e0 R% c$ n ygit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
[! Z, G) \7 c0 ?0 B9 H
5 b5 ^8 |( e, T7 R; o* h- K3 mmv owasp-modsecurity-crs /opt/tengine/conf/
# f* A4 I4 _- r6 S' [2 ]) V
9 |9 b. K% r8 ]! e) s& T" |cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
+ |3 o; F& B7 Z* l# A& [- e2.启用OWASP规则:/ ?6 I7 Z! V# C* |
8 b, c4 k3 e3 X" N- g& @
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。! h2 U; q5 Y9 o! Z: m# W+ I0 a
* n& _5 q6 R# R9 n7 i" a: n编辑modsecurity.conf 文件,将SecRuleEngine设置为 on! L2 x1 x8 H; u
9 X [/ k) F( d, Z
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
: t4 G. d" b$ Z( R! h6 F8 p, V
8 b4 }% W; i5 t3 D" [" iInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
- S, v! t3 ^# @- `8 p4 _Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
% j# j6 ^$ w8 ^' n$ G( }: ?$ kInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
/ h( p/ w( s! b/ ?/ vInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf9 H9 ]! ~) |7 @5 E7 D& l* h$ F N
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf# I) N! o; ~* I- `) ?! }1 J
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
! u+ K7 a, w$ N2 U8 d& L& CInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
" S b& ^! Y3 C五.配置nginx
. ?* e6 ^2 S1 B2 p' c3 @/ q, R/ R2 i9 E! Q
( L! L. d K0 ^" N, Q在需要启用modsecurity的主机的location下面加入下面两行即可:3 [7 I& k9 G- y) I3 G1 W, V
5 T" y' E5 I: R* nModSecurityEnabled on; 0 t+ o, ~9 i( u2 c; Q$ J
ModSecurityConfig modsecurity.conf;
5 c' b2 U: C, Q# Z下面是两个示例配置,php虚拟主机:
( P- c- U0 I3 x9 c. z5 q4 T: T/ S7 e) s, L
server {
$ p2 ^2 ]- \) K z5 e# Y listen 80;1 h& J# b; r! S, j* r4 n
server_name 52os.net www.52os.net;
" ]0 U; j0 O! s2 c6 c0 a
: G% i7 _* g& R" j: L location ~ \.php$ {: f5 e8 `& ?0 r, p5 y
ModSecurityEnabled on;
9 E' Q6 Y( w& v% w: z5 ~ ModSecurityConfig modsecurity.conf;
+ Q% f% [! m8 m3 }" c
! R1 u5 ]% f5 a) ^' F root /web/wordpress;
* e; x, {- G, b2 _1 _6 V index index.php index.html index.htm;
! L9 K- O6 E: T& ^1 _& V
0 B, p4 L5 Q! Z# c fastcgi_pass 127.0.0.1:9000;
J7 q4 @; w% \4 Q fastcgi_index index.php;
& J6 v2 u. X6 j5 N fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;0 T1 y/ w! W9 @ A" D, K4 e6 D
include fastcgi_params;
. \/ g) K1 t! n" |, B }
( B3 @& S, Q! m8 u- V: R }8 i& n* S3 `7 S0 v0 L
upstream负载均衡:2 e( V; _, n5 v6 \
5 U9 k7 @, a8 u/ P' S: B: Lupstream 52os.net {
! J: D, \. b( M" x; D$ B server 192.168.1.100:8080;2 \* z4 }" f3 m/ F
server 192.168.1.101:8080 backup;- v1 _% q" U% F. n6 ]' i! L3 f; m
}
# q& r$ B; ]* u: }1 k& p! g8 p7 p, S D; p& E8 R
server {) Y2 ]: t% A2 `0 |! R' }
listen 80;
) ]4 }% _# f$ R, `0 i) S" cserver_name 52os.net www.52os.net;* u- j" `; ~* z7 D0 s
- t8 Y) d% ?8 O- P( k% v. t- i
location / {: g- u* E' n- D* Z! r# m; z
ModSecurityEnabled on; , N$ Y1 t. o2 O: U
ModSecurityConfig modsecurity.conf; 8 |' m! ?; ]" |& ^
! F# o$ J( P: p; J, e
proxy_pass http://online;, |6 Z0 `2 b0 a Y {/ ^1 X5 b
proxy_redirect off;3 ]" X, t9 B" U$ ^3 [' ]" q
proxy_set_header Host $host;* I3 e; h1 x8 ?* y
proxy_set_header X-Real-IP $remote_addr;" I% z& s {$ k/ \( Q2 [5 ]- Q
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
" K; b' m. K/ Q' T' h$ m }
S" \8 y+ W; L}
8 v3 Q* a( B% K0 r3 n六.测试
* o: k1 U4 J9 L. `1 a1 m
/ I4 o* i5 X" T% }6 ^' z我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
: {/ c2 ^% k# d+ H' S1 t, E) ?" [! u* x/ t5 C0 @( t& q$ {
<?php t8 [) o+ F' j6 Q4 q# P
phpinfo();
% H8 p" W: F9 D2 S1 o# I; t9 W?>8 f H& r B9 {3 f9 t
在浏览器中访问:/ l# I% a+ Z/ F0 c6 ^7 U
1 d/ l9 E& g9 ^http://www.52os.net/phpinfo.php?id=1 正常显示。
8 l4 G; `. S) u: [http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
& X# N( A @* ~4 C& fhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
$ W2 f$ m' v# u说明sql注入和xss已经被过滤了. ~7 l5 n7 p: ]0 M
}0 o: g9 x5 R( f, m七、安装过程中排错
+ h# P9 Q5 C& H" y! X2 k1 J) I! S8 M% \8 b4 ?
1.缺少APXS会报错
8 k% g7 j' y" n: ?" q+ u' V' k* {! S5 ]3 H$ _4 M. n
configure: looking for Apache module support via DSO through APXS3 ?) Y1 I8 m. T
configure: error: couldn't find APXS
5 ^/ O1 B0 F0 |1 zapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
H+ r5 V4 R4 z1 L解决方法:8 h7 D/ A) Q2 V0 }/ u- z' `$ a; i
) ^0 P! Z+ [* e, E" |
yum install httpd-devel
# K9 k3 _" Y' ]1 T' T8 U9 o2.没有pcre
6 i- @# l; J: M0 q0 P1 `& K3 b+ s7 u
configure: *** pcre library not found.
) o8 y' c s; N" U4 S* ]- }configure: error: pcre library is required
# O/ Z/ ?* Y& r# {( I解决方法:
3 z6 O$ O8 @+ E9 {" l# e9 [. f( n! v. o7 E+ G1 j! Y1 i
yum install pcre pcre-devel
+ V G3 x7 n# f: t3.没有libxml2
! T6 o! V2 D2 ^% k
0 ?3 {) ^# t" l5 @# W- {: s% @. e& w* x$ q8 r* N' l! v& S
configure: *** xml library not found.. [) J3 R' o& P
configure: error: libxml2 is required1 f# N$ t) e0 }) z: L
解决方法:! [9 ~, N7 Y0 [ E- {- L" K% E
5 Q ]6 I( L& b! h" ?yum install libxml2 libxml2-devel
3 ` Q) I: v+ Q: i* k6 f4.执行 /opt/tengine/sbin/nginx -m 时有警告
9 B7 U# R( x% j0 y, i* D8 s6 z; j y
Tengine version: Tengine/2.1.0 (nginx/1.6.2)1 I3 c' t0 c4 _2 j8 ]* [
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
v4 E. b3 C; [% Q6 ?$ _3 u/ |7 Q7 {原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log# D; n9 l. L$ T7 p; Y4 _
0 K% ]. `. M) ]$ Q* x
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.9 S" T' N; C6 `
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
1 ?1 [9 U5 L. ?2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
1 n, \; b% r5 k8 t# b& K2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
: l+ j- a- T0 J$ e u2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
) P/ @. b$ L" a& M9 F! g% t2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.8 h6 m$ P1 C% ~: y
解决方法,移除低版本的APR (1.3.9)
; e$ B2 A! u$ ?( d, }
8 S$ @: C/ |( b0 lyum remove apr& U. p* q( y. ?7 L: z0 \! k
5.Error.log中有: Audit log: Failed to lock global mutex
9 {% h8 `5 J* T% T8 i6 U& v; q6 i/ p3 a& n* f+ l
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock . a/ H$ h" V/ M2 Z5 |" u" {0 W. c
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
8 J9 h# b; e2 u" I" s- h' P' H: D解决方法:. F& t# l! X' Y l* C& K! U
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
" f8 X3 d5 q& Y9 C; B$ R4 u' o6 L" {$ g3 E8 X/ \$ k
SecAuditLogDirMode 0777: v/ Z d! k2 `9 p
SecAuditLogFileMode 0550
- _. u0 \: F! \; ~/ q7 `) RSecAuditLogStorageDir /var/log/modsecurity. s5 Z& V+ w4 R7 C4 c7 E! z4 r( i
SecAuditLogType Concurrent/ r7 ^4 G* r# m( K3 r
参考文章:. N4 Z6 v( B p8 n' X5 E
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
5 W* b6 s/ H7 Q) y' I8 _& O+ a, khttp://drops.wooyun.org/tips/2614 |
|