找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13057|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。9 ]; p* O  @2 X, _; W
1 }; n6 B7 V5 K& ^  {& C
一.准备工作
% _8 E0 I$ @6 g+ |3 Z9 Y9 P7 w- h# O% n
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
" M( u$ t# k. ^
% U$ c  r( k' m, j! K: rtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz1 p8 d* B% F% ~5 Y3 J( T9 }) c1 n

4 C/ `- Y) v2 z) N/ [6 tmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz- b* Y! P2 ]* i& t- L

4 \& r/ ?8 r3 [+ R) c$ qOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs# F( ~" `3 G# t$ y. q5 m
9 \2 E% _' F, B' i
依赖关系:
* X$ S  a& u! D- U- f  Ytengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
- N7 ]) R# R6 l4 }( r* o$ A$ B7 U: J+ b( O. ~5 q) o+ [4 r
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel7 @/ x3 F8 A9 H, W3 w7 u+ T
modsecurty依赖的包:pcre httpd-devel libxml2 apr0 ~) B" ~1 G- {4 n1 ]% U

( V+ `1 b" `. ~0 syum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel2 c# ^1 Y8 q; m. Y9 z3 `( k" \
二.启用standalone模块并编译
' F6 s, u7 q: y) G3 x" \0 \8 C9 K: Q; ~
下载modsecurity for nginx 解压,进入解压后目录执行:
& F. G# g# p' D5 s0 f2 Z$ z) J7 Q7 ]: o7 y% C+ G9 I
./autogen.sh8 K6 ?8 q  w- _9 J
./configure --enable-standalone-module --disable-mlogc
/ }: L3 P% G# C( f" F# Pmake
+ P% G# R+ z2 B" J7 B三.nginx添加modsecurity模块7 u8 |: U: w8 K) F

