|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。% \4 L1 t7 J. P5 M$ X
9 [7 H( L* `& H0 D' T# d# J
一.准备工作5 Q3 K4 j& _/ y: v7 r
/ H3 K4 _# g' R; I系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0! L/ K5 J8 z0 o* W- w9 r/ {" J
& F9 N- M( H! [, j) Gtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz, `8 {" ^ Q8 }, l2 Y5 a6 r8 ]1 }
2 q. ]6 S5 N) Q. I
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
) ?9 r4 \0 F5 [' O0 \/ ~( Y8 X5 t. L; M7 s) I# e
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs' p! d' p9 Z4 ^. [ Z
# l7 z/ |" y) r. P; U依赖关系:
. F9 V! g& r, f; ~% t6 N& s1 a2 mtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
# O- D0 D7 E8 ^4 E7 X& M$ m1 v$ c/ }4 ~" K* z; m5 J/ ^6 o4 s5 j. A! n
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel# N9 H9 ?7 d& G0 _1 F
modsecurty依赖的包:pcre httpd-devel libxml2 apr. B- W) t! P3 O: F
. v$ u T# u- o, z- g5 a
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
" ]. U y! y1 W |二.启用standalone模块并编译3 m: F, ]+ j! X g( B2 V
( l" ?+ v* d, Q( B* b A8 q
下载modsecurity for nginx 解压,进入解压后目录执行:
$ n* ], S! V. ?2 L7 o. b* o8 j- R/ q0 D: c& ^, A; S% e
./autogen.sh
* n; m9 [ y# }2 H" I% d./configure --enable-standalone-module --disable-mlogc) p" Z7 j+ O5 E* D1 ?4 I* ^
make & e- d$ c/ @+ ?& p: K* a
三.nginx添加modsecurity模块
, C; i( I( F' V R5 W; y5 P. S! D8 @% p0 d1 g
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:. K6 ?, w" y- \$ A9 b: x1 x
/ m9 \3 d5 l" O' _
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
# p( C h2 t$ W% dmake && make install
( ^, p# |2 p" m% x; @. T四.添加规则6 u% y2 x& O- ]
- X7 d; X9 Z- e! y( _modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
/ @" E* Q0 b6 a' v% o$ G( x6 [1 [6 I% q0 \7 Y N
1.下载OWASP规则:3 X! i+ n7 e Y$ X4 g/ d8 N6 o$ \2 Y
6 H& B/ T* g; k v% n- Ogit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
, }$ G/ r6 u. b' M! o$ Z: F& J* q; J/ j
mv owasp-modsecurity-crs /opt/tengine/conf/
) g7 N& O8 D) x' i) f& W* W
1 s( n* Z3 g% r' s- c3 r1 Scd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf% a0 P. [ s* s
2.启用OWASP规则:
2 B7 I* r& h$ i' R
* U0 @" K7 W5 W% T: z' p复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。2 }; S: L8 C* d; ~, Q
7 F1 B8 J U% [$ w编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
6 T) Z' l, j9 h$ s9 c1 D' r* a2 l
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
# U/ k. N: k x( ~
8 i6 {) Q" z: v# w* yInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf8 e. J, }) j( ?- ?+ H
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf$ \# m ^3 n/ j. [6 f$ K- ]
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
9 x1 w7 [& _) `9 wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
8 g0 ]8 a: }9 y0 N4 g7 `% ~. NInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf" k4 |. z* i+ O" Y& `& g, F
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
' d7 P) P0 q, I# B6 X. tInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
! V/ x5 X, @7 E7 I5 G W五.配置nginx
' E8 o7 v1 u8 w8 h, e. t: t7 z- Q% r; K
在需要启用modsecurity的主机的location下面加入下面两行即可:
' Z9 @$ T( i. o7 r3 G1 m& l
4 C9 E5 K5 z$ j: N" kModSecurityEnabled on; # O, ~: ^* }, Q- D* S7 q
ModSecurityConfig modsecurity.conf;/ K$ C M5 l" ?2 G" T: w% ^8 U! @
下面是两个示例配置,php虚拟主机:$ {, b" E, {0 q* Q5 g
: `% ~+ S3 c" v. a/ |
server {
1 J6 P9 o4 s# S T* t: M) i listen 80;
& y3 b5 J+ L; r8 X server_name 52os.net www.52os.net;
- t2 G+ I4 _6 i2 Q
7 d, N+ r' _8 n. W8 o location ~ \.php$ {7 c& W' E. B3 x: Z3 l' i
ModSecurityEnabled on;
8 U1 ]8 `! z- b$ D ModSecurityConfig modsecurity.conf;
2 I" p+ a! S; _* ?4 ^& o0 v4 x2 {4 m
root /web/wordpress;
( ?7 b, q& _; z2 n6 |( t3 e# h index index.php index.html index.htm;
1 E& C) G: j2 z' e3 X7 B
4 D6 X' n& K6 Z* M, I+ J fastcgi_pass 127.0.0.1:9000;
7 U& h Z9 k6 ~( [6 I fastcgi_index index.php;
8 |: ^. N n' V7 S fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
8 x* J& {" i2 K( l include fastcgi_params;
! B0 F# {, G. M3 g }- r* e2 T4 m& n. T$ A* M8 i: E
}
. Z; b w% z- m+ Dupstream负载均衡:
+ E1 N9 c% P3 i6 T" V2 n2 U, m+ F- B# ~ Z" u/ ]( x `
upstream 52os.net {4 O8 [4 q: R8 K9 c4 a- Y# B6 [- {
server 192.168.1.100:8080;1 _! m$ X& j7 Y7 v' C
server 192.168.1.101:8080 backup;
4 N( x; \; |. S+ J$ d6 g}' R2 l$ F& y" w% F& H; s3 [" o
6 c# F0 e% ?$ q ?" Y
server {
8 Q5 {& ]: r% slisten 80;
$ J4 G+ ]7 w6 r5 ^ H1 O$ _# t1 W; Wserver_name 52os.net www.52os.net;7 z, ]+ `5 ?+ w! A! G9 s$ l1 T
4 a, L# ~$ J2 P& R0 S- ^
location / {( c8 y: U P. C& X) t. B0 R1 {4 j
ModSecurityEnabled on; + U; L V4 i4 H" n
ModSecurityConfig modsecurity.conf; ( h% Z% j$ e- C# |8 E# `0 U) l
0 T, s5 y/ Q: l) M* J
proxy_pass http://online;* f9 ^0 a7 W3 A Z) U8 G$ {7 u
proxy_redirect off;
8 {0 ^. R$ f6 E6 s" } proxy_set_header Host $host;- P) }6 ^" P7 w" U6 h$ A
proxy_set_header X-Real-IP $remote_addr;
- p4 e" M* f9 h4 q5 c: m" n' ]3 Y proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- j5 g9 O$ i% K- v Z }
F5 ^) Z' v: m& a( k3 @( k}. }8 O) X3 Y: o/ y( W- V2 e0 s
六.测试
) c3 G$ ?# h% ?/ @' @+ M4 ~2 K0 O
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:! k# x5 g+ }6 L" y! z( C' m
- N8 P) \2 T) r: t+ d, C+ G<?php
+ B, M: b5 ?5 S5 u- N phpinfo(); ' J2 e; O) L' I8 R
?>) _& H; a' v) _1 o( x8 D
在浏览器中访问:, o+ [" \8 H" c! U; R# W6 z
$ x! G# y# T1 \http://www.52os.net/phpinfo.php?id=1 正常显示。% R" m( [) u$ d% Q! |% _) P
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
3 @1 ~2 U0 T; [% ], ~6 [http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
) H3 I; T- c' a: e( {$ r6 M说明sql注入和xss已经被过滤了
) ?- u7 d; H$ B! x% D! `7 p& l$ Z6 N; H& N
七、安装过程中排错
7 h2 P5 }6 p1 Q) q% o9 ?8 p9 a; J4 R" g3 y6 X8 [2 K# l! z: W2 W
1.缺少APXS会报错: E; R% G6 f( [/ L
. ]" }& Z, y! r6 c7 R, A0 q
configure: looking for Apache module support via DSO through APXS+ K( L$ W. i8 u% N; S+ i) s
configure: error: couldn't find APXS
+ T0 I3 A. t8 H+ n: v7 Iapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
a- m. Y, x/ K$ |解决方法:
( }; h0 \5 b4 Y0 K; `
- U# y! `. F4 w7 d. K0 t& ^2 m g! `yum install httpd-devel6 }& m) x i, x; p9 B
2.没有pcre
3 U" C3 O7 P4 X; ]! w! L8 ~' w# J, T& y* | @- }
configure: *** pcre library not found.! j' v$ L; e8 I1 P: U8 Y
configure: error: pcre library is required( o# N) ~, v8 O$ l, |4 Q9 M
解决方法:
/ h' l+ `) a3 q4 `6 V" @' Z
/ e. g6 d `# Q: {- `/ qyum install pcre pcre-devel" n" _& L4 ^7 Y
3.没有libxml2
, y% q) c6 O' y# R. j
/ _% C! O9 r& _3 B2 y" G3 _" F1 d) _( Z3 t" N
configure: *** xml library not found.
9 S4 K4 X& m/ h6 @: wconfigure: error: libxml2 is required! r; Q, H5 f( o
解决方法:8 J- y2 a* O2 A2 D q
3 U' m# D. K0 F3 ?9 Xyum install libxml2 libxml2-devel
! v+ y2 s% D# {7 X. m t+ {3 r# k4.执行 /opt/tengine/sbin/nginx -m 时有警告: @" }+ i$ z3 m# N
& w0 Y1 J5 d& Z& wTengine version: Tengine/2.1.0 (nginx/1.6.2)
+ a6 r* u/ P9 b! C) Inginx: [warn] ModSecurity: Loaded APR do not match with compiled!
& Y# f! x4 R2 ~8 O6 K3 l, x `原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log6 `& k G0 Z% C3 T/ y! J( E
# B h! I% p# c/ a# e4 T, w2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.( s4 q7 n4 ?- D8 R+ K: \( J
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"7 B, S8 J! R# ]* Z2 ]
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
8 i! }5 w4 d8 g/ X2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
) `2 h# P$ E; @. G% ~' q. d w2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"/ ^8 G2 f* ^2 r) t
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
3 [! Y% f: L" `解决方法,移除低版本的APR (1.3.9)! d; U/ |% \7 l. L
. a$ ~. Z O1 `8 w4 \+ w$ tyum remove apr1 H. o( {; B6 P7 L: a6 [
5.Error.log中有: Audit log: Failed to lock global mutex! }* H/ A( q& G3 A1 ?
# b, F% W/ g- R; a1 M2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
- g& u ?: i& V; j, [, mglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
! ^2 l4 K. o( g* h, x解决方法:/ p$ o6 [" ^, \! p5 K8 U
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
6 G" t- \. h) `( f& X8 }
! {2 ?8 @/ Y5 d& M& R' Q) b) @+ ASecAuditLogDirMode 07776 n6 w6 t) ~% G& s2 f
SecAuditLogFileMode 0550% v8 e- F- I( h) j; E% g
SecAuditLogStorageDir /var/log/modsecurity
. j1 f5 G$ G1 O B7 QSecAuditLogType Concurrent
/ s9 h8 n3 o& p参考文章:
5 |: I2 Z/ J; Z- p% s& _https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
x c0 S5 }) A! s6 W% o2 {: R4 {http://drops.wooyun.org/tips/2614 |
|