找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12646|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
1 K* W! k* m+ Q; R# \
' ^+ F4 j; v9 C" t5 a一.准备工作
  E, G# H( S7 w4 W6 N, M% J' O" p
: M! ~! b3 ~( @3 W9 f系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
- R7 C" \4 _  R$ |2 Z: i3 W, x
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz$ G0 q8 c) H3 ]0 f+ w  t! n2 ~

, N( x  v1 S1 Y5 w4 u% D' Mmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
$ d, A' C* l& c  Q8 M" v4 _, D# y
1 @1 y  U2 x+ W5 }OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
$ ^6 \' g+ u6 r( G5 y, E: q2 ^/ A" e/ |6 K/ B
依赖关系:4 S8 I* v% `* p1 N  [
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:) n# |, L% a8 n% Y% v" p

1 Y* A+ `8 t, X! y, H& Tyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
: {' [, a' C6 Ymodsecurty依赖的包:pcre httpd-devel libxml2 apr
1 ~  R0 g) x; R5 s9 t5 ?
; O0 s" a& t4 Y9 O+ D. A1 C+ Ayum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel4 w- F2 T, Z8 g" Q' w7 ~
二.启用standalone模块并编译+ }  i, q5 }' N+ b) U+ U& J, U

$ M" z) I4 I7 L$ i0 `下载modsecurity for nginx 解压,进入解压后目录执行:: {. X% m0 F* z1 T& H* i( y

