找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12127|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
! o- Z4 A9 }9 M: {( v) w4 h" Z4 v( }& ^# Y+ F; h9 G# O
一.准备工作$ B  @0 m, h5 Q; l" b& F
8 [$ Y! w8 P3 y! M# W
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
3 r9 g6 T* V. S/ {: u
; S. H3 C+ G; Q/ S( g7 O( [) Z# N! Vtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
/ z0 m, S0 S% I& C3 v. D' `* |
$ c& u( P, c; F0 @6 M! z, Nmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz# n3 |" H  o8 W. T# k% k

: a6 ~/ p4 P; @( Y' y  yOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
* M3 t' f& A2 H! U& O. y% K  B3 u0 i) Y% A/ C) q, m/ j! `
依赖关系:
8 Q7 I: q/ j6 rtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:6 r) @0 e4 A) s+ x4 D) x' M& a

5 `+ A/ V( h' x2 J+ z& iyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
1 h- R1 t3 e; O* amodsecurty依赖的包:pcre httpd-devel libxml2 apr* O* \& a! {2 w  _
- o/ o+ w( K0 S( z
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel/ w' k% b1 J1 {6 o% ?' V+ t
二.启用standalone模块并编译
, _, v' L* g3 B% R. y* |6 ^/ z# z) [$ Q2 U& k! c
下载modsecurity for nginx 解压,进入解压后目录执行:* q3 c0 j# t. _: z: L* u) B
/ s0 Y/ H) _* d' i8 {- w& @
./autogen.sh2 y( I4 W4 E7 R! n
./configure --enable-standalone-module --disable-mlogc' J' D6 k/ B) Q, R
make
" L1 N9 \8 L- ^% L4 D三.nginx添加modsecurity模块: i. J$ ]1 y0 o* _; F

: p- T, s) l/ H& g) Y! R$ h' Q在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:% @' w0 f& A3 ^

2 T" l8 }& R. E' d  F2 j./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine$ S, W8 l) V2 Y$ t! E# E- {
make && make install9 B5 F" Z! ^/ E6 d6 Z
四.添加规则& p* |' q" ~3 `* _9 G

6 |" ~' o2 F9 s! Tmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
, K/ Y+ \' \; j/ {$ T: e& O, G. v: d  b- i4 y# y; W& L
1.下载OWASP规则:, _8 _7 c/ [: ^# B' O

7 x$ [3 ?5 U: r: L) kgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs9 Y) |# b4 U4 u

  j; U+ y5 a/ N! I6 Smv owasp-modsecurity-crs /opt/tengine/conf/4 w( ^1 T) d9 t6 X

3 e3 ?, q! I/ }  ~2 kcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf; H) R. [& s7 @2 m' ~- u( u9 c! k
2.启用OWASP规则:- T1 A# G: f& d

4 ^9 B7 n% o6 R7 K2 {5 s, y复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。' I: l3 W& l& x( {0 l7 s/ E! x; U. P

1 v$ y7 ~) P+ j7 M3 `编辑modsecurity.conf 文件,将SecRuleEngine设置为 on% |. I! `, c% }

8 K4 P; k. C# e7 p- Gowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。5 c9 u2 r* i) Z3 [) M1 t
  b- J1 n# I3 ~8 p9 j3 q* a
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
, Y1 G( v8 P6 j1 ^% P! _Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
+ \% j, K- `; ]* w4 a8 s2 dInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
& R% P' A& e6 zInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf# D8 _% l  p+ M' m7 @6 m8 S
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf- X6 d/ [  U: P; E3 O6 h# n1 ]
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
* Y5 i1 o( E: |/ z8 nInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
0 }/ o6 q$ }  u7 g( e五.配置nginx9 G5 V4 n2 L) U9 v# l5 o

3 f; p! ?. l0 L2 f1 p2 `在需要启用modsecurity的主机的location下面加入下面两行即可:
! A) u# q( ?. j: i" g
3 {) g( J9 c& H' p: b0 EModSecurityEnabled on;  
, r5 J) L( j0 wModSecurityConfig modsecurity.conf;
9 |$ M% n2 a& t3 `( S下面是两个示例配置,php虚拟主机:. r5 r  u% e  F( P3 m
5 v& d8 ~% M) ~' y
server {
, @! S! B' n8 W0 w1 h      listen      80;
' |4 `$ a4 f# J$ z  m0 j      server_name 52os.net www.52os.net;; O; H9 t8 d! ~3 k+ W5 ?& c9 V
     
, W6 |7 _! S7 |# C  P      location ~ \.php$ {
5 _" L% n+ ^% W, ]      ModSecurityEnabled on;  3 [. y7 h9 a4 ~3 v% z
      ModSecurityConfig modsecurity.conf;9 m4 m% r3 p4 \/ n/ V* T; N

2 h# ^3 [; C6 d) h  P: d9 _      root /web/wordpress;
, I* b/ C: R$ d% }  U- o7 g6 A5 ~      index index.php index.html index.htm;
  P5 [' z3 `4 ]! `$ Q/ r, Q  z  
9 j  d( [1 [; A+ Z      fastcgi_pass   127.0.0.1:9000;9 g) d7 n. Z- \% m6 z! U' |8 K+ H
      fastcgi_index  index.php;
  S+ B# Z5 c' ?5 Z      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;2 Q( e7 a+ B4 ~: }/ w" |: K
      include        fastcgi_params;; }$ D7 x5 r: m5 U! w+ q5 c
      }
* d$ l2 c$ T/ N* D. |! ~1 V  }. w; |# F/ u+ p; Y( K
upstream负载均衡:
; A- p' B/ V' w: x; R. p/ f+ ]$ T* E3 t/ n. E& P
upstream 52os.net {
% H. g# f% E1 q7 j: b    server 192.168.1.100:8080;9 `- x  N: l( W4 Y) @" M
    server 192.168.1.101:8080 backup;
0 g7 s3 ^2 b: e8 N% c6 S. z' G}
' F; ]3 ?( [; N' J, M: ]  ?# j/ R. f9 m
server {
0 j4 h8 F( I9 h/ ?listen 80;$ H! h+ o2 H: P2 A' K0 J3 O
server_name 52os.net www.52os.net;
0 T$ I: B) [# t  L2 K
1 |. I- c+ g$ Z& e, Ilocation / {
( b$ R8 Q) j5 G    ModSecurityEnabled on;  8 G7 H, t1 B4 |3 |: W
    ModSecurityConfig modsecurity.conf;  & ~& V3 Q& K4 p& j/ ]" Z, d

- X( i3 O* Q5 A1 b( ?        proxy_pass http://online;
1 }: N2 |1 i; o3 u7 c7 ~        proxy_redirect         off;
  q' D$ \7 V4 C9 F1 y$ ~        proxy_set_header Host $host;
; |( `: V7 J6 J0 X        proxy_set_header X-Real-IP $remote_addr;# Z2 X  R% _& F+ q$ A; y) L
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
, H+ x0 R' N& J. y( g    }+ u# p  w7 z/ S6 _
}$ u' @5 W) M9 J* n1 N! E
六.测试7 R0 [1 v+ c' V7 x$ a( Q

; {, o; v( L/ O* i1 R我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:6 m4 g: Q% i3 A3 u  z* {

9 R3 ?$ I5 `  B) U5 d1 p<?php2 T# l9 i7 D5 N) i; x$ k7 Q
    phpinfo();   
7 N2 K5 g' Z5 y" F3 H8 A) q?>
9 m* u5 |. p& g( `" Z1 e. R在浏览器中访问:8 K  E4 a; s( m- R% ~

) z$ `! D- E' Fhttp://www.52os.net/phpinfo.php?id=1 正常显示。
' I! q! D5 y8 bhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。+ I) G, K$ _% B4 _, C$ c& E- x
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
6 z. _$ ]4 c4 E+ ?说明sql注入和xss已经被过滤了
( T1 s1 H8 i! U" V7 b5 k! I
/ m* F0 |/ E2 @; n* y  e& a- z0 F4 Z七、安装过程中排错: I# n1 v4 S( I0 U; ~3 q8 b
1 K. ?" D$ M( V) F" o$ D
1.缺少APXS会报错
: P, k3 m8 Q1 J1 \; G2 u: c  @/ n5 m7 {1 k% x
configure: looking for Apache module support via DSO through APXS
+ V/ F+ {7 {; G. R' T9 j: n3 tconfigure: error: couldn't find APXS  v9 J' r' I3 v  I; a# a1 A" J5 b  t
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
9 w& i5 m' v# X/ D/ X9 U9 N解决方法:0 c8 t* h- u  n/ B/ x% V1 b. U

& r/ ]% x2 V, Ryum install httpd-devel
: |4 T, j% t" O2.没有pcre
5 ^! ?# a- \" ~; L- S& ~; U+ T4 n
: M5 a4 Z5 j* r, qconfigure: *** pcre library not found.0 m6 k2 R. ^1 P" E, |
configure: error: pcre library is required( t; X% B1 S. W( W+ B( c* r. @
解决方法:
% h/ B  }2 [% ?8 g
8 G1 `  b; q' q7 _5 l) Cyum install pcre pcre-devel
# g% T) \0 d/ ~  D8 [( z7 K3.没有libxml2' b$ E  e6 j. c: {; {' {! e

7 U) a3 y; @+ A2 x( h4 s, J: K* w8 f! O
configure: *** xml library not found.
5 D$ ^1 I4 q; e& Rconfigure: error: libxml2 is required
5 A9 p% O1 v: R解决方法:
: J9 B3 {! ?  S* [- [) w. M2 ?0 ]$ [+ n+ z
yum install  libxml2 libxml2-devel
: L+ F4 b9 I' k6 z$ w. W; X& Q4.执行 /opt/tengine/sbin/nginx -m 时有警告' Q7 v0 _* M; {& W  j  h5 d
: y0 d/ y; h% g% j1 T
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
' ^+ ?+ Q# r. J+ Onginx: [warn] ModSecurity: Loaded APR do not match with compiled!" F: p& h1 O2 n1 B
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
% r- C. M& ^) o+ o4 Q4 ^
) A8 C' i+ v& K/ K, a2 i# ^2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
! F8 j5 e$ h( i+ E2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
% O) Q$ x* r+ d6 a9 M) x2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
* z$ o" z9 K) O3 g2 \; C; b! Q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"+ g1 u2 \) E* ^2 D8 z' y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"* G8 D7 |$ i1 o, }, ]4 ?: F
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.2 Q* ^4 v& W7 `, {% v! q4 x$ d; H" n
解决方法,移除低版本的APR (1.3.9)
0 ?3 b" t' ]0 P0 P1 A
9 a* G, |3 U5 ^yum remove apr; h4 p8 y" I" j; k/ n5 d" s
5.Error.log中有: Audit log: Failed to lock global mutex
% A3 w6 _+ H& ^! y
* D- s8 y& b- _& Z" U2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     * N7 z8 [. O( ^' ?, v
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
* r) d' p0 T& ^6 n+ M; {解决方法:. r  v6 y. L) h# J
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:  N" u! V3 R+ D) S# N% r
# Z$ c/ A* O; X
SecAuditLogDirMode 0777
  W5 n+ E2 {6 i+ D  tSecAuditLogFileMode 0550( Y5 I% D- |$ |2 M) p5 m
SecAuditLogStorageDir /var/log/modsecurity* s0 W' z6 A( n7 z& G9 e
SecAuditLogType Concurrent
7 S$ h) e( _$ B; `1 A/ Z参考文章:
* Q  h' k7 A* B% n4 y* u! Shttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX5 g( C% c: ~4 M! b( M6 A
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-11 22:24 , Processed in 0.062423 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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