|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。' N, z! g; C& x7 Q
9 [6 E: Z( }1 p- G/ C一.准备工作
! o# ?0 H% n4 ?* b( P2 l k4 i* _+ l* p) |9 T
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.08 a0 H2 Y6 r2 S' S, P$ F( s
! k# V" N `" [' G. Mtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
" b+ e" B/ o, J; Z
: H3 ]" k) Z; Q) i2 a& d* ]$ ~modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz) ~, D3 z- N/ r# ?' \
. K% y( s+ p5 K$ l
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
5 B' d$ A$ b! ~8 O& }8 }1 R1 a% I: w1 V! n
依赖关系:
2 c+ |; p: X# }tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
! x# `: \$ M# u* n* n) R( C9 Q
2 a9 J9 ?5 e3 [yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel$ k2 ]8 Y9 F* v; p# O1 y0 I6 t; Z* t
modsecurty依赖的包:pcre httpd-devel libxml2 apr n7 E# C) G; ^; }
$ b3 R$ p) F8 k3 S, K1 o
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
9 g; ~) {/ [0 f" T二.启用standalone模块并编译
, b& q- j+ z0 a \ |' x/ l; N! w; U. [* q9 d5 |; F
下载modsecurity for nginx 解压,进入解压后目录执行:
2 Z) `1 A) T5 w. v u9 r
4 f; H, K; }1 ?+ k8 X7 l1 Q./autogen.sh
# o; M0 Z, m' g1 w./configure --enable-standalone-module --disable-mlogc8 |; H( @7 T% [$ J) _
make
: z4 |' N% j3 s5 F1 d三.nginx添加modsecurity模块
9 h. E8 v3 s6 E
* U! g8 c1 p7 {6 O在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
4 }$ K& `! q) e3 E
, J! D8 K" o5 {' _# J* V./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
5 O' R- L z: h3 z$ R" nmake && make install* u5 C7 R( w5 v1 _/ v+ S8 s2 a+ U" {% G
四.添加规则7 w; `( p! l) y1 B
; ^# [8 E! E! b) a1 k" ~, Pmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。7 b8 v! ?: d- e& A
3 J% V; u, d# u, Q# d# y4 e4 m1.下载OWASP规则:* B0 F% S4 f1 u5 w& j( q
* {: Y7 {/ `$ ^git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
7 x# x2 X$ q: o9 ^( r% r! W" w3 q. b) b. T$ r+ E+ j; t' \8 e, s
mv owasp-modsecurity-crs /opt/tengine/conf/# T- e! e; y, m
2 }1 |, ^; e. K7 l+ Lcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf% O& E u" ^- |
2.启用OWASP规则:. o. K7 q- M; D$ `9 q, E& y
9 K& A! @0 b# [
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; [' k! O% }) d& I" e1 A3 P
$ z; N8 M* N$ ~+ y编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
0 F- J% @5 n+ i n4 `
$ W, U, u; ~- n2 t+ dowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。7 p8 P+ x( h7 ^( M8 I2 Q
# n$ ~! D0 O. e
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
+ G3 z& S9 U3 w; p( H4 L, ~Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf% J7 ~% u& d) _6 Q3 J
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf% u4 o0 X- W3 i
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf H; d# }' h6 [- N
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
( D& \1 U+ q8 a" k& e/ [) xInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
) n$ K1 L1 t. g; Z7 UInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
* r- c6 T: z+ P# h- Z) ]) X |五.配置nginx0 ]4 j( J# z6 s9 @4 }/ j8 f: i; Z
h ?- h N" B; `7 T. V) m- K
在需要启用modsecurity的主机的location下面加入下面两行即可:
- E6 x1 R$ G j0 Z' Q: H4 K9 Z! ^1 y
ModSecurityEnabled on;
3 o. c e. G% {" w+ Y" {. |ModSecurityConfig modsecurity.conf;
9 W ?& h g- n0 I7 M+ ?! B下面是两个示例配置,php虚拟主机:9 w5 q! v- I; V, G* s
" |6 p0 L4 u4 G: m
server {
7 r# m$ P7 }, @2 P listen 80;9 ]8 h! R0 m. U7 }1 {8 }
server_name 52os.net www.52os.net;
' Y+ N$ C2 M8 F ! b4 k/ y" p- d$ w
location ~ \.php$ {1 k2 |; ^8 b) o) }+ o W# ~9 m
ModSecurityEnabled on; ) f1 F3 H4 S5 f, d. z
ModSecurityConfig modsecurity.conf;" Q5 N. q& L2 _. N9 l) }$ ]
: T5 [$ ^0 m( p0 |9 {% N
root /web/wordpress;
! I/ m; X. V) ? index index.php index.html index.htm;
4 e! z0 j2 i }
- U$ O, H' K L5 M* K fastcgi_pass 127.0.0.1:9000;
; W x) g1 k2 Q! t _: @ fastcgi_index index.php;$ b& I8 A9 T. i- `# N% y/ M& k1 y; e, x
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;: O* R. l2 O# g# C
include fastcgi_params;2 t; p1 m; B P+ k' {$ M% ?
}
6 R" `2 Y/ q( ]/ {, O+ _6 t$ ~2 c }" L# ^- y1 F% r# {8 `% _5 l
upstream负载均衡:/ V, V9 P- @+ R. A
7 Z$ ~! j' ?- v- ^% U! M3 y
upstream 52os.net {6 p( h7 @+ o1 l' s* {8 u* i
server 192.168.1.100:8080;8 N( E. O. k1 @( \0 {7 m6 d
server 192.168.1.101:8080 backup;
9 h/ c0 E1 c: |* w) q}5 f" G7 F! O* j. J
: k* j5 _) \7 `; Q/ |# eserver {
) D0 A4 v, ~ H3 [7 B$ p5 Rlisten 80;( _/ m6 ^; u z/ b s5 N# X! V
server_name 52os.net www.52os.net;2 C& @& F' Y) `- `0 |
- Q. Y6 m, D" r" ]2 x# b. I1 ilocation / {; [* o, u. g+ C, _7 b8 A, V: r
ModSecurityEnabled on; / p' Q: v5 A: W1 s
ModSecurityConfig modsecurity.conf; ' w; m/ I8 K; \' [: w! F
+ B* q( ?, H( E+ t/ {/ V8 r
proxy_pass http://online;
, \# X1 w v5 Y% L) z2 k7 ? proxy_redirect off; S0 Z# M: x- S
proxy_set_header Host $host;( y* g; G) {$ n# U
proxy_set_header X-Real-IP $remote_addr;4 U" V) H5 y5 T) j+ m3 B U& s
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;* r2 z F7 y; ^+ g* f
}: o. k! T3 G" Q2 W8 S0 ^/ i
}0 L" I; F$ t0 ?0 U# t! f: C8 l
六.测试
: A0 C5 E ]$ I, ^
y5 y R- `3 }. h我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:( M% x. {2 ]$ K1 [/ ~$ S
! y/ Y9 z2 k: d6 p% w6 g, k+ s7 \: v<?php/ C/ I6 ~9 M3 j/ Q
phpinfo(); " `. H2 Y7 B9 R2 M
?>
; ^* H# N5 Q: j7 o% P4 H在浏览器中访问:
F8 T; _& M* `0 b+ \! @) g& ^7 s. e& {8 n1 H6 i+ P: Q' K7 ^
http://www.52os.net/phpinfo.php?id=1 正常显示。
3 T7 z( X3 y$ i4 X9 D) _http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
: S5 H. S" G+ Rhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
, Z( S: \" K1 R1 X说明sql注入和xss已经被过滤了$ Q2 [. }/ n# W* [6 ~
% ^" e5 H1 F& ^1 x3 V8 |% D) V7 C七、安装过程中排错9 U7 A1 `8 w: o6 P) L0 ?/ M: h8 B9 f
. w; y, N- f& t
1.缺少APXS会报错. f5 ] s2 ]% W l
9 ~5 S! e5 u ?+ y, Vconfigure: looking for Apache module support via DSO through APXS
5 f% ]8 B6 x0 X3 K, J' _4 Fconfigure: error: couldn't find APXS
4 \# c7 e2 J, S+ u( P" E( t) kapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。5 h7 {2 k- k. b ]+ W& x, E6 ~
解决方法:
* z% ~( U! @+ }! x L) q
# h; _6 [' i8 V- j q$ H( u( pyum install httpd-devel
+ ^7 i+ v: m7 _& J) ~$ ]9 E9 Z- q- b& X2.没有pcre: T$ u; a1 w/ W, m e$ Z" d
& [( j7 x. ?2 y) Kconfigure: *** pcre library not found.9 s, v' e7 l* h3 @* M, ^" X6 }
configure: error: pcre library is required
$ {; v3 J. q+ K* Z+ o解决方法:! C3 m/ F7 g: @5 ?
" j9 o! X6 p: v; M! u& k
yum install pcre pcre-devel. j v+ L$ v& w( K$ I
3.没有libxml2
* F& \& F Y- s @: R. |0 [. b. ?5 l
' I. Z( I" v) K9 Z' |4 X5 m, aconfigure: *** xml library not found.
' _# D4 |8 }5 g" a$ wconfigure: error: libxml2 is required
/ x: Q; U- t* I! O8 u `: }2 D5 Q解决方法:* ?) `5 v; K$ Y2 y7 T4 m* D! a
) Y6 ]/ t6 O3 u' C6 Cyum install libxml2 libxml2-devel
% n7 Y9 B% q9 R2 y8 S8 ~4.执行 /opt/tengine/sbin/nginx -m 时有警告; q a0 F5 ?6 x' P) P7 u
- i. y/ j& Q/ [
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
, \1 U1 t: p# C& Q/ F# E& }4 J. snginx: [warn] ModSecurity: Loaded APR do not match with compiled!
' W; L; f# v V5 E5 j1 z原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log* w# g9 n: ]4 w% y, P, T H
0 h& S$ O/ {5 l3 S
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.! K1 y& o$ p t! p
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9". `; n# f' ?# t/ s, H: y7 [+ I
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!0 D6 J7 }, J4 [. s! y$ R, N f, w
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"2 r( S$ Y# q r9 A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
% k& x" J! c1 F1 I+ e2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
P$ w4 h$ G7 e7 y9 x% ^解决方法,移除低版本的APR (1.3.9)( a$ A+ |+ S: P! c2 G! l' k# ~
$ j: V$ t' w2 _. V9 l7 n5 gyum remove apr: ~, a( z8 x9 v& i0 s
5.Error.log中有: Audit log: Failed to lock global mutex
, m5 S. L* ~' B
9 ]/ J8 v( w# O9 Z: V4 c) V4 F) d2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
- o1 b/ `$ ~! u" {global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
% x( k5 |2 y- N' `) {5 x+ \解决方法:! K5 s9 U* G9 T4 c; {/ Q8 }% K
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
3 L& y/ k8 `/ d4 B4 ^. K; E1 t1 O- w" r0 G1 R' ?7 C
SecAuditLogDirMode 0777; k: Z* I1 u8 |' s
SecAuditLogFileMode 05507 \ A- y. T* V, ~
SecAuditLogStorageDir /var/log/modsecurity- w$ R4 X+ ?3 e; X; H: Y; C% k a
SecAuditLogType Concurrent
+ p* v8 E# J( J- Y- Z参考文章:
1 O0 W0 \1 i1 d* E/ x& _5 r" E! @% {https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX! ^6 J- ^ M+ ]9 V! x3 P3 e N
http://drops.wooyun.org/tips/2614 |
|