# p8 x1 [2 S" }8 M# S' V+ u./autogen.sh
( Y, X1 [( J0 r9 ~& v# ]! }./configure --enable-standalone-module --disable-mlogc
# \3 z6 R9 n5 F9 x6 n) mmake
% @6 D  u, g, T1 o8 H: A& T6 ]三.nginx添加modsecurity模块0 v2 A5 ]$ ^! w0 b
1 ~( s3 F. @' F' ~$ c' q
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:; o' ]4 c- I4 e! k7 B% {

3 x1 L. {5 L+ l0 y. D- w./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine, R% }5 b! `/ Q0 ]
make && make install
+ T# [) g. Y8 E$ t3 k" Y) `; d& a3 l四.添加规则: ]0 ~/ q1 ^* }+ h/ [! ?' U
: K7 l& X5 C& R0 u2 W
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
0 u; ?/ s: g' e
9 \2 t, t* @8 m& x; t, F) {1.下载OWASP规则:- {* \6 C- C( b: o. r
/ n$ @% M* K  }: u
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs/ j9 Y5 W! x) Z' F4 H5 U

* i, K. |  i* T: j6 x) lmv owasp-modsecurity-crs /opt/tengine/conf/8 j+ H4 m  u# {/ z: z- a% `0 p
: l9 b  I- {3 T# {  \6 h
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
" i, X, J; b) U; d2.启用OWASP规则:/ h+ [5 J+ q3 X: U6 \* S' b. |. p

0 G* Y. J, [% g* g: G复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& \9 m7 x' e. o! w& R

; e- Q( P1 d) ^% U/ i# s5 ^, R编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
/ J8 {' A. D1 {$ `& C4 i/ U' X* H8 c
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。9 f7 G1 x8 H0 f, i
, j7 w8 M5 f! |  `2 p; H# O9 ~
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf7 L) W9 p8 d6 g
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
. U3 i% V/ |4 YInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf1 A6 a2 ?0 Q( s7 L4 }% w* M
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf/ `( {$ y3 R' b8 p) v. v
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
* [. I0 T7 P4 C7 U7 k/ TInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
5 r; F) _8 |+ SInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
/ h7 O2 w/ [* T! [' [+ c五.配置nginx
6 h3 m% x- |' c# }" [
( H) _0 W5 }  C% K在需要启用modsecurity的主机的location下面加入下面两行即可:
7 i* J# }( T- F: N6 f$ \
# Q- X$ v* r; p. rModSecurityEnabled on;  
0 v, G) V9 w9 O/ k* Y7 t8 P- vModSecurityConfig modsecurity.conf;6 l8 S  R6 u/ v  q, B; M
下面是两个示例配置,php虚拟主机:
7 w) P$ s, S" y; ^! ~
$ u! Y4 E6 \8 F$ {9 r/ m  \2 Cserver {, l% {9 M: {' X- q; w
      listen      80;
5 o' {+ j. x, M- h- Q! \      server_name 52os.net www.52os.net;
9 {3 J5 X/ @9 |( K$ |6 U% Y     
1 ]8 E+ E: |9 e. Y      location ~ \.php$ {
6 z- ?/ V  Y( [; a4 V7 o) N      ModSecurityEnabled on;  1 h5 Q# `+ Z" P! _6 R9 n8 ]- n
      ModSecurityConfig modsecurity.conf;
; Z2 Q. j' d# {7 r7 n  H0 W  }. X' I" a% H
      root /web/wordpress;
6 }; ?* C$ J+ ~' e      index index.php index.html index.htm;
! j4 k3 S" X+ w" a+ g: ^# K  # a+ X# B  ^3 C) d7 p" s
      fastcgi_pass   127.0.0.1:9000;
+ C! A' [! ^. e      fastcgi_index  index.php;3 X1 F0 d+ `$ j8 S1 @4 q% G' N- e' [
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
' G/ a& V$ r7 L3 J9 G* R      include        fastcgi_params;( J& d( f( k* I
      }
& |" I" J' V- x* N4 a  }7 Q  }: B- F# \5 T2 G  `
upstream负载均衡:
, O3 g7 _: g  W. K3 W) k; u
4 g- }6 n; J( f# a2 g3 Uupstream 52os.net {
  j  X) k7 y9 d* I  w$ `    server 192.168.1.100:8080;% L- L( X, ~, O: U1 m' a3 v# i
    server 192.168.1.101:8080 backup;
3 ~/ [* X. K/ L}* ^* D0 z* T, q( M9 X
$ n  u5 o, o8 m9 u2 [: J* m
server {+ {: c. F8 I- a; k
listen 80;
  V$ w& ^* L3 C( t' b) A5 Oserver_name 52os.net www.52os.net;
4 d7 f3 }8 Z; {  [7 m$ o: e7 z, r
location / {
2 m( T' X) l& k    ModSecurityEnabled on;  
8 J; F. ^" i5 [; m5 J( m& Q; Z: A    ModSecurityConfig modsecurity.conf;  7 M8 H) f& S  A7 `! x
5 J/ v. R3 O9 j: D- }3 G4 _# L* G9 @
        proxy_pass http://online;
* m2 Z; z. S2 ]% h. D% C& K        proxy_redirect         off;
3 s8 I! z1 G- E& i# `1 ~/ k        proxy_set_header Host $host;4 F8 Q0 i! L( r
        proxy_set_header X-Real-IP $remote_addr;- Z; {# F) \0 q# ]$ r/ w3 ^% P# ]
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;( `9 h( R# l5 P  W; x) T
    }
5 L# }- n" J4 q1 o3 o9 i$ P; x  P}% ~# A& y3 W9 x; r* J
六.测试
/ k4 n- p1 X" A2 {. S% u8 t2 j) c: I; U( X
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:. n% h+ F3 h3 u5 F" D4 t) m; x* Y

. C. p9 [! A1 {$ j! `* _0 T<?php
, c& M+ ~, N0 n0 G    phpinfo();   
5 h- o9 [8 x, F1 y7 J! U8 ]0 e?>
5 B" P* h$ j( ]在浏览器中访问:
: d4 |. p" N. l; {
% b: q+ E9 w# R- Y8 ^http://www.52os.net/phpinfo.php?id=1 正常显示。
0 ^* [. l# p+ ]; \4 @+ ]2 l  W' W: chttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。8 A5 \' j" L: t' H8 S% o
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。+ T3 Z& ]- N* o+ j9 q
说明sql注入和xss已经被过滤了: `: V, y7 Q' O# a" G3 E" z

- E# |0 ]& V3 v9 x七、安装过程中排错. A  O. |" P* I0 x, Y  g

% ]! R  D3 o1 z4 G+ ], O1 _! L1.缺少APXS会报错( Q! ?3 X$ {7 \7 V6 V1 S

% k) y, q% a& a; K' H4 j5 Econfigure: looking for Apache module support via DSO through APXS
( g2 j; L9 @; F1 Aconfigure: error: couldn't find APXS. u% V6 g+ w7 F) |
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。4 H% P6 t8 G5 U$ w, y# e1 B
解决方法:
0 X' B* T( s8 g5 H# w) E$ H1 C7 z. n
yum install httpd-devel
* D# e( h3 J- X7 C/ I4 c2.没有pcre9 Z/ f. g8 T0 A

! F9 _% T5 K: F! u  Gconfigure: *** pcre library not found.
8 v) Y4 q* c% g- U" mconfigure: error: pcre library is required
; X& P/ L5 U4 p7 S解决方法:/ {2 {( R  @9 ^7 J- ?6 x$ k2 I
$ {/ y8 m- K' x" h1 c- K# a
yum install pcre pcre-devel9 O6 k2 b  V  f2 ]& f  {8 i
3.没有libxml2/ x: t! n. J! \9 J& i

- m8 [# d! L, [; Z+ g  r6 ]" s& I/ e2 n; N* a6 l% H
configure: *** xml library not found./ w0 M9 a0 o5 C  k7 H- e0 S
configure: error: libxml2 is required$ N) x% _6 q' p! s/ h
解决方法:
: q) m" K3 M- C
3 R( u/ S9 Y# ?yum install  libxml2 libxml2-devel
* K" [! e( w6 W7 N# s4.执行 /opt/tengine/sbin/nginx -m 时有警告, x0 @0 |) Z& b2 f" w

+ [' j; a+ n: N# C" `& BTengine version: Tengine/2.1.0 (nginx/1.6.2): k. N% t0 l; H, F
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!" ~5 W7 H/ k6 G! }: y( c
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
4 o# X1 i+ B9 ?/ k* c- j2 C: T$ H: ~/ G) m' V7 v2 O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
" d7 q6 o5 K( A1 e0 Q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
. R3 ^6 M8 p) F+ ?2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
. p! }! n* N4 R5 U- h! u2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
- e' u% w( A, S& [. ?5 L. Z8 G2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
# j3 v9 }  `& q' }4 W2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
, Z2 Z) K( F3 @解决方法,移除低版本的APR (1.3.9)" W9 c: [3 s1 t  _; K7 E
" ?0 ~  z) o5 m. g
yum remove apr" {9 M7 K, E  Y6 y
5.Error.log中有: Audit log: Failed to lock global mutex; {' S) A* N& {) w+ ]! h

0 }& x# N9 }# l1 h  n2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     ) y$ w2 T4 ~4 M4 q
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
3 ?4 X; e1 h4 m; P8 N8 v+ |5 y解决方法:
$ t. H4 w& V  r4 Y; n编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:8 o- g' V4 o- h; z
4 z: m8 z5 i7 A: T
SecAuditLogDirMode 0777- m- G2 K% m% P
SecAuditLogFileMode 05502 O( f( w5 Y6 R" ^) F
SecAuditLogStorageDir /var/log/modsecurity" F' v8 W0 i8 E& Q: ?
SecAuditLogType Concurrent8 u" A! e9 ^6 E% L. j& s' j
参考文章:7 G. a/ D, o. V8 A: @) F
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
) S& b8 ]0 m3 F- s  mhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-26 04:17 , Processed in 0.069857 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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