找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9375|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
1 I6 I: ^& y$ ?, |$ {, Z
" E1 z! v2 k( Z& M! s4 E4 |, \( C一.准备工作
5 o& \) `# e4 c1 Z; c8 t+ z  p( Y/ E' w1 B- d
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
/ V% i7 r+ ^* h0 s5 Z- m7 Y6 i
+ E% \4 {" h9 N5 i! ^: etengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz8 I2 v$ O) X* q

3 }; i( t6 S) g) emodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
/ V% b- r6 Y/ f6 C- ?3 M4 H0 Q5 P' _; I
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs$ @! z, W6 ^% k+ C5 s5 R1 X
5 r( U: |7 W% {4 c, s8 @
依赖关系:, p# S+ b5 b  r" v$ t
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
2 U: J' e) l7 Q5 z' q7 {" P0 }4 F) q$ c1 A0 U8 Y
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel+ X/ _( \1 Z1 V# N: |
modsecurty依赖的包:pcre httpd-devel libxml2 apr
+ A7 l6 D% |* Z3 R' C; M7 w+ v3 e. n8 E: [. H7 x
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
! {% C+ v0 t' V* c0 U+ a$ A: Q# j  Z二.启用standalone模块并编译/ s6 e1 p7 w' r& G" u. C

/ n+ W( j3 A8 K* x  R) q1 p下载modsecurity for nginx 解压,进入解压后目录执行:# p8 E3 e5 w* K% U
- b  J0 ~, d/ K# Y
./autogen.sh4 i3 a% H0 `/ c9 v& }3 J
./configure --enable-standalone-module --disable-mlogc7 x' u# {  e" j+ u  K
make
5 M! n; H0 M1 H: E$ i: v三.nginx添加modsecurity模块: P; H5 {4 L$ b
! l/ b" x* K5 @2 W( M4 P
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:4 \8 N1 m1 }. b+ Z: e% k
, H7 E! p" O: S8 w3 S8 c
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
4 J- ]1 @0 m- v+ t' [$ Z' ~make && make install
" [7 V+ Q% p0 j$ d( g2 t四.添加规则
+ R' K0 \1 e8 S9 K8 r" \5 _4 V& G5 T4 ~& s
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。, f- K! `: c4 O; \& ?
% K& B& d4 f0 O( W
1.下载OWASP规则:  h/ H" Z/ j( b% m! C! C. y" Y4 Y
9 G0 ~" U& N, C) U7 t7 H  r
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
8 z* L" Y1 Y8 s, K% J  k6 J6 p) I4 b# p+ G  ]" `
mv owasp-modsecurity-crs /opt/tengine/conf/$ O" u: T* }+ C1 r' \! I
) o3 F; ~3 x  X
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
% \4 X3 ~  r3 _, c/ }8 W2.启用OWASP规则:
. F0 c& S2 ~* q- p8 \; r3 h- }; t& @/ g; x3 B8 p& [
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
! O, M; I) H% j7 o8 u/ R
2 s; e1 |" O, m: l9 {4 C编辑modsecurity.conf 文件,将SecRuleEngine设置为 on9 j" h1 x. s8 N' ]
9 @2 @% j+ Y0 f+ U5 U
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
+ t, O' _3 R# _8 @+ C3 i1 E, M# L" ^4 X/ ~
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
+ y& Z, m: J- X9 [5 l, Q1 r: u, ]- V; b* wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf  @4 g! \1 G# o) P# M
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
# M2 G1 L; p: @3 b8 SInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
+ d; Z0 {: o& h( ^Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf. F( i* _5 A  q' }* ~! S
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
+ C* \- a3 X$ C$ O2 R, q" LInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf, S# Q! Y* y. R. _
五.配置nginx
  c! ~+ I5 a3 ?7 Q! J7 E+ Z; v4 Y' j( B1 n
在需要启用modsecurity的主机的location下面加入下面两行即可:# w2 l0 T5 n7 O, m- j
  P2 e6 M/ U5 D: x0 C& l  c9 I7 u0 |
ModSecurityEnabled on;  3 U# o& m9 R9 ^1 S
ModSecurityConfig modsecurity.conf;
/ L) I9 D. l1 v. d3 d下面是两个示例配置,php虚拟主机:
  x, \1 W2 \" Q$ Q2 W  Z* p  ]2 h2 H$ z* u7 P
server {# T' d& d) l9 C! {" P8 F
      listen      80;
2 G; H% J, I" e5 k' o      server_name 52os.net www.52os.net;2 z: u: q) p* j% z2 p& M
     ' u7 y) h5 ]0 ]4 V( R
      location ~ \.php$ {
% a+ M/ s: o2 h" S- A      ModSecurityEnabled on;  
  Z' {- b9 V  _9 n2 V8 j      ModSecurityConfig modsecurity.conf;
4 J$ _/ c, g1 I1 d9 ?1 [! u% w; E
4 q- I  v3 Z* Z6 I      root /web/wordpress;
- }1 a* i1 h4 p4 P# I# |      index index.php index.html index.htm;- W8 g$ _% R2 z7 k  [8 v. P# q6 s
    q* s) @$ v! A' T4 @
      fastcgi_pass   127.0.0.1:9000;
4 M( R; e) ^9 u; O      fastcgi_index  index.php;
8 C- `4 R3 s# Q* X" ^3 f      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
! _& R1 B4 E5 x" ~$ U- h      include        fastcgi_params;
% c) O" ?0 g$ x) P; E      }
$ Q$ k, W( Y, ^  }# e' E, w& ]4 F$ x( [, O8 u
upstream负载均衡:" A( e. {; O3 ~- u& L) f

2 m, G. [6 k8 D* N5 n3 xupstream 52os.net {
0 l- l9 L$ A$ X3 d5 r% _/ w+ M    server 192.168.1.100:8080;4 f; v: ]& ]  z! |
    server 192.168.1.101:8080 backup;
( h- G+ [5 N( p7 c4 h}
( l2 `# E7 J7 T- b$ e7 X
6 q) ~$ ?, x+ U* B7 j. ?server {
; u7 y) r1 w0 k8 j0 |listen 80;6 `( z9 h! y; E4 \5 b
server_name 52os.net www.52os.net;' x: _* \+ G( [; H" [, g7 s
6 @6 w# J* M8 p7 R# {
location / {+ w2 P" x: N9 m$ i
    ModSecurityEnabled on;  " g6 p) O  ^. K
    ModSecurityConfig modsecurity.conf;  * _0 O5 h* |7 {  X

" G0 K' U* ^* y1 I        proxy_pass http://online;
) k1 ^- {* n; Q: w3 j( Y1 |5 e* T( {7 C        proxy_redirect         off;" ?3 \; [2 z2 `9 A  c
        proxy_set_header Host $host;
, t2 i/ p. |: a- Q        proxy_set_header X-Real-IP $remote_addr;
* ]/ V' [% U' U* G, K7 ?        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;: I& g/ e, i' g- l
    }
