找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13158|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
3 m' `  t( a% g9 D5 {5 W" W. A) l  \  ?! p" @: K- A7 [0 r: e" }
一.准备工作9 Z; H. j2 \- }/ s! n' J  J

. {4 x! v2 l: q9 f  R" u3 N系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0  w& D# r: I" L  y, }" I3 B

( }' o! B9 K* d" H- xtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz) r1 G! ]" |6 M/ I
5 B# _# f3 K, U# S1 M# \( a
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz; e. d2 p7 z) f9 R
, j! i3 _3 Z3 v5 H- |/ @$ O
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
' W' ~5 v  ^  ^9 i. v1 |  Y/ x% k) V, N: x
依赖关系:
+ ]+ }. Z/ n* M& u! y: D7 K2 etengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
7 i7 E* c7 m, e$ l1 o% m/ i+ w8 G( D1 [) i2 I
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel- ~3 K5 H% |! e# |
modsecurty依赖的包:pcre httpd-devel libxml2 apr
6 m* W" y+ y( p. D2 x8 L% d6 Z) v% ^) {5 M5 g$ D3 e7 F  d
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
) J8 Y) [8 n( v" f5 A. o- p& g% S二.启用standalone模块并编译
/ i1 \4 ]9 a* L* d0 H" f+ _6 B# x; s6 ~! I( z2 P
下载modsecurity for nginx 解压,进入解压后目录执行:" L- B: o# `$ `: _% W' A" k0 [

2 r* n5 n. H* i* p) Z- W! U6 r  a./autogen.sh
( X# `. ]1 n8 m$ a4 x! h./configure --enable-standalone-module --disable-mlogc& C7 ?& `( y; ^
make
' z( D/ l  B& p% x4 F+ o三.nginx添加modsecurity模块- i- e9 u- r* }& L4 V# x. y
, D: Q  p- w7 a, n
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:. v8 R3 _, F. J1 b: }* M

" @/ O1 ]; s! T: R- C2 x) a./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
+ [$ P% `) E  u, z' P7 `2 tmake && make install
  a5 F; Q0 u! X0 R$ C' b6 |  \四.添加规则
5 X  m' L( S, k, j- \& @* k( k- H% {# R; h2 n- S7 l* j1 m
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。$ y1 k+ }1 I3 [$ D2 h. Q* g( s3 N3 H

$ F* |7 Z9 P6 |2 v- v7 f1.下载OWASP规则:
, D' E# V* k$ G1 z% U+ V$ K% U2 o
5 e: k# z0 L9 v8 z$ R9 I8 qgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
6 `. p3 {  c- @4 Q( Z4 I& d4 M1 T
mv owasp-modsecurity-crs /opt/tengine/conf/9 D4 |: s8 J9 p& Z. Z4 ^

% S3 A" e) q& N) |* Acd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
8 f; Y* F; |8 o5 Y+ R2.启用OWASP规则:# L1 u7 l' M0 I& X' ]. d+ N
; T% A& z( ]$ d! D& Y
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
) b3 a* g7 [1 P7 D% w
$ L$ R' A" m/ J编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
4 J8 d/ J3 S  \) ]( R0 Q) ?# [  V. h: z1 C# o! L: ]
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
2 n" f; s7 |: p; ~0 g- d0 {2 M4 V  B+ F( {& _' c
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
% K! `4 h) M; g: W& t$ u0 SInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
# R4 Y- K% [; j& KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
9 a  X3 {; q. m% d! o4 s9 H$ l" CInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
! A$ k! n+ u' u$ fInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
3 w' P1 G  {0 I3 ^/ k5 j6 CInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
5 ~4 x9 H* Z) }) P4 _) J# j9 Q9 d. TInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
9 K& ^. ?' ~% g. R五.配置nginx
4 z. z; R1 w7 C9 w# J! V9 |! u
& {# H; d3 B+ E在需要启用modsecurity的主机的location下面加入下面两行即可:
5 M4 w9 F, z! d4 N# S
5 f' C0 I% T; [6 o0 OModSecurityEnabled on;  / C4 Y: g2 W: z3 y
ModSecurityConfig modsecurity.conf;
4 E* @# C! b; S  X$ @下面是两个示例配置,php虚拟主机:9 S' }3 C5 ?! h( g2 X; Q" A

9 e+ B5 Y! h0 x7 b1 X& m9 z, ?server {
' q. g. s; p: e; z, ?      listen      80;
" H" M) w/ ~( a+ S      server_name 52os.net www.52os.net;% n) I$ W! p, g7 m: m# p  D% w8 |
     
0 I" k: p  A! V5 b! o+ m! g      location ~ \.php$ {$ g# ?# u# H' A7 j
      ModSecurityEnabled on;  ! }. ^: g. E' M, Z
      ModSecurityConfig modsecurity.conf;
