找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13002|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
: J, z, u/ X. {
8 E. u, K$ c; K一.准备工作
* X# N. |2 b/ C; G. ~
6 Z) d7 X* R& d2 n系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0" z+ a) u. w( T- l
% m0 R6 q( N6 ^, z' q
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
6 Q- c9 l7 z$ m1 X3 g* N
9 G7 i  n2 v5 w0 x! n9 Jmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
2 r# J7 t. P$ f! k6 [( N3 [* g  G/ C; j
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
) ]& R  N; ~; D$ y" f: o
* h% M: o1 |6 i0 ~( |9 d依赖关系:' l0 L( {7 h! u
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:/ E4 h3 Q- M6 R$ d1 n
1 O  u. L! e3 d
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel. b; I6 |' X7 k; g6 l) f
modsecurty依赖的包:pcre httpd-devel libxml2 apr
* M, Q. E. S) U' G$ B' A
/ w: h0 T! E& wyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel' X) @5 k  @. D+ R; N3 n  i- C7 v
二.启用standalone模块并编译
1 i- T+ \; b; H+ o: Y
6 D' ?3 c6 h/ \. ]3 E0 `& Y3 S下载modsecurity for nginx 解压,进入解压后目录执行:% M. c. Q; F8 i8 i0 P
. x3 E: S" w8 _+ [" z  g+ x1 L% h- m
./autogen.sh! F; {  U" D" Z7 q2 E
./configure --enable-standalone-module --disable-mlogc
+ {  I2 s: J' K; o: ?1 W5 V' Kmake   ^6 `) |# B1 L: S  E- S
三.nginx添加modsecurity模块
0 n/ Q' I0 Q6 }; x6 j
+ q2 {5 P, q' B在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
1 f8 M! ~' ~& k9 W3 D, J* @1 B+ m4 A) X( ~; Q2 |6 w( w5 B4 O5 C5 r
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
6 u: \% c6 G3 i5 `, ?' ]make && make install
% D/ A5 |9 \3 W+ R/ ]7 y四.添加规则
) s4 J8 k" l4 i0 H+ ?
+ E. a3 E/ _9 m: n; Fmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
' c) {7 d$ \3 |2 A* T3 |1 `  u3 p4 m/ h6 T
1.下载OWASP规则:
* W6 |  j; ?1 U1 w
- S$ E/ Y8 j/ A" k" @  U5 B7 Cgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs5 v3 F/ S/ o3 a
0 j9 \% Y  s- ^, H3 w% }
mv owasp-modsecurity-crs /opt/tengine/conf/
" |! P) k" o1 x. s: ~  o& S9 V+ n4 f
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
& R  n! Q- v3 O1 [3 W, \- `* C2.启用OWASP规则:3 v5 c/ w5 _5 R/ o+ c7 L& T! m
0 W8 w6 I; q- S7 l$ _5 C2 w; T
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。, l0 i8 h8 e! K) j
0 Y" Q0 Q; h2 Q' n
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on2 D2 a+ B3 P1 `5 J, P2 N* n
7 P) M4 h% v$ A5 L2 Y+ K
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。8 [) n% n9 r4 D0 |* a3 u5 j

  P3 s* ^4 T( [8 U; XInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
  I$ S7 }8 }$ Q! ^Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf3 h6 o: x% x: b! y2 Y- D
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf* \/ }0 N( B; k& P( d; E
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
/ v( Z4 B$ I* V( l& VInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
# P! G# f7 s1 o7 ^Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf$ c/ I+ x  Y) B3 N7 q2 O2 a6 Q# z
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
8 r# ?, b' H, ?' Y8 s1 F: s五.配置nginx
! j% o( |" S3 C5 s
# a( |) p; ?7 E0 g( I$ _# Q在需要启用modsecurity的主机的location下面加入下面两行即可:0 T' E) s. M0 n) E" o) P+ I3 @0 i
" s7 U+ p9 P/ {2 R
ModSecurityEnabled on;  ( ~" H, ^+ c' p' R9 |2 }
ModSecurityConfig modsecurity.conf;
% J" _+ Z6 ^$ E9 y) a下面是两个示例配置,php虚拟主机:" \5 x; G( H9 g* j2 |+ y
# s" r2 O0 A1 ^2 M
server {4 C3 l+ z# f( x3 F+ ^5 z- w/ b
      listen      80;- j" V% ]1 T- M
      server_name 52os.net www.52os.net;
5 }8 E; ~/ @) H     4 s/ I( ]! o1 N6 q4 \, n% R: P! z  r
      location ~ \.php$ {# [, u# B+ k' U4 j+ C: K
      ModSecurityEnabled on;  
, W% Y% @6 x4 k- F6 s      ModSecurityConfig modsecurity.conf;
4 }( |1 v3 h) R1 b. T% A0 |' q
9 `3 e+ v- V5 k3 ^1 _      root /web/wordpress;& K, c$ Z$ h! j, w
      index index.php index.html index.htm;" C& d# ^/ X9 v# f& K+ `& c- `8 b
  * U7 B6 J# H% F9 z
      fastcgi_pass   127.0.0.1:9000;7 T( Y& k+ p0 ]2 P
      fastcgi_index  index.php;
. A, v2 \& z$ T8 k3 M/ O3 @      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
: U; `  E# f  I8 D      include        fastcgi_params;
4 |7 T, [( _2 K* r# T5 l      }/ d4 t! d+ G- q& W$ [
  }- @  ^- m. K6 q, y; x' ]+ K
upstream负载均衡:
; O4 {# V+ R: Z5 r! s0 L0 U0 }! E# _& e  ?
upstream 52os.net {
/ I# L0 D' W2 M% T* w  m) J    server 192.168.1.100:8080;* m; }8 V2 o) k# W0 W
    server 192.168.1.101:8080 backup;5 l6 Y) R# [' L0 @2 B
}6 ^! t4 t% S- Z+ z5 d3 Z
; T7 `& r/ n( H% ~2 i
server {
1 ]$ ^, p  m: x" D8 [- ~: p# B7 H* Klisten 80;
! y! `* K, X  B* w! Yserver_name 52os.net www.52os.net;3 {9 e+ D& J2 g4 l

, B+ R/ G  p+ w7 D# wlocation / {9 ]( `, T. I  ?: ], G
    ModSecurityEnabled on;  2 o: X/ f4 o  b4 I
    ModSecurityConfig modsecurity.conf;  8 J, M5 k1 c& u/ H. D
" h# C% p, {8 _7 c3 l; [/ |
        proxy_pass http://online;9 l5 S7 r: h$ P! \% v  D
        proxy_redirect         off;2 v# `' c& v: b  {' s" A6 z) s, B
        proxy_set_header Host $host;" R2 m4 Y2 J4 ?
        proxy_set_header X-Real-IP $remote_addr;- |: u' S1 ]* ^, K( P3 X
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
: o* @$ T1 ~" ~8 g5 m    }' g8 B9 t# [# y$ a
}
% i# ?: o6 E' H, o+ j. u6 w六.测试
# R: F+ v" E* w3 n7 Q9 Z+ D: P7 n) h1 Q, \
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:( ^4 F) V" b% E% Y

$ s  m2 w3 K$ Y" ~<?php
, d" {! r- o7 d! K$ x4 h& q    phpinfo();   
" B1 ]- X( U0 G/ C6 \?>
7 }5 h) D1 n  f% [; Y; Q在浏览器中访问:' ~3 J/ O. w' x  W$ T& C' C- `

) `% r: a! x4 J- D) y1 Ehttp://www.52os.net/phpinfo.php?id=1 正常显示。
; A9 Y% T  S  ihttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。2 ^: V4 K/ S7 Z& Y- W
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
* B" S0 |# w7 u8 K3 n6 }+ {说明sql注入和xss已经被过滤了
0 X* C$ ~0 B2 c- Q% U5 z# D( {* ?9 I9 _/ `0 Q
七、安装过程中排错9 g/ W9 _! G* ~
3 j, V& N* [+ E+ O5 e9 t8 M' K/ Q
1.缺少APXS会报错
* M4 L: {/ a3 b2 N* I2 J7 z( b5 [+ }2 c, ]3 K5 z$ E% Y8 b! _: `
configure: looking for Apache module support via DSO through APXS
1 c2 t( H4 J8 j1 u+ Bconfigure: error: couldn't find APXS: ]2 x* E5 U9 P3 X
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。1 F( F0 m5 O9 R/ T4 j
解决方法:8 s$ d4 w  |7 x  c* H$ X! D
( N( l; Y9 N% X, s6 v4 S
yum install httpd-devel
0 d4 q, X. y. v0 ]$ o/ N, I2.没有pcre
: F9 F# c9 N/ c) r  ]: g: M9 m
# R( U1 }/ P7 ^6 `/ sconfigure: *** pcre library not found.
6 ]  n/ s; H, G9 ~+ b) u4 U/ `configure: error: pcre library is required  [4 Y! o+ s! ~  y3 c( u
解决方法:
6 y" i6 _7 ]3 v0 b! H
7 o! a8 e$ S( H" A) Oyum install pcre pcre-devel
3 t, i% m  b7 L7 I6 z3.没有libxml2- I5 q5 S* c" X/ B9 @- S

1 I4 V8 w+ s. Q* v, j
  O8 [- X% Q9 J. Z$ {6 m- rconfigure: *** xml library not found.& V1 g, q9 l  e
configure: error: libxml2 is required
4 x4 g/ g4 w7 W- O解决方法:# E3 W* _7 r* m" T' d

, q* K8 v% S. f% W# Cyum install  libxml2 libxml2-devel
' p9 d& S7 f% I, X4.执行 /opt/tengine/sbin/nginx -m 时有警告$ K2 O1 h% V( x% I& e

& r3 r6 l$ Z4 Y7 h; `+ c$ ATengine version: Tengine/2.1.0 (nginx/1.6.2)" I; |+ b: J; J$ H$ p# r9 o, A1 T! z# ~
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!0 |# _0 v2 }1 |- S4 E3 _" i
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log/ p, `* R$ r6 O' i# G/ ~. [

  W. y2 j8 q% F2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
) b. v) b) f5 }( Y- j& X2 T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9") Y/ J% X8 D7 U3 n7 c+ W
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
" Q3 k1 T+ d$ W! O3 m- e2 o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"$ z1 W6 b+ m% Z/ B
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
9 h# V% x. S! b- {9 t2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.2 ]. H/ u5 R  y9 X) H1 Z
解决方法,移除低版本的APR (1.3.9)
4 s6 b/ ~; I; g8 }6 [
6 y" D1 q7 j. H* x5 A9 jyum remove apr
, d0 N, v& |. @6 I5.Error.log中有: Audit log: Failed to lock global mutex
- b0 B: \6 H3 \/ i
5 M; \! t6 [$ R  p2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
. \. D5 D6 o* N. `+ Tglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]) S5 v) ?- p% j4 r
解决方法:1 e, f. |0 W$ E
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:! K! T% V! I$ o4 Q
( c6 `- g7 }3 Z3 O9 ?2 _1 N$ g
SecAuditLogDirMode 0777: W# L3 n! L* `3 [
SecAuditLogFileMode 0550
0 B6 m! V& X9 q$ H7 pSecAuditLogStorageDir /var/log/modsecurity. D2 i% ]/ J* p5 G5 P
SecAuditLogType Concurrent
. G! h" W1 ^% J3 z参考文章:8 `  D3 F  f! b+ X3 [0 o
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX9 c) _/ B& A( }( {
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-2 16:22 , Processed in 0.045283 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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