找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9940|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。# x3 ]: h6 t  z; B5 d
, R: b  H7 a% f  f  r
一.准备工作. [7 g& e3 c- X1 x- e" {

8 p3 z. e6 n" w2 d- ^! S系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
' b7 i3 ?1 `/ _( F  W8 _7 c1 E1 m, T' [& n* q' Q
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
, N- P2 S& r* Z+ i! K. U
4 b, P+ t( U' R" {1 X+ d6 Wmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz! k) V* s+ ?3 n0 t; m1 ]" C
; q& |+ Y) B& K" x
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
5 q1 {& a/ I2 i% A* W9 q
! n( F2 }0 g+ m) i% Z依赖关系:' @; B- ]- R1 v7 n) |( V; n) |
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
/ b# Y8 U) b, W4 B" [8 a) i3 Z$ [1 X% u" b6 @; ~
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
7 @3 J# s9 C$ E) nmodsecurty依赖的包:pcre httpd-devel libxml2 apr
1 t) E( Z* \$ v! X% ?5 m& {5 [
# L/ {4 M* l0 m  Y% K, U: Oyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
% y3 H8 b2 _6 g2 e6 Y6 U二.启用standalone模块并编译: B, S7 `6 g( ?& ~- C

- j: B$ z) c: Z% u下载modsecurity for nginx 解压,进入解压后目录执行:8 `' A( c* I/ ^1 Y5 h1 f& S, j
/ V8 `# t6 _* w
./autogen.sh5 f: w, Q+ J) ]" K& c6 i
./configure --enable-standalone-module --disable-mlogc4 p( r" Z! e$ y! S5 k
make
/ m' S, X5 c% }  g3 P三.nginx添加modsecurity模块
4 h6 E7 j2 N0 H
, ?3 Z$ D- Y. v7 t1 c在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
; S+ j. ]3 ?" |2 u0 f, q+ D/ F+ ]
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
/ Q) A+ J5 a! w2 E5 y; o) x& ^make && make install
% D# ^1 S# g2 A$ t4 z% y4 F( S3 e# E! L四.添加规则
9 c- g; p6 M  c) |" @
3 @& K; c( Y& o7 L: Hmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
: ~1 Q, b6 o  @
* {: x8 M! i9 Y& R1.下载OWASP规则:5 P8 W7 [* G* d& b6 V% b; A

* Q7 a$ {5 u. F9 M+ hgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
4 B, X! F- H! W6 {
9 o, a- G. g( G0 Q! Fmv owasp-modsecurity-crs /opt/tengine/conf/  u+ Q0 k8 h  u2 J$ @

9 W4 j! E0 b: Z) ]. y) k: D# {cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
4 e+ e& }5 x$ h" X7 ~6 N, ~2.启用OWASP规则:4 n  \) e7 D$ m. m6 G% g

& [& l# s( k, y2 p4 U: O! R复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
) r  C1 H$ G9 e
3 q5 F6 @- I, p编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
! L& R* s& y0 b) j! t" [8 s  d2 w/ l, v( B) b" ^9 ~$ I
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。  Y$ s- v! v7 W5 {0 g
& _8 s, b% {9 i$ M- L
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
( e( a! a) n; k: Q! |' F' E+ _. PInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
! A& m- r) ?, B$ JInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf. F5 _9 ]- K5 s4 K
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf1 ?3 L2 L" p! Y+ q/ M
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
7 e( Q- G) [* F  d' aInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
1 X1 p; d2 z' UInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
4 K% q4 w  ]6 m) o. _. P五.配置nginx6 L( @4 t8 J! q' {7 A- `
& ~9 X1 A7 G- c
在需要启用modsecurity的主机的location下面加入下面两行即可:
/ m+ L" V2 k: Y+ ?4 |; Z( J6 h8 G  A, p1 F0 }" p1 G2 H! b  }, f
ModSecurityEnabled on;  
9 G4 D: t. Y- J  l0 s! n0 D1 lModSecurityConfig modsecurity.conf;5 H4 n/ u: M" A1 I. \0 s/ u( E
下面是两个示例配置,php虚拟主机:: v0 i3 ~3 i/ `4 S
0 y& ^; ?/ z! @$ l
server {/ I9 R: {) L" J1 M
      listen      80;
4 w5 J: d  X9 x2 D0 y      server_name 52os.net www.52os.net;2 I) n; j) }/ O- }& @. t# z) [
     
, H0 a1 ]+ O) |; f      location ~ \.php$ {4 W# J7 C) O1 v( K. c  G6 y
      ModSecurityEnabled on;  
" ], y' a+ U5 u/ Q2 ~( |      ModSecurityConfig modsecurity.conf;; M$ D2 q+ c  W; E, M3 S" F
! r  v6 B8 ]! m7 X2 \
      root /web/wordpress;+ W. v' ]; p$ _+ t9 ]
      index index.php index.html index.htm;
) T1 t8 w( b; ^  
4 t' @3 F) d+ Y8 v( l" z8 U4 ~( ]' v      fastcgi_pass   127.0.0.1:9000;
- _! L" _, v9 W# ~6 I      fastcgi_index  index.php;, r$ q% h' a0 \- L, J: X+ o
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;' x9 u+ \7 s% t3 Q+ E
      include        fastcgi_params;2 \2 i  i: G' B  m( i0 X: [
      }
9 P: ?" a1 E7 o: r  }
# E) L5 f$ X& L3 o9 `# j) v. \+ V' bupstream负载均衡:
8 u$ y& d2 N- k4 s9 t* f3 b& S" D. P( o+ Y8 z
upstream 52os.net {+ {/ I0 F% V# v3 ?2 ]1 [
    server 192.168.1.100:8080;% H: ?: Q; o5 G/ B$ F0 ^- Z! B
    server 192.168.1.101:8080 backup;
9 `( e' E1 U' [0 o, Q}
3 o1 m' U% {2 `1 {0 e0 X* F! ?1 l6 s
& b4 J* `3 K" V; Q$ z4 f2 W( Bserver {5 ~. \$ y3 N/ ?
listen 80;- p* z# Y0 ~$ Z) @6 o7 Y3 w
server_name 52os.net www.52os.net;
0 S7 \- q$ R4 n4 K5 {% c! \" ~# y& j& A
% t' H/ k! f3 o9 }0 m* G* Glocation / {( r! X7 t& K1 K; k& m8 Z
    ModSecurityEnabled on;  5 Z+ O) T) L( [% S/ v
    ModSecurityConfig modsecurity.conf;  
) s1 Y! w2 Y& \. Q8 J& f3 `5 j! c1 y/ b* e5 Y" j' }) A
        proxy_pass http://online;
! g! W; K( z" s$ N        proxy_redirect         off;
9 Z1 W' w0 O# k4 E0 ~, }        proxy_set_header Host $host;3 E5 _8 y; y5 H+ \5 x  T: C! Y
        proxy_set_header X-Real-IP $remote_addr;
5 N% [; x7 ~7 z% l        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;: L" `5 P1 b! p4 F9 ~0 e
    }- \+ V/ z; S0 y1 l
}
+ A& R; i3 L; P" d( O2 l& x; u六.测试& l$ z3 h8 r$ G+ Z8 c2 \7 n
$ B9 x. t6 g3 u% K  x
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
4 X# r1 x! Y! I: g# e: Y- f4 f
& z: v. l) J, f7 ~% G. h! @<?php
' `( V* [6 h9 o8 u# y    phpinfo();    ) I, }* A5 E3 I' F" j
?>
; h! ~' Z  A7 ^/ U4 z$ @在浏览器中访问:9 Y" i7 B+ I& P9 M

7 y# s, a) {1 r' \, f- ~3 }http://www.52os.net/phpinfo.php?id=1 正常显示。4 T2 d8 \+ }! A
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。' ^( v9 U# Y" W  L# m
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
- l; x: x& |4 B7 x0 O& T5 o7 ?$ n说明sql注入和xss已经被过滤了
2 P8 d) ^/ }  T& n9 L7 }- M' c# h6 `$ o5 _
七、安装过程中排错+ r" l' s$ E4 M2 |2 E

' X1 q+ x% _( X) m& T: i& N, ?1.缺少APXS会报错* c4 N0 T7 `5 j) S: s
: _1 x% G9 I* L& r8 g7 e; ]5 S8 Z# L
configure: looking for Apache module support via DSO through APXS/ n( p" G: _/ F; G
configure: error: couldn't find APXS
8 R, N9 Q6 @% H# {4 s9 u5 ]apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。! J2 t; G) B3 I  C3 B1 X: |8 \
解决方法:' t% `- @% X+ F% z5 {

6 ~) u, d* x  q- jyum install httpd-devel* x9 G+ T3 F" B# h7 |: |
2.没有pcre
  d, s7 L+ C! U/ S* \" T' J
6 T+ ^7 h# N, @, G1 o* iconfigure: *** pcre library not found.+ ~, x/ b4 q$ W
configure: error: pcre library is required
8 |8 q" ?/ p3 D$ O, ~( I& E解决方法:
7 x! K+ b) i, _+ G& @# o+ `0 R3 I
9 Z, g: U7 `7 P5 |/ E; X' _9 Ayum install pcre pcre-devel
1 q- `3 m. ~" }$ K6 M$ `) b3.没有libxml2( ~2 K5 x, v/ J) S5 J
; b9 ^- ^7 x9 N+ ~0 r. l