, w8 Z8 ?8 {2 G& [
$ U% c% V1 U9 q3 d: J      root /web/wordpress;/ M4 g- B8 W7 `" ^( |8 N
      index index.php index.html index.htm;
. T. y) S' X' Y  + i1 k5 l1 @- S% B4 S0 @3 {* Q0 r
      fastcgi_pass   127.0.0.1:9000;
, G- _5 W. K' F. P/ F) S      fastcgi_index  index.php;2 h1 ]7 X3 Z% u+ J0 f% H$ j
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
5 r- ?# |* R! y8 D1 |1 c, t% h      include        fastcgi_params;. H: t; l* W9 f' K
      }
; B* m$ Z. r0 B  z2 t) J  }
& C0 b3 ~; Y5 I7 Z3 w& ?upstream负载均衡:7 b; w1 p  M% R9 [5 O

% m6 C$ y+ Q5 b. @/ d' B! T$ H% Uupstream 52os.net {
1 A1 i/ v2 ?6 }/ F5 I    server 192.168.1.100:8080;
' W  L) V5 }) ^    server 192.168.1.101:8080 backup;; m& y8 I- s8 v- h2 T" u, r
}) q  \) N' v  P8 s# P' [, f1 t( W7 C
6 q- j: |5 e! E& A+ e( a) i- c/ Z
server {& w! U, H5 j. I* l' m% D; ?0 K
listen 80;4 [# N7 M0 q+ D" e# \
server_name 52os.net www.52os.net;8 ?3 g  r5 j4 y; d) R

) \/ u* f+ v+ z, G/ P$ Q3 Mlocation / {
( l9 ~9 {4 f+ e8 B. r  |0 ]    ModSecurityEnabled on;  
, b; m" F& W/ {/ L- L# ]" \0 T% o) \    ModSecurityConfig modsecurity.conf;  * c; O% `9 }% x2 {  i
- @  l! w/ J: g
        proxy_pass http://online;- N6 L, Q. A% p' t/ L8 \
        proxy_redirect         off;8 Z0 v) G8 K( B: b8 D
        proxy_set_header Host $host;% J6 H; v) a0 |. @! O& w
        proxy_set_header X-Real-IP $remote_addr;# U9 ^3 U3 U* Q
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
4 H5 H, g8 a/ ^9 E% G. B    }
7 _$ v! j0 I* B: `}* ?' l/ L& t9 r1 R7 }% l9 e. U/ K
六.测试! E, o$ r" z* y' ?  p

- ~3 w) M: s; i+ A+ n/ f5 P  T我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:9 m7 G/ s& i+ a: Y9 r% w5 Q
1 P2 o& L$ E$ H4 l* ~
<?php
! o. R7 x. d" r    phpinfo();    3 E, @, a% t* J
?>* |& T5 i5 e7 K! l$ [8 o$ R
在浏览器中访问:
$ M$ ]$ w5 `- `% @
: f; M6 c& s# U! B, K" l) @. Z, Phttp://www.52os.net/phpinfo.php?id=1 正常显示。
1 Y/ ?1 W& y# R7 A6 zhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
) H6 ~$ y4 t% z* b8 zhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。0 ?: v9 s5 U' z( z* C3 R- D) _( X* m
说明sql注入和xss已经被过滤了; l! h1 R& o5 u! p
: Q/ E" Y1 C6 |2 k( G; G
七、安装过程中排错
2 {- k# B+ p/ u$ K- _
; c' Q% G4 N! k) ]1.缺少APXS会报错7 }* t* h. y1 i. X" g

9 U: s) F( ^# \4 Y/ g+ O# Lconfigure: looking for Apache module support via DSO through APXS
- K3 ]! s  f. @# ~, Pconfigure: error: couldn't find APXS
" O6 \' P. l  q/ uapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
- C- Q  b8 Q: d! b解决方法:
  X' s( N0 p$ y( l- L* F
" M; t. h; A! M- c5 syum install httpd-devel
. T/ m" {7 o7 `/ j7 p& M2.没有pcre* h1 c" f3 C2 u7 q. n

