|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。/ Y+ f; J; B8 G* q& l0 f w
) h) a. I# ?5 Q- b# s$ o一.准备工作
0 Z, \* c$ H6 E5 w% |. Y: w j
) ?: M& J! T/ e/ r$ F系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.08 ?7 A: G$ o, B" P) R0 M
0 W {% U' K% B# D [tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
" S9 s. D" I$ ]7 x- j
x3 O# |4 s5 R5 f9 l( d! Y; t8 D0 mmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
1 w: y& A l- f9 y& w7 v: _6 c6 `6 Y/ ]( P$ N7 z, L( O
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
6 o9 i1 ~ S$ D: H( p0 y7 A2 G8 e* _1 B
依赖关系:, M( s5 H4 D W2 I8 b
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
* | m+ K5 n* c9 B( e# C' ]6 ?* m
# C1 x* ?5 @2 L. qyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
0 g( K9 ^. w) [. }( W, tmodsecurty依赖的包:pcre httpd-devel libxml2 apr7 a" l$ I% ]5 q" `
! N. n2 V7 u; ^1 V# u( _& U4 xyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
, m B9 \5 l6 R. H/ L3 p二.启用standalone模块并编译
# p, I& k M' i9 g8 Y# x, s3 M1 ^) i P) M5 R* ~
下载modsecurity for nginx 解压,进入解压后目录执行:- i0 O( e" ^' _, i7 |) x8 T
- \# |3 h) [4 E* k8 r./autogen.sh( m* o f+ H% S4 j( J: W$ I2 ^
./configure --enable-standalone-module --disable-mlogc
" {0 ?8 L3 b6 e0 Omake . q: @0 r1 J8 @2 b W) G7 y. J/ T
三.nginx添加modsecurity模块
: a. m, M' @# T( y8 i3 M7 h" E* s5 k$ E( [
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:% ]) @+ p. n# ~. y7 e
9 K) E, s/ j' J./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine/ @$ [& v- S' w! v; Q7 V9 S7 d6 j
make && make install
7 G* I) X7 m5 z) y& v四.添加规则/ u8 W0 E& K/ w
9 K* a5 h6 f& a2 E
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
1 S' }6 W' Z8 D ^( T s* r e- J" V0 I$ b8 @; j n6 @
1.下载OWASP规则:
- a) o+ y0 N6 H( M, o- }
5 |; G1 i! Q4 x: F3 Egit clone https://github.com/SpiderLabs/owasp-modsecurity-crs0 m7 J3 e2 \8 }9 {# W
, y; B8 T) S& r6 Y
mv owasp-modsecurity-crs /opt/tengine/conf/ h0 U) B9 \& ~2 U, p9 c1 n% s
8 L$ j9 ?+ v2 h/ e
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf. [- i1 J" _" K. z6 X7 [
2.启用OWASP规则:+ t! |- f; a5 G# R4 Q
' C; ~5 Y, ^! P' }8 g# v7 \
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。' K) X( N9 p/ ?% N! Q S
* Z9 k" _3 ~, i/ \& R7 K
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
1 ~: h$ x2 @; T6 q- Q/ d" z; S+ _+ g; A6 B, `
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
l% ^+ o" Q( G7 Z! }. u' l. K5 H$ w6 @7 k6 w" j
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf! y f( [2 M H, X4 ^
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf9 x. X1 s% T/ B" k. A8 X
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
1 T% d' t2 H* u. ^1 o OInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf) c7 a; [- R @* O3 y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
# h9 Y2 Z, T) Z3 Q4 WInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
8 Q! T) V; X9 [1 hInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf1 \- @0 a8 j. w/ }% {* Q$ r& j5 n
五.配置nginx
& k0 @( G& c2 |5 d6 p+ q5 i/ }
% a' o$ `, V6 y' h$ N- Y' L3 d在需要启用modsecurity的主机的location下面加入下面两行即可:( J0 o# a& S) S, F( g, X+ r
* O# b2 V" m# A) g2 v0 lModSecurityEnabled on; , y) J- A. W' E. _+ @
ModSecurityConfig modsecurity.conf;: k8 k# u, ?& f# N$ D
下面是两个示例配置,php虚拟主机:
; n5 J; T' ^% L8 N- u6 C& ^. E3 j5 Q1 j+ `4 W
server {
8 [( R# J7 ^' U listen 80;
- D+ `( o" D& `" @ server_name 52os.net www.52os.net;
' u( O3 ^$ [5 h8 L( F% k$ [
' v; I* P! z# _. ^' u9 U: K location ~ \.php$ {
" D7 V0 n% B8 I; C; N ModSecurityEnabled on; 3 o' j5 s8 Y# ~, I( b8 c, ^# g
ModSecurityConfig modsecurity.conf;, u/ y# {# w0 f# `9 v
5 l! e7 U, g) i
root /web/wordpress;
! v6 @+ w0 e1 z2 F' j' w index index.php index.html index.htm;# P$ Q8 g$ \# I1 B9 ?6 L' J
q" }6 s1 M7 o5 |
fastcgi_pass 127.0.0.1:9000;$ B. h$ ?6 F% q: g
fastcgi_index index.php;' `# o+ ?) ^0 R/ g K4 j" R/ `
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;) s6 k1 r) b& [+ N
include fastcgi_params;( |: M. k% F5 S: E! n
}) c$ n: F% d: d' \
}
$ |+ w& {1 _- H; X' U' Xupstream负载均衡:
$ Z$ P# d5 y6 p$ t# Q( w2 n. ~; o# M9 @. r, m+ I
upstream 52os.net {
+ p8 s5 [" a: @! E! r8 V server 192.168.1.100:8080;1 H, k8 j" B0 x/ j; t
server 192.168.1.101:8080 backup;
* m" M+ a# W8 a! ]}
# {5 J! S4 P8 |3 [2 J; [9 k) V; w% [% h6 S0 e% E
server {
' A/ e8 s6 G! j. X* y2 o% m5 d7 [listen 80;
& `0 {" Z: }( k9 z4 F: w, Rserver_name 52os.net www.52os.net;% L2 Z5 ^) e% q, u0 Y) w0 g
. P( k, _8 d" ]& e' d# a! m F
location / {" ]5 K( Q3 q' X# I
ModSecurityEnabled on;
/ K# y. j* L6 q. i! ~+ n% X3 | ModSecurityConfig modsecurity.conf; & B9 u' P) m! B) \& H. _/ L2 r
# Y" R; u+ x: W* x9 Y5 u
proxy_pass http://online;
# L0 @3 j V) J9 Q8 x proxy_redirect off;
. {4 k4 c7 @2 u9 H proxy_set_header Host $host;9 f6 q: _- g9 R( @, [6 L
proxy_set_header X-Real-IP $remote_addr;
( q2 a z6 j y( V proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
7 T$ v/ `0 C3 ^( x9 j }$ Y6 ?, \% d+ ~! J$ A
}; {5 `9 Y" o9 R( M
六.测试
9 d% |% _& |7 D! r# \5 ]2 s! j9 Q& @) J0 x. m
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:/ q; Q4 T% `) L& h) g7 d! L
+ v V* z5 y0 H$ v$ o& h
<?php
/ k. p) V2 J* r/ f" j) M9 j phpinfo();
" a- n" n) b, N" u?>! u# }. w+ [% K8 T- l! i
在浏览器中访问:
& Y" Q9 M) A3 @4 l/ V& B( Y. R8 s
http://www.52os.net/phpinfo.php?id=1 正常显示。
' h& p2 Q: a- j% L9 C' Shttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
) A1 F- N& B5 w4 ihttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
5 v- x! Q& \/ Q- \: @2 X8 ^! n说明sql注入和xss已经被过滤了
7 P8 w( d' f" v ~2 r* | K2 T& r/ t/ v' k
七、安装过程中排错; o0 }5 _ B! D# {9 c0 m& }
, ^& D( C& ~2 ~" h1.缺少APXS会报错; s- W) U' Z3 g
1 J1 w; @9 f, A: z8 e( }) Qconfigure: looking for Apache module support via DSO through APXS
. x/ A9 m1 D. e L$ Fconfigure: error: couldn't find APXS0 n7 j- S# u( G) d
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。3 P! w/ h: n0 J* |9 ]
解决方法:
- o9 ?& v% }9 t& C& T
6 Q) V2 ~ v$ J, u6 k) {3 Wyum install httpd-devel
) s6 ?8 c. d4 O" i2.没有pcre/ C% q6 M1 b3 S5 `; |7 M% _
5 e! ]& @3 N! L5 ~5 ~& q! L
configure: *** pcre library not found.( T( {$ R+ ^) d1 Y/ ?: l; d1 q
configure: error: pcre library is required
2 ]4 Y- I; |" ~3 E5 g解决方法:
) Q* _- R) U8 h, F7 ~2 a4 X" `
6 q) ~9 D2 ]1 ?$ {7 B5 l% Byum install pcre pcre-devel, `, m, ~2 u D& g: n4 ^
3.没有libxml2& I. x: R5 ^7 z" H- [9 U" U% e% Y
( I* Y) Z& x7 |0 V; E }# ^( E
: W f% Q. J' s" X9 Sconfigure: *** xml library not found.
( `% M- b5 G, N4 w# X# ]4 Tconfigure: error: libxml2 is required
9 B# p' a; Z4 L解决方法:
) D& m a2 c& r% s
0 Q9 c8 ]( [ x6 v* H" Hyum install libxml2 libxml2-devel
8 F) m; J, [5 ?0 ?1 X4.执行 /opt/tengine/sbin/nginx -m 时有警告
( g# s% b s0 R7 Z- d1 h# l; P
+ P" s) L2 w& |8 |Tengine version: Tengine/2.1.0 (nginx/1.6.2)4 j5 b0 c6 J# G* I% M- b r
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
' e/ c- b5 w; E9 C% Z6 j7 {0 e: G7 J& @原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
' l6 g- A D2 |& g, K" `. f# s% T( t3 {5 o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
6 T8 L( E) H6 X( @5 s% G8 C5 @/ X2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"- `5 m3 {) ?4 \
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
' J! A( K6 h$ V4 c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
: B/ W! K C2 D) h- D* e* H; \2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
" x" z$ m- ^# z& C2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.* G* S/ F7 W. O: p
解决方法,移除低版本的APR (1.3.9)
6 }+ u5 z+ N: h3 G0 _& I- E2 K0 U" x# M3 s+ }4 e
yum remove apr
. Q& P4 J* m% h& Y" I0 }+ L1 b5.Error.log中有: Audit log: Failed to lock global mutex
6 D& |3 c4 I }4 c5 @8 ]+ k8 p" ? ]4 M2 I5 Q, o+ i
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
+ Z6 m! C. ?" ~3 o+ h8 w* p! _global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
' P4 `+ ]" h! [解决方法:
- ]# M+ e, l* H* d0 n0 h7 E编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
4 ]7 i+ L. B( M& X; @, ^& y
% ^! O' ~ }0 @( F& vSecAuditLogDirMode 0777, Z3 C' G! I6 ^" D6 p2 S8 v0 i2 G2 }
SecAuditLogFileMode 0550! M" f# W1 Q# v. j g
SecAuditLogStorageDir /var/log/modsecurity
9 q/ o9 d+ p i$ T& v5 iSecAuditLogType Concurrent5 d# a4 B9 f# ]+ Z5 V0 v" g
参考文章:$ f' D. G5 Q( ?; L( P% E: \
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX$ t( w- ]% {8 Y: @/ v0 i8 P! w2 _
http://drops.wooyun.org/tips/2614 |
|