找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12429|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
! k! H" z2 y& j4 T
; R! T7 a3 o  I5 G; {- j/ A- r一.准备工作
! D+ s. w% x" K6 n% i
! ~( a2 h9 q& Y# Q, j. }! Q系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
" |# u! X" N( F
6 U" q6 y' n; d# T, O6 ~tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
) M, m3 d$ N5 K- n  O8 q9 {( e1 n- q7 Q
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz) X- b+ g. Q% }& q

& O4 O4 j8 P4 j, L2 c: DOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
' z" o2 A8 v9 X+ O# E# f9 u: Y/ _
+ Y3 B1 p, x: ?0 y' e+ P依赖关系:0 h5 ], S4 P- n3 M/ ^4 @3 T( T
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:# L8 z- q$ S- c$ h. f

7 S% m) m- {6 f/ v2 P7 o2 fyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel3 ]; q* z1 E  {* P! I$ ?
modsecurty依赖的包:pcre httpd-devel libxml2 apr- I$ Y: V0 g. N

% D" b0 ?( g3 i- byum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
' K$ t% ^, f: t$ y1 I: @# d二.启用standalone模块并编译
+ X/ T  v/ I8 t  M* H4 T& ?2 Z6 M0 r) Q* a/ ~
下载modsecurity for nginx 解压,进入解压后目录执行:* e- U. R  U  o0 y! W: D6 J

8 Z) M8 c2 i" m9 _./autogen.sh
( L3 t6 @$ h9 x7 W. X./configure --enable-standalone-module --disable-mlogc
" H0 P: Y1 b3 y  H  G5 Pmake 2 B+ L0 v7 o" W0 U9 x- e$ y- ]1 A! d
三.nginx添加modsecurity模块3 B- E* T1 U! g& {" Z+ h

8 H" ]& j4 V: Y2 D7 Y3 i在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
& }8 ]9 l+ h; o+ T! Y# o" P. w, B" e* b. S6 y
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
( M0 U  s' I8 @) u& B* gmake && make install
) \# ~. ~6 o- S, `四.添加规则
; s& V! A9 B# O8 e- ~$ h; L3 k2 }& P+ m7 L8 o* F5 F" q
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。# L. e9 h  ]3 l! [

# {/ U+ ~6 P) B3 S1 g1.下载OWASP规则:0 y( b% Q% k& E& l: D
; b. T9 K# p$ t. m4 h
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
" U5 h- [' l* n& g0 ]8 W/ T
. D# W7 c# r4 t9 b, imv owasp-modsecurity-crs /opt/tengine/conf/& M$ r+ ]! U. y8 N- b
$ c8 l* p' S3 x* s/ @. F1 s
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf$ Y. W  Z( \9 ~( L
2.启用OWASP规则:
1 Z- o3 {" m% n; |& c" a
8 m* E: B  L9 E5 T) ^# D1 C复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
# r( f1 w9 i: |
& _% J, P$ Z3 V- Y! s7 _/ l编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
8 {/ K6 `; S) z2 ?" L
2 i9 }5 P0 _& z3 Mowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。- G5 s: Q+ f3 q$ q( I" Z2 {

1 `0 Y% O' w& [6 k' J6 x% F- ~' F# fInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
" h% Y, d9 j* V* [1 W$ IInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf- `7 p# r+ v5 n3 x
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf1 o/ u4 v) g  Q, q1 u9 Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
0 n7 ]4 t, D2 U6 S. V$ AInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
0 ?) r$ ^  n" K* C6 L) c) }Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf) e. @5 C/ w: r7 ^) W' ~3 H
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf& F: [+ s5 Y9 g5 z8 O
五.配置nginx% Y; t! e4 q% e6 z% R
  k3 ^- O  P4 ]3 R, ?0 ]6 m, u
