找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11962|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
! i+ I! A7 [; ^, J6 [. B6 A. x3 p& h$ k
一.准备工作9 e* c3 p1 q% `  g7 X
2 \* j" W# z+ t# \3 U" @, V% i
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
' D/ x- B1 p9 p
( N6 _1 N2 \, R' Ctengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
* k5 ]$ b2 y  ?2 t! N. b5 E! y
2 N2 a- y) [) k6 H- ~. Z5 c- nmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz' U! A/ r( I; W  }& a

( Q$ I9 `2 {) [OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs% @. i" D. A1 ~  p

3 X# t! p+ c- d# i8 g依赖关系:
  o+ z+ {4 _" C3 z% Y3 R( ktengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
$ r  I, _' y- s8 U" ~
5 C1 i6 E  J" t) n1 c  h- _( Q+ v6 Vyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel/ @, l' F6 s1 }  W0 E( ~- v% ?
modsecurty依赖的包:pcre httpd-devel libxml2 apr
# Z9 O; n$ D0 f# E
- _2 j1 H2 v6 Y4 oyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
$ K! V! n+ g/ `. }二.启用standalone模块并编译& m) D7 L# c: K! L

; }: d6 Q. \& H7 g下载modsecurity for nginx 解压,进入解压后目录执行:
2 O, Y' s5 c3 ?9 W4 D( i
8 C& ?3 D, b2 r) z" H./autogen.sh
$ S" r# y0 {9 V( ~6 h( i: h./configure --enable-standalone-module --disable-mlogc
& [5 c: s: S1 M+ Imake : W' R( o/ n6 ]! R7 q' D3 g- v
三.nginx添加modsecurity模块# Y7 z- [& Q- J; O2 t0 ^

5 p- p5 i) ~3 U1 F1 v% P! t在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:( K3 f# O- [; v5 l* t' f4 C

; @6 F/ y8 |- b( @) U./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
: V# `% l. W. d) P- i$ \make && make install" g3 x) B4 A* U  Y/ r
四.添加规则
! K! V. b* D- G; I$ l3 }/ c( N, g9 W  g
: S! P1 F# @4 D. y  c, Dmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
$ C% y% F$ s; j, l3 A2 d
* m) A1 O% q8 [8 W2 ~1.下载OWASP规则:! T9 `7 C5 `$ {. E0 |6 z$ U2 x3 X

$ \) i5 L& f* ?9 x" f7 hgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs. X* E' p( d: K$ F" S
; |# a2 F% ?9 i6 W. x" f+ K
mv owasp-modsecurity-crs /opt/tengine/conf/5 w! b- K: u# ^3 J6 b) |
5 L' a2 A7 w# ^6 _' ?9 w! |: p
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf+ ?+ A& Q0 c7 h
2.启用OWASP规则:! H( ~8 a3 U4 L

7 Z) D9 v0 ~5 _% h, m( |1 V复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。" L6 m& P/ G2 v5 w6 M6 S
: a: N: v( ?: f1 y5 K& A
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
, G& g4 r, [4 f- z2 \2 K/ y. K- C) y
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
( ?  W% z2 u" |9 {  p" {! h
3 [6 f* f) r7 M- b2 m6 ?4 y6 VInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
5 y, h! c6 ~7 B; c* AInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
5 u0 R' F: d/ v1 ]: rInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
7 m% N  L+ O# f& ~( o4 B! tInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
7 J0 z5 ~. f4 F, f4 qInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
$ f( D) ^( N7 ]Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
( A( ^/ j* I4 c3 U8 WInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
* k! w) ^" o4 E8 Q- D9 V& }' G五.配置nginx# ~# K; a4 ^8 r  _/ c
5 v6 c2 T- G6 q
在需要启用modsecurity的主机的location下面加入下面两行即可:
: R3 h+ N5 G1 e$ ]* |) h/ H" z1 ~7 H  h  E7 C* S
ModSecurityEnabled on;  $ w3 H) u9 `( O/ _
ModSecurityConfig modsecurity.conf;
8 s5 U: M+ h6 p, X下面是两个示例配置,php虚拟主机:
0 r4 A6 ~3 t8 H+ V! N, F& K! S
% m! t) h  N( G# D- zserver {$ K& o/ {" t1 S6 F/ w; ^% g' k: h
      listen      80;  P2 \: R, \! H+ L8 E
      server_name 52os.net www.52os.net;
- C1 F+ i% V9 w/ Y     5 D( h8 a$ L( ?
      location ~ \.php$ {
% i0 y2 s8 r8 H1 a) b9 e: x0 X: t, F      ModSecurityEnabled on;  8 }% S& U0 G- m# L, Z5 A. {
      ModSecurityConfig modsecurity.conf;9 v# O* p9 E- l( w% J; t$ u5 K
% F( A( a( A4 w1 H9 G+ X
      root /web/wordpress;/ y- p( y" B- f$ `, {* U) h/ e7 A
      index index.php index.html index.htm;. v  m2 P% O$ i6 ]: x! l
  
; J7 p; j# w& p/ I3 q" j      fastcgi_pass   127.0.0.1:9000;; D  F. I$ l6 {) B% N
      fastcgi_index  index.php;
6 f4 P9 M1 v5 w4 l) j      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
, I7 t4 o5 I: N) |; H# j      include        fastcgi_params;
1 {2 E& _# L6 H0 f& g      }
2 o- \, U* ?0 d3 s5 q; c1 z+ X  }
: q% l( `' E8 ~$ M3 \6 k, I: M9 Fupstream负载均衡:
+ O3 V) j/ I' \* L
7 s9 j+ r4 V# }) R+ Oupstream 52os.net {
, ~. k' [' |( i6 m! O8 X8 E    server 192.168.1.100:8080;7 ~! I& j: D& R/ Q
    server 192.168.1.101:8080 backup;3 A8 j( e* _$ R0 Y- M! j6 M5 f
}
7 N5 a7 E' F$ k0 W, L( K% X& q" @, z2 w8 x& S/ g: l0 g
server {
6 j& I% [5 q- ^( Tlisten 80;, e4 ]' _" y+ |' j+ H1 S( B/ q3 R
server_name 52os.net www.52os.net;: X4 q6 Q1 d9 o! F

) ^, G4 f6 k3 ~- Y/ G* Olocation / {
( {$ U8 o$ ?0 I3 C) u6 U% x, p    ModSecurityEnabled on;  , O) n4 q$ v1 X: E- M4 |
    ModSecurityConfig modsecurity.conf;  8 q) g0 }1 ^8 n5 R/ h

1 t' H1 ?* i) B( C' o, T! r        proxy_pass http://online;2 x8 Z2 m: ?  J
        proxy_redirect         off;9 e( z9 ~' ~, \( J6 p2 F; n3 K
        proxy_set_header Host $host;
1 q- ]7 C; g4 \$ q3 W0 P        proxy_set_header X-Real-IP $remote_addr;
& `7 r% a, Y4 j        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
4 P( _1 J/ E5 i* Y    }
9 P3 q4 S/ }' ]}; W( E4 O! T3 [( _) n5 h, C
六.测试
" O- s. P" h9 ~- |
# ]+ R4 o6 b  Q5 |$ ~' a0 [8 w5 f0 i我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:3 I8 n, |" A' n  ]
% w: M! o. j7 h! g) ^
<?php
6 Q0 N, L  z' {6 V    phpinfo();    , H- K, I+ ^4 R$ k, Z; q9 ]
?>
. [: u2 J+ G* t; C1 _在浏览器中访问:
' J' R5 j# _! M7 M& a* p; V! x
# b3 s6 E  E. Y6 v/ P7 ~http://www.52os.net/phpinfo.php?id=1 正常显示。* @5 }* t+ g2 X; }
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。. {5 U6 N( \2 j5 I( P, p
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。* M# W- }; s' n7 t2 J  h
说明sql注入和xss已经被过滤了# I8 h' R! p  m. p! n: s

+ L# R, Y6 d2 L& }' @. m七、安装过程中排错, z: a1 ?' A; t$ \3 u+ M

+ G/ G4 A8 F" u' u1.缺少APXS会报错. G2 r- E9 e& k: h
3 T+ u' F$ W9 ?' b: q) I
configure: looking for Apache module support via DSO through APXS
6 F3 b; I* ~' @* J+ ?configure: error: couldn't find APXS
8 W& m6 Q2 w! H7 U" Bapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。+ a1 S  [3 B) Q# \
解决方法:
5 \; v$ x1 }8 r: U) j
- z% j) W  p% n  Uyum install httpd-devel
2 U, _! d9 Q, A- V( ?, }) `7 D2.没有pcre. p& w1 ]/ g& L! g6 N& |

7 E' I! q, R0 [configure: *** pcre library not found.
9 J* `' d5 Z9 B& W" pconfigure: error: pcre library is required
' Q, c' p) K3 B) U- v1 ]# Y9 }. E解决方法:# E/ ^- [+ f% l
9 P) {2 f3 t7 B# @
yum install pcre pcre-devel& E& B/ V/ V& v* M1 U4 o/ q; _
3.没有libxml2
- B: g3 X1 w% E! R' _+ W9 o
, ^/ w6 E9 o6 Y$ N2 [1 S% w& E" Z4 t5 y- |$ h3 N
configure: *** xml library not found.( w! M0 }& |( |, T" U; @3 t
configure: error: libxml2 is required( J. x' r" P7 S2 l  S/ Z. Q
解决方法:5 b& j$ H+ M5 [5 Y: S2 f; C

* H  |1 E2 l. e$ R3 j) gyum install  libxml2 libxml2-devel
5 I7 m/ S, O! W* z+ f1 {4.执行 /opt/tengine/sbin/nginx -m 时有警告
2 ^# `# Q  h1 f0 z: ], i6 V" d2 q
6 W" N% c' U6 z( B& j* BTengine version: Tengine/2.1.0 (nginx/1.6.2)
( M* a6 x5 r: g  fnginx: [warn] ModSecurity: Loaded APR do not match with compiled!) v/ g" _4 n1 U# u) v2 }
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
+ l# E4 v& g, @0 t- O
% e# p4 a1 ^+ `) w; ~2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
3 `& n6 a  B0 E2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
: z9 b: p  l7 C7 M( P; V3 Z2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
; L# _+ a. f$ b8 V# U! `2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
, H/ t$ A% K: t% [5 {2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
% |: ?$ T& i+ Z% N5 G2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.& e! N% }7 M  J& f) o
解决方法,移除低版本的APR (1.3.9)& z9 l8 E6 g: J
3 u; x; I0 b/ t, [
yum remove apr
- _) V) C& ?3 x9 {7 i; R5.Error.log中有: Audit log: Failed to lock global mutex4 D) _& L3 r9 i' r

% P8 t# W. @4 Z$ M% a) c5 x9 E1 B) s2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
9 z  @! m8 m0 |; T% r2 o. ]6 wglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]1 E6 p8 J8 H1 ]: h: m1 c( K
解决方法:
( f  R( h2 h' u4 F7 u8 z编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:% m( k" W. _! N/ k' p9 n; X
- j( Q! K% }! }9 t, I, D
SecAuditLogDirMode 0777
' C$ b1 c8 K$ B1 X9 ^4 Z; @( xSecAuditLogFileMode 0550
2 F) A5 n: y( SSecAuditLogStorageDir /var/log/modsecurity
4 A. Y4 R! s2 b0 jSecAuditLogType Concurrent# Y* K0 I6 `5 x! Y0 m
参考文章:
4 R6 {  l# }$ h# P' ^https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX% g6 z  M$ h4 D" f+ L
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-30 08:10 , Processed in 0.059180 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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