Scaling CloudFlare’s Massive WAF

22 篇文章 0 订阅
2 篇文章 0 订阅

转载地址:https://www.scalescale.com/scaling-cloudflares-massive-waf/
Application
HTTP Server nginx
App Server: OpenResty
JIT Compiler: LuaJIT
Algorithms
String Matching: Aho-Corasick
Rules
Open Rules: OWASP
System Profiling
FlameGraph: SystemTap generated
Real Time Analyzing: Nginx SystemTap Toolkit
profile

I first heard John speak at the Nginx.Conf conference in San Francisco. He’s done an amazing job explaining a large scale, high volume WAF (Web Application Firewall) platform that he and his colleagues have built. In this interview he’ll explain design goals, benchmarking, testing and WAF rule new roll outs. The story here is really about performance and scale by optimizing every last drop out with Nginx and LUA. Enjoy.

–Chris / ScaleScale / MaxCDN 

John Graham-Cumming of CloudFlare

John is an Engineer at Cloudflare that designed their WAF.
What is the vision behind the WAF?

CloudFlare wants to provide a WAF to a very large number of customers. To do so meant two things: being compatible with the existing mod_security WAF so that we could leverage existing rulesets and allow people familiar with mod_security (both CloudFlare people and customers) to write new rules.
How CloudFlare WAF Works

CloudFlare’s WAF stops attacks at the network edge, protecting your website from common web threats and specialized attacks before they reach your servers. It covers both desktop and mobile websites as well as applications.

The Web Application Firewall (WAF) works by examining HTTP requests to your website. It looks at both GET and POST requests and applies rules to help filter out illegitimate traffic from legitimate website visitors. You can decide whether to block, challenge or simulate an attack. With blocking and challenging, CloudFlare’s WAF will block any traffic identified as illegitimate before it reaches your origin web server.

How CloudFlare Works
CloudFlare’s Web Application Firewall (WAF) automatically protects your website from these types of attacks:
• SQL injection, comment spam • Cross-site scripting (XSS)
• Distributed denial of service (DDoS) attacks • Application-specific attacks (WordPress, CoreCommerce)
Testing CloudFlare’s XSS Protection

Using www.jgc.org, it’s very easy to see the CloudFlare WAF in action. Using a simple GET operation with a dummy variable that contains a basic XSS script will trigger the security feature and show a page saying that you have been blocked.
Request Headers

GET /?user= HTTP/1.1
Host: jgc.org
Connection: keep-alive

Response Headers

HTTP/1.1 403 Forbidden
Date: Wed, 10 Dec 2014 06:56:35 GMT
Content-Type: text/html; charset=UTF-8

Click here to see the error screen generated by the WAF
Where did the initial and new rules come from?

We use both the open source OWASP ruleset plus we developed our own internal rules based on attack traffic against CloudFlare customers. Today the majority of blocked requested are being stopped by our custom rules.

We develop rules internally based on attacks or vulnerabilities and then build a test suite (positive and negative tests to ensure that the rules are blocking only what we want). We have a large automatic test suite for the WAF which gets run across the entire rule set to ensure that it’s working correctly.
Recently added WAF Rules
Description Exploit Blog Post
Drupal 7 sql injection SA-CORE-2014-005 Drupal 7 SA-CORE-2014-005 SQL Injection Protection
Shellshock Shellshock (software bug) Inside Shellshock: How hackers are using it to exploit systems
Shellshock protection enabled for all customers
WHMCS Zero Day Vulnerability WHMCS Security Advisory for 5.x Patching a WHMCS zero day on day zero
Protect Your Sites With Rapidly Deployed WAF Rules

We process all requests. GETs, POSTs, etc. and the bodies that go with them. We have a custom routine inside the WAF that looks at POST data (for example) and identifies it by both the MIME type and by sniffing the actual bytes looking to see what the data is.

The WAF is not enabled for all customers. Only paying customers receive the WAF.

We work with our customers to define site specific rules for them and regularly put in place WAF rules to block site specific attacks. In future, we plan to roll out a user interface where customers can write and upload their rules for their sites.
Is speed important to you? What is your philosophy?

Yes, speed matters enormously because of the scale of CloudFlare and because part of our service is performance. We have a variety of benchmarking tools but perhaps more important is our metrics system that allows us to examine real-time and historical performance information (including WAF performance).

Our goal is to run on average in under 1ms for each request being processed by the WAF. Currently we are in the 100s of µs (10th’s of milliseconds) per request. As an example, in the last 24 hours we have blocked 1.2 billion HTTP requests (that’s about 14,000 per second).

statporn

• 14,000 blocked reqs/sec • 1.2 billion blocked reqs/day
• Goal: exec all rules <= 1ms • actual execution ~400µs
• 1,937 string matches • 5,682 general rules
• 102 Cloudflare Rules
When you first launched, what kind of latency did you see?

When the code was first written and tested we were seeing about 10ms latency on a laptop machine. That was optimized using techniques like function memoization and then some architectural changes (mostly the elimination of the use of closures) and the latency was close to 1ms. After that the WAF was put into production and work was done using systemtap and internal tools to analyze LuaJIT and PCRE performance. We worked closely with Mike Pall (the LuaJIT maintainer) to ensure that WAF-specific functions we need are JITed.