在需要启用modsecurity的主机的location下面加入下面两行即可:
: B0 B, j. ^) O2 `6 }
0 ?" W! t/ I2 M3 H# gModSecurityEnabled on;  2 N. d3 F( s# C
ModSecurityConfig modsecurity.conf;2 ~: \( J+ M! b7 }4 l& l- \
下面是两个示例配置,php虚拟主机:% [6 \( S. }2 J. L! `, x
! X) `" W8 l; P
server {( y0 v6 r/ z& K* u' O3 [' T+ X
      listen      80;
& M* {9 ]" J) r; {* [      server_name 52os.net www.52os.net;
( _5 Y' ?) \9 O4 Z     
, B7 C& t* u; ~) R+ d5 N      location ~ \.php$ {" D, o3 d2 X3 F. i, n3 e; j
      ModSecurityEnabled on;  
% Y7 i$ v0 O1 o) m+ u      ModSecurityConfig modsecurity.conf;
$ e! u2 S: c; a% x: ^& H' @
$ i' K! Y4 ^2 r4 F6 I      root /web/wordpress;8 ^5 N0 q  f& L2 K. i5 e" I
      index index.php index.html index.htm;
2 I# H3 D1 J4 g  , K3 `3 H# l9 _9 X) h$ D( f- U% t
      fastcgi_pass   127.0.0.1:9000;* R, _1 I# G/ _/ y+ |
      fastcgi_index  index.php;7 f2 F: J  D. u" e- i
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
" M( e, {- N  ^& ~. k  w      include        fastcgi_params;+ l. k) H9 i3 H6 O- J2 G1 ]( }
      }
