找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11184|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
; L1 \9 h) u: I& Z( M! s* g/ R% j: ^: f" o. X
一.准备工作
. g) n6 {; {, A' r. S) k9 V
- w5 x( X" k9 _. w1 N系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.00 f1 T1 ]- p  j4 W

$ c9 l! X4 c4 t4 U8 ytengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
% J8 @/ x: d" X- D! @# c: m' u, c5 ?
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz. l3 u3 a4 b4 U* V* z

# Q6 u2 o8 l) T2 U7 ]4 M. A7 S6 y) }OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
- y! U0 V5 {% m) g; g% h% e+ l: \; }  x% D8 L5 y, W) E4 A
依赖关系:
) o4 R- A2 ?0 i* \$ n- w; m5 htengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
/ F, }0 m/ J) m& J
, k; ~" G2 m+ g/ Ryum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
5 t( _$ ^; }! b( ?3 j0 b1 Jmodsecurty依赖的包:pcre httpd-devel libxml2 apr
4 G- W" G( J2 ~) A( H" R+ l2 g9 ^8 E8 ]$ W$ }
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
- w8 N; G* u" p7 t* L二.启用standalone模块并编译
# j( b+ j, A* ~+ m- Y+ K0 A0 d
9 B  A' L9 |8 y下载modsecurity for nginx 解压,进入解压后目录执行:4 U/ ?5 {( }4 d( W0 d

