找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12063|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。- p* Q/ a% O  U  f5 H4 N1 D

6 f1 K/ }$ T9 V9 P9 k( a: f一.准备工作$ l8 Q1 Q2 m, ]* m

+ ?) t: D7 \5 g3 s; H% V  U系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0  L: g# X1 t0 Y
8 F; ]; ^: \# ^6 _( F
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
5 ]! M8 [9 ~1 t  G  C9 M, g
  K# O: G7 J: B5 l0 q) l; N9 R  emodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz& g; d- \5 Q" ^, L% D* \4 I
8 L" v- B* a0 x1 z
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
! V- z& l6 m; }; L+ A, `6 [1 y+ j6 t) y" ]' J; G2 p
依赖关系:
. K6 @. X, ?+ xtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
2 D5 G$ G" V  l4 C+ @- X: F2 q  k2 H8 \: W$ f) p( X
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel1 ^: C- l+ r* W+ y) r4 H* V; g
modsecurty依赖的包:pcre httpd-devel libxml2 apr
; q" T5 n! S/ z) ~5 K; ]3 C7 ]. }- a2 E- l- _
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
, E+ S) T/ @; Y3 Z二.启用standalone模块并编译
4 {) N! K6 D/ N' x1 q  m# L1 w+ _; T' M9 J0 z. ]3 w
下载modsecurity for nginx 解压,进入解压后目录执行:
. \. I: l. X: g5 ]* v  I  {8 q' _! y
, I% u+ s# R3 Q  N5 n./autogen.sh
* L" b* U  ~* F# G% M0 \./configure --enable-standalone-module --disable-mlogc
/ w! u" o" _0 `' j6 ?6 A6 mmake 2 P. k7 j1 n! j$ Z
三.nginx添加modsecurity模块
: E0 @6 L% e' p7 b- U) Q. I1 H, R" `
1 H3 k' \6 X. T1 g在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:. w* s! Y# i: w% F5 c$ P- \" M' g$ K, V

/ e6 O7 X4 F/ g# v* V' v6 C./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
! y5 `7 O% @1 m& A% Qmake && make install
( f$ `; B9 `9 e5 `% M四.添加规则/ e; K  O/ w9 Q" b' @" c
7 o/ p) R' \6 U7 i7 Q4 `  E
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。: t; v" Z8 t& t

0 i- i  X% F: d( F% ^) \1.下载OWASP规则:" q" d) P9 X. z: j/ c0 ^) o* v

