找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13027|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
% I/ X. p" A2 G! v* b" x
# i2 s! U: F! e+ b+ D0 Y一.准备工作
) F0 j! ~" K& ?# C
: ~/ J# D( J8 @9 L系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0' V0 N; N' T  x7 S. s

  W: X- T0 y9 d- V- k, ~: _tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
& i+ J. N; p) N/ V1 j- m6 ?8 F) g
# H( O6 v9 \) @$ B, t, \  ]" zmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
! s8 r! p2 Y& v7 u5 _$ N  e6 i3 s0 B  [/ e3 e/ p7 j" B. k  Q
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
8 Q8 M+ G  ?0 [. M4 b4 p3 a0 P7 ]1 Y) i) E, m5 p5 T+ _5 I
依赖关系:# X1 D# }; V0 a/ h! l8 h
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:5 w$ N; S# Z9 f. S

7 T7 ]2 D" W# b% B' D& r( X) o* _' \yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
; H0 ^) z& \( }- W; Z4 |modsecurty依赖的包:pcre httpd-devel libxml2 apr; x' w  b8 k5 l5 i
* t( K$ E! B3 {) c4 x# _
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
$ E+ g8 g% g2 A" B6 ]( [% q二.启用standalone模块并编译
. }' @3 A" X5 L+ ^9 b- Y* d  U4 |' d$ K- @- Q
下载modsecurity for nginx 解压,进入解压后目录执行:% X% I- S7 ^* C7 S* O' r0 r! J

8 U8 _9 h" Z% L7 j$ t2 x' c4 h./autogen.sh
6 A6 x3 ?( k! ]* y6 }1 {./configure --enable-standalone-module --disable-mlogc
2 M6 Z' o& w7 L4 zmake " y5 O+ i4 i+ @7 ]7 B& S# z5 Z4 U
三.nginx添加modsecurity模块% j/ q1 _6 d6 P' `: Z, f( `, s

# u* G+ [# X5 G  {9 o% M% v# P在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:# l0 g/ J1 s' M9 B* U! ]) X

# i5 M# t! }  E/ A5 S. V/ m./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine, J8 D6 p0 ?; e' ^7 B
make && make install2 G  K% G' y4 @+ h. q- J% ~1 r
四.添加规则
# K  R- h; k$ F
9 i; d. o( R$ X; Omodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
8 y  x7 o3 N/ j6 W, T5 R- w! l* W% q7 }; g6 @
1.下载OWASP规则:
- [8 G- u- s% l8 d: d, d) c9 Z6 {. \* U+ E5 n5 a/ s
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
0 p8 F& t& s9 I5 K# O0 F& x! `* @7 Z! J& c2 l
mv owasp-modsecurity-crs /opt/tengine/conf/
5 k1 C6 ^) \  P6 Q& q! Q( Q3 T) @: ~
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf7 U! u" Z- f) B. K1 j% J) l# P
2.启用OWASP规则:
) V. [* H) g. e3 N  i) a
/ ^. S) m6 J. b' {5 F复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
# W0 d5 Q4 u+ y2 |; m1 c  i0 o% `) ^$ @! j7 y
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on1 m' |9 P. |$ s& y3 f! \3 L

