找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13261|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。/ N4 Y' F, D" {) u
: h$ t* M' r! S2 k* I4 z/ {
一.准备工作
: A( s0 H- q0 n( E
( s; [3 h" ]7 ^3 O) f系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
4 C: Z* K& W# u3 F. E% J" V; K) J8 |: @8 }5 [( ~: |: S7 N; \
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
3 H' B0 l. ]1 `1 V/ A; H& `+ Q
( @& P' r0 k+ T: n! smodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
5 G" E- E0 F5 Y; y! E! H. j; q. O, r' ]5 e
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
$ o' q, _9 q3 ?& t
+ N! O; J% s$ }; L" C依赖关系:4 `, M4 X& j, G  O6 R. K& B
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
+ ?0 G& V; e/ i& j6 \8 |
* X9 n% L, q' ayum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel/ x& q* H1 e  C4 j% K' I; m
modsecurty依赖的包:pcre httpd-devel libxml2 apr
' ?: H( D7 Z3 S! F/ T6 D/ c: e- D: ^7 w
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
( y% j) g- D% T% ^6 Q+ @二.启用standalone模块并编译
' P, x( D) J8 ^; z4 ]- F5 B4 [7 r! j# `5 c5 B' P0 ?
下载modsecurity for nginx 解压,进入解压后目录执行:
  Q  J& k( X! j0 F- J1 g9 x, {$ k  V
' j" W/ |8 @. M./autogen.sh& `! y; e& @4 ]6 c; O) d* n7 Z
./configure --enable-standalone-module --disable-mlogc
# y/ J) V: Z1 h$ q% ^1 ~make : r: F1 ^/ S+ V4 s7 b5 ^
三.nginx添加modsecurity模块! b8 o0 f+ ~+ q2 Y
( D( \" o. l/ ~$ l/ s# |
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:6 Q2 X) S  y0 L7 _

" x% Q8 i1 d- I./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine& b+ D, V( A! r; K
make && make install
7 D+ P- b, l4 {% f: q! @四.添加规则8 v) O, N3 h  q* Q  R$ J
0 |+ j0 @# O; w
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。$ d1 h# s: k& B3 p7 ^. F* R

! Q% \9 l% O8 z1.下载OWASP规则:
: h8 R0 }* U2 \+ u+ J
! E6 ~5 N* C+ r, [- a; J# K. ]git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
4 @! E1 S" Q: M8 F) G) D) ]9 H9 G% b% N2 v& W: T2 I- X) q
mv owasp-modsecurity-crs /opt/tengine/conf/+ ]$ t7 x5 Q2 R8 h4 [
# Y- @% v4 j6 ^' B1 x
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf; F: F2 \' V' U. D, g
2.启用OWASP规则:
9 b( u; r; g& u8 S$ ^, p! Y
4 L% G' W  }& N. i* m/ S' y) ?复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。: T% \7 _( t$ d8 O0 l4 V2 r) d
: N5 h9 ?, X7 b+ ?
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on4 i2 k2 z5 O1 z. B% u9 x

5 R! c6 O+ l; l* c) `- Mowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
4 g7 \" p5 Y$ k) ]! t4 f# H# q/ [2 }/ w; Q* e
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
' p" M. {4 ^/ f1 y9 Q( ]2 uInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
1 M, }# W% n# e/ I0 |Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
6 |2 ^: ^, g8 ~% R, _4 O& ~Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf; E0 J" `' o- m* G1 C
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf: u0 g1 y( G0 T* z4 j
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf2 L: B' Q: P% [
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
+ w2 ]; z0 }; K7 {, i; a5 m' z2 @8 Q五.配置nginx
$ x! ^& U& M# g0 x9 M% r" J9 c& K2 C  V! l% y$ M
在需要启用modsecurity的主机的location下面加入下面两行即可:7 ~2 E& G/ T: G9 b

& Y: T; ^0 s, h. R4 C) {$ f9 t2 fModSecurityEnabled on;  
" w1 z0 U; c' O, w. u1 |5 E% oModSecurityConfig modsecurity.conf;0 X5 ?/ H3 J* T3 U9 Z' q& [
下面是两个示例配置,php虚拟主机:
& K$ |; Z7 _0 {" |, N% E( J! a0 H( w1 c" ^( b$ o
server {3 S" L) P5 e0 f  Q
      listen      80;
; U$ x2 }1 `2 L- C      server_name 52os.net www.52os.net;* x; V# u5 t5 k% t, d; T
     
1 p. X8 X$ o9 ~3 E      location ~ \.php$ {
/ ?! k, `4 z) M. l      ModSecurityEnabled on;  
$ D: k3 w5 K5 x5 \4 A( W& [( v      ModSecurityConfig modsecurity.conf;
8 O6 G. q. s7 N0 d, g) T' e/ {; V7 {* Y( l7 i% ^/ y
      root /web/wordpress;
. [6 J) E& z. u6 t/ |" G      index index.php index.html index.htm;5 f0 V  T0 A) v  F, u
  
3 W6 P. B4 K1 K      fastcgi_pass   127.0.0.1:9000;
) P% L  T' }) _0 b      fastcgi_index  index.php;7 o( E0 K; o8 J2 J" o6 U4 i
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;( |, O/ B4 ~9 O+ m+ e8 b
      include        fastcgi_params;
' M# L+ H0 z% v" @/ g7 U) h: a( R      }
" I) n) `% x1 j4 ?4 ]! m  }
) a0 p  `: v1 @) A; t: p2 X4 ^upstream负载均衡:
4 D; ^. S2 U: Q& F2 P/ s% M9 K1 x# v: C& O; n
upstream 52os.net {
' y  P0 G* X; N1 N6 G& N9 i3 x    server 192.168.1.100:8080;
- ^6 C" U! I2 M* y4 p# ?    server 192.168.1.101:8080 backup;
( t) V: p- f1 g% l4 h0 M+ R( v# z}
* p8 }  S' D% _- d( a* A5 P% Q6 E" ?  [) T* C
server {' F  I7 D0 f9 i, Z/ p
listen 80;, @! l$ X1 V+ J( g
server_name 52os.net www.52os.net;+ v) K5 S* q; [' V
3 l6 R) D4 j9 l$ B
location / {
7 b1 v) H% A3 D+ D* y    ModSecurityEnabled on;  
- j, _( D7 [: [: I" n  A1 S: Y# d    ModSecurityConfig modsecurity.conf;  ( ^3 d; C% n4 S& V- A& w$ J

* E! V: l6 S5 e- N$ v! @        proxy_pass http://online;* \  @  g* f6 S
        proxy_redirect         off;% |* T' }, h. n1 d3 u& d+ o
        proxy_set_header Host $host;- B8 E, q9 a( n1 k  Y3 B& T2 O
        proxy_set_header X-Real-IP $remote_addr;, P4 y6 Z( M' ^) ]4 _0 l
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
  \+ m, D. K( t; ^, r& G    }$ k, H2 V4 g$ ]% F7 r
}2 F4 ~/ o2 l+ n+ @3 p
六.测试
# ~) A0 q* \( C0 L, V, h( c- E0 Z, n+ Q" O( X5 x4 Z& X
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:9 `* v+ d. B+ M7 c; o1 z
! G/ o7 i, d; S- b( a* X
<?php
, T* d9 O  r& R* i. n( c3 i    phpinfo();   
. u( k! l7 z2 H& r: V?>6 O# g0 B0 n8 v, h
在浏览器中访问:. D  j5 e; z: `) N1 d( J4 o( h; J
5 O5 M9 X5 \7 F) ^0 Y% I( C
http://www.52os.net/phpinfo.php?id=1 正常显示。, C. ~/ k7 n2 H# I
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
: @! c- Q# j6 `" _9 e/ g2 vhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。8 l. l7 R; t* _" T1 v" a* E. a
说明sql注入和xss已经被过滤了! m1 \! N( E* i7 c$ r0 @

2 _: v% `0 _+ `1 l" L七、安装过程中排错. U; p3 ?6 q1 H, ^

; N2 m; j/ \+ I) S* @3 d9 X1.缺少APXS会报错7 g/ Y/ ]- D% F# F4 a7 k/ Z
! G5 P& F" j2 f6 l; p0 g  ?
configure: looking for Apache module support via DSO through APXS( Y, x' a. H# E2 }
configure: error: couldn't find APXS) v+ s; O4 x9 Y, I' J$ U
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。/ N3 z) _  v0 d% C. M
解决方法:5 k; I+ E* K+ o" N) t! ?: V

- c, E3 f4 p0 J' [& v# _yum install httpd-devel, E! `* P; v& G* z/ Y
2.没有pcre
  ^. k+ S  }' A, n% B( r- {* Y, C3 Y! w: y
configure: *** pcre library not found.
+ V: S! h1 v4 J" u2 J" w2 s8 j; sconfigure: error: pcre library is required
, R6 X7 f! u5 [; q! D解决方法:
# K, j8 x( S- m8 @! U7 u/ H9 Y1 j6 u( f9 k: W
yum install pcre pcre-devel9 B# i% B8 d/ t, ?
3.没有libxml21 @' p5 A3 ~3 c# q

0 M. L2 H* M& n- ~
7 J; v( _9 B$ econfigure: *** xml library not found.
( P; A: Z7 [$ x: e! S# wconfigure: error: libxml2 is required1 h. g. n) k1 |9 H- N
解决方法:
' A5 c) u* a9 G0 `( B# s7 n7 H( a' r7 f3 [. M
yum install  libxml2 libxml2-devel% Z. x# _7 p8 S
4.执行 /opt/tengine/sbin/nginx -m 时有警告( ]8 P. r0 y/ Y' u6 p8 v& e9 ^& N1 v
5 j) N6 C3 \# n5 E; `+ o
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
4 \$ Z7 t! y, @5 ^0 cnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
" F- s! w/ g" ^3 D( `1 T! m原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
$ [) |& {! o$ y( D3 r$ t9 M- X$ ?6 n% @- H! `3 o7 U) S  [
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.( Q$ {" ~7 u: H; v/ J
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"1 e4 j; h1 C. e4 h
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
; W( c% u3 F: r2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
0 i5 u  M( Y, u% s" ^% Z' `3 E8 ]2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
! C* u" B* B% `( }7 C* W2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.: @  o2 T  T& p! Q6 H/ @1 q
解决方法,移除低版本的APR (1.3.9)# `" M) q) a" a7 V
2 D& U6 ~& P- l' v3 m7 A
yum remove apr( q9 ^7 s) Q4 F" _/ s
5.Error.log中有: Audit log: Failed to lock global mutex; w3 c: W0 |# o) e* G: D7 I

! N, ^0 \$ m4 v) c+ [+ W* h2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
0 t- V, x* `/ G) S- Iglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]6 B9 ]" P0 W0 ~, u7 D1 e
解决方法:
2 n5 b' @! ~' X编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
; q6 [$ ~6 Z& A' A( w7 a7 }  f  {, S/ f/ j0 W+ V
SecAuditLogDirMode 07779 u# q' q6 s& Z$ |
SecAuditLogFileMode 0550
# J: Z& B7 y7 K/ r+ cSecAuditLogStorageDir /var/log/modsecurity
, ~# ~3 D8 i" BSecAuditLogType Concurrent
; E1 J1 p$ Y" \& G参考文章:" i" d( p2 i/ G
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
/ Y# `) E+ g! p9 s7 u9 Vhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-30 00:40 , Processed in 0.069036 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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