找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11903|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。: }6 r5 _7 b; Y% r: k4 o2 Y

6 k# S  B6 Q; J: K0 j  x0 ^一.准备工作. g8 q* H$ q3 n8 x! M+ ?! P# a# d
: |/ K3 z# [, P$ l- |+ n
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
2 Y! g; Z  Q( f" Y2 x5 a! z- u9 M
( R1 ^- r5 r  T; Gtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
; n: \; b: a" i4 C; {$ D, K' S- I+ L8 C& h* K# V% y! M
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz5 G: |) J4 l2 {0 {1 E) ]
# F6 T& K  P, ?: ^3 ]$ U) g' F, b
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs9 i+ s# S4 M9 H# }
& o2 h% l% p! R, a
依赖关系:8 s8 n( y# w6 {6 z1 E& G
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
. n" l# C# e  H8 G7 p, J1 I# \1 U+ y
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel% j! v* `* c& M/ J
modsecurty依赖的包:pcre httpd-devel libxml2 apr
- }/ {6 L" H: t' S  f4 C7 f* D5 J6 C
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
7 @( E& ?( W" W9 Q& g/ n0 r" O二.启用standalone模块并编译7 T# \- h& y. G7 S

- P+ ~  D9 B/ E9 V下载modsecurity for nginx 解压,进入解压后目录执行:
0 a" O# |# c4 H, F1 T$ e* \
" p6 u# b) C, V- U7 Q+ Q./autogen.sh
. l/ l% n# w- Q) U./configure --enable-standalone-module --disable-mlogc" D7 B: f( Y4 d. Q( g3 b, {
make " |6 ~; L6 M1 |& G: T0 ^
三.nginx添加modsecurity模块. F' H$ y7 l% D2 {9 x) k3 u

& s7 W# D' X# o8 t在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
  s& w/ g; J7 t* e5 v4 r
) j7 O6 T1 y! Q6 g2 ~./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
, n- _3 O# N) F6 lmake && make install
3 b. [3 k  ?0 @4 H, D5 V四.添加规则/ q! ]5 v* W! A* e' d

+ Y4 R/ C9 }( f1 b) Mmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
5 u9 S. O; o% h4 ?/ A" h% K2 m0 G. \
1.下载OWASP规则:- @: x7 S2 k1 j) s2 L/ W, m
, G/ E6 L! V& ^3 T! N
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs1 ]5 Y9 i# c" w, W: L  l* y
' V4 V* H: g$ T6 G$ H6 P# {! r
mv owasp-modsecurity-crs /opt/tengine/conf/# O% g" R: U/ H% \4 n! o
0 N  V* R7 G$ |8 R6 F! \
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
) E7 E/ i, @* m  x; V1 [2.启用OWASP规则:
% n( z" i% h7 {& ]
# `# G( C$ j& G复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
( Q. n9 p1 c5 S( f! g# o- g( @" |, w9 t
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
& d" J7 [5 I/ Y- o) `2 _: B
" R5 c4 _1 l. w, a# [1 M+ Kowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。7 t) K+ Q  W$ `5 U

; w( c$ A' G# j  h6 a' YInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
( s( |$ {/ T' T! WInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
7 [- v  z; j( U3 L6 Z# SInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf8 ]5 T+ L0 l- p$ \  D/ {
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
# E+ K* c! i8 L3 Q- d1 @Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
9 }# P% N  f# XInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf- S* t8 o3 O' C, X* \& a; ~
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf5 g1 X( \4 ~& T) Z% h" p6 J" K
五.配置nginx
& k& o* \/ E6 @' Q$ S2 f9 R* t/ ~9 Y
在需要启用modsecurity的主机的location下面加入下面两行即可:/ j* D: u2 @3 u5 n- U, o

$ c* ]% ~! x8 K1 g1 a  W! V7 {ModSecurityEnabled on;  + f5 H: F: e8 y6 F! O6 N
ModSecurityConfig modsecurity.conf;/ ?3 D1 V& m- j" H8 \, w1 I
下面是两个示例配置,php虚拟主机:* ]9 B' x- U, D( B8 O6 K1 x* Y

% o6 O: n9 I+ A. r$ oserver {
7 X+ `, \  r/ K1 K! G7 B) \( O      listen      80;# p# u8 O8 s4 h$ X
      server_name 52os.net www.52os.net;2 v& {8 s6 N+ x' C& b/ g5 m3 m. w
     
! G7 f$ U* Y' d# b6 Z3 Y      location ~ \.php$ {/ W& n: n3 v( Z; _' Y
      ModSecurityEnabled on;  " t% f6 X6 w4 l0 b6 a+ Q1 `
      ModSecurityConfig modsecurity.conf;- [% y$ X. p+ n0 y* }- Q

, y/ Q9 g7 C( C; \  c1 o      root /web/wordpress;: N  E4 ~7 L1 U7 g* G
      index index.php index.html index.htm;
, a$ e* l1 N# a9 _/ ~& v% g4 I  6 _" Q! q9 H$ V8 ]
      fastcgi_pass   127.0.0.1:9000;/ m7 I! X% _1 z) m
      fastcgi_index  index.php;4 T- O! g/ z' {  W3 L! N8 A6 v! `
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;# S, u* d7 o/ m& O! E. {8 j
      include        fastcgi_params;
: \- @0 L* h  V* [& O      }
3 i1 m+ g6 _) D  _: Y+ X  }
% y5 C% l( c# jupstream负载均衡:
# i* Z4 J8 z/ ?1 I
0 j/ q5 O( B7 I. }8 qupstream 52os.net {  O0 m* E1 X5 }# }3 M/ g, X
    server 192.168.1.100:8080;
/ ~6 {1 F' y7 Q3 W* }; H    server 192.168.1.101:8080 backup;
( k" [4 e) ]3 k( S1 o# X2 N( a1 e}% Q  ^3 n" t9 M6 a) m1 h# l  T

$ C% O' W! X5 @1 z* Jserver {
) P2 E6 y# D& ulisten 80;
% Q& t: Q! g8 \: q( d- E! iserver_name 52os.net www.52os.net;
( f9 H$ I% |: L' O! z% L1 i8 u( n" _/ G' y  k
location / {
! {5 \/ A- a, H8 f; e  z5 l    ModSecurityEnabled on;  
. @  l( a; l- w* y) k    ModSecurityConfig modsecurity.conf;  
; g/ B0 N+ ]+ A- Y3 X: @: l
  w* c$ Q" _+ O1 e- J        proxy_pass http://online;& L) L7 [8 B3 y4 P' V
        proxy_redirect         off;
" |  u) z4 H1 _5 f" l7 `2 E1 I        proxy_set_header Host $host;$ u) w# d" b, U
        proxy_set_header X-Real-IP $remote_addr;, A* L/ }+ N! C, _$ k
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
  Y% D6 t: ~/ N0 q9 U    }
' p0 P5 j8 h- `0 i* Z( q}
! r0 ]8 {, ?; ~% d- S& U六.测试
) z' i4 [# F; E4 q8 ]$ T
# n# @* z# z6 \我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:: P+ w* W  l; x" W% M7 f
8 ]/ H" V/ @  Q, b, o
<?php; u0 j7 S$ U- F9 i
    phpinfo();    & [1 L3 _4 _% D: L% X: D6 R& F& S& }; Y
?>
/ i: d4 U/ v& ~" Z0 k9 b: N9 M在浏览器中访问:7 G$ i- k; _3 O

) O4 F/ L- I/ f3 L  fhttp://www.52os.net/phpinfo.php?id=1 正常显示。
: Z, }, o5 J1 j7 R' thttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
( h7 i9 E( G! lhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。8 A' @7 p* a- X
说明sql注入和xss已经被过滤了0 j6 s: U) F5 q$ N* s% l
, V( J, g/ K5 j* u, h2 Y$ E
七、安装过程中排错/ g' f3 f7 S9 H

1 `# }4 n6 Q  O$ ?1.缺少APXS会报错
+ C3 w1 \$ l# J% n* s
2 ]0 Q+ x( R( P; N  p( nconfigure: looking for Apache module support via DSO through APXS. K9 l. O; J# G/ U9 q) l6 O
configure: error: couldn't find APXS
7 C9 p" d/ n. }9 M0 H: @4 X- Eapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
! K1 x8 ]8 z( h% L+ I解决方法:
4 J7 s2 Y4 K5 m
$ _- e% S) j4 F- m8 w; Ryum install httpd-devel5 G) a4 }+ [' P+ {+ }2 ?
2.没有pcre
' [  t/ i' _' [1 G; S0 [- N4 o0 \/ @% c+ ?9 i% _
configure: *** pcre library not found.: r6 U8 K( |# I1 b
configure: error: pcre library is required4 c9 {/ `6 V" w6 h; h5 e9 W) ]
解决方法:
- {7 S: Q4 G2 G' c0 x# S7 A8 \# ~0 S3 t6 j! G7 z5 p
yum install pcre pcre-devel
" x0 ?* u' s8 ^: K6 l. H  M) N3.没有libxml2, R3 |& ^" ^9 F* u; [8 z

0 o) Z1 L9 j) j; S8 W9 F! v
% ^: Y# ?4 h* U. i6 U3 r8 `1 l, ?configure: *** xml library not found.
1 \2 r3 E! b" R; mconfigure: error: libxml2 is required
: T2 j/ V, _0 \5 c解决方法:
; R& L. w, ~! K+ Y4 D9 T! R* W* M1 T1 W5 j0 ~
yum install  libxml2 libxml2-devel
/ T) d( N" Y+ X! c4 z& Q5 Z/ [4.执行 /opt/tengine/sbin/nginx -m 时有警告4 ?8 z7 e: Z. B4 N9 F+ I" y8 _

; N5 M3 }6 p1 k/ J& S  ?5 f9 ]Tengine version: Tengine/2.1.0 (nginx/1.6.2)- u; P  v. T) z3 R: v) M
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
# Z! T0 P5 M% A原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
  p, a& M4 O+ d* K3 q
) q, L2 J; J1 r4 h6 R- K1 c: E# V2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.) K* V$ x' _5 H$ h0 m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9": ~0 A0 ~/ n) b) i
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
0 o( ?+ M+ h+ n+ g) f8 }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"& t% x9 d  q2 U, _8 e6 {
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
. K' n6 \# s4 _/ I- w2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
/ v% V! Z5 z2 L4 G7 K解决方法,移除低版本的APR (1.3.9)
% ?+ r9 {- ?: W7 t/ K  S3 y9 S  h6 C
yum remove apr
1 D5 _6 j; x' X" w$ k+ X5.Error.log中有: Audit log: Failed to lock global mutex' h  D- J5 s5 B9 k4 W& b* ]
6 E& [' s4 }. @5 ?5 u
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     . h: V/ v" p( X% Q, O: G9 c
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]% t$ w, s5 X  ~. D. W
解决方法:
4 _5 R# d7 a" h6 }$ x4 S编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:' m' S8 V& a) B2 h

4 {  P  l5 m. @; ?$ d( H& ]SecAuditLogDirMode 0777- U% U3 s: s  |' j% Z+ z/ r
SecAuditLogFileMode 0550
: s4 u$ F1 v2 C- N$ RSecAuditLogStorageDir /var/log/modsecurity
. |  N/ Q- o" Q, e6 L3 q9 dSecAuditLogType Concurrent6 B3 Z# M9 i) Z/ x4 d2 u7 h
参考文章:, M% _$ W# Q- p1 ^! [+ E) ]
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
, ?3 m* d+ v$ Shttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-24 02:57 , Processed in 0.083463 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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