/ e1 D3 a) E# W1 a* v  }  \8 q+ ?5 R# x2 V6 {0 x1 I$ {
upstream负载均衡:) r/ b8 {3 y( i# b4 c
" h2 T$ @# ]% a( C  V( l3 H6 u3 V! T
upstream 52os.net {
4 h- T% c; I( A8 V- q* @    server 192.168.1.100:8080;6 g1 ^$ L6 q4 J* s9 I, I1 _: h$ K
    server 192.168.1.101:8080 backup;
6 q" W) K: M' t/ E}
; L4 o* M4 F0 a6 ]' r7 F. c9 R6 q' ]# f6 U8 \) X
server {5 [% A  _" h5 H2 m7 E; e
listen 80;
7 @, A' I) t0 z2 U" [server_name 52os.net www.52os.net;
4 @* q& D; x  t% W. i/ |) ], E( y% W6 P
location / {
8 \" Q  G1 }# e, r8 g! T    ModSecurityEnabled on;  
  A2 n* O* M" Z. K0 F    ModSecurityConfig modsecurity.conf;  * c) K" T$ ?. w/ s, k% A! a" J

* u9 X8 V: V- m7 p# }        proxy_pass http://online;5 P: @1 R) c8 F8 ^+ E
        proxy_redirect         off;$ d& p3 B* H9 u+ M7 h' i
        proxy_set_header Host $host;' e1 r$ o8 q- _# t* y5 K- x
        proxy_set_header X-Real-IP $remote_addr;
9 M! a3 f$ q# |( p/ w: a& a: \        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;8 T7 d! k2 s. t6 X8 `4 r
    }: Q& I0 t9 ~- U$ ]
}0 r$ [2 X* Y6 k' Q( ~8 r
六.测试! l2 y( y7 q' @& t" |% M* _

+ L# S- C: v/ y) s$ F! _5 x! w  D/ N) e我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
+ W, g8 y! `* ^- E! s7 @. T6 G  I2 ?
% B+ u0 p1 y2 ?: E& K7 A<?php
, A3 X3 P1 g( L$ |2 j( T; H& u    phpinfo();    & S; Y, T0 @7 T7 V3 |2 p
?>/ Q" A: c& y+ d* B7 x- }
在浏览器中访问:
. Y7 S8 x7 N# i
% x6 l) x$ y; H9 }5 Y0 Fhttp://www.52os.net/phpinfo.php?id=1 正常显示。: S/ j% V7 V/ i6 e
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。0 m9 e! A$ V+ {; i' z
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。  v7 I! l6 T2 \
说明sql注入和xss已经被过滤了8 L9 ~  r! _- z) F9 X" i6 T1 F

% o5 m2 M* @" N! ^七、安装过程中排错
+ u  O9 Z- v4 i' h( t' Y
$ b* C8 k' ?; i- [" `. a& V" |1.缺少APXS会报错; D+ |; ^6 |6 P9 H+ F

7 v6 k8 Y4 P" b) q1 d0 t) uconfigure: looking for Apache module support via DSO through APXS
: h8 x5 i# m/ t3 j" d" cconfigure: error: couldn't find APXS
: l3 K; f6 n5 @1 N8 e; Bapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
: d4 `, |& g% l4 O6 Y1 P解决方法:
  c4 H0 v9 Z4 Q+ p) n8 Q" d+ o
3 X0 a+ Q8 r, K  f- M( ]yum install httpd-devel
( @& h! ]( `, ?1 h1 h3 O2.没有pcre
3 ]8 A# F* @1 q2 e4 Y1 G8 H2 ]& |" [0 w( ?6 E9 M& Z, a& q( b+ T& |
configure: *** pcre library not found.; J. Z/ L/ w, G
configure: error: pcre library is required. r' j- y$ E- K, b/ M; u5 Y, g
解决方法:  K; \3 `3 J0 Q- I

) A9 v% m: Z- k2 @6 Nyum install pcre pcre-devel% }9 \" C3 v' y' e! N: A4 G
3.没有libxml2% ~' t3 v5 T0 G2 i- Q+ x
1 X. p3 ~+ E. t; Q6 |

/ G2 p/ `5 j( q$ tconfigure: *** xml library not found.
" E" w5 K+ h1 M+ @! Y7 pconfigure: error: libxml2 is required. E: N3 A1 V5 \0 \# I+ `
解决方法:' H9 g5 {& u/ |7 C7 M7 T+ O* h

' c2 l/ H. ?8 B, K( ^( dyum install  libxml2 libxml2-devel/ P4 x8 N! Z& m) [( G
4.执行 /opt/tengine/sbin/nginx -m 时有警告
; F& J9 Z  R* v2 ?% `. u7 r) \* Z! t( {3 e3 u; K
Tengine version: Tengine/2.1.0 (nginx/1.6.2)$ A0 V0 |% n/ L6 W
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
# q6 }9 Y. K; x原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
4 p& p' L  x0 s) s" e# f5 U2 H
, }, W# f" G4 q0 k. ~1 p- J2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
: ?& f+ ?0 F. ?& x9 ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"2 A5 N, k( u, e& O5 x! T- e' z! a
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
* X: j8 V+ E; d& y+ W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
1 ^4 T3 U4 ^3 T) D% ]5 w; O% X) j2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
5 N/ E% S0 Y1 ?7 Z' u0 X2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.9 K" n) R7 R) f
解决方法,移除低版本的APR (1.3.9)5 m' l5 B0 L0 J/ `  T

( H" x- \! T" d3 \0 {5 V; gyum remove apr
( G7 ?7 K& `7 }2 X) u' T3 A( X7 G. N5.Error.log中有: Audit log: Failed to lock global mutex
  g, N2 x2 I+ `1 s+ ~7 j+ l
' ^% [& S5 y. ~2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
6 d+ r$ D3 B7 N# X( eglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]9 @9 t, a9 U1 `) d
解决方法:. f9 ]3 s+ \. y3 h
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
' A+ n9 C! n& g' _- Y9 C, r6 H0 d& Q  \2 N! g
SecAuditLogDirMode 0777/ u3 E. C% [2 f) b1 A& e
SecAuditLogFileMode 0550- J: {; T' A/ q1 o" _5 @
SecAuditLogStorageDir /var/log/modsecurity
( J) J' b( h" \$ l  d# gSecAuditLogType Concurrent7 z, n  H. r* Z! u& Y7 \# ~7 A, R
参考文章:1 H  k) e% B1 o7 M6 E- k- ]
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX9 P. @  D7 q1 E: d. ~) T4 ^  p1 |6 C2 T
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-5 22:46 , Processed in 0.076240 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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