找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13207|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。3 C+ g& ?! g$ m- ~+ P
% O" [5 A, \* ?, c3 c
一.准备工作
8 ?$ F9 i" v( c, @- c* u: K6 F/ o: R9 L* `9 T' D- A. X
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.06 a% p1 g+ @  E5 |

/ Q6 n+ q. g: z5 ptengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz: g2 o# D' ~/ _) z# a- o

/ |6 X) L( A* q9 R$ j4 ?/ `  bmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz! ?9 R6 Z9 |3 F

7 M* u0 ^* `7 c" g+ x5 P" s5 vOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs& \$ j' `7 U. X3 z2 [2 J0 ~2 U

1 S* V2 a7 ~4 X! [6 N依赖关系:) d# ]. G! k9 q/ @: I
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:1 m+ f6 f. M) Y" D! m7 T2 |
% ~/ a( q7 Z& i) z
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
3 G! T7 y' T/ e9 Y: Jmodsecurty依赖的包:pcre httpd-devel libxml2 apr; _+ }" M/ W- x6 _+ y5 u
, H& J4 _7 }# O( `& E
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
5 f+ z0 X3 I# b& V- t5 S6 ^  a二.启用standalone模块并编译
( h3 g3 r* }7 ?8 Z+ l2 @# g: J! X! j- W# j; R% q: u3 d- F9 R
下载modsecurity for nginx 解压,进入解压后目录执行:7 q- f6 \- R/ d8 K. }
" D$ f2 @* M) f0 g- @
./autogen.sh
5 _! j- d5 x7 g+ q./configure --enable-standalone-module --disable-mlogc# D9 c% v+ M. s' j7 ^1 a" O
make
  o9 \4 n5 a% e三.nginx添加modsecurity模块