# X0 U% \, N) g6 igit clone https://github.com/SpiderLabs/owasp-modsecurity-crs0 T) S5 l: ^; {& f
! _+ H2 S$ h9 l
mv owasp-modsecurity-crs /opt/tengine/conf/
& P9 |! M) @8 w) H% O( L4 K1 j3 v* R- ?" M5 h
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf3 L; s% o$ r6 X8 K# ]
2.启用OWASP规则:
0 ]: H/ s8 `6 O1 t7 ]: N* A, C9 |! Z
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。. Y6 }  m' x. Y% j

* b. P8 Y7 }* v编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
( F" a, j7 x$ i; x) C6 C6 {0 W, a1 U5 k
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
9 f+ S6 ]  |/ M5 {: |
3 v9 {6 W. M5 ]* gInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
7 y, q* S) f  y( Q, s- @: |Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
# s+ ~5 w5 u4 @0 p; H7 T2 uInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
$ ~; l. L5 V) A5 y+ J: ?5 JInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
9 \0 Z$ S, n: ~1 i: NInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
& x9 U8 h) W4 u4 uInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf( b7 G1 O* Z) ?% d
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
. a5 |' S( S  b五.配置nginx
' ^% |( X* e7 V$ ]9 X/ h. @9 B" W4 J
在需要启用modsecurity的主机的location下面加入下面两行即可:2 j5 s, L% S* \  x
! y5 g  ~: Y  W" E. n* Y; c, P  a
ModSecurityEnabled on;  5 R# S. A3 W- N* A* s
ModSecurityConfig modsecurity.conf;
" G9 {& C# f4 G7 y3 o下面是两个示例配置,php虚拟主机:# A+ q( G. N' G4 f1 B

3 r% V5 o9 R; I( G- z" n3 R! dserver {
- ?* `1 g! v; _, u7 J! J7 f      listen      80;
) p( _2 R3 {8 h9 X; f5 ]      server_name 52os.net www.52os.net;2 J  L% K, F  ~; l
     3 p& a$ N: C& j+ n( a* A
      location ~ \.php$ {
) y7 c+ G+ s7 o1 n0 E      ModSecurityEnabled on;  - x: O9 r3 d6 V+ h7 s
      ModSecurityConfig modsecurity.conf;' e, D6 z7 K6 r
" b( g: j# R/ o8 F
      root /web/wordpress;
, \; Q/ ~. l  M% Q      index index.php index.html index.htm;
9 w: m! D9 m' w! B# d) f+ S  
6 V8 P, `8 p! a1 p* V      fastcgi_pass   127.0.0.1:9000;5 Y& h- k/ q  c6 p$ Y
      fastcgi_index  index.php;7 V) U8 s0 b# J
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
$ R1 _% J  \4 K) f  {# T& n: R) n      include        fastcgi_params;2 d+ w7 p8 b9 G5 d, o1 _/ o
      }* L+ v( |& C* X* W* _2 h2 [8 U9 M$ g
  }% Q# o3 ?' b- U% E; O
upstream负载均衡:
! z2 \7 j$ [- X) [  x, `! M* l7 T2 X/ Y3 S. n; ~
upstream 52os.net {
0 X5 q2 H- S* r* a    server 192.168.1.100:8080;
* {  C5 G( ?4 ~: m) m( k! @0 l. k$ S    server 192.168.1.101:8080 backup;3 M5 `% }! E( P& i. M
}
  d) i- |% ~7 N; p
7 L' M+ D) \6 j9 m: `9 _% i$ cserver {
. i% _6 {7 T* ^1 Y( h' o* Slisten 80;8 x# T4 z$ h1 L; k' m
server_name 52os.net www.52os.net;1 r% W, E9 O. o; B7 {) t

3 L$ D: a7 f1 Plocation / {
' a- _1 [' S- E6 {" t    ModSecurityEnabled on;  
# M+ `, }, p. r: @. v# }    ModSecurityConfig modsecurity.conf;  4 h( R( B' [& r2 }
! h8 s5 m2 K+ ]5 ?2 A
        proxy_pass http://online;
- y1 V" u( J( {: _6 d  G5 {        proxy_redirect         off;
$ m+ Y3 s& w8 e7 X4 D6 y$ M' k        proxy_set_header Host $host;0 {! I* H3 _6 h5 D
        proxy_set_header X-Real-IP $remote_addr;
' J+ }6 t9 q0 N" D; }( F2 n1 E$ b8 _        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;# ^, u* V8 p% j$ S6 r
    }4 A& Z, n2 B* \. ?/ f, W
}  C  u7 j/ u. l) p/ q# c6 T6 g
六.测试' P( v6 [, \8 X0 o9 T/ l

' ^) s5 N8 G) s3 v* ?4 X我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:$ s9 {2 J2 X. U8 g& V
  `. A. ]  ^+ Y) S& |8 Z. i! y
<?php
2 e# ~! M/ y+ U4 ?8 r  m    phpinfo();    / Y7 o8 D  n8 |; k( M
?>
  R& ]) r1 s0 y- }在浏览器中访问:1 C9 I, [) B: s6 Q" O- j

6 N- Z5 N9 r( E2 V+ `http://www.52os.net/phpinfo.php?id=1 正常显示。1 U, K, y4 z8 W& m
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。8 t0 u) h8 z! _+ t( I. z
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
1 b- Q$ E9 A: E说明sql注入和xss已经被过滤了
! |: Q8 b4 {* L: {3 K  J0 l" {* X2 V0 t9 u0 _
七、安装过程中排错
$ c% ?  s3 P' [" v" f% k8 z
. S2 ?! r& K# t1.缺少APXS会报错4 G. T  @# ]* K" [
; B4 |( M* w+ d  B0 D4 @
configure: looking for Apache module support via DSO through APXS
& m7 w3 h( I- b, {! V, L2 K6 s( \configure: error: couldn't find APXS
9 t6 h9 \2 l  ~5 ~& M# lapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
/ X4 k. h- u- o  c3 L解决方法:& r6 p- P- h8 J% {4 t* P$ J
! g  @( d  q. u2 |: ^* p
yum install httpd-devel5 `" V( H$ a: [. F* B" |( t
2.没有pcre8 J3 q$ D2 M+ c4 X+ b
5 D  S5 c# R6 L! g0 T
configure: *** pcre library not found.- T  r# `3 a. m& M2 B
configure: error: pcre library is required
1 X& o9 M. E) Y! |5 ?" @解决方法:& l1 D2 y  Y( V) f5 B2 Y
8 ]+ p+ _0 H  \
yum install pcre pcre-devel) z) t' M6 o- r- i1 v* a/ b
3.没有libxml2
) `& [) z  w( q5 j( t. l# W7 ~4 w; d; W5 }
4 c9 g2 }7 ~( t( L
configure: *** xml library not found.2 ?6 x& i3 P/ A' R
configure: error: libxml2 is required5 u; |  x5 ^1 |
解决方法:
' C0 \5 X8 q" ?
* V/ y1 D* e2 C, b( wyum install  libxml2 libxml2-devel
7 R2 G8 i% J& z4.执行 /opt/tengine/sbin/nginx -m 时有警告# l2 \- I8 }! v$ R! _2 K/ c

: u- f0 E% ^! n3 V6 n6 }8 r7 tTengine version: Tengine/2.1.0 (nginx/1.6.2)% A7 y5 b" |% z
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
" L+ v* w( Z, p* Q" j原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log1 o8 P. Y; I7 w9 d8 t/ W9 J

, e) u# ]$ J0 c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.5 D* d9 {6 S/ r& \: R6 O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9". G# d/ Y0 n% ?! e* j4 }, z* [, `6 a
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!/ P0 ~; z3 @6 k- }( j5 C/ x
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
8 ?1 h5 m8 F4 \; }6 g5 H4 B4 Z6 O4 C, j7 K2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"- h: z4 {* n) R
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
" F/ D* {; D4 A3 }解决方法,移除低版本的APR (1.3.9)
5 E; G, F5 C4 I* D) j$ k, E( R) C7 z/ V1 N' H5 \. L! g' z6 M4 W
yum remove apr
) A; j2 O" _" [" y, M0 }( M8 B5.Error.log中有: Audit log: Failed to lock global mutex8 ]8 u9 Z2 g. q& Q6 `
: q2 G' J7 F# v# N- V1 i+ [
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
6 ^$ @6 i" m* w8 Y3 U2 Hglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
8 I, `9 \2 |$ L解决方法:
3 s- x" ]) X5 X编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:9 y& D: u( ?8 _4 n4 S( l
2 B* e7 x4 a# _% L% F# f, Z/ _+ O+ f
SecAuditLogDirMode 07779 f& r) \6 ^' G/ a. _
SecAuditLogFileMode 0550
# I7 f8 k# f) r- J+ @! dSecAuditLogStorageDir /var/log/modsecurity
3 L0 \; q& C& A6 @! J( N/ h% g! LSecAuditLogType Concurrent5 P+ B) e# x, N$ t% M, z  m
参考文章:& Y, w) U' D5 V( }* ~, C, ]
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX3 F4 |7 [* w1 y  Z# D; n
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-7 15:45 , Processed in 0.048682 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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