找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13326|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
. w8 H$ Z1 M! [; T" ^
: \6 e' {7 y: L  c" v, I' Z1 `一.准备工作
- r9 I: N8 d3 e8 Q+ O  A: ~- i9 t$ m1 G/ p; D, v- F
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0  G' R4 v* Q& C

3 X$ j1 W/ f" l+ r) ?8 `6 btengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz( ]3 z7 ]( ~8 d1 F3 z) z* @

# G/ S$ z) m4 X$ S: O9 u, ~modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
! ]8 o4 z4 p# p! B0 d
3 [+ I- b' g$ HOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
( ^5 w/ f4 s1 z+ q5 ]) J" Y* m6 j- L) I$ o* g+ k3 W9 m9 I1 P% w
依赖关系:, R: j; ?: p% S+ {8 P
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
: ^% e7 ~2 |: u, f9 {( i% O1 D; R3 P) k  G; t9 K3 X$ _
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel% A( B/ T3 Y& [8 q* [& a% {
modsecurty依赖的包:pcre httpd-devel libxml2 apr
! t2 D6 g5 ]5 n+ T, o% P* {+ ]1 x/ U+ _/ n  y: k$ ^
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
7 U% K( v1 l) J/ f二.启用standalone模块并编译
3 k0 L/ i/ r: C% B9 j  ?' Q! @% o; N& T3 Z1 k( y# U+ k
下载modsecurity for nginx 解压,进入解压后目录执行:$ D# N, X2 }9 T0 c" }: P* @5 W

1 V# A( T( [3 M4 x0 [- i2 d./autogen.sh  ]9 J8 u7 J. ]- G% ^: L
./configure --enable-standalone-module --disable-mlogc
% o9 v2 ?  _- ~4 Qmake
2 j" A% h) X; f8 F. t6 [三.nginx添加modsecurity模块
8 @& N  {" r. \/ s
8 Y% g; e7 j' U" b' H9 w  q在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:$ \7 `$ @( r4 {" H. `$ K! |. m

5 t( v8 N* E2 ?, n' v0 c* @./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine1 o$ Z/ ?5 T% A$ `
make && make install
$ }  C% |8 n" L% s: T6 o; Y四.添加规则
- H3 B* E4 g0 @  _+ E# k
) A5 i9 c; A$ `% W/ imodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。7 w8 b3 R9 N0 q# s6 r4 x; @9 Q' ]9 e

5 O) x) @5 z" e. Y5 i- B1.下载OWASP规则:9 c9 Q/ B, T6 ?" f3 d" H8 S  J
# A2 [5 C) j9 E& m; {9 n# h
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
; q# I/ E1 P0 _, r
6 j; D. y7 [3 n' G# h) H  dmv owasp-modsecurity-crs /opt/tengine/conf/
3 C) C. f: q2 w4 P
' l& F3 ~! T. Y0 H! ~cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
" Z/ e$ J4 j! m2.启用OWASP规则:% x# N+ r/ k3 S  n- `
. M1 ~: C+ r8 X. W* W8 e
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
) p$ a0 R. A( y! i& e* b) z" e3 @& k( S; E' l& J
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
% c/ P' o: D4 I
% {3 N$ k4 b0 o' ?; H% Uowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。  A4 e. [# r6 ~8 S
; V4 I$ m0 F2 Q: ~& O' d
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
# W% q8 N% |) |+ N1 O: HInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
+ r! n) Q. Q* i0 [! h4 q, @, VInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
' h6 u8 ^" z; Y7 ~Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf! M1 J# C1 D. }, r" T
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf% `8 R) X3 N: M  c& P
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf! u* }& s& w& l5 p( c; A
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
* M' Y/ F+ H5 z0 Y五.配置nginx
& d; t- L* r2 U/ z: w9 x( [
2 ]* v$ j; }5 d  ]在需要启用modsecurity的主机的location下面加入下面两行即可:
$ g8 _) n" S; W- Q
0 q, H# c: ?9 k- D7 PModSecurityEnabled on;  & R. g0 V2 }7 y
ModSecurityConfig modsecurity.conf;: D! v1 n* H+ H9 |2 S7 @: c% y
下面是两个示例配置,php虚拟主机:! G6 N$ Y  O+ b" o, e5 t8 C0 R8 ?5 e

7 p! b8 `9 R0 v: N$ }, tserver {
8 _$ x+ M+ L# P- c      listen      80;
" P" K2 h4 p& r& i      server_name 52os.net www.52os.net;
2 r4 M  T1 G) \9 ]/ ~9 c     
: @# a4 D8 \& @2 m! q      location ~ \.php$ {) i4 F2 [- |; I! o
      ModSecurityEnabled on;  0 E2 s1 K+ F# A( Q; o
      ModSecurityConfig modsecurity.conf;. I5 u7 ~* z% h5 E2 s( x. b

8 |" V9 Q) {. z5 k# L( |      root /web/wordpress;
) U7 w. g0 f4 Z1 ?2 Z; d      index index.php index.html index.htm;1 K- C$ r" |/ p( f& @2 v8 K
  * {+ p( L6 M# ^' b6 }
      fastcgi_pass   127.0.0.1:9000;
- ]  i2 u5 z# ~& ^% \, Q      fastcgi_index  index.php;0 D9 H2 T' {8 j, J' q
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;5 m: x; i: l" ~
      include        fastcgi_params;4 @% d* A, M. t2 I
      }  d& `( G! b4 R7 A" k! m  A
  }
3 Y% g' |- m# u4 M  |. Yupstream负载均衡:
( X) T! k  |3 S; O+ `7 i0 y5 Y
upstream 52os.net {8 }* ]1 V4 `$ @6 c2 l
    server 192.168.1.100:8080;
0 Z1 x0 Z1 D9 S3 `    server 192.168.1.101:8080 backup;+ N' M: S1 R2 ^; w" {( _; }
}
+ |9 e9 {- b# f
  t% }& X' O5 Aserver {& f8 P+ N- S5 y$ I) H/ D  @
listen 80;
% `& L9 U% N: |' j, [6 e; zserver_name 52os.net www.52os.net;
) A  \- R9 S9 p: |( d9 i# |  X0 g5 R- J) \- `* S% X
location / {
- ]+ S4 P+ ?1 m; V    ModSecurityEnabled on;  " i- L3 n! K3 L7 u
    ModSecurityConfig modsecurity.conf;  ; h6 h. ^/ F. Y8 c
3 z5 l" O: G9 ?0 D7 p
        proxy_pass http://online;+ p/ Z% l, S) M' a5 e
        proxy_redirect         off;" ^2 e! Z: k6 v! w8 Z0 ~
        proxy_set_header Host $host;9 a  O( e5 a3 O/ k$ {2 ~& _  O7 t  m
        proxy_set_header X-Real-IP $remote_addr;$ f4 z& e/ [8 d
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
' u' I8 v8 p2 ^  o    }
( F7 G1 V# w$ F% V: {1 r) {}, k. j" `/ q2 r& u+ L
六.测试6 l* D5 F& [/ `+ Q8 M) H2 [% O- n/ e

/ w4 L* w% C4 I2 v/ T我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:0 M% h* i8 [8 K& ~8 w$ G' ]

; C2 U) v* ^9 ]4 X" u<?php
" Y/ b7 @7 N( b' V- l    phpinfo();    6 o' P* A- F  u; J  n/ T" E
?>. E9 y  a7 m' i4 {' R1 G% w
在浏览器中访问:
, C! n  E/ x$ U8 L2 X3 `$ Y
  t# v. V# L4 W- n+ a4 Rhttp://www.52os.net/phpinfo.php?id=1 正常显示。0 \; j8 v  O' k, y5 i+ `6 x2 ^
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
6 p; R4 R# V5 qhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。; H4 f% g1 B/ o
说明sql注入和xss已经被过滤了* p3 ?- G( b6 w) J: ?/ T2 N) z

. P7 y" \9 @, ]七、安装过程中排错
; n1 N; C/ Q8 F& v1 X
0 Y4 a5 @$ G5 p  @1.缺少APXS会报错7 i0 n6 t# F# M
2 F! h- T4 J* g
configure: looking for Apache module support via DSO through APXS
& X! r: ~. q. z; O% u# nconfigure: error: couldn't find APXS
, ^! E' M/ B% \# t9 X4 N* ?apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
) y+ N" O6 Z- m2 ~解决方法:# S- A1 p0 k9 s( [: I7 p8 v

* x3 J+ o, F4 I8 q+ oyum install httpd-devel  ?1 G4 B/ r& v
2.没有pcre
0 X. y* J9 ~1 D$ h5 r" E
  [. `# n" j- u9 w7 ^, n  Uconfigure: *** pcre library not found.
; B. z  x* z9 J, p2 X# F* Q, zconfigure: error: pcre library is required
5 Y7 z9 j2 X$ ~9 h* d( l( r解决方法:# S7 Z- J7 b. P) Z5 L  e
/ W0 b) S+ r+ m* C
yum install pcre pcre-devel
" ^4 p+ d" F+ ^6 d8 F3.没有libxml2- h4 N% [* O: e2 Q3 H
' z  O5 A  }; a9 _* t

' T$ i6 Z: @4 O! wconfigure: *** xml library not found.7 ^% y0 t! A6 G
configure: error: libxml2 is required* N) ?! e3 N7 w$ t
解决方法:
6 w. m) b. V$ g2 o- G1 ?3 g
8 ~+ Z" S2 _& v( R: F; G* j% fyum install  libxml2 libxml2-devel" |8 l# V* S0 m/ V
4.执行 /opt/tengine/sbin/nginx -m 时有警告! g' e) Q( V! ]8 P3 ?& Y' P3 W7 f

( a1 L3 R) f& o( F2 s+ y! ]Tengine version: Tengine/2.1.0 (nginx/1.6.2)3 g; b! C! n" Q# D1 _" O3 s/ j' V
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
( C+ C* l& E6 ]1 n- k# e6 R$ f7 n原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
# I, n! E7 B. K8 z  f1 w
" ^9 e  B0 ^; ]2 O2 H2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured." x3 E- o( {0 k" o* {6 _
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"5 e. o" i9 q, I4 C1 T( c
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
6 v; T( |) \8 f# A* u2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"8 y; g  c- Y3 U3 ?$ o( @5 \
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"  w) V9 K4 d1 t1 s
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
; M& k1 n* }  Y# I' K) _解决方法,移除低版本的APR (1.3.9)
1 ~! e* w: N$ B1 Y
$ I1 A/ t( W9 y8 a( cyum remove apr
, Z( I4 d$ @! K- q% j! Q1 @5.Error.log中有: Audit log: Failed to lock global mutex7 O1 ~9 l: _0 u% q. P! @* Y
/ o7 t6 h0 I2 h) R
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
: r# j3 s! @  u3 |( `6 k$ Xglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]& f- r/ e  [8 @% j
解决方法:
/ \, R/ _& x, `$ q4 |: ?2 F+ [编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
; ^/ Q: z1 P0 }' n; u
+ R7 J* r' i; G( FSecAuditLogDirMode 07779 ]2 c: Z/ R1 C
SecAuditLogFileMode 0550: x4 V% R: F8 a6 H* ?6 m$ x) o% d; E1 [5 \
SecAuditLogStorageDir /var/log/modsecurity8 [& O9 L& G& ]  f
SecAuditLogType Concurrent
: g1 ^- A  E8 f. Z( x' {  A8 x$ z参考文章:
5 t& R6 I; F. R9 ihttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX, g% c& X6 w* l% l% X
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-12-4 12:16 , Processed in 0.072553 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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