找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11779|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
, G# B) H/ Y9 @  {2 O2 T' \0 ~  {8 j
一.准备工作  n; z1 Z! L# K3 V
/ G: j" I/ l  Y2 Z+ ~
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
, V6 Y, K8 e0 H6 |2 y# Z2 I1 w+ y" L' V9 r5 z8 t, R( f+ t
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
* q' x3 h$ ~- o0 p
6 \; g& u, h+ u3 I( lmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz/ d* @2 u# q3 R9 O: L
" K# g8 Q% f1 v
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
8 l- M: Q- p3 z) s7 [" P! d4 s5 I+ k: f9 O. {
依赖关系:: N* n* V( q! f5 L! d6 T6 d4 u* ~
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:2 G5 }( ~9 x' j- q3 M
4 }6 Q2 H, H% _7 j- B, W4 G, ~8 Z/ C
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel+ e8 f2 `) X6 L- n2 i$ M) s
modsecurty依赖的包:pcre httpd-devel libxml2 apr) d: D+ U6 `5 J3 P

1 }& A6 ]4 i7 Y( u* O- k# M7 ]4 ?6 b" Yyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel0 `! X3 M9 s! V9 |
二.启用standalone模块并编译7 j. O1 v# P0 \0 c2 x" k3 Q" N! s4 H

  U3 U7 q1 K& z/ T下载modsecurity for nginx 解压,进入解压后目录执行:  H9 F/ i0 c) h$ ]

+ g+ j% l" n' ^./autogen.sh& D% R8 S, o2 h
./configure --enable-standalone-module --disable-mlogc
/ x8 n) x3 O8 C1 _0 Vmake 1 f& Q3 }5 e. U: V* @' d
三.nginx添加modsecurity模块
7 s$ Q, `6 j4 d8 G/ @  ]1 y
9 A" g* X3 l  N  W* |1 d( H在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:/ [* m& ^& y0 {

1 Z, C2 ~) z7 P7 m./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
: F1 U" A" T6 O0 L: \. @! c8 K! Lmake && make install$ |: o4 P$ w8 H* |8 [- T  w
四.添加规则
/ @' K/ C  e: R. G. F! p; [, M: [" Y) k& k! }+ U
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。7 H3 ~5 R0 h8 ~7 Q2 O0 Q- t
1 w- l8 ^" E. [8 k/ }7 l
1.下载OWASP规则:9 {) j% Z7 L+ u0 t( j$ x6 N

! h" e) x( @2 d4 H- Sgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs7 g7 _: h; ^8 j# x' l& Q

2 D; f2 E* @$ }3 m# x* I6 }mv owasp-modsecurity-crs /opt/tengine/conf/( H! v  S% c; L3 a# j" S1 G; E2 _

: _* A4 m1 M5 `( _8 d8 s2 T& t4 M; i0 c6 wcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf# b3 F* K' X" m5 k7 {. K3 H
2.启用OWASP规则:
+ E& O3 J& \8 Q( Z3 g" K2 s3 @# U8 D( ?3 R$ s5 ^1 t3 m
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
3 C: u  N6 L- K2 R8 {
8 B# e8 J- x# B  C! D编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
! q  N# X$ e6 M% x: y1 y2 g$ d% J' L
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
5 z) N+ ~2 u8 ~
0 K/ o' o3 a( e( ?, k+ [Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
( O1 V7 z8 S/ hInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf4 w+ l6 y0 K2 |7 @
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf7 J: a0 _+ u; F# ~
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf7 C- Z% t. }) z( X  U8 q# I
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
% A2 g9 ]- T1 i6 _8 f1 b9 [& s% VInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf; d& W! w- s* s7 {4 X
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf) P( \) H4 W4 `0 [/ e& x
五.配置nginx
0 g  k+ {% J0 |  J
6 O/ U/ I  z) e- v" e7 g; @+ Q* j在需要启用modsecurity的主机的location下面加入下面两行即可:: W, }. L) U8 t' g! v0 J+ m3 K
" \9 [5 h( F$ ?! D: v/ F- f
ModSecurityEnabled on;  . ?, k1 b4 N1 m& a  a7 I! u* w
ModSecurityConfig modsecurity.conf;
; S5 r& w8 d" O0 L2 c# z2 P% f下面是两个示例配置,php虚拟主机:
! F: @- F& z. f6 h, A
7 q& o% R2 x& w& j1 M: L4 |, [: ?server {
7 M  D" H% g$ J" [" U      listen      80;. d  z8 x5 i6 O' ~; L9 k! r
      server_name 52os.net www.52os.net;
/ U5 c- N5 X! ^! A: D     
$ Z5 t6 Q* O7 W/ K, ?! t      location ~ \.php$ {
0 y2 [" o1 x5 H: k$ n      ModSecurityEnabled on;  
4 H0 O7 r$ k1 i% m2 t2 p8 u' A5 C      ModSecurityConfig modsecurity.conf;/ a/ \( w! w# W2 i6 g
1 A" |: q" [, u2 ^9 X; y! T  G
      root /web/wordpress;
, @! y: L9 ?6 S; @& x* Q5 v- ?      index index.php index.html index.htm;4 H: U" k6 S  F; e: U$ {) Z8 u
  9 K  a0 s; Z. X4 L) D$ l1 |, T
      fastcgi_pass   127.0.0.1:9000;
1 U1 P2 `  I3 V; T5 h      fastcgi_index  index.php;* N0 Z2 R: p7 E
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;3 B4 H! L* d, p9 V) V, i
      include        fastcgi_params;
; L$ q. D2 a  ^) d      }* T8 A( U2 x9 }: O; a
  }
