找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12619|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。6 n$ }$ ^" Q& v
7 N" }4 q2 |0 r
一.准备工作5 U' K5 L% f3 ?( [$ ]5 @
6 f/ ]  K  r3 u- z) e( P
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0& W* m, @- P7 ~4 n
9 y. d( w* O6 p
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz% M* v2 `1 l$ U! |+ p! r2 I
" }6 U, p) e8 g* S5 s1 G
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz4 o. X" Q1 K' l0 o/ M* Z) s

9 B- ^. Q. `6 v5 x  Q& c2 yOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
7 N  x& h, s9 d5 B: y" p9 Y9 k+ z
, z" E/ Y. v/ l: F$ F- j依赖关系:
; D2 y3 i0 _7 T. \. Mtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
7 J; @3 V+ k7 L/ l  K& P) R. d+ [. X2 B- @& e9 j1 S
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
1 L; X2 ^3 ?! X3 ?modsecurty依赖的包:pcre httpd-devel libxml2 apr7 m3 d0 g, L2 B+ ]; ?, U( Q

) o( F/ Y8 ?- K  r- E* wyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
8 ?, s8 i/ Y( p5 d( N6 {) E二.启用standalone模块并编译7 w4 E: {$ A" Y4 T: q

; N: }6 }6 B2 n1 g下载modsecurity for nginx 解压,进入解压后目录执行:! |! K. ]7 z) c; ?7 I% Q* {

% D* x/ u, |5 {  [./autogen.sh2 o" P. F# l9 Y+ l6 \3 o
./configure --enable-standalone-module --disable-mlogc
6 l9 N$ J( }7 Q; C& qmake $ j; c  _9 u0 N
三.nginx添加modsecurity模块) K* t7 M1 r. o7 s( a: q5 R, `
$ G% N5 V8 D& }- q5 y0 k
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
7 w3 Y% |' S1 j1 {7 W9 N8 p9 |1 Z2 b  d6 Y, K1 m- i3 A$ }
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine8 d# m4 d% i# L1 c9 C: U  D/ Z  x
make && make install( h( l' ~! _+ j
四.添加规则
" L* J5 X  _- i& N: }% U9 @, P2 f+ g* v2 r+ f$ c% i
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
( x8 ?) A& M8 d# [) O+ [2 \, K
* p$ p* A8 D/ [! p- R1.下载OWASP规则:
1 q6 Y. N' |# v: Q
3 S1 \* x. u$ `: h+ z$ I) [git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
3 B. k, t5 J1 G: {$ T, Z( P' W9 `% q7 A/ {+ @& H$ Y: n
mv owasp-modsecurity-crs /opt/tengine/conf/
" o7 F7 u3 N0 f4 T6 ]  ~& W8 b3 a3 Y+ g
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf3 E* A6 O, Z* o, G) Z) ~
2.启用OWASP规则:8 |" }6 Z% _' K0 a, n; E% }3 p' |9 S
/ {/ q. V" \2 _( |& y2 @
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; Z+ G; [9 n: }( [8 A

( m9 e! g8 F; R3 k2 @* k5 u% U编辑modsecurity.conf 文件,将SecRuleEngine设置为 on" D* Q" d4 B% q9 J# Q: r% }
7 F: @4 g. K( X& b) e
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
! |0 t/ j( A3 V) L% _. J; n# a: g$ Z# v1 C9 V
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf* ^, _8 ~' w- x) x* ~' @! F4 U
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf. j9 I7 g/ e  t! y' t
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf3 h% B. m+ s& Q* F0 Z( o# z! b
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
5 a4 |5 n5 s3 J8 _0 h- ?; ~, pInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
9 j. N/ i; v/ W$ K! n& d2 dInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf7 k5 o9 z6 K7 \
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf7 W3 J- [: S9 w- Z
五.配置nginx
; V1 I, i; _9 u/ U9 l4 w7 @' \6 j- N$ D1 f; r- S
在需要启用modsecurity的主机的location下面加入下面两行即可:. G3 F( O# x( O! Q7 f. k

; O$ H% a: W6 dModSecurityEnabled on;  
* j1 c, q# R3 ?' n$ [ModSecurityConfig modsecurity.conf;
+ Z$ m8 @; I& p' }7 @下面是两个示例配置,php虚拟主机:
2 F9 F9 A/ _8 `' B- f6 i" V- O" g) m8 y
server {2 H7 y$ z. ?/ ~
      listen      80;2 g$ l& C) ^4 f  ^
      server_name 52os.net www.52os.net;4 D7 g! C1 c: c& O4 N/ {
     * d" m8 X4 a5 j$ P2 ?" n$ X3 R3 f+ s
      location ~ \.php$ {  u, @# Z7 D' r% C: v# B
      ModSecurityEnabled on;  0 H- z! w. p. ^+ {. U" C6 L
      ModSecurityConfig modsecurity.conf;0 |' k$ S5 |6 H5 U4 c! Z9 p. `; |
, b5 T, [# F: C2 w5 q% W3 ~
      root /web/wordpress;( h+ k" c  p. g& x
      index index.php index.html index.htm;2 i$ `% D8 I( V" f
  " N' d( E& ^* ~3 H5 |+ T$ ^
      fastcgi_pass   127.0.0.1:9000;+ _0 B0 q+ v, T. \
      fastcgi_index  index.php;
& G& z% x# X: p; m3 Q+ M      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;8 b5 Z( j2 U; L0 C
      include        fastcgi_params;% L+ J2 y' f* K9 d+ u9 r
      }$ }1 y, [( z  b2 |# r6 W
  }( j* c$ Q" `! Q, R* C9 `& F/ s
upstream负载均衡:
- j- @1 I, e1 n" m
' v# b: {% Q1 R3 A# bupstream 52os.net {* t' f& ?* G; `. F* x  O7 H, T
    server 192.168.1.100:8080;4 A# Q8 ~6 _# U% ?& c' Q
    server 192.168.1.101:8080 backup;
' x" F. [9 M* ?0 \; `6 b}/ K- ]. P; h: ?5 A4 q

  a" b7 P8 \9 g5 d% o5 }server {# ?7 G- \' a( w$ U8 B6 x
listen 80;
6 R5 ?5 N8 V- e# Mserver_name 52os.net www.52os.net;) n! C: G3 R: n5 E" `* h
: f, V: {3 O1 }; Y6 H
location / {
( W; i: J- |$ k' _4 ~$ H" n8 |: a    ModSecurityEnabled on;  # U6 y9 y. l  l+ S5 q7 d
    ModSecurityConfig modsecurity.conf;  
. E# ^7 l: F! h/ o7 ~) \" U
: Z* y; d1 \  y( ^% |        proxy_pass http://online;
% N4 H9 M0 y5 l5 o        proxy_redirect         off;4 u' m' o' H6 a5 ^+ }0 c# d
        proxy_set_header Host $host;
* h$ o; C, u6 C; r. x- e  s        proxy_set_header X-Real-IP $remote_addr;# w. u- o+ g8 B7 V2 G
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
+ D* e$ u  D: G% m    }/ K' V' B! W8 j4 A) D1 E: S
}
: y2 Z, E! M7 t, d2 \- k8 v六.测试; U& W% ^! r2 z7 R+ {& c8 w
1 W# l7 d2 T* b$ [4 \" v
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:! x  k3 k5 j0 R8 c7 |" ^$ g/ u( s

" |; u2 ~" X8 m0 x- w% P* a<?php1 S5 q. g, @7 H1 @
    phpinfo();   
! E( P4 [: x- i& u; [! Y?>9 N/ N4 V% C9 q+ X2 c% P
在浏览器中访问:
' i7 W$ M) c3 b
1 }$ @; D9 i# K9 m( [0 whttp://www.52os.net/phpinfo.php?id=1 正常显示。, W- z  k- K. f/ U9 n- M
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
3 P& i6 }  Z. u% H: fhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。0 Y; o7 A  S! x& y
说明sql注入和xss已经被过滤了7 L7 _4 E3 w# e' k# w% R7 o
3 l6 h) g+ j! q1 A: u. Z
七、安装过程中排错
6 X6 v$ [$ G  ]  z0 F: P' i/ Z
1.缺少APXS会报错1 b0 S* Q0 F6 r/ }" U
: m1 T6 O$ t% z" F8 n+ F' m
configure: looking for Apache module support via DSO through APXS
9 W% Q; @- M( J) Cconfigure: error: couldn't find APXS
6 g( T5 U* p9 d# N, japxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。, F# O; ~$ T" Q$ C- y7 I! ?4 G' e
解决方法:
3 I# c; A" l4 L7 D5 v0 F* h+ ^2 m" v/ y
yum install httpd-devel  \8 \1 Z6 k: ~9 s7 F
2.没有pcre
) h( }6 L/ ^, j2 ]! ]- d# O" B5 H' H) o$ Q% i
configure: *** pcre library not found.
1 C9 x! c/ B8 G) k* I8 Q6 b8 Cconfigure: error: pcre library is required  @7 U! b/ T9 ~
解决方法:2 Q$ E+ |7 P8 p% V4 T: v; T6 V* `
3 r# R7 A( C& ?& W. R
yum install pcre pcre-devel$ K; N+ g* k: M% u& l# `4 l! ~
3.没有libxml29 t8 D% y* ]* F7 ~0 d

* x; X& {) a& x: y1 y: @* H9 b0 B
7 V$ H  C0 H0 M: Y) {configure: *** xml library not found.' J8 A) X$ l# g! w1 z
configure: error: libxml2 is required) G- M. X7 H" {  _. Y
解决方法:" p- `4 [1 `& ], L6 }) A

" h! J0 F7 {6 D8 Qyum install  libxml2 libxml2-devel
& S. n7 i* c4 t% q. N# \! {  S% L4.执行 /opt/tengine/sbin/nginx -m 时有警告
: _5 R/ b+ _$ R% F- ]/ ?; f8 ~: [5 p7 I7 x% Z0 o0 P
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
% A& K/ U9 |' Z$ H- b8 N% C9 Ynginx: [warn] ModSecurity: Loaded APR do not match with compiled!
1 J! W$ C6 Y9 y原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
% @8 G# Y: W0 [7 I4 k1 [2 `7 j; r1 E  p) ^
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.6 L4 S0 ^& q. x% \9 K& V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"' ^2 e, P, c; U! [7 R
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!. f8 U& O7 R6 B
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05": v/ V3 J" U' W6 e
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
+ P6 k8 {- M& G, H& i- [2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.* I4 V" s( ]9 D9 l$ _0 {4 }
解决方法,移除低版本的APR (1.3.9)8 S3 U0 y; B3 B3 r0 P
; P% y1 B8 H. \- v1 u1 x, @! Q
yum remove apr# `4 k7 i5 n( u  q
5.Error.log中有: Audit log: Failed to lock global mutex
8 B6 ?/ q* u7 N* J! M4 i9 u; v) w' D, x+ y9 b9 f% r3 B- {1 ?% \+ y
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     : q% Q1 [9 d( T
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]4 l+ G0 l  t2 c& c
解决方法:
) _/ @  j- ~, Z7 {编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
! n# m2 B. c" b8 Q, q% ]+ c8 O/ T3 k, g+ Z. Q$ w. ^% N/ A' ~
SecAuditLogDirMode 0777- {; r% J  g+ b+ R% B% M$ F: s$ J6 V
SecAuditLogFileMode 05508 N. B4 d5 n* c; E9 ~7 T1 `) x7 X
SecAuditLogStorageDir /var/log/modsecurity
" y# a3 [9 [/ {- o+ i3 J) gSecAuditLogType Concurrent
1 c4 |/ C6 C; Y, y! s/ c; I& o参考文章:9 c1 \. r6 O' W4 ~+ u$ i
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
- m! F& w4 u3 K1 D* r0 U: dhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-23 02:08 , Processed in 0.064480 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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