找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11708|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。) m' D3 g3 r, C) g! A* U2 @+ B7 t
: L  i9 X% _1 b- a* }9 k: n  |
一.准备工作- V. H/ ?& G9 Q3 l6 Q4 ^

- t, z6 Z% n5 ~$ q! \6 L( v系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0& @4 o6 x1 w. ~. S0 }+ O3 D* u
+ N9 r# y  i- P: T7 P. b
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz* O4 ?0 Q4 Z. J8 V1 D

" H- Q; W9 b( L- wmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz/ ~6 z* B  [9 H1 R
/ L5 j7 ]: a: ^8 b7 B
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
4 b- i) X0 J0 @$ d$ K7 g  N! j* D; P9 o
依赖关系:
" x' ~5 `3 U* stengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:* ?2 p, U( `+ Q( W0 R, G
* B. C4 q- @& `# J* U9 _9 z) o' |
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel$ b  U  M( Q/ c. H3 S: J; o
modsecurty依赖的包:pcre httpd-devel libxml2 apr8 W9 |* q3 D  J! N
6 W( p, W7 o3 p- x" ^. e
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel9 s! o* `2 b) K+ Y; K" L# J1 C
二.启用standalone模块并编译
$ `0 P) A  U4 g7 L
: T, l+ R  H! A4 r5 C下载modsecurity for nginx 解压,进入解压后目录执行:
3 G) Q' M/ U$ i; N( s& O4 R2 M8 Q1 _7 a( t
./autogen.sh  b8 H& Z0 s. `" x
./configure --enable-standalone-module --disable-mlogc, V4 Z6 l  u+ n3 R+ V  g1 d8 N0 Z# H
make & e! T/ N) g7 n, c( r
三.nginx添加modsecurity模块$ W; c: f) J( m6 w, k+ {
8 N# X. i+ B$ X  d
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
5 f& }- d: y. X+ D5 h- O! ?
" y) H( Z/ j& O./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine3 a) H0 b. u% x* Q6 K* `% X& \9 u
make && make install
- e  @* _3 W, E4 Y' ]四.添加规则
3 e% U8 i7 Q3 y& i/ o
0 y8 j2 m. t! Pmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。& t6 m8 i# u+ Y  a; ^
9 G2 G7 D0 l4 K  K9 q
1.下载OWASP规则:! h) P! O5 h7 Z% S

) C, u8 u3 ^0 I" ^3 ygit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
1 }1 M3 H7 Q! i. Q! z4 i4 ?! G0 w* V( |1 G- [7 L9 A- ]8 v
mv owasp-modsecurity-crs /opt/tengine/conf/! S. i5 d# e( t* m/ t
- O9 u% u1 J- P
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf/ Z0 ^  l; H9 v& S
2.启用OWASP规则:, }0 q; A2 z! \0 P

) v  X& i" ^, v! C+ j& k复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
& k) a5 Z& P9 W% ?" s: K) j$ K& x' Q. @6 X. c/ _
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on4 t, q3 k- S) `! w. s$ Y7 O
) \! C; V  B0 x' E9 g: j; S
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。0 O5 h& o% g9 @

! Q$ _% c& d% A" ?Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
. d1 ]& f7 L9 e' ?/ d" g8 jInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
" n) r; M7 z2 j3 h2 uInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf) v8 e1 L+ M$ {2 f6 [
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
( s+ {4 @  M0 r# JInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf3 E( B1 r8 z$ S- x, O: `
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf. p  K  v1 }, j( I+ B2 i
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
% c( _, E  ~. J9 {, t) ]. q+ Q$ [五.配置nginx
' ~6 N) M" I6 A3 B3 \: S- M) d3 _" \' ?2 y  j9 J9 B
在需要启用modsecurity的主机的location下面加入下面两行即可:) A$ m( k7 d* N. _- r" U$ H

" f/ s  f8 j4 ~6 f2 K; qModSecurityEnabled on;  
; `0 z+ K3 |1 S. C7 C- j! gModSecurityConfig modsecurity.conf;: G" S! G3 o4 v
下面是两个示例配置,php虚拟主机:* W* Q) P- r  _: J. k. n
) i. l+ O3 s1 W2 m& ]
server {- |* b- z% i" {- z: q
      listen      80;0 f. [, V. f5 V0 w* E
      server_name 52os.net www.52os.net;
. Q% Q; C! C: v5 `     * {* _# H8 P; W/ C0 I6 H* P
      location ~ \.php$ {
: n. m! Y: h  v" r' g; R2 R" t/ K$ v3 F) p      ModSecurityEnabled on;  
4 X( }' s9 X; h, s* Y6 l' F8 G5 s5 Q      ModSecurityConfig modsecurity.conf;
# K$ I. C* a' u: X7 [+ P* p9 [
; P) k  D! j1 M$ ^3 ~2 ~  L      root /web/wordpress;  |# o: Z' g6 \& u
      index index.php index.html index.htm;
/ c* Z# o! N# S3 Z( ^4 |  : Y; j& P! [. o% d- Q8 X
      fastcgi_pass   127.0.0.1:9000;. C7 i0 p) l' Z3 y3 |( Q: O6 Z& ?* d
      fastcgi_index  index.php;
, p2 l2 ?: G* J6 n7 U+ n; _) p      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
# v% V$ I# b* S& U      include        fastcgi_params;
# p" d. b/ E3 d6 d" [1 E8 K- M1 ]      }% A1 L% i. P3 ?4 f
  }3 z- E$ a. G6 u6 k: t2 u% K; F- a
