找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10580|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。9 ^* `- N% M9 q
5 t: u' r, f9 l
一.准备工作3 L8 c, m7 R* o  \  V
! U! t+ u" r# M0 H, L. A
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0" [* }5 z, `8 v9 S+ t9 p
$ G6 s3 K5 I; ?3 w! P* T
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz; K3 n" C" D) Z  ]* Q9 H0 o
! l5 _6 ?( ?$ M' |" G
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz/ u$ o) ]2 @- I+ F# A1 z4 N
9 a% M' M, E* ?- t9 j
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
- X* w3 Y, @% n' |  r* O7 y
' J9 c2 Q# W( S3 R; `& N5 i9 {" @# m依赖关系:
3 D4 a7 _% E6 S: D) }, Ltengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:% r  \6 A) E! s8 m5 M) h) r
2 {" w0 D3 e. ]
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
5 s$ o+ J8 k% }! c. o% Nmodsecurty依赖的包:pcre httpd-devel libxml2 apr8 q( ^- w" o- T
; o1 Q* s* r$ ]. O
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
$ d5 s) K% Q8 X6 O% l6 k! O二.启用standalone模块并编译
( @! M  L6 Y+ N+ ]
" y: @$ P  e& k- Z下载modsecurity for nginx 解压,进入解压后目录执行:7 \) c0 K. P: Q0 W6 a& Y4 z; F" @
5 w. U' r7 g# ~) r
./autogen.sh( \! B  K- x* D: N8 u$ L
./configure --enable-standalone-module --disable-mlogc; r4 C: L: |2 m( [5 o7 t4 t
make
8 Z2 d# O- a% {* ^9 F! B三.nginx添加modsecurity模块+ M4 S$ s: h7 g! F

* \, I2 R+ e. p' j8 V在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
# E  o; \' B1 ?+ l$ K( F" e$ U  G) d# g, {
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine3 L. r2 s4 ~- m5 Z+ w4 L) R& q9 u8 a
make && make install
. ]( Z% c! B8 Q6 B! Q1 h: Q& X% {0 M( F四.添加规则
6 Q1 a( E& d: C) u
6 r& H0 n, K& }6 Y& R" F  ]9 u5 Mmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。* Q1 g* M% s% M& h8 M4 o- q2 p! ]

' B. B& w8 d" N. k# M6 |1.下载OWASP规则:  u' [3 G7 `. _/ h5 a) W9 J+ w

" c) r) N& R) N4 zgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
" h  X8 H- G! G  r  A% {- W, }' G: H
mv owasp-modsecurity-crs /opt/tengine/conf/( Y1 x! D' ]; p& P; t/ K, J& o
$ N: z4 [( P" n8 w7 h
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
. @) [) K# K4 p! U3 N2.启用OWASP规则:( h& E& ?( Z+ E5 Z+ y$ L# }% E' r
2 J- `; s) `8 H) ^6 p, c
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
( L, T, V7 P6 b* C
% ~7 v# W, R( X9 a' p" u. }# @% o编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
' B& q4 Y! K  U& v- w: v2 d8 s
; w3 d$ H0 [9 `6 w$ J) rowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。/ |4 I: u0 J/ n# q6 X
  i- b" J& `; [+ n
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
- G! i. s9 x; I0 ]" Q2 |9 o% `Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
5 r* ~6 b. Z6 b7 I$ HInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf3 M, B4 \5 ^. _# ]4 ]4 ?; ?+ `' p& g
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf- a: Y' e* D; `, r& u
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
: a- T: T  e, {/ ?1 Q$ aInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
* \3 b3 A4 F5 o& ~; t. T6 GInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf* B( ~( q; {- L9 E
五.配置nginx( U) S3 b, {) s. w* K

' n; T+ u$ m% W! ~8 K$ u' H1 ]) p2 h在需要启用modsecurity的主机的location下面加入下面两行即可:
1 U7 j! c# ~( W6 ?) k8 x1 J% w
7 M% K; b$ _7 ]: F) L! T; M: HModSecurityEnabled on;  
8 n6 r9 D3 T4 r; [0 P" x1 {" J2 dModSecurityConfig modsecurity.conf;5 a/ n" }- T; W
下面是两个示例配置,php虚拟主机:
+ F* i% `; E. |4 Q5 P6 C1 n) \  [. Q
. I0 v0 H. I) E/ K' userver {
; v/ c$ d% v' D- l" p      listen      80;+ q; i& k& z* T: c9 ~$ h5 s4 _$ t
      server_name 52os.net www.52os.net;
0 }# M7 ]' W% Q; S     
# P, v1 j4 p" N. g. ^+ v& k2 E: Q      location ~ \.php$ {. W- U* b  [* e$ b6 T, J$ X
      ModSecurityEnabled on;  
: X. o; i* b( j4 H      ModSecurityConfig modsecurity.conf;
- |. c& y3 u, P. ]" ?. H% D& S& X3 z% x3 M& Y
      root /web/wordpress;/ G  l# Q# r# c1 R1 E
      index index.php index.html index.htm;
& a- n0 o! Q& c  * k# H9 o9 n. m5 W* z9 B
      fastcgi_pass   127.0.0.1:9000;
, ^& o! _! s. b8 Z$ n" |      fastcgi_index  index.php;; V. ^4 X: ?) D' e8 r
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
! }# |" r. Q. H+ W      include        fastcgi_params;
2 h- x# {& v# T      }) |  k! D+ U/ m, @/ ?! B
  }
2 Z+ ]* p' y4 S% w6 H5 r: `upstream负载均衡:. J" D* ^6 S8 H5 w" p, K

( @( e; c* f% E/ V! n' q; o3 F2 vupstream 52os.net {8 m/ s8 G" d, W% {
    server 192.168.1.100:8080;
0 f; k3 R& d0 |$ v7 T, z    server 192.168.1.101:8080 backup;$ X* Z( c$ C! r/ c2 `$ w- O, @
}5 Y9 y% g" H& _* T8 n- S
  J4 {9 V5 ~  T
server {
& h# y' d5 S4 @. p7 Ulisten 80;
9 ]. Z7 D$ i- k* l. d# H% userver_name 52os.net www.52os.net;! ]* C) K, r- D2 z, c
+ x* r4 h$ ?$ A6 s+ e* @
location / {) F8 ?4 L' g8 G! N; C4 U/ I
    ModSecurityEnabled on;  0 C3 d; n$ Q* h9 C7 c$ }5 X# z
    ModSecurityConfig modsecurity.conf;  4 K% }& C( v2 H

1 m  L% e8 i/ B% z  t8 `. c% D        proxy_pass http://online;
) z4 m2 H( D6 F9 ?# z8 N        proxy_redirect         off;
6 f; ~# B; X, J( p% p        proxy_set_header Host $host;
/ y4 {+ d% d6 Z        proxy_set_header X-Real-IP $remote_addr;/ v7 J9 R) j+ i: T2 t) A
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
" Y5 Q% {" r) V* k; s: v    }
1 ?$ ]) }7 M  _) C/ L/ m}% Y" z* d4 J. s, ~
六.测试  {5 w) c  t3 T( v: R
! I, j1 H1 W) o: e/ J
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
8 Y% v2 Z% i5 H2 `9 b0 H' f( @" d9 {6 q# }- B; o* M8 P
<?php
5 ~* k3 ~( E, I3 }* A/ u    phpinfo();    0 {3 n6 ^: ^! G$ R( ^& o  N5 S
?>
+ j: @4 W3 F6 c1 S* k$ |在浏览器中访问:! e7 I) p  a. N# s- L
, C) h! d: V5 V. [
http://www.52os.net/phpinfo.php?id=1 正常显示。% z, d  n7 o; w! A! z' ]
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。$ K. o: \2 r  M& R& K, H
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。1 a2 O4 p( Q( t# S# a- u7 C4 y& G
说明sql注入和xss已经被过滤了$ ^$ Q9 \# T: G
7 w( N( P0 y  E( D' V2 y+ r
七、安装过程中排错" u( D$ n/ O* b9 V. M
1 M8 Q" J7 K# e, Y" |0 a
1.缺少APXS会报错" H8 U; B6 [7 u* }0 X$ s7 }0 Y$ |) S1 C

8 x, S7 K& O# h/ t+ {$ L5 m1 p0 Gconfigure: looking for Apache module support via DSO through APXS6 z$ z, [7 l) W$ h" W" m
configure: error: couldn't find APXS
1 Y2 A! c1 x4 f; L' B3 U# H" aapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
7 @) ^. _7 Y, m. o3 Z解决方法:4 Z  r9 O3 i- Z2 \/ B
# [  F6 p1 n6 _4 }
yum install httpd-devel2 ^9 Y; R; q8 Z8 |7 h1 N' r
2.没有pcre2 i. o% {# Q4 o
& `) a' n* Z, y" s  p: G
configure: *** pcre library not found.
7 p* G9 M3 U6 o, ?5 w5 Aconfigure: error: pcre library is required
& _: I% H; O! S' x! J解决方法:
; S0 s! r! @( j* U. B% b( z( F: g7 t& X
yum install pcre pcre-devel/ w% w2 f/ O5 S" G/ {3 M0 [9 o& D' z0 ^: w. u
3.没有libxml2( S, e. V. [3 `1 f

0 ]4 z( g2 T( U. Q  D
" H* t# Y4 A" G1 C2 yconfigure: *** xml library not found.
+ M$ b" g8 ~4 \, z" {8 Qconfigure: error: libxml2 is required
' K& j% z' t2 U解决方法:
$ {* x: i0 a% [2 B$ ~
& b' i+ ]  x* G; L1 t6 F1 d9 V! Uyum install  libxml2 libxml2-devel/ w/ k% R8 u1 _! m6 v- }7 I) s
4.执行 /opt/tengine/sbin/nginx -m 时有警告
% d! S: N) q3 Q7 W1 g- ?/ Z7 }
  v. B6 d  t* o; _& @' |5 c3 UTengine version: Tengine/2.1.0 (nginx/1.6.2)) E: S: p: v* s; Q2 e4 o
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!" O1 j! ?" @+ ?$ d  I3 ?
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log% Y- x& k& a. |7 d, z7 p
7 x+ t6 n; j! s& ?
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.) ?! [/ _( d7 S& P/ s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
$ s7 a' e" v+ u& M, }3 m2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!( s1 Z+ z$ T2 h: i9 g, c9 N+ n2 E
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
/ D2 d2 A7 f& X8 H3 i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"; n9 s) Y- h! u5 D" p, A
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.% ~* x, b" E0 ]
解决方法,移除低版本的APR (1.3.9)8 C* p5 P# p2 h# C. h  [* K1 c8 a

0 r5 J9 b2 S  k$ vyum remove apr7 V# @+ z% n, v) H& g6 S+ u: X
5.Error.log中有: Audit log: Failed to lock global mutex
" w5 U+ V1 E. d. L1 N4 G% X' N4 P0 u0 H- b8 J  z
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     & ]/ A# i5 @7 f7 U/ d; d
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]' @" [$ h1 x5 I% Z
解决方法:; I+ E: B. T: n, R0 @6 w% ^+ `+ v3 l
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:; O$ C7 }- z& `) l( Z& ?5 i! Z

( K" k. J5 Z: ~3 g3 u8 HSecAuditLogDirMode 0777
4 |% I  u4 ^2 g8 d1 ]" f) DSecAuditLogFileMode 0550
% ]7 `  c# K2 F/ {# a4 ]) FSecAuditLogStorageDir /var/log/modsecurity# Q0 W5 \% C1 b/ ?. t
SecAuditLogType Concurrent
( H/ d8 A: C) x; Q3 V" N" ^参考文章:. J! \/ }; b6 K' s/ g
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
# v+ t, M: |) H* f- u6 l/ \/ Ghttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-4-10 14:31 , Processed in 0.062656 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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