4 \. `! |, D2 Y  e9 z' E( Rconfigure: *** xml library not found.
  U$ [. G- q2 S! Q* R& V1 Z) v- Dconfigure: error: libxml2 is required
* r6 x0 D: n5 J* g$ G& A/ n# W解决方法:
5 X9 j, z9 u1 q) k
9 K8 P8 I; Y6 b0 U2 v6 v; P' byum install  libxml2 libxml2-devel6 q( G5 L; @# t  ~* N6 V
4.执行 /opt/tengine/sbin/nginx -m 时有警告
/ W* L, @$ X" F3 V' e  k, o- Y( E6 g/ C- i( \/ H
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
# U5 N% N( @0 D5 Dnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
; b5 x% `8 o5 a9 g6 B( F& [( @原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log6 ~  h  F% b" T. P" m

2 |; n! G( g  }5 L) E$ ^0 |2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.. w: }( E# |( G* F* D$ m+ f# I
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"- c: a6 ?2 m3 h6 |
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!1 M8 O# J6 N1 D1 t( F1 B1 Y  g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"( r, [& u& |: Z- z+ i7 x* p5 m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
* W# h# r# [) r( S6 _9 t9 a2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
' n$ a6 h' M3 j* ^, J解决方法,移除低版本的APR (1.3.9)
2 G, ?" D7 M4 o3 [7 p& I4 v, u0 M& Q0 S5 o$ q( |" `7 V% b
yum remove apr! f9 @4 ?9 I* J# G9 w
5.Error.log中有: Audit log: Failed to lock global mutex
" o- d: O6 _  U  N' l- A, G8 t: W4 |$ H7 S" D' b. [
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
, V: ]& W. C2 H+ i: Q; V, K: xglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]% U9 B4 K. s, t) t4 f8 h: i6 d9 _
解决方法:! V# ?- n, J  {8 I
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:4 J2 r1 w7 z6 q- [: _0 H

) z" y) S% U7 oSecAuditLogDirMode 0777' i( j3 M2 |9 D  |0 c8 W7 A% h" ]% W
SecAuditLogFileMode 0550
% h# l5 |6 e' VSecAuditLogStorageDir /var/log/modsecurity  T4 H- N2 H) D0 f
SecAuditLogType Concurrent
/ h+ ^6 w( [# h$ T1 y) T$ X* h. @5 m参考文章:
0 B" z* e' S3 ~& z/ Ihttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX# v. d. W8 `# O% h6 `5 m
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-2-15 11:37 , Processed in 0.073292 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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