|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。" R- I& l8 R; {% R
+ G _$ [) A7 O* h- n5 W; D7 i+ H一.准备工作
4 d/ S6 G0 x; A8 R8 |4 x3 o- y3 B
, F+ G5 n6 A% a系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0' |% g# g C7 J0 r- E
$ R7 j- ~9 e, gtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz( ?9 t% M5 Q. k1 e+ u
% v/ Z5 d/ L0 A$ v
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
6 [/ d- u- W* P" L) c- o% h0 m( L# |8 R; r! S# ~+ l" v5 K1 H" Q
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs# A; x# o, _9 O3 B7 x
2 H1 v. ?. h( _/ s依赖关系:0 N* u/ @8 q. B, Q9 Y e9 ~/ s
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:6 p0 v( a! u1 z9 B+ q; N5 L. ^
?) b9 \: J3 h5 S2 ]* z- Y5 y) Dyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel, l R) H) a+ G9 P$ n* d' G; Y, K
modsecurty依赖的包:pcre httpd-devel libxml2 apr
2 X: w- M7 ?% f2 o. H" |2 |8 y( Y t0 X2 X% B0 {
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel2 [% ~) d& Y* O4 g1 q7 W" B8 I, C4 E$ ~
二.启用standalone模块并编译# h1 P. |' K5 k( m/ q8 V
8 @4 V# i# ~2 u \2 q
下载modsecurity for nginx 解压,进入解压后目录执行:
; b( f7 i7 [& K
$ y- r0 d% d& Q/ A4 }% X./autogen.sh6 A) i5 c' r- Y4 ?2 ~0 I$ s7 _' C
./configure --enable-standalone-module --disable-mlogc
8 ~! N5 V* h$ O% Hmake
m& m) q+ k4 r( d& L5 r三.nginx添加modsecurity模块
# D7 E( ~8 s6 I; h) R
# R# G- R. y, i2 q6 }在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
4 X# G# J1 @# P% M% P+ D# p; M- T% R1 R; L" F" X- m1 N6 Z: e
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine* u2 Y u! `# ?# b5 ?/ c
make && make install/ X9 v* g+ W7 \# @& t1 v# O+ W
四.添加规则
9 x% ^6 b' v2 i
* K1 ^! A1 j' k7 y- Nmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
9 l$ Q- J" a* T+ r, L& d" y$ [
$ E: H& P- G9 _- |6 z3 p, y- ^1.下载OWASP规则:1 c9 |3 }" |! X" R! Y9 z( A
/ `; \$ p" G- p9 z0 \: V8 egit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
5 f7 H$ L% D3 N Z) G$ y* T5 ?( Z# f3 ]
mv owasp-modsecurity-crs /opt/tengine/conf/
5 u1 ]' y/ i. Z" n$ p8 q( g
+ t( s! \2 O: icd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
9 J# |4 j3 i" T1 |# J% p% T& N+ o& X2.启用OWASP规则:/ T, [7 A1 l2 _- [) m" a r
, ~5 c, l3 k$ V1 H% U g F复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。- [, d& R' l4 \ w- F
/ l/ ^- g. x4 ?9 v. U, M
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
$ E! Z m: E3 b/ h
: D Z2 p$ N7 |- \9 Mowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。" _+ `& H4 l0 Y A
& ]! s$ _8 n2 K7 c, |- {, p. n
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
$ h* k) r) c# }9 V( P! Q- m1 Q& mInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf3 ?% ~7 Y) G; Q7 b6 A: X# [
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf8 t3 F" I* [% d& s" k: ^
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf: f% q: q2 ]7 n6 ?" i& E3 y; B
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
C8 r1 @# A3 O, z" wInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf4 p+ S8 v- y) ?& y
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf: \/ y1 S( N/ f% q# R, ]6 m
五.配置nginx; U |2 a# J; m& T
' G5 Q6 Y; S T( I' M在需要启用modsecurity的主机的location下面加入下面两行即可:* R8 G8 v/ A( J6 F# p9 k
" k; Q- i, L& j$ e9 m! L
ModSecurityEnabled on;
' R; V$ n# D# w$ I' B6 v: DModSecurityConfig modsecurity.conf;
, W5 P5 ~8 U8 p! I( i下面是两个示例配置,php虚拟主机:' I8 [9 F4 e5 y: i
9 _$ X. B2 m6 W. }( Q6 {; U7 hserver {6 j8 w* O. n: e! y! `
listen 80;) D% o; @% ^* x- W- m& a% P# ]: f
server_name 52os.net www.52os.net;+ N8 Y* v' k W: G
( O" v w6 Y. H location ~ \.php$ {
- I( G$ _: |- B9 W2 r! r ModSecurityEnabled on; 6 O( A% x, I$ N' y
ModSecurityConfig modsecurity.conf;
0 I9 C( U0 J8 [" i5 y' W) B2 Q3 v! S
root /web/wordpress;! l" L& m% K7 U
index index.php index.html index.htm;
8 b0 M$ P# K- N% O; h
6 Y. q2 A. C: O8 F& s4 n% A fastcgi_pass 127.0.0.1:9000;
$ t/ w3 N0 s0 W2 y' T" K fastcgi_index index.php;" K! E! P3 y) G9 i9 s" n
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
) S# d# _9 z2 Z# }. s# C include fastcgi_params;
z! w0 i7 l* v6 b4 w7 n1 G }
) ]! H/ `4 r: q: \: }" v }# ~% _6 {* r) \+ A' e% [
upstream负载均衡:
* W- A' i8 Z6 m: [3 V- \
- A, x2 X: _- nupstream 52os.net {
) O2 K5 W7 Q) t* Z0 A! n( c server 192.168.1.100:8080;( a1 L1 T, [; D+ t1 M/ J! X
server 192.168.1.101:8080 backup;
4 T2 K! N' J4 l i& ?; K6 Z2 Y}8 j9 `+ y7 z/ F; o7 K
- G5 Q+ z+ |9 n" O. qserver {
3 p- T8 f# L7 c: Slisten 80;) F8 R6 w8 l' J: ?; d8 I6 A/ i6 Y
server_name 52os.net www.52os.net;* }4 r8 \4 ~- V# `
: [5 D5 W$ ^" N- {6 T. a" M2 _
location / {0 W1 [' E: `7 j1 L
ModSecurityEnabled on;
: h/ p; ]/ A8 c/ W1 U2 V1 C ModSecurityConfig modsecurity.conf; 3 M! O. E3 F8 o( i1 ]3 P
9 R1 _5 L4 \) d! {8 j proxy_pass http://online;
& l! C; I6 l7 _7 x; V! ?) ^ proxy_redirect off;* R" R4 ]( L/ t, @5 T' r
proxy_set_header Host $host;) o T4 K; ?( ^" p
proxy_set_header X-Real-IP $remote_addr;
L# J% T8 _0 B. d- ?# V3 M; l proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- q8 U: W& r8 E& E4 e } S4 l4 |. e0 L: M4 e
}: u* m6 J/ b4 @+ n
六.测试
. @- l5 R" T6 O1 ^/ C6 m8 M, H% F2 y" o' U3 f
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
& Z9 }5 n! u/ U, { G) k2 I/ c+ }/ d
<?php
4 G9 e$ \( h0 J/ f; [) d$ O phpinfo();
2 `5 W" \$ T# n?>! F( |0 m, q6 w7 G
在浏览器中访问:4 T% n% ?9 Y" C1 {& k
; \" u2 Q% T$ O9 a7 Whttp://www.52os.net/phpinfo.php?id=1 正常显示。7 p* X8 X' s0 J
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。0 z) q0 y$ V- }
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。( x+ p3 s4 `9 K: Y
说明sql注入和xss已经被过滤了
1 s' b2 Z) I3 b8 _* K+ H; T6 ]4 T. D
七、安装过程中排错6 K `" p# E8 d4 e
6 } U% a2 Z0 `7 W
1.缺少APXS会报错: p! @0 u; M: n1 T3 l
0 ~5 @) X) x1 e" E2 }configure: looking for Apache module support via DSO through APXS
( z( J. ?" U$ B% L; V4 m( C, Nconfigure: error: couldn't find APXS
5 h! R; z; {- K, W3 hapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。- h+ K2 t/ V5 [8 s
解决方法:
1 n1 D, D m* \' X, I7 |
5 Y; J. v; @5 O$ T1 w5 [8 @+ F% ]yum install httpd-devel
# G* e4 Q D& {/ M# e8 K( ~2.没有pcre( ?: n1 \3 Z7 H- P. I
/ K0 E I) q3 Z; E4 h/ O- U
configure: *** pcre library not found.- `9 _+ ~( z5 D( A1 l( h! I+ i, a9 v
configure: error: pcre library is required
- N# w5 q8 u5 n1 B9 L+ h解决方法:
& c7 w( n- L* e+ @, _% B6 ]/ u5 a/ E; l; _6 k8 A0 N) \4 A
yum install pcre pcre-devel" A5 z6 B$ V$ K# E- R
3.没有libxml2
: N( G. p$ ~) ^
9 L f1 m6 S8 p: F" e# q5 \( \: h+ w% p( r: x6 r) y: h
configure: *** xml library not found.
( b X7 n6 W9 ?, b& T }1 K7 zconfigure: error: libxml2 is required
- l& c3 U6 L T, p. [) L B% o1 q解决方法:
1 m2 M; q _+ s1 ?' K
' y9 S& w" e, P- @yum install libxml2 libxml2-devel# W+ r7 [2 k9 ?, j7 j D5 e" e
4.执行 /opt/tengine/sbin/nginx -m 时有警告
" e6 i4 v! Z; Y: W4 ~3 k1 I* p; w; W+ F
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
6 Y1 U0 q% P, n. M O G8 Pnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
. V/ g9 i" S9 c2 v) D原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log6 m! Z7 V* ^$ C1 B. ?( s
& X; K; H% `3 K: H2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
`, w8 L, z! x+ `2 o9 s$ V2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
1 Z3 ~! Z/ w( Q6 \( S2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!; B4 @( p: w6 o; }! ^* x: {
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
( X$ a. R; f% F/ v/ b8 U2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
) I- M+ F( A6 s/ m' r6 N2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.3 Q. Q! Q5 g$ H+ W
解决方法,移除低版本的APR (1.3.9)% k( r' Q7 m! L. o/ r3 n
; I! Y; I: `# d; q$ r) zyum remove apr: O1 [# K" g, [! B$ B" M6 K
5.Error.log中有: Audit log: Failed to lock global mutex [7 O0 o$ Z% \6 {2 b, Z$ W
+ t' F1 F: x: x. w! }5 C
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
, \0 B+ s+ ^4 e% _5 O% G. l- s* wglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]% r y3 U* T( b4 |: C9 U
解决方法:
# z+ T+ H, V6 I3 `编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:+ B. ?' }* _9 E" h8 R
1 u- ]. l' {5 C2 n
SecAuditLogDirMode 0777. \* T. a/ C+ c
SecAuditLogFileMode 0550$ a7 B5 u1 w' s5 h! t
SecAuditLogStorageDir /var/log/modsecurity5 y# s U* F) Q1 s% B7 R2 D1 g1 V
SecAuditLogType Concurrent
9 C1 e- o1 L: ~8 n参考文章:
) {! A1 ]9 ~! J7 x$ i' {8 `https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX! z6 X1 w$ {4 X2 U B+ U: c+ Y# |2 s' R
http://drops.wooyun.org/tips/2614 |
|