找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12336|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
/ s- R! v% u6 e5 i3 L6 p" j0 ^- j8 v4 k  d& I# x) j( O9 T
一.准备工作' I% r# u5 x2 Z, p$ i& t- z0 C0 i
- h4 _0 M1 v4 f: m: [2 o
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
  Y* p1 N) F3 y" C  ]1 b1 ]
( H; Y6 |! ]3 _' u, X9 n0 ytengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
& P' W5 S8 _6 c
6 V, t& X+ C" T' M% F/ omodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
  L7 |# r% G/ x# L1 O8 c  o3 N$ Y: H4 }- C# J' V. _
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs- x- K9 h, Y: s1 J9 P& e

6 A4 L6 E8 k8 E依赖关系:
; o. E8 p- Y% o- A) rtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:8 T, g! g- {& ]. Y' c, R

/ }& y3 W( ^: a5 f% O% ~6 _yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel2 o6 Y' |4 ]" s: e1 }+ t+ a
modsecurty依赖的包:pcre httpd-devel libxml2 apr
) U3 `3 k4 o, i( h0 c+ B, g- i5 p, ?! J, s
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel6 P$ M6 r$ R) }& y% ^! [2 b
二.启用standalone模块并编译
5 g( w( i/ S! _, e2 X& S4 k* q
. n1 ?! r! d' u. h  D下载modsecurity for nginx 解压,进入解压后目录执行:
8 ?2 m7 E; C0 W- l0 i: z
9 Q6 R7 v* f% B. D, f9 f./autogen.sh7 Y; q2 x% }: s: |5 R  ]. g9 \* c
./configure --enable-standalone-module --disable-mlogc
' O# o( j% q- P4 V5 w7 gmake 8 h. H0 ?/ U1 f% K
三.nginx添加modsecurity模块  w+ K' F- Y. ?* g- P

+ o, u5 \1 h3 a在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:9 Y0 i) ?# Z1 [% y

5 [2 Q8 ~4 C5 E: V./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
; H- q: K* _5 }' {* l0 amake && make install$ L' m* z, n# \. c
四.添加规则
. C% c5 {8 G7 {! {; Q+ [3 C. N; r9 g& c5 U" S
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
4 i9 y3 N2 t; {5 D
5 M4 D9 E/ E4 ]2 ?1.下载OWASP规则:
; p9 O6 R7 O8 K3 m: ^
! f/ q. Z, M) v: w$ Wgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs+ A& c% t4 \0 C

# i6 e3 D6 s6 q9 f1 Ymv owasp-modsecurity-crs /opt/tengine/conf/: G9 ^/ z$ [1 x) V, j

& x9 J- |7 m  {) ucd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf1 q% Q0 U& p% h
2.启用OWASP规则:; G+ \+ D" y5 g( T8 M- E+ f- a9 g  n

$ A1 ?% O1 }, q& h复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。' u% n8 ^! d) q6 `

