找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11622|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。7 b6 T; J. \) m4 G! A8 [
: Y4 d+ p  D+ f* n
一.准备工作+ A' N! ~2 Z) b5 `

1 k1 u8 }8 W0 O4 Z4 s4 |系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
- Z- Z0 M7 ?& L3 f; ~
. w* \, b6 ^# ]4 B- ]; D  Q. ^tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
" A4 E/ y, i2 N8 k, Z# d! Z: q' b" F, c  Y6 g! _* c) }' n1 _. U
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
9 o* k# `7 L) c% [6 L1 k; @
: U$ s. b" x+ i# \: A$ XOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs0 F# p( a4 W3 H) |$ Q0 B

$ X' V( q8 o( j1 j, t依赖关系:2 R! D$ Q9 U5 D. U; N8 A8 ?
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:! H1 k/ \8 `) V8 J' ]) V9 z

; ^8 Q- p* R! ~yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
, a' O6 m5 W& o0 f! D. Q1 Vmodsecurty依赖的包:pcre httpd-devel libxml2 apr: S, ?1 F: E; a9 ]5 L
8 C9 l4 b2 N9 p7 W
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
2 U, Y& T" v+ r7 P4 S8 S3 o# G; F二.启用standalone模块并编译% W( R( N) w. l5 E8 w

8 H! k9 b6 H3 F# m8 X5 M1 _' x下载modsecurity for nginx 解压,进入解压后目录执行:! A' A1 S5 B, d) [- d7 P- Q; V
4 Q" g. Y' X! K2 R; z
./autogen.sh4 ?% C9 D3 v# D, J
./configure --enable-standalone-module --disable-mlogc2 F+ Y/ e, Y) L" j" Z
make
! j3 X" u! J& i& }) e+ c6 {/ H7 F三.nginx添加modsecurity模块
% b- m7 \9 t7 r; a' x/ F  t- }: h, D% N! y( D8 T- e2 r
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:- k% Q7 r) j  ~, R0 }  d# l7 S
$ M, Z) L* i4 T+ ]4 n  Y
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
* e- y' D/ l$ v. i7 X7 T* kmake && make install% n0 f3 \+ A: M
四.添加规则
8 c: c- `9 }; {1 [9 C/ w9 g0 p
  L% Q$ q3 m7 t4 ^; y! l, Emodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。+ n. B3 ?+ k6 S7 e, Y
2 i( K* \. y# f- v  b' Q' o: D9 m
1.下载OWASP规则:6 X0 o" [9 i4 F
7 n2 ^; z" k7 R7 O$ H
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs# ]& Q0 d$ u* o
, \; H$ W# w- W) @+ O2 n( H
mv owasp-modsecurity-crs /opt/tengine/conf/& Z2 |( r" k1 E" V& _5 Z& a0 b

/ ~5 p) R" |  ^' d1 B' r9 b' x; b* ccd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf: \' w) Y2 y5 x3 P9 @
2.启用OWASP规则:
6 {' S. c9 n# K
8 I8 i8 E- b- z5 @3 ~. ~4 v+ w4 N复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
* S9 E- o; D: k5 f7 P5 C$ c' J: N3 L/ R) g4 f9 l2 |
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
8 ]* Z1 v# N* Y7 D! s/ t. v! P' V. ?: D% K* p
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。1 D! }0 r. s% M" s' j0 n9 C

* t+ q. R; i6 x) CInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf7 e& x- F% O, l1 L
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
" V. F' L# g! Q; A7 e4 U7 x- JInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
/ f# R# ^1 |; U7 GInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf( J8 ^* S0 H: p. E- o  N
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
! H3 Z# _8 W- F! J: y' ^% `Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
( H  C: a! S# {9 M' Y4 ZInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf. |+ T9 Q- I+ ]
五.配置nginx
" v' b, p& R) X  l
- c9 I; O' f. p! G) \, O在需要启用modsecurity的主机的location下面加入下面两行即可:) }, x" h7 @* i
+ V# K( i  s8 _( U
ModSecurityEnabled on;  4 u& ^/ S& r6 K( Y- z- T
ModSecurityConfig modsecurity.conf;9 ~8 F/ _0 d# d: }9 u! w, u
下面是两个示例配置,php虚拟主机:; p# h6 t3 F5 L( z/ u$ }
) l* @' n6 o7 y: f0 w1 A
server {
5 B6 v" u$ L! U$ }7 a2 s, P      listen      80;' T* h" q1 y' J  }! I+ y
      server_name 52os.net www.52os.net;
( h/ W% v" b2 B$ i5 `     
% g) ~0 K  g  S      location ~ \.php$ {. [1 w2 ~) w. `4 T* e& {, X
      ModSecurityEnabled on;  ; J1 Y% S/ n3 s, m1 `7 b
      ModSecurityConfig modsecurity.conf;
! W, B, ^6 X4 w: [0 G, r2 h% N
7 X5 E7 F6 u: h* n      root /web/wordpress;
; y7 u  J8 O" I& L. `      index index.php index.html index.htm;
7 ]  t7 }3 \7 a; F1 J" b  
$ E% I, j# O; Z  h      fastcgi_pass   127.0.0.1:9000;8 Z" p9 b# q# J5 z& E7 g
      fastcgi_index  index.php;
+ z6 Y, x6 d" ]' t+ n8 y  n      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;. O7 y" G" m6 m& Q
      include        fastcgi_params;
' T6 H' m; P: w. D' z      }
' p9 H0 T, Y. c2 [$ Q# K  }2 H! }( F  x8 I( O6 D) N. E5 f- }
upstream负载均衡:# v; X+ a( s4 v) L- q: U0 T+ D
" U% B0 n4 W! x1 K+ \3 k
upstream 52os.net {0 H. \) \+ }* S7 j% g/ i" c
    server 192.168.1.100:8080;" {4 R- Y6 d5 @/ x
    server 192.168.1.101:8080 backup;* z1 r" q4 I7 o3 y& x* l
}9 m6 C) j! J4 v4 ^7 @' o1 N
' [6 B7 |$ h5 D5 v( k
server {
1 {% n' F4 s' p! dlisten 80;
4 ?$ X* N. N0 ~) D% e: ?9 Dserver_name 52os.net www.52os.net;
5 B/ {: l2 G& a+ f9 q% b% V" S7 p3 k$ e5 M1 |( [: \! R
location / {
0 A+ ^2 S8 `" T, n, o- T    ModSecurityEnabled on;  9 T* \7 G  i" n  a3 m) Z
    ModSecurityConfig modsecurity.conf;  3 p- o1 |  C! R6 G5 T
, P: n3 T9 Z$ k3 U
        proxy_pass http://online;
- C0 S! |' o3 G' s0 G1 D& G. G        proxy_redirect         off;
9 s* i$ Q1 G: ~        proxy_set_header Host $host;+ _1 t. A8 Q" R
        proxy_set_header X-Real-IP $remote_addr;
% l: c9 u7 {: x0 v        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;4 [  g, w* x# t3 t: z/ v
    }( @- X, [2 C) ^4 f6 i9 `
}9 l( Z/ p1 z: y7 I. b1 Z& m
六.测试/ }' R* t2 o$ @  U' k! e

8 x; I' g; A( W% \2 B) R我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:1 M2 S+ }( ^( F; j
" G5 ]2 j* _9 b! w$ ^& I% k
<?php
+ ?& C* f. W+ J6 ?' k8 @: ]    phpinfo();    , S3 o$ b9 q- O" _; r( d8 ^
?>
0 S$ f# {5 I) P6 ?在浏览器中访问:
, F4 V0 D$ ]7 z/ \. X/ _. C" q% L$ m$ A; n5 n3 L
http://www.52os.net/phpinfo.php?id=1 正常显示。
9 s6 C) B8 t& p9 |- ehttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
1 G: R7 E1 Y4 `, hhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
' c4 h1 Z9 ~3 N) W( L! A! B& Q说明sql注入和xss已经被过滤了
3 g9 D, w1 E' |& ?& n. o. ^0 I9 f: m8 l
七、安装过程中排错
8 {" l# X( k+ H! s' G9 m3 b+ T
' e1 D- m! y$ {1.缺少APXS会报错
( y- y' z0 ]& V2 }( m9 W
: o9 H4 ^* m( r% Q# ]3 _configure: looking for Apache module support via DSO through APXS
7 r& a' I9 w( Bconfigure: error: couldn't find APXS- U7 @8 ^1 v  _/ ?& J
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。4 n. z' t1 Q: w/ I  O
解决方法:
5 r& h2 H5 U' l+ g+ u8 [% `$ }2 D  b
yum install httpd-devel
' S2 R" a6 o/ R9 ]& N. ?' q2.没有pcre
9 |* x: Y! k! l. y+ r+ S# N; {
5 k3 P, @$ h3 Kconfigure: *** pcre library not found.9 T- z, S+ C8 g# _5 I
configure: error: pcre library is required, M+ M% ?' w- i+ B
解决方法:
( g. i* x7 d/ S, \4 ~% t9 f& `
# |: f5 i! b9 P" Z5 `: Vyum install pcre pcre-devel" F% C: n$ u8 B; c) u: D7 K
3.没有libxml2
  r& A% `% {* L" q8 @8 e& a( A2 c8 Q0 s. j

: s: M1 m  c5 ^; O0 q7 Hconfigure: *** xml library not found.
( ~/ A* {4 u6 n4 N1 tconfigure: error: libxml2 is required
( f, r- w8 h( h0 p$ Z! n. c解决方法:. n9 j5 M' M4 Y5 C; A

$ m) J% c+ O+ h, d- y) m! wyum install  libxml2 libxml2-devel8 _3 N' d: l  a7 c& s
4.执行 /opt/tengine/sbin/nginx -m 时有警告5 w. m9 G! m# N

' H& E( O, r* |) G2 o$ {" gTengine version: Tengine/2.1.0 (nginx/1.6.2)8 y) I8 o) Q; _5 L* v$ T8 Z
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
* G' Y# A5 ?& G原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log7 e8 o' ^) e. i4 f4 x

; `" O* c7 m( H9 C5 M2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.5 i$ n- G" W- j7 }1 o7 \! H/ S8 s: y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"& Z' a+ Z/ x5 `1 B1 z' F3 Y' q) @
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!3 j4 U& e. H& M: A6 r' V! I
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
* ]8 t# {( h" L) S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"+ N  [. c  G3 O, D
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.9 D, E/ a2 q+ s' h+ ^
解决方法,移除低版本的APR (1.3.9)
( F7 \' E$ H! i  O  s: {8 {3 x$ {
: W7 p* O3 ?/ l0 g8 ^, \5 r0 tyum remove apr
4 J8 \3 Y$ t2 T4 S5 {. d, w) l5.Error.log中有: Audit log: Failed to lock global mutex
# c- K& ]/ D8 V' B# K  S& z9 q
' W- I; d% G! ~( T( {! e2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
- i- i3 s; F1 X2 \( t4 B3 R2 c* vglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
0 f* k& c  ~3 r0 Z8 Q4 Y解决方法:
: _- W# [- n3 c9 k编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:% K5 [" u) `  }( f9 h7 M+ S
0 c8 t+ S2 i5 k7 ]' u( A" x- H
SecAuditLogDirMode 0777" t9 B9 |7 X. F* y7 n7 s% k
SecAuditLogFileMode 0550" ?0 X+ a2 r  h5 |6 Z) \
SecAuditLogStorageDir /var/log/modsecurity
3 v8 X4 j/ [2 m$ m* XSecAuditLogType Concurrent* O9 O" u6 U+ Z* v! P% R
参考文章:
( ^1 T3 X& v+ |6 @9 g8 {- b) Khttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
9 [9 O' j2 k9 L  W+ k& T9 Hhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-1 15:56 , Processed in 0.076597 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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