找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13125|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
7 @! W9 y/ S  N4 l1 B! e. o/ o* f' S! h9 ?( I" K- S' b
一.准备工作( f9 p2 K( k, q5 `

) T0 s' p3 v8 v; M9 p4 Z系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
& {9 D' {+ C& u& Q% [1 p) \+ W+ B$ b1 S; B8 Y
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz  N3 ?2 X, D  V  y5 x4 U
9 |3 V7 C- ]9 F& o( M. d
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
* R3 ]0 x5 `' q! r
' `% b6 o6 Q- e) z* F+ x  vOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
. d2 A7 U6 b, H, d: @
4 g' G4 ]0 _/ R! M( B  A- x/ r3 y依赖关系:
6 z# V, m8 n6 w1 etengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:9 _  M) T! S$ z! U& s7 V0 t7 B& j

+ C% j" I6 U* |3 B2 {7 r& Lyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel) _( O! H0 V: h# ^. M
modsecurty依赖的包:pcre httpd-devel libxml2 apr
2 j( D  H' Y" [2 m: C8 Q- ~) F9 U. c3 m( D
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel1 y* y; v) W0 W8 i9 ]) ]
二.启用standalone模块并编译
  r/ ^) h) N1 Z6 F  x5 k3 }6 _$ p! F5 _- x6 s: p+ r
下载modsecurity for nginx 解压,进入解压后目录执行:6 T9 e  o, c5 |. y  o: c