/ {! y* `0 O2 i
2 z+ P6 O+ l( r3 T; C  A, ]在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:3 N9 C! N3 J1 J4 S6 r9 g2 \: T% H
2 c/ K, p& `8 A- ?# ]. x" o1 K0 _
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine6 \9 t0 d/ Z1 j# t% j3 y- U+ G3 t
make && make install/ y: j, ^0 f7 y5 ?. s1 z) w
四.添加规则! r0 p0 ~1 Z: X9 e* Q5 [

* b- P" S8 t% {5 B) mmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。( H' x4 s) e  J5 g! l( ]: T2 X
; A/ \2 x- k5 x$ N
1.下载OWASP规则:
; }! T( Q+ ^" c7 F: F5 W2 d- @
7 o" k* Z. f' b- @8 J: Y+ _git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
/ X) I( q/ d; Y3 h8 E
9 e% h& C& X6 M0 `: [% [$ Emv owasp-modsecurity-crs /opt/tengine/conf/
0 g" y! n, U% I5 O4 u5 Z% G, N3 O* B/ l" ^+ V  {8 J; l
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf) Z3 Y4 F; o. W% Z
2.启用OWASP规则:
: b! U% \/ ~/ s& u/ N* Y! h/ ]0 ?) Q% t7 S- e2 f6 i% C( s
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
  J, {4 x" M; `9 r' a8 s4 u1 W" K* A
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on( w+ l! `) n9 C8 R

: A) F5 M" a$ k: w5 C8 Bowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。3 f; |1 O; M' o1 [( k$ m

# l/ s6 V) A9 k. ~Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
% ^; j, v7 h. M0 ^Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
% w+ Z2 k& S6 G; r8 h, Z# C4 w' qInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
1 S. ?# a9 t  j/ z% @- M+ V/ cInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
4 P/ F$ n4 J* B8 h& {1 ?Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf9 k3 Q, t+ N/ a; p
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
" f! F9 `" u: J7 pInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
# d1 O3 n7 O9 H1 b8 T五.配置nginx
8 X( x) s$ I+ i, S/ k5 t% h1 x/ r. n7 o9 \3 _* d
在需要启用modsecurity的主机的location下面加入下面两行即可:' j$ f5 C& H' n& G& n+ [' C
" g8 C) L1 G6 a. |, f& `. C! D
ModSecurityEnabled on;  
: g6 ^/ }, k. q9 M- ^ModSecurityConfig modsecurity.conf;' G% }& Y3 |7 [% S) Q
下面是两个示例配置,php虚拟主机:
- ~( G3 T2 \1 H; _- B" ?+ F2 H7 w7 R; {2 q+ C7 _. O8 `3 q
server {" i* d9 B! ~/ G' R
      listen      80;8 K9 y# U' D( C2 {' G5 n( r1 e, G7 R
      server_name 52os.net www.52os.net;
6 ~2 ?. X7 T& B$ A! e9 J4 t     
: R0 U% b+ P# F0 j  `      location ~ \.php$ {
9 g- O4 o8 V6 p) P; a      ModSecurityEnabled on;  4 o# }: |+ ]6 r* |/ s( m
      ModSecurityConfig modsecurity.conf;' z6 t0 Z6 b  r4 N% D
& I4 L" Z* p' {$ E1 K
      root /web/wordpress;
  [0 b; }) o/ n2 v* e$ r      index index.php index.html index.htm;
' `- o/ q, w4 o5 s0 |7 F    b* B: C- D& n) A
      fastcgi_pass   127.0.0.1:9000;
9 r9 N) l8 Q2 l' P3 C  i0 i. j! _& Z2 j      fastcgi_index  index.php;
7 i2 J0 v2 a& w' l7 c/ X7 d8 e      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
9 J6 c  \! X$ p( h      include        fastcgi_params;; d" N4 n; z* d
      }
& Q7 F  I" {4 h6 Z( H/ O" |- {8 X3 ^  }
0 `# F% G; d* R% @- Xupstream负载均衡:
6 E) i# U9 T' N( k+ E7 O% m; W% d2 L3 K5 J1 E, t; ?
upstream 52os.net {. A8 ]& _/ q% D( n8 ?  x
    server 192.168.1.100:8080;8 f# A7 ^  K3 H9 @
    server 192.168.1.101:8080 backup;
' [% j! \* h+ F* J. V! l}. z* i6 C2 e2 ?0 O
& V0 o/ i" u( g: t" ]
server {
$ h; {; ]. M- \, Slisten 80;& X$ \- f9 h" b' \
server_name 52os.net www.52os.net;+ N7 P" e' x( A1 h

: z6 A/ }- Y5 f# M7 y9 O! R  c9 Xlocation / {
: t; ^- E# K' S$ c  _    ModSecurityEnabled on;  5 a$ W. L; I8 F) Z
    ModSecurityConfig modsecurity.conf;  
3 Q' W9 b# w7 e0 ~4 b. l+ y( u' o, a+ L# ~0 o* l- m3 ^
        proxy_pass http://online;
9 \2 f6 c/ Q% [. i5 f        proxy_redirect         off;
" U; z$ U( d: H; n, h        proxy_set_header Host $host;; Q  r2 @2 y; [3 u
        proxy_set_header X-Real-IP $remote_addr;3 V% S& z5 h: v4 D2 [
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
% m7 j+ s+ ?* v4 Z- Z( F    }( R  Q/ o( `% e, y8 h+ m" \
}
/ ~# G" V8 I! D0 A% T: ]六.测试
9 a" W9 Q6 O; r1 g5 V- D. [) |( H$ {  H4 _8 ~, {8 a3 O
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
1 w6 l% R) J! _) {$ A/ K; d
& v. c6 f+ l7 {; y1 B<?php4 B; o. S& H; O8 w% A
    phpinfo();   
' `  v  p/ o7 |9 [?>' E" R% X% Q9 P' Y* f. s
在浏览器中访问:
: s$ ]9 I4 T* K+ G6 u  W
! j6 W4 G4 i8 U" E) Z* m- {http://www.52os.net/phpinfo.php?id=1 正常显示。
0 Q6 I. B% c* k0 v) y. l; Uhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。' K: g; T! T: P  q# W7 U, U$ h
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。1 g. s7 ?6 d! t' X2 _% i  Y% ]
说明sql注入和xss已经被过滤了
0 o+ O, r+ M0 V; Z$ C  C6 H" r- H; b/ d8 F- T0 u; j
七、安装过程中排错0 u4 x4 T9 T) E+ X3 G

