找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12898|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
8 g* o% f8 C. l1 L; }4 `
4 p8 L" Z; v+ j) R' d" u一.准备工作- a! u3 ]9 x8 @* t; h2 v
: i8 u) d3 V5 W
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
- l2 B( R' z9 ]; u* h& W! }) [7 J/ w7 k' @: J
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
. [+ i0 v5 c0 l% b! F& D* i5 f' w% ]; k/ B! ?# E
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz2 X' v8 e0 l! [8 p7 c7 K7 ?5 z1 T
* ?0 A; B& `1 Q* `( g
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
; J$ ]2 h& J; v5 ^( |7 f- B5 Z3 u( |4 J$ {' p3 v! k3 }
依赖关系:
9 c5 W: A5 {9 v- @- Btengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
. [& ]1 W) B- S% Z  b! |! \- }6 b, @) \, A/ t0 R7 E# I
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel9 q* u! s* N9 p
modsecurty依赖的包:pcre httpd-devel libxml2 apr$ R. d" P8 E8 b8 \! W
/ \5 B1 z: m+ Y% d) q
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
5 J8 P1 e' q4 r6 a9 c# w, W' K& r二.启用standalone模块并编译- o' ]% o' B% e3 V4 ?
& Z8 `' g7 D- d, r! H
下载modsecurity for nginx 解压,进入解压后目录执行:
  `6 n+ m+ _$ ~* t" g6 y. S; U
" S3 S0 U$ u3 |7 r7 U./autogen.sh
/ [/ W. J- Z7 ]6 ^) G7 I1 t./configure --enable-standalone-module --disable-mlogc( {2 G) {. g0 o( ?" \
make / E: P+ p5 K& k' e, G1 v
三.nginx添加modsecurity模块/ o/ r0 _' @# A7 \* |6 V5 ~

2 Z7 [% F' B/ Z5 G& c. {* n在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
4 \) i  t% |9 g  g
  b8 T2 u- B1 j9 [./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine( c" J6 e" M" H  u6 J- ?% s0 `( U
make && make install
# X0 D, C7 A1 I* k+ p7 X  G, Y四.添加规则
: V) g% J( a6 C. B1 h- I  v
- G: w7 ^4 a1 u. \modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
/ b8 a' t7 s% a' D' U- V( ^+ s* W1 n9 X
1.下载OWASP规则:* F+ H: v) U- d& k
- T: n& Q2 h" y  j1 {0 q
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs1 U3 Z' o5 V( D8 I) r
+ K4 z  k9 v, I0 B
mv owasp-modsecurity-crs /opt/tengine/conf/4 {; X% b3 n/ ~" y/ a0 h
! j. X0 c/ Y/ u, v2 l* M; w9 Z. q3 W
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf/ ^; A) E; ~+ J. w2 y
2.启用OWASP规则:
$ S4 C$ q( p0 D) H  ?2 R3 F% Q% R% q  H4 e
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
6 \8 c  P- x$ a
5 v: l" ^  b4 d8 K1 h" _/ C编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
3 F  @$ i# p' }' ~: t. y. M! n( V' c5 t/ }& r2 w9 o- d# |
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。& l3 S" l5 ]4 S5 x& O/ Z) n% h% \
6 _7 N; h* r+ n! _/ k
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
5 h7 Y$ T1 ]& ~Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf9 o, H2 |" r& F( W$ t8 Y4 s
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
% N' b. t7 z; u, R! VInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
& A7 @$ w7 ?5 W) j" g: Q. EInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf3 B) |: w1 [4 X- n
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf5 J" _: X& K+ O! u' A
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf0 j! ^7 _# u9 z2 r3 L6 K
五.配置nginx
! _, y4 V' P+ }# v: @
4 H0 m5 h! w- e4 i: V1 J在需要启用modsecurity的主机的location下面加入下面两行即可:1 j& e- S  h  f  E0 b; P4 f