' k8 m/ V. \4 a3 V) A, E  Howasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
) r5 e8 Y7 u  k$ {3 o
2 t- n, y8 U8 p8 A& i0 |+ {6 ]  RInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf) r8 u5 j' Q6 d* a' ?# g7 C
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
1 W( j- U6 x, Y  PInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf# D* D: [3 b2 `1 N/ M- |% P
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
0 A* V6 L3 P$ Y; IInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
. z- |5 f* N( j; T  k$ V% cInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf# h( l5 v) }0 h3 x. Z7 H
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf- N8 z' d- T4 D& o, V5 y
五.配置nginx6 Y; F' z8 h$ d# O) J$ n1 h. K

& o$ b7 M0 H# U4 c- n- h, J7 V在需要启用modsecurity的主机的location下面加入下面两行即可:9 k$ X6 Z. B1 y% O' T) X

  x+ S- ~+ q8 W/ NModSecurityEnabled on;    j% L% Z# e2 y6 H9 \+ E+ q
ModSecurityConfig modsecurity.conf;
5 V" F& V. @" b9 N0 t* j' }$ A下面是两个示例配置,php虚拟主机:  w6 V' ?+ a' Z# O! o
4 D9 B# _" X! Z4 N' r- |1 E; o! `
server {
% ~3 i% R" |1 k8 e  Y4 i* R      listen      80;
5 x+ l) K& d* M4 C5 P# k; f      server_name 52os.net www.52os.net;
: K8 R2 Q/ t4 I4 c     
2 c' p# z  a# I+ c0 I. {8 l. v      location ~ \.php$ {$ x, w! Q7 Q+ e* f. ]) T9 e
      ModSecurityEnabled on;  
7 E( O0 K6 x1 E' P      ModSecurityConfig modsecurity.conf;
# j) I# o# m( U( l$ A! l+ t
* J: t  z! {% F, z1 G9 m      root /web/wordpress;+ C- A( ?( V" [. j% ?
      index index.php index.html index.htm;
3 }6 q/ C* p- M" @) C, U5 U1 e  ; A4 w* v  [( |/ v% h2 b
      fastcgi_pass   127.0.0.1:9000;
8 u- G8 G% ?+ |3 H1 H      fastcgi_index  index.php;
1 \. @: m) [7 a+ D+ M. y% Q      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;& Q1 j  ~2 z; f, C
      include        fastcgi_params;
1 o+ Z6 }: V4 G$ u      }
5 Y0 v' N5 d3 p) \0 G. V  }0 u* _9 i5 U$ u* Q: T- q
upstream负载均衡:
% ?* R2 j6 x  v0 [8 G! L1 P8 L
0 T2 D( t0 f0 ~0 `  ?* l+ lupstream 52os.net {% J" F% \0 |. l. d
    server 192.168.1.100:8080;
6 f% p, k- l2 M. V  d( L& z    server 192.168.1.101:8080 backup;, d8 h, f0 f2 m! P' I+ `
}" I$ M" X8 Q! T: ]

# j  |0 _" }) G& j2 g% r+ dserver {" a; U1 j8 m- m- z3 R0 w
listen 80;
# ]3 c6 P  n  Y7 bserver_name 52os.net www.52os.net;
: y' o: d6 w* w. G7 L  L7 `
) I  `2 V; J. V( O& b; e7 N+ alocation / {
& o8 [" M9 L* c+ J5 r    ModSecurityEnabled on;  
+ D" P+ K* v: M$ C' G: @    ModSecurityConfig modsecurity.conf;  
" S  ^: V2 S/ ]! j; d
7 {3 a* T2 K. d2 M2 |        proxy_pass http://online;
% w. H1 d. r, e        proxy_redirect         off;7 T9 [! ^; W' w) J$ k
        proxy_set_header Host $host;% d+ Y' M; T% o5 y2 a
        proxy_set_header X-Real-IP $remote_addr;2 k% r# c0 `6 I7 @+ \
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;- N! S# g2 H: o( T$ d
    }9 R* G- J* M8 X1 ]: l/ d) S, U
}3 p! e' ^/ @  P* |
六.测试" k: _) e) p2 C: U: W( u

7 ^+ t% {- t/ P- [3 o我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
7 q  `' G( r; y/ t) O  G2 I
) M- `) h& Q! @; X2 B8 m9 c<?php
+ H* I" M0 k0 ^* k    phpinfo();    * x+ |3 y2 {' M; E) _, G3 Q: h! ]
?>7 t% m# R2 c: J/ Z) j; `
在浏览器中访问:8 ^8 T8 y$ K! W6 L7 f) s& J

