找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11933|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
, d& ?! K! }9 S4 U3 o, a
/ H, ~# Y1 `% F. v* {+ E$ x一.准备工作
' o% Y/ q0 p0 D" W+ r
& |0 k0 [* x6 }% J5 N系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0& x) @, S6 a. f$ ^2 G0 R1 T
. h" G- H1 N- U# E; `+ y8 [: B
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
) C4 S3 P5 b' V2 A" j: x' @& o
6 X& n3 X8 O/ O7 H2 z2 I, smodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz: B0 \/ q$ }2 C% V) t. j# {0 y7 Y( Q

0 T4 S/ \  ?9 S2 bOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs6 I' @7 k; i1 `6 Q( U
- ^/ N/ O* F$ b1 p
依赖关系:7 x# ^# \4 D* h! b+ N8 d
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
; K2 B' a& L4 i; S4 t, |
! n! @' t- ?+ }yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel" e2 j* R+ G& ]  P' J$ L+ P2 D
modsecurty依赖的包:pcre httpd-devel libxml2 apr
- r- c) g3 p& z7 ~, y  T+ B
, u$ B: S. H/ b$ B5 F# |yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
, [4 @' S- A) K7 U  r二.启用standalone模块并编译
" m- F# ]5 q0 U/ \
  x+ W) V3 `' V3 _下载modsecurity for nginx 解压,进入解压后目录执行:1 M, o8 A4 L7 p% ~  t
  o; w3 z5 X7 t( U1 Q5 {
./autogen.sh
/ n( ^& d8 |) y  s./configure --enable-standalone-module --disable-mlogc" O* d7 H4 |, l
make
, m! V$ \& Y; F) z) m三.nginx添加modsecurity模块" B5 ]6 ^" j- @7 ?; M' I
( C; R8 M! Y/ q6 L3 N8 i
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
, J" {1 l) e$ A; u3 c6 F
- \% y! b' u8 z) e  w./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
* S6 w4 M5 k- N0 R+ R5 jmake && make install: D+ M& T+ x9 F9 H, Q& [3 Y
四.添加规则* v( h2 s6 C: W9 D1 b
6 _: G/ D$ r7 u& |9 }
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
; t8 x$ o* Z8 j8 i+ |
/ x/ g  ^0 a" z7 |1.下载OWASP规则:
  t% S% I; N3 s* b* |1 s- j/ `% o/ c8 q# b0 n. {" M, I/ M9 g
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs/ T7 i. Q5 z" q, v2 Z
& i  Y7 ]) E9 z7 s% I
mv owasp-modsecurity-crs /opt/tengine/conf/
; o9 r# A8 r& e/ F" R. J4 Q- e$ N; p( X; Y% p9 p/ X8 I4 M
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf  ~/ E5 ~' f9 ]- p# s+ b) A
2.启用OWASP规则:
( w" N  V4 m' c  z$ p5 I" V1 S
( v  N+ m# b3 U& B复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。1 [; Q/ b. |/ Y# ~" V$ W& J
% A! D- o8 \3 @* x& E! d; v
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on9 K) \/ U! m3 \, y* T+ ~$ a8 q1 o

, p' D/ b8 F' f6 A% R+ ~owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。0 Q8 g1 l2 E, f* b( e1 u
9 D- C. @  N. N$ N/ @
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
3 T8 i9 A, F+ M$ ?/ G3 e7 ?# aInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf. ?1 i% y1 ?6 D: i. N
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf& z' K9 H0 O/ N2 B. \% B# Y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf! `' O, n8 w" R3 _! r: t# C
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
- X" e7 ^4 q1 k' R( [Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
& t8 Y7 A$ e; C! ^2 LInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
& `% Y0 y1 a9 p& J3 \# k# A* o4 Z6 [五.配置nginx) ?) d8 }+ Z8 `+ L) O& j
& M" T7 S' S4 z
在需要启用modsecurity的主机的location下面加入下面两行即可:
7 ~/ _( m6 V5 G  E! N* \3 J
; ?1 }7 [/ a& i+ i) P2 K( e* I6 a( jModSecurityEnabled on;  + M& l3 C; C/ c; ^$ T+ x/ A! D
ModSecurityConfig modsecurity.conf;: t1 J& P. b2 W- _# X) l4 z
下面是两个示例配置,php虚拟主机:
5 ?9 H- \. p0 Y) R1 j
6 c/ v1 U  h2 V, i1 Nserver {" J1 [" V9 h8 h8 l1 V9 `
      listen      80;( ]% T' D  q5 H" l( Y/ W: I
      server_name 52os.net www.52os.net;
0 W) b2 ~) K2 I& M2 g4 X* T: S! T     * a. o3 `& J& w0 B+ M9 g5 e& w
      location ~ \.php$ {
2 c. j* ~3 L: d" r9 Z5 ~      ModSecurityEnabled on;  % Q6 j8 o4 u2 n( J% G. x6 E
      ModSecurityConfig modsecurity.conf;: w  Y8 m6 F) Z$ w/ }. w4 P

* A: F# n. Q1 B3 V! E3 y      root /web/wordpress;$ C5 L& B5 ^% Z
      index index.php index.html index.htm;
/ c- {7 G7 y( U( j7 E  6 f' B* t, T4 q  y6 q4 j
      fastcgi_pass   127.0.0.1:9000;" R2 w) G) z2 p1 a. M% n& ]' w
      fastcgi_index  index.php;
& E0 Z; D/ p$ G& M( x      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;7 V1 J, B; M% [5 y3 v$ }
      include        fastcgi_params;
4 c; b$ n2 Y" I$ B: _      }) d. B& {9 ?9 B% h5 ^3 F6 g
  }6 B  C$ {0 [3 F4 u& B( h% e4 `
upstream负载均衡:
" k4 S; h) B4 g! f- X. y. x3 w
upstream 52os.net {
# Z; R8 Q' O# }2 ?    server 192.168.1.100:8080;
/ u% B9 _; j* d5 Z7 F    server 192.168.1.101:8080 backup;. x6 ^# t# D4 o4 C
}5 K& {' l, I; `

: s& F" e" ]- Q' j, z( M/ Iserver {
5 ^. L4 i% k6 i0 n' [3 s3 Hlisten 80;# m0 Q" ]+ b8 ~
server_name 52os.net www.52os.net;0 w& R9 L+ u8 Q" X1 t

1 t: ?8 }7 u, `* Y! K7 {, O  A% Q. Dlocation / {
, A/ q) w+ n; m0 f% I& m+ |    ModSecurityEnabled on;  
8 L/ u( O% k. A! |9 T    ModSecurityConfig modsecurity.conf;  
  Z7 Z, n1 j. x6 w+ }! K4 Y$ v- `# a
        proxy_pass http://online;
9 _- r  f3 Z) j" b4 L' R  \        proxy_redirect         off;+ T+ _. g3 ^; U) M& V' S  L& n
        proxy_set_header Host $host;7 J6 j" E$ c: ?( ~5 @& R, D9 [
        proxy_set_header X-Real-IP $remote_addr;
, d3 y, c) Q9 |2 s; E! _        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
5 p0 v5 v; b( ]    }
+ c0 D  O5 {. ?& D1 p}( C: L% N6 K' m! F4 m* T
六.测试
2 c" q5 l2 G5 q5 Z9 q( S# Q3 m: I  D1 T
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:6 M. ?8 _1 A' f+ d
% {. \- a7 q7 U. ~2 y% {6 B  G
<?php
6 Z3 q. i: Z. H4 D1 [- v4 d    phpinfo();    9 D" y& U7 ~& s
?>( h1 ^: n2 E4 ]& d/ o) q" X
在浏览器中访问:+ P) `3 D: z+ ^8 M4 N7 D# [/ L
1 |" b9 _% o% K: Z) l. G8 d; \
http://www.52os.net/phpinfo.php?id=1 正常显示。
2 M7 M: s8 R* \# N" M9 e- ^. Rhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
" U6 B3 Q2 s1 X, G+ y, Q( I4 Bhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。. B7 `4 }( g2 |3 V
说明sql注入和xss已经被过滤了
6 B# X1 y0 o, y- Y* A/ u) `
& W9 a0 J; m; {' ]3 I七、安装过程中排错
7 j, C1 E. h$ y' g) k1 x6 [
3 u4 x* g' z& K+ \) G1.缺少APXS会报错! e8 c/ q" x* U) n, O; F7 ]$ |

0 a6 d6 @; s7 i6 ]* I+ v4 ]4 |configure: looking for Apache module support via DSO through APXS. ~$ h4 W3 [; J
configure: error: couldn't find APXS8 ~, ~& d' b  L7 ^
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。7 ~  k0 u/ L9 e3 E0 l8 w' }
解决方法:
8 |6 m) p% j: T4 u/ S) T; }- A9 V2 S. t* L. o+ Q
yum install httpd-devel
& g- W! e" Z  o  {2.没有pcre5 ~4 [9 e0 d; R4 Y/ [

  C, B' X5 @3 u% vconfigure: *** pcre library not found.
) X. F: ?/ L( [. `6 zconfigure: error: pcre library is required
5 |, {! o4 Y' }1 ?6 F解决方法:
* K( V$ @6 @/ \+ l: B- Q7 {, ]# ?; t
yum install pcre pcre-devel0 E- Q& L. G4 N$ e% X2 ?2 g
3.没有libxml2
6 \# v; V9 b3 U5 V) q1 @) x, J$ e  {& J2 j" X

5 L. M( z, m8 |9 {configure: *** xml library not found.; \. H- L! G; N2 z
configure: error: libxml2 is required
) ~% u8 U1 G2 x# p7 e解决方法:) v- p' i. ?/ K# V2 |) B

+ p  h2 o6 ~: |7 X7 G& t+ \yum install  libxml2 libxml2-devel
! O3 f4 T# K: U+ E! S4.执行 /opt/tengine/sbin/nginx -m 时有警告
  W4 p, l; u" W
7 l* R3 p* [& ]8 WTengine version: Tengine/2.1.0 (nginx/1.6.2)5 C0 J# {, i  |7 N' Z5 O% M
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
* ~2 f) z: ~( k# X原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
# V, J' W# `+ R( H
2 M, M+ J: K# K: _" s0 ^4 [2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.2 _* S, f8 X8 y/ s% w4 G* ~4 o( d
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
% P# z9 l7 {1 B7 E  G2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
. C3 a$ U) x% S. H  H# m3 e# Q& i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
- G+ ^  ^6 y$ u- [' h2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"$ ?5 w9 R0 C7 T# Q
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
4 o& i7 S. ?8 D  O5 N6 h5 I4 X. N/ a解决方法,移除低版本的APR (1.3.9)
1 ~+ }! M/ E, U+ [) y! |
) ~  X/ H& h9 vyum remove apr
9 X* Y0 F+ j' a# K( r9 d5.Error.log中有: Audit log: Failed to lock global mutex
! L5 T' P/ X$ @- o' k+ N- Q) p6 Z, y7 I! n5 T+ B# V2 n3 T* e
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     8 Q8 p% j1 _2 V# V; T" u7 z. z# k
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
* e' t5 g$ @' [8 Z6 g6 y: d解决方法:$ ?' F  P4 W% o. A* m* D
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:: k) K( {$ ?: b; N+ N+ g

1 z, k# q) b+ _) f' H# dSecAuditLogDirMode 0777: T5 y, Y; z. ~5 P. P, {" h# @
SecAuditLogFileMode 0550
( t$ D9 i8 `0 i# Q! g; cSecAuditLogStorageDir /var/log/modsecurity; [# o3 V1 x3 o  |, X
SecAuditLogType Concurrent
5 Z% X. _- R% |2 P1 a5 }) F参考文章:
) ?: h+ o( m8 h( ^* }* q  ^https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
3 _4 z; y! n8 Y  G+ shttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-27 11:22 , Processed in 0.050200 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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