找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9780|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
/ R7 D8 d( y+ ?$ ~; f& C; V- I4 c! c. d& z! B# }
一.准备工作
8 O; K: u# }: K7 \' ]+ n# T# A9 U9 [3 P; s6 D% E
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.01 {8 R8 O0 n7 X0 m
: ^0 k2 }; q, Q  b, a' L4 A5 \
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz, k4 |1 H) C) k9 b
  U- n" u8 e1 j6 T- O- x
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz# [* l7 c" q" o2 @0 J

6 F" A2 C! L3 }1 R- ?/ _" n  ?OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
! W) X, x9 G& K9 U1 i& A  R+ B% o
依赖关系:7 [( g3 q6 I6 C! v; O
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
9 Q8 i) S' K/ n8 U6 U$ ?
  r/ c/ l8 N8 ~( zyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel; ]1 C" X3 y4 I* a
modsecurty依赖的包:pcre httpd-devel libxml2 apr6 o. V' ?& l6 G4 t  S( Y/ M

& G, S4 g1 h1 `3 Uyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel/ Q+ l* o( O/ I0 F5 t
二.启用standalone模块并编译% r  w" D2 s( i  g

0 N+ D2 e4 n2 M; Q6 Y0 x: V下载modsecurity for nginx 解压,进入解压后目录执行:
( a6 B: A7 S+ m2 G7 s: d* Z8 `0 a$ B0 r/ B1 ?) Z" z
./autogen.sh
. J- {+ @  O0 A& k6 @% H./configure --enable-standalone-module --disable-mlogc
- I; {- @: Z& v; }make
% n/ E8 ?) N' T9 s三.nginx添加modsecurity模块8 V; m% J1 H( k- w" g; G; G