upstream负载均衡:
: B9 h- n/ y' g5 u, Y, K; n: a' v! J& D6 m( f& H) U! @
upstream 52os.net {; [; b1 }: q+ J6 _
    server 192.168.1.100:8080;
/ p$ a, J3 H6 w8 y    server 192.168.1.101:8080 backup;) v! t8 P* Z& M% u
}
3 d3 A) x1 v. r" o
+ L$ q- Z1 E6 k( [3 H- Q' L  J! Tserver {3 Y# g) ?% A8 q
listen 80;$ U8 i) m: |2 s  r+ [* u- ?
server_name 52os.net www.52os.net;4 X( |1 p* z9 D( y5 F8 `9 T% S
- ]+ Z3 T* ~- X4 h0 {6 `; u
location / {7 @: F$ e% f, A
    ModSecurityEnabled on;  
, L3 K6 M7 d! N0 }. J- W) d5 k    ModSecurityConfig modsecurity.conf;  
/ e2 B) W  U, T; X; y3 S' a7 a9 J- }* d3 m9 B% h
        proxy_pass http://online;( u2 M$ P7 o5 P! _) v3 H, E
        proxy_redirect         off;2 K; h; q( J; H& i3 B1 k( M
        proxy_set_header Host $host;
3 _, k2 Y. P$ |8 h) y        proxy_set_header X-Real-IP $remote_addr;8 D; o  U& ?7 {7 e3 w
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;! s' u9 f9 A5 D4 m1 o" ~
    }
* e9 m8 J6 ^  |( ?: {6 G}$ a- a( r4 D/ h6 l8 V# c: p
六.测试
( `9 z# d& k& u- b8 n( T7 X2 P+ g. ^+ y; A2 _, }  s* R
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
$ @7 m+ Q* C) D
+ ?) S& d) A! w1 C<?php/ ~$ l* J; ]6 J, \$ s: a: {
    phpinfo();    ( {' {- A- g3 d
?>
9 v0 v" e9 j' \( I# C在浏览器中访问:: m+ o/ J$ V: V

( J! L( G4 u$ Z" n2 Y. X5 Dhttp://www.52os.net/phpinfo.php?id=1 正常显示。: X. K8 N- |9 \( T- @* n3 v
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
& J/ |( O) o! @http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。5 M6 l! D1 t2 o. C9 n
说明sql注入和xss已经被过滤了. M+ n; l+ ~  W2 Z/ V# L
! C9 `( u; E' ?% d; K: M, o
七、安装过程中排错5 n! R2 ^/ t3 o

$ B! W  Y  u8 f# B% Q8 D: ^6 N) X1.缺少APXS会报错
' \% E* }$ |0 U( M. T9 E9 w3 l$ ?. v' P7 F* I* a
configure: looking for Apache module support via DSO through APXS! M4 p' z& M# a* d
configure: error: couldn't find APXS
# ~' J7 ~: v; S' Q# Qapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
! J2 U7 u2 O4 b& i8 t解决方法:
" o# A1 ^" I, C' m; f: Z& K  c: y' g8 ?$ ?8 H9 X$ j, N! X
yum install httpd-devel+ F) a# @! D' {/ ?/ [1 L
2.没有pcre
$ [: K. K% G1 M! {3 _
: Z: n5 K- E, y$ S0 ^+ Cconfigure: *** pcre library not found.
9 x% i: T8 Z+ k! Q, bconfigure: error: pcre library is required, o8 u4 Z- i/ {& L# e, ?
解决方法:3 Q' R6 U9 J# Y

9 g- L! }3 a; y% ~yum install pcre pcre-devel
7 W+ h; K; n; h4 M+ t6 S+ ~3.没有libxml2& y* W- z- C. `6 b+ b1 d- T

( x' l+ ~( F) v8 r- j  z% @2 j+ C5 w
configure: *** xml library not found.
* m$ O. U+ G5 |7 _8 aconfigure: error: libxml2 is required
/ C6 W( [) X3 k$ Y% L8 D) k; q解决方法:
8 s5 C0 ?4 W1 i8 `' V( K% B: a  ?' k: n
yum install  libxml2 libxml2-devel
5 Q( |8 p5 d; [+ L- w4.执行 /opt/tengine/sbin/nginx -m 时有警告
% g4 d" j1 A. ~2 H# }* [8 I% N) S  G  [  x, v) D
Tengine version: Tengine/2.1.0 (nginx/1.6.2)0 u. O4 y' t% S8 w' B9 L
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!9 A& g  s1 U+ ~0 |0 t* Q
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
8 Q( K. ~- r  G
8 a" Q' @. n9 U& m3 e2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
5 a6 Z7 Y+ U7 r0 Z! x+ T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"' K9 f  ?. F5 Y6 e2 K
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!2 W+ I9 B  P  E  _5 H0 T$ ?
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
# m2 W+ E+ O) o+ ?* ~7 _  L% Q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6": B9 }! h0 |" T0 N1 i: t
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.6 S& X. X1 h1 y$ w, h1 }& K
解决方法,移除低版本的APR (1.3.9)
- a4 t, |: l1 h8 e( r
$ @% _6 ]! L7 t9 `/ o, x! @0 Z/ Kyum remove apr
9 O" _- m( H- b5.Error.log中有: Audit log: Failed to lock global mutex( |, y: L7 A- B( ]2 r* N7 V

2 [4 E; I" _5 v% b2 q- y2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
; ?; W* H8 J2 l1 }; I0 W) _: i6 Hglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
% ~. g: h* c  [' i: h! c: h& P+ [解决方法:; K, I' v. H5 C$ |* e; F! I
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
( c! |- l; Z! F0 y4 u6 ~+ _. Q
  d; G1 ~5 L! t* a* RSecAuditLogDirMode 0777* L8 X( a- h+ a7 L) S: p! [
SecAuditLogFileMode 0550
. b, n- n* E7 |0 t! P; |. w, O$ ^! wSecAuditLogStorageDir /var/log/modsecurity7 N5 i' D9 c. P+ r- j
SecAuditLogType Concurrent0 d0 u4 j% ]0 q
参考文章:; V/ E, J0 z& n6 B6 {% _, |  }
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX+ Q6 l- S( Z3 K1 z9 f- F* U
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|第一站论坛 ( 蜀ICP备06004864号-6 )

GMT+8, 2026-7-7 13:47 , Processed in 0.091021 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表