& v5 A: h7 f0 q7 {8 I7 `0 D# \1.缺少APXS会报错
7 i# g& Z6 m6 p6 n% S' g0 G; r! G  k
configure: looking for Apache module support via DSO through APXS, a5 y$ u: D- }7 M, d" K4 O
configure: error: couldn't find APXS
& a- j. B5 {! H/ k! y' rapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
$ w; Q( W$ Q" X! J6 a解决方法:- T3 x  y2 r, R0 W! J5 V
% S" J* |7 C* ~6 |
yum install httpd-devel7 l; }- F( S' |/ O% S, S$ r
2.没有pcre9 f% J  @6 z  Q' r) Z. H0 ^( E7 o" }

5 X$ x8 L8 Y+ L) v! c9 k) Xconfigure: *** pcre library not found.3 Q4 u9 l  d- y* }- ~" y
configure: error: pcre library is required7 ]6 F, m! `0 E/ x0 g  E
解决方法:
' G! v( k( L3 _. Y0 \+ z. P' U5 a
8 x: C! D$ Q- Fyum install pcre pcre-devel
0 z* s+ \* S1 ?4 L* i  M  ~& v8 [$ _3.没有libxml2
" d; k4 u2 c- f( D) v% }3 h# y
' a" o. d2 k. j7 F8 D3 Q% l9 g
configure: *** xml library not found.: m( q+ z" u* }7 R) C
configure: error: libxml2 is required2 _9 u, p" O# y5 z$ s/ }4 J
解决方法:/ [5 O" V0 B) d9 b
  S& F4 k$ K( K6 w5 A$ L
yum install  libxml2 libxml2-devel) R9 ^, E% B" d! X5 {2 [0 q
4.执行 /opt/tengine/sbin/nginx -m 时有警告
+ i, c& @& Q+ |3 x8 i! G8 U* f  {6 \4 U, F0 f
Tengine version: Tengine/2.1.0 (nginx/1.6.2); s0 E2 f6 ^8 u% k
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
1 ^/ c* T; \$ N3 }. z0 m  @$ n原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
7 t, U+ h0 k$ T" K# R. u
5 j" [7 q. Y/ \! o$ M, \4 r2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
- Q7 n7 h0 X3 S8 @* ?) h2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"4 v5 z4 s; z1 e
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!# Q: J6 r) |7 J; U5 a6 `3 i2 W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"9 C  q& r  [' L: _
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
& x4 h# n! M/ j2 h8 D  w2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.5 ?3 V7 [/ T" }# ]
解决方法,移除低版本的APR (1.3.9)( j1 c/ M0 ^, @
7 U7 _/ A7 I5 k$ B; v/ Q7 ?; p
yum remove apr
. c5 j% `- G- Y2 ]0 m# @5.Error.log中有: Audit log: Failed to lock global mutex
6 @# ?: h. d- F  \. y) K: ~( Y& @5 V# H/ @
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
. [  C1 Y, L7 x5 H! Qglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
# y3 M% `( Z- @& ]( E解决方法:
1 u9 E! ?: [0 i0 j; I. b4 V编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
. b4 ]1 }* Q0 Z9 x
5 X# d9 B, H- v/ c0 tSecAuditLogDirMode 0777: B' k. [- b; Z/ Y. @' i5 `$ R- p+ e0 I
SecAuditLogFileMode 05504 M4 l' v9 c1 Q! {, f! J3 c
SecAuditLogStorageDir /var/log/modsecurity
2 y  K* F3 c- I% O9 e8 NSecAuditLogType Concurrent
) F& M  s& d8 M+ u参考文章:
, |4 A' j8 G& w' e- phttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
6 \& ]' C$ ~- l, Zhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-23 19:54 , Processed in 0.068941 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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