找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13094|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。- p7 U' X/ W# F: h
! K$ V, {( W1 P0 l0 ]
一.准备工作
* z7 y) z9 L' [/ e% G9 ?
0 n( X2 y% |0 E系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
1 o. _9 l+ k0 m# @$ J/ r
5 O& N' `/ j: E5 Ttengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
& K, v  Q1 f  k5 L& g) b) b3 ~6 D/ b: p9 \9 g) H, Z: N% X+ g
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz& l4 Z/ P  T: i

  X' e% W7 r) V! {% J# \OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
0 A- `0 S7 A! G
/ y- o% r# }9 @& A# d8 P+ C" T/ z依赖关系:
; s' Y5 i5 e7 [  Ctengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
( u- x) W: \# C% r7 K8 f/ d4 v- R# g7 G
, T+ d  b$ b: y" M( ryum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
6 w7 t& _# c6 z  Bmodsecurty依赖的包:pcre httpd-devel libxml2 apr
% x- u7 }( r$ m- X- v3 K  Q+ J) ~2 U  l% M" g
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
" _5 o1 G/ I% Y9 S3 _, P! `二.启用standalone模块并编译
, y* d/ V. L3 |% _4 b# O, |6 {
8 s6 N5 ]* M7 Q下载modsecurity for nginx 解压,进入解压后目录执行:2 y& e3 i" A$ |2 q3 u" d; I
  `! J( Y1 h6 G! k: ~- D
./autogen.sh# M; c/ F" u" Y( K6 t0 n
./configure --enable-standalone-module --disable-mlogc* g/ d, i3 {4 X# u& a% c$ O
make # n8 Q( h, z  N8 m( {
三.nginx添加modsecurity模块
( ?' J3 `/ o+ B- z/ P$ Y' i
1 z  u7 M2 @5 z, h# \( h; S( v/ I0 |在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:) K# C/ w9 f, g
- S& k8 E8 Z- n' B7 M
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine0 Z+ p  F: }  x2 C" u+ P. E
make && make install2 X, g7 J5 I/ Q$ J8 v
四.添加规则' h; V3 v+ O. [/ r' Q/ g
- X9 u0 n  f- W/ S. I
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
& e1 v% K& w1 m3 L: a, ]0 T+ S# B* U! Z# I6 B
1.下载OWASP规则:3 q) Q5 |  I3 s5 u

