找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12965|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。3 P; C8 ]2 S8 z7 B( z5 H
9 c/ C/ \7 L8 p+ A2 |
一.准备工作7 Q; \( [% ?" q% e6 o; S
: q1 o& x& h7 S. S& j3 K
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
4 s8 S) n7 z/ [8 @2 |) g4 f0 I% A& w3 }
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz  T# d5 O4 K' ]3 h$ v4 `* ~& R
7 [/ V, x* O9 y: ?# M0 \3 k9 `
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz( f! f7 K( j5 g9 \8 s$ @

8 B3 z5 i5 V* d9 c: P" i9 f  H% ?OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs/ x4 i( O) p8 l+ I+ [7 ], s

7 x' r' e/ l  D2 D" V依赖关系:' q% b( ~' I; z# b3 y, ]* H9 ]5 J# `
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
" b' X! d* w( W1 B/ O; q
, V* X3 ]3 Q' Oyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel: ?$ }2 Y8 t% K; g5 N
modsecurty依赖的包:pcre httpd-devel libxml2 apr
' q& S0 q- f  K8 W& z
: T, \. R; b4 Y1 U* I: h3 T( z5 dyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
; g0 B5 Q) t+ w( Q6 {' P$ E二.启用standalone模块并编译; _* G* r! E! l* T7 E" i

, _- i1 ^  e% i5 O7 p7 y" V# C4 c  x& o下载modsecurity for nginx 解压,进入解压后目录执行:$ T( f& R; Q2 `5 _5 Q

; Z$ s. h. B( r./autogen.sh
- s7 D; R/ E% F* g) z8 Z./configure --enable-standalone-module --disable-mlogc0 u8 R& `; i" R3 L/ t/ W
make
1 S! Z9 d$ |$ z1 ^+ u7 U2 K三.nginx添加modsecurity模块
# ~0 `! N" b: e$ m$ T; }$ `  @$ H+ i5 O0 e% W. d; f
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
1 ]3 ?6 y' J. P7 J! K
: p; @, I% ^( S2 ^. U: O* k6 h./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
. o6 q- F" u- V0 Y* m, D9 ]make && make install
% ?  V' h# d6 D/ r! Q1 x四.添加规则! h5 u$ M" Y+ e+ E- v3 Z
) l5 Q- a3 g" h
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
7 r0 U2 m2 i2 F) ~" G8 y5 y+ j8 a" i8 Z. F# S
1.下载OWASP规则:
) ^4 l* Y4 D! f5 B7 T7 @) E; ^# D
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
, i: c- x( _; a6 l: u
' K# x% P, D$ v1 E4 B% b6 n) Jmv owasp-modsecurity-crs /opt/tengine/conf/- Y4 M) M& _& P7 [
, h, m6 R3 R* L& M$ f
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf0 a9 |* Z* ?: G5 {& V$ P6 i
2.启用OWASP规则:$ Z7 l8 l( n( E# ?+ c& {% n% j

( x4 c8 E( f6 X* g7 L1 z复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; A/ J# k" {( G
: E, b2 Z5 }5 M: ]! O
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on" H' `1 l4 ^, c) T, ]

6 _% R/ q# B" }" S& C( }owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
: {* e2 V; k; A  a; I/ }3 ?/ I' `% i& S
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf3 u; Z/ |8 A  k7 L- V
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf, X, k7 W0 Y( L6 c, J3 l" a+ J$ l( _+ f
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
6 @" j+ f, ^1 `& m" n" }Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf0 h! Q6 t* {4 S4 ?$ n
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
1 a0 H8 V1 m5 l5 ]" W# B( `Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
8 N  [: w* }  KInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
6 e$ z/ L3 ~% c7 \; r& R# p7 o/ _五.配置nginx
7 j2 J+ N+ N5 Z0 r8 Q
, m- `- o3 @3 p1 B5 l在需要启用modsecurity的主机的location下面加入下面两行即可:; F& G/ [5 r$ W4 ^/ T- \( I% q) B) K

7 p+ j0 G9 J; U6 K& t6 q( s! GModSecurityEnabled on;  
0 p7 x3 L. w  a. Q/ ?ModSecurityConfig modsecurity.conf;
  m2 d6 V6 q& {' Y下面是两个示例配置,php虚拟主机:  g! E- V6 D  a6 }! o# _- M# W7 T

+ y3 B$ o" H  [3 ]8 m/ Gserver {7 Z- H' X; |+ J) x) n) f
      listen      80;
* A6 u3 l3 g1 |4 K  I' s) ^. W      server_name 52os.net www.52os.net;
+ ]3 n9 G# \! u8 ^: ]     
9 P6 F# ]9 O1 v" S+ Z5 F1 t6 i3 m3 p: m      location ~ \.php$ {
& o; z9 Y8 g' x6 x% E( R8 L      ModSecurityEnabled on;  
, u% c3 M& Q8 W4 e$ t& L& K      ModSecurityConfig modsecurity.conf;# l; l& d$ b7 N: c
' f& `6 d9 H4 k, }
      root /web/wordpress;
2 z" W  o3 W6 g$ O9 F6 @  {      index index.php index.html index.htm;
/ M8 D1 C9 ^; ?3 A1 H4 t- X* ?  _3 B  ! V1 d" R# j2 r% d
      fastcgi_pass   127.0.0.1:9000;. Z3 I; m0 s0 ^3 c, L/ L& |1 ~
      fastcgi_index  index.php;* f/ ^2 S; {; Z8 ^2 a7 c
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
+ T4 X9 E- F* h' h. N4 S3 {      include        fastcgi_params;
' q) j7 r! ]5 g7 O      }/ v3 \& M# Z2 n8 l" w' B
  }
7 Q/ m6 F0 t4 r  K3 N2 bupstream负载均衡:) e2 E' W8 L) ?, q! U( E
6 W' w# I& |# [3 k/ \7 R2 x
upstream 52os.net {
3 c# h7 ~, L7 C2 t) D9 J) T& S  q    server 192.168.1.100:8080;9 i" C7 c- t# |" |
    server 192.168.1.101:8080 backup;+ j* ~) a0 G+ J: C
}7 Z  T  G# `2 W& W
2 X# ^' s; _4 a: p! j) o
server {
- A, v6 m% `0 {8 \% a3 Klisten 80;; A7 E( C! g3 F5 {: n* t* ~
server_name 52os.net www.52os.net;' ^% R7 G  T; v# W" l; `

; B" c" B; i3 h; b# B6 K4 f+ glocation / {% A& y4 J" |3 I2 d
    ModSecurityEnabled on;  4 Y$ G& [8 n& w0 b6 _  Y) E& Q/ A
    ModSecurityConfig modsecurity.conf;  
% [. |. d  q2 j
$ V) j" [- P6 G/ F1 I. R        proxy_pass http://online;9 A5 k5 r; W( K0 E1 t, X4 V
        proxy_redirect         off;) s* s! ?( Q/ J6 }
        proxy_set_header Host $host;
, G* O0 M' d9 [( e, C1 m  \7 J        proxy_set_header X-Real-IP $remote_addr;
* P, |7 d% L, z9 Q1 x7 i6 a9 n- j, X9 ?7 [        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
3 `4 P8 y! q) A0 r    }) R" K5 g) j7 y5 l4 M3 C9 B
}: E0 u: `; n/ u# T- F( n: Y2 R3 |
六.测试& `4 `: U+ {  h9 F& D$ u0 F1 ^

& O* N, v- o& }3 ]$ B  m# H% Z& b我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:* c; n  i! ?: H% Z' T! G/ ?

3 E! ^# g' r! `1 F! j  H7 ?1 W- I. x' |<?php' q% @4 M% [/ t% L* E6 I
    phpinfo();    7 L9 |0 x! ^0 \
?>
. Y0 p+ O  z- z7 v) F在浏览器中访问:4 @2 L6 h# c$ S4 t/ O& k

0 ~( x" w. R4 o% k. V$ p4 H! }http://www.52os.net/phpinfo.php?id=1 正常显示。
8 b& K# S/ t8 u% g, R3 U5 j  Dhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。( i: t( d, b' l4 a. n: J! R
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。- u9 k: `, e# v2 F
说明sql注入和xss已经被过滤了
& @+ G6 s5 t* v) F
: O. X# z3 v" L+ `. N- B七、安装过程中排错
, C- X! Q! W1 j- T
8 G" l$ i& C. j% j# W. \- p1.缺少APXS会报错  W% {5 _* d3 ^3 O

/ _9 t0 e+ K8 }' }' i1 Rconfigure: looking for Apache module support via DSO through APXS, S& Y' Q" N' R
configure: error: couldn't find APXS
" e$ q9 u# n4 `" ]apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
! U: }4 Q- w7 U解决方法:/ w, m  m4 R  z" K