1 t/ y$ \7 e$ m) t./autogen.sh" W$ ~1 m9 T! N: R
./configure --enable-standalone-module --disable-mlogc
  W( Z8 _+ W: F: h6 Mmake
& }8 Z( F9 y6 Q8 K9 x* m三.nginx添加modsecurity模块
, L& d$ p6 b3 J& A: x  ]  X2 y1 z- f% C+ Q2 Q" x2 {- Z+ X, a& l* S
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
: k3 h. a0 o! n) O( e8 P# y/ U: V& E7 T* U  f0 {1 J5 u% d
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine, t- ]$ K. ]2 c9 }
make && make install
7 H% T* x' \& `* Z- F" i# S四.添加规则
" M* Q  g" b9 G; r4 U7 n& e8 h* \; @0 }  c
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
' h6 I% c; R' g! E
( p0 h7 Y. W8 I( M7 {# {1.下载OWASP规则:
! |0 L" d7 ?9 y; @) l3 }1 F8 e' i5 m9 d4 j* X: h
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
: i& Z# w. X3 G8 _" x. J. V3 r5 D2 n7 X  R
mv owasp-modsecurity-crs /opt/tengine/conf/1 H: ?: a, H: Z
; O$ h0 @8 x, k9 q" R5 c! k
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
. _* a# F: a, m' `" ^: {# P2.启用OWASP规则:# z" K) {. k4 Q4 Q* t% |
4 ^; i+ I# Y) o9 k1 w
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
% w9 h3 }( H. ?% k' h; x6 c( @: |" B# L9 G0 ^0 y
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
! s6 Y, i4 m2 `0 i
& s! B+ j2 l" p' }, N: {6 Jowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。  [) ~9 S& j5 D, t0 \# c

: }* e# g: U- n, a/ C7 T+ X3 [: Y$ t4 GInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf; \& {1 v  Z6 `+ O* D
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
6 @% I6 O) ]2 kInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf" e) s6 R) g1 s
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf4 g/ {- K2 C5 [" [. V6 g) A
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf% U: n& @/ n: C% u
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
5 W/ H5 ^8 b* E9 N$ u2 XInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf: `* ]4 D  Z* J3 n' P& F
五.配置nginx
1 w8 a! }4 F% S! y6 \- g7 P/ k* ?; c' N, O
在需要启用modsecurity的主机的location下面加入下面两行即可:0 p* j3 p  i3 `& |# P- I
/ i, W8 F. w" S& w* J
ModSecurityEnabled on;  # J- o) W% _* N+ ~! h* P+ L. V
ModSecurityConfig modsecurity.conf;( r( E5 ^% c  a7 \0 U, E
下面是两个示例配置,php虚拟主机:
$ M8 O2 F/ w* }# P; S$ ^' h4 ^! C9 z  B( v5 p. M7 K
server {9 `1 S2 C" ]9 _, u" z, I1 ^( m
      listen      80;
) _! v/ i) g! Q  x. @0 m9 J) L      server_name 52os.net www.52os.net;
. Q( J# n2 S. R6 V! u0 X     
1 z: R4 `9 b: [' |: s5 l! z      location ~ \.php$ {5 g% d& x; B0 y8 d
      ModSecurityEnabled on;  
. L+ Q* [0 `0 z/ c  `* f$ R5 q4 j      ModSecurityConfig modsecurity.conf;/ K0 T  Z; r# A( p5 V
' U- Q- t! _  f
      root /web/wordpress;
9 F: @: B: y- l& F: |0 S* y* Y      index index.php index.html index.htm;
! g1 J) C8 c) i. Y+ J; D$ \/ G  
8 T* |+ {$ R3 o* p6 j; _0 r" ?/ |      fastcgi_pass   127.0.0.1:9000;
- [4 F' ^: {' N9 e- [1 l  |6 J      fastcgi_index  index.php;
. k8 T  W" r( U      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;$ V0 a, B; ~$ S# }% p& C4 m
      include        fastcgi_params;
* v" \6 J0 r: G, d8 B      }
7 i6 a; t0 Y' e- K3 [  J  }
  J# I( |6 K1 @% |' Y& O" Eupstream负载均衡:' l8 V, P+ l& i5 E6 Y6 z
' [; Q9 P9 R8 p/ q9 r. c
upstream 52os.net {* ^8 x; Y" H8 Z) c5 Y; u
    server 192.168.1.100:8080;
. [8 U; C+ u$ z+ {! `6 u. ]    server 192.168.1.101:8080 backup;4 r2 }* T1 E0 ?* Y8 S$ @, j
}) s+ y8 W4 J3 |+ _" M
0 ^& a* \& f0 l  ~1 s2 q$ {3 B
server {5 x3 Y7 C& [& ?. q/ ^' e$ L0 L  I+ I
listen 80;4 i% ]+ @5 f4 X2 y7 U( d+ M$ K
server_name 52os.net www.52os.net;
5 N# ?( u1 V) j  k$ A' [/ y3 G5 x, o& A4 c. q8 \; v: O
location / {
+ s' R( ^7 f) Y- ]; g0 C3 y% z    ModSecurityEnabled on;  * I! }/ R- [$ K' N8 a: T* }
    ModSecurityConfig modsecurity.conf;  9 v- Z1 T9 M: ^

3 m) l( F! M; H! r        proxy_pass http://online;2 J- u* _- J, B& [
        proxy_redirect         off;
$ h$ u9 g$ U: i, c' a        proxy_set_header Host $host;
4 r- m% e9 C7 D        proxy_set_header X-Real-IP $remote_addr;  k; w' m* P% e) d; o* h
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
$ B0 ~7 c2 m6 G) \6 k9 [& f    }
# ]) [+ C/ T8 A" l' L}
& B& l6 g) t! @六.测试8 X2 A5 \+ r( W" j

% J2 m) N: t7 g" q  k* ?2 b我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
9 b5 [5 e% D. f' v9 Y( Z
. N$ F: s5 E& t9 N; d<?php; N5 j+ x/ c1 H3 g% }
    phpinfo();   
) I9 d! m- [. E?>2 \. d+ E: ~3 h8 @5 n. V
在浏览器中访问:
* X. d1 G1 K, X9 x( x# z2 J: }( c9 |1 O: }
http://www.52os.net/phpinfo.php?id=1 正常显示。
. d2 c' x  V, ^; p0 j8 d0 Qhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
. J7 o: a, a  ahttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
. r. B- l8 B9 H3 X# T5 k  C说明sql注入和xss已经被过滤了$ E' t" t: D7 D9 G

; L2 [1 ^2 G8 _七、安装过程中排错% c. b" R( W! m4 J
6 S$ ^" c0 y2 ], Z: R
1.缺少APXS会报错- q# y% @5 e( W
2 _' ~9 n. Q2 w
configure: looking for Apache module support via DSO through APXS
% t9 E4 z* p2 J  F2 s0 nconfigure: error: couldn't find APXS
& E- y! }* K; w+ Tapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
# ], {9 u* B2 Q6 O解决方法:
; S& c+ C0 r: d$ d0 U: }4 ~
" t' S. L* e$ D7 g% M2 h7 eyum install httpd-devel
: l( ], B. |! b) T) P8 Z4 F2.没有pcre9 c; `/ e( T& E+ m) _* q0 s

& v8 K  s4 d. S! e, H& e& vconfigure: *** pcre library not found.
8 F" A" K! T! I3 q5 o# a- bconfigure: error: pcre library is required- m$ Z# ?# u/ k# X+ a( i2 y# \
解决方法:
' G, ~2 h; b* V- _, ~$ r, E/ h
3 V, z3 h1 s# ]3 }9 Cyum install pcre pcre-devel: ]7 O; D4 ^. `
3.没有libxml2) G7 D! F4 P3 p$ S6 |! G

  X+ j; A2 E- i& p8 L- B4 |0 E8 F/ O6 c$ |0 E
configure: *** xml library not found.  P: T5 a& m' T
configure: error: libxml2 is required
5 o3 o" z0 ?: O, ^解决方法:
6 z, ^6 x, c  \: I
6 ~& c+ e6 A$ I! M6 m! dyum install  libxml2 libxml2-devel
/ t+ q! {3 {/ u3 S' t4.执行 /opt/tengine/sbin/nginx -m 时有警告
- ^% g: J7 p$ z
* |% S5 N: I( {5 _" z0 h3 aTengine version: Tengine/2.1.0 (nginx/1.6.2)
2 \: b) \1 ^* ~  Rnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
4 o; K8 E, z6 z$ {$ [+ \原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
8 j3 s$ b: g7 B5 n& d9 Y, s1 b! v( l/ f# u. ~/ D$ V1 }) J8 ~: m' R5 Q( W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.: A' C( Z+ J% }7 S3 Z) \1 j
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"5 K) l7 e) c. z2 G- M$ e
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!, F& q9 c; |$ g3 ]5 F1 q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
4 @2 e' }# S5 T/ _: K9 [$ `; d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
: B) {. J# `! T, [7 Z# ?5 ~2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.6 v2 x  d3 |# j+ q% S$ ~- g
解决方法,移除低版本的APR (1.3.9)& W" i5 v7 J+ d& l* N6 ^5 \3 ^
; @6 \0 G3 m5 x: X* t2 Q
yum remove apr5 W0 t' w% F, ?% P' ]+ b
5.Error.log中有: Audit log: Failed to lock global mutex! `" ?% P! c! t1 L5 ^

6 A- D. L$ j6 H0 T3 Y/ d7 V2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     + f/ U  X4 n8 {  \* L
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]; O2 i8 m5 ?# x9 k
解决方法:4 f% r2 \- {- `3 e
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
7 R! R3 S% l5 E" |1 y
2 D: z2 N% b2 z4 xSecAuditLogDirMode 0777! F" [9 N" ?. q$ x) [
SecAuditLogFileMode 05506 V: {  D( F, P, w; E
SecAuditLogStorageDir /var/log/modsecurity9 H$ c/ s. m& K8 ]" M& k
SecAuditLogType Concurrent9 y* ]0 W" s& i
参考文章:
- O1 K/ i+ d7 ]( c1 n0 Qhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX8 L* g2 ^+ D2 l1 f' B( X' p: o$ n
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-17 17:11 , Processed in 0.053056 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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