找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12105|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
( \& a+ w) v0 G3 R0 ~/ F) `
7 M9 ~9 ~# [, x) s一.准备工作$ g3 V& c, O- a/ W  n

3 t4 h6 B: J( r& K7 p+ b) Z- ^系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
' o9 \8 X, z2 p) k
' `4 P' c! ]- s0 }( C; a+ [' M2 Rtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
% C. b7 L$ H6 T. a: l4 d) I- W" D# H$ o1 P9 n; j4 {9 D6 J0 w
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
2 X4 A' I* H/ N6 P5 n, V$ r1 _7 v
' ^% y- S9 _+ g6 r. `* y- _$ bOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
( Q  c& Y# e2 p! O8 J7 @3 [/ N) v- }5 G- B# i5 c6 N
依赖关系:) s; i; W* E' X
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
% D$ @" Z, \9 w( ?
7 Y9 C7 O( r9 o1 Tyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel; Z) W& Q4 h$ R" Q3 h$ f3 G
modsecurty依赖的包:pcre httpd-devel libxml2 apr
/ \$ E) `! }7 r9 V
3 Z, D/ [' g) g6 ?2 G* s; [7 X) myum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
: ]  p# q* b( t( e4 P5 z( r二.启用standalone模块并编译
# q- b! N- X5 I4 J/ }9 ~+ K
/ P$ s& H) w" z6 t; p下载modsecurity for nginx 解压,进入解压后目录执行:
2 W1 M7 ~1 t6 i; s' n& Y) W) T, a; a
, a  }: W  ~  p- V% ^7 `7 k, b./autogen.sh
* Y$ u4 |6 p& ]; _+ M) z- q./configure --enable-standalone-module --disable-mlogc: B5 X. E" Z4 g" P# C0 m& R, |
make
) U0 k* p- V' ~8 |( X( u9 S三.nginx添加modsecurity模块( P& O3 G( w" E+ T& X/ r
6 i, M% L" ~# K& Q8 g
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:  z5 ?6 k3 |# I* x1 S# B& ]! n0 H
: R: ~# T7 M2 j. `& U
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
2 G9 y# S! E% I, y# a* xmake && make install! |8 W+ K  J( n3 d
四.添加规则5 q* |+ h5 ]4 S) H& ^  H2 e
7 h0 C; X, ]! J& y4 @4 K& ~) A
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
6 z8 Y  ~) K" p. \9 x/ ~
6 N  s' @; z, w7 l* v4 S% h) ]1.下载OWASP规则:$ m. f0 a1 x/ G9 K

