找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10722|回复: 0

nginx配合modsecurity实现WAF功能

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

, a  k, g+ r3 u8 ^; i; b% A一.准备工作
( \# \1 `& J* Y+ S
! P- O  A! h, @4 L系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
+ |1 ~5 k- X: `0 @8 u. B
# Q) M( V( j) Utengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
9 G2 l$ n* @+ n% e, R  `+ Y. u& z$ |! j
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz6 w$ z7 O. N% K* g3 S' r. W) C
/ e; S3 Z1 W& `- v) e; o
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
, u; B5 W5 }. U: V. ~
3 U& ?1 w/ f! b  ^依赖关系:
0 ?+ o2 t* Z% F0 h7 l" mtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:/ g3 W& g; Z* E
7 \& v* S% w6 i  C
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
- p) W; {. T' S2 m) u  r- r5 }! @modsecurty依赖的包:pcre httpd-devel libxml2 apr+ A6 K/ P8 ^( ^! v
+ T. Q4 G4 v3 y& O( }, g. B& o. @
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
' H  u1 |; p% J3 [4 a0 B二.启用standalone模块并编译5 J0 `/ o  n2 x) ~* T, @7 v5 i

0 m7 ~" J$ N8 C3 ~下载modsecurity for nginx 解压,进入解压后目录执行:7 K  T9 J2 E# v6 @. X4 S

- c( |4 E  p( p2 H5 _4 V./autogen.sh2 t& F6 E7 }, r$ N2 R! y
./configure --enable-standalone-module --disable-mlogc
- |) P4 T+ ?4 T" L7 tmake 3 Z$ Y, o1 j  E; G) J1 F
三.nginx添加modsecurity模块; ~0 J3 l# S0 M" y7 O

