找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13233|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
* T% j+ A6 Z' m* K( |( p' n3 {3 e0 i6 e  [" n# a! n4 \0 `
一.准备工作0 t, F" M/ W, D( }. S& l1 M

5 T% D: w+ D+ J( b系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0  ?/ \8 o- L3 U8 P& e5 v/ E
8 R0 L" U" g* M' h& x. ~
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
  Y! I6 ~; ^) A8 X
5 U, x! ~& S4 h8 {+ I! \5 W2 g& D: X" mmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz2 J' l9 A4 _9 _, x6 k
. d  H1 W+ b+ a& y! J
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs2 B+ O" r8 i$ D) Y% |% J- O& X
, e* C3 _" U1 }( {" n5 Q% s
依赖关系:5 ]6 Q8 W! a3 ~* q9 o) o* z
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:& y8 V5 B, L5 @) d& @: R

1 @  \" x) Z& Hyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel/ s& I) }* }2 o, z
modsecurty依赖的包:pcre httpd-devel libxml2 apr
/ P4 h0 D0 w: X1 I9 Y
+ ~7 z0 w! R1 fyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel6 O/ O3 A4 T" S9 ]6 K0 v9 w
二.启用standalone模块并编译
  U3 u( V6 z' ]  o) q
  a7 p) \9 {: {4 k5 {下载modsecurity for nginx 解压,进入解压后目录执行:
- F4 A& o8 Q  g% f6 F7 I' k6 N6 W6 `8 P
./autogen.sh8 S! k+ B7 l! O/ J
./configure --enable-standalone-module --disable-mlogc0 d& u- D7 b% w9 V( C: H5 U
make
, i, m) i9 ?1 m- `; L+ L2 D三.nginx添加modsecurity模块
3 W) w1 M$ Q" K  _' h% w1 g& N9 u. Z6 V0 j1 u: T0 F
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:6 `7 H+ |/ X+ m2 ^& U
) |, m' F: h- t! p& ^* R3 s
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
& h# e4 A2 l2 G9 R+ K: l, Gmake && make install
+ O! I# F* c3 }5 O  E: L) X! D四.添加规则
0 n) {- B" D) L3 w8 |: E4 I) Z* C2 J/ I
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
0 n' w6 \8 w" j$ |  u8 V) b+ e# }/ |  t$ [
1.下载OWASP规则:
2 Q3 V( v5 S6 r! ^, i1 j, u9 B0 c+ o' s9 J7 L# f2 A& H7 }  M  [
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs0 g& I( s5 U% }

- |& `& L- @# e. M, ^mv owasp-modsecurity-crs /opt/tengine/conf/
; @* |, V4 q2 p0 Y1 A# @  U) X7 Y7 u
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
% k3 E3 R7 i7 {7 n2.启用OWASP规则:" D( X/ Z7 Z. E" D: y, a- j

" O0 ^7 m* ~; [$ B! Y# B复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
8 P. Y" q" d) W1 C7 v" {1 S) N6 m- ^- y4 A& Z1 O! |
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on; w& q: T2 Q: E; L' C8 k! i

3 Z+ t9 I7 Q1 P2 w. Mowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。) t4 X( F) K5 y  T/ d
, Z% i+ V  R  q0 ]: |8 c
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
  ]* o% t; M+ [* k! y& I' zInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf' f/ v+ q  z: e0 w; i, I' n
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf) w  o& ?2 {7 h+ p" b" \2 [
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
# c9 Y  I2 j$ x" x. EInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf* ]% t) j$ v: ^! }4 Z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
% J! l% D0 |9 T3 a  ^2 ]Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
6 s9 S9 |1 n7 M: B' A; g  M0 b+ _五.配置nginx
7 @% T. {4 A+ x! C8 Q: I8 y
( W# ?2 g% ^1 k3 i在需要启用modsecurity的主机的location下面加入下面两行即可:
2 ?# i5 R5 I, z4 ^5 J; ~9 V; f1 r. S1 F/ {1 i$ Y" y- x. d
ModSecurityEnabled on;  
' C' [5 F3 N: J6 y  \ModSecurityConfig modsecurity.conf;
4 L& s  d) [) S( Z0 j5 _  f! `0 p下面是两个示例配置,php虚拟主机:) H: S4 X  L7 W, {1 N( `

' ~/ w: ]7 G8 y. z' Zserver {
$ h% x9 t; R9 x# r: l' h5 y: a      listen      80;
: Q5 A) A+ S6 p9 w4 L8 @      server_name 52os.net www.52os.net;
- [  ]8 C" `6 X6 _; B& L- ]     ) s( H* [/ A- I8 [  r% _+ ]
      location ~ \.php$ {
' l$ Z, ^$ M2 P5 N3 p1 z2 b5 w      ModSecurityEnabled on;    w4 }7 W) Z; r
      ModSecurityConfig modsecurity.conf;' |3 e/ l' s' l$ g5 J1 M4 H

% H2 v" }/ z2 X      root /web/wordpress;+ \9 i* e4 F2 o# m
      index index.php index.html index.htm;
1 X8 k( m, f. A  5 m5 y' E( P3 |( i1 _( |- a2 u, d; i, I
      fastcgi_pass   127.0.0.1:9000;$ i7 b/ B' d$ U* y' o# u- d
      fastcgi_index  index.php;! z5 D9 A/ q  o2 i4 ^2 a2 c, |
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
4 V9 u' J) j$ A      include        fastcgi_params;
1 I3 X0 \5 u2 ~  ?* p$ \1 ?      }
0 A7 A; s9 M* F0 x% {# T  }
: K$ p+ k- i" c; Rupstream负载均衡:
8 ^& e- `9 L0 p- Y, B2 W% @
* h; m9 }* I, S8 X! [upstream 52os.net {
* r* \# {& \, W2 Q    server 192.168.1.100:8080;
6 [! o" t/ n9 E+ \7 K: Y; l    server 192.168.1.101:8080 backup;
2 d, m' p3 d7 ^" ^6 n! U  }: T# ?1 h}
8 b8 h( Y; p1 l6 Y- L
* g( ]! {2 u, c  ^server {6 c& z- P+ B! M6 R8 a
listen 80;
% l& [1 _, B+ [) P& h! W/ fserver_name 52os.net www.52os.net;5 j; f" n0 q) x& i7 Z  |' V& |+ n
) ^7 b0 Q) n8 g4 Q
location / {
, z& S  E' C* x9 h    ModSecurityEnabled on;  1 _5 l6 X6 _! s) y' C. J0 x7 |% p; ?
    ModSecurityConfig modsecurity.conf;  0 j  X/ u( f1 ?5 D

1 q# q& ^- k: \& a  k- N5 }        proxy_pass http://online;
8 R" B) i+ \5 u. M        proxy_redirect         off;  }5 [& w9 S; x' ?
        proxy_set_header Host $host;
& U4 n9 ^5 i% o0 a: j0 z; @- |        proxy_set_header X-Real-IP $remote_addr;
, z  v4 M+ z" f& {        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
' L# H$ k/ e' P3 U% D2 @+ I) y    }
1 G& E; y* M( H  l9 I4 u8 u! D}
" S0 s9 K% D7 j- ~" }) {六.测试0 ~$ j3 W! b) ~1 v: [; Q8 F
- ^6 h/ [; X3 W( X) a8 \' C. J% m
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
8 W2 M# K& N7 |- X- v2 c4 g$ W% u. S: ?1 |" D3 K2 N* e9 K
<?php
3 a; U6 B" u/ U# K; \# B    phpinfo();   
' Y+ t! P# U3 a7 z* i* p?>% a$ q7 @5 k% x' n  ]% z
在浏览器中访问:. }- A8 c/ R& n! B$ O: ?

. s8 s7 o) O( y. G' Z2 s3 D$ phttp://www.52os.net/phpinfo.php?id=1 正常显示。: u  C+ b; e- ^* u) ~" k6 C. A* Z
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。0 @5 y" ?4 o/ Q5 j
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。! q8 `: b8 u2 u: |
说明sql注入和xss已经被过滤了5 B% L1 ?( L8 ]; n, I  T+ v
& @3 G! Q0 ^, m! D* f; d
七、安装过程中排错7 n3 o7 i: d8 y0 d* d9 T9 F
- C8 c& Q# ~* [9 `9 j& f; g6 |
1.缺少APXS会报错- l: j+ w' P+ E
- C$ v- g7 J7 A5 s
configure: looking for Apache module support via DSO through APXS& _+ y+ b. [8 Z' J. v8 c
configure: error: couldn't find APXS$ H2 x, \8 d; o6 ]
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。, z, w2 X/ l' h( g* e
解决方法:! D0 e' U" }+ T5 S' X- N! D( c

, @. b7 f0 u2 ?& Hyum install httpd-devel7 H* C" H3 H5 W1 R/ i* y
2.没有pcre4 O3 `+ ]* s: V: f* Y7 E
* E2 a5 ~5 o9 [
configure: *** pcre library not found.
) n# C2 P8 U3 ^8 `; y4 uconfigure: error: pcre library is required/ \9 m9 Q1 h8 E" r
解决方法:
) Q$ N3 Q8 `$ F* Y+ C
5 f6 @$ |8 U. Tyum install pcre pcre-devel
7 u- r. x, }) y2 |( E3.没有libxml22 P2 [* a6 Z7 |" T  D( m2 E$ U8 C. {- E

! |% f, J8 X: x$ K9 d( I+ O* E# V
configure: *** xml library not found., W+ q; C3 a# |" V* m
configure: error: libxml2 is required
6 {/ n  b. @: |% ^8 i$ R, g解决方法:5 q7 X/ F! Y% m, }. T7 @
) `3 [5 |$ K9 a4 k: m! w; [) O
yum install  libxml2 libxml2-devel
' j+ T, r! E% \# q8 W3 c4.执行 /opt/tengine/sbin/nginx -m 时有警告
) [7 I- v1 U% s0 e9 h5 m% ~$ m" t5 |0 F. }6 F. i
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
, Q5 c; v  {4 G2 H. Vnginx: [warn] ModSecurity: Loaded APR do not match with compiled!# m: F- G7 Q0 J4 J- Y, X
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log5 l  M9 X( w( n' m2 c8 T. K+ Y+ G
# i1 o& @' r3 u7 U
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
& o8 F6 a+ Z! A2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
) c0 P4 Z6 k' |0 ?) J# u" I( ~2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
0 c) w# U. ?- i6 x3 c/ g4 L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"4 A2 A. m/ {* k: Y  L5 N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
0 `8 }4 v( v( Q' L0 w2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
+ d" X3 p4 i+ x* i8 P解决方法,移除低版本的APR (1.3.9)9 m( O  P) T! ~) h+ V5 {4 J" \# z
4 {+ u* x( ?# b
yum remove apr/ G& ]" t( P; Y) C
5.Error.log中有: Audit log: Failed to lock global mutex$ G' Z5 N& F& L, l

+ ?# v" b( G7 u0 ]; o; C  G8 a  B2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     $ P$ w5 |2 P( Z3 `( D
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
5 Q2 z, X' t' f2 X8 B4 J解决方法:
/ ^9 W$ h: s: w& }) B3 B- T* w编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
. N' q2 P. o0 b# n
8 F( C- J  Q& e; E1 u3 b& j6 _SecAuditLogDirMode 0777' a5 y* d! [8 G$ e) G, T
SecAuditLogFileMode 0550, A4 R' X4 C# f: x7 m
SecAuditLogStorageDir /var/log/modsecurity
, @5 M0 L" ?9 {. @SecAuditLogType Concurrent+ w4 T  O# ~# o# q& ~2 u. V
参考文章:
# T( m5 u# l, O/ khttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX5 k. G# Q+ s6 I  S' Y
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-27 01:13 , Processed in 0.032304 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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