( k5 H; w! W( q9 Z  ]3 v/ D编辑modsecurity.conf 文件,将SecRuleEngine设置为 on1 Q' j& T5 @! M6 a3 o! }$ o- [

( c3 b5 a/ x9 z& R: \% K/ w8 Mowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
. X& y- s9 \7 D: I8 B) c8 c/ b0 O
( @2 n6 Q' o7 XInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf! t! U$ k( H# J' ~" p7 Y8 p4 _9 R
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf" y4 `' {4 l! Q. n. f' P
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
6 }7 r, h2 `. ~Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
$ _/ h1 k& K- d) A' C- DInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
! }# i/ f# v; ~# P9 h4 iInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
3 D0 r2 g2 k+ @4 O9 ~. _' t5 J2 V- ^Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
$ E' t' W9 G" S7 m9 T5 A2 I8 E4 v* v五.配置nginx
3 e. X5 I. I& y1 s5 I; C# j: C+ Q4 m. n# ^9 L" G$ p
在需要启用modsecurity的主机的location下面加入下面两行即可:
" D4 _4 \* }! I, \, K. \- R1 d! {7 y$ J" [6 Z6 ?" Y9 }$ ^  _
ModSecurityEnabled on;  
% C% V* O$ J% f, M) SModSecurityConfig modsecurity.conf;
$ d" E. C. |: z& c; P: L$ P下面是两个示例配置,php虚拟主机:+ f1 n3 e6 r$ H7 X& V- Y

) v- }. y" w! Z* h- dserver {
. n8 C4 q% r4 T      listen      80;" E& [! E8 O7 ^' J' l3 u
      server_name 52os.net www.52os.net;
5 a  N) j: U: a8 J3 L     8 h' \6 f% D5 y& M: d
      location ~ \.php$ {# f. G. h( Q- H0 H7 e% \
      ModSecurityEnabled on;  
2 K+ T' ?5 Z- S1 E( f, d      ModSecurityConfig modsecurity.conf;
5 H8 h) k) w( O0 ~7 o# B7 W/ q6 e! a
      root /web/wordpress;4 d7 w+ O3 A0 X$ v" x
      index index.php index.html index.htm;
/ u/ k- |. O8 r8 f. ?+ H  ' {. X. O4 D- m7 b
      fastcgi_pass   127.0.0.1:9000;/ Z) z' Y/ [( J7 g. D) R8 H
      fastcgi_index  index.php;
" [; R0 Y) w- _% ]3 b, s) L; A. K      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
  w# w6 X" m4 `5 R$ Q      include        fastcgi_params;" R6 V9 q! B! V
      }
" H! W, \$ w9 z$ }' i  }
7 q4 F+ p* \! ]) wupstream负载均衡:8 D: F, |4 c# I9 i
5 E0 x% P" K! p  G4 m
upstream 52os.net {
: z' @  E+ V0 g% B* z5 _0 b    server 192.168.1.100:8080;
# e8 ]% W4 I+ t1 n: N2 }    server 192.168.1.101:8080 backup;
* q* Q1 E; W3 n, M$ I0 g! L}
& t! A, L% ]1 U9 V
- e  h8 t' Z3 Bserver {
4 S8 U; L' i: h0 v: s6 f) z, mlisten 80;7 X) h  D! [) V8 w, \/ B; E
server_name 52os.net www.52os.net;
0 i9 I; ~# l/ O" N$ R, p* l4 J" k% _1 H! D0 C) _/ o  T
location / {
. s. j% d6 ?: X5 k    ModSecurityEnabled on;  
% ^& C6 b3 ?! W    ModSecurityConfig modsecurity.conf;  
% y7 `# ]$ F& x8 I
$ Q  h7 D6 Z8 C3 _2 e2 V        proxy_pass http://online;+ P& R0 K# q- {5 n3 d8 U
        proxy_redirect         off;
/ \& [# ?9 V1 {1 Z        proxy_set_header Host $host;2 c/ ]1 h" d; Q/ I8 e1 H
        proxy_set_header X-Real-IP $remote_addr;2 g5 v+ Y; f$ c. _1 c
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
6 z( h& P, K6 q. C, f1 K; T4 B# M    }# w) |) ], S/ _; X7 a- h5 F# Z
}
, k2 {2 X5 H6 ]六.测试
( b- d$ h; u! \# i& J: @# h8 X6 q: G. h/ W6 d  e+ |" F
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
- k' {9 B- `! x6 Q  ^7 w2 s  Y4 V' e, W- h
<?php) {" Y" ]& ?$ O0 n+ f  l
    phpinfo();   
