找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12002|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
+ D2 ^8 c& J) q8 ?( c8 |
( E/ ~( g# f0 g0 N) p# x一.准备工作
( u& g5 a% j9 \- k% z
$ E9 C6 C2 j1 T9 _! \/ Y1 T系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
/ l8 j9 @: j- t- S2 Q
6 {' R4 _5 ^0 ^3 A2 ktengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
  l( ~! X) B# I. f1 k8 ?  b
! p: _' E9 a) n2 F& C( [modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
9 \! g7 d/ z: O( e* k0 c! X) ]% U, ~5 h5 Q
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs6 y; \3 g. D' V; p3 O
4 l+ u# @. T2 V- Y! W" g% u
依赖关系:, _7 {# `- K& G8 q; f' s% c
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:; N5 O+ B; E" G2 |
+ ]$ F8 K" w( C, i" q
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel, q( `& M* k! }9 p( X5 c5 ]
modsecurty依赖的包:pcre httpd-devel libxml2 apr
/ E3 [8 h8 F0 t/ W; X- r& z  A2 ]2 y2 \$ t5 E
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel  Z5 h) y( J( A3 ^4 F
二.启用standalone模块并编译
( @' I# b7 f9 h# B7 \9 C$ }
/ v. ^5 _. G% g/ k下载modsecurity for nginx 解压,进入解压后目录执行:- X2 V7 {& S5 q8 H" r  O. x
: e: |; r5 Y  L
./autogen.sh
4 D; \+ c) O) w% D5 t./configure --enable-standalone-module --disable-mlogc
  b$ F/ d0 L- Z4 k# L/ {7 h  Lmake % R2 C: W& |7 R; v! S, [
三.nginx添加modsecurity模块
( K8 q% O/ S1 Y3 x  g& _2 D( m0 x
# V) G) I; h1 Q4 q, H1 y% o在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:2 \3 Q0 ?  v$ H
) I( {1 s5 t* T: J; S1 i3 g4 V/ v; |
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
9 V2 I5 |5 Q8 T: I3 o1 Emake && make install, [5 ?9 F8 y. H3 E3 Z- C) _
四.添加规则* P/ V8 S  @$ B2 q. r" A6 K  m

+ Q" B$ g) E+ x) Gmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。2 p/ `* y" D, ^5 g! ?

# W2 c: P% v1 ]  J1.下载OWASP规则:
: k4 ]" f$ h. ]3 B4 D" ]; A: M4 N
7 W* e- X3 u7 `9 _; G; y- t, wgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs% B) N, n7 E' Y" U

+ X$ c4 w8 y9 pmv owasp-modsecurity-crs /opt/tengine/conf/
5 ~( p1 s% Z; C: t: R! P- S* M7 P& R( R% e
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
+ ^  u3 C2 F) D# h; f' {, Z. U; y3 I2.启用OWASP规则:
0 V; z8 o. X7 ?4 N% q7 e
" \7 h( ^* _) T6 h0 A复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
& u7 S' D4 W# j* {/ y8 K
, B- ?1 b. {) n/ V* |9 R; |编辑modsecurity.conf 文件,将SecRuleEngine设置为 on0 F* u3 x- F  b% P! G/ d- l

; ~6 Q6 K& s1 A  kowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。. U: ^& F& E9 Y) }3 ?1 P# \
" {9 g' }* O2 {1 I# J  ?: V8 V% ?
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
# b: X( w8 K2 EInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
4 z' v! Z0 F, z& i2 t# @Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
, U% N; S" u& A/ m& P/ q$ J/ dInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
5 _0 P4 F) Z( j  UInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf7 z1 x- Z$ i4 h# W
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf: v8 A# p; S8 Y3 V5 |
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
% p' b9 x" k0 j! N五.配置nginx7 `1 J0 w0 S3 Z

2 S5 m4 N6 S: w3 k, B2 \; C在需要启用modsecurity的主机的location下面加入下面两行即可:
8 y# P* z6 E0 P! `
* N" h' ]1 Z3 i, ]ModSecurityEnabled on;  $ u7 {) p* y! m2 g, x
ModSecurityConfig modsecurity.conf;
9 \, M5 D& |5 _8 z# T下面是两个示例配置,php虚拟主机:/ S: n- n$ n: q+ p) J8 s; W

& ^9 ~7 y$ {. T+ A! Yserver {3 @3 V- w4 j+ p/ s' v
      listen      80;
* Y' Y6 n7 A, }" ~, K8 I3 V# w5 ^      server_name 52os.net www.52os.net;8 t" s0 [! l' {# f
     
7 |1 z& g- f) v* e- y8 t      location ~ \.php$ {
4 ~. Q* [( e" N3 }2 i, Z      ModSecurityEnabled on;  " \- x1 ]: |2 [: y+ B8 }' t
      ModSecurityConfig modsecurity.conf;
3 U1 u0 [! O/ z! g6 C$ o7 i) X- ^& ?$ J" t" d9 W
      root /web/wordpress;( B! U# R, S- M  M% h# A
      index index.php index.html index.htm;; T- e: I5 C" |3 ^; L
  
5 ^( Y4 W1 q" |. ]$ ]8 J      fastcgi_pass   127.0.0.1:9000;
# i  A4 `# f/ W+ V7 t      fastcgi_index  index.php;
" z( O0 C% l; Q+ Z: L, K      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;( m/ |5 ^( R. ?' v$ X$ u1 q
      include        fastcgi_params;# f$ o: H' f9 R( F0 b
      }- @8 R  t! T7 }: R/ |
  }4 F  U5 H: O8 @2 i0 T, l
upstream负载均衡:
6 p3 o6 d8 B+ {& `4 K9 k2 y* v3 E' k7 W* j/ a$ B# z# s
upstream 52os.net {
( {0 @7 F7 P: c1 g    server 192.168.1.100:8080;
9 o! Q/ k3 ^+ N3 v    server 192.168.1.101:8080 backup;
5 F  b* _; G# ^# Z" H}
8 c# y( A2 Z0 Z4 S3 @
" L& n! P/ s% o, Y" bserver {
4 A3 Q1 X  A4 w6 l9 F. |listen 80;. P" W5 r0 S$ I/ h6 E
server_name 52os.net www.52os.net;8 w7 N% C' ]( E' D+ A/ ^; C  Q
  {8 _! L. r& A: S
location / {! ]2 |6 c. _, G( ?) Y, [0 S4 ]
    ModSecurityEnabled on;  
) A7 p4 Z8 p, B3 g! J, r    ModSecurityConfig modsecurity.conf;  
" e5 `+ _$ v; \1 y8 z5 o0 v+ B0 V' _" U/ t# l0 u% a8 @2 p% V! e
        proxy_pass http://online;; T+ Q& _5 A. H1 C+ E
        proxy_redirect         off;
. o$ r+ z% D8 M3 n" q# ]        proxy_set_header Host $host;
/ t! C' [! t- Z) I& c        proxy_set_header X-Real-IP $remote_addr;
2 V' _! \5 T- B1 W1 K6 l        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;6 m7 Z1 f4 m  y
    }6 r* S# l" R* a* D- b" e: r5 ]
}
) X% q' g8 a; O% c六.测试
& J) ]5 p& G: s0 x% Q4 C1 ?' ~. M
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
0 O1 ]" Y+ f6 L4 K  M7 _6 J2 x
4 ^) g5 Q: _; _0 L<?php
9 ~2 q/ z; Z  p: _    phpinfo();   
# S2 c" q6 h( j2 b, \?>+ R# _; E8 i8 Q! k# a3 Q
在浏览器中访问:
% j  b" f* u, B, v
( a8 J* `' q+ z0 o& |http://www.52os.net/phpinfo.php?id=1 正常显示。3 V/ V; m" L! D1 ?& |: c
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
; ]2 a. a  J9 q6 z8 E2 Y- Vhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
$ k* N3 i" d4 m  V6 f说明sql注入和xss已经被过滤了
5 ]4 C8 B' {$ D6 f( b, c: s- B2 i- h' R8 t# S
七、安装过程中排错: s! b+ g- e0 D+ ^
3 ]) V: s* Q1 m$ t* c# \. A& q
1.缺少APXS会报错
* i* f% I+ {9 }; E5 g! N; f2 r* w  m2 K7 K+ `$ Y2 C
configure: looking for Apache module support via DSO through APXS
# ^) W' M& g% T) \& d9 D( x) Lconfigure: error: couldn't find APXS
1 A! z  b6 S7 V) x& dapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。+ L* b5 j# u$ a) A, k( v) Q
解决方法:
2 m: [# i" S% N/ U: b: l& K9 Q  x! v# K2 g& F9 a& Y8 [
yum install httpd-devel
1 S. L8 s; _& R! J2.没有pcre% l# Z% ^8 ]7 ]: V
8 j+ u( c' n4 r
configure: *** pcre library not found." @& g6 D  g& f9 L7 z" K8 Z% c
configure: error: pcre library is required
; ?6 q: f' l- T& r7 K7 z4 p  _解决方法:
) @; P; C$ }/ j# |* j1 N) n
+ X* @. z  n5 z0 F9 c' w0 n0 J; eyum install pcre pcre-devel2 G( {" `6 C) w7 Y: z
3.没有libxml2
- Z8 J- d. H  T  V
7 n7 O2 I* E# K; F$ i& C& O
$ R; [$ T7 L; P" _  A; |/ v! iconfigure: *** xml library not found.. m& i, O4 @6 q8 G
configure: error: libxml2 is required8 S2 M  h# D. \1 k( b
解决方法:
/ e2 p" K. u" {8 f! Q/ J/ K
1 N' `# w* w# E! o5 T8 u' yyum install  libxml2 libxml2-devel- c5 E4 T$ @6 [" i  K
4.执行 /opt/tengine/sbin/nginx -m 时有警告: `% \( a" t; P* G- R
2 l* ^/ Z! I0 W, T
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
/ `5 v+ p0 o! {) T% [nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
/ I- X& g8 g* \3 K  z2 C: q+ V原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
* O9 O8 [! ?: ~; x5 [- m# _3 }4 m) ~6 w1 R% b
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
+ f% P8 l; Y2 H2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"- B2 d# K/ j" q1 P- Z
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!8 O& [# ]( D; w0 u' P; s$ t
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
9 s# o7 k) U/ C, t! q1 A$ Q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
& y, ^, s8 r& J1 c3 X2 _7 q' Q2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
& [) `0 g0 S6 R# ~# C7 {; z解决方法,移除低版本的APR (1.3.9)
# y; P- \# x) ~& T+ `+ i, K2 ]
; P- H9 B' S5 V( {1 ryum remove apr
7 G# E  o3 ?; F' H1 X' X5.Error.log中有: Audit log: Failed to lock global mutex
) z* q( H+ \6 O; c" ?% i6 U* I' Q
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     : G% P& X' J' n7 u
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
! Z# ^5 |1 a2 e: s. Q- P/ S解决方法:
) Q) k# h% F5 l- v/ ]! r, V编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
0 w( I- }6 D9 V" G# E8 V
+ R" N& q, A' L# x' X0 S$ R5 nSecAuditLogDirMode 0777
4 P; D4 b* H5 D1 DSecAuditLogFileMode 0550, Y' Y. I4 m. ^) [* V
SecAuditLogStorageDir /var/log/modsecurity
- x0 I& w4 N5 }4 [  ESecAuditLogType Concurrent$ n" N8 h: \  m. z2 x) F
参考文章:
2 J+ i. B4 x3 h* b9 Fhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
) j9 Z# x# b5 }4 t: F" _http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-2 14:16 , Processed in 0.075950 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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