找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9854|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。$ c: F" g% }9 ]0 D# D0 s, w
9 c+ P, S( d9 `
一.准备工作$ N+ g! H6 I/ r- P9 X$ y
  A. D2 U! T4 {7 f9 ]3 s) H
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0% j* k3 [. B- C( S1 u
" c3 z5 K& p3 V4 z
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz6 k. _* s$ O" O3 W9 C
2 G* W% k: n, }( F% W' |
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz9 w& u9 W8 _) g
+ ~1 I( J0 \5 M! N% ?. r) |9 z
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
$ J+ e. {1 e/ U+ Y8 O
' W+ B$ _: i2 g1 E% p. ^! F# U依赖关系:$ M2 G. q7 B$ s( {4 n
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:% o8 w1 {) Q7 F: ^
- ^1 a; v# j9 ]  `& \: n  l. _
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel/ o' z/ E+ e" k) `- \" D5 s
modsecurty依赖的包:pcre httpd-devel libxml2 apr
* _5 S% e3 d4 l! N/ v1 z9 A
% |' a# p  Y- R* W6 G6 b0 Tyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel( _, f4 i; P; k4 x: m7 p
二.启用standalone模块并编译
4 \: k. Z1 \2 _- j+ o" J1 M5 v, u7 k" @) f) D$ W( d
下载modsecurity for nginx 解压,进入解压后目录执行:
1 t, L. H& ~3 d) S& F4 q3 O# t" e! i' n3 o) B4 n
./autogen.sh
6 H* {- K  U9 F5 G7 b" O2 V./configure --enable-standalone-module --disable-mlogc4 l* q- F( Q3 Q
make
; d/ g/ |- w& L- d' K6 Z7 V- E三.nginx添加modsecurity模块
8 U$ p8 a8 r7 b9 Y) Q  M8 \1 ~, L. b+ X' K( n0 |' x
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
6 v" S* J7 h5 f2 L8 L- F
# y0 ?8 @& b( U5 [3 a. h  U' @./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
& N' m3 n8 w4 K. ?* B: u6 i" Kmake && make install" H) {7 F# ~8 W) W" t
四.添加规则
7 o+ k4 ]8 U$ B. P7 @
) k0 Z0 m0 Y9 q' u: Smodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
' |/ T& w+ N5 p1 y( |
8 x( o6 j. T/ J6 X1.下载OWASP规则:
1 V  {: |, C8 F
( X5 Z# x& _' f! |. lgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs+ h  c' `. y4 ?$ w4 S- `
: R  M. Y3 n6 w; c  r
mv owasp-modsecurity-crs /opt/tengine/conf/
9 C* E+ T9 Z5 @' a/ F  Z1 _3 y
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf) |) j- T3 ^! R$ x
2.启用OWASP规则:; e) J- R8 s5 x& @7 R

