找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12207|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
& `+ ?8 Q) [1 r! P6 D# A" l- Q, g3 ]5 b
一.准备工作
* D2 v7 g$ R& H. D& E
9 r2 o" Y& ^4 N0 N! ?% M, G系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.09 k- d( u5 k# ~5 _
% k% F. N; P5 \1 @3 g6 s+ y* x
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz7 ~+ S2 Z2 P: m, L0 G

+ O; m5 v7 y8 C& {modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz$ b& z8 \- R" P" {7 p/ z
* I0 v! l" C- t1 H
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs4 Q4 o$ f  R' a8 [& b  J/ E7 W
" J6 z: d+ ]! c7 j3 u" o
依赖关系:
4 ~% k6 V: T6 A$ ~tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:! I0 f+ w& H( W+ T9 y5 S

+ h1 A1 }' X. F( H0 [yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel9 d1 @( f8 M& q* {9 a/ H+ h
modsecurty依赖的包:pcre httpd-devel libxml2 apr
. B8 v! U8 ]8 v% h" D  [
8 n+ b8 m$ [4 \! _! O5 Y  C7 J( jyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
( `0 U# H6 t  U# b8 P二.启用standalone模块并编译
1 G+ q2 M5 |$ |& o3 f( ~# w3 G0 E9 y/ P' R# \, ^
下载modsecurity for nginx 解压,进入解压后目录执行:+ U0 `  @! Z& R. q
- N+ i; W, L( }: ?3 P/ j0 D1 p! @
./autogen.sh
& P" S: t' h; [6 ^8 J6 R./configure --enable-standalone-module --disable-mlogc
) x& l7 }1 s- l1 U3 f% cmake
. }- d9 @4 P+ N8 K: f" p三.nginx添加modsecurity模块, ^; p) \# c% b( U+ q6 D6 F
( @8 l6 F1 B3 p+ B$ l  J
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
: }# f$ _) q9 }0 F# I, z5 ]9 o; v% W% Z5 T
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine! k- K2 Y( l  A) K: z5 n9 }+ h4 v  D
make && make install
. k  i  z+ @* A1 Q: ]四.添加规则9 \4 J* R$ O1 ]; Y& L3 p, T
$ ^+ D4 b' e% I+ U9 _3 H
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
; k+ I: r* x! [. @- L4 C% h5 I$ E4 k: s. t! i% j1 ?
1.下载OWASP规则:
* X9 O9 o4 c' y. `; o$ o7 w8 q8 |+ K, Y+ L; K  T% t, l+ l/ K
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
- r! V& B+ d. T5 t2 a& N6 E+ R& |+ z7 h0 @# t+ _8 b
mv owasp-modsecurity-crs /opt/tengine/conf/- c$ j2 k5 n# i2 \: s8 _& D

! j. T8 |! v4 e6 i1 I1 ncd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
: q- S1 y  x; P1 d. w2.启用OWASP规则:
  ]0 L" {4 H( ~& F2 U) A1 O$ e
# X( T; v( M% S! b* b7 }复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。/ L: i2 d& r+ L) ]: I

