找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12972|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
$ N# N+ Z# S! ]
$ p7 [; w3 N6 o; r; C一.准备工作- h6 |$ d+ k! i9 e! n( {
' B6 {) v( y! P5 G
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0  s1 l1 r+ z; }0 b; ?
2 k# [( q6 @% j" N) x8 _6 P
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz3 e$ z/ O: T: a$ {

7 Z  G; A( Z: n( ]$ omodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
. v+ \1 y6 l3 f$ V/ \
" x9 a! e6 n6 t/ s; |OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
; r* O; _( Z  ^- _9 A0 S0 r4 ^8 e* m! ~- |/ O/ d
依赖关系:3 b5 p  ?6 V" Y2 \3 p" F
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
& |1 o/ N: {* C. f, q& L9 K( X% x4 {# M7 |
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel; O2 j5 U! ?5 {
modsecurty依赖的包:pcre httpd-devel libxml2 apr
. `, l9 [5 z5 _: f3 [3 R9 A5 l) Y0 o9 n4 [! P3 h( l* C# X$ E- j
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
) q  ^) `. P# L8 `: d- l& o! Q二.启用standalone模块并编译/ i% H& E/ U" H# j

3 j" s; y" I) j8 L下载modsecurity for nginx 解压,进入解压后目录执行:
  S8 H* F; {2 e9 @2 C+ n+ p" ]- {3 Z  o& V
% N; T' ?, S$ E/ `/ r* v./autogen.sh
+ p  b8 X8 }% O* z+ U) ?./configure --enable-standalone-module --disable-mlogc
" L+ s+ V* n7 b5 X# F5 m9 }9 Umake
; b( r' E* g6 N' u( I三.nginx添加modsecurity模块1 Q3 {/ \7 l* }% i; R$ {

; O0 I% u  J! w% d4 J, y在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:2 r- h. V3 a& v: s! Y

" N; ^' E% |7 R4 A* b% h./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
. a5 k( X) I0 ]make && make install
. ~1 z' X# \7 B2 x  U四.添加规则6 A8 v" B+ A* ]
% J/ M) {3 V" M0 @
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
. @. l8 r# y, w4 S7 N2 M8 h
+ ?" y3 q6 u8 ^6 h0 L: [* l* e7 U; A1.下载OWASP规则:2 d" O' \2 k2 w. O/ ]4 V

: j- |3 x+ q$ e! ?. Z, h7 Kgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
: X  h# K( s* z" j' G8 }$ u& H& @/ p' N5 Z9 Z+ ~6 v
mv owasp-modsecurity-crs /opt/tengine/conf/
! @. y. m3 `3 o+ n* |$ @0 q7 N
% ?  x4 Q  r$ y7 ~/ _: e$ Fcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
/ x7 B3 l# [  b2 u. z. W$ R2.启用OWASP规则:/ o0 V3 }* I0 P4 P
* @! o6 x# h6 o, i* C2 @- y1 M
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
( v1 w* L+ G* s+ u" R1 v+ z5 W! q& m6 w
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on; x% h' H* X. |3 P
6 M  k1 Y2 H3 o, N5 L, b
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
6 F7 @6 N' H' d' E
. E7 v0 \% g- a* U8 s/ c$ lInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
+ D2 v" ?/ k" \  }$ _Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
* \; K. T6 [$ X4 {Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
' B4 n1 y( o2 LInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf, I5 S; C6 _. y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf3 e6 r# x" ^5 @6 J/ t- H
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf+ I* r4 x1 t4 e
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf0 ^- b9 f9 L7 z9 k7 }
五.配置nginx
& D" P$ d. Y) s  G6 }6 j0 n  V* x5 @* g) i' }+ _5 v0 [
在需要启用modsecurity的主机的location下面加入下面两行即可:
) r) B% B! I, r
  a$ Z& Y5 Z- B: ^6 y1 pModSecurityEnabled on;  7 a  b. z( t  D: Y
ModSecurityConfig modsecurity.conf;
) c3 J+ R7 Y- ^下面是两个示例配置,php虚拟主机:& _( ^% w  [2 T0 B1 n
; c* y. [6 n. ~7 b
server {
" x6 w  @" r& P; V      listen      80;. [: P. E. Y! K* m1 C
      server_name 52os.net www.52os.net;
* k- {: _( o3 E3 e" _     
6 }; a" [( Y/ q( [+ r. p; i      location ~ \.php$ {) O# U( s5 w) u3 Q! o% N. S4 A. n
      ModSecurityEnabled on;  
, f: K" a6 C6 D8 D      ModSecurityConfig modsecurity.conf;
2 i: l$ h4 d' n; X- k- L
/ E+ B& p" e# R# C0 |      root /web/wordpress;
$ n1 [: V6 G2 G/ Y. Z7 l      index index.php index.html index.htm;0 h! {3 O" u  B: H- @
  # o# B8 b: o  {1 y; X5 ?
      fastcgi_pass   127.0.0.1:9000;; j/ A) Y7 E9 B$ p& L% n( R
      fastcgi_index  index.php;! S+ M% O( u7 ]
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;% O+ O4 A1 {/ [5 F9 c+ S" {2 [& g
      include        fastcgi_params;, P: `3 q, |: t: ^$ r; b" _( W; B
      }
1 I; u# K3 I4 x2 k' U& t$ q5 I  }, P' W- z# ]% G& N! U- r
upstream负载均衡:& [8 O8 z( w6 }$ p  v) F4 }. h' c
; n7 O* t+ d) p& }. C
upstream 52os.net {% j- K% H3 [& R7 b! u3 J
    server 192.168.1.100:8080;4 K2 h) p& g( O
    server 192.168.1.101:8080 backup;
/ m) D, Y; a6 T* {) i0 Z}
, C5 T1 @0 k5 \- r, z0 z! c7 }7 Y6 A; P1 u/ V3 [) q
server {
% c3 g7 h, @" E( I* G( z5 U6 |listen 80;& m2 L& N' ~: ?( k  h) F7 M+ c
server_name 52os.net www.52os.net;$ f1 \# m9 x, w

$ k* {' O, g) h. \4 ?: N1 p4 b4 alocation / {3 f# R. j- ^! Z& ^' L; _
    ModSecurityEnabled on;  
6 d. l' i* {9 j3 b0 h6 A+ |2 p    ModSecurityConfig modsecurity.conf;  
4 F! \: A9 c6 \0 p' N1 I! c0 b% K9 H: U6 y
        proxy_pass http://online;, \6 D' ~' L: ^- g
        proxy_redirect         off;5 X$ V2 b3 P/ f9 ?) z  e5 {
        proxy_set_header Host $host;! T8 Z8 T& {- {& N& A
        proxy_set_header X-Real-IP $remote_addr;
. k9 c+ e5 N! V, [2 l5 _4 Q        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
& H7 s4 ?. E) i, F8 A1 d3 E" r; r! u    }
% Y7 X  [; g1 K}  d4 K6 r* e4 y) v
六.测试. b4 C' V$ t3 }2 S; w) n- s4 Q3 i
2 P: N: x0 |, |, F( H2 M
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
5 D( m9 O8 \4 e/ O, ?
8 j5 @* G: M  w+ j<?php+ T8 g6 ^5 X1 U3 N: ~* ~' u
    phpinfo();   
, I- A& m+ v  `  r, T% c?>
& {" G* `0 u& N) g0 A在浏览器中访问:! W9 c4 E* c; F* ?: y  r

6 B( x! d# [/ {: s1 w" k0 ehttp://www.52os.net/phpinfo.php?id=1 正常显示。
! B+ \) c' R* n, Z8 A, j' X0 ~http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
8 i, m& S7 a# ihttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。  e$ V" a7 v# U+ y1 S
说明sql注入和xss已经被过滤了, n$ [0 C. u* v7 d: @- P) a
2 s; A' U) [3 V" @: {  e# @
七、安装过程中排错6 J& Q' L1 |/ d. v9 {
- P% n3 u. x' m; L  X
1.缺少APXS会报错8 y% R; ^3 d. |
% w' R* z: x8 ?
configure: looking for Apache module support via DSO through APXS
9 m/ |7 F7 S. I0 G2 i" v4 {/ aconfigure: error: couldn't find APXS$ O# q# Y' G; |5 b, q" D
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
( q! \/ D) i9 L5 c6 C解决方法:
$ J' x- P" B/ y2 Y$ k& ]  j. G/ u, J0 P; i. X
yum install httpd-devel
9 @. l2 ]; n! v7 L' @2.没有pcre9 E( [" a" ^2 J+ q% U
  u8 g7 h: `6 q+ p2 n# t& p
configure: *** pcre library not found.# c0 w2 `5 L) s4 V) f0 \
configure: error: pcre library is required
7 ?% _! }; d8 r  z* l+ ]解决方法:1 i7 T& T+ W. Z; `1 O% M. q

0 `! m1 Y, e' d* [) {yum install pcre pcre-devel/ T. w' |/ Y! t- g: l
3.没有libxml2, h% j* D$ T# S

8 w' ?0 I! _6 S# |1 x% Q+ |; f* b$ x! M
configure: *** xml library not found.
+ t3 [6 g9 a+ i4 r8 s! E5 Gconfigure: error: libxml2 is required
1 \! ]% d: H+ X9 `解决方法:
3 O7 e9 \/ O& D+ V
1 H- a7 ^  S- V# N" n  |- Oyum install  libxml2 libxml2-devel6 q6 y$ a& }" E4 ]+ f5 k5 C
4.执行 /opt/tengine/sbin/nginx -m 时有警告
/ M1 O& b# E( I9 n+ Z3 }! U2 r% S# I- m! I4 K' e
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
% a% z% G" K& w) e. u" [$ ?: T9 i4 Jnginx: [warn] ModSecurity: Loaded APR do not match with compiled!" Z- u( f) a' ?1 i( q& z
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
3 N* d- S8 f1 i* u- {1 f5 I% T9 d2 P' V  A2 x  ?4 z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
/ b/ g+ }9 f: B$ f2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"- e9 l* ^0 {4 q# t! s
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!2 }1 _- s7 r" {4 V: p/ O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
7 K' c! [* C/ a& F# Q8 ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"" n' B7 m2 W5 ?
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.# B/ ]! \% I% D3 y  w8 L
解决方法,移除低版本的APR (1.3.9)1 v9 J9 K2 ^/ k8 P: @8 N
& j% X9 m9 @, K3 v! [0 ^
yum remove apr0 v& {" T7 Z5 v  n: S! f
5.Error.log中有: Audit log: Failed to lock global mutex; O  b7 X9 }3 l+ J+ {6 `

& J+ |- b, Q/ E7 X( k! t2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     4 _$ o$ R! M' X0 O2 O! `) s# v
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]: T  j3 s) b2 J8 n7 m* y* D) ^
解决方法:
2 v* @# v, K( M4 p7 `( ~3 P编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:7 r$ G- H7 m1 {( ]- a& g
1 [% [, w) z& Y( l, T) X
SecAuditLogDirMode 0777- M+ F6 u9 a+ Z" _4 ]; ^+ W
SecAuditLogFileMode 0550! R7 u$ M3 j0 o
SecAuditLogStorageDir /var/log/modsecurity0 e$ `+ \. a) ^" X- D
SecAuditLogType Concurrent
" V# R; o" {! [: \参考文章:$ |3 ?8 l2 c4 T8 x/ Q0 c& M
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX: J& b1 i) y$ f' \7 l: d) k; G. @
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-31 13:40 , Processed in 0.060612 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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