8 r* G" d4 \0 G+ K$ z0 Qgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
5 {- W3 C' F5 g  e6 i9 D$ I
; v; d7 M" Q' @* c+ s/ vmv owasp-modsecurity-crs /opt/tengine/conf/; h+ N" z  t2 u" y9 C0 T) J
/ W2 c  j2 L; W% p
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
9 v/ J8 \% G* K& c7 M& K2.启用OWASP规则:
9 i5 w$ }/ ^8 U% ?' o. H
8 s- d: `% s! F( L& Y& O& B- r复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
1 s8 |) q/ j. f2 N# S# k: u6 F, a9 ?
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
: M# z* Q9 c5 p9 l* ~) L- E' A* t& @- q6 O
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
2 l3 h6 F" J$ ^/ w7 m$ @3 L6 y! |+ W! M% {2 `" x7 p
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf( f0 d+ `1 O, b3 b) Q
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf" ]( C; l9 L( J1 Q, M. W: c$ `% w
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
- \2 K' p) \0 }3 v# b+ n; VInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf* s. `+ V' m* K" j; x) r& m1 J2 ^
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
' g; y. M  J9 k  F4 ]4 R/ D  B! ~1 EInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf1 Z8 J7 [; E( T$ F; M
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
% I6 O5 O( b3 Z0 u3 @五.配置nginx% L3 H# Z+ y; E* G9 T/ h: V0 S4 o/ B

2 z0 F* _& q( Y; y5 {0 b! a在需要启用modsecurity的主机的location下面加入下面两行即可:6 R. M" f/ D) }8 L9 p
% O' k* i- I4 E, ]- a. H
ModSecurityEnabled on;  . j4 l5 E: r8 F3 Q
ModSecurityConfig modsecurity.conf;/ l# @  a8 N' _
下面是两个示例配置,php虚拟主机:$ M8 ?) s6 K+ \, }. W
/ j: H: p6 d! Y, k2 Y
server {
. `2 U0 v; c+ j$ k$ I' u      listen      80;
" y$ a- a0 X' O  n4 n  y' \, }      server_name 52os.net www.52os.net;. E6 ^! A$ d; ^; ?
     
# p) F0 V/ ~0 b3 ]4 ]: e( q' Y      location ~ \.php$ {1 Q# K( V0 H7 x0 X, U. G. S% Q
      ModSecurityEnabled on;  
4 p7 i8 ~( a$ d, C0 e      ModSecurityConfig modsecurity.conf;5 k2 T% v; B, J% `2 j, E
# S' L5 }! K) [. n- k, |
      root /web/wordpress;* ~3 @0 J) d5 M# e0 O
      index index.php index.html index.htm;
+ o1 ?% @$ [" n5 ?3 [* _$ {: a  4 s- A* y4 q" M/ q# g8 f: {
      fastcgi_pass   127.0.0.1:9000;1 |( e9 d2 o" [1 D! c" M' ]  t
      fastcgi_index  index.php;
3 `7 t  K1 ^, R      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;* ~% Z1 w% ]) C( k
      include        fastcgi_params;
1 A; _1 F. R1 V* c9 v  v, H      }
6 g% a! t9 `: y6 g% k% f& Z6 F9 z! E4 a  }
# p3 T# }0 {6 _% x1 E! u1 Jupstream负载均衡:
8 k" m" Z7 @" y0 @
5 }/ W5 K9 `. F/ y- P: lupstream 52os.net {! R. _9 m0 ]9 U
    server 192.168.1.100:8080;
  `6 _6 i; J7 l7 q3 |7 }' Z    server 192.168.1.101:8080 backup;
  n$ t+ x6 n, i}
/ N( e; z( `/ U" J! T2 R2 g* a$ Y  G
server {& b. e$ v+ I% Q' ]
listen 80;- i, p0 B" J. V, X; l
server_name 52os.net www.52os.net;3 q4 u: u; z4 c: G  p8 G
& h) Y- z* a: G! }9 D2 |0 `) |
location / {, P9 G* h: n2 c0 B
    ModSecurityEnabled on;  
( g) [, j# ?; \6 q; D, F2 D! Y    ModSecurityConfig modsecurity.conf;  
2 e! A: \8 Y- k
3 K) J1 n. ^+ w5 w7 Y1 w: X        proxy_pass http://online;
: S- q  Z+ i7 B: e% B        proxy_redirect         off;
) w+ |) @% i) M$ `3 V9 I        proxy_set_header Host $host;' W1 W' T9 u( F: }
        proxy_set_header X-Real-IP $remote_addr;) a' p2 c# ^$ T+ w
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
) j& e! S5 L+ d* S  d% g7 N4 K    }
8 E; }6 F' c  q! A}! ?! m& x2 r( |( R/ \" {/ e
六.测试, ^4 S# t' J) D
2 X8 G& P4 c  w0 s
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:& W. P5 R1 l8 ?3 g2 @! |$ b

# P* o4 t0 L; J5 C* J9 N: F<?php# d  f+ Q$ l5 o2 G
    phpinfo();   
) e1 r' m+ \7 Q. o?>9 {$ _: T, H/ y4 J, _
在浏览器中访问:" N  ?2 N, i" C" ]9 Z: B
' {2 F7 q/ v  ^4 a' E+ q
http://www.52os.net/phpinfo.php?id=1 正常显示。
. W3 |. n8 ~8 P2 o# h% @4 lhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
, }8 N/ R6 M1 J& U( k: C4 whttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
& u4 t8 V* j6 ?4 Z8 U1 X7 S$ b, m说明sql注入和xss已经被过滤了3 @2 ^; E6 R) k. h: j
6 @" {) y% \2 V( \9 H5 d
七、安装过程中排错) L! K4 H% n7 P+ V, X
2 X& O5 {. ~: y: x  k, K
1.缺少APXS会报错
. Q. z9 w6 `! ]& v) n; k0 ^
( s' F( h' j0 J3 k4 c5 hconfigure: looking for Apache module support via DSO through APXS
" `7 G1 J$ c$ R5 E# [configure: error: couldn't find APXS. E' a  M2 M4 D, [- i, c5 }
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
& U" g0 W: {$ g$ ^1 p9 d# @' f9 `解决方法:
/ c0 L' I+ }4 ^- r( e+ M3 _" H4 D& u4 u4 A
yum install httpd-devel
3 K( \  P5 g' m. e2.没有pcre3 U' I# S1 g% J: _/ t

3 o1 r* v! J& T; Mconfigure: *** pcre library not found.
2 E8 X4 o8 p6 l/ Z. Fconfigure: error: pcre library is required. ]) g1 o: T* E0 J3 b
解决方法:
  m. X) {! c, ?6 \/ O6 x
2 ^9 y$ b+ k* ?3 w; E7 Q4 d1 gyum install pcre pcre-devel, B1 Q$ T3 @7 z% h- E( u4 ]
3.没有libxml2( M* n8 c- a# s" E2 Y7 z& R" P

8 e" q5 n6 E. z# T# ]: W- n8 ?( I+ L$ U. N" l5 x! }
configure: *** xml library not found.2 l! F. u! ?7 N- O! H
configure: error: libxml2 is required
9 \5 p/ x- D; I% d0 P) }6 W# n6 c3 z解决方法:& j8 z3 G  T& `' k! z' R