% \2 U% G. Z! l8 r( p( _: F( \在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
) i! Y. e3 ?3 K% [7 l) Y- E5 X# |. ?9 w; u# I6 S
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
7 P2 S& S3 Z  o) C) `/ {make && make install5 y, O" B0 S& l$ |4 I
四.添加规则
7 Y9 u4 M3 H( M  G" b
# X0 c# A9 Q3 I7 a3 M* Omodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
* r/ N6 X  p4 U0 u9 E; X- K/ ?6 l, F7 p- ^$ w) ~8 [8 T
1.下载OWASP规则:- z6 M) q+ V) e& {# X1 L9 o- Z

( Y  `9 F7 e! U' @5 ?. V- Ugit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
8 b5 M4 X- y6 e2 h( \" ~( N" u. T- v; \; ]5 x0 a( X7 u
mv owasp-modsecurity-crs /opt/tengine/conf/
" K  f' ]/ W- ?7 a
3 D0 j. C% `7 ycd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf! y- s+ r1 x& v8 X/ v% l6 h* d
2.启用OWASP规则:, p8 j. w; a+ a

# \" F* L2 U$ V# R" V/ k复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
. b( I& l3 \& Q) q( @! E- `4 J4 C' z4 U6 N$ G, u3 r
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on% E2 M; t$ U% j2 h2 F+ C( C* r
1 v) n( N) _. v: m, V4 d, u5 x
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
6 q$ _: D* b3 Y( V3 j2 D! T( _  m$ q+ S" i
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
+ W. H# @* H4 I" ]4 lInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf: g6 O/ `' @. L- @/ L/ A* t3 \
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
; l2 n' A' L4 D# h* YInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf. a8 n- q2 i& Z' c
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf$ c, }8 S5 ?& U3 l( S; D
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
& u$ ]& ~; ^( k' w, c' |+ lInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
4 E6 X' d7 e+ X$ X" L  A  b五.配置nginx+ n; y* X" I2 `) o7 A

3 C: R7 c8 i8 V$ }9 x6 C8 ^在需要启用modsecurity的主机的location下面加入下面两行即可:3 q% r) g$ K! S8 Z4 z& }
& w+ C/ i7 [1 u  `) c  x7 h
ModSecurityEnabled on;  , s# X4 V, ~4 |( }3 F
ModSecurityConfig modsecurity.conf;
" ^1 W! q% G$ [/ y下面是两个示例配置,php虚拟主机:
$ w- G$ E5 p1 U9 E0 A8 i& j+ z/ Y
8 T4 Y, v; ]+ Eserver {
9 f3 Z, a) \" a$ T      listen      80;
: M; }! b& K% J0 W; U# b      server_name 52os.net www.52os.net;
( k3 b. R) k; T" f! ~; N. x     
3 ^5 Y% o7 }& x, n7 k      location ~ \.php$ {
' ?$ N6 [/ C  j4 T% Y6 O      ModSecurityEnabled on;  7 J' T4 h( f1 u  ]% s8 P9 O- v
      ModSecurityConfig modsecurity.conf;
3 Y% i. X1 L+ B- ]$ C: G, x! n4 D9 M" E- m2 p$ S( f
      root /web/wordpress;% ~* k( V( M2 {
      index index.php index.html index.htm;
1 p$ i/ W4 U0 z( l6 r5 u  
  i. R. J( s* M9 k( [% j  g5 Y      fastcgi_pass   127.0.0.1:9000;% h1 ]7 A9 Z, U$ R7 `2 [5 M* q
      fastcgi_index  index.php;: W' c/ W% E; d! A2 ?1 f% f
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
( W5 ~: D/ q( T; s: ^& X5 ?      include        fastcgi_params;
- P% {- L6 N, e5 X      }
, Q: j5 |9 }0 M+ K1 g7 O0 d  }
5 f0 ?" P2 e5 F7 Z9 Iupstream负载均衡:7 ?1 j, ~/ b" A& N5 S) |9 N

+ T( \  R1 f" @7 e. h  hupstream 52os.net {, Z) w; @3 a, I
    server 192.168.1.100:8080;7 Z, V- @  K4 P& ?. b
    server 192.168.1.101:8080 backup;- |& X6 y7 W0 ~( B. K1 H
}
$ O8 O7 B- ?3 g% @, {9 ^4 f  Z$ K& K4 ~; W! R7 |
server {2 U. B6 @4 U% U8 ?
listen 80;  U1 L5 ^/ F- O1 S0 j
server_name 52os.net www.52os.net;
- q- T! K( P2 c2 `8 ~1 N. ]4 L; Z4 W/ z& y; O; E2 V/ k& ^4 j( B8 t
location / {
. P$ n- J0 }8 V7 e$ c5 F    ModSecurityEnabled on;  
: M8 I& z, ?/ u9 l1 W$ |! M& R, Y    ModSecurityConfig modsecurity.conf;  $ E' }) B& Q4 N& y8 j6 t4 Q5 F
; B0 Z  S. l6 t) f' P! Q
        proxy_pass http://online;9 I6 U' \" o* M( \6 L3 h
        proxy_redirect         off;
. J& t8 N  p2 S& Z# P6 K! O        proxy_set_header Host $host;
9 l# T6 G& k; Z3 O) {7 Y        proxy_set_header X-Real-IP $remote_addr;: t, E4 D& s; G( j5 o" s8 d1 I
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
4 y4 D4 J# r% ]  w    }
8 T- w) R7 Y' }}
6 C7 r" @, P$ Z; H六.测试
; `0 w8 e2 ]. P9 h
/ I$ E0 Y  F8 v4 q" [/ G我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:- o5 w. E/ C1 b; p! T- E9 h( Y1 J

6 _  C" [, v& [, K7 q' e<?php3 w9 }& N8 O" T7 r
    phpinfo();    + ?+ @+ i* S7 }6 J  Z1 I
?>
% a! w& E7 r) K' Q0 u* W8 A在浏览器中访问:
# e8 c8 j. Z; x" l  Y) r+ k7 f. `8 A- p/ K& G, w$ K' \
http://www.52os.net/phpinfo.php?id=1 正常显示。: f* I" u0 r/ ]# k. ]
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。' D1 v5 ~% ^  {# a8 V
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
! ~) t+ n& k7 Y* i% h0 i. I说明sql注入和xss已经被过滤了
% E3 \' ^3 S1 ]8 @9 x& h
  Z! U; g0 q3 R' s0 s七、安装过程中排错
% x$ b% n7 T0 v  k+ X; {: @* V
  _- w0 ]/ f7 B  X1.缺少APXS会报错! W8 i6 N( L% T! }% m! {
: u1 W/ q2 e. j2 f% x
configure: looking for Apache module support via DSO through APXS, O) V. h1 s, x1 u+ J& O6 M
configure: error: couldn't find APXS8 M8 U) O% [/ Z) d) Y
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
# l8 x+ \# a1 \0 U1 A: G2 r( J, T解决方法:
) }9 x' [, m' ~0 m' M' s1 f- K
/ U* E* }0 _6 h  K% L8 P+ P# hyum install httpd-devel) l; b) r5 Z3 Y# @! V6 o6 C8 ^
2.没有pcre
$ H' ^* e- r; [' u  M3 u
. k+ t! C% q% Y8 w; I- p; ?+ E: Xconfigure: *** pcre library not found.
4 c* q  I; ~6 K- P# U4 Sconfigure: error: pcre library is required
* U. |5 {  X* D3 _7 D. D# ~解决方法:& S7 u8 L) O  u* T* ^

0 l& U" r/ ~  hyum install pcre pcre-devel! G9 [0 b1 A9 H- c4 w
3.没有libxml2
! I1 `( I, ^& [) J0 N! b
3 F' P' [% X" e- X
! v7 J/ ?  i; Y0 V. y7 `% I; z8 ~configure: *** xml library not found.
9 e* ?* J1 Q! C  k& d9 D5 A  Zconfigure: error: libxml2 is required
7 G( q9 W; s4 M, B$ P解决方法:
) V. O  C7 v# c7 t% w5 Y" S! U' F' E" J  b
yum install  libxml2 libxml2-devel
+ ]' U/ d) T% [$ W4.执行 /opt/tengine/sbin/nginx -m 时有警告
; L2 L4 c+ w9 M; B% J6 \* a. H& A( M/ D
Tengine version: Tengine/2.1.0 (nginx/1.6.2)- g* N* M: n3 H. o' o" ~
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!9 @$ f* U' V+ Y4 _, v0 U* d
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
' @5 ]$ I3 i% V- r8 N9 m& o% n
2 \" f. H+ J5 i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.; b7 ?, a! `' R* N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
& d% h: L. y8 k" M/ r2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
# T+ S; {& ~! `: D# o( [$ f2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"- F/ v, a! L* u. p
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
) u) T7 p, |( r, q6 }3 w% n3 g' N2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.8 U1 X, {- D) z* ~
解决方法,移除低版本的APR (1.3.9)
! P- f, G( f  M
" f; L% c. G  Syum remove apr
9 V5 b# w% h+ L3 d1 t9 z5.Error.log中有: Audit log: Failed to lock global mutex
+ c' h4 g2 x4 b" i) q) l
9 o# Z2 x% L; I" j2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
* Q7 K" Y% [3 L3 {4 M8 A# Nglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
6 Y" }0 J4 E+ w* c0 K2 n解决方法:# W1 |" m! h) M1 ?8 `
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:4 a, _$ b% ~6 l( z
2 N( u1 D' \$ i. v4 \1 e
SecAuditLogDirMode 07770 A0 e" M! r% ^! ^5 [- H8 K8 T3 X
SecAuditLogFileMode 05501 a% X8 n8 K1 \' f" b
SecAuditLogStorageDir /var/log/modsecurity4 k, [, a5 K6 N! J9 d% q+ d
SecAuditLogType Concurrent
3 D7 ]" |1 ?, I3 k- d9 N2 C; p参考文章:
7 J) P7 Z' D) u( A  N! q) Thttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
  k( j  e( f) L' `# k# p- p3 |http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-1-31 13:56 , Processed in 0.089447 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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