" ]5 e! Z/ y! F: w. Mgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs- g/ I$ S% v" h5 g3 n8 W( r& L

& Q+ j  Y' V3 Q! P8 f" {) Smv owasp-modsecurity-crs /opt/tengine/conf/% U0 E8 F9 q5 ?/ ~9 S

' \6 I8 H1 g( ^" {% E3 ~4 Ucd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf1 @8 z6 T% j7 q. `
2.启用OWASP规则:$ p$ }7 ~6 O6 O( t6 y
/ j7 N4 n" W: M# M$ ^+ {* R6 H
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
( s5 L1 x3 f% e8 b
: Z* S0 Y5 `4 Y0 U# c3 Q编辑modsecurity.conf 文件,将SecRuleEngine设置为 on) L# Q9 C3 @- H4 c  R" \9 s/ h

  c2 b  y/ v0 v# Sowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。% W4 t  \9 X4 a$ ~: j: ?

6 F' d8 r+ `0 F* EInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf/ m: L1 D: M' p1 N8 _3 |, J3 V1 i. M
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf7 p" @7 E* p2 J5 A. c9 P( z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf6 I8 j; D! m8 e, c) m( B- }/ B
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf4 j4 Z# G1 m! E, }9 Y$ |
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf& _& M; e1 A8 Y$ O  n
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf% W4 L- N4 I5 I  H* P, T: h- f
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf2 q" {% n+ {, w' Y7 {5 F
五.配置nginx6 s( b: y7 M1 m& t# Q4 G- P* {5 X
: F/ f# Y* c# B: I8 `) o
在需要启用modsecurity的主机的location下面加入下面两行即可:
, O+ e1 v, t' ]9 x8 T# S; X) a% ~" [& }# e0 P* f
ModSecurityEnabled on;  , g( g, n0 c, v/ L! G( }, z6 y
ModSecurityConfig modsecurity.conf;$ S. [7 k( i  h6 n" g
下面是两个示例配置,php虚拟主机:: \$ Z- M: V* Z' L( ^
- s- ~; h7 j/ R; t$ j
server {' q8 F0 S9 ^5 \% O+ i' I( i- |
      listen      80;) Z5 _$ O' p7 Z
      server_name 52os.net www.52os.net;9 [! I% M/ @7 }% O2 D* ^3 q/ x$ b
     7 B, n) s9 X) d/ ]) L# D
      location ~ \.php$ {
% z9 z# {5 H4 J- k$ |! a+ y      ModSecurityEnabled on;  
9 m3 c% u5 g1 B9 \2 ~      ModSecurityConfig modsecurity.conf;$ B1 K) `" M) l2 ]* O5 ~# r! T

: I: x. j# W4 S' _6 f0 X( x! `      root /web/wordpress;! V; W, `' K0 a) j1 R
      index index.php index.html index.htm;
0 W4 M5 {/ i. C" @  
0 j. e* A5 y0 D( t! x      fastcgi_pass   127.0.0.1:9000;
/ r) u* U9 V" ^" r* W      fastcgi_index  index.php;
( g* A7 }: j; N5 A2 e# T* D2 o      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;: R( \; a8 y" ~2 i* H
      include        fastcgi_params;
% L# k) ?* t  k' g& b2 Z4 q! }1 l      }: g: E+ j& W1 F; Z
  }' C3 V6 ^5 w2 e$ _. k
upstream负载均衡:
1 W: S& _% r; Z- }, |
% d+ ^$ D' j! u2 s+ M. eupstream 52os.net {
6 Z5 x, u2 [8 U8 |5 m    server 192.168.1.100:8080;! \. o6 ~/ y# D" i
    server 192.168.1.101:8080 backup;6 K* n; ?- o+ T9 K
}9 J7 s, U' A* f4 \* m. F
: q: I5 o# b& P; |6 e. r
server {  H; v8 [+ F: w+ w8 V
listen 80;: w2 _( ?7 [( B+ f
server_name 52os.net www.52os.net;
/ G9 ^2 s3 l) b+ a
# u6 O% s7 K( ^4 b; hlocation / {
' a8 k1 E4 R4 j$ n; r    ModSecurityEnabled on;  # `3 s2 R9 l0 c2 W& D* ^& {' d' e6 b. y
    ModSecurityConfig modsecurity.conf;  
" o3 J" _, [; `$ l% x1 j& r# R5 C9 [. A1 p
        proxy_pass http://online;
6 R! O( h; c1 @' j9 `        proxy_redirect         off;
. y3 E( O6 K& \7 F- ?+ Z        proxy_set_header Host $host;
% C  d  I' v0 z" Z0 M0 p        proxy_set_header X-Real-IP $remote_addr;' \2 T! Q! e5 H6 y& i! H
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;3 h! r4 S1 s" ]2 K
    }% V8 \' @! V8 j( e
}
8 n! x" N: g  E, S- \1 D/ D  v* O六.测试1 h3 F6 f: f& E  X
% Z9 H# n; G; B$ }
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:7 x! J2 m) V  l/ d- `) h% @/ |

0 O, K& I# m- X4 Z<?php3 f" S8 I: B( l4 B/ N" W: E3 k3 }
    phpinfo();    $ y& T% s. C6 W0 y) P) s- P
?>% m0 v7 F- r. A& r
在浏览器中访问:( \# l4 x* A2 h4 N# ^
# N" x* \& ?- C! l: D3 T0 Y0 G
http://www.52os.net/phpinfo.php?id=1 正常显示。2 `4 S5 e1 d2 K
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
9 o( M, R. a2 T% S* O: dhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
5 i! k' E# N, k3 {* X* N& l  M说明sql注入和xss已经被过滤了
6 V5 S$ G; @+ I9 y) Q
& a' Z8 `& A( C" Y. u) x七、安装过程中排错
8 K) m, U: \+ n% E+ D/ g5 k  C* J9 Q5 e8 O8 ^4 ]
1.缺少APXS会报错6 C% @$ s) d7 k- }6 A* X