1 T1 n) T  D7 p& ^, T1 {8 U复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。. |7 {8 A* _% H: P- Q# Y7 E

" p" O: X/ n1 R编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
3 c( O( p4 Q0 {$ h
- j* X4 h# L  r  Dowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。3 `. S: B+ P6 T0 d
7 Y  z# \5 t; Z% k
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
' _3 ]7 |$ o$ u$ H. G; I' B, UInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf: H& T5 Q7 n+ q: l5 p0 V: y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf% ?0 _  b7 R/ e0 @/ `( p9 [
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
# }& H) z: a; e) tInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
9 X! C# P- G* Y* G3 {Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
5 \  w$ G2 @) BInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf% ]0 j+ v& `* ?$ b7 n
五.配置nginx8 v6 A/ D8 W1 @6 p3 F) Y
% a5 S1 d- E" t: C4 }/ W/ g
在需要启用modsecurity的主机的location下面加入下面两行即可:2 a, ?+ @0 Q$ D& f

4 Z. D, a9 V  v7 f5 G4 C+ UModSecurityEnabled on;  
' e% V' I3 \& WModSecurityConfig modsecurity.conf;
7 ]. R8 m2 x7 Z: {3 C下面是两个示例配置,php虚拟主机:4 L" A# d5 }8 A) Q% v

1 n% n$ j' }5 [0 Zserver {
7 X/ [8 ]9 E9 b3 R9 M- J      listen      80;
  I6 x: \2 X" ^7 G; r5 L- x      server_name 52os.net www.52os.net;( p& O! y) ?" B% m  @8 e9 t) X
     0 a3 y" F# g; z
      location ~ \.php$ {
: q% D, Q& G% [1 d: `. e9 C3 f; t      ModSecurityEnabled on;  / b" l4 U& U- {% F0 q6 ^
      ModSecurityConfig modsecurity.conf;
" F# R4 K/ V  Y1 i/ `* h
* j- Y( }! `) a, H/ b, q9 D      root /web/wordpress;
, P! h( N3 O0 G6 w  x5 W9 B1 _      index index.php index.html index.htm;
' E# W" J' A* x# ]( L1 x; L1 c  0 K  @& q" B/ r3 y
      fastcgi_pass   127.0.0.1:9000;
! m2 G  q6 v5 o' q' O; R      fastcgi_index  index.php;
. p5 L( N' O( v) J      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;; i! I/ S8 s$ t& f2 \8 `
      include        fastcgi_params;+ H( O3 h% t( V
      }
7 U) ]3 a! W% ~) D! Q7 H  }8 a+ c" K- e, u# S
upstream负载均衡:* m! B0 p5 @1 z9 R5 L5 G
2 E2 k! F. _- _0 L  z( ]& c4 l
upstream 52os.net {1 P  c) ~- z8 {. W  U  u
    server 192.168.1.100:8080;  X# L: S, x- U1 o3 j/ g9 F, p
    server 192.168.1.101:8080 backup;4 H- z# {; [" m1 l; @% t) c
}
2 B' O4 f! u5 R6 X- ]+ v$ W5 L: B' i! A7 O
server {" r9 h3 l' k# J+ g$ D6 h+ j" \
listen 80;
/ Y3 E5 g) c# Y$ x/ a1 {' fserver_name 52os.net www.52os.net;
) |8 G- U6 f( X$ ]. X& n2 B( ?* w# \/ |4 A% p6 n
location / {
' S- q& x" e& D    ModSecurityEnabled on;  ) N, |- b9 ^* ^& B
    ModSecurityConfig modsecurity.conf;  , `- _5 f* j; [
8 x0 w/ w3 H( O1 s6 d* ~
        proxy_pass http://online;- z1 o6 N+ W9 p. V) X- N2 p4 G( i
        proxy_redirect         off;
1 a# O" y4 B8 P; v        proxy_set_header Host $host;
( a& y! l2 H/ P; g        proxy_set_header X-Real-IP $remote_addr;. Q2 D8 q+ a& P' @, K
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
0 x( v$ W( K4 l    }$ g( `8 N8 V6 G4 s
}; c/ P6 E' c( i& t* h
六.测试
/ V" R1 I- L/ n& v; m8 q. A+ g% Z
$ ]% C0 i; a: A我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:" b  I) ^, P# E) w: v6 s
7 P& G( A6 @) K8 Y) r( \8 {; S
<?php& F! [/ p; L& u- W, ^5 @- ^+ c. E
    phpinfo();   
/ @# |' ^5 |6 J- E?>% [3 r/ O# Z. Z: J/ j* M
在浏览器中访问:6 f1 F$ O! Z/ y% ]' c/ F- K$ J
3 B6 R5 l; g' [7 q3 O, q1 {5 K
http://www.52os.net/phpinfo.php?id=1 正常显示。
- i, e, ~0 f1 r' x' d+ l' uhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。  h! ]. K& v: p
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。0 h4 w. l7 M# y
说明sql注入和xss已经被过滤了6 G1 r6 i/ w" i+ T

/ Z2 [6 x0 z* z七、安装过程中排错
$ h" ]' p+ U3 z0 H, N* f  r: z- o; c( j8 K) u; I
1.缺少APXS会报错
- a7 W% l# z1 Y1 C2 [# R# U& p7 y" n" N' e# o" Q+ u
configure: looking for Apache module support via DSO through APXS8 t& g6 i1 s8 j4 t  ~
configure: error: couldn't find APXS
+ x  E3 j) c" f. i# z$ Iapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。: K6 q8 G$ `8 T' o8 q/ k3 A
解决方法:
: W2 w) x( s% H* B6 S  P
; c% {  O' C2 H1 t/ K/ Ryum install httpd-devel' M9 q" L/ D$ D7 d6 j& R
2.没有pcre8 t4 L4 n3 d* J2 |- e' p3 u

: Y( |9 a9 c9 h  xconfigure: *** pcre library not found.
  z  E( `6 F5 [) M3 \configure: error: pcre library is required) B' q7 V! R( S( e
解决方法:1 ]3 t# N) Z1 x: l' D
2 ^* L, _' ]) _, ^
yum install pcre pcre-devel
! B% d. E1 {6 m5 j8 Z% w2 c9 S$ H! I# O3.没有libxml2
% K5 ], {) G! ~% R: D
3 g& D/ T  M4 Y# s! C+ r# o( u
9 R# }8 t/ C5 d% E5 F1 y6 Z. mconfigure: *** xml library not found.
+ d5 h4 I8 J' O; [8 Oconfigure: error: libxml2 is required
" x' C5 ^& n" \4 Z# \解决方法:
; w1 }2 c# F. M8 A' P! z0 f, n8 \' ]9 ]5 @) d. @8 g7 ?
yum install  libxml2 libxml2-devel
0 G% Y* |! p) X4.执行 /opt/tengine/sbin/nginx -m 时有警告- ~' l, z; _' ^, I2 `+ s. I9 {
6 G8 K" }8 K* Y4 W4 `* M
Tengine version: Tengine/2.1.0 (nginx/1.6.2)- }# u4 s" B( t; k
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
+ Q6 M  ?( P0 \0 c5 X7 U原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log4 P0 M: o4 c1 g% v/ s0 ]& c

$ `  x# @- G& c( N2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.4 A) W4 h, x- |0 `  G0 ]! S
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
! M- a+ d, D+ L5 o9 V4 n: h2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
$ \4 N& v& U! m7 ^2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"" ^0 S* d5 W4 l* Y* I; i% N. C' F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
( d" k/ n" J2 y5 y5 e2 h9 J4 `+ c$ G2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
" Y: ~9 Y, B& Q# W: k5 |+ |4 X解决方法,移除低版本的APR (1.3.9)
- g6 I8 d2 o. b
+ Q; e9 H- D2 O5 R  Pyum remove apr
9 A. r: N$ J6 W' V+ B5 @: ~5.Error.log中有: Audit log: Failed to lock global mutex7 \8 R- u4 s( g1 _3 J$ y

' I, e) z6 _8 H: e$ C+ @2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     . K; Z" g: G) V  F
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
$ i! X, B: N3 ^( a! a解决方法:8 S: h5 Z/ d0 N2 E
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
+ d. W6 y! B/ H/ Q6 g0 e( z" J
2 R: c4 w8 [1 [2 J) Y1 j0 G* mSecAuditLogDirMode 0777
+ g3 y1 J+ y% t- C9 m6 z: ~SecAuditLogFileMode 0550/ `$ R. p; ~* |4 X/ M* ~
SecAuditLogStorageDir /var/log/modsecurity& e( w9 t, s! y0 C4 j  E( L- _
SecAuditLogType Concurrent
  o8 O  A2 D" t) M1 ~! x1 ~参考文章:2 ]- N, y0 M2 f
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
, U! h. x* B9 C1 e" chttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-2-7 11:24 , Processed in 0.058800 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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