, `, J: o* K8 _' X编辑modsecurity.conf 文件,将SecRuleEngine设置为 on7 F* J6 \9 K0 Y8 M; N/ t  t. e

- D' G! C: R) k+ L& z) Eowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
& h/ z8 ~2 w, P, `8 d. t: @+ `
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
/ n+ E- p1 v( iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf, W. i) |5 S  e1 W8 r; |$ [
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf0 }. H0 ~+ B' M' S% k, R
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
9 S1 u" @9 |  ^5 x, eInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
( Q4 u! c8 x& I0 X; oInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
% T/ `* s; l6 R" Z) IInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
3 [" ?, ?/ J4 W五.配置nginx
  n3 l  |0 d9 K7 i
0 K3 u3 h+ e& r5 t2 `8 c% L在需要启用modsecurity的主机的location下面加入下面两行即可:
8 E! Z' I" P: z9 ]
' |; v$ F2 b1 l4 N% u. hModSecurityEnabled on;  9 Z8 m3 W( _' P5 [! x5 \5 j6 h
ModSecurityConfig modsecurity.conf;, i( m/ x3 P, `1 O
下面是两个示例配置,php虚拟主机:, G1 @8 `+ z9 g! y0 A% M3 [; I
6 {) i( ^" ?& X4 V0 B# {) r
server {
. y( q# Q: g. n9 F1 R% h5 [6 t      listen      80;+ \* D9 G9 X! n3 n1 c/ `% c" _; h
      server_name 52os.net www.52os.net;
$ i0 W; L9 u" {! g! c7 p     
2 K! y( X  o$ _& v8 Z! Q4 H, k$ K      location ~ \.php$ {
3 G$ E8 v8 P7 W4 y$ Y: m. Z1 D1 B      ModSecurityEnabled on;  / r- E3 _! V, B  k! O5 V$ G* y- ?
      ModSecurityConfig modsecurity.conf;+ b7 P9 B; j) p8 M2 }* y

# w+ g9 o- e1 K      root /web/wordpress;7 g: b& K+ T/ T$ J
      index index.php index.html index.htm;7 L% Y! |6 k" M2 r% a$ N3 V
  
4 L7 L- w7 @1 p; r% P      fastcgi_pass   127.0.0.1:9000;
4 s# }* ?: ?+ u" Z$ i      fastcgi_index  index.php;' b0 d( h/ l+ J5 F
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
  \* u) R) ^8 q5 ]% L6 x      include        fastcgi_params;2 ]$ G5 `! }/ b* R) ]
      }
# \. s( L: K- r  }
8 b$ S# _+ |2 [upstream负载均衡:, _) s" H* [( y, [. k. ~* F3 E
7 r! t* [3 f2 `4 Y1 Z
upstream 52os.net {
" w+ ^$ Q: x; J& ?- A9 A    server 192.168.1.100:8080;5 b7 L) ~" b& O; }1 Q6 m  C, x
    server 192.168.1.101:8080 backup;6 _' y% [4 m- X# ^6 b8 l
}6 o" n0 J- S$ _3 c

