找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12498|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。8 l& w8 h8 Y4 E
7 E& r0 N  R1 e' T" v/ a" P
一.准备工作! C+ T$ d, h1 i, r2 S
) O2 e/ v7 N' G  x; \. c
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
* o6 f: U% x* |) w8 M7 k
  \  Q. u, ^5 _tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz: C1 A& h; {  ^5 g

+ ]; E/ P% W  m2 o* N( |modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz& ~5 ?! j9 U; g8 W& s
/ X% H; G0 R1 q- T! B+ f8 s
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs! w; x, V( T9 q0 C" A! z4 ]3 a

1 o+ X* D) f, v5 C# x7 G+ \' J依赖关系:8 Z( J$ n" w$ ?% ]. a8 u- g
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
+ Z' E: j4 k; c2 A! u& V3 z; f& I" p. x6 l+ @
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
' C. \; ]( v2 e* ?4 B! {% Tmodsecurty依赖的包:pcre httpd-devel libxml2 apr9 Z7 r- H! Z; {$ N* E
* E9 k# Y4 r3 C" n' d
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel: K2 `0 b- [+ _1 ^) R# `  s1 f
二.启用standalone模块并编译
1 }6 @' L- y2 ]: r7 Y" D3 R5 U5 U1 ~5 k, t# H8 `+ k& m
下载modsecurity for nginx 解压,进入解压后目录执行:
% F5 C0 w/ V- c* i. A  c( X  O9 c1 \
9 \; n& |9 c6 `7 C& I0 n./autogen.sh
# v# ^* K: q2 H7 y./configure --enable-standalone-module --disable-mlogc4 J( @2 L+ B" X
make
* [) e0 C- r1 O! D; s5 o4 q# `& F三.nginx添加modsecurity模块1 A9 f+ Z/ S/ ?- m% T3 q# K
9 b1 L, i  v' I  T' U* X5 _8 H% Z
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
6 d0 K+ e3 }) \7 G4 U# p* H9 a/ a. Q, k8 n8 I8 H' M% P
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine& g, `+ p3 v; N( E) ]' n) K
make && make install1 J( N, M# ^5 ?- U
四.添加规则
# t5 G: K6 J  g, T' @& s& w* w8 u" e, u2 ?
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
. n4 ^# g: k+ Z( h" i0 X) B. s8 Q/ k1 n$ \
1.下载OWASP规则:+ P" Q- G* ^) \9 p5 I, b/ W

$ E2 A& _: ]/ `0 x5 H$ Z: C! Zgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
/ S% [; Y# u1 Z# ^; o0 L4 y4 |6 `' x: [
mv owasp-modsecurity-crs /opt/tengine/conf/* L, w: u0 H% H5 M5 V2 [( V
$ G7 h* S; ^. c  e$ O9 C( L
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
& }/ _  _+ g' }( I  J* X2.启用OWASP规则:
% ]9 i& T& h+ @4 ~8 s+ z6 f0 r4 r0 H/ `7 I7 [
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
1 I$ v& L5 B4 {$ i, @
& X! O- z- G" t8 o# J编辑modsecurity.conf 文件,将SecRuleEngine设置为 on# \7 O& i7 ]9 S/ b& g

8 s* e  [. x0 a' K" D0 `owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
) T9 R* V, ^$ o2 X7 N! i6 H
8 k3 }* f+ f$ A) n/ {Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
! j2 F, E& b; tInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf$ W8 k2 J+ t+ Y/ o
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf& z" O3 w& u) L0 f- U# M
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
* b- r; s3 v' |1 pInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf# P* @4 @" I3 X
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
3 y9 t6 `% n/ r; }" n" tInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf% X, q( k, M" Y( H# }" ~
五.配置nginx
0 P* h$ \& d5 p; p: {  U% p* }* @% U, ~+ a0 U
在需要启用modsecurity的主机的location下面加入下面两行即可:
% _+ Z* y% x' M: e* z8 }7 r! G' q. q; S; c& F+ y" o
ModSecurityEnabled on;  4 U& l- Z  x/ s' S! I
ModSecurityConfig modsecurity.conf;
) w" u' {; H9 m" W1 D7 q1 d" ~% ?: ?. ?下面是两个示例配置,php虚拟主机:
, n- [* O) Y! S  ^1 i0 n; P8 w* B& C8 w4 {" q' p
server {" G& Q7 F, v: b+ e4 s
      listen      80;' g+ }( L; @( x2 c. u( g
      server_name 52os.net www.52os.net;
' s1 c, P0 g. T0 \     / o1 U$ F& P; o$ w
      location ~ \.php$ {
  B. e  y$ ~& q: O0 z/ E0 J      ModSecurityEnabled on;  
5 I/ T9 O6 I' ~* I. T      ModSecurityConfig modsecurity.conf;
2 |1 `/ B# u% P' X3 L! ~5 p8 a- Z: o3 i
      root /web/wordpress;( C0 R" s  j8 G
      index index.php index.html index.htm;# E3 Y1 l7 @  d. X5 m3 P0 g- S
  " B! b+ [1 t- C+ E- t
      fastcgi_pass   127.0.0.1:9000;/ C/ y4 s- E; E* c
      fastcgi_index  index.php;
4 }& x; e9 Q+ |$ V      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
9 ~) m9 S. A1 m' T' d+ ?/ B8 D      include        fastcgi_params;
# @2 |9 ~6 i4 |& ]1 d# T      }
$ |% ]8 q! w$ i$ r8 ~4 p. v0 ]: D9 y9 Z7 @4 X  }" |, D. J( L' f& H! a6 h; ^
upstream负载均衡:
; H; t9 s2 a, Y% h- D1 h" W- y& _9 I% z5 w- b6 `
upstream 52os.net {
: G) C" k* M* Z1 C3 _4 V* J    server 192.168.1.100:8080;
* U" O4 S, l2 s+ O    server 192.168.1.101:8080 backup;
7 R; ^' Y4 k+ k}- ], |% y6 M" X$ Y) `
8 l5 ^  t* C! j- F  X3 s6 a* ?* I. Y
server {! f7 S! c9 C: [1 B0 z
listen 80;
) @, Q$ i! S9 t% oserver_name 52os.net www.52os.net;
6 L; u4 ~9 y( j# e
( Q6 y3 F+ `" _7 y2 a. I# qlocation / {
% G* \/ f% Z/ N/ ]/ T3 }    ModSecurityEnabled on;  + X) Y6 l) u9 _, D4 a; i7 A
    ModSecurityConfig modsecurity.conf;  
7 C9 F" ]. M  ?$ g7 s
; E/ q/ _% K" N3 Y; c6 M' s        proxy_pass http://online;7 ]7 k! q3 M9 X+ W0 a
        proxy_redirect         off;
) w8 p+ W0 P3 g# I& o8 Q- T: S        proxy_set_header Host $host;) q5 p2 f! }! @2 B; R' ~2 C
        proxy_set_header X-Real-IP $remote_addr;
" c  h$ I3 P, e+ a! p        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
- M+ K8 i% h9 X. n( X( z    }" `! b7 _6 h# F  ^+ k7 j& w; x# G
}
8 K: @( l0 _- m( \% A# s六.测试
+ l7 K) z, B# K' j, F  W+ J* ]- j) M' C
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:/ D' E) @0 ]/ s$ n+ W

$ o6 ]; f( G8 g4 v* }<?php  y7 N0 e# ^. r& p* u$ v# r
    phpinfo();    $ d) f' s4 P+ m  z, D
?>) {$ _) W- c" f
在浏览器中访问:# Q+ W4 R; L7 T' J) B

' v% n' k3 C4 B3 n0 k4 u! Fhttp://www.52os.net/phpinfo.php?id=1 正常显示。$ U) ~& {* u# X2 `' M/ l3 Q
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。1 L& R2 t& C2 P! T" f0 V
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
9 ?6 z' T6 h* s! s! K8 P说明sql注入和xss已经被过滤了
  j  |$ Y% ^0 V8 Z8 F4 I* D
( H- V; m* ], a: ^% V( N* ^9 B0 G七、安装过程中排错
9 d# ]! u6 A# [' A# a1 H3 F! c- P# }9 h! P
1.缺少APXS会报错4 Y- L& x( m" C# q6 Y9 d1 F
' Y/ o% n, P! T1 L  f# o
configure: looking for Apache module support via DSO through APXS
' r2 n# F% V3 p" @* O. m% p5 Uconfigure: error: couldn't find APXS  m- R  s' c/ L' U, D) `( f
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
  b3 p& U1 I9 h: g, s解决方法:! z+ u& {0 L3 i
8 h$ E8 r' q/ \  l! i
yum install httpd-devel) @. j9 N* Q. z( O
2.没有pcre! ?2 l6 n. c8 N: r
; D0 N1 ^7 P/ W% V& n
configure: *** pcre library not found.
* Z4 M/ x" G3 ^+ r: Pconfigure: error: pcre library is required
0 c; Q+ N1 j7 H! w* Z2 A' v解决方法:# C+ d8 ^/ T, e) ?# X& s  r4 ]
' B9 t# Z& ^' V# J* r
yum install pcre pcre-devel
! r) T  a1 C" o; K. k- g1 y3.没有libxml28 k- \' q2 ?' }, E

. L! ]$ c8 d4 S/ u: A& F4 H  F8 Q% N4 }$ E% t0 |; G( w; S
configure: *** xml library not found.% J6 Z' s& n/ n7 P+ U( Y" X
configure: error: libxml2 is required
8 R. i5 W& g, W1 Z6 F/ C' X解决方法:
( r5 Z' L7 L  Q0 H. ^7 S9 M1 n! N/ }( w  S* `2 {
yum install  libxml2 libxml2-devel
( J* |1 `) ], A, T* x, b4.执行 /opt/tengine/sbin/nginx -m 时有警告3 e  \7 B! V  U( N8 U  i) W
3 Q& |( D& Q. a- ]
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
6 p5 u5 [' [) b$ n* y' |+ wnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
; v& s! I6 c  j; y8 [" Q原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
0 l0 |2 t, W1 V/ ~5 g* y
0 y2 [  }4 F5 o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.# a- }2 M, Y; m/ X
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"6 E$ c' y4 _  \' a$ `
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!  ]3 ?# @0 G) t4 c4 u$ U
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"0 G# X) B# }- p6 _5 m+ X( a
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"3 K+ ~/ a/ H0 b% [2 J! Q+ v
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.) L8 D* E7 V( h5 k' r) H9 |
解决方法,移除低版本的APR (1.3.9)2 i, T  l$ U0 g; \" M8 L# Q
9 b! Z% E. ^% W! v
yum remove apr
1 ]* `: ?1 F9 A% U6 {, d  G. o5.Error.log中有: Audit log: Failed to lock global mutex
0 ?+ {& U( H# T" T+ ?* C6 w: U, E/ C
6 V8 S5 y( x; i9 G1 Q! `! r2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     % J; j% x& z0 l% Z. @$ u
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
* {# i8 t  f& k. s9 N+ T! s解决方法:
7 l7 S) e9 n5 B0 h" z+ f编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
* o0 R/ }) F4 s: d7 ]! s
3 ^+ t1 ?" V6 b" ^SecAuditLogDirMode 0777
0 j5 S" J/ R; \- _/ }) o/ |* YSecAuditLogFileMode 05509 P/ S: }2 w$ ]4 f' ^8 {: A7 @
SecAuditLogStorageDir /var/log/modsecurity
9 Z4 L9 c1 c# c6 t! @SecAuditLogType Concurrent
+ m9 u% u4 c9 r' K. a# R" W) ?: P参考文章:) q7 K5 N. I) M4 O, h( Z& Y# r2 I
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX' l8 |* d0 N! Z& C2 o
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-10 23:29 , Processed in 0.261506 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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