找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12820|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。" N+ ]8 N7 x3 {, Q
0 ^4 q( t( {+ }* [' _* b
一.准备工作
% _1 p5 P: w% v2 l, g. C( c" v( |3 p: l
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0/ c) O# g2 w# e: f# |7 Y1 c" o- d7 n4 R
. N4 ^" m5 N2 ?' A
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz, N5 F; e0 u0 C' g
1 A# Q( D! I0 p1 `3 u
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz4 F) _. Q7 R9 T1 m

6 n4 z: ?6 z% ]5 m/ L1 U( p' T% IOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs5 s9 w3 h% V3 H& I" Y$ y1 ^

$ {& p2 W! j4 T+ o! o依赖关系:
6 o! K9 l  Y, i- [. u4 Ytengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
7 j. ^! |: w( o1 K3 }, Z1 E( H7 `" D  Z
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel! p3 T. u# Z7 J( ]4 C
modsecurty依赖的包:pcre httpd-devel libxml2 apr; B% f1 x( J1 e( j5 @# ~
: G$ T  K6 c3 P& I
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel7 p! a2 P) J0 r5 _! Y6 f
二.启用standalone模块并编译
, q: U6 ]4 v2 J* v, k5 `
3 |6 p2 W- g; x9 g0 X9 q. h8 W- \. h下载modsecurity for nginx 解压,进入解压后目录执行:
- @, H/ k& r; q3 L3 |1 g; ~8 s: b3 z: w) z$ p1 Y  t# b7 P8 @8 W
./autogen.sh, r. [5 A5 t3 Q  o) c& {5 S+ J5 y
./configure --enable-standalone-module --disable-mlogc) S, a9 q% Y' }6 t! n  x: h2 {4 J, K
make # s2 @- b* M$ l4 K2 N: _3 o# z; y) ~
三.nginx添加modsecurity模块1 |( E; G2 P% Z  W- J
7 p5 B" ]- h6 X8 z! }( g2 u8 m! F6 y0 ?
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:' ~9 x  M) K2 h& `, M. f8 e

& _# ~# e. M" t5 H2 i+ a! |./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine0 |) R/ z  J: |1 S: w0 ?6 k
make && make install
1 g+ C- }. A6 v) h" }# {四.添加规则: R! _* L' n+ w8 B" S4 T
; \" k; {7 z9 u- p
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。; ?4 B# s% b, h4 D# |
# {1 q! [$ L, A+ ]# e% p3 J! L8 u
1.下载OWASP规则:0 [, ], U( [6 V' {" l& k# h

" N- U8 Y# ]! n0 B, Mgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs8 _" u; ]! T' M7 i0 F
9 k+ M; e8 D; }6 E
mv owasp-modsecurity-crs /opt/tengine/conf/% @6 b6 D* K3 g5 v7 x! @5 k6 t5 S, \
6 E* Y9 t8 G/ o: |
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
/ e8 X4 S! R4 E- i0 q3 J2.启用OWASP规则:
9 K! n5 X1 D$ u7 }$ u8 L. d( m  q/ P& L4 W: ?" H' e
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。% V* ?; _6 Z, D/ L) g: R
2 o) w: r7 E2 L. D) [' Z
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on+ [# A4 A8 `1 z9 L6 [6 f2 j- S

/ H0 [) e( y( q, v( q9 uowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
3 f2 }; U' ?! Y; S+ }$ L8 n
2 G  S" s  t8 h: {6 Q) _7 pInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf2 S9 L  L7 F0 T4 Y- I
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf  N6 m8 v; |  p4 c. |& z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf$ c0 s+ e6 |2 ~. x  w0 f" h
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
; g0 k7 R3 G9 f; t6 g$ a: j$ MInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf1 G0 j- f' H- t, }
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf  K. c( U+ ~, K, w6 Z" o
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
+ A# J+ q8 M4 K7 r2 C. K五.配置nginx
; o4 n6 o7 |9 L8 s) u7 J) ]1 o
8 N' b7 u6 y( T4 T8 o) \5 N( U在需要启用modsecurity的主机的location下面加入下面两行即可:
0 Z" s  L; e# H9 k. ]5 i, z8 Y
2 i2 i0 ~5 q2 M3 hModSecurityEnabled on;  
- X- N3 B6 Z+ H. ~6 o$ M  p8 TModSecurityConfig modsecurity.conf;
) B% X. w, M! M  K4 S下面是两个示例配置,php虚拟主机:
2 M8 v) z0 V9 U; J  B7 p4 Z' H3 g
server {% D) d5 M( u, ]" \
      listen      80;& U5 U$ b/ j- ]$ r  X' Y, g" Y
      server_name 52os.net www.52os.net;( r: `# z/ C+ ~3 d) T
     7 A- `, [  C. Z& A1 @' ?$ t
      location ~ \.php$ {
$ E" W/ |" f' U" \6 t  G/ I      ModSecurityEnabled on;  
: k0 _# k* J7 [8 r$ R' Q      ModSecurityConfig modsecurity.conf;
/ _" p& Q7 I5 X) m" U2 S2 U) R# r8 t) C5 T- [
      root /web/wordpress;
/ V' f! `1 T. f) T" r/ I3 R      index index.php index.html index.htm;8 n) d# v; r$ {( W0 H1 {+ L
  9 b) ^  l3 j7 }8 C7 D5 J
      fastcgi_pass   127.0.0.1:9000;3 G# D' l; t9 ]
      fastcgi_index  index.php;+ n$ h  `/ y+ N2 z& K& v4 W, S
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;8 V% @8 {( T* ~% \
      include        fastcgi_params;
8 W! y1 A8 s! t      }# _; ?7 u2 c0 {
  }
( M- f1 [$ N1 r6 p" pupstream负载均衡:
& `, n1 |9 Q* b. k- u( p# U3 }: P, \
: \6 a7 y6 j" `- Z, f% Z( hupstream 52os.net {& p; n) W! I: Q6 t$ b) l: n
    server 192.168.1.100:8080;
$ J2 G3 a0 |2 m$ k  ~    server 192.168.1.101:8080 backup;
8 b. ^% q, v$ s$ T; U}
4 B# q; b. i7 f5 N$ x, t4 K5 A
server {
6 H; w5 A* w' y) _, Q- b4 rlisten 80;- S0 X! I& J$ y5 L$ I
server_name 52os.net www.52os.net;# c" E4 j# M1 h& a

& s' e4 K& u; Klocation / {
& p$ e  l# I; b6 Y# ^7 {# I    ModSecurityEnabled on;  9 D3 {" N4 k4 T+ R5 W* ?1 ~
    ModSecurityConfig modsecurity.conf;  
& |0 i% k+ r6 m, j8 R% C
8 z" B8 [  d" j  o        proxy_pass http://online;5 B, T1 d' R! R- B7 P( d7 W+ s
        proxy_redirect         off;/ z' p) @$ l: s; W4 P, E
        proxy_set_header Host $host;
4 |5 S9 t9 R9 B6 P0 s7 V9 c- a( [        proxy_set_header X-Real-IP $remote_addr;8 P5 |. C8 Z3 L; Q  Q
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;* \8 J( g7 X- l4 I
    }4 }  U6 x+ O  d3 m- B
}2 t) L- Q; O, Y
六.测试1 X' M. Q8 C+ n  D) N( C! Y+ [
7 S* }0 J, F7 I( k% ~( a
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:7 m3 n; p$ N/ Q8 T. R9 Y1 ?1 J/ L1 F
7 i' c+ t9 y) |5 y& e1 S: }, Y4 x' {
<?php
; p! h; t) B4 L- F8 f& b4 a5 Y    phpinfo();    - r' @4 M1 n$ D( M+ P4 f
?>' m1 x3 R9 J0 e, N: L' \4 \3 ~
在浏览器中访问:
# l4 H  e+ `7 ?$ }; L! R+ O$ N" y- F; z# O: x+ N' ]
http://www.52os.net/phpinfo.php?id=1 正常显示。6 F4 J+ A% ~0 N5 ^. A% ~
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
: V1 d1 L3 |! F: Zhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
0 v" S* H( ?) d) c! B说明sql注入和xss已经被过滤了) D, Z' e0 ?5 @0 C
* B( p0 w7 R- C
七、安装过程中排错
+ u! T: F5 j7 G9 B8 C
& H3 o' P8 y- Y( U9 i1.缺少APXS会报错
! Y4 [  @1 Z* @. {( u' X$ \4 d! o! Z% u
configure: looking for Apache module support via DSO through APXS
$ N& h" `8 E3 ]. e/ p1 Wconfigure: error: couldn't find APXS
4 \) o( t$ @% k" G8 r  fapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。1 l3 J0 X9 E0 b. a; m
解决方法:" Z  `- s4 {. O/ S" E& L" W
) J( Z% Y# [* q+ Y. _( |
yum install httpd-devel
' t2 M+ \! X  k4 ]/ T' u2.没有pcre
& P- H. D: P9 ?- e) |. D7 _/ D- m. K6 }8 _! |, ~
configure: *** pcre library not found.
# c+ v7 r1 T% k; n5 oconfigure: error: pcre library is required
* y! N$ K. N5 Z$ H" i: P) S解决方法:6 T$ ]$ v% N: U* W$ [3 ^' g8 W

- B& [/ }, D9 D' e) ^' }  g, Lyum install pcre pcre-devel
: Y' v8 B5 ^9 U7 Z$ ^3.没有libxml2
# m8 R2 _: T; _
  X) r0 j8 m$ b: f$ b% \0 k9 u( c; i" E9 o
configure: *** xml library not found., Q! k# B/ g# V% _4 S$ z. O, R
configure: error: libxml2 is required" ^( _0 {: Y( B7 z- c& m
解决方法:
& H8 V  R% J% y% l( [' S9 f, g! X( ]
" E* z# H3 _+ ?: @* _( k! Hyum install  libxml2 libxml2-devel
" o4 w& `% \* A1 l7 S4.执行 /opt/tengine/sbin/nginx -m 时有警告
; y5 F7 V; R) B0 O: m3 a  Z, m
+ m0 i* g2 i# d4 ?) I' ~2 g- N: g0 ^Tengine version: Tengine/2.1.0 (nginx/1.6.2)
! R+ f. z" t4 hnginx: [warn] ModSecurity: Loaded APR do not match with compiled!: l# T+ M# L* ^0 n  {4 T  |  G  ?
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log2 e) m) L5 K3 g. y6 b% _  ^

; U, n5 J9 N/ T( {9 Q) r2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.1 Q$ f% C- n$ e( u! J
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"% {/ @0 o2 U1 R% k, `: f
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
  L8 W/ l5 x2 W+ Y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"- T2 c7 N  L9 D1 Y1 S/ I8 n: a+ v" q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
7 F% h! p6 ^% ~+ }6 \2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.5 t  u, q& H& C3 }
解决方法,移除低版本的APR (1.3.9)) v' a( p. u+ A3 ^3 r: }/ C: e
6 F" ]+ X* l/ x
yum remove apr
& V3 W9 ]* q2 X; [* w" K5.Error.log中有: Audit log: Failed to lock global mutex: l/ S  J# O  x/ u/ a; n4 A% V
: l3 h( R1 S9 Y$ {5 [5 X& Y
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
; D" x4 t& I- c& Vglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
* |! O# ]* y6 u8 _2 _" [解决方法:& s5 n0 [, O) d
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
! @& L' A; }0 i+ T( S" ^8 B. r. B; w9 K# I
SecAuditLogDirMode 0777
( ~. t9 P5 k! l% hSecAuditLogFileMode 0550
5 K; y8 W* T. z; `, ?/ mSecAuditLogStorageDir /var/log/modsecurity
. v: J0 o9 j% z9 SSecAuditLogType Concurrent& c4 |, K; R, z; H$ W7 R
参考文章:
' _. F% R7 ]/ Vhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
& }4 d1 y5 b# _( Thttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-15 04:33 , Processed in 0.065879 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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