Using LuaJIT is night and day. We would not ever use lua itself in production. LuaJIT is way more performant than Lua on x64 hardware (see http://luajit.org/performance_x86.html).
How do you speed things up and look for slow execution?

For the initial tuning of the WAF code we used Lua-based profiling tools (and wrote one ourselves) to look at performance of the Lua code that implements the WAF. Once in production we used systemtap and flamegraphs to identify hotspots and optimize them. When launching into production, we did not need to change anything in our physical infrastructure. We did not purchase or use any new hardware. The WAF is mostly CPU intensive.
< 1ms Latency

< 1ms Latency

Before we implemented the new WAF, CloudFlare has been running Apache alongside nginx just to be able to use mod_security. This combination was very slow and cumbersome. Ultimately it didn’t scale with CloudFlare’s growing business so we started working on a new WAF using nginx + LuaJIT.

CloudFlare is operating one of the world’s largest deployments of nginx + LuaJIT. Every fraction of a microsecond that can be shaved off for processing a request has significant impact so we decided to sponsor some changes to the LuaJIT opensource project.

The overall goal of the project was to get the median WAF block/allow decision made under 1ms in real world scenarios. Optimizations were made by examining the WAF’s performance under a test harness with line-level timing information. We ran the WAF in CloudFlare’s network with very detailed systemtap-based instrumentation.

Information from the systemtap is fed into a pastebin which parses it and produces a flame graph showing where the code is running.

FlameGraph

The flamegraphs early on showed extensive uses of closures which was causing slowness in LuaJIT. Some parts of the compiler were rewritten to remove their use and make it run faster.

Here’s another view generated from the same information which identified hot functions. Here it shows that string matching and regular expressions are the most expensive operations.

FlameGraph

To make these matching functions run faster, We have implemented our own version of the Aho-Corasick algorithm. The Aho-Corasick algorithm is a fast string matching algorithm that can match a large set of keywords simultaneously against incoming text. The advantage of the algorithm is that it can match multiple strings in a single pass over a large body of text, compared to searching for the strings individually using the Boyer-Moore search which requires multiple passes over the text. In this article, the author shows how Aho-Corasick is implemented using Haskell. CloudFlare has also open-sourced a custom Aho-Corasick implementation in Golang and C++ with LUA.

Optimizations in the Lua language, the LuaJIT compiler and the WAF core meant that for a very fast and flexible all Lua WAF which runs within nginx’s core.

See an example in LUA »
Read & watch more about building a low-latency WAF inside NGINX using Lua

Watch John’s presentation on “Building a low-latency WAF inside NGINX using Lua” on YouTube. You can also download the presentation used in this video here.
Popular search terms:

ScalingCloudFlaresMassiveWAF-ScaleScale
golang waf
nginx-systemtap-toolkit
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
此模板用于爱站CMS内容管理系统中。 "政府类网站模板"背景嵌套这故宫、长城等图像,页面中大量的图片以及文字展示,将更多的信息及内容传递给每一位浏览者 。 以简洁、大方为主体,模板中设计了"地区旅游"、"政民互动"、"信息公开"、"地区概况"、"领导信箱"、"投诉举报"、“留 言互动”这些都是做为政府地方站的核心内容。 模板使用方法: 1.解压模板包。 2.拷贝tpl_install文件夹到网站根目录,如果tpl_install目录已存在,先删除在粘贴或者强行覆盖即可。 3.在浏览器中输入您的“域名/tpl_install”回车即可运行模板安装程序,等待安装完成,按照提示操作即可。 4.安装完成以后务必删除tpl_install目录,避免发生二次安装。 5.恭喜您安装完成。 爱站 CMS 内容管理系统:代码全部开源,可方便进行二次开发,功能模块可以自由安装与删除,个人用户免费使用,系统频 道模块很多,适合作个人门户网站。精美模板免费使用,页面可设置静态页。该系统专门为企业网站(信息平台类、展示商品 等)、政府机关、教育机构、个人站长等用户设计的,该免费软件可轻松打造专属于自己的网站。 爱站CMS内容系统特点: 1.爱站CMS是免费建站软件; 2.爱站CMS以不懂技术的用户为基础进行建站,灵活的模块组合,让网站更丰富,只需操作4步就可建站; 3.简单易用的模板引擎,网站界面想换就换; 4.便捷自定义模型; 5.高效的伪静态页面部署,有助于百度等知名搜索引擎录取信息; 6.流畅专业界面设计,良好的用户体验; 7.爱站CMS延续性较强,不断免费更新完善系统,后期将推出免费建商城模式; 8.爱站CMS即将推出免费版移动平台应用; 9.“爱站CMS软件”由铭万科技有限公司发布,开发团队庞大(铭万科技有限公司在全国成立了30间分公司,员工队伍超过 3000人)。 爱站CMS是集安全高效、开放灵活、简洁美观几大特点的开源内容管理系统,是国内开源CMS的新星品牌,他与其他CMS产 品不同,操作极其方便,建站只需半小时。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值