找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11723|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
0 b9 F4 _1 D' s. c" U0 F" e' D& ?8 u/ Y% I6 O
一.准备工作
; M- }* b7 H8 t: Y# I! f( |2 c4 x3 @3 X/ @3 ]4 c
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.09 x$ a* {9 ~" m) x; u6 ?

  s9 A) I% I6 P: V2 ]6 N. Ttengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz8 ^9 ~7 l4 Q. w3 G$ t( j8 h

* L; R3 u# ?+ y) ymodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ A0 K0 ^* T$ |- `
; e( C/ z2 l' \1 KOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
$ Q9 i  G) N5 j9 L# I$ B" h( s4 ~% \; \( S
依赖关系:, C' X) C& y6 ^  E* i1 [
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
  e7 j% c6 ^# y# U% C  _! Q
# x: H4 v. Z. N/ ~9 k1 P% g' x# s* Pyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel% `: v  c: A; J2 b% [
modsecurty依赖的包:pcre httpd-devel libxml2 apr
4 n2 Q9 b- a" a9 _/ A" M
7 E3 X8 }8 j1 ]; P! ?/ o+ Xyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel+ O8 j, a# L2 a2 [
二.启用standalone模块并编译
$ I0 \+ K* d5 X- f- F' M, P5 n
' F  g$ x' z" R# L. Z/ |0 ?/ `下载modsecurity for nginx 解压,进入解压后目录执行:9 U$ B( R* W; q/ S# L/ M# o
, h# t: t: W, B+ T3 O, r$ g7 F
./autogen.sh1 P6 o0 e' U7 {
./configure --enable-standalone-module --disable-mlogc
+ V' p& K1 Z7 c% R* v4 m- P2 vmake . u- a8 T3 k5 P6 ^0 \3 o$ `& z
三.nginx添加modsecurity模块
0 q- C1 ~/ Z4 g2 L  S2 s# j9 o% b; @& \0 z, K  a; Q% X* q
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
* m4 ], J# B4 x) s) B/ {
, T) X' g2 d4 [: R% }./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine3 [  _( i$ }* O4 K" M" w# t
make && make install! R8 U/ O% I- A/ N# I9 Y3 K( w% l
四.添加规则& Q: G0 u0 s) Y6 Z

0 U) v+ T- a* o1 l  l* [! ?0 }- o/ Dmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
* Y  d& {4 c7 J4 {. T) H/ O2 V; F4 F: b5 ~7 }+ Y, J1 N
1.下载OWASP规则:: s: e+ c) P  P

0 t. M8 K$ ^0 |6 b2 V9 o8 O: sgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs1 \6 |" T8 g+ D* d+ n

