找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13360|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
6 ?# A, H" R- ^% B. [% W0 y- R0 z* h$ l0 G. j. S
一.准备工作
+ J/ n  u- l0 W- |4 X( A) T' p- y  M' @
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0! e7 V; d& ]: g2 T0 Y6 E

% n6 ?' d- @, P! |) b5 w7 Wtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
2 U. c8 @' m4 K7 Q! F
/ r0 V$ b4 s: |2 u8 S, Fmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz$ I# M& Y/ f4 T! Y2 D4 H4 x

" U" H+ V2 W3 M6 R! @8 tOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs. ]  |( h* z. C  Q* a7 ~. P

) l! `+ _+ _* C; c4 Y0 o7 W依赖关系:- J' t8 |/ ]4 q' L' E
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:. C% J& ?  z7 `3 L5 g6 j9 c

- ^9 ^: \4 p% O/ J2 c9 Pyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
  P/ C$ P1 h8 o2 \3 Gmodsecurty依赖的包:pcre httpd-devel libxml2 apr+ m$ ~& M- L6 [$ O8 D

7 [) `- {4 b; Z  j( n/ oyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
4 U: `6 f6 I& B; v! t二.启用standalone模块并编译
, i3 c0 z/ F3 L5 U1 M+ R4 l" _+ K- W
- r& l+ X4 s; w* l. X" ]3 C, }! A下载modsecurity for nginx 解压,进入解压后目录执行:
) V1 k, Z: L% |4 ^) H: P" d8 D5 V0 N: |6 R. t3 a
./autogen.sh) S+ [- M* x% `' E% p0 d2 V
./configure --enable-standalone-module --disable-mlogc3 L. [9 C1 H7 n/ i6 L2 U
make 2 N9 p/ S. _2 v) T
三.nginx添加modsecurity模块4 r- \- ?7 L, Q% i
" L, K% A& O" W6 R4 g) c; r. U
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
' K# F; I( R: E" @2 O+ ?: p$ D) t0 a
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
( {+ l. T# G6 k4 P& @' xmake && make install0 t4 d; a: [2 S2 X) R2 ^# \8 w
四.添加规则; u( |' S1 k. X  k0 }
; T. b8 j; j3 [
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
! K4 L; b- H' o
2 s3 h( A: v; s0 O4 ]1.下载OWASP规则:: ^* N) k4 ~$ Y' M8 b

- S" w7 n' q$ l1 z$ {% d; Ngit clone https://github.com/SpiderLabs/owasp-modsecurity-crs8 [8 g" a/ q9 F  {
/ ^7 \/ Q7 o* p8 q3 b" _$ A* U( I
mv owasp-modsecurity-crs /opt/tengine/conf/1 e+ ~7 o# P/ A

- y# C" d- I3 m7 ?$ u  ccd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf" B6 t0 ]" z: [8 i  N* e
2.启用OWASP规则:2 f9 g2 H+ D+ ^7 c# U0 \
$ z  D: e0 x& M* L. e. ^
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。4 q5 H8 i* R5 k1 O
! x. a" @5 ]: C
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
/ P5 p0 h3 w" q5 T( f. w7 Y0 o
& H$ ~+ }6 m6 Y) Q7 E5 d& Fowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
5 A! v) b: d( Z* f/ s6 G' v
8 ~% s9 W" A! @* VInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf" A% l7 I( N! R. ], I
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
9 `0 w; O6 ?! SInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
" D% p3 I1 ^  k9 k6 J/ }1 j# SInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf) s, r1 Y  i1 S6 x3 r9 B
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf! ]. u# q/ h& X; t
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
7 {  f  _& U: m% p- `Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf" m# u  D# U' d  V% e5 Y
五.配置nginx
9 Q9 B% u( j+ _. L' @- {+ ~& a* y  {6 t& J
在需要启用modsecurity的主机的location下面加入下面两行即可:
  }4 u1 Y! d0 |' y" C
1 S' K: A7 ]) d9 I# C' GModSecurityEnabled on;  2 X. a; g0 x1 ]) s) e: x
ModSecurityConfig modsecurity.conf;. t; ~$ @* j( E& S- b1 I' X6 o
下面是两个示例配置,php虚拟主机:
, E4 _8 n1 R4 D8 w( q
: Y1 q5 [) W5 Kserver {
# D# u8 H3 _# I5 q7 J' Q      listen      80;
1 k5 b# E- F  G; v- j      server_name 52os.net www.52os.net;' [" Z+ Y- Y% |+ B2 O
     2 O$ T( n  A2 B2 N6 o3 }& D
      location ~ \.php$ {
; B9 U; y# w$ r+ ~9 F      ModSecurityEnabled on;  
" L' M; ^, @8 c, N5 y9 b1 ^      ModSecurityConfig modsecurity.conf;
. U" v  {) p2 z; \. `
7 V+ u% x- k, S% h      root /web/wordpress;
6 Q" O9 d1 h' Y1 c- ]) w      index index.php index.html index.htm;. I9 O  {( _  g" @: U, p, a* @4 s
  : |; i  a  Q8 ~; z9 R' h
      fastcgi_pass   127.0.0.1:9000;
- e; I- L, P! J$ G      fastcgi_index  index.php;9 Y* N8 q7 s" j  s- ~( w' X
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;( y# s$ g# Q% Q
      include        fastcgi_params;& r. J1 l! ]2 q
      }