6 }4 m4 }) r7 c- BModSecurityEnabled on;  2 m# t$ \$ B3 \- N( G; S7 V+ l
ModSecurityConfig modsecurity.conf;, \: k; s9 m5 [7 ?7 e+ }$ s
下面是两个示例配置,php虚拟主机:
; N2 q, K& c: s5 z5 l9 ?: L( s" p+ l- y; @
server {' X% A* S# X) D6 l2 k
      listen      80;
. Z! l, c; O0 Z5 ?& _3 [      server_name 52os.net www.52os.net;
' u% k) p2 `: G7 [! t4 F) ~8 z     
+ ~. p5 N4 k/ `0 `# }( F4 G      location ~ \.php$ {- L0 I- _3 C0 `. k& l7 J
      ModSecurityEnabled on;  0 J; T- k( Y* V5 {! s
      ModSecurityConfig modsecurity.conf;) o4 V) H4 [: S
/ X, r. f3 B4 Z6 z9 [
      root /web/wordpress;
5 E6 _- `9 A: S+ X) v3 c      index index.php index.html index.htm;
6 ~# [  G# s% j- D& d  
9 C. a' F/ f" u, j9 I6 ~9 R      fastcgi_pass   127.0.0.1:9000;4 [" k/ b- R6 ?! E* v
      fastcgi_index  index.php;0 Z  g+ j7 g4 j
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;6 g7 O! O: i8 q4 s: h& m; G% Y6 ^0 Z
      include        fastcgi_params;9 i- T2 J' n" M) @/ {, r6 X, F
      }
8 ^0 h2 a& z& U) Z9 a  }
5 w' p) e: b" l  K/ i+ ]) A9 ^( Uupstream负载均衡:
; p5 B& T0 |$ G$ @; t% R. c3 K% k0 c$ }, |4 t" _
upstream 52os.net {* l! Y! z3 g+ }3 e4 Q/ |% n
    server 192.168.1.100:8080;
( W- @; l( m2 t" D# E0 ]    server 192.168.1.101:8080 backup;
" N, ]% |% [1 S* ^$ w& `) _4 I- I}0 a/ K  q, [/ w

! R8 s# z! H4 d$ w" R; ?, bserver {$ K* c+ d* h6 }; V9 x3 z
listen 80;. P2 [3 O# Q& ~3 g1 k
server_name 52os.net www.52os.net;
7 ]' J% d( _; f' d: Z) ?$ C" E
& H4 S. h* ~0 B) slocation / {) _2 L2 s" F8 \" ]9 o1 |
    ModSecurityEnabled on;  ! c4 `) _/ z$ l, O
    ModSecurityConfig modsecurity.conf;  
! a: `# y) y9 }/ b1 m/ h
/ k! y) N# h2 j9 L        proxy_pass http://online;
7 \6 L0 _0 C$ L; b! c        proxy_redirect         off;
# h+ I/ V. e* ]+ h, S7 F* S% n        proxy_set_header Host $host;- {0 w  _* g! l# y; |' e
        proxy_set_header X-Real-IP $remote_addr;
" l6 e( `9 I0 S. c        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
$ e/ k' I3 J- M' _- d    }% Q! w3 w4 W/ j7 H; X0 n" f
}2 s- \# v: l* ?+ M/ y
六.测试
* D/ s0 r4 }4 }9 Z' z4 V
: }8 h! `2 u8 i; I- ]* p我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:8 Q- C0 X0 m- ]3 s
7 ]; t. m* ~; t  m0 O& J- J% ]: `
<?php. ~9 F" k8 p9 O, ?& r4 _1 S2 L
    phpinfo();    ; D/ R/ D( K; e0 \0 \
?>! @0 j5 I" |+ A
在浏览器中访问:# q) D- ]+ j1 v7 V

0 {# @; t$ |5 Whttp://www.52os.net/phpinfo.php?id=1 正常显示。
: Z3 h5 E1 L4 `0 m) P) k0 }http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
8 m3 z( q( S5 P1 Zhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
9 N4 T( D7 a9 {9 ^7 \说明sql注入和xss已经被过滤了
, r( E2 X' J( }  o: ^% E- u) d3 ~
0 e, u' k. o1 @! y七、安装过程中排错( V# z+ V( ]0 W) ]6 i( ]/ d1 n
: z) t) {7 q- F% H, [
1.缺少APXS会报错
/ T+ @2 {" d1 t4 q$ h6 {' k" p* v) l3 D* j7 |$ u' V( u+ E  d
configure: looking for Apache module support via DSO through APXS( _% t+ {7 o1 `9 i* J* I) L
configure: error: couldn't find APXS
, b& I$ B1 H. M) D8 Y7 Yapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。* }; p& w+ k" q' z. C. J) _
解决方法:" K+ A9 N1 E3 T! L9 U) U
+ p2 @2 |7 [$ B- g, n: T
yum install httpd-devel
6 Z5 t. ^/ L: e0 [3 r- V8 H2.没有pcre
( [3 v7 ?. o) L8 e  ?5 ?
6 `* Z( A: }8 Z3 W  Z$ Oconfigure: *** pcre library not found.
. m& d6 I+ e% sconfigure: error: pcre library is required
6 r" s' \! t: j& A8 p7 \解决方法:8 g/ Z4 `/ T* Z, s0 H! C6 [$ |) N4 j
" N7 q, ^, r' V; M2 Z
yum install pcre pcre-devel
9 j/ a) I& t& v2 n" p1 j; J1 g% R* |3.没有libxml2, R4 m- E! S, V; x- R6 Z/ B

# J, O; D, o' {8 C) P  R
' u1 a1 |: ?  t$ x; X6 Gconfigure: *** xml library not found., U% ], J& ?2 `  f* Q1 h; J
configure: error: libxml2 is required' X% ~% g1 b, N8 ?
解决方法:
4 K5 C9 _* V* S5 Y' V
/ h5 N# r# s* j/ G! v5 |yum install  libxml2 libxml2-devel
2 U7 |# S0 X5 L0 F: H4.执行 /opt/tengine/sbin/nginx -m 时有警告, O9 c  \& g" Q2 y
9 p  i( R6 ^- q
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
  _! A- G& p( {7 vnginx: [warn] ModSecurity: Loaded APR do not match with compiled!/ a8 N, X5 p8 j9 S* E# }( c# s
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
, h  J$ g" o% L9 {( o1 n8 q% L$ S
, t( d8 y5 ~# k4 |2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
$ l( B  _( |+ }9 ^$ c) x2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"- p+ q, {6 s! z3 v
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
6 ^0 I! p" y" }9 P3 [1 {% v2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"- a" W* {, L4 t# q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
& n3 B' l+ d- ~( C1 V2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.9 e5 X% A) s/ `' e3 H8 J( Z
解决方法,移除低版本的APR (1.3.9)" D, ^8 _/ {/ z" z: c' v6 B9 b
2 i, s$ o& ~! G% D+ q
yum remove apr% v6 J  f& k1 ?- m/ N
5.Error.log中有: Audit log: Failed to lock global mutex) f& \% ~; M1 M- m' S5 a; G& Z0 P
; A7 B+ J% C! s6 e% N8 u0 q8 K6 O
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
2 d3 O0 \5 z8 L  o8 X  s; F0 Tglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
  H, N% E# B! s解决方法:
; {( W# y6 ]/ S编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:' l' {+ Q/ v! e/ I3 o! K$ K3 n& Q

, N; f( K3 f5 zSecAuditLogDirMode 0777
7 V0 d% ]7 e8 R! ^/ @1 ZSecAuditLogFileMode 0550+ S9 V8 N$ t+ D9 r% g6 H: U2 \
SecAuditLogStorageDir /var/log/modsecurity& J1 j7 }# R8 o5 Z$ Y2 k
SecAuditLogType Concurrent
; y. W6 [  h' Q" n  c参考文章:4 u3 Z- l4 ~1 ~5 H- L
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX( u7 o: g- U6 A0 K: X
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-24 07:15 , Processed in 0.067879 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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