6 g* p6 L& ~$ o8 wmv owasp-modsecurity-crs /opt/tengine/conf/5 D+ i4 k& L, }$ \
. R& W+ n+ L" `( g8 b* g
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
8 F& W$ |: E$ A/ f$ z2.启用OWASP规则:
( w& K0 {$ q  V& k- w* z# C8 M# L1 S& b8 I
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。, k, b3 r0 F' J) p7 F5 `
& N2 t; d/ V0 i4 L; ~; h
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on5 D6 f. J& _/ t% K" u

1 Y% d, B" L. Zowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
' k- T  N3 A$ S) a" S. o6 K2 z8 K5 K% Z0 z
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf$ t) j5 A8 v2 E
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf; o9 M# W+ [( U* y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
/ p' `+ {# x" S! C) }' RInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
, H& n" C% v% c- ^' `1 {+ f. tInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf, V, p- t( y) }* V/ @3 g! T
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf* k7 M# K& M8 {4 P
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf4 W* i* ^1 c3 l8 k
五.配置nginx9 u" X2 S. ~9 z3 O7 u7 ^( E

$ T2 X$ F" O% ~/ O在需要启用modsecurity的主机的location下面加入下面两行即可:9 S! h/ ^& b+ z  V) B% |
0 a$ U+ S$ |- x# X6 ]
ModSecurityEnabled on;  ! N5 \5 H3 o( b  k
ModSecurityConfig modsecurity.conf;
  b! H" r3 N% g/ M; e( l! a下面是两个示例配置,php虚拟主机:" A& v/ d/ N3 B, I: P% t0 W" y2 d0 m( v

3 M7 N, o$ W: p7 n. aserver {) j* z$ D1 C2 V( |
      listen      80;6 S, ?" L7 U2 o
      server_name 52os.net www.52os.net;
6 k% y& @5 V0 @5 G) t: d% n     + y  {/ f8 z! c9 Y
      location ~ \.php$ {
5 E1 s4 s0 v& [0 f3 S      ModSecurityEnabled on;  
4 Y$ u( _+ W7 Y* [7 v- _/ L8 G      ModSecurityConfig modsecurity.conf;  U* T- Z0 j  y. v% E  w
7 I% i2 B; s. Z0 e# n
      root /web/wordpress;
* i* T, C; x' G7 _* @- ]2 I      index index.php index.html index.htm;
1 q2 X' Y6 |" \( V0 B: g  4 L. Y# u% A9 x+ c' q
      fastcgi_pass   127.0.0.1:9000;
, d* v7 R. k' s+ r% N1 E& X      fastcgi_index  index.php;+ J8 r/ B- S& r
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;, Q7 r1 Z1 k- G: Z
      include        fastcgi_params;1 G4 E( P3 p4 S1 b  M
      }
# u- X, l' p; |/ L! P  }! Q6 ?' ]2 H7 x7 t7 c2 X. x3 d' j
upstream负载均衡:
& \3 y  O* E& f- T* i& ]+ r/ `
5 ^3 [1 K5 x* D- m/ ~4 Eupstream 52os.net {
2 g" \3 y* \6 [' \- G, F" e    server 192.168.1.100:8080;" A# ?% n3 y8 E6 Z5 b
    server 192.168.1.101:8080 backup;
3 G" U" o* z8 O% x/ r}
1 }- t# O. `' |  L/ q
% \& C5 |' Q( Dserver {
9 u, ]  D- O! \# O4 g" X( ilisten 80;
- J4 k; E/ r# B8 x/ R6 }& U& Yserver_name 52os.net www.52os.net;' k/ O6 \+ B7 B& C# }* L0 K9 @
% [& }' {9 W& y8 U& y
location / {3 A9 l/ }7 W4 {/ w
    ModSecurityEnabled on;  
- @5 o6 U2 f+ D6 g& g( K6 C    ModSecurityConfig modsecurity.conf;  
( }' R* e4 E9 c" P7 g" s2 y" H/ G- }, K; [- ?1 U( ]0 @
        proxy_pass http://online;' d' ?& P7 R: {- I( @5 z
        proxy_redirect         off;
/ c( z3 _6 T9 e$ O        proxy_set_header Host $host;
5 D+ g1 e- `/ y- v# m2 g        proxy_set_header X-Real-IP $remote_addr;
1 S% l$ l/ t6 ~1 G3 t        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
) A6 Z$ {/ G& [& z) x  G    }  E9 z9 a( x. I$ `: t3 Z
}, G! B2 O; }( ^! n  m5 }! r3 w4 K
六.测试
' J( B+ I8 i+ w  \/ a5 `0 e" ~: Y2 w% s$ |
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
$ L' e6 S. R. ?3 W0 m. i6 w+ ?" J0 g9 }2 |+ F6 G7 \$ d5 D2 k
<?php
( |5 u) x3 |! k; y4 _4 y/ \    phpinfo();   
0 G9 Y- U" ?$ ?  o' C1 b?>
6 M1 m! _1 b% t: R' h4 f0 Y3 i/ p在浏览器中访问:
( t1 [; h: I& d8 K+ Y- X  u; t& n. z! o8 y
http://www.52os.net/phpinfo.php?id=1 正常显示。6 F% B: m9 @$ t2 ?3 ~4 r" ~
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
# `* G# x( J) p; v0 thttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
* {9 q& i3 W! u3 J说明sql注入和xss已经被过滤了
# F* I) C/ S! H& e8 A& P
4 W: a- h" v: I3 T) S七、安装过程中排错
1 k. ], E, I: P2 N7 ?" b# f3 d# P8 l- g* O
1.缺少APXS会报错
' F  P: G  [1 k  }- i# w7 e1 D0 B; Q. S& W, Z/ J7 P+ d
configure: looking for Apache module support via DSO through APXS
9 s6 S8 e( |% j, W5 r8 C* Lconfigure: error: couldn't find APXS
7 P, D6 [6 l. K) b9 d6 m# [apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。! Z* Q4 M- ]6 ~+ A* O: H
解决方法:
. A( {% V  E. A% ~& ^6 Q. q0 {$ L; a. N
yum install httpd-devel
9 J- H! R- j) C8 e6 u2.没有pcre
. ]; J" c+ c% Q) t- O; ?! }0 u2 Q
configure: *** pcre library not found.
: ?/ K+ r( P4 E2 ^9 Aconfigure: error: pcre library is required
7 k- P; q. s+ f/ Q4 D/ R解决方法:& D7 {, ^7 w; {$ [7 X
5 ~1 ~$ W' I( E
yum install pcre pcre-devel
: m1 ]7 z0 L0 L) c) c7 n3.没有libxml2, |9 p: L1 ~3 g: y+ m' b- M3 Q6 a

4 B9 ^+ U8 a1 Z- k$ d' ?/ g# M& e0 k. z" K! A$ h! V
configure: *** xml library not found.  o2 n8 Q" |  {" c7 J" r: @" x6 C
configure: error: libxml2 is required) A+ j- L+ U& k
解决方法:- G7 Z9 H6 ]9 e; a! `, y

' q4 I& g1 O/ a6 \) Xyum install  libxml2 libxml2-devel8 i% ]6 E( @" K! v' r
4.执行 /opt/tengine/sbin/nginx -m 时有警告% H* b- a* ?  Y$ L: l2 k
  u3 q+ u! Y7 W" M* R! H
Tengine version: Tengine/2.1.0 (nginx/1.6.2)4 ]5 `6 y$ t/ T7 j
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
. J8 I4 ^6 i5 P, _# q原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
9 x# q- n. h/ Q" S8 C- C* S$ m& e' {& _' @1 b; \5 B+ ~  _7 e
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
; ^% H. }0 Q2 V3 C, Z% a! K2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"5 D; i( D7 P( F4 v; Y5 f+ X' X8 e
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
3 N, |. {4 A7 B* s! n& _5 N- W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
/ f3 w4 `: U) B+ g7 p  `2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"9 E9 Q) ~5 g3 A/ `8 [) c+ g* T
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.3 J6 j" c& {' [/ V
解决方法,移除低版本的APR (1.3.9)' B4 @: U% c. y

* r) S' f& W" R# y; M7 \" l9 jyum remove apr
9 P* m; F8 X- s& d/ r5.Error.log中有: Audit log: Failed to lock global mutex
! M$ ]* l  x# N& a# o. y3 y# x7 u& A  F2 C& Z. P
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
, J5 r8 @2 U# Y* o- L3 [2 A/ x9 }- Fglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
4 Y6 @5 U( z: n0 W; p解决方法:4 |* @  N7 I0 R1 M) y
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
, q: J6 v# R/ h9 W) P5 {2 s& t5 x7 _5 L& W. y. \8 O, {
SecAuditLogDirMode 0777/ |% ?0 M! X! \( Z9 C
SecAuditLogFileMode 0550
" U/ G' C" Q8 d% o1 ?SecAuditLogStorageDir /var/log/modsecurity8 t* i/ r! A7 o# c9 |
SecAuditLogType Concurrent5 @# R9 r5 w" ]& d& j. v& w5 Q+ A
参考文章:+ K% H, t8 ~9 F/ ~) ^" g
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
! ]7 J4 j. w* _  vhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-9 03:59 , Processed in 0.074140 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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