$ h- j4 T. m5 H3 d7 q! }) [5 j" c}' U/ W0 H/ u/ |, y7 |# {
六.测试
4 V6 `$ ~; p! D% T* j- `4 g0 _4 w7 v- C8 j3 w
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:% t9 H9 D4 V8 q/ b) ~- M$ ?
  ?- N) p$ e4 \  T
<?php4 i+ H  ]% o" U1 B2 X; Y
    phpinfo();   
8 ]; b7 ^. X# u5 ~5 Z) v?>% }3 n8 I, ~1 H7 Y: C. g, A0 {
在浏览器中访问:8 q; J  l+ Q% I0 c- a6 s1 ~3 e

) y1 Z! E% J# hhttp://www.52os.net/phpinfo.php?id=1 正常显示。, x! T9 v. [  L" @$ |  y- v
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
  x! L/ b1 s+ h. \& L* Lhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。- P7 J+ h: R1 |* _& |
说明sql注入和xss已经被过滤了
8 q" j2 g# _- {% R5 _3 G; V3 v2 P! t  p0 n& A8 c! g5 I" i
七、安装过程中排错
8 x6 L$ U* _! A. @; r3 t; G- r/ N/ K; v$ I
1.缺少APXS会报错
+ x- ?: [! `; f- I3 k0 V  F. t' Z+ ?
configure: looking for Apache module support via DSO through APXS
9 I3 ^8 O/ f6 V) e# M% x# V9 s3 Mconfigure: error: couldn't find APXS
3 M$ m2 ^. n6 O  F: _% v/ [apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
) ^) d$ F% }8 s! v" i: k! d. ?解决方法:9 ~: P; k! m; l! W% d
' e' B' m! P1 I/ p; O
yum install httpd-devel4 j" ^8 K3 H, e# ]# L+ s
2.没有pcre. X' v4 O6 s& a  A6 J3 M

3 N1 P" g* q! W7 uconfigure: *** pcre library not found., Z$ j$ X; I( |: K! s
configure: error: pcre library is required  o# Y, x" U, R
解决方法:
* K# C% j1 V* s9 G& t! h( ?( t3 C- u! T! K% J* e
yum install pcre pcre-devel) W/ P; B* L5 {/ J6 ~6 [  t
3.没有libxml25 F3 u. `8 H* a) U7 `& A

2 ]( M2 D- w- B' ?5 c  G- t
2 J4 Z/ w4 I: g/ Pconfigure: *** xml library not found.( l- x/ U4 o4 W  ?4 ^
configure: error: libxml2 is required. P' d2 E2 T' ~2 e( `: K2 ^& y6 D
解决方法:
+ Z& v9 E; c: n: [2 b2 @% o
& H: k+ G' Y4 V0 F$ k  Z1 Hyum install  libxml2 libxml2-devel/ a0 i; p1 q* }
4.执行 /opt/tengine/sbin/nginx -m 时有警告: @& y$ z, m2 K  c$ [/ l+ J
: r# A9 P  ^0 v" M: V& R
Tengine version: Tengine/2.1.0 (nginx/1.6.2)) S* d( G: l2 j, y; b' B
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!+ E9 q1 v0 F& x0 d
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log( _0 m% m+ F: D- `8 y

& n7 y7 d1 o. e3 ^# y8 g7 X% e  F2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
. C7 a3 j3 t' O- R4 u2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
. f4 P5 P. K: p& |7 S+ @2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!5 l* `, K4 |  Y- Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
( ]+ P: G1 @  V0 b' Y9 R2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
, M. k1 q, U5 v" R( i2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.. }) Q* [1 v3 E/ E1 H" q
解决方法,移除低版本的APR (1.3.9)
; S, E8 p$ X" c* r; x# J/ W
- k; [, f# c& h! Z) Hyum remove apr
) D9 _  V: m) C3 j9 `2 u5.Error.log中有: Audit log: Failed to lock global mutex
3 C1 d6 r- E4 s) r) i6 u; c* G# ]+ O% f6 X
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     " b5 t% r( Z5 t) z
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]! A. e1 w( K: v- S: @5 [
解决方法:
' `/ U6 Q1 I! T  p7 A; _. e7 Z编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
( j4 L2 }' D) ?. r. i: d3 K
. |& p. r& L- r( F# t# h* eSecAuditLogDirMode 0777$ d, `1 d" e7 J9 e% @# A% |- W# e# b
SecAuditLogFileMode 0550/ h0 p# [1 h. y8 N0 g1 b3 K. [
SecAuditLogStorageDir /var/log/modsecurity
$ L- m! ~( Z. Z) b& A" x# SSecAuditLogType Concurrent
3 B8 H' U' i: d( `  H参考文章:% q7 o- o- f: \' z/ s6 d' Z' r5 ^
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX/ Z' Q& `0 \$ T& }% w' e5 O2 H
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-12-12 16:01 , Processed in 0.064332 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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