找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10083|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。: B8 A' ?" x/ l( G- v, L
1 J& R* z' f: x# n! X( O
一.准备工作. o; `1 n# X1 K% b
; r: T. F5 A# ?  t
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0+ K5 [, [5 J1 t7 E; A3 D
- h& D. ?, r; h& R5 N/ M
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz" f1 k7 @* E9 r9 Q* \- V
$ E% L% s. j# t: q) |
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
( D5 \# x: J2 |' n( Q" E$ A+ S2 }/ U5 o& C! W3 V& g9 X4 W! C
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs, O& E, r- Z. K6 S
: G- |# _( j' M+ H! Z- Y: R! l$ m7 H( X
依赖关系:
( b/ E) P! r  ~) F1 J/ Gtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:1 ~& j) E: K$ h$ U6 C

5 e6 j) l, N- G- _' F0 ?yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
, V8 G8 Z% k0 d; Wmodsecurty依赖的包:pcre httpd-devel libxml2 apr0 R2 X: l6 \# f/ Q

' P, t( C- I0 A+ Eyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
. Q' w6 [: U+ e. H二.启用standalone模块并编译
" z9 w. L" V* N, Z* T
" W4 s0 j! t5 V* l# ~下载modsecurity for nginx 解压,进入解压后目录执行:! F8 o7 \1 _0 K# K: A6 E& m
- j! W/ H# |5 h3 p; c
./autogen.sh3 a+ A: j- @" P& }& j
./configure --enable-standalone-module --disable-mlogc
" T6 Z* H( w. I# n* M5 w0 Nmake
; G2 b5 W/ Q& g) n0 A, M# U0 Y三.nginx添加modsecurity模块
! i% }0 h# O) t1 Z
& c" X: S- Q" ]在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:: p7 g# V+ s1 h! b" z& Q; Q
3 Y# Z2 ~+ i# i2 n
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
4 _3 T1 I/ ]" U/ ]make && make install0 @3 z& X! k" P
四.添加规则
- y  o" t. A1 Q+ T" A4 @) u' n
. O5 u" }+ F' Umodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。# Q9 g$ d2 R- }. H1 k; n% }4 ^. b
9 @* \3 s5 w& I
1.下载OWASP规则:# N. K* S* Q5 v; o

0 K! o& R$ _# \git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
) t4 x) h# T+ r; z! z, O" f1 t5 o4 f- ~) Y' S
mv owasp-modsecurity-crs /opt/tengine/conf/
' [7 z9 P7 J+ n# v6 f6 A6 y" ?/ H* b4 |$ U! ~) P; ?$ K7 S
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
( ^' ?& w' _( B7 ?3 U8 H2.启用OWASP规则:
7 x( B0 f. Y1 X; v9 r3 w7 I
: ^' R! y9 ^4 }  {4 |3 N- t$ C: e复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
6 `- y2 `/ d$ o7 x* G8 a
0 L2 V5 p/ c; e+ E, O$ `编辑modsecurity.conf 文件,将SecRuleEngine设置为 on8 O0 G1 ?' b* o; j3 f) |! m1 g- E1 U

5 H! s7 H/ e4 Iowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。  U/ u" M# C$ w# |% X2 Z
  {3 g3 A, {& j/ _
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
) b, c  \9 a. x. ^  a9 Q7 NInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf( A7 b* h) v3 U9 Y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf% H( G) [4 D; f9 O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf: W) J4 g( _0 B3 B/ K$ Q9 m4 l
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf+ g. r  Q! l' x# _
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf6 U7 \) r, M9 e) Z* a+ ^" y7 g
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
% y! O$ d4 r; ~1 [. A" x+ O. E五.配置nginx
; m! f6 w. q+ ~% `+ J$ h1 g! t0 K: \, C% w: t9 u3 ?  Z
在需要启用modsecurity的主机的location下面加入下面两行即可:
; u  c- w# A% F& w$ R( S5 z. F9 j3 E3 I
ModSecurityEnabled on;  , s: `. P* Z2 Z
ModSecurityConfig modsecurity.conf;4 B1 s8 c6 e) k. T& g& N6 }
下面是两个示例配置,php虚拟主机:* J) I! B* A/ O