" ~) g8 r9 n& w* Y8 `' K+ c* w4 [yum install  libxml2 libxml2-devel
: a* h! D/ O; `! M3 q" l9 G; l( x4.执行 /opt/tengine/sbin/nginx -m 时有警告
2 Y8 {% K  \# N' [) T% C9 S9 }4 Y8 M/ p
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
( p/ H7 D5 K  N0 K- f) f! knginx: [warn] ModSecurity: Loaded APR do not match with compiled!1 E4 Y8 F# W4 h3 V2 u$ K; D
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
. a. N6 o; n' d- J/ S
0 U% X6 {( y1 Y9 t2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.! h& f. `8 L3 l0 m! m2 `
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
# U% E8 O: I# B/ t3 Q9 }* \* u9 E4 Q2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
. ~+ {/ H+ z; S! i0 R. Z8 _0 W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"8 u+ H: Y7 J9 {! @( Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
$ F  i$ o7 Z6 n2 l+ I" @2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
) D* {7 U% Z! O) I$ E解决方法,移除低版本的APR (1.3.9)" Z. T8 |: b0 z5 S/ f5 x
3 j1 b2 h6 S+ h+ o8 W0 b
yum remove apr
. V0 ]( `2 {9 V+ ?; D7 X& q% G5.Error.log中有: Audit log: Failed to lock global mutex
" t  [. `* c, Z4 v* i* w
7 c9 V; n9 M# i7 E# j2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     $ a- [7 P1 E( c
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]) t. q( w" H; Y% _$ m7 E& D5 D$ n; \
解决方法:
7 u0 ]; S% N: K编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:$ N9 ], P  o3 ~$ G

8 J9 {* s1 M, W! l) d4 \/ k. rSecAuditLogDirMode 0777' C+ |3 V5 h! j" T5 y
SecAuditLogFileMode 05507 U+ V( C* ^9 {6 |
SecAuditLogStorageDir /var/log/modsecurity5 ~: x) i1 Z% I2 s' v
SecAuditLogType Concurrent
4 S2 Q, \7 ?& h6 @7 m/ S# A参考文章:$ |# ?$ b5 V) l' F) t
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX1 b; r( ?/ r7 j: X7 A+ \( y) c7 G* B
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-13 22:30 , Processed in 0.061790 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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