找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12467|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
0 Q7 w9 B& I* w4 ]# z) S& `' B0 E+ `7 t. F8 B- {7 d
一.准备工作
7 ^; u& e, Z) P# U& L- ?
0 z* o: R$ R0 x% k2 e  }! z4 O系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
7 i" E: y5 f" y. \$ t" x8 g$ {
# l6 _% U; {0 n8 z4 Ttengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
5 y2 n8 i" m$ X& b; F% a9 N# K2 i+ P. s8 ?& t6 S
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz# L) ~7 j. F5 |  b" ~

$ u" z3 Z- U& ^, p. p& cOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs0 u4 d+ j4 _, p- T! R
9 P4 z5 ^4 o. ~! i# d+ r1 s
依赖关系:
, _9 B6 S1 I/ N4 R2 m( X1 `tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:$ d$ }2 `* y! h- U
1 T+ ~: T% k2 f9 W
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel( I- b* o5 d7 Z" ?7 a3 p6 Y3 s
modsecurty依赖的包:pcre httpd-devel libxml2 apr
# l7 s" c- Y9 q' h- k8 b/ E! i2 ]$ |0 O* [* Y) _! ?! \
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel" X$ k* v2 B$ K- }
二.启用standalone模块并编译
5 E# d* U6 c, s8 @3 }" f
' X' \& r4 c; |2 N  p, Q5 P下载modsecurity for nginx 解压,进入解压后目录执行:0 p+ b; G( Q& i6 c

. [0 L  s( [2 e! m$ O* d./autogen.sh
6 q) v3 y' c* T4 k( \./configure --enable-standalone-module --disable-mlogc
0 B# D# k6 d6 i( o/ l; Vmake
4 f+ K1 U! O/ d8 Q5 q三.nginx添加modsecurity模块
1 u" Y  C* t# q( b, h6 Z) q( ?: R% X' s
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:3 a2 @1 w, D' @3 B! k3 \

, s, F+ n6 T# o  U6 H./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine% x  a! O9 S( |& }  M
make && make install
0 V" m% f4 G" `四.添加规则% X/ r# E; k- u$ [0 Q: ~
( c3 ^1 i* p, m6 @4 C& s
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。0 P- D0 F  X. J* N- f& D
, g0 {# a& B! h) h: {
1.下载OWASP规则:
: {& s$ S9 J& i
( `+ j. r7 A) Agit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
, A5 A' c8 d$ v) d2 {6 i+ r( R  D7 N  r3 b& X
mv owasp-modsecurity-crs /opt/tengine/conf// U% @4 o! T' n% ~  G! b9 `

* D' S3 L& u# i: B5 lcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf9 T# B9 h/ ^  x3 J- Z4 D
2.启用OWASP规则:
0 M5 d# c+ @( O* i7 `% I# J; d" d$ _. Q# R- A0 V* f( L7 ~. S
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
; g; Q. u, o4 r- @  A8 v* m+ n: r; P# a7 g/ @3 m
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
) t- z5 K7 {2 `) \) X" a9 Y7 ~8 ~: z. E/ R. c2 T
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。4 O2 c# I; O2 y- L9 X

  y1 P) P+ S. J2 _* k" jInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
# Y% A7 ^+ }' @. d2 k1 p2 VInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf) S  g% C5 D" n9 ?: i. j! g+ B
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf. A3 O7 O: V  n
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf  w. T# ?) Q! w. u+ D$ ~/ L; K
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf  ?$ a4 D4 q0 J  w6 \- a
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
- G' Z" j* W) w% uInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
0 d! m( y( b; T1 D* ^+ T五.配置nginx
) ?1 s8 V( l/ B5 A( s& c" G- `4 p" U. G( t
在需要启用modsecurity的主机的location下面加入下面两行即可:
7 g5 x% O( \8 n' C+ j# V. {* Y, n1 u( g7 G
ModSecurityEnabled on;  / Z/ `. D9 v( _2 R5 K  {% k
ModSecurityConfig modsecurity.conf;
! ~7 R* [, t3 }; p4 w$ _0 @9 ?; V6 s% x下面是两个示例配置,php虚拟主机:
$ ]" [( t3 K2 U& c
- P) @5 l2 o6 o) o% o0 m4 fserver {
2 d  M) r) `- v; L( {7 Y8 k8 n# }: u, k      listen      80;
+ Z8 C+ \" a4 X$ X# }      server_name 52os.net www.52os.net;6 Y7 P1 h6 T; X( W- j# ]0 N1 [5 {
     
: L2 x/ `: L2 s+ T( Y% t( V      location ~ \.php$ {0 |9 {+ g4 `4 V
      ModSecurityEnabled on;  
$ v& `8 D# h1 s7 A  f5 a! o8 T      ModSecurityConfig modsecurity.conf;  A! S3 [6 x( J- J/ v- k6 `$ u

- [# L4 g0 O9 }% ?$ J      root /web/wordpress;
$ U5 c/ i/ @. N- {      index index.php index.html index.htm;
( q3 B9 u8 x) ]- W* E0 m  " a; h% g: i4 G9 H* o
      fastcgi_pass   127.0.0.1:9000;: d/ L1 q# g5 `7 H' \
      fastcgi_index  index.php;+ \7 F" g1 }) g: v8 ]6 H) B% p
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;! O  I9 Q3 m5 A. X
      include        fastcgi_params;  N3 p/ N5 `1 c2 Z$ N4 R: B
      }( u3 H8 O& H# K$ W3 d
  }
! g( c2 q; P4 x. D# Pupstream负载均衡:
. Z4 T3 p) C! N/ W: @  H
9 H6 `6 B1 c; x8 q& eupstream 52os.net {+ }* @! I, A3 o$ \. B
    server 192.168.1.100:8080;
( i- ~& `: K& l7 u4 L$ Y    server 192.168.1.101:8080 backup;: q% m* t9 `; {9 Y3 u, v
}$ P2 z- {8 |9 U( z+ c! D" ^

9 p" ^5 p& n3 H7 l0 s  M" U7 U" A3 Gserver {4 S; ~) t4 G$ C' g/ j8 Y
listen 80;
" N9 w* C( K# b! X* G" wserver_name 52os.net www.52os.net;, U4 D& M' m. L: D+ X, A
3 `; z/ I/ p& ?
location / {5 L+ r/ S; o  N' d) A
    ModSecurityEnabled on;  
* p8 ?7 J1 |: H6 @; v, Q, `    ModSecurityConfig modsecurity.conf;  
& g# }/ x! J4 D2 U1 }5 O+ ~5 @, O1 e
        proxy_pass http://online;
  w( Z: v6 Y6 A7 ?" H        proxy_redirect         off;
4 I) d( H6 D; X6 J: B        proxy_set_header Host $host;
' u  _0 z6 Y8 s+ y0 v7 p        proxy_set_header X-Real-IP $remote_addr;
% R+ M% Q$ j- ~$ O* r/ C+ N        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
( V: z. Z( O8 ]: G- E% z    }8 I5 A$ ~$ M2 B8 ?/ k5 p/ q
}
* ~9 N4 {( a2 }* x' h& C六.测试
" i* V  L2 v9 H9 E+ D# a/ u1 V- }
3 n$ z# x% @$ b8 r我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
3 C% ?! m- A" w5 _" A+ H) |5 P) {" r2 N% k( N! `  j& f
<?php
' _9 L2 M( i: J) x% q' K! D    phpinfo();   
; `6 q# O: P9 h. f?>  u0 B; g) v/ `0 r
在浏览器中访问:
% W$ _/ T3 |6 _% C! u
9 u; M: j3 @- F! U$ Nhttp://www.52os.net/phpinfo.php?id=1 正常显示。
% `$ w1 z/ y9 X2 c$ A2 x/ }, l  Mhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
) |" q2 O  e) Y. k) Y' hhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
) q1 B: ^$ _0 {. b' h& L; j说明sql注入和xss已经被过滤了8 n$ y( |7 f  A

$ z  c; _$ o7 S- W3 f, c4 E  N七、安装过程中排错+ T- G: \- Y' [' b3 {! z

0 C3 M' }( L8 `) S4 Q1.缺少APXS会报错
. s3 \4 l( [5 y$ @5 O: Z4 ~9 Q8 G$ f8 ~% N+ |% ~1 c
configure: looking for Apache module support via DSO through APXS
% |5 O# {& V. M, e3 ~configure: error: couldn't find APXS8 C. j  I+ t) b: I
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
9 ^# q  ]% \5 ^4 o' H: h解决方法:  f4 o9 ?' p# N% n# G

8 b  ~4 h. I5 X* t1 O/ dyum install httpd-devel
3 Z! P5 T; U" J0 J2.没有pcre' ^1 W- C  l/ t0 G7 O. ]( O

- G# D: [4 q$ ?$ ?. \4 ^+ O! B3 Econfigure: *** pcre library not found.
2 B8 n! ]  K( B# _/ m( q) ]configure: error: pcre library is required
! g- q4 ^7 I4 R, e! N, c/ B/ U6 Q5 s解决方法:
& v7 @9 b) X, `) L: z; |8 v& A' k$ K& J" Y) }
yum install pcre pcre-devel; q6 Y' B# T# \$ k6 ^
3.没有libxml2
* C8 h/ G' u! V( k' R
9 h) Q2 _/ M. ~  G) ?; \- _7 ^# Q+ E5 f* H7 ]
configure: *** xml library not found.& c* s( r0 m& X6 [( U
configure: error: libxml2 is required
6 l9 s4 x" w3 \* H解决方法:3 X! n  m7 n1 U

3 B. H( i' i$ T- Kyum install  libxml2 libxml2-devel9 ^0 @4 \) R% M# e- Q
4.执行 /opt/tengine/sbin/nginx -m 时有警告
) L  e" i- z! N/ ?) o; Q# r- |; }1 a7 {$ Z5 M
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
. C. i2 }; p" l# b% xnginx: [warn] ModSecurity: Loaded APR do not match with compiled!8 B) W4 ]) Y  K/ N( g+ _
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
$ p7 d. g. k% W; l0 I( X  [
# J, ^( o8 W/ T/ M2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
( l, g) o, y: {. X& @3 G4 {2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"0 n) a# e5 c; ^% E9 X0 ^; I
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
+ ^% Q8 Z3 l% k% {7 S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
- t, w) R4 ]5 J* E2 I7 }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
: ]0 L1 |' O: G$ U, k3 V/ N4 ^9 r2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
3 ^( r0 J) J3 p/ t% q+ P解决方法,移除低版本的APR (1.3.9)+ W5 V: h* H- D* `$ M- b  Y0 ^& ?- u

4 e& t: M; B* y- T# N  f) M& s4 Syum remove apr
  e2 U# a$ o" }: w, R  w) C2 n* X5.Error.log中有: Audit log: Failed to lock global mutex
) y. j  T6 m3 X8 p
) |# _. P: P2 b5 c* z' o! d2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     ( H, _( o. B$ r3 W3 C7 K, c
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
" v/ y8 g: c3 V解决方法:# i) \* q# e0 Y. N' _
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
0 K" Z  u2 z. U
. a) ]1 G" K' jSecAuditLogDirMode 0777# P% F8 u  v( h* _4 Y2 U" G# H
SecAuditLogFileMode 0550$ f; I5 K# B4 Y3 X9 c$ Q& b
SecAuditLogStorageDir /var/log/modsecurity7 o% K5 O0 ?% Y2 J* a
SecAuditLogType Concurrent3 [7 A) H  p: k6 T
参考文章:
& A' v" g2 c, s! V( J: q' ihttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX- ~9 ?( b; e' v6 i
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-8 11:38 , Processed in 0.061645 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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