+ ~3 `  }) c5 [/ M! l  J0 t9 oupstream负载均衡:
% X( p  T* {! S# Y, D
. I' }- {& _  m; vupstream 52os.net {* v& p' e  n3 b. H( e/ P2 b9 Z% o
    server 192.168.1.100:8080;7 E3 `8 ^2 ^' _
    server 192.168.1.101:8080 backup;
6 y4 }4 u% k- G% N! w: q& O}
- s2 Z; X, {8 a6 M7 G, V( T
6 }; o/ y% I, u! r. C3 q# o1 {server {
1 G( H# L" _" E' n, ]5 `0 |4 @listen 80;! P+ i( o% @9 X4 g7 `
server_name 52os.net www.52os.net;
: k$ A4 z8 Z( W) l/ C- Y6 U  C. j6 A' ?9 R
location / {
' O2 }. g* _" M1 e    ModSecurityEnabled on;  ! U$ N" p8 V. {6 L" G4 N
    ModSecurityConfig modsecurity.conf;  
3 g7 Q$ ~: i( h) x. w: ~
4 h, c& ]: A; j8 Z+ S* c" O: {* q        proxy_pass http://online;
- C; s* {$ S8 K. [" D, W: H5 M$ N2 q        proxy_redirect         off;
8 e  ^3 `1 j$ k        proxy_set_header Host $host;
; D1 |" z8 c/ D' R% y6 L* I% t        proxy_set_header X-Real-IP $remote_addr;0 c" Q* c- y9 G6 ~  q- j4 m- m
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
0 ^4 k# i" l/ x1 [) t# j% b; N    }
$ i& D  s' n3 C}' u8 }& ~4 D( N* w! ~. _
六.测试
- D2 ^: m# n0 i
( J: a& E% C4 M. v5 m我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:, @$ t5 I) i! X6 N
7 d8 V9 m* i/ y: U
<?php
" x/ I2 G6 t9 R  t' y" C    phpinfo();      g1 k8 E8 n& l8 ?3 I3 h% s
?>
" p* Q* y& P2 q' t  K; ]在浏览器中访问:1 q# D6 S9 E: p0 \5 s$ H& E* S
1 P3 \: `, C9 y/ A/ F- z9 c, G
http://www.52os.net/phpinfo.php?id=1 正常显示。/ J. X1 ]7 s4 x$ N3 ^# p; }5 p
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。! U- [6 O0 ^. a
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
7 K5 _4 P/ ?* c1 q9 q0 [0 c说明sql注入和xss已经被过滤了% Z4 r+ d" K% h; F

) I- K* ]) T$ J6 G" u! M: ]七、安装过程中排错2 q. s% Q8 V3 l. l# w3 `* }: T

- p5 N# E! W. n& n) f6 H9 F" }1.缺少APXS会报错: f7 L' M7 K& K) v3 i, a& O2 x
. s  x- ]6 u* ]+ |7 t
configure: looking for Apache module support via DSO through APXS0 W* O6 P1 K- ?* \% P4 B
configure: error: couldn't find APXS# l/ r3 a7 C* t' p
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。  c' t$ X' p% X/ @& H7 D
解决方法:" O- D! l" r3 Q+ {
2 P6 ]2 G! K; G2 ^' o% z, u
yum install httpd-devel
9 T/ R  ]2 H: y& Z% H7 L3 Z2.没有pcre% A! m7 K" U4 l  j7 ?+ A$ V7 }; f" ]- m
- P1 [4 C5 h2 n9 g5 ~% C
configure: *** pcre library not found.
. e) L/ s# R  `0 S1 Wconfigure: error: pcre library is required: {/ q$ ~, g8 n, Q
解决方法:9 p, K0 G7 U( L4 W0 B' G5 F" b
" {# d9 r' L! p, g
yum install pcre pcre-devel
* }6 B5 m# I; Q4 ]3.没有libxml2' T9 [1 V6 j2 }' {( q/ v) L