* V( a; }; m9 A在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
/ J" Q( b/ G/ D& G3 \$ H5 A* ?" u' f
+ P( I, Z, A6 `- G, O1 _, f./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
" a- C7 l( ~1 L/ f9 t2 Amake && make install( n8 Y, @8 t: D! A9 \$ @# M! }3 s
四.添加规则, F- R( M: {% s' S
. d, f  v& M9 \% N
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。- [  {# K0 ^5 H
  n8 |/ r) }! K- k9 s
1.下载OWASP规则:5 S6 ^& ~; Q. Y  D  v
0 E& T( Q' F6 y
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs) K. g1 Y( F* ?9 c2 j7 Y* K* ?
; s/ S6 z+ ]: z9 h9 P( A
mv owasp-modsecurity-crs /opt/tengine/conf/& e/ X5 V$ V5 ]1 M

9 H6 J( z9 \3 C* ycd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
' X# m: I( c6 m' C/ I2.启用OWASP规则:9 f, }) _$ ^, e( x2 }7 \

% w/ i# z# \; P' U# [8 z% H/ a复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。1 `' m4 I5 x+ `3 ~  P2 p( J

7 S: z( I3 ?7 m" Q& s, e1 j# P; ]: D编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
) n5 S( v- u$ A) P7 ?. K! k: {  i( g  `
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
$ H4 J2 G$ H0 U2 K: C& D( K% _" H9 Y( j( j0 k
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf: Z2 N% ?* f$ j. J, H( H- D, r
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
4 [5 Y  k! `  NInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf7 v+ T8 M* A; d* c, L+ K# X, N
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
7 v8 z' y6 j; r1 G8 u4 O$ rInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf0 J7 }6 n3 u/ Z9 H% O3 k
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf7 c0 |. ^4 R8 _( S# _$ S
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf$ M4 c' `0 u* p) Z; |
五.配置nginx; l5 i1 L/ p, {, i
+ z- O9 U- E# s; o% l6 K8 l
在需要启用modsecurity的主机的location下面加入下面两行即可:. M9 x1 V9 x# V. i7 I/ Y3 p

, W; C* u( o6 j" x- x7 y+ eModSecurityEnabled on;  , H: @* e0 U% L' ^
ModSecurityConfig modsecurity.conf;& z# G$ ^6 [, t$ n2 c* h5 N
下面是两个示例配置,php虚拟主机:- z* g* r# Q( `" r) l9 y$ \$ Z

: o, F5 D" Y  yserver {
8 m0 l" C+ x  l$ P      listen      80;
2 x. i/ v# K( P( t8 R: }0 X0 l      server_name 52os.net www.52os.net;, o$ O2 d& m( q8 H  q; T
     
0 N8 o1 Q4 b/ q1 V3 K' j0 e      location ~ \.php$ {  k" ?9 Q, Q7 ~. S) O
      ModSecurityEnabled on;  4 h6 n  {6 [7 Q& y# d
      ModSecurityConfig modsecurity.conf;3 N! f) E" B# q, M4 I7 F" n
) s+ u8 t: O  |
      root /web/wordpress;. B) G+ U  Y3 {
      index index.php index.html index.htm;2 A( _4 }. F; p$ O3 w
    k  _! n$ x4 a* S$ \/ \# \0 v
      fastcgi_pass   127.0.0.1:9000;
: [  r) }. s4 E) l      fastcgi_index  index.php;
3 d+ o6 I  }7 h  O+ `      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;* U, u8 j  U) T$ K4 S
      include        fastcgi_params;+ m. Y! m* Q  ^: V
      }; f$ |% G' ]$ t( q! P: \
  }
+ h8 E5 _  l7 s+ d" w( gupstream负载均衡:
0 l, s0 r  Q" `5 v% s6 D( M8 w* s7 B1 s4 B
upstream 52os.net {
" K1 f$ Q! d6 G) g: Y7 B    server 192.168.1.100:8080;+ p" y2 ~; T% b0 |2 _. C
    server 192.168.1.101:8080 backup;  Z1 ^6 @" k. i4 w) a5 i8 O8 |; f
}
; k6 G! _) I! F7 K# {* p, J1 ?; J( G: H
server {
* `6 |: k9 @" G. l4 m! P# [# c  olisten 80;: d" l: c5 k, _( B) y- V
server_name 52os.net www.52os.net;5 ^0 \& D. T% w7 ^  o0 i1 j

7 }$ [- @" C% Rlocation / {. X. S) w* m0 g1 a
    ModSecurityEnabled on;  7 M/ C% i5 V3 ]' R2 T) T
    ModSecurityConfig modsecurity.conf;  
1 d# C- p( [" f: s! _" w5 }) u  p; L. [" x, k
        proxy_pass http://online;4 Z& b3 s; ~+ c; r* \
        proxy_redirect         off;
+ E  t9 v) x. b        proxy_set_header Host $host;
5 J& k& [" r8 x/ O: S" ~+ I( L( I3 [  i        proxy_set_header X-Real-IP $remote_addr;
4 W* f% C' d) d& w( y        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
( u! }* C) r6 F  h    }
8 C5 a/ G2 p, B8 C! q4 k}' i" B0 F* _! R7 Y" s
六.测试, \6 u4 m; i& Q9 h* y

% U  s/ a% p0 W- F. x我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:0 b4 Z( K. c8 F) L  w  D
* L4 T. W8 Q. a; g; e0 w
<?php
% v5 b9 c* w8 m) V+ o" T    phpinfo();    5 y+ }. ^4 `6 C) y4 g- e* C
?>
# y& P0 J3 [$ x# u# [在浏览器中访问:" I# L+ O5 |2 w

' }4 y1 O; i2 |http://www.52os.net/phpinfo.php?id=1 正常显示。- B2 o+ t" S( c; `. u; o' W
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。1 W, o9 K/ A7 \. p3 C: a$ o
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
3 N; D8 `, D$ X4 V6 w说明sql注入和xss已经被过滤了% H3 ?0 r- r3 K4 _0 [$ U
1 x: V* Q& m" [+ G: m/ f
七、安装过程中排错, m( C$ P$ ^0 ?

+ P) ^  m  e; R7 T7 C' C1 z1.缺少APXS会报错) a6 }, ^' f. d: }. }: Q) }
! I+ K# H' D" n7 o' b9 R
configure: looking for Apache module support via DSO through APXS
: s$ g" V3 q" H) }1 o* \. \9 \& sconfigure: error: couldn't find APXS
. Y6 ^3 [( R2 F  K& oapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。0 E+ y5 s: B% ^
解决方法:( D, R* h" y' ?* A

# O" Q0 t1 L$ Z5 t; J% E" gyum install httpd-devel
' T- ^" H. D# H% r2.没有pcre
0 ~* }9 e! R/ O' U$ O; y) d6 ]& k, Z/ u: y9 c; z& Z$ t0 N- m9 x
configure: *** pcre library not found.. d5 x* F; N& j1 N! s8 o. ]
configure: error: pcre library is required
; H) l( J; d9 P) I3 S- u! Q解决方法:" q: v8 ~# |6 G- x. K" g+ s6 N# [

+ g' \2 _3 @7 l7 K! L7 @yum install pcre pcre-devel) K9 p, b; E; ~' u6 U2 M
3.没有libxml2
/ ?) B& a8 H) w9 M" T+ t' t/ g" C5 B9 Q6 @
2 n7 M  y! }# p: j* T6 W" f
configure: *** xml library not found.) l' z) B. k7 Q- v" e4 `* T
configure: error: libxml2 is required5 v* Z1 w" J$ P  ?% u, h4 |
解决方法:5 s6 Y$ \, q$ o$ e% Y* x

4 k7 H! z* f7 Q- g/ Iyum install  libxml2 libxml2-devel& _7 _% u# D* L: G
4.执行 /opt/tengine/sbin/nginx -m 时有警告
" q3 [7 v& {/ I8 ?
# @5 Y* x9 R5 }" K& Q7 {Tengine version: Tengine/2.1.0 (nginx/1.6.2)- [( ^  K# _& F" r4 R
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
# J% W+ u  w, P+ E. p" k7 q原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log$ _. s" J7 a: m7 r# l

( `, n# h) @& ], k1 r! U  }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
! B1 z; L$ D3 ^0 O% {. k2 B  p2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
/ c1 Z4 E( ]( M  T2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
+ t! a. \  p; y/ }# _& G! k2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
- b; M. T5 Y) `2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"9 v2 J; _" u8 `& U( J5 R
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On., q: {2 g8 {# {- @" }' ~2 U
解决方法,移除低版本的APR (1.3.9)
# S( k5 k6 q9 L  `1 h  E, K. t2 A# x/ k
yum remove apr/ m& f( @2 G9 V! C4 M3 W
5.Error.log中有: Audit log: Failed to lock global mutex
" f0 Y3 h" ~2 |: `0 S: V! W! N9 ]$ M, _6 x0 X. E5 N
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     5 L" W4 s/ F) }; i% _! k6 ^$ a
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
2 f) B, b# G  J  w( y+ l, e解决方法:4 i& n6 C  z8 [& l( s
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:6 d& z4 q/ V5 J- ^! N% [

9 m' C0 z- |4 XSecAuditLogDirMode 07770 F" d. P/ B* U, \6 m5 u; |: O
SecAuditLogFileMode 0550
) @1 i# i& |' x; l. rSecAuditLogStorageDir /var/log/modsecurity
1 m* C* w% [. f+ QSecAuditLogType Concurrent
9 |) ^* I. n4 ]5 ]+ V参考文章:
& A  C% T/ P5 a3 K+ T3 ^https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX3 I! k2 k1 w: `. H
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-9 08:52 , Processed in 0.055409 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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