找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11943|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
, X9 V" D" Q$ f2 }8 R* r) o$ K% k2 q) j8 D
一.准备工作! {( I9 h: g- |& ?
" P, Z! Y' K9 G2 v3 P
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0* I$ m* J, J' R, n, B" x, G' P/ k
5 `/ O, r3 ?: f) N
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz/ X* t8 y2 G4 \# l7 `9 I2 l

8 a* u) I: o9 E8 K+ Rmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
! i! }6 _, m9 X. V- J- e; }! e  |1 X  |) z
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
+ |" T! E! f  {, o+ X2 v6 x% a+ R1 J5 c3 _4 w/ ?' [/ v. L
依赖关系:
) p$ w. K# l: z$ |tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:: R+ B# u7 X+ \6 H5 w6 j
0 c5 l" I: |0 G7 _7 h& v
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
) Z, G4 v- U' ^4 Qmodsecurty依赖的包:pcre httpd-devel libxml2 apr$ x5 d& Q! V! k  o! H! V
; I$ ^$ D, U* ?" G& N  F+ X4 d' ?
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
. m$ G6 r- p. P- F! `# a4 `) v二.启用standalone模块并编译
3 k0 G' Y: N& B9 o* h( b4 Y
; n2 ^# O* H' }( P; R下载modsecurity for nginx 解压,进入解压后目录执行:
) i* I. J* P+ c5 [& o2 {! W  k8 w8 f/ Z/ z8 q
./autogen.sh
/ o) U+ z* Q. m" J- r, z$ G+ L./configure --enable-standalone-module --disable-mlogc1 L( m% D1 ]0 O0 G1 H7 B  |
make , a% m  \6 P9 y$ r+ H# M1 c
三.nginx添加modsecurity模块
6 A. L& I! a$ @
4 l3 L! {: i- b9 J3 M4 @0 O4 l; j1 X( e在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:, b3 g. o- }1 u0 j6 B* Q" ^7 i
7 x: G. J- x2 C5 S. B
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine! s8 f9 N4 u' J. i2 P
make && make install
, Z! a! y- s* Z" a四.添加规则( X1 ~& f0 y+ ]" K& E

* R* N7 K9 `8 {; s, N8 Bmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。- p% x; ^6 K2 u: y" B1 D7 H# Y

" [- i& E! i2 K* R1.下载OWASP规则:
! u& \/ n8 Q! p) I5 F5 ~  }" d- ~# U: x7 L- e! h
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
3 l# m5 w: G' A; q
6 ~  R) y1 w! K$ ^$ U$ d- G  wmv owasp-modsecurity-crs /opt/tengine/conf/
! Q9 z9 O8 H2 Q  l
! E" W3 [) y. r  ]* M6 {cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf) I! r7 B7 d, f5 r
2.启用OWASP规则:' T4 E8 n1 R7 k5 I* e! x! m/ _

1 P6 y; ~4 d3 F: m7 Y9 v复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
6 |5 R3 D; D& T
$ d0 Q- o" |* [# `4 J编辑modsecurity.conf 文件,将SecRuleEngine设置为 on5 C, w4 i" [2 f, r3 X# h

+ k* c7 n, I0 T! H, ^& W' [: Kowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。$ C3 ?: `) m5 |' r4 h+ a

  u9 h. ^5 \: S2 }+ fInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
1 F5 h3 N/ G* H- mInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
; B+ v9 E1 h8 x/ P: X$ E; CInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
4 c& p# M& W6 B( t6 |# TInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf# D+ x1 d1 ?6 U4 L! k
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf8 f$ n3 s6 Q. {# E$ O3 D, V  t! T
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf% b# m8 A) P" j9 a6 u1 z
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
$ V# ?; c" B% f& \五.配置nginx' u' w8 h  q; _$ k- S7 h$ K
9 \; r/ _# g! O2 v" Q
在需要启用modsecurity的主机的location下面加入下面两行即可:" R$ J( X& O. Z) Y3 S3 w! t

) O# V  b& k9 cModSecurityEnabled on;  0 s! u/ K( w- t
ModSecurityConfig modsecurity.conf;$ V- C0 s6 h7 P
下面是两个示例配置,php虚拟主机:
8 j3 W9 W0 V; f! z) Y0 `: }: H- t: h/ j* y" Z4 c2 I! F; W# l
server {
& @- W& b! q; c7 ]  ^      listen      80;; c. {, S2 o4 H* I  t2 w6 x4 `
      server_name 52os.net www.52os.net;+ f) H! h5 m! n0 s$ ^+ V/ p. r
     3 I+ C8 S$ |; r* f& ?
      location ~ \.php$ {
* \2 D/ x* u) N' @2 N$ N' M) ~# n* J6 p      ModSecurityEnabled on;    p2 }" P& y6 |' ^' O) }- X3 }
      ModSecurityConfig modsecurity.conf;. c& v3 R- F( p' u# c6 d

6 H7 k* ~. f6 g5 Z; Q      root /web/wordpress;! G. S$ I  x9 {* X& U
      index index.php index.html index.htm;
8 G, H8 Z  U( @5 B* o. v5 G  * _' y# q+ M/ D1 b, e5 R
      fastcgi_pass   127.0.0.1:9000;7 `! K. H& f: v% {
      fastcgi_index  index.php;  _8 i! K2 w, x9 S- J
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
$ a% G- P/ B6 ?6 F2 I) u5 O# m      include        fastcgi_params;# S( |; N8 O, {8 O+ n5 G
      }5 n; u/ ]2 Q& g/ g- q
  }* _8 V! {( ]" D  s7 }5 G
upstream负载均衡:7 O7 k! J) Q! N7 d/ a4 X
2 H/ T. f% H8 J" X
upstream 52os.net {
6 S9 p$ U- w1 p    server 192.168.1.100:8080;
0 l8 T' G* G6 U2 P% B    server 192.168.1.101:8080 backup;
% u2 M7 [3 p' R& N5 t, ?}
* V# F& x/ C% c
" v( q) @8 G/ M. R! W  Y( w1 ]server {
& P' O. n/ E" U' |* x. n$ @& zlisten 80;
# J0 V! J. |9 h- B" d* ^7 N+ zserver_name 52os.net www.52os.net;, I3 o/ a0 C/ j, t3 [+ R' r0 t

$ @* R# O5 e! a' u: |location / {
' @& J# p) J! e' X  R+ c; `" g    ModSecurityEnabled on;  3 m: H; @" p( g# K' [3 P
    ModSecurityConfig modsecurity.conf;  
8 |% J# w$ ^7 a6 ?3 \5 G4 _1 x5 K" z$ Y6 K& w: D$ n2 |2 I- ~2 J4 d
        proxy_pass http://online;" E$ D/ ?& @# P7 q
        proxy_redirect         off;
" F, p' \. S+ u' i  O        proxy_set_header Host $host;
  T" L- @- g4 z8 q$ r, p        proxy_set_header X-Real-IP $remote_addr;
/ w0 D: o* A( @& e3 K        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;6 D; t$ p2 q) y6 ?5 H( B3 x
    }2 a2 j% ?6 t: C9 Y4 y6 B( g
}
$ j: F, n2 `8 d" J# q( E+ W六.测试
$ B2 f7 W7 ^" r# Y" G4 I: u4 _
# Y5 s' u0 }: g我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:% k: Q6 X. I: r- x4 E
; D# c/ Y8 o, t# p; \' ]8 P, ]
<?php
% K  L5 J; H/ u- y: @& [. B( r% M    phpinfo();    4 h$ N; u% V& z& I7 }
?>
1 D" q3 u. T; ~* ?% |在浏览器中访问:3 d9 Q2 X7 ]1 q2 E
& Z; ?+ a4 P2 k  a3 R1 G
http://www.52os.net/phpinfo.php?id=1 正常显示。
4 ^) M6 ?( f0 E6 ?. L5 Q' `: hhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。* h3 h! O! U6 k: _4 g. O
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。4 e# X3 }' C( F: Y" s
说明sql注入和xss已经被过滤了* ^+ D) J" [; _1 i
1 I& ^5 t# O; G" m' _
七、安装过程中排错2 Z. ?7 r" u  M9 D- _! _

; t3 k2 x5 r+ T$ j1.缺少APXS会报错0 H" `( l5 O' Z/ x% ~2 E
) }3 @9 K4 q& @! [) K
configure: looking for Apache module support via DSO through APXS
3 A1 X8 n+ Y8 s. W! T; oconfigure: error: couldn't find APXS7 k$ E8 p1 ?1 D4 E9 \1 A
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。0 B$ F  }  a2 S6 B1 u, {; F
解决方法:3 R" p" ^# Q+ g/ i) j2 ^$ y
3 Q( w, W5 {" K2 g! }0 f
yum install httpd-devel
# M+ R! R  D7 i, ~2.没有pcre% x! d1 Y, z3 ^$ m/ I( z
' V5 x9 \  {% O( g: i
configure: *** pcre library not found.) v( L1 U6 i. C
configure: error: pcre library is required# b+ h, Y, w/ R* U2 X) h
解决方法:7 y/ b+ f+ J) r7 L: B
( \3 j" f3 u: q- G
yum install pcre pcre-devel
" }) C# O) i) [+ k8 P1 r$ D/ F  q3.没有libxml2
. J( H% j/ t" }! c% X; y& P! @7 w0 }2 G
1 z# B3 d+ g: G1 u$ _. _
configure: *** xml library not found.
' D! j( I& J' a2 ~) q/ u" @( J  fconfigure: error: libxml2 is required" P6 T& @! a8 K0 V" ?
解决方法:
8 E% G# s' A9 O) b
+ L$ ~( H% |/ m: @( s9 Xyum install  libxml2 libxml2-devel5 W7 {& P1 n0 U& N
4.执行 /opt/tengine/sbin/nginx -m 时有警告
! r8 h& Z& H& K
( g$ H# s8 ~) r" ]4 \Tengine version: Tengine/2.1.0 (nginx/1.6.2)# S. M- |, }. O9 R* k* \
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
! u+ p" I; Z/ r( @原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
$ Z! u) z; ]$ m% s
# D$ ^  n! z7 ?% i, L+ E2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.8 t- c  _+ c! Z' E  c  |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"6 [' C* C; z* {) W' N4 E
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!1 c8 M& `0 _) p% ?1 N% \& g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"7 A, I- b/ S8 \3 q4 M+ ~
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
6 A' V- {1 T7 n. c# r8 p! `( q, x2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
. W2 M1 A9 K- N2 A解决方法,移除低版本的APR (1.3.9)
. I( ~7 X! |3 Q9 d( a. n' Z
2 G* i8 m5 `$ Pyum remove apr
8 S3 x+ z8 v9 g2 t5.Error.log中有: Audit log: Failed to lock global mutex
+ B& ~& I9 [" }! G' l) [7 e% ~' z" p( m6 V: u
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     % [! z1 z& J4 Y- O  m
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
/ T% S5 ?9 ]+ D* m9 u3 X0 h解决方法:& p3 W3 Q9 ?  r1 o4 T8 {
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
& o3 A! W' p& }, v0 I- b, n2 J
* B- J: D# [4 B" G- MSecAuditLogDirMode 0777
( q6 }3 O/ T; ^) T& Q( E% FSecAuditLogFileMode 0550
4 F% g! \+ ?+ @5 JSecAuditLogStorageDir /var/log/modsecurity/ M% S! ~6 V0 ]
SecAuditLogType Concurrent6 e8 L- @/ F( Z+ ], R0 g: D- p8 D
参考文章:
! L" {# w! b/ ?9 R$ Whttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
, e# ]0 T: D; P# E; l0 Jhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-28 20:33 , Processed in 0.074449 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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