" u+ N% {. i1 _& w3 b; x. o1 g: nserver {+ v$ a$ M4 O* v3 z, O
listen 80;
7 j( B1 h2 C4 p  pserver_name 52os.net www.52os.net;* l' r# D" L; o1 C& E+ t
# A! ?- C( j5 [$ J
location / {
8 L4 C9 _. D# L    ModSecurityEnabled on;  - E5 T; q  e2 Y& P' R  J7 q. |. T7 N
    ModSecurityConfig modsecurity.conf;  
. v" \! t1 p, q- X0 A9 {* O! j1 C+ T+ D
        proxy_pass http://online;9 S1 a1 ?$ R$ M- M+ o8 t. ^! W$ e
        proxy_redirect         off;  C1 @9 x+ p: G# l
        proxy_set_header Host $host;$ L9 |& f9 |, @
        proxy_set_header X-Real-IP $remote_addr;2 X* a" E/ C) ~: {
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
7 L' J9 O; c; _6 R0 l    }
, V0 q% M+ w4 T8 |; M3 H8 }}. r! W" D: I/ ]3 a+ q- V9 _2 G
六.测试# M$ ~) i  \# @1 L  j7 c
' A1 W7 N. {5 v# H
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:; b" L" [# w6 [/ p; `3 u

# h9 R% E8 y" m( ^1 e9 i6 x<?php
, G" D# ]9 l# f    phpinfo();   
. i* p) U4 ~$ k4 H?>
3 z7 A4 y. r: ^7 |$ w在浏览器中访问:8 o& _6 s7 d+ L' Y; V6 ?
# @7 }$ n! a8 a% s" q2 Z* i* x& x! n
http://www.52os.net/phpinfo.php?id=1 正常显示。
+ C' L( O1 a$ q- x, H9 mhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
7 g5 b2 l2 T% N0 x/ khttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
& d/ S" _6 [# D7 `- j4 H4 x说明sql注入和xss已经被过滤了
9 @; |8 X3 s# N" q9 w/ y7 \; I5 b" {% a# x
七、安装过程中排错
& ^: E6 U$ [% j$ w% M  `
1 n8 e7 z1 l' d0 d' f, r1.缺少APXS会报错
% d0 A/ m# q& e* V0 J0 v) s9 ?6 o3 ]/ G  {' \$ E5 L
configure: looking for Apache module support via DSO through APXS
( `: f9 j& D) econfigure: error: couldn't find APXS
/ U& K' [) r6 _' \  k4 S) M$ Mapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
; Y' ^: c0 P% w* w4 P+ u8 z) \解决方法:
, b4 I$ p7 M0 |+ Q1 r2 t
. b: ~5 \7 R2 t4 Gyum install httpd-devel( o; |! [' I2 A- i" Q- e+ t
2.没有pcre
8 v& W0 B8 t! A2 g: X- U- ~. d& M4 U. \* n& H" m
configure: *** pcre library not found.
% b+ I6 \5 p& n+ s; s6 E- N8 Aconfigure: error: pcre library is required
, t0 m/ Q- C/ W' @解决方法:# m8 m. ^& u1 C* m
* O- ]4 U1 {5 M* Q1 p0 O- o
yum install pcre pcre-devel
6 x, h" k. x" y. y% O3.没有libxml2
4 I: C" Q, B( @5 h# A) A0 }7 h$ b5 q3 c  B+ s3 a. m# Z: t( N
5 s; M% C' X5 }
configure: *** xml library not found.& ^% Z9 Q6 G# U4 x: @' l
configure: error: libxml2 is required  y3 m7 Y) d7 l2 U7 e* i+ w7 {$ f
解决方法:
; p; R9 P- W5 \0 H# s- K2 H# ?6 h8 Z6 m) O
yum install  libxml2 libxml2-devel2 q8 l7 n/ r1 ~1 P; X) ]5 g5 ^
4.执行 /opt/tengine/sbin/nginx -m 时有警告
% a, U& f& ^4 {. H) k3 u+ Z
% w8 Z. w6 @8 Q) hTengine version: Tengine/2.1.0 (nginx/1.6.2)
  M, J2 z# }* K& v* ^: n$ anginx: [warn] ModSecurity: Loaded APR do not match with compiled!
: A9 ~- r: z1 g) c0 X1 `原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log; A/ h: Y2 A; K
; G2 ]9 b% B' v: b6 v+ I
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.) P3 {. o2 ]+ G. T- s3 E( f' d5 t! ]
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"; k# u0 k( O8 l& W2 i+ R( K
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!' |" F5 e4 ], V' `; d% `% w$ d! L
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"  }$ V- j1 x' M. Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
. A9 e( N% B/ Y3 s3 N2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.. }! K4 h* {9 j9 c8 Z! {/ g$ Z! T
解决方法,移除低版本的APR (1.3.9); A* w2 t# V2 T0 [$ g& O" h

$ X4 Y- s# L2 Z. Qyum remove apr3 o5 |5 K; b5 K- c) v# p5 `  D
5.Error.log中有: Audit log: Failed to lock global mutex
1 V$ g5 r/ [- v* j( `2 I$ @5 ?1 P# U" S" c) S
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
% g$ l; u8 i8 o! w. M6 X6 S, |global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]3 A1 j& H& e% ^# H, m2 e0 n0 H
解决方法:6 a$ c  x2 R/ [& i
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:7 n8 V  G5 Y- a7 K* l/ K- N, N

8 t- [  \' S$ p% u; ~' X0 D, JSecAuditLogDirMode 0777
5 u% m! D8 J: n& E; qSecAuditLogFileMode 05506 C4 @" U: E" o
SecAuditLogStorageDir /var/log/modsecurity& A+ y" X8 m0 Y( v2 q0 A+ \# {7 ^
SecAuditLogType Concurrent- ], k( `, `4 J  T
参考文章:
+ m* b- z: N9 I7 khttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX+ z9 |+ t+ `$ Z
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-16 09:46 , Processed in 0.060280 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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