8 S7 I) k5 R; R. Ehttp://www.52os.net/phpinfo.php?id=1 正常显示。
  ?: d$ s2 z# E: {  Ghttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。$ M7 R8 F' f  [- B7 K; A1 p1 Q
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
3 O, H2 C2 o/ u8 a7 r说明sql注入和xss已经被过滤了+ l: N& `% T1 o( l2 w. _
/ g' t# j, t' o4 T! v$ }
七、安装过程中排错) m# U6 i% E% w) _: \; i6 j+ p
9 c+ m  O" s/ c9 m5 E8 l1 s) y. i
1.缺少APXS会报错
- f& S/ ]9 P5 S% \; k+ q# |+ `8 v6 `# C+ e, T
configure: looking for Apache module support via DSO through APXS* d' @: y, R" z
configure: error: couldn't find APXS
+ g/ ]1 X  R. X! e1 |apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
4 A; J7 v/ U" P解决方法:
" X0 [+ V  q- I- Z1 _2 ^5 g
6 V! S9 `% y& b8 E4 Iyum install httpd-devel  a5 b; k+ q4 B
2.没有pcre0 d- p- o+ K/ q' A; a" C) I
6 D1 [# C; Z8 I: {; ?
configure: *** pcre library not found.
1 }5 e: z2 ~% _9 K2 \$ w- Zconfigure: error: pcre library is required& f, U1 X7 Z' T9 n# }
解决方法:) j3 R  [$ g/ X& q( ?' g& H" y1 i
+ v, `3 R8 t: A: p" A5 p
yum install pcre pcre-devel! s& b4 e: G* b1 X( \
3.没有libxml2
2 R: T) r  K$ _- I* d' t
% ^' ~1 u5 c" R* O4 q' ^
6 [6 [' t! y4 }0 D: H& X' |configure: *** xml library not found.) o% Q0 R8 Y( x. F, @& n( I
configure: error: libxml2 is required
. [5 r5 W' Z0 j解决方法:
$ t  r7 S+ e; N( `4 `* y
5 S7 l$ {# h$ ]3 p9 Byum install  libxml2 libxml2-devel
# z6 v  ^; x5 p* k( ]. D4.执行 /opt/tengine/sbin/nginx -m 时有警告+ J! t4 V" H! H" [2 d7 ]+ ^

! y5 @% k! p# K6 A+ V9 q% g4 rTengine version: Tengine/2.1.0 (nginx/1.6.2)
: T+ }* H) l( p# ^9 g6 ?nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
" C; ~, l5 ]* {) {原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log0 F- i/ ~1 a$ z; N7 s

8 [8 D$ ?2 }, K$ s- {2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.5 f3 q. C" t5 t  P2 ^  B
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"! _  c- x6 L5 n1 Y  S
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!' W/ w( Y( P) Z2 U2 h& w) Y$ n
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
- ?5 ^- M. J+ E! A3 ]9 X2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
! W; G0 ?" E5 k) j" M2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.3 V& G% ?3 A! Z+ C% C' h
解决方法,移除低版本的APR (1.3.9)
$ D4 `, J' C: R' u
+ E+ {. u4 H* e" B# Hyum remove apr
, S" l# M6 L3 {! k- M5.Error.log中有: Audit log: Failed to lock global mutex( H- @' @6 p6 s- w' i8 o

! t  K" h; ~$ t. b- s  P4 O- F  j2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     ( Z( G* U% `/ O6 [
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
( N9 o3 ~8 J1 z, W解决方法:
7 G4 l+ s; G* `. s编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:- ^5 @/ F" m+ M7 Z  ]5 {

9 j1 P* q& d& W4 ~# ?8 V0 bSecAuditLogDirMode 0777& E" I  O8 A+ z3 _  g
SecAuditLogFileMode 0550
1 W, `! M. i* Q, P- a4 P; jSecAuditLogStorageDir /var/log/modsecurity
! {- X$ @% \+ Q/ W; p6 xSecAuditLogType Concurrent
0 `7 o) e) `6 n参考文章:
# v" m( Q1 R- R/ rhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX8 `+ |3 i6 F- X' j
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-5 07:05 , Processed in 0.069939 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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