找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13062|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。$ R, M( ]3 B; O/ n. {

5 r1 ]! o; J+ D一.准备工作- x8 n2 M9 Y# V! G- ^

4 |- _1 G- b9 D( `& L系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
; U9 |' O$ o' X, q" r: x3 w8 o
) I* K6 s; B% I2 r6 G9 \tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
& \, s$ `4 h2 ~$ y' M  ]* ?# Z. w: i" o" f& k" n: W, r0 L( S0 z) l1 A& ~0 |
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz: g: |( _8 w9 Y

0 W0 L, Y; v6 V8 wOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs& K* @3 v' _4 O$ b2 b) f" {

0 m4 R: b; m3 r/ e  M* ]依赖关系:8 S0 p- a# x1 i* Z9 I; x
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:4 Q: B' c7 f' M) S9 r4 x6 [! X* S
2 I2 l. {* \+ I1 e7 D- D4 u
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel1 k( l+ @2 X7 p
modsecurty依赖的包:pcre httpd-devel libxml2 apr/ Y8 c7 V2 x2 d, F9 ~+ j# W; B

" P* V' c. A# R. z8 O  E! D. {3 Jyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel# J2 F% h# B4 d
二.启用standalone模块并编译" f0 A5 f: g+ o3 u, Q1 Y2 i1 c0 Y
$ ?7 A( S( F' ~4 K( q
下载modsecurity for nginx 解压,进入解压后目录执行:% [1 P( `3 K% W

- E* B$ v3 H- x" N: }6 x6 @+ R./autogen.sh
* Z5 _* K* g1 G' N2 ~  ~./configure --enable-standalone-module --disable-mlogc
, M2 m) P8 ^& `9 ]0 C3 V- D. D# Nmake & o  o5 Q/ A6 ]2 e& Z( c+ \  V3 m
三.nginx添加modsecurity模块
. i8 T: X, a# e' f& j
% L! \/ U. G0 ~- J$ N在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:* i! @1 [1 ]/ K* v" V% t& |
, A0 P' }1 ?. g% {/ N! y
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
9 c/ l/ h; c( G! s% ?make && make install( c2 q7 X$ ^2 y$ l3 _2 j: c
四.添加规则
3 v/ _* j% v$ w' p8 I0 p% l
: v' k6 H3 g! F& j- ^  Smodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
# m2 w: s& u) M( O: z
6 f# b6 L, k* o( F, f! u& l1.下载OWASP规则:
7 I8 f2 S& c0 c3 H% S" P& w- L3 }9 C2 Z" @
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
$ h0 g2 I7 b+ C6 j
- F1 _0 ~! X) E& j# m3 A8 Q4 Smv owasp-modsecurity-crs /opt/tengine/conf/
8 s, F5 [; a0 F; u1 z2 |8 @$ M* s, [: {, w" d6 j9 y
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
8 t6 |$ j! {2 C' q1 B1 }2.启用OWASP规则:# Q: A/ C, ^! s$ K7 e$ n
3 R! ]0 [+ V0 p+ t5 K7 n
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
8 R# Q( h$ e1 K! U  L' [5 [  c; z3 t" a3 Y7 O% A  {; Z8 v) k
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
1 M  I' l) e5 Q# D% K2 I2 m* x0 Q+ {: j
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。; Z( v8 |9 D; v2 N: G& L

- V8 U! [9 X- H% p' c! R4 uInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf, e* e0 f0 A- j4 d& r3 G
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
2 ~' x& s: r+ D) V. `. G  F6 J! e* uInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf7 `7 C; t& T  V/ @. e
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
* J0 V  y  R) L7 Y6 ~+ i4 B- fInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
$ {6 u6 [, k) p1 R/ W5 eInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf$ f% z6 k& N* j) s& ~6 b/ V$ K
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf8 L. `! |) i7 k0 y6 A% ]0 M
五.配置nginx0 Y" i) k) S% G- M

2 s( [% m6 v8 g; D8 o; Y在需要启用modsecurity的主机的location下面加入下面两行即可:# t7 `8 w  u4 ^* A/ @! f. B

) H4 v3 U" F& m/ [' {ModSecurityEnabled on;  
, K; {  d2 R2 Y' M: DModSecurityConfig modsecurity.conf;
( h! D8 w9 V( ]下面是两个示例配置,php虚拟主机:2 }6 R5 V. ?% M5 }, }
$ Y' E) m& R7 n" q; |+ \4 q
server {) t& l% i: {+ f
      listen      80;
7 n5 e& q1 D% c; \# H4 R* w8 x      server_name 52os.net www.52os.net;0 L' |8 Q: V5 ^( U# P" J. R0 T
     / B( }6 k  F3 C' {% Y& M  R7 I
      location ~ \.php$ {
+ c2 T3 ]% ]7 z2 W& ~      ModSecurityEnabled on;  
+ s* l+ ~" f, S  b      ModSecurityConfig modsecurity.conf;) {/ t, k# M) r' s# B) M9 v
+ g* B$ M7 H- r% U  g
      root /web/wordpress;
1 z% x( M: D  x* V      index index.php index.html index.htm;
9 d8 `2 @7 _8 I5 A# E* v  + ^5 }% q! w; @
      fastcgi_pass   127.0.0.1:9000;/ n. L$ D3 L$ q7 }( n. p4 p/ B
      fastcgi_index  index.php;
3 v8 H) ~9 o6 F, m2 M, S      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
9 ]3 h% O3 o& }7 {8 D; t      include        fastcgi_params;6 l: J/ B1 P9 T: P: q% S+ I
      }' T3 ?  x8 @; ~8 n0 W* o, |
  }, a1 ?+ A- B& ^
upstream负载均衡:9 D4 `- l8 ~% ~/ ^5 o$ v! k

0 K8 e, f7 r8 X# t, i: x% xupstream 52os.net {
# j! j0 W* B0 ^    server 192.168.1.100:8080;
: E+ ^7 T" G' {# n    server 192.168.1.101:8080 backup;! q1 k# i8 V. D- c$ c5 f/ ]2 ?  o
}( u$ R# H4 b4 O, B3 d
2 Z/ b/ c8 i) r2 k$ i6 ^
server {
, t8 t! I( h3 `" }/ Wlisten 80;
, F& a2 w8 [* o! ~/ `server_name 52os.net www.52os.net;4 J7 w( X5 c8 y0 V# h8 _8 A
  O4 n4 M" A' V' b
location / {' \! k( S9 y/ Z, m( a5 p% ?
    ModSecurityEnabled on;  
! W6 N; w' ~) d6 x  v% ?    ModSecurityConfig modsecurity.conf;  
& p- \" S3 w, t% e1 Y) U, u
- n2 v& ^+ H7 S        proxy_pass http://online;
9 q7 c# S9 f1 Q        proxy_redirect         off;
' [! S+ t9 y' y2 t        proxy_set_header Host $host;
( ?; t$ u# P% t  o4 \) t: v        proxy_set_header X-Real-IP $remote_addr;; X; d/ t* q2 {5 @$ N) [3 z
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
9 j3 ^( w3 y: k% G9 ?# G    }4 X5 p: {, s, O
}! K; N1 U7 e8 e3 d& A
六.测试) O0 o% k$ u, n
  b  D- W) E+ N) I& c
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
8 B  U  ]1 i( \4 N1 x* q3 p+ O3 r8 h
<?php/ }# P/ F. d1 b' X4 D! V" b
    phpinfo();    " d8 n8 v0 y4 f/ f+ o* d  m8 c
?>
3 H1 b4 [! z4 {2 S在浏览器中访问:
& @# V5 B; E+ ]& M& n, N
( \3 t7 M( N0 y, X+ ]% b1 Chttp://www.52os.net/phpinfo.php?id=1 正常显示。$ Z; A* L  A6 Z: n( Z3 I
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
3 E. ~. V' K4 G6 M5 fhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
. h3 e& s# ~0 y2 P9 x/ n说明sql注入和xss已经被过滤了/ a, k$ K' W7 ], A4 _
4 o9 U' Z' V1 y# W( }* ~& t  d
七、安装过程中排错
6 ^! \3 T5 n& ~( Y  B( K6 R% K# r  g2 n# J, ?4 D
1.缺少APXS会报错
8 F1 Q# f0 P" z/ l# X9 v5 H& l* E: t/ H, H
configure: looking for Apache module support via DSO through APXS0 h% v6 n! w# m8 R4 `- V8 k- }0 h
configure: error: couldn't find APXS. y3 |0 l2 ^7 _( A( u0 P. W2 h
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。! }" j3 B9 }$ f; F' a' x0 j' c* }
解决方法:
- T6 R/ [+ C1 o3 _! j
; M. D% @- J. \9 R7 ~yum install httpd-devel
' s9 T' S  }* z( [! n0 w' `2.没有pcre5 v7 ^8 q" _1 I, C
/ M4 d. ]; i# i, T7 K( T" A0 |
configure: *** pcre library not found.5 p$ R4 L( u+ T# K( ~
configure: error: pcre library is required  t  f# I% W, j  Z6 n7 o( q2 \) R
解决方法:8 R' Y1 t1 `/ H: q! x

, n! P# B% _. x7 p& C9 R% E. X: ?yum install pcre pcre-devel
, t) O: |2 g6 _, r; ?- ^( C3.没有libxml2
! \/ A+ E) r7 D' T6 |, }% o& C6 q: V

& ?6 f  \, x3 F" Dconfigure: *** xml library not found.
+ K, u: w" r7 _1 ^configure: error: libxml2 is required
+ s$ u2 f) e7 y6 P解决方法:) v( F' e5 f  o# l8 o& A1 q

7 t1 y: y2 o9 v$ Cyum install  libxml2 libxml2-devel
4 `- w6 [; X# `3 A2 a- Q$ j4.执行 /opt/tengine/sbin/nginx -m 时有警告# `" H# }0 X& E: |1 T8 N/ d, N

! Z% h5 K. `6 t' L$ d$ j4 ETengine version: Tengine/2.1.0 (nginx/1.6.2)
+ a' s$ g1 J  f! v0 d  r& qnginx: [warn] ModSecurity: Loaded APR do not match with compiled!6 C3 g/ e, \8 ]
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log0 ?, s  M; T  ^2 `

: I+ e4 D2 @6 d( A( ^7 M2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
5 H' h4 k3 q$ l+ F2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
# ^4 f1 N6 B( ~6 C$ e2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
8 z: {( ~6 d5 R- a+ z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"0 A6 m) o! I% W9 H; g" K4 y+ _1 a  p
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
- M  [3 A7 S+ W& e: t7 {2 f4 q2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On./ j& v" R- O3 E& Q4 Y& |
解决方法,移除低版本的APR (1.3.9)4 f/ L' W4 w% R4 M
% {# E) N  r+ _$ [+ k
yum remove apr
9 p# a' s% x3 O- Z, G6 ?) P5.Error.log中有: Audit log: Failed to lock global mutex' i9 B% [3 F' C3 K; F2 n
7 w/ u  f7 B, y0 X/ i
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     , p# }& W1 Q+ F$ j$ k% g: r6 `
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
! H4 X. }" l( r$ {* w( x解决方法:" @& x* ^6 L/ s: e. F2 R! O8 H
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
  v3 R, A4 D5 M$ B( M; @. e7 a% Q+ @  v3 D# Y
SecAuditLogDirMode 0777
$ f. i  B. _1 u' N( e7 K( ESecAuditLogFileMode 05507 ]& g& E9 I; D- c8 I/ `7 M
SecAuditLogStorageDir /var/log/modsecurity- Y3 O+ j1 d3 K- F) F/ \7 \/ V1 Q
SecAuditLogType Concurrent
# I$ S$ Z& E  u, o' m5 G参考文章:$ h  s3 p/ w7 \8 t
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX* p2 e  z# t% O3 K
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-9 21:53 , Processed in 0.146262 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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