找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13040|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
  a" p3 Q( ?: n, m
' \6 t+ |4 a9 {9 W* L" ?一.准备工作) Q3 x2 g9 J' Z4 F% ^9 w  P* Z

$ i1 V) f7 |- T" y" x4 E; @8 p系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
" C+ y9 o% k7 I6 ]) a4 L) e, u
# O3 W  g0 W7 w, s6 Ftengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
0 g# {* C. ~5 Z, c5 `/ Q, y' R
1 \4 v8 e0 m3 c9 G, g" rmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz( _1 S- \" K% @( n

0 j4 p6 M# e( g" m: M* L; \4 D( _OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
/ ?8 ^3 ~% r" {$ m! I( R1 Z0 a8 m
% u& l) q" _1 A( d5 F- p- x8 }- a依赖关系:
/ ?! i+ h2 Z9 c7 S+ V! e" [tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:8 ^' J3 ~- K+ r- G+ M

/ u1 W' s( G1 }2 V  ?& Byum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel' Y  H2 Y( f1 T9 p# S% _3 ~1 Q
modsecurty依赖的包:pcre httpd-devel libxml2 apr# U1 ~9 b8 Y# E. p2 G5 ^9 ^
' _8 a: l) ]/ E
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
7 l1 q' a% g  Z$ W二.启用standalone模块并编译
& l9 n$ b- g' \' U7 }" A0 y5 D) h. I) e" U' @, X% Y. \
下载modsecurity for nginx 解压,进入解压后目录执行:% W( h" K- P  B4 S( U0 y. g

! w" p+ r. w$ G./autogen.sh& d6 N2 A& ?' N4 M* N# I) u+ _
./configure --enable-standalone-module --disable-mlogc1 q$ z0 @  i: v) B) N' x
make 6 X2 ^* B6 b  u% }$ c- }
三.nginx添加modsecurity模块2 y; v2 Q# v' B/ p5 T. h# Z* u4 k

% ?# f8 B- a- ~% Q( s在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:" P/ o0 p/ g! f" W. C