( C3 T/ A+ G# K7 J/ e4 l7 Cconfigure: looking for Apache module support via DSO through APXS
3 j0 B' M9 n+ M( a8 U3 T6 d# k6 Bconfigure: error: couldn't find APXS/ _& X5 ~( J% Z! l' C! }: K$ t
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
( S, ?) W! P% @$ s) H解决方法:6 @7 j9 h- @, i( S
$ f  h  m( e4 ^5 E1 |# d4 E- n, }
yum install httpd-devel
% k, W1 s( d' F1 v8 R9 f( }0 r; T2.没有pcre$ @) U* L. t) @- y
. V. z$ Y, P) @7 W" _6 O/ h3 [; q$ Z
configure: *** pcre library not found.: L$ K# }! x3 t3 y& a' e3 x
configure: error: pcre library is required
$ b9 C: N/ [) Y; H$ k解决方法:& P$ d0 w8 s" v

8 S7 w4 X6 l6 N$ F, h1 [yum install pcre pcre-devel& [$ q4 }7 P7 x2 d, a
3.没有libxml2
! M3 {" S$ r+ g% S8 ]3 L1 w2 N- X- l, F3 i" q/ {, i
  s$ A) d8 \; I( e- @
configure: *** xml library not found.
5 Y' N+ N4 Z8 r9 B& [' [- H# qconfigure: error: libxml2 is required' \% u. h7 H6 ^5 `" p% |- x
解决方法:& F7 w/ T6 D9 y" N( O8 Y5 Q9 J+ o

- h0 m/ P+ N$ C3 r7 B: myum install  libxml2 libxml2-devel
* T$ A- H! Y+ x! I/ t9 i2 I" k- l4.执行 /opt/tengine/sbin/nginx -m 时有警告8 @: S5 b/ q: z. P$ \7 z

* i6 t6 n: P0 [Tengine version: Tengine/2.1.0 (nginx/1.6.2)4 Q4 ?' U% w6 l4 G3 V
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!+ g; c# _6 J: t' {; C( x/ q, z) J
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log7 ~3 Z6 W$ _' G# o

" A! a6 t  R5 H* a6 N. P2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.4 C: D1 I5 x' u1 E  r
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
8 Y* Z* J- O4 S: q2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
- o. ^# l" N$ `  b2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"8 B/ N: s4 [% l; g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"3 H' G3 u2 p" F+ m/ u
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
1 t5 [- e6 ^( J$ t解决方法,移除低版本的APR (1.3.9)- ?$ Y  [7 {7 L- b9 j( v6 _

) s  p! ]1 o0 f( A' V5 nyum remove apr9 o, N- M5 b" y
5.Error.log中有: Audit log: Failed to lock global mutex! r( C5 [$ {: E5 ]+ _% {

) k: ]; r$ K+ u# B, ], Y2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
) H7 A) c1 W9 C( mglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
1 z+ J/ f6 s- J/ w# i; e- K5 E解决方法:
* U. l* c0 p* e编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
. I0 [, a* v7 M9 s3 d5 a/ N2 T" g0 _. |! c" _' a" D
SecAuditLogDirMode 0777. y) I  b/ h5 L# E
SecAuditLogFileMode 0550% k& p" T+ B' m
SecAuditLogStorageDir /var/log/modsecurity/ V! u+ N$ T+ V
SecAuditLogType Concurrent! c: E1 L4 }4 u  p
参考文章:
( i6 ?7 r: G6 g+ l1 G4 y) B; Jhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX$ z8 B2 N+ v& H. N
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-10 09:15 , Processed in 0.060820 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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