找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11728|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
' r% x7 @6 ]& q) h" M1 H5 q
! X, v- j3 L: i' f7 ]" C9 y一.准备工作* i* h) F& a' C; q) ]' [& G3 x7 N

4 R( Z) _# x8 M$ T, Q系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
1 ^+ q4 g$ q' i  g4 }9 s( l9 d+ }7 F- E& N
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz+ K/ [8 z8 ^( d/ P9 J) {, U

: L4 {- d4 P. T$ H# t" B# h# |modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz* i) i! C+ q" S
4 t- J2 y1 }) L
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs- M; M5 L1 F6 y' ]' A* d9 K

% g4 _9 W9 k0 z  ~4 ?依赖关系:* X5 F, o* O8 d) F: ^  M& K
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
* w$ |/ L" J% S% S$ h8 q& D
, k7 X8 p3 B& c% s$ Syum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
1 y* P: s8 w$ omodsecurty依赖的包:pcre httpd-devel libxml2 apr
$ ?' L' i) B$ g( C. B' o0 F+ n9 x) H/ T( ]' U# l% |
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel" e( W9 B4 ?. r/ g! e
二.启用standalone模块并编译( N! D+ r# a( Z1 ~# Z. I! C

6 Z, \: H4 x  q) G+ \下载modsecurity for nginx 解压,进入解压后目录执行:
( ?4 o" Q- m6 F% k, b
. T+ E, p  I$ |' d% A./autogen.sh+ k# p- D! T2 g3 ~' g9 I
./configure --enable-standalone-module --disable-mlogc& T7 _+ R5 q- a8 z4 \, z
make 1 J% E/ o. y9 ^# p' L) P9 }5 p) W0 u
三.nginx添加modsecurity模块/ Y) Z' p% S, p0 j1 }$ b4 r* W
( y0 @  d4 d0 l" J% ~$ `
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
; [7 F, B' @7 D7 {: L
" X- {) H( Z. O' v+ _./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine+ p5 L; |( F5 @1 m1 ?, ]
make && make install
6 I" `  N  ~6 ?7 B! ?" O4 W四.添加规则
4 q8 Y8 H1 u( l+ a) C9 }+ b3 a& g
/ z* n' G6 S+ o& `. @modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。# d% O2 d: K# O& \

! E9 A( g: {0 W- I9 q  q3 C8 x1.下载OWASP规则:
7 \* e3 A0 \$ s! Z" d
- s: o; L& [. N% p2 `% Egit clone https://github.com/SpiderLabs/owasp-modsecurity-crs9 m7 C6 p7 B# T/ b% O! R

0 b% W) C/ G1 v% I1 Y$ V4 {& @mv owasp-modsecurity-crs /opt/tengine/conf/; P5 D& R" l2 Z1 c4 z
& |4 X* }2 j6 c; y2 F8 q4 W
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf: U% F# p. y) ^
2.启用OWASP规则:
- K! d$ I/ }( R
1 S4 B$ ]2 H; b' y( t& e& A. s3 o复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
8 S/ R' Y& V7 ?0 i
) E, _/ u$ l, z: T' E编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
0 L4 s  L& [0 B& Y
, [6 |& H; v" N) howasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。% M' R) a8 n% c
% l: j* d  p$ Y  E8 K, R
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf/ S  O2 z! Z4 c1 V( k2 w
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf3 D3 k+ {! q: b* |9 W
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf* X! Y/ C( l. M- {6 w- p
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf! |2 N% z9 R$ }8 P: V6 P
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf6 S! a$ ^6 x. Z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
3 F+ s9 C+ m: |! BInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf. a8 N8 Z5 F8 ?1 ]# Z/ o
五.配置nginx: ?& `* ?6 g! u% n  W  a

1 O3 T# T& Y$ ?% k6 @在需要启用modsecurity的主机的location下面加入下面两行即可:4 O5 s0 W6 _) l0 B% e
9 B$ C2 E2 L1 r! M# G: H1 F+ f0 J
ModSecurityEnabled on;  2 |) R5 s8 ?8 U6 R6 |' E' F/ j
ModSecurityConfig modsecurity.conf;! w8 I) J& \' J3 P9 _4 A3 k
下面是两个示例配置,php虚拟主机:
+ Y6 O0 m- v  S6 _5 G/ R
% y1 }1 n/ D& u( Mserver {" M- E- I7 D6 @# @
      listen      80;; _' i) g. e) c* [: g
      server_name 52os.net www.52os.net;% X4 V* k$ G' O; W. M( ]" G
     
  s' d3 R3 s+ C6 y2 J! Y# e$ X5 j% p      location ~ \.php$ {, w5 p9 z. G% A' m" u7 I
      ModSecurityEnabled on;  3 }) ~" H9 P- X1 \9 z
      ModSecurityConfig modsecurity.conf;2 S! M5 l2 Y: M$ H1 b- S5 `! l
; ^  X7 m3 t+ L! K9 _
      root /web/wordpress;# }, R  L$ i; @
      index index.php index.html index.htm;
! P& d2 a4 I' S% b& C* y* R  
! M1 {5 V" V/ p. S$ U      fastcgi_pass   127.0.0.1:9000;* f; w1 g4 ~, ?& J6 X0 {
      fastcgi_index  index.php;
6 Y/ j$ b/ k" g9 L0 u5 V& T4 C( E      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;/ a# Q9 ^$ q8 ~+ @
      include        fastcgi_params;
% k9 u$ V: o, F/ P4 ?: Y      }- Q) [9 k/ T6 \8 C: i  k
  }6 f& ?* T! Z' E: U3 A% p  g/ U, \
upstream负载均衡:" l  B  I1 ^5 v
. e& p- r. X) z& Y* z7 t% o
upstream 52os.net {6 d( X' q/ |3 F2 h9 g% B) v
    server 192.168.1.100:8080;" I( x( [' z( S) ~
    server 192.168.1.101:8080 backup;: N) `: k: C% p
}5 M1 e- A8 Q& ^& U5 `1 ^/ X6 d2 S

1 @+ J% _: p8 p' B% c# w: Nserver {- N) B* ]6 k- I& H0 w* T  ~
listen 80;5 P! n, t. v1 T- r
server_name 52os.net www.52os.net;8 o& n5 f: t8 I& K. q1 T
5 m+ P& M. Y/ @( w% k, Z8 C
location / {
; V% S2 G: \+ ^/ L    ModSecurityEnabled on;  
% Q% j" X# s" Y7 ]/ M8 X6 ^. g/ a2 {    ModSecurityConfig modsecurity.conf;  & D" m, P, N: H  c3 Q# ?0 y
  K/ O% F3 f( \( ?2 Z$ A
        proxy_pass http://online;/ ^5 W. k% P2 W- f
        proxy_redirect         off;
8 P8 U+ J1 v' W( n        proxy_set_header Host $host;9 H9 b( o3 U+ C+ f& |4 j9 W+ ~
        proxy_set_header X-Real-IP $remote_addr;
; r* ?. C$ |  d) Y  i        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;& T' h6 G8 L1 y% J
    }; v- w# Z7 Q% u0 o& c
}
% g( X* U+ z- h) ]六.测试0 d7 ^& @1 t/ F0 Y4 d' U/ e

  A0 p) m/ \% j/ ^3 _" u" j我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
( n; C: i7 F5 P7 k7 z
$ p' }, X/ ~. g2 t3 g1 k& u8 e<?php
- Z! k2 [1 S1 m+ P, D" j! Q( {    phpinfo();    , i, P2 x, k; ^' k; Z4 E1 Q
?>
4 ~% h$ a* w$ s8 W9 B4 `) s8 V在浏览器中访问:
: W# c0 W- O5 m( A
" y% w% ~) E, zhttp://www.52os.net/phpinfo.php?id=1 正常显示。
" s( E# t5 G/ B* n' E+ O" lhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。4 s+ [- C1 G, R# l$ ?( x
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
5 f0 J$ v0 p; w/ C说明sql注入和xss已经被过滤了% W2 X- r4 \$ v. n+ @
/ G3 _: {1 A& M$ \- V
七、安装过程中排错* x' a7 m5 N+ L! t

: N/ {; d$ \1 T6 Z1.缺少APXS会报错6 E8 P7 i3 f" j1 P4 t! Z

5 O1 ?2 T- a7 L0 Z  ?3 ]" {configure: looking for Apache module support via DSO through APXS
% Y, q$ l  |! Q1 Z$ Cconfigure: error: couldn't find APXS5 z) u! U9 a) {
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。7 P. R0 ]" B% C! ^
解决方法:4 H; M9 M+ u; {" ?' ~

8 u% A+ P0 B3 dyum install httpd-devel
8 a/ u" W! i# n2 ^7 g- f/ ^2.没有pcre
* B/ g3 B7 s9 A# ]$ @" q; w3 N0 O" p7 `* l  k* m
configure: *** pcre library not found.$ ~3 e5 d8 X& s# S# i3 E' x, b  e8 b
configure: error: pcre library is required
3 ]. n( p, u- J8 Y7 V" N1 V" ^  x* D解决方法:3 }9 Y" K  b5 ]: Z6 \2 n6 H, [

5 {4 e4 [. K' O# A, T; ?: g& xyum install pcre pcre-devel6 O; o! }8 h$ D7 U& [! k
3.没有libxml2
7 D. [2 Q2 E) L7 z8 x4 h+ J$ O4 M2 A7 k7 m  H

* p5 q5 `) u; h; jconfigure: *** xml library not found.9 B' a  F- c: q  _1 w$ l3 A4 |
configure: error: libxml2 is required
, ~- x  \: [4 z0 o, h解决方法:$ b* D+ f$ a/ y# k$ |

/ h4 n: m" _6 l5 A% D6 Fyum install  libxml2 libxml2-devel. |, P9 d! m( X+ R
4.执行 /opt/tengine/sbin/nginx -m 时有警告
0 O' Z2 p3 f0 B" O3 C8 E
4 E' A4 m% N, a" ]# y: oTengine version: Tengine/2.1.0 (nginx/1.6.2)+ E9 I0 Y% i: J8 A4 V* E
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!' o- X, W/ y! h& X6 g; i8 u# C
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log* n8 t( v4 q5 I, e3 K6 h1 X
6 G* R- A: H) @8 [4 U3 e# v
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.% t. q- Z1 N) F6 |; l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
+ n7 i, l8 |3 Y2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
  m. C: O! x4 H- F6 f0 H8 g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"2 f/ W) y, F0 m8 z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
; g! V6 h) H% t/ e! u- H* L2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.& Y* W) s0 q8 i$ f3 E/ T, @
解决方法,移除低版本的APR (1.3.9)) U/ X2 j5 G( \+ {
' l) Y0 ~3 ~+ I( ?# P2 l
yum remove apr* m& [3 e% b% `5 S6 h. e
5.Error.log中有: Audit log: Failed to lock global mutex/ O. ?8 Z- ]. Q* U$ \

8 G9 q0 }8 S& I9 S# \2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
% g, W. a: d0 Q. ^" K9 b. tglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
7 ]( G, A7 j  o2 S2 t% f解决方法:
6 o  {/ W* G/ a编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
4 S' _) ]; @' ^) ?
2 c$ @- d9 \1 V8 l9 |SecAuditLogDirMode 0777/ x. p+ k' c( j# j; J
SecAuditLogFileMode 05505 d3 |2 _" }/ s+ |
SecAuditLogStorageDir /var/log/modsecurity
' d, f7 a' q8 A$ q& M3 g  DSecAuditLogType Concurrent
; T' {, ^% D( L9 o7 Z. i( |参考文章:
2 z! T% X- K2 I( l3 jhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
0 d. h; b9 Y0 F, j# q5 _http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-9 11:41 , Processed in 0.070186 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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