- |8 e" w9 K9 Q- V5 Q?>
( X4 U7 A) F5 v  D在浏览器中访问:3 b+ `  X- ~. E4 ]. {* B! e

" b5 o. e' ^5 F* t( nhttp://www.52os.net/phpinfo.php?id=1 正常显示。
4 L4 f" ]3 x5 Xhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。- _, X( i+ Y' _/ f0 k
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。: Z" V8 _! i% {) K0 b5 C$ K* h
说明sql注入和xss已经被过滤了9 D( e( o0 V( Y  ?5 U$ l  P
' R% n' C& n* s+ ?
七、安装过程中排错
# P. \+ ?- j, g7 V4 P. a9 E6 v8 [7 Q% ~- Y
1.缺少APXS会报错5 G) {, K9 C" V) Q
/ h' s; f  o" e( ?* C$ N
configure: looking for Apache module support via DSO through APXS7 T2 \) A7 R% w& k$ l  u5 M' B
configure: error: couldn't find APXS
8 R! o% ?4 x# Z0 ^apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。5 t$ G, w* G. D( C. _8 z8 e7 b
解决方法:
* i% ~' I% g5 Y% F# T6 Z8 g: c/ h; _# Z- J5 e: `. Z
yum install httpd-devel
3 C: j: _2 K$ V; I2.没有pcre+ x( Y; \- q: Z+ P" J
; X9 d& S4 t5 C1 `5 d2 Q/ L
configure: *** pcre library not found.
# X" |( ^6 K2 i& r( u/ z+ c! {configure: error: pcre library is required
5 m) q+ d& _: N2 U* X4 P/ b解决方法:+ V, I  a$ F; c! M* i/ W
2 N5 C( ^+ ]! U0 I
yum install pcre pcre-devel2 I6 g. n: D  n( a3 N6 ^! _1 v! \
3.没有libxml2
' D4 R: @3 S1 D0 h. ?# u, P. p* d4 @0 l; u$ s
4 k+ W! ~* F6 j9 {5 H7 P
configure: *** xml library not found.! L6 d( \! ~" e& y) _
configure: error: libxml2 is required2 W3 Y" N1 X; c/ g4 r7 K- |9 z
解决方法:
7 U' G8 R; _6 ^9 T+ q
& }8 m/ E3 \. }0 Q9 Syum install  libxml2 libxml2-devel
5 a) v! `- X0 U+ t: o& p$ }4.执行 /opt/tengine/sbin/nginx -m 时有警告, S; u, ?3 H9 `: a
" r' B" B, ]! W! x# f
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
8 v0 _. i% s7 C/ lnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
) m6 x. ]+ ?. B  m3 z: }原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log" n9 \) n1 ^3 A9 O
) Z/ w: n5 O1 N, m8 d4 e, q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
  ~  T; ~2 i1 Q. M' O# }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"& C' T% ]& ?( h  E" w
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!) \) N7 s! f# ~' r
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05") V+ U; V/ H+ g8 f+ B- s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
2 K3 C, R5 B9 q) @5 Q2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.. p1 r' I: e2 Q0 x1 V; s6 n
解决方法,移除低版本的APR (1.3.9)
4 |+ V+ U+ v& h8 Y2 _
; F  b) d( ^3 j+ b6 iyum remove apr
( q9 g! }1 y: U$ b5 ?) _5 [- j5.Error.log中有: Audit log: Failed to lock global mutex
4 ?# ]' j# u: p, \& b% x
0 N# e3 P9 \5 @( W) }5 H  p. i2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
5 U, w( Z* p* h3 pglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
$ y' L" k, v$ G, M* }7 P1 M6 L解决方法:/ m& \* D- B4 ^' I
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
+ t) t! A$ l) ~5 z
0 e7 J2 U7 U* @" Y1 k& m6 YSecAuditLogDirMode 0777
, s. U2 [- B. q& l0 OSecAuditLogFileMode 0550
% ]2 p7 U3 d% ~3 h! CSecAuditLogStorageDir /var/log/modsecurity
: t5 J0 P6 O+ R0 SSecAuditLogType Concurrent/ @+ L& o& _& p+ O6 k, D% _
参考文章:/ _6 p" f4 M! y3 B7 L, B
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
6 @0 @% a8 _  u) shttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-28 04:32 , Processed in 0.079047 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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