. g, U3 K3 N+ H, uyum install httpd-devel1 U5 `3 s' _- D( a) t1 Q) s. R0 B! j
2.没有pcre
: U6 ~- S6 W' @& d9 u* }6 F( P2 \1 I; D( D3 h
configure: *** pcre library not found.
- f5 R7 ?# x# A* a# e! g7 zconfigure: error: pcre library is required
! }0 A# Q8 C; y解决方法:  m% m9 p4 \8 E9 h  M
1 N! n, I. N* }9 W& m( I
yum install pcre pcre-devel
0 Q# v  R) P. P# B3.没有libxml2
! M. Q" {+ ^1 Q# j+ ]( u6 ^" Y2 C/ ^. O2 U1 N$ f
1 I6 Y3 C! @) q- g- P& c$ W
configure: *** xml library not found.
9 B/ R4 v. L, O- @+ j! x8 Y1 sconfigure: error: libxml2 is required6 ~3 ^, f6 [6 ~6 @* t
解决方法:9 b! R5 Y4 Q, Y" d- ?$ Q. C
3 }2 M7 c0 `( M( b) G
yum install  libxml2 libxml2-devel- G  J. i0 z$ y
4.执行 /opt/tengine/sbin/nginx -m 时有警告% f* U/ u0 C1 y- j: g: g+ F% l

( S2 O: Y4 _7 I" N. fTengine version: Tengine/2.1.0 (nginx/1.6.2)) O, g) u( |3 z+ w, u4 r
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!& s9 N* d4 D! D3 ?% Z* a
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log2 ?. x6 e# b$ Y2 D7 W; Y1 o
% c% q; U3 w# h* \
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
5 V1 G, B: U, L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"+ X% y' a  K5 O9 J% y
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!! k0 v; o: o/ o! x
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
# a+ K/ P0 K6 X" S! @# q% C2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"9 N% J  N/ b5 n
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
1 G; @3 g+ j7 |' _7 d& ]解决方法,移除低版本的APR (1.3.9)2 j+ j6 |" P1 e; `1 v  V3 v( P- a

- m4 n5 }/ l/ k! S* N/ Cyum remove apr+ v' H" c4 Y+ M- ~: a% X( d
5.Error.log中有: Audit log: Failed to lock global mutex$ M3 c8 h; c. i( I" ?0 S# |

9 g# J# M: w% w8 M9 L* b2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
, _2 G+ a6 B* V5 Zglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
, A6 x& V: t4 m* _( s解决方法:' R+ _& i+ {4 w' j6 I+ i
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:7 M& _! o6 O) ^( W( y4 R

4 A' T* }( ?* H( p8 uSecAuditLogDirMode 0777# a) S  J4 d0 ?0 d- @2 C
SecAuditLogFileMode 0550
( K" X) E: h* Q) b) aSecAuditLogStorageDir /var/log/modsecurity
" Y, z$ T, V+ z- N1 G2 S: C3 t! I2 D5 xSecAuditLogType Concurrent! G% m+ s2 X5 a8 v9 S
参考文章:7 }# B! {3 Q# A7 A4 E
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
7 f- g, i9 U& a; {8 E* j3 Nhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-30 22:43 , Processed in 0.056451 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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