8 b. o& h7 u; Z2 k5 r1 cconfigure: *** pcre library not found.
' R0 r7 u9 r  \configure: error: pcre library is required0 d( j. _! v  S1 E9 a, w1 I6 h
解决方法:
. E2 A0 W+ v1 |$ o
( c; y- F- B- t2 W( S; ]6 lyum install pcre pcre-devel
! K6 a: V  x" ]3.没有libxml2  C( v6 P; G7 k8 _; a

; j' M# B3 ]; y1 o* D- F( b+ i  w* ~, R0 n4 x' d
configure: *** xml library not found.! b- o0 h( H) o" Q0 b, x
configure: error: libxml2 is required. V$ v* a1 ^( b! |4 K
解决方法:
& ]7 d6 ^/ G$ w  F- I$ d" o1 U" ^0 ?: C- \' x% l. _% W7 S
yum install  libxml2 libxml2-devel
: j$ p& b' u" j$ m% L; r4.执行 /opt/tengine/sbin/nginx -m 时有警告
4 @, {; B/ x/ ^$ [  l- k6 P# W
! g+ f( |6 h" `* K' X  \Tengine version: Tengine/2.1.0 (nginx/1.6.2)
, m$ K( Q3 I: anginx: [warn] ModSecurity: Loaded APR do not match with compiled!. [! C6 J' b6 h3 Y3 E
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log& J3 Y: u& B6 ^9 i2 B* {
. J6 U: W) [" ~! B
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
5 k+ h: Z) O# G6 ^4 F2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
% @( X2 c/ ]$ Z2 I/ F0 O2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
$ l, T+ p8 e3 U  ^. P2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
) T/ y6 K. P3 Y" [8 ~! d' F2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"4 S; e& R3 X5 m' s, l, ]* L
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
) u4 E( Z4 r- ?6 q7 C# A解决方法,移除低版本的APR (1.3.9)
" D1 v+ }1 j5 Q) }; a% C: k' C; ^  n0 ~/ N5 `$ W
yum remove apr0 R1 w) `4 y. K
5.Error.log中有: Audit log: Failed to lock global mutex: B% D. f4 A+ u9 A" ?4 }
! J6 H9 c/ h/ q% l+ H/ Z& a* G, l8 v1 f
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
% i* m9 X& n2 R) _- h4 W0 zglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]+ r: F; K" Y, N0 l/ |7 C  z' d
解决方法:
+ V5 y' W. \8 j7 \3 |* L编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
7 o: h( `; T' w
( s* m  {" T5 z/ k5 C9 ^SecAuditLogDirMode 0777
+ @! k, j) F  l1 O% x& eSecAuditLogFileMode 0550- ]) U' Z5 Z# C& C6 x9 h4 ?& y
SecAuditLogStorageDir /var/log/modsecurity7 O( R" p$ G, u7 O) N; d: G2 W
SecAuditLogType Concurrent6 n- M: N& S- s+ O; t& Y4 Y
参考文章:
/ C$ _8 f5 p' ]- ~. rhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX- n% I2 I( j. s* j$ a
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-19 18:25 , Processed in 0.046868 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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