找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9841|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。+ f7 w5 Z; P$ F* z( ~5 B- L
4 ]% P. |  O; b, ^
一.准备工作
6 \. M" {9 j9 ?) S! h$ M0 h$ R, l
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
) j9 b5 K" {0 a$ C4 m8 [1 ~- }* \( u
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
0 |$ N6 e# e1 x+ K* C0 v4 Y5 i9 d) x7 C9 R' \, q" S6 f' ]( c
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz) v) P+ H9 \6 S$ G! [

# H8 r" t# ]* s# e% d1 j$ mOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs" m3 }8 |" T8 @7 U5 v- q

. \+ D- Q/ _, |/ S依赖关系:
* s- Z9 {8 o( |7 A3 t# Stengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
7 X8 O- v2 w3 Y% s5 G
6 a( ~" ^2 ]  M9 R% ^) Pyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel: A, x& n3 ^$ Z( h
modsecurty依赖的包:pcre httpd-devel libxml2 apr
; L. P( k( u6 c3 y
1 N9 B3 [# G. H1 q3 yyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel' s; `7 [9 X+ h2 s; h$ E
二.启用standalone模块并编译- K) m" d  g5 F' [" T3 [9 @5 r+ s

: ~8 w0 \; C& L) d5 H下载modsecurity for nginx 解压,进入解压后目录执行:
3 _4 V9 m0 U& F) B/ N3 D
/ y8 I  z) w5 m  l) I/ ~./autogen.sh' @$ a2 K# }; Z: S
./configure --enable-standalone-module --disable-mlogc
7 ~. ]9 [# x& Y4 C( L/ Bmake
: k0 }7 i5 P+ T4 S三.nginx添加modsecurity模块
/ G2 j9 G+ W6 m. I9 A" m' E' E4 m/ H$ }4 p7 ?
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
3 Q9 U3 e4 ~+ l
, @/ \) ?6 @1 i9 r. w. t: t" y./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
+ \# m( Y% ^8 r+ G# ?* b4 a1 |. F* imake && make install
& y$ g' s8 d: H; Z8 t四.添加规则  k  t4 D$ H8 P5 {. J

& t/ A2 K. b$ O6 V6 Q5 F& Vmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
2 v& e5 O( f  y; i" V5 {- B+ m; i- W& ?+ V* w. W- M  N/ @6 E
1.下载OWASP规则:
9 u$ ^# |# g3 P% n; b; f( w$ T: k1 P( u9 T" w  Y
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs/ Z! p- ~8 M$ `4 i" Y
% N& ^! M9 P* a: q& v+ b; q
mv owasp-modsecurity-crs /opt/tengine/conf/( M2 m# g. \6 e- o+ H$ v5 m
+ x3 {4 t5 F# ^) E3 N! Q9 C
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf/ Q4 L, _7 ~6 A5 S  O
2.启用OWASP规则:. R2 [' \9 u2 Z3 ^6 I! W

7 [$ ?7 y; G, C% e. }复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
! F. W5 @! K4 S( F6 ~% h' T9 y& K& o" L" {# y7 d8 u
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on, z, M& F6 ]* }' Z% X! r( q: T* B

, o) ]! k4 I; \' j; d  J9 [6 W8 T: [owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。- h4 W2 x# N0 t, V
! X3 o$ @( Q9 W7 y
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
, E; Z4 F* A" z, v8 o  A, w( h& [Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
' m. V- m" n0 c: N3 XInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf0 D8 N& t5 ~* I: n6 k2 ?6 @% y# t1 l; }
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
# G0 G# I1 f" RInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
( I0 L5 y( X6 h2 L% bInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
" a1 V& H. Z. S! G, tInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
$ Z: ]9 a% z7 K' u五.配置nginx6 @+ }7 G8 V$ p! x5 R& \- e7 f& w
& E8 E# o+ k6 h
在需要启用modsecurity的主机的location下面加入下面两行即可:4 x6 \1 |9 O* _1 v6 h

2 S+ J; N8 t; z$ lModSecurityEnabled on;  
5 Z( N% A# g( o6 w* kModSecurityConfig modsecurity.conf;3 M# Q5 g& N+ d: ?& m: N
下面是两个示例配置,php虚拟主机:; n2 C! z- s$ P% l! T: a6 ^  s/ c  B
6 |& y: X/ T5 h! \4 A! _
server {
$ `4 i; Q8 e" I5 X* W  s      listen      80;
2 p9 n2 ~7 [$ _& b- T# C  D3 Y' q" }      server_name 52os.net www.52os.net;
: i9 B) v, f* ?/ o       ~2 D: u7 l0 _- b% y4 g
      location ~ \.php$ {* P* w+ V+ F( M9 r* L$ f) S  o
      ModSecurityEnabled on;  : \  L6 X: V% t- b
      ModSecurityConfig modsecurity.conf;
. \5 m! }' L# d: S6 d0 H  H) _5 Q! l
      root /web/wordpress;. ?8 i$ y# b3 f% i5 q* a- Y0 k
      index index.php index.html index.htm;) `  |7 M, A0 G- Z! G$ B/ }
  
, Y# _9 R/ |4 A8 |$ H' K      fastcgi_pass   127.0.0.1:9000;, C, b+ Z/ b$ e7 U
      fastcgi_index  index.php;
3 J2 A5 F: |9 K/ Y; M! c4 J7 z0 U      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;, E6 N; w" k) h4 c" A
      include        fastcgi_params;6 f7 x; o0 ^- H; Y1 y8 E( ^
      }
4 w# u" j& N8 r$ _/ P  }
( V2 v1 O, m* j* i! o  A9 iupstream负载均衡:) o. L2 k& D  l! C0 U1 _
" i# E# U( V& [; p' T0 H( `
upstream 52os.net {2 s  ~% L: T( m% H
    server 192.168.1.100:8080;
$ _! f- N7 f- @2 B4 Y    server 192.168.1.101:8080 backup;7 ?$ P1 F1 S) A" Z, Q% m" Q
}
6 S0 B5 \* L( ?& ^- s$ A  O: n. K" g1 |0 c% V5 m
server {  a; I& \% Z* y1 X
listen 80;
2 w: n2 b% s$ d0 p; l: nserver_name 52os.net www.52os.net;
  c- S; G: M. T0 C, R: {/ k5 k1 c; s$ ?5 b0 z5 Y
location / {
" y5 x: R$ c# Y4 t    ModSecurityEnabled on;    r3 y6 F! x0 V" v' W
    ModSecurityConfig modsecurity.conf;  & z" w/ U# S- E9 L* D( ?, d
* T& C+ }% q* H* {
        proxy_pass http://online;5 `' s( _% H* T# N* \$ w( |
        proxy_redirect         off;3 a# N/ K6 j' g) n
        proxy_set_header Host $host;
4 l% R+ i7 y5 m, ~. z        proxy_set_header X-Real-IP $remote_addr;
- p' H9 o. k; ]& \7 X5 g        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;; _: }; {/ K5 @
    }
4 R" y: O% `& F, D3 ]}8 T& O2 ?/ z% U4 {
六.测试, g3 K* T- b4 m, B

4 h/ p/ B- s1 v- }; B我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
& w" l( M; [1 o& ^
  U5 O4 W$ N/ J' w5 V  Q. z: |$ G" ~<?php
, ^% V3 I, X  K4 h: i" z! Q    phpinfo();    / s# X; x" V: y# L
?>
7 ?& L0 F4 D# J/ K在浏览器中访问:. _: [( p* q: w

& u$ q/ M$ `- S& ~http://www.52os.net/phpinfo.php?id=1 正常显示。( n/ S4 `! n- W4 z5 T- k
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。; ]% g. ^7 [6 b9 ~  x" ^* v
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。, u; Q  A& b" p, H" y/ r$ X
说明sql注入和xss已经被过滤了5 N* w7 j! ]+ d, s1 m
2 L, h7 B& U+ a8 F7 D
七、安装过程中排错- r, `, k$ ~1 f  s  |% y! D

6 u, E' z9 L; a' R0 q  c1.缺少APXS会报错
! `" ?, k- M( \6 d3 \; A9 r  ^. |0 O6 x" \* ]1 z& D
configure: looking for Apache module support via DSO through APXS, M2 X; w# j7 r5 T' r& F
configure: error: couldn't find APXS) v( @/ s" |( L& g& v
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。$ c, T/ s0 o8 w/ g9 c
解决方法:
: Z2 U2 V( y1 I. F5 J
7 v+ `! _. B9 c1 _. n# n7 yyum install httpd-devel; s. x! {/ e5 C& g- f4 u
2.没有pcre2 \- `$ I+ i- }4 Y6 q! L
3 L4 k. T0 k3 S  G+ L% a& M" y
configure: *** pcre library not found.$ F; Q1 W, l8 y$ \0 b' |
configure: error: pcre library is required4 Q) u) ^( X/ s( K7 |% g* f0 M
解决方法:
: U6 w, \7 L# m% ?0 B6 ?& k% H* U7 O7 k, r4 n/ n7 F/ C; }
yum install pcre pcre-devel
/ ], B: |4 q: }3.没有libxml2
6 [& G4 {/ k; n" k  W8 a1 @: O4 c  a$ K

  Q9 V2 p: N9 p3 v0 {configure: *** xml library not found.# k6 ]5 ?# u" E* W8 `0 f
configure: error: libxml2 is required$ A) x- ^1 N% y" r
解决方法:0 N# x4 U4 s% n6 r; z# N4 @

. T9 j& J- x: w" kyum install  libxml2 libxml2-devel+ v% D& F3 o$ I' Q5 ^. b  Z- T6 ]( O3 X% h
4.执行 /opt/tengine/sbin/nginx -m 时有警告$ F; u. w3 ]! }
% L/ a+ x& }# e1 `3 {/ T3 k
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
- a+ k6 S8 d6 F$ M% d% snginx: [warn] ModSecurity: Loaded APR do not match with compiled!' _$ _5 O0 h4 d/ ]! Y8 z
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
3 X" m, q: @5 z, B/ E+ v+ n+ c9 v; t1 E1 ~: f4 u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.& H0 k! V* o; t0 @
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
1 c# W3 p4 k, f- J8 L6 _+ d2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!/ _% n0 ]% Y( y, W; _+ U
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"0 E+ Q7 ?/ W" s6 q! M
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
+ s! R, `& H9 ?# }  g8 E2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
! D2 P% D4 _$ w# H! |解决方法,移除低版本的APR (1.3.9)- A! P' p5 O; c: s( T4 v0 s0 Z: u

  e: s& M8 f  Iyum remove apr
" H* w$ H7 y, Y" l5.Error.log中有: Audit log: Failed to lock global mutex
) m: N8 K2 U# G, k% r2 s
0 u4 {  b& X$ t/ Q& L( K5 d2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     1 O+ h/ M+ d  M8 E; l) g6 \
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]' X; Y7 O- U( h) x! A$ w5 Z9 f8 F
解决方法:/ Y; f4 }6 I+ S+ h3 |
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
/ q! S3 |; ]4 h! z7 @+ ~* i" s# e
; h9 \- `4 ?* S; NSecAuditLogDirMode 07776 ?' g/ E! j0 J: y
SecAuditLogFileMode 0550' D, T: I  D* k8 A
SecAuditLogStorageDir /var/log/modsecurity
( X5 {* K( ?; x- BSecAuditLogType Concurrent0 |6 Z* Y  d9 q
参考文章:% N9 S9 |& z) w9 \+ N
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX( s6 V) ~$ l$ z% S
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-2-6 08:42 , Processed in 0.075705 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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