找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11872|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
$ T* G) A  o' x
$ I; F( K! `$ ]* A: u' Z一.准备工作
2 E& ]: w% o6 D( \2 _1 g2 h6 l: l* @3 O' |/ ^, |
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0# {4 L2 t# o& A  s
$ u% e8 @! @7 g; J+ n
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz$ c3 ~; \" R+ F. P
4 l4 N7 U" ^7 r% S" T% L: k
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
/ t/ ~0 I# J1 N, E! T+ t- R: W
4 e, v5 x% W  u5 b, i6 oOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
3 w6 T& n/ W9 F( W$ x$ H+ Q
, V( i4 e) }2 l) m" `' |依赖关系:
# i! y9 {- B& U7 V0 D" t$ ptengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:" `/ y  P4 {/ J1 L( G7 w  C3 ^5 }

5 C) L. B3 z/ @7 [yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel) {+ O5 P/ u3 v7 }4 {( \8 K
modsecurty依赖的包:pcre httpd-devel libxml2 apr" \7 m' V) ^7 x9 g5 j6 U! M  D
6 l, E, Z6 c$ z, H$ g9 u- {; j
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
/ ]: ?! w8 `: i+ }9 o7 o1 I二.启用standalone模块并编译, Q6 K1 c' @% I# Z, s$ B, n6 a
5 o9 i5 c/ p0 @, L
下载modsecurity for nginx 解压,进入解压后目录执行:
% K  p3 v- X* ^/ M& O8 i' J: h1 M6 l& ~4 Q' W- L
./autogen.sh; o1 `* f& L% i) T- o' C" r- q9 {
./configure --enable-standalone-module --disable-mlogc' r2 s$ j. E7 }
make
. k2 F6 P6 W6 s* _. S三.nginx添加modsecurity模块
2 @/ N/ u) e; O: P3 T
" z+ N. K# }/ f/ o- ?- _在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:! _; I! L/ [/ X' g$ \% P/ o' [

" Z1 ~' h' m, z: \; C/ e: ^1 _! H, C./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine5 o& i- H9 S& Q! C; d7 e* m
make && make install
! O* u' w8 _7 |+ Q四.添加规则
5 y! t, ^& b7 f  y& P+ G$ q7 X- b
! o  V  C; G3 Q" fmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。: M9 ^. l& d& i% D6 H

+ F1 h, i; r5 P' ?. E( L1.下载OWASP规则:- I7 k) L# n( h7 I  N' w) Q