9 I" m4 }& I6 J8 D' P  jserver {; g/ F6 T! o4 f1 N$ G0 {* r
      listen      80;
. H8 i! O7 K! m6 n. g7 D      server_name 52os.net www.52os.net;" `  z% M; ^! _( J* v
     
1 b( f  s4 w: Z( S5 w; d      location ~ \.php$ {
6 W  E& s: Z* y9 x/ ^8 F      ModSecurityEnabled on;  ! @% b0 o% w0 X/ r" \* L6 k4 c
      ModSecurityConfig modsecurity.conf;8 O1 i/ u4 c. X% x1 ~# ^! }+ r4 @$ @

, k" b/ R7 e# u! g      root /web/wordpress;, L! k9 h: w; f0 B
      index index.php index.html index.htm;" J& A* _2 m5 Q
  
& k. y! k6 I; ]      fastcgi_pass   127.0.0.1:9000;
+ v/ Q' \) F/ `$ q* Y' w! N; {      fastcgi_index  index.php;
9 y! t0 n9 c. S+ v# {  d      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
( x2 H5 M1 O3 P# d2 v4 M5 K      include        fastcgi_params;& U2 J8 S  ^1 Y- N
      }- ]7 X6 e' |9 l, u' i
  }
  r! R5 m7 O4 R) Nupstream负载均衡:9 a# T) @$ C! \5 z3 ^) a