4 F1 V' R. A* _5 Z# ]: K& ]  O1 Z0 g( M8 G9 `2 o, d
configure: *** xml library not found.
( K! f. i  I5 H0 K3 R! ~configure: error: libxml2 is required  R& b. D4 X2 _: A% p- X
解决方法:
9 g6 v# n/ |; P" R
) P; H- k' e3 Yyum install  libxml2 libxml2-devel( M0 L! i" p. ?) u% X
4.执行 /opt/tengine/sbin/nginx -m 时有警告5 S! _9 s  }& h/ r1 s& u

  L4 B6 n3 z  h, {; }, q  GTengine version: Tengine/2.1.0 (nginx/1.6.2)! a! H& R! I2 L$ X$ W3 ~( M! ~
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
! I+ K6 x. Y6 s& o* h& Z原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
$ i  g5 R' f. _9 g1 R+ Z/ D/ T+ A
3 y6 m1 U9 B* f3 H  Z8 @1 [2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.5 U; s; l# w: }5 b; `; A! |: r( u8 Y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
: g+ W/ q0 m8 Z7 s2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
5 }$ W3 n3 q- s! Z- g, }3 F2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
+ ^# P3 N! t2 _  q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
; Y# r$ N- l/ i0 X2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
! r5 @2 O) f5 E4 D/ m解决方法,移除低版本的APR (1.3.9)
* ]* V' Y- h9 l; {+ `  ^4 j6 N0 X) I, t! z* _2 ~& `. E$ e0 @
yum remove apr# r8 O% y, C7 l, ?& ?' J
5.Error.log中有: Audit log: Failed to lock global mutex# _/ d+ w8 P2 F( c
/ ~' ?( {& J0 f2 L: `% L
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
8 x4 g' x  A0 Qglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]' k1 g! o' v7 ~- r$ @  _) d  `; K
解决方法:' ~( f  l) w. f9 w
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
9 W* `7 @0 F8 z2 K* J) Z
- H! @. j0 V; c. d. p  s6 K% eSecAuditLogDirMode 0777( l( C8 a/ e0 i- z( u: }+ ]! b0 @
SecAuditLogFileMode 0550! L9 j2 b9 v! `4 m2 W& v
SecAuditLogStorageDir /var/log/modsecurity
* m3 O+ B* x& r  D$ WSecAuditLogType Concurrent
3 p; v. F: O- [2 a# O参考文章:0 M$ `& N& Q' m8 t( }2 D3 g3 E
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
, T2 Z4 m  T0 X7 ~/ Xhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-12 08:31 , Processed in 0.068132 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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