1 x( q& V) ~, j8 \1 I. f./autogen.sh
/ b7 e( T& C# [/ v" Z, L./configure --enable-standalone-module --disable-mlogc
7 g3 _) o& N$ ~1 d+ ^, ymake
% ]8 d* Y6 D7 \" o8 ~* T, C8 e, N三.nginx添加modsecurity模块' L9 U6 C3 a. y
) D* z/ s+ j- t4 D
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:9 }% s! W/ N! J7 q1 c
" q1 {+ C: w( P& R. h) }
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
# f9 g3 c- j1 [+ [make && make install1 q0 t& a  _2 Z! W
四.添加规则
  T/ I) a9 ~+ g/ p8 q- W" n) `3 T' p* n  l$ K0 b
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
( e4 N+ \% L& N( S& A1 M$ |2 M
" P# x5 h$ I4 y+ T1.下载OWASP规则:
9 {- o: J3 s& G4 _* p: d) f% k% U( J5 g4 |, z. m
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
6 T2 R8 y2 ?. r$ U$ \
/ b) ?) j7 A$ R) Imv owasp-modsecurity-crs /opt/tengine/conf/
* g. k2 S, ?% |' Z. \  K
/ r4 E2 t4 j  J! L- xcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf, z) a# n+ K0 f1 |
2.启用OWASP规则:
; [, @0 C8 ^$ H. F+ H
* ]# ~7 Q& K( U; v0 X+ s; S$ X% L/ ^复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
! n' v1 b3 Z* Q, J8 K! E6 v% f% P7 h6 Z* i
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
. `% ^, U# i5 C/ L3 D0 i" W' }* U
. h. h5 F' q. k$ E! ^; ^6 zowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
! C! g$ o; X9 `3 c; R/ ?( I3 X
+ Z( g# S/ P3 X# H8 mInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
" ~) V: [7 V9 l: E$ `Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
. I/ [7 e6 D9 N4 W$ p0 S( Y5 A; EInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf5 A9 |$ x" H& z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf$ s8 t3 Z* ]6 I$ j* J; A! O
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
  w& h; O0 @4 P& E* w+ _Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
: Y5 r2 v$ r! B/ v8 qInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
0 Y: g5 D) T1 X$ s# F五.配置nginx
; K6 {! v6 j0 {/ y( E3 g; D, D5 A3 ]+ @9 ~
在需要启用modsecurity的主机的location下面加入下面两行即可:& P- D+ Y; X, n1 X
' Y: O& c" y6 I5 ]
ModSecurityEnabled on;  2 R/ Z3 G& X% {! t5 V' u, W
ModSecurityConfig modsecurity.conf;* n+ s7 l! j# x" r) `% h
下面是两个示例配置,php虚拟主机:
2 w8 R3 |1 f1 D+ E7 M9 _' g
2 Y' t0 @4 q% d# F8 I$ }0 Z! B) cserver {
: S. D: |' Z7 u( r4 r" G      listen      80;8 V" U2 P; u& J$ m! O1 c* P( L3 m
      server_name 52os.net www.52os.net;
3 n1 F3 Q7 @8 z0 x# d0 \( n     
6 k, U* c9 b; m% T7 [      location ~ \.php$ {
# g7 k$ [& J1 }6 s0 ^      ModSecurityEnabled on;  3 g+ ^# F7 h! v
      ModSecurityConfig modsecurity.conf;* U3 Q. s: r6 E( K  l- r

# Y$ N' l4 d; W% q      root /web/wordpress;+ f2 c6 \; ~7 X% w! S. G5 r
      index index.php index.html index.htm;
3 p+ @7 l2 r% |& i0 I, ~  ) e: ]$ e  U* l5 F9 `
      fastcgi_pass   127.0.0.1:9000;
% _* H! c& n' ~( j- w      fastcgi_index  index.php;
  _% P! V9 K7 K+ z      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;9 V  L+ a2 u$ M3 B5 g
      include        fastcgi_params;
+ n" x3 `0 p+ W      }
4 L2 J- P: N6 n2 g  }* S0 V! J& D- S4 B
upstream负载均衡:1 e$ C1 \/ o5 t- G; k8 f
' S. y+ F, T" {3 |9 @0 x
upstream 52os.net {+ Y5 Z  I1 v$ o/ M% m
    server 192.168.1.100:8080;0 T/ ]5 N3 J9 j7 G
    server 192.168.1.101:8080 backup;2 M- H# C+ E  A2 y
}$ x$ L4 n$ y$ _' m

4 ~3 }6 E8 C6 ]server {
8 g7 b2 V3 K9 h/ X6 ^listen 80;
2 T' J% d8 O5 v5 b! D  N9 Tserver_name 52os.net www.52os.net;
* ~4 m3 l% J1 i# E# N- E) B! `) L6 r, D
6 K4 _' T" k" J# q: W4 D$ u: plocation / {" Y" U" r( m2 T$ g* v5 f8 v! J
    ModSecurityEnabled on;  , \7 h7 t) E9 l4 `, c8 Q9 c% e
    ModSecurityConfig modsecurity.conf;  3 W, f7 `  u4 W: Z1 ^7 I  }
" t) t7 B& T5 e6 ?
        proxy_pass http://online;$ ^. P1 @1 }5 Z# s
        proxy_redirect         off;
+ M2 N; j7 `8 b6 t- O        proxy_set_header Host $host;' Q6 [  U. w$ Q+ }! Q' H( `
        proxy_set_header X-Real-IP $remote_addr;
! v& `( E/ q( \  Z) k) b        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
, n  A& h3 H, ^: F# h1 a- k+ e    }/ f0 W6 z9 y. b3 h# Q
}
0 ~. O' U+ M2 h+ t六.测试! _5 q6 ?  D2 @

& y2 \% {6 d3 m. p2 {我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
1 c8 K- }( G5 i1 Q( c/ f7 w8 |# S8 c- t6 W  _, I, t5 P( G
<?php
2 P5 {# o* Z$ Y4 K" u" q    phpinfo();    9 [1 {/ G2 v) [& n
?>( w; j& t9 e8 Q( e. a, H
在浏览器中访问:. \, G4 T6 V/ u+ F: }9 e

$ }. _$ L: |/ R, B' Xhttp://www.52os.net/phpinfo.php?id=1 正常显示。8 b, X* a. H; s: Q4 J  ]8 h& X
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
% k" y' K; H& G9 khttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
6 j7 ^4 _/ s* G2 }! R说明sql注入和xss已经被过滤了
7 g+ W& f1 L1 `. L0 p) `) r9 l2 O5 [& y# P# Y% N5 J7 Z" s$ e
七、安装过程中排错
9 b: h" S/ }( G( o! X7 {8 P! t! a! d8 X$ w2 D: y
1.缺少APXS会报错
# K, }2 @/ E+ y7 p" B1 B* b3 o4 Q: @- r) a
configure: looking for Apache module support via DSO through APXS
" x: h  G) ~. yconfigure: error: couldn't find APXS. {- U& q0 E1 A5 X
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
9 h& s) ]+ t- a" r0 X1 @3 f* H解决方法:
" T# j" i# A) |' ?' `9 k4 B- M
- ^) B% f+ e) |8 F+ i( {5 ^yum install httpd-devel* O$ p) [# K/ j, v# }3 `
2.没有pcre2 l: `! [1 ?9 d3 C  r. |+ O/ ~
0 @. X; O, w5 v4 d
configure: *** pcre library not found.8 _- b' @. w5 Y1 Z
configure: error: pcre library is required
* _5 @5 D: K* F2 M0 k解决方法:
: |' f: A% q9 l1 w  ^+ o
& R& a) c7 `7 c' v9 Ayum install pcre pcre-devel
6 n; n! ^4 D0 _$ X$ C3.没有libxml2
. C$ ]2 n2 s( D8 h4 i: v9 k# H8 D7 C7 y0 U, ~

4 F4 m: _. s' P5 `# }& }- _0 Qconfigure: *** xml library not found.
! B0 b1 u# j& y- i$ o/ D' ~0 M4 a& Hconfigure: error: libxml2 is required
) F' n# r7 n3 \* |) T1 `解决方法:% L9 A0 C, k: K8 ?: n& |) F

, ]" V# J! L+ A2 Yyum install  libxml2 libxml2-devel2 ~/ b7 v% k, S5 v
4.执行 /opt/tengine/sbin/nginx -m 时有警告; @9 r( w. }$ d0 v" @$ ?3 M

