|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。7 t& {% W8 S0 S, q- h5 s
9 M, h& G9 m3 W! X" K
一.准备工作
9 ^5 ]! ^. s$ H7 Z7 Z. d- f" m. B9 I: E5 [8 B. q* ]* w0 {' b
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
& S0 R, V- O, @! a
$ C' c1 B' @( c7 Z: d* ttengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz R, w7 f+ q, t/ { F
% R, d7 S. p8 o) n5 F: }& }) \
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz0 e- o5 a; B" p1 C* x; g& {' O
+ R0 y7 T2 ]( f+ H3 R
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs4 l2 R9 E) A I/ W
7 }1 ^5 o3 L5 s% H4 U8 c
依赖关系:2 P* ~3 b+ A5 z$ Y; j
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:5 A8 H8 d, B! q( n0 t
: f$ j# n8 t1 ^/ u4 F s- i
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel9 n4 o& u+ g7 q2 |) F. {
modsecurty依赖的包:pcre httpd-devel libxml2 apr
" e1 H, p5 M- `6 Z
2 e+ T4 d5 X1 K4 [# ^$ F% gyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel4 o U8 X9 p4 b$ h+ j* _6 {
二.启用standalone模块并编译
2 N$ X+ R5 E/ K# x3 |$ B5 e e. U# Z$ A- B u+ j9 A" E- U" e) Z
下载modsecurity for nginx 解压,进入解压后目录执行:
% t: ]. ~6 j" j7 p0 f' J |6 |! d- y
! x6 p$ @2 M3 f: t) w5 ?& B9 n% }# B./autogen.sh
7 v$ m$ `4 u. u7 [* j& p./configure --enable-standalone-module --disable-mlogc6 A, ^5 [7 [5 Z4 ^9 b3 {# Y) D
make
, O* ]0 T, U% s: x& t三.nginx添加modsecurity模块! r; }9 i5 ^& p. I
0 S. f) r# A, a: {* _在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
7 R( j& p# N" ? I
D% q& N, k: o6 l) S. W./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine2 z9 C( H8 n8 ^+ F5 \
make && make install
$ l K6 c! F! W四.添加规则
5 x% ?- _. e4 F3 ^* N) ~# ]) S/ [8 I2 T
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
0 ?/ n8 ~$ `6 B
9 d1 L1 Y# P) S6 e' u) b1.下载OWASP规则:
8 I5 @0 V: y) q
- ]+ }8 i& \" Jgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs* _( E7 c9 U3 S* r/ {% [
. }; O- j" z# I- `: b2 hmv owasp-modsecurity-crs /opt/tengine/conf/
4 k% A0 J$ ]8 A& p/ ^ r. |7 u: q) a E' s
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
( t n4 S& Q" v4 b2.启用OWASP规则:4 Q3 S! ]9 s7 o. S {# T- @; H- b
- f3 U O5 U3 K- @
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。$ ^9 W5 [* s3 z ] o1 w; j
- g0 e* E1 F0 ] P
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
5 q; [1 P7 N. Q& b" {8 i
4 p, V8 e" v1 ~& Nowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
$ y. z8 n4 _$ ~ E; \* |. R f6 F
! ^ S; ~! o# H4 ^3 }Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
7 ~* z/ P/ x1 H' J z2 sInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
1 w- I$ v4 Z8 M; g/ _; jInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
& L7 V- i; T3 A/ b; y, Z @Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
! g" H" v- t$ fInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
$ }, ^4 f! _, dInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf! k7 j6 P! w9 s
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
& W. l# s7 p1 t6 C3 K9 X五.配置nginx& ?2 r( Q7 D2 p' z: ?
. l/ n& c/ D" v `/ g6 z
在需要启用modsecurity的主机的location下面加入下面两行即可:: n5 V$ x2 k/ |
9 i7 x, e0 _( b3 dModSecurityEnabled on;
' Q0 _+ L, T7 QModSecurityConfig modsecurity.conf;, p+ R3 K8 {, h; m/ S) `+ C) Z4 P
下面是两个示例配置,php虚拟主机:
" V, W8 I% V d' Y e6 r) b1 P
4 ]# m, Z: M7 Q& u7 u% | iserver {
+ r ^- D: d0 d' e1 Q listen 80;
# J7 @ k/ P+ c. C0 X server_name 52os.net www.52os.net;9 Q* ^1 z* @& P, y4 a6 Z9 ^
0 g& u; `- E) r2 p! H location ~ \.php$ {
" x0 n- O6 q. ?1 B ModSecurityEnabled on;
* [: L# P8 E z1 b* h ModSecurityConfig modsecurity.conf;
' Z) @( O8 ~; P! E5 m& @( D; T
. H a: o9 Q4 L; U b! t D* ~/ R root /web/wordpress;7 v& H S2 W$ z2 R6 l. x
index index.php index.html index.htm;
2 O ?2 i; ^8 W' ]0 b' |( h- { 7 G8 z, i) u! m) {
fastcgi_pass 127.0.0.1:9000;
- T, T. K$ I. t0 O- l+ l' _ fastcgi_index index.php;
1 O( [; f5 A0 [' ]* z8 j" G1 E0 F fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
' n2 h' x' G; R( k include fastcgi_params;) P" N4 T5 Q' L7 U4 m1 _
}. C! | i0 c/ G" O
}8 g5 d' ~ `# g; e8 u
upstream负载均衡:
, P1 s/ I6 d. q" k7 y8 J( |$ I4 x" a0 k( b+ j
upstream 52os.net {
8 q5 O2 l$ X) X8 l# v server 192.168.1.100:8080;
4 e) `+ Z/ A+ e! v& {( Y! T server 192.168.1.101:8080 backup;
( J! M& \; u. D}1 k; X* \* F: b* R& {$ k; H
8 P; Y/ h- U+ r P5 `/ H
server {8 t1 e0 Y. U/ e$ C$ I2 y8 i4 A
listen 80;
9 i7 k$ Z+ \) xserver_name 52os.net www.52os.net;" T. }& m- ]2 b4 `
: U* g; U) e3 f @: r* _2 k$ F
location / {
- F- A8 A5 |' }0 G( R! ~ ModSecurityEnabled on; 2 A" b% z g t$ J" D2 r+ |
ModSecurityConfig modsecurity.conf;
, H9 T8 e, s7 j3 Z; q, n% Z; ~3 h
& E6 f2 f, w" m8 a! R9 P proxy_pass http://online;0 E1 X( n9 f! h& J3 q6 I0 w
proxy_redirect off;+ [9 o1 `. u' W1 j$ u
proxy_set_header Host $host;
4 g' a2 a0 t) Y% {% R" R proxy_set_header X-Real-IP $remote_addr;
9 P, y& T2 m0 k& ` proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
5 T/ ^6 C4 o a" y6 V1 g$ L }7 C- p4 c, T8 W! i% g6 p1 v
}2 @4 l6 k* ]# W \: k3 [& Y
六.测试6 }. I1 @) ~& q0 T1 `" k
8 Y- M3 |# G# D1 r. u& `
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:; X6 |7 s, N4 i. L+ W3 ]
) [" d# o6 Z4 ?0 }4 G8 R<?php+ ~) q" C0 @8 H: O4 u
phpinfo(); 0 J; M. b! j4 ~: h: u; y, G/ F" c, ]' B( }
?>
) Z' J9 Q, z. S8 [在浏览器中访问: {' s# G! ^1 {8 U% Y1 r+ ]! d0 T# F
, o+ n: L, |* b; z2 _8 P
http://www.52os.net/phpinfo.php?id=1 正常显示。
; y! `% G! K2 y' Q; k, o5 J: {1 b% E! Xhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
/ n9 J* N( m1 L& p. F9 phttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
* a: A: T) C2 n$ k0 D1 C说明sql注入和xss已经被过滤了( a$ ]9 _/ A; U) ~# Z# A! s
) G$ k9 x9 } |1 ]1 L, U! [七、安装过程中排错
% G$ L0 o6 h- X3 c- ^
l" D2 Y% r; r6 l% \1.缺少APXS会报错' v; i: C- [3 x. ?: i
4 K8 f# |0 N9 |2 g7 aconfigure: looking for Apache module support via DSO through APXS2 Y! ~. Z* S7 H3 h
configure: error: couldn't find APXS
2 T4 h7 i% U. |4 iapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。' o m+ F( N! I1 U, c: F1 y
解决方法:
7 p9 K0 q! b; A) L" p% i' x0 X. j2 Q( H: }) S6 |* O
yum install httpd-devel: G; m g, A9 T+ X- V* _" i
2.没有pcre0 b5 E4 B' ^3 @# D1 k
$ N# w" c2 q& W( j
configure: *** pcre library not found.# w! k& A/ o7 @
configure: error: pcre library is required
& Q% m( {/ C2 v, a( g+ H0 w解决方法:
5 R, R/ {/ o, W8 u, ~7 y. }, v1 O8 H7 E2 k. x7 v
yum install pcre pcre-devel
9 l, E" g/ J, l3.没有libxml2
) A( E, e% X# c5 h5 I4 C M: e9 B1 O! S! i2 b& ^
r' O! \3 I; C) q2 Yconfigure: *** xml library not found.
& K. y h M0 _( K1 M& pconfigure: error: libxml2 is required
9 I5 p3 W! C; s/ L; A解决方法:' K% j5 S! p' z( \+ W, |$ Y
; \5 o/ O2 d& ]8 L8 P) kyum install libxml2 libxml2-devel9 ^2 Y) Q; z2 i. Y( D" q
4.执行 /opt/tengine/sbin/nginx -m 时有警告
* v% U7 f; C `+ X
! f0 v7 r1 C1 L+ b' @6 KTengine version: Tengine/2.1.0 (nginx/1.6.2)
/ }" n# A) |+ Mnginx: [warn] ModSecurity: Loaded APR do not match with compiled!8 K& O$ Y, f7 D5 _9 g* R
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log; W. h8 W; Q/ b. t2 Y, @( \
/ S7 \7 y0 g: O, O' v
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured." V: S$ N7 S. H( h9 D% H
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
4 F1 W7 s: W/ C- ]4 W1 u2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
( e1 U. K' I3 [3 J2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"" }. j" H, ?8 t1 r/ _4 A8 A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
9 @" w; ?$ j. _2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.2 Z9 V5 D1 |7 g {4 S
解决方法,移除低版本的APR (1.3.9)
) p% B1 r8 _& I r" _# @$ d4 c' x5 y! P( ^: X
yum remove apr
\/ }' a. F8 V# \& n5.Error.log中有: Audit log: Failed to lock global mutex
: s( F- A. m& R; U% h P6 z* D f6 b1 C) K1 g
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock / j# ~: y1 h# o8 F* c
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]1 w3 S1 Q3 o H/ T& l, i
解决方法:
/ X. t! t4 N; H$ {1 @编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:1 }/ w/ Q& Z/ Q/ y8 e- P
% A1 `+ h5 y' }- D' HSecAuditLogDirMode 0777
9 |4 x7 x x) L4 Y2 aSecAuditLogFileMode 05505 R" B7 C& n9 N% c
SecAuditLogStorageDir /var/log/modsecurity2 x. W; g0 P6 R! i
SecAuditLogType Concurrent
8 D" U- M5 y/ d参考文章:
+ I9 f+ N7 C; ^https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
0 c) }7 ^3 r1 s* A2 ]( lhttp://drops.wooyun.org/tips/2614 |
|