3 i3 P7 c- ~4 m2 }7 @% S  }$ ^9 o; H- d8 q& `- N: d
upstream负载均衡:0 M; H! p9 T7 z2 f1 p
/ g; n; W4 K7 T( f
upstream 52os.net {
, M" G: R1 l. O' u    server 192.168.1.100:8080;
# w* _3 k9 l/ p  {    server 192.168.1.101:8080 backup;; b3 u6 o) o6 D& d* v
}% J7 I9 g/ ?& O8 X. g5 _
; W5 l' U7 e2 V7 Y- l9 G# b5 K
server {) M6 o" S$ @# J
listen 80;! @: a; e4 b+ R: v0 C
server_name 52os.net www.52os.net;; p. x& _: t( E, \1 p

- p2 H+ K" r5 _4 _6 D. Ulocation / {
- f: a' @' Q1 l' P! x6 D    ModSecurityEnabled on;  9 a& z. p4 ]. h0 _! j6 o
    ModSecurityConfig modsecurity.conf;  
: }$ p2 A. O3 @  h& U
: A8 O" |0 D/ u7 U/ o2 h/ h3 j/ n* f        proxy_pass http://online;$ e' U8 ~: C' B4 y1 c
        proxy_redirect         off;
# n9 o  d' k* Z! V9 O+ g0 Q# R        proxy_set_header Host $host;
+ N' K/ o/ B  ?8 b' d        proxy_set_header X-Real-IP $remote_addr;
1 s" g) W* n2 b2 I; @7 [# z        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;! h9 ?( M1 m! y- o
    }
9 Z& k# K! E- |& `6 z}
+ w! [1 ^: H9 l9 Y  ^: j, Q2 u六.测试) s( S! p# X+ _( s% y; n5 n
5 G; w, m" U, v$ |7 j% _
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:, @* i7 g, ?0 U3 N7 B4 c
, z1 w: ]3 B+ r$ M
<?php
; c. `5 Q( @( j2 {; Z: y    phpinfo();   
* @) [( [: W4 ]8 a/ C?>, D4 T! l. {' k2 u" f
在浏览器中访问:( c9 u5 q  J4 K1 p% y

# s. I8 f( A" J$ b* ?' _' C8 A1 Jhttp://www.52os.net/phpinfo.php?id=1 正常显示。1 r9 k+ t, g& x
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
* t( u& X! N( p& d: N8 ~http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。! T: I# {" ^8 Y0 K
说明sql注入和xss已经被过滤了( F5 K6 j( d/ v& h

  r/ {( G+ M9 E9 b1 P" q- k七、安装过程中排错2 O" h3 B' j, e* F+ X- C
+ Y: x) _' N2 u; {* Q
1.缺少APXS会报错4 n9 Q* F; Z* T: ~+ c5 l6 j

: L% g4 S8 M+ d% Lconfigure: looking for Apache module support via DSO through APXS
5 V0 T! B! q* Xconfigure: error: couldn't find APXS( [4 G  Q# h+ v% D4 U
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。8 r9 f! i; _/ D# I: T; g* `7 f
解决方法:1 g; m3 a. \$ s
7 F. T* L% b3 K
yum install httpd-devel
: j, o. e% q* j  H, u$ U! `2.没有pcre
( t' ~; ~* ?8 o5 C7 t1 g' o2 ?- s: R* n
configure: *** pcre library not found.
/ P2 q; c+ K7 B) y4 a/ l6 ?1 Fconfigure: error: pcre library is required
/ s9 k  ]! _, n解决方法:7 h* I2 E$ c. u* n+ R& V6 p
1 e" h" r8 }! [: o( V
yum install pcre pcre-devel& x$ s& N( \$ P% m  M( I7 `) x
3.没有libxml2" d% U) U8 o0 I- K. y# ]9 ]
; }1 f) s0 q& P5 F% R% l
  s2 I/ a( o. `/ j# a$ V
configure: *** xml library not found.$ F2 x' D! D/ c6 V6 n4 [. a2 q
configure: error: libxml2 is required
( T6 a& S  c9 E* p解决方法:& Q, q. D9 Q4 w' F
. r0 v6 ]( ~4 ~* V6 o
yum install  libxml2 libxml2-devel
6 x, Y! X% w! }# y4 c0 w% w) Z+ ^4.执行 /opt/tengine/sbin/nginx -m 时有警告; D: Z% E. c/ @% o
( t4 g0 A- e$ F3 c2 H) F' h9 Q$ E0 T+ y
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
* q, R$ h0 l1 r, |8 D2 @nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
( P7 g. [: `8 m( N) a6 z. {原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
; C& D5 {% \! v! [3 W2 E+ o; q0 n  z3 e; Q- ~: _& H
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.  Q# d4 g  G; q8 I  h( V6 t/ N# @% v# o$ u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
. v1 K' i! Q8 q# `2 O+ z4 E; |2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
" H4 B7 ]$ N1 G  \/ }3 N& D2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
% G, R) y( A' ^  b2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"1 ~+ R+ A* f; [7 Z2 Y
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.9 a! E: U, i( H4 F6 s( B
解决方法,移除低版本的APR (1.3.9)
/ i: J% _" b" u0 J2 h, ~# j
! J: s# P8 y% K  \! x/ Syum remove apr
' T! X" M/ E0 H) L5.Error.log中有: Audit log: Failed to lock global mutex
1 `. f) ]2 n2 j. q) b" _( a
5 }( D4 q+ ^% f8 d8 o  a2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
$ Z9 f. ]; G: m1 p& u* V% Zglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
( v5 t1 E: {" a/ ]2 }5 f解决方法:- v6 E* `, ]9 G# E8 f5 `0 i
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
. I' P9 v8 v, Y) Y
& Y1 n5 s5 E# b2 v6 _, p% ySecAuditLogDirMode 07776 i7 C2 r3 L0 x7 z3 C+ k: q
SecAuditLogFileMode 0550( U% s' I+ I, _6 ^- j+ m. p
SecAuditLogStorageDir /var/log/modsecurity6 ?- L+ ]2 k1 ^% q8 x. d  N+ Y% ^
SecAuditLogType Concurrent
  z3 ?% R3 O  ~6 e5 [8 ?. }2 b/ i3 V参考文章:
3 W  D/ H( B, Ehttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX2 w8 i  x8 f, F" g
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-12-6 18:07 , Processed in 0.068032 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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