; U* D! \5 L( |( D$ A. Cgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs6 S% E7 b1 Z8 b$ s5 l8 z8 m* G: v# U
  {/ O: ~0 X2 q' G) {% l: H. ~
mv owasp-modsecurity-crs /opt/tengine/conf/
3 c9 H' c  F) \
, {- Z2 N& w, U: h( w: v7 I% ycd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf2 j! h. \% E7 i
2.启用OWASP规则:" Q; W$ h. N9 ^
; A% }( s' `4 U
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。/ [# w. h3 E  i. E" {8 M* [0 n
1 f. l% O+ z: A0 z+ i
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on0 f+ \6 Z" @& d* ]) d$ j2 [

  U4 B" U0 ^  v* A- K1 {* P  w5 Kowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。+ P$ U5 g" i5 c! h" C& o

% |4 }! v, u: d7 {4 }. e, {Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf3 [( f! h! Q. \% d" j# o; I
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
- u7 t5 G/ t& _3 C" I6 XInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
6 ^0 g. m# t: M! W+ a2 w1 MInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf% O; i) r" _3 L0 L
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
' L( @8 q: W" q6 }( O5 QInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf; E* R  _9 m6 H' g8 R: n
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf, g7 I: m: F: i9 [2 @
五.配置nginx- S: ]4 Z$ S' n8 [; z; P& \5 v# k
* x4 R2 Z. n0 i" [3 I; N( }
在需要启用modsecurity的主机的location下面加入下面两行即可:2 b4 ~) x( e& Y6 z2 i

2 l' g# Y/ Y' l# K# c- A4 T& ?ModSecurityEnabled on;  . {0 J$ j, A- |# S! h2 ?. t
ModSecurityConfig modsecurity.conf;% v! L1 p+ ?4 ~4 l7 ?1 N1 c
下面是两个示例配置,php虚拟主机:/ G# f4 W! l% s# Y; O6 x) m+ k/ y! `" J6 Z
5 _8 @6 c: |( j  [$ J) z
server {
* u1 X4 F# ^, R) B* I, g      listen      80;' v# b! [' F) u3 l" H) |% G: D
      server_name 52os.net www.52os.net;& u2 u  s2 Z% ?8 j1 c/ w7 j
     , d) a' }2 T9 G# @
      location ~ \.php$ {
5 h5 l: l0 ~8 v: S7 I5 {      ModSecurityEnabled on;  
( D: W! e' A) m( ]* Q" _& a! ^  ~      ModSecurityConfig modsecurity.conf;
% W% M6 u9 {# {  F, _. b; l; W" z
% m; A) l% b1 @! ?      root /web/wordpress;
) Y+ s* S& R; y, V5 o      index index.php index.html index.htm;
( ^8 j& B- [+ h6 N  
6 ]: z; T) ~+ B. X      fastcgi_pass   127.0.0.1:9000;1 T  ?3 H( b4 |- I5 H% e1 B
      fastcgi_index  index.php;
9 l( A3 h3 x0 U9 B7 l$ f      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
/ D$ B3 w. t# ~" E, N8 \  z      include        fastcgi_params;
7 K6 e  k7 e5 k; t      }
5 A  ^7 A0 D+ q- `  }
8 i( }# K/ l5 [6 {" fupstream负载均衡:
; t, h+ F$ t9 q1 c+ |
/ f% T6 a! ^: h3 w4 \, Uupstream 52os.net {
7 ~6 d) d' R6 L    server 192.168.1.100:8080;* o+ M( F0 v# q( J7 l
    server 192.168.1.101:8080 backup;; d5 l! n( P) Y* e& o2 Y& k
}5 ], `; u; l' E" M

, N* h6 N6 b; I6 m4 C$ P! ~server {
( G  V& _" t  ]1 r- _* @listen 80;- E3 _& D: G8 `" i7 C- o! h( }" u( Z
server_name 52os.net www.52os.net;, c, Y6 w2 B) m$ b9 s7 M8 J( D& m

+ L- D8 D% Y% J# w2 T4 `location / {
4 ^/ A" |) H+ Z% F7 V# w# h* f    ModSecurityEnabled on;  
+ w) e* z! B" A4 o$ F. W( k; L! p  v    ModSecurityConfig modsecurity.conf;  $ p) \$ q; z; w% G! a8 A8 ]% \
) b" o; ?3 a& b( F7 t
        proxy_pass http://online;6 X3 P3 K# R* V8 p/ s
        proxy_redirect         off;
! x, E6 H6 x+ V  W- L+ \7 y& h        proxy_set_header Host $host;( h7 H- ^6 @5 t- c' [) X
        proxy_set_header X-Real-IP $remote_addr;
" S2 N2 |- C5 D        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;  l6 R. U4 H$ |$ U, i% F# T
    }
2 }& n  t" m( T6 r5 t7 R' Y' P% x}* f: I- H" t* ]0 W: F7 C
六.测试& H+ Z2 |  ?& ?& C. c9 X$ r
1 m3 V( _3 z/ L8 }( H# j
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
' F0 p' @: i; C- ^* q2 k* m6 F! s3 |. Z; j
<?php) a. h' M  d; u. |
    phpinfo();   
7 \5 r- }" B% t6 I1 F5 B?>" R/ Q0 O$ A8 M+ J/ p. i
在浏览器中访问:0 I: m" ^$ H. g8 |& @  B7 A5 n

5 l% M( {7 X" ~* g& Fhttp://www.52os.net/phpinfo.php?id=1 正常显示。3 H- v3 R' L/ U9 _
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。: c1 E! d" _- G% l0 \' r" C6 X
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。8 U2 g- L) I; g0 C  H% }+ `
说明sql注入和xss已经被过滤了. a& W; p! Q- ^- n$ H5 I
/ i8 u7 ?/ Y4 `: ]: r5 [3 L
七、安装过程中排错
6 S9 V: v0 u- ?+ z, g: \# N: q) U9 m; _1 u7 w: z& C
1.缺少APXS会报错$ c. q6 x! x. L5 B5 X- f! j1 s
8 w% E2 ^' x3 j' @1 r
configure: looking for Apache module support via DSO through APXS/ \5 G0 N1 m& _8 G+ O
configure: error: couldn't find APXS
: b) P3 p# O% z. N) D0 dapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
- S6 t) E- C$ s% e# @$ [2 ], f解决方法:
3 |/ b$ r4 t' s  ^9 E) g# w% @9 E2 Y4 j# w$ ]
yum install httpd-devel4 q, ]. W/ ]0 K; Z8 u
2.没有pcre
. @; W) e" ]8 M% N/ Y( l# R
7 s) ~0 }/ D  I* }7 c6 Cconfigure: *** pcre library not found.& ^: A( d! |+ j& Q1 K5 O9 j9 l
configure: error: pcre library is required
3 ?% T7 i& R/ n3 k4 W+ F解决方法:6 f* c+ N% p3 T: S  L, D! d  j

/ j( E* ?5 b/ `, Iyum install pcre pcre-devel
/ c) I5 a" E9 j' R) Q! c  Z3.没有libxml2
- R3 c3 X- f& ~. M6 k
5 [; I3 u$ m0 }& f* i. X2 R- |) l; X- t9 b
configure: *** xml library not found.
' L3 r  K1 }  u5 [configure: error: libxml2 is required7 R* u7 i4 g4 z, [1 c) J1 P) a
解决方法:
' z: J5 B' w4 q
$ q+ W1 p4 U9 T+ I4 Wyum install  libxml2 libxml2-devel
1 k  Y$ i, a' L" q4.执行 /opt/tengine/sbin/nginx -m 时有警告, q* L5 P- ~9 k) f5 A

; r, B" O: b" T% {, PTengine version: Tengine/2.1.0 (nginx/1.6.2)
1 U5 [+ D( R- e$ t1 L$ nnginx: [warn] ModSecurity: Loaded APR do not match with compiled!: s% N7 D. p. I1 t
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log* u% R9 ]: W- W) u- m) N1 d

8 z8 t( j% D& v! X' u$ v, ^2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.; O1 b8 Y5 X& g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
, n0 O4 a, i/ {8 p9 I$ d2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!; E. _. P! [- @1 j+ z: D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
9 t1 h3 j% z7 w/ k5 x% m. x2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
- q- N# P: j* t+ ^( h6 n2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
: m4 e% n# r- p0 E7 M解决方法,移除低版本的APR (1.3.9)
2 k6 {2 Y% F$ x
6 W3 E0 v6 P4 r! wyum remove apr
( [! Y9 N6 b) R7 J8 \2 `5.Error.log中有: Audit log: Failed to lock global mutex
* I- f: V2 N- G* u, W& C3 d" u  U: C# Y: v5 E- {1 A1 k
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     / n3 F  X: q& V- ]. H! W
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
5 k& @. S" X7 o% q* v" `解决方法:9 C/ A: ?2 L6 ~: Z! R4 P
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:+ S- n+ ^8 Q" x+ N+ b
- @2 o  Q* w; q, Q
SecAuditLogDirMode 0777
$ ~. q5 r1 ^0 Y, m6 W' JSecAuditLogFileMode 0550
: ]9 @+ I) ^, ^2 R2 |SecAuditLogStorageDir /var/log/modsecurity
- g% \- e8 |5 }' X) r# E8 XSecAuditLogType Concurrent, x+ Y* n, P7 Z8 ]5 X: V$ T" b+ m$ Y1 \
参考文章:" h6 {& k% u5 g% W: `; c
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
- O* R5 Q, N$ U* phttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-21 07:44 , Processed in 0.070951 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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