! A: s9 E4 K& t2 R& y在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
: M5 G% I' x7 @" q! }& j' Q8 T# [+ Z- ^% h6 Q
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine" g1 x( x2 k7 j# w8 {+ V1 U( J
make && make install
# x2 w$ T9 j% u2 l5 N四.添加规则
+ u- v5 S. N6 j7 q- g9 X
" R/ r7 {. D; dmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
0 Z% V5 S2 n1 U: [5 u$ {/ D) y) T2 i
1.下载OWASP规则:! s" a. S5 R5 I# s  ~; E5 e3 o* a2 }

  @  }, q6 o# X5 \; G( jgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
) M/ A8 L% Q& d6 @
$ T; q: r. c- I- x0 R6 }  C4 K/ I: H) ?mv owasp-modsecurity-crs /opt/tengine/conf/. x# `8 j" f' k# r! G" [. `
* e8 O& W" m5 W) {+ q$ z7 P, f
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
$ A8 W$ y5 f4 [8 Y  S2.启用OWASP规则:
  i( o3 y4 t, }9 `
& x' @6 d# C6 q8 Z0 ?+ M( x复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。3 L* r3 f: Y% s* J7 q) F+ C

$ x+ {& X$ B. H' [6 ?# U5 c! ~编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
# }/ ^7 C" q9 o* T  R, i8 `" J4 I. r) x* g/ N4 Y0 ^0 P
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。+ D3 \) {; W. j: |% u

, x/ \* K$ S0 g3 A: w' FInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
" a& y5 a1 n& I, C: _( L0 EInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf! B0 W, u6 p3 Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf, h/ [- }6 C) G! S4 I
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf" B2 h2 O' F# x- G' v5 F" c; R
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
5 O7 e' v" ~% gInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf$ a1 W" G# u! D5 o. Z+ q
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf* A/ [  v1 S- p( K  `& q
五.配置nginx/ h6 O- s2 [9 I: ]- w4 z

& q. K# V6 {7 @( J" F/ l7 i在需要启用modsecurity的主机的location下面加入下面两行即可:
% P9 ~& p# X( g3 k8 R* F* v- ]4 K# V# s4 g" x
ModSecurityEnabled on;  
1 g0 t- W! n: j# E0 KModSecurityConfig modsecurity.conf;0 ?- c7 g/ T1 d
下面是两个示例配置,php虚拟主机:
; ?; M% v; h' x8 j% w7 b3 t5 A6 U; y6 a* m4 z
server {
7 E8 H( `: r: m0 r" V      listen      80;2 ]0 X, p% I" ^2 g  P
      server_name 52os.net www.52os.net;, j! X2 Q' w$ h* |  S3 g) Z6 S
     
* G1 t8 c! @! I6 F$ n; G: K2 N6 ]      location ~ \.php$ {
2 P; Q# C2 O0 ?" m9 _" D, z, [8 ~      ModSecurityEnabled on;  2 T. m2 A; m$ r. x" e5 ^0 e( p$ l
      ModSecurityConfig modsecurity.conf;
, O; B. ~% `" s3 C2 I) e1 u  R0 i6 O, y0 k
      root /web/wordpress;  m# m, a, K0 O" U3 ~' @# F
      index index.php index.html index.htm;
" k: e; V, {8 z8 R* j8 I8 D  2 I. Z$ O- B8 f1 r( ^
      fastcgi_pass   127.0.0.1:9000;2 P$ M1 [. F1 T, v
      fastcgi_index  index.php;
% C% f+ O" }4 \& [/ F* G# `& c      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;* H0 `; [' m/ d9 {: r& I
      include        fastcgi_params;
* E( C# z% j/ k7 ~      }5 p+ k  I7 F! w  \4 T+ L
  }$ s8 E* d) |3 g% o' N
upstream负载均衡:( X: X, \) k5 ~" A# k6 G+ e3 m

/ W" m4 W& i5 j9 R( Rupstream 52os.net {
- l# Y1 b& D; E5 g    server 192.168.1.100:8080;- [4 w8 Y" u5 b" y' D  q
    server 192.168.1.101:8080 backup;
$ t! C8 B. M- u" E: c9 ^/ }. H}0 V" D% p& F6 x
: w, Q8 W& r- V* E
server {
/ J  |6 w# h& J3 W0 B" Llisten 80;
% e5 Q4 p1 y" y$ m7 H9 ~' r( bserver_name 52os.net www.52os.net;
0 h6 q) U$ j" R! z" `' s
; L2 f# Y* `, L/ P, |  W- Ulocation / {) U; E  a" m5 [1 P+ ^
    ModSecurityEnabled on;  ; V$ b6 {" K& n: q0 w7 b
    ModSecurityConfig modsecurity.conf;  - e* d, U# T! U" Z* e

, w3 Q2 Y: f6 W        proxy_pass http://online;- y6 b( {. G6 p  b, E2 P- C
        proxy_redirect         off;
  f. @7 e4 p* }6 @" \; J4 d' l  M        proxy_set_header Host $host;
! R% h6 d' ^$ s  v# O+ H        proxy_set_header X-Real-IP $remote_addr;# \* r* c% {# T1 G: v; k: S
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
1 L8 ~4 z- d4 I* F6 f, L6 _! \    }
8 N- i3 t5 D( M. y0 `4 r  R}
' E! u9 Z! s' \, C5 `六.测试+ @( b& F5 ~5 Y0 r% ~9 A& v
' b- P7 X1 |2 V" B$ H0 b& A
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
- f* @' m. S" J6 a! V- o8 T! j5 A7 V& y6 t' o9 b4 a- p' A
<?php
$ s4 E- g' J. F) T0 A# F    phpinfo();   
- ?& s( b$ l5 U5 s' u?>: p0 `, _& \% e4 y6 L5 t3 E
在浏览器中访问:
- @8 p$ q2 ]5 M5 m& t1 o
( n( _. G5 z5 `8 D4 l8 C$ c) ihttp://www.52os.net/phpinfo.php?id=1 正常显示。
0 @: y0 R) T, O3 z% Yhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
/ q9 G4 D: m2 m3 khttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
8 R2 w& f' `' {7 j' m说明sql注入和xss已经被过滤了
0 @' ?" b- e) a6 v6 B4 D0 o' M3 S* s2 ]$ q
七、安装过程中排错
( i9 Z$ b8 C& I7 c) r7 a* @- s) h$ k* c5 Z+ q
1.缺少APXS会报错/ s, e3 Z3 X$ W& K
8 y# O/ g5 W5 K/ ~+ F) l. V
configure: looking for Apache module support via DSO through APXS/ n) M% y- |6 B' A
configure: error: couldn't find APXS
" G; {* M7 h* p2 eapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。) j; z& h' d: y/ k% C
解决方法:/ h0 Y8 c6 \4 ^5 G& }/ O& n! x
" v7 g: r/ Y+ {1 s$ a7 F
yum install httpd-devel
! g3 d9 z6 \( z9 a2.没有pcre+ Z. H# ~' F; U/ A, E) t0 t
/ J' A8 ^  C3 ]( B7 X
configure: *** pcre library not found.* }* [. o, I4 t; D
configure: error: pcre library is required
- f- S% x  O( w6 U, X: l解决方法:
' G4 S/ N6 @9 f- i  V" O: m4 u' n& M  {+ A$ v& z
yum install pcre pcre-devel. g- h' [5 ?# W' }
3.没有libxml22 ?$ C$ w- B. f9 t

! S1 v$ @/ Z* j2 Q0 ~1 H4 p% _0 T# P1 Q5 c* j) {& D
configure: *** xml library not found.
; y( o6 h& M. G- mconfigure: error: libxml2 is required: v7 ?+ D1 r  q
解决方法:
# z# I  j$ o4 @% M! V2 f5 U) g
8 ?' A- B/ z; H( S2 zyum install  libxml2 libxml2-devel
! S; M! g- ]6 \9 w) _+ L! ^4.执行 /opt/tengine/sbin/nginx -m 时有警告8 S% v0 g* P8 Q. Q9 _

& H( Y: i- C" v3 ^4 hTengine version: Tengine/2.1.0 (nginx/1.6.2)
; Z: h) g" G; n* k$ N+ inginx: [warn] ModSecurity: Loaded APR do not match with compiled!6 d' U8 j  \5 E4 V9 x
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log! C  W! l3 h5 q; ~

7 Q- s' W6 j( @* D: A6 q9 i2 |2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
- E& t: V# q' v! n2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
, {; V0 o4 ?7 S" v4 }8 \( {: V2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!/ H2 ], I! k7 _- |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
2 i. X1 o2 D7 E, x5 D- S6 \2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6": D! l. k- p5 z, O
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.& Q* |  w+ \. L* }; a' m8 x
解决方法,移除低版本的APR (1.3.9)# T- W& j- n) R4 w* \4 M" v  m+ d
  E1 N3 Z% O( w6 ^5 N
yum remove apr& z0 Y2 W) j* u: |& B% s* d3 K) D
5.Error.log中有: Audit log: Failed to lock global mutex5 h1 R8 t. e' j

& D0 w$ ^( n% z6 C2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
. |! B+ _2 ^* Jglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]9 H& A2 o: d8 o" @/ r
解决方法:, k3 d0 `! a( {9 V( ]
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:7 l, `) e  l* Q

2 x: ]  E8 y8 b9 m8 lSecAuditLogDirMode 0777
" z5 e) G' _0 \4 XSecAuditLogFileMode 0550
7 W  k) z* r. Q/ L& K0 _, o$ y6 w& gSecAuditLogStorageDir /var/log/modsecurity
* Q  g, l) U5 L# Q6 \SecAuditLogType Concurrent4 \4 d6 E; m) P9 i1 v
参考文章:
0 ~3 G& p; N' T$ vhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
# g3 g! K  T8 r0 `' q6 Nhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-4-21 06:29 , Processed in 0.071917 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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