找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12923|回复: 0

nginx配合modsecurity实现WAF功能

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

+ a- p) X# e+ X1 O% P' x$ ~一.准备工作
8 Z( S5 U& S$ f
! W- P0 y: I/ t+ g, }9 y系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0$ \- R% m% c7 F6 q5 E) u# C  W
1 a7 `0 U& S7 P- }4 Q5 ?5 P
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz% F" N& H  m6 k
! r( n2 A, g+ a
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz+ E, X' X* c- @2 N

5 N4 k: C) L$ O# k# x- fOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
; A: @2 t0 n% Y" p& v( _4 ^# l' j2 ]9 W2 T+ c# }( k
依赖关系:1 t) n( f, \3 w$ ?$ U7 [3 A3 V3 M
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
  D8 B; c) c1 ]4 N. M8 N5 x. F. r' k- g: q, M
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel$ X2 h( a- J& U
modsecurty依赖的包:pcre httpd-devel libxml2 apr
  D* h4 ?: e* J, D$ s
! h( |6 ?( ^) K6 a$ M* {yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
( Z: N0 B6 o) N3 S3 t- o% H二.启用standalone模块并编译. L' D+ T4 z! }) Q; X3 ]  X" R
; I" e4 ^7 X! Z. `. O+ G
下载modsecurity for nginx 解压,进入解压后目录执行:
/ s9 Z  x2 x! l& \, j! }/ p5 R
. e/ K/ M& u, G2 ?1 M. |./autogen.sh
* W7 u- I* {7 J1 r* J+ e2 L./configure --enable-standalone-module --disable-mlogc* f8 v; P4 K/ O4 o+ R9 j6 e
make
3 |6 Y0 k' L( `% s- A. Y# X三.nginx添加modsecurity模块
4 J$ L, l; G4 d" ]0 y, K( \
$ {$ M/ j. m7 \5 x4 J在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:6 C$ E+ ]0 z1 Y* ~

; g/ D7 V) z$ R: V7 M; }# H1 t6 `./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
  H7 F/ Q, n( E* P, n4 q- [make && make install
. n4 Y; l; E/ A* ~: O四.添加规则( g: C! V9 \) x% m% |
- N5 j5 w9 R$ G+ A! l
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。7 e3 O3 I! j3 h* `' g& v

8 d' p3 O& ], }; d- N1.下载OWASP规则:2 p- l/ E, _. x. f$ r: c7 l
) h. X6 R* R; F/ E+ H0 J
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
0 u9 B* c7 @( \0 L6 C
5 t3 C9 u( J" \mv owasp-modsecurity-crs /opt/tengine/conf/
  l& C& g' g8 b8 G; F" x" |
% \, z, q% m; s9 @3 vcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
9 P! ?' \) E! ^+ R) \  |4 X# ]2.启用OWASP规则:
/ R' p9 d- C1 _6 ]3 E6 I: ^
& }, n" j6 Y& d/ `4 D0 N复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
3 t/ e) W0 t8 E5 X# }/ m; t0 e
  q9 T, l  Q" S( W0 Q编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
0 }5 j3 \( ^3 Y# m" Q* s
5 V  [/ ^- n9 `- b$ Y& t; Gowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。/ q' F/ u4 \2 N9 F4 J9 i: x: G9 l
; H, \# s! a! N
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf6 e0 }# N0 E* e5 A* y+ N6 Y" T) n
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
/ ?( @; q+ Q, A/ \% uInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
; O& m) O3 X% u* i- y+ {, {Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
4 ~5 L8 e% g7 p( S$ pInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf! S& E6 N) u: W7 w1 V* H# k
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf( t/ G  Q, m* b( ?1 C
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
' O! o, u2 D/ D5 V- C五.配置nginx% h: z8 ?; U5 Q7 Q% E7 L
! N" F8 q; p, \' O4 k) \
在需要启用modsecurity的主机的location下面加入下面两行即可:
$ H7 w# \! [6 v: C+ ~9 v+ d3 a% x6 w0 d7 [6 `) h* K
ModSecurityEnabled on;  
0 K( q. X  \/ f8 C" ?ModSecurityConfig modsecurity.conf;1 J4 B- \8 {1 a  ^
下面是两个示例配置,php虚拟主机:( b+ ]4 M4 G5 z6 \
. T$ V9 Z- W2 l& p% P, X8 a
server {2 V3 C6 T5 t! W, j+ n
      listen      80;
7 w0 W4 r! F2 \% `      server_name 52os.net www.52os.net;
/ D9 Y3 R5 v6 d/ ^     
4 o) V) d9 [  ]8 k3 g+ Y7 e      location ~ \.php$ {: ~( P* ?; @; d: x7 q
      ModSecurityEnabled on;  ' r& W9 U8 g/ ^, u
      ModSecurityConfig modsecurity.conf;1 k" j6 D% P/ J! ~& c9 S

) s; f  Q9 r: b0 l& X: j  N( d, b      root /web/wordpress;
: N  o! C" W! d. a- h. E      index index.php index.html index.htm;0 d; I: Q! j8 ~$ ^2 Z
  
  ]+ Z8 @- u) l  c1 ~- p, \8 V      fastcgi_pass   127.0.0.1:9000;
5 W% E$ F* |7 m) }" D1 \6 l      fastcgi_index  index.php;
9 ]8 R7 p$ N- ^& |      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;' ~; x4 o/ J/ F  H! }! Z6 b
      include        fastcgi_params;
4 }* O: I5 q- \      }" `2 S) J6 X+ K8 O
  }
; D3 ]! x5 a6 O1 Y- k  l9 b+ Eupstream负载均衡:. p5 y( M; T5 f1 x0 J
+ {/ r. o: Z$ X/ |. j
upstream 52os.net {1 @. J# m$ L1 e' n
    server 192.168.1.100:8080;  O: Q- q4 x7 `* a
    server 192.168.1.101:8080 backup;) [; J. a: L4 r) ?# O/ R
}2 L/ P: A5 z4 o$ \* _6 b7 C
& [! l' f/ D8 |7 _; e, i* ^' h* O
server {
1 h/ {! ?% h' Ylisten 80;
& J5 V- @; z- {/ A+ I$ t4 b$ z/ }server_name 52os.net www.52os.net;
6 S+ f( |# ~5 i0 ^9 [/ q( @+ r, B# i+ K
location / {
% }1 X8 j" E7 s- T' M    ModSecurityEnabled on;  ' t6 z" A1 M/ ?2 H1 ~8 _& U
    ModSecurityConfig modsecurity.conf;  
' F+ Z6 V- m8 N, U# y* l# W0 R, e
" @/ n5 Z9 {4 ~; n2 b5 B) R# I7 ^. g9 `        proxy_pass http://online;
" }3 i- q$ j0 L2 [& G        proxy_redirect         off;, \: j# p2 q/ H. t1 @
        proxy_set_header Host $host;* w# V% [/ ^3 n# i
        proxy_set_header X-Real-IP $remote_addr;
% E6 S5 \0 G" i, {        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
* I6 R6 e0 k* v( y; t4 v, Z    }
! f/ A1 ]" p, X7 u# k$ m0 y}2 G3 ]9 Y! Q! I* L) g2 x
六.测试3 s9 \4 ^$ i5 v  ]

! Y4 h9 n7 a: H' z我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
6 \0 Q# d1 d/ f: |0 S/ K8 Z  V% D- m1 F6 H! O, z
<?php
8 r/ D+ V/ }! i+ z5 z    phpinfo();    : V' t, Q' q6 b( }1 E& G0 H: |8 X  _
?>
7 J! B( W) V6 L& X- c: p在浏览器中访问:/ u' p) S$ D  l: H! t
3 k3 e, _, J" @; {+ m' P
http://www.52os.net/phpinfo.php?id=1 正常显示。! u' j" L" r  a
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
- b3 f; ?6 }- L6 f* Rhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。; \0 j# t1 W' f. E. Z" k& T
说明sql注入和xss已经被过滤了
$ F( N5 e% u, M: P: {  f; g% |
1 K; n! B# x2 i5 z; `七、安装过程中排错% r# D# h! o( B
+ q( D' g! S5 D/ Z' P4 e
1.缺少APXS会报错! L: u5 l- a; J8 z

: {8 @: U: U$ ~" Q$ [configure: looking for Apache module support via DSO through APXS
; f1 y7 Y; S7 m9 q7 \configure: error: couldn't find APXS
+ y; k$ T. |9 ~; l/ gapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
- f/ s$ C1 r: U解决方法:
( H* K- F; ]7 c8 j* W4 }' r7 @5 r+ R4 u/ w( ?' k: H
yum install httpd-devel
+ o* C' m- C5 L$ `$ x# b$ I5 J2.没有pcre. O) ]! w) _9 P3 K( h

' ^+ n' ^  O& z! y/ [7 r# n3 oconfigure: *** pcre library not found.
7 Y* u1 s; D$ b; fconfigure: error: pcre library is required
. t" X8 r3 T* ]解决方法:
1 m: o( T1 B1 V! l* C& v1 g) q: `  z- K" P3 d
yum install pcre pcre-devel
8 n; f6 I5 J& x/ M/ n3.没有libxml2
% L1 S0 w: `+ |7 m% ?
. R+ y- m6 R! l
( v  j/ ?" i- \/ Oconfigure: *** xml library not found.0 W: P8 b! ]; n5 U+ q
configure: error: libxml2 is required
1 p$ U9 R; d6 Y4 T解决方法:
3 P+ d  G& g5 l5 u7 r& ?) X9 O6 P$ X  g, \7 D' w" `
yum install  libxml2 libxml2-devel
9 m( ~0 D; O( Q+ d( m) [8 j4.执行 /opt/tengine/sbin/nginx -m 时有警告
$ h" S. s3 U8 E& s- |
/ Q+ R* C. j/ rTengine version: Tengine/2.1.0 (nginx/1.6.2)
0 x3 Q+ @# p" B2 Tnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
0 ~/ F  d/ s0 X7 S. f1 U原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log6 r9 P: `$ _- S3 z* {
, V3 b# Y& a. a' H/ v8 ~4 M
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.; {* p9 d/ J- v4 o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"8 Z' T) ~$ ]# j  }, b# q1 M9 c
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
. d% G; {: H$ E* k) Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"/ }, O( C1 _- V) ?
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
  Y$ @1 N4 v* B7 `4 J: w# u2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
/ {, f$ n2 _+ j* h. T  |9 J. K解决方法,移除低版本的APR (1.3.9)! R; Z- |" C6 X6 d2 P
+ ^0 P" F  D, t+ r. T6 i
yum remove apr
$ W$ ]+ A9 E: N$ A3 }5.Error.log中有: Audit log: Failed to lock global mutex
, m6 @0 P4 `* Y; Q9 j0 L
% t3 N) B/ \& ?  r2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     0 m5 c5 ~: E' N& ?* Y* I
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]2 E3 M' H, l) x3 e( ?$ i
解决方法:# {% [  i, U( {0 E- Z6 \, i
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:. O* V+ E0 ^9 g# s

5 I- h" ~1 c/ i6 K6 |5 MSecAuditLogDirMode 0777! B  c( w1 X3 u7 _
SecAuditLogFileMode 0550
  k( K$ b+ u; Y: q& N# kSecAuditLogStorageDir /var/log/modsecurity
$ u* i) [; P, w1 K6 ?/ ]# m$ Y' OSecAuditLogType Concurrent* x  v' ^* j# `1 S) W
参考文章:
* ?" S# w' C& H0 G# whttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
; y$ h# a, Z! A# v$ `http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-26 14:11 , Processed in 0.072223 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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