7 d, O& u- @$ N$ j- D4 l: g' U./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
) O/ ~* i  s0 X9 pmake && make install
8 p. m) b0 ~- b  p/ {( s4 g2 s四.添加规则4 i# C5 y! E: d) H" ]# m1 R3 s& M0 |

( x6 _7 L6 |, X, H* x" a, \/ ymodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
; s5 |1 S0 N* B$ P# P7 K( {0 i& g$ V% _* @: z5 N
1.下载OWASP规则:
/ q& Y2 T4 p* m" G0 m& n( d
0 O. k, J' c  m+ ?  x' ~- ?git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
7 y' ~! B7 n$ }
$ _0 O) K7 P) ?0 L( g! tmv owasp-modsecurity-crs /opt/tengine/conf/- @+ j2 ^/ R# @2 M" t% R0 l+ y

: l% P+ W! L  a! Z( I6 Icd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf5 V3 P$ F( G# Q, E3 T- [
2.启用OWASP规则:/ D! O6 I" |# i1 n! u( O

5 T# _& G7 u# f# f* l5 A复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。+ [% k- v! V& u7 s* n1 y
. O3 s9 m: |( A! Q( o& t
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on! T4 T# @! @2 g2 R$ g
2 n. L" Z3 Q: |" C3 t1 I! w7 O
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
: N8 L' [% C+ G3 h- ~+ w$ Q) f4 |, |% p8 M
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf2 A4 W/ _8 U  k* j
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
5 `2 P. n; J2 Z, ^$ C' Y# qInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
5 t- v/ f+ [0 M+ RInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
' F* V3 _& u1 m6 a' E* GInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
+ u: b+ `1 f. {* f0 P+ ]2 B4 XInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf- s" N, s# X- Y& z" w5 S) l
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
8 \* f) Y$ z' I3 U+ s五.配置nginx, T: \9 E3 c' ?& B( M

; Q( ~2 K% }: M在需要启用modsecurity的主机的location下面加入下面两行即可:
4 b4 l8 X  p4 f% ?( h0 F5 P$ Z5 q+ t9 Z8 {, c+ T' M) o
ModSecurityEnabled on;  
' x" ]8 |/ F# h' {* O$ KModSecurityConfig modsecurity.conf;1 J5 a' F8 t: ?" G* o
下面是两个示例配置,php虚拟主机:0 `# w; i# e  Q4 x. J, O% I3 X: `
- C2 o4 Y6 |' [4 y0 n# H3 G: a
server {
7 ?: Z" z9 i6 n* \/ o. c      listen      80;" Q+ J3 T' Q8 O+ M6 W% ?
      server_name 52os.net www.52os.net;% P; x7 M/ e' z9 A7 {2 V
     
) H4 N+ V% a: q& e3 t1 a      location ~ \.php$ {
. i( G. W7 t3 x) E- O2 [      ModSecurityEnabled on;  / e' ?4 P9 ~8 V- Q
      ModSecurityConfig modsecurity.conf;8 M7 x" `; |- ~  P/ @

3 f+ v7 W4 K% k6 I) c5 _7 m* `      root /web/wordpress;
: W# J9 C- q  l  ]3 t' [9 B2 Y      index index.php index.html index.htm;5 d+ G% M; o6 O* s
  
+ C. F2 i: v7 w& x# `/ u      fastcgi_pass   127.0.0.1:9000;
" j$ T. h  @$ l. Y' _! H      fastcgi_index  index.php;& f2 n/ H4 J2 X* y! T
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
: n5 x9 D6 a9 X, N* r$ z* b      include        fastcgi_params;# |0 u( x- U9 H' H5 a# s
      }( R% ?' A! T: }* K  r/ ^1 N
  }
- I/ z. |8 F4 W& W+ p# uupstream负载均衡:
4 N; k) U' k' Q0 n% D* D8 u# U" _3 l) n# E6 m3 `
upstream 52os.net {9 h' g& [+ v/ K% z) x
    server 192.168.1.100:8080;5 J3 ~2 r4 Y1 y
    server 192.168.1.101:8080 backup;* W# w6 s+ H6 _' F5 d
}: e8 O2 O/ O1 O; f) B6 y
0 f; X. H( i/ n/ Z7 l- c
server {
2 N% V2 t2 l/ I! O1 w; @listen 80;6 y1 U+ t! n5 B$ y$ x! n* B
server_name 52os.net www.52os.net;
7 i- z! Y1 S2 n0 @4 K
4 C  H0 ?9 U) x, c5 H) v" llocation / {$ O/ j  \+ m4 _/ h; c" z* j3 I5 ?
    ModSecurityEnabled on;  
+ a) v; e8 T6 N" G    ModSecurityConfig modsecurity.conf;  , ]+ Y1 N& W1 n4 R6 G

0 b8 k! }3 c& ^- f  S! L9 ?8 \        proxy_pass http://online;% q( t: s2 e8 T2 S  F4 m7 e1 ^. W
        proxy_redirect         off;" `8 P4 N0 n0 r  Y7 o: `
        proxy_set_header Host $host;4 C% d5 Y: q, h5 B7 `7 z  b" C
        proxy_set_header X-Real-IP $remote_addr;$ e) `  D! \9 R  n4 z0 j+ _" D$ U
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
, J$ k7 c/ s9 M) Y/ ]: C9 j+ Z0 O    }0 d! G4 y: _% }& ?
}
. ]; B* y2 K8 r0 s( K六.测试
8 Y1 A4 k6 _/ s* ?
3 r1 u0 a' `; S我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:9 \; _6 S( @+ l3 R- _$ T% [1 L  l# P1 p

! ~9 Y5 G+ L7 Q; k! I- T" t<?php
/ I* c, D+ w7 `8 \    phpinfo();    ; u: D4 f# {0 _  A: r# ]
?>
# G9 d% @! @+ A7 L4 [在浏览器中访问:
( w& m7 g0 S2 Y) T5 o. c' G1 a
7 K6 m% g/ ]8 ~http://www.52os.net/phpinfo.php?id=1 正常显示。1 C& s- {6 c' M/ C3 L: @
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
/ {6 h0 [% G. f# S# b3 u/ Phttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。6 U0 Y: c/ d$ B+ U2 q6 c, t# M
说明sql注入和xss已经被过滤了4 \2 a' j9 M; H" \3 ^0 n! a1 w. ~

" U& d. u6 U0 T- y- m" }七、安装过程中排错
( L6 m0 v* {) Y3 l" E$ c! D6 R: \, O
1.缺少APXS会报错
$ L5 B' n, m* s/ ]$ ~* h* {# h. \- g: \8 G
configure: looking for Apache module support via DSO through APXS
! Z" g& G7 [+ J$ Uconfigure: error: couldn't find APXS$ h: K4 h: K. h
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
+ d8 E6 ~# ^% p解决方法:
4 ]2 P4 H* T) t/ [
- J! ?, {/ \. X3 f2 G1 Pyum install httpd-devel4 o- e( s' e& M% L
2.没有pcre3 |. z% _, O8 A$ w. `

* Y% V' `6 u  R9 {$ M& T+ bconfigure: *** pcre library not found.$ ]: t# ^$ I  H$ ~. ?4 C
configure: error: pcre library is required" B, j: b+ t% U  L- [
解决方法:
# S( w# e' A- }2 M6 ~( |8 p# a! A* B/ {
yum install pcre pcre-devel
( W# ?" {7 U" K. S/ ~& f3 M% Z; O3.没有libxml22 I8 Y3 y% g& `0 t0 q! H1 Q. ~7 w

$ d  O! }$ c, |7 z# J6 g- l
7 X! I- P6 I- J  u! m# J: {2 R4 Qconfigure: *** xml library not found.& {) }" L7 y! K% K1 Z! C  g" T( E
configure: error: libxml2 is required
& F2 _3 I) o. p( N. R. M  A$ h; @解决方法:& d6 S! p: e$ u6 m3 g! r
! x4 s2 I7 ^7 l$ c9 F+ s" ]
yum install  libxml2 libxml2-devel# W: G  V8 n; W' j, q
4.执行 /opt/tengine/sbin/nginx -m 时有警告
9 I- M/ s  m4 z) g6 C2 P- |: c& w+ E/ o" m4 I
Tengine version: Tengine/2.1.0 (nginx/1.6.2)! J3 `1 K3 r+ |, X8 R
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!  h. h' t2 z5 F: `7 i
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
8 b: ]4 m" o7 g* s: f' d: ]  }# a1 s, O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.. y' }7 w7 U1 e. c, ~3 o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
4 _- I1 I! M: f/ Y2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
7 k5 v& w' t3 D8 ^; h: u3 T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"  v" |) G$ t# d& ^$ q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"$ s: O3 o& R  O3 \9 k. N' C
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.3 u7 r7 K. w8 T% i
解决方法,移除低版本的APR (1.3.9)3 V( J# X' L* D6 t* W) o8 l
4 @. u- C; V' F) `2 J" L
yum remove apr# @  e& j2 b# a( ]) H: w! d8 {
5.Error.log中有: Audit log: Failed to lock global mutex
: Z7 n- f2 f* J. c" j6 s
8 D3 L8 x# C2 }7 }2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     * i: X1 {$ v6 C
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]+ j2 p- W4 Z6 n6 @, o3 t
解决方法:1 G. H2 q9 R. y0 t
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
9 E0 ~0 e1 k# B! l5 @' d1 H( m
: S9 z9 T  M$ X; B& b: ^SecAuditLogDirMode 07771 _; U; _6 g. c1 A- B% k) v/ M% {
SecAuditLogFileMode 0550
/ P3 r8 _' r$ u7 g( \3 cSecAuditLogStorageDir /var/log/modsecurity
% V8 o0 G; a; |/ M9 LSecAuditLogType Concurrent5 }6 m6 m) `( l+ J4 e$ w4 F
参考文章:9 a1 ?& {1 U$ _: L% T6 j
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
) c4 I' V+ f# K1 N. {3 k  l2 @% khttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-7 01:52 , Processed in 0.071405 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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