2 a, @( F0 F, i/ e. f: c6 B
upstream 52os.net {6 u- J* O/ y7 s& {2 s  X
    server 192.168.1.100:8080;# b2 H5 ]9 s9 Y2 u: {+ V/ W/ S8 s
    server 192.168.1.101:8080 backup;' v* e6 c& O4 P/ H7 x- W+ D  F
}' ?; q& F1 {& z5 w! @4 Q2 D( l, \

* Z1 b; v; G; A: F/ V! {% Lserver {) x& _2 ?# Q2 E4 |+ ~# @
listen 80;6 S: Y9 C# z2 x1 ]) B
server_name 52os.net www.52os.net;
2 j( b9 C+ w$ F* {' h+ V4 \& T0 ?' t3 {2 N* X: n+ D% l- m
location / {$ b6 A$ [* A* H) T
    ModSecurityEnabled on;  
& [3 o3 m' z) @  l3 T* w    ModSecurityConfig modsecurity.conf;  4 i: ]0 g0 Y- \1 }$ f) J+ A$ I

: E4 V0 X4 [7 D; Q9 E/ a        proxy_pass http://online;& |2 V5 G, f3 h, [
        proxy_redirect         off;
5 O3 `1 B& U1 n* ]+ H6 s! v5 U        proxy_set_header Host $host;
4 a4 K: W: ^; S) r7 A7 `        proxy_set_header X-Real-IP $remote_addr;" G" A( V) D( \
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
3 a5 y% V2 \. P: g+ k$ u    }
6 T8 I7 L' p: ~0 U0 B- M}
( ^! L; J6 Q- O% Y0 F5 u5 k六.测试
# H5 K  C; j, Z; N7 I. E) }" W
$ N! ?5 Y& o+ X" Q我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
& c) P2 K9 C& H' M
% I% I- u5 \' w: ^( Y/ X<?php
2 L8 Q& C7 k: `7 c7 q    phpinfo();   
+ r+ k5 Q# K- u2 ~?>
3 w( F, X- ^0 d, Y* ~在浏览器中访问:
  M6 U0 r1 ~) _2 s& H: O  b6 A% H. h3 I4 n% F
http://www.52os.net/phpinfo.php?id=1 正常显示。
  B4 K  \& @- y4 Y. b) chttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
5 C! [; I* ]9 q& ?$ P2 z! bhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。, n# F- a9 M0 r  w8 Q
说明sql注入和xss已经被过滤了
# `' ]  a3 R4 ?  r  y. Q& |
. a8 D- T* E: ?: L8 P0 K- M$ P七、安装过程中排错  |% Y0 p/ w+ R* N+ M3 f

8 o5 _7 c  t4 @+ l$ }1.缺少APXS会报错
' U$ Y! s( I) i, t$ ^6 A2 ]( v9 b/ U( ?
configure: looking for Apache module support via DSO through APXS1 {" }7 w( D7 O' U3 Y% L
configure: error: couldn't find APXS
# e4 ]3 r$ {6 N, D" X" @( wapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
8 X1 u- N9 ~) b* M+ d  K7 t解决方法:
: Y( w, l6 }2 q: J0 {4 p# i! |, `0 c
yum install httpd-devel
+ J% i$ `2 Z4 j4 y- Q& w" x  [2 O2.没有pcre6 i' v) l- A4 k3 b, B" {) h0 M

! o$ ^, k" _$ Y* }( j4 oconfigure: *** pcre library not found.
. ^& \0 R5 _; }, j) t7 u' M; Jconfigure: error: pcre library is required
7 y5 p: E9 g: u' f% s9 g. ^* ?$ u解决方法:
0 ^6 a. k5 _0 n( \' a% @2 D( |
6 u! _' I( q* y6 Syum install pcre pcre-devel8 R( s" [3 `, @5 z2 y8 {1 ~
3.没有libxml2
* g* p+ R' G$ _% b( y1 J4 f
- f2 Y' h  ?! h( L$ G4 e5 Q
) G# ~0 V4 _# [1 A. l! fconfigure: *** xml library not found.
% B! n7 q& d0 x/ \9 lconfigure: error: libxml2 is required
' N: y" ^. J  m/ E9 b解决方法:
2 s8 j' z3 s7 f1 P* n1 J9 Z+ |( [5 N: ~  |/ A
yum install  libxml2 libxml2-devel
% D: W6 a# j  P4.执行 /opt/tengine/sbin/nginx -m 时有警告8 Z- c6 L6 Q+ o  Q% P8 L& r

2 c$ b0 N5 z. |* o7 w4 r  `& h) |Tengine version: Tengine/2.1.0 (nginx/1.6.2)
: F; z, P, b# f1 ynginx: [warn] ModSecurity: Loaded APR do not match with compiled!
0 B, d1 L0 U5 Q" P8 l原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
' T. K" V! R3 o& [0 H0 }7 C' f7 ^$ U& Q5 J7 F6 ^3 z& i
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.0 J4 Q, g* A/ L8 j
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
6 j9 n* ~  Q1 }# X' }2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!- X- x& ?. ?0 M+ i* {. \* W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"2 y4 o' E7 k: A6 `' m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
  d/ `0 C" Q8 q/ z7 c6 z6 X% c  z2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
+ d8 t% j/ `# `3 B0 C解决方法,移除低版本的APR (1.3.9)  p. _- I7 o1 V! s4 Z) Q

- {" F/ Z/ c1 K$ K" k/ o% u& wyum remove apr
5 G" o$ |% b4 F8 i7 m% u( Y5.Error.log中有: Audit log: Failed to lock global mutex
% |) U3 F& `1 `; i0 G: q( D; z" _' n& p/ b7 e
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     & [! I& `, z* `7 d! ^
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]" E" p% Y: x7 X8 ~1 ~0 u1 ?  h5 w! i
解决方法:
% N1 c+ j( M6 n) q0 U' N编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
9 s' M; Q' |$ a9 \9 ~: J& ^3 _- }# Y$ Z$ h0 h+ _3 m' d, l2 `) |/ e1 U
SecAuditLogDirMode 0777  r( S# ^( H; _5 k8 ~0 F
SecAuditLogFileMode 0550& Y8 t5 N6 v! L+ Q% b0 G0 B) c
SecAuditLogStorageDir /var/log/modsecurity5 W" e# W% C; a( j8 |2 Z: o/ z
SecAuditLogType Concurrent
! q* o5 h+ q; d4 |$ \% I参考文章:4 ^9 y& F8 `8 X$ G+ D, U
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
  p4 {" c  t7 l, p! lhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-2-27 15:21 , Processed in 0.071699 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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