* c0 j. T. A# R9 qTengine version: Tengine/2.1.0 (nginx/1.6.2)) E( F, M' b+ e" d" }& A
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!. O! B( ~  E5 Q5 z
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log! ^! X5 m( |4 }: X7 [' j
5 s5 a, u% ?- k* u5 j  \8 C9 i2 J
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
# K& x! o7 ]3 X" V$ k1 J# h3 Q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"4 [% O1 Y' ?( A; T7 d5 h
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
- S/ g: I5 N# f: y; W, S2 ~  h8 L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"8 U0 J* w; _! n7 G9 f) r3 v. u3 E
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"  e: i4 Q4 ~# d; d4 ^8 O3 p
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.. P6 K: X1 u- U% p
解决方法,移除低版本的APR (1.3.9)% p/ t/ Z7 i6 J$ ]6 @

% m# R  N  `* G; O' }. F. J$ oyum remove apr2 ]  q* H4 o5 N7 n+ [& a
5.Error.log中有: Audit log: Failed to lock global mutex
5 o3 k) v7 @& T: \: w6 Q5 @( j: [) |7 y% p- c
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
5 _2 c5 D! D- b" X0 q5 c' Pglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
" m! z, y& \$ Y解决方法:
6 [3 i% h6 A/ y% J, \编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
! {! z" @- Q& ]1 \1 f
9 f1 F( U2 H! V  {; m( e5 s) tSecAuditLogDirMode 07778 e% y. U) F' g& }% Q
SecAuditLogFileMode 0550* @3 R, A1 ~5 ]' _+ G- E) C1 z
SecAuditLogStorageDir /var/log/modsecurity
) S& K/ x5 o8 M6 s6 YSecAuditLogType Concurrent% ~4 K5 L# g( ~( V' }
参考文章:0 V% i/ K& I5 d: G
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX& P9 o1 Z' u1 l' \: X7 _5 z5 P0 P
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-5-27 22:31 , Processed in 0.093090 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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