跳至主要内容

白话 IT 之 聊聊数据加密

原文地址白话 IT 之 聊聊数据加密,作者是 嘀嗒嘀嗒 微信公众号运营者angela,表示感谢

嘀嗒嘀嗒按:今天的分享来自我们 Airbnb 的数据加密的大牛桑立锋。说到桑同学,那比我牛出不止一个数量级了。俄亥俄州立大学毕业的 PhD,曾在Yahoo!、LinkedIn 等多家公司担任技术骨干。在过去三四年间获各种数据加密相关的专利达五项之多。

除了技术扎实,桑同学平时也是极好相处,又特别愿意帮助别人。在我过去和他不多的几次合作中,也不时又机会请教一二。虽然对于数据加密仍然是个门外汉,但是桑同学解释问题总能听得懂。因此在我的再三请求下,有了这篇科普性的文章。

虽然我们都就职于 Airbnb,但是这篇文章和 Airbnb 现在使用的技术没有直接关联。下面是正文。



数据加密是一个古老的问题,但又是一个无法做到完美的问题,尤其在一个复杂的大型系统中需要考虑方方面面的问题,包括 security, availability, usability, consistentcy, performance,extensibility 等等。除了那些完全不做的,大部分公司都会根据自身的需求,环境,资源以及工程师的能力等,设计和开发适合公司实际情况的解决方案。有做的好的,也有坦诚做的不好的,还有自己认为做的好的但其实经不起真正考验的。各种各样的都有。

就算在同一个公司,很多也会有不同的解决方案并存。有些是因为历史原因,比方说早期用方案A,后来开发了更牛逼的方案B,但没有把A完全干掉;或者扩张原因,比方说兼并了一个用不同方案的别家公司;又或者是政治原因,不同部门谁都不屌谁,各自用自认为合适的方案。  无论公司选择什么方案,因为话题的敏感性(法律原因,或者圈内的潜规则,或者仅仅是不想当出头鸟而被黑客们盯上),绝大部分都不会公开数据加密的细节。同样原因,这篇文章也不会涉及笔者公司怎么做数据加密的具体细节。想到哪儿写到哪儿吧。


先说为什么要做数据加密。越来越多的数据泄露事件,比方说 Yahoo 2013年被盗超过10亿用户信息 [链接1], Yahoo 2014年又被盗超过5亿用户信息 [链接2], LinkedIn 被盗一亿多用户密码 [链接3],Ashley Madison 被盗三千多万用户数据以及大量支付信息 [链接4],Target 被盗近七千万用户数据和银行账号 [链接5],Adobe 被盗三千八百万用户数据 [链接6] 等等,以及由此导致的大量法律纠纷和巨额赔偿,还有很多很多大大小小没有被公布的安全事件,都说明了数据加密和保护的重要性。

说到这些安全事件中用户密码的保护,插个题外话。笔者曾经跟很多工程师(包括很多应用领域专家)聊天的时候,都会被问到,为什么笔者认为仅仅 Hash 用户密码是不够的。很多人(包括网上的很多文章)都误认为用户密码只要 Hash了,就安全了,其实不然。不说 weak Hash 函数诸如 RC4 [链接7], MD5 [链接8]就算用 Bcrypt [链接9],KDF [链接10] 等运算复杂的Hash函数,虽然能防 Rainbow Table Attack [链接11],但对 Dictionary Attack [链接12] 却是无效的。当然这不是 Hash 函数本身的错,而是很多人会选一个容易记的密码,而这类密码往往 entropy 不够,很容易被解密。对用户密码而言,笔者一直建议不仅需要Hash,还要加密 (比方说用 keyed hash function,or MAC) [链接13]

还有很多公司要做加密不仅仅是因为保护用户隐私,更是法律法规的要求,不得不做。有些涉及特殊数据,如信用卡号码,那就要做 PCI (The Payment Card Industry Data Security Standard );如用户健康信息,那就要做 HIPAA (The Health Insurance Portability and Accountability Act)等等。

在笔者看来,任何一个收集以及存储客户数据的公司,就算是初创公司,都应该认真对待这个问题。在圈内我们常说,不是系统会不会黑,数据会不会被盗,而是何时被黑被盗的问题。也许有人会说,就算偷了,盗了,那又如何?这种事可大可小,笔者私下就知道有公司被黑后,被迫关门了的。所以安全领域内的及时投资,对公司长期来讲都是非常有益的。对重要数据(包括系统密码,用户信息等)进行有效保护,数据被黑被盗的门槛就高了;就算有一天系统被黑了,数据被盗了,也能把损失降到最小。


你说数据加密这么重要,怎么做呢?如果把加密这个问题抽象出来,其实就是要计算一个加密函数:
encrypt(data, key)
(当然还有一个函数就是解密,跟加密类似道理,暂且不论)。看上去似乎是一个非常简单的问题,但要把它做好非常不容易。尤其是在一个系统复杂的公司,要考虑的问题很多很多。比方说,密码界有很多算法,应该用什么加密算法,对称的还是非对称的?具体选哪个,AES,DES,RSA,ECC等等,各有什么特点?题外话,笔者在曾经工作过的公司见过很多有趣的例子,比方说有些早期开发人员用 XOR 来加密,或者做点简单的迭代替换,或者自创所谓的加密算法(有点掩耳盗铃的感觉)等等,这些最后都变成 technical debt,需要花很大力气去清理。

每个算法也有不同的变种和模式,各有什么特点,性能如何?如果某个算法被宣布不安全了,如何快速迭代?这个 key 怎么来?多长才是安全的?怎么启动?怎么保存?怎么传播?怎么控制访问权限?怎么知道谁访问了什么?怎么来监控?怎么来预警?怎么来系统性的更新这些 key?key 能不能丢,丢了怎么办?不同的应用可能是用不同的语言写的,怎么兼容?怎么支持大流量等等,等等。还有更加不近人情的要求,比方说如何保证被加密的数据能够 preserve 原始数据的顺序,支持搜索,但又不牺牲安全性等(这方面 MIT 有学者在研究,有兴趣的朋友可以看看他们的论文)。

一个好的加密设计方案,不仅仅方案本身要满足安全上严格的要求,解决上面提到的很多问题,还需要实用,容易扩展和维护。在数据加密变得越来越重要,系统越来越复杂的年代,如果资源允许,应该把加密服务独立出来,然后提供高性能,统一,简单又容易理解的接口来进行数据加密,让应用开发人员很方便的使用,这样他们只需要专注他/她所擅长的领域,而不需要去思考怎么解决安全问题,因为术业有专攻。好的加密方案应该把数据和秘钥的存储分开,并且把存储和运算分开,尤其在 SOA 架构下, 这可能跟很多传统的数据加密方法(比方说直接实用某个语言的内置库加密解密)非常不一样。很多解决方案往往密钥和被加密的数据同时存在一个服务中,结果就是如果那个服务被黑了,那就整个被黑了。下面就捡几个要点简略讲讲。

(1)为什么要把运算和存储的分离?很多需要被加密的数据,往往和具体商业逻辑数据一起,属于不同的服务,比方说支付信息属于支付服务,护照号码属于用户服务等。把运算和存储分离可以带来很多好处,比方说:
  • 加密服务变得简单和高效。因为不需要存储那些被加密的数据,加密服务系统不会很复杂,也不需要负责存储系统所带来的维护,扩展等诸多问题;
  • 加密服务的安全性能提高很多。因为运算和存储的分离,如果仅仅是被加密的数据泄露(比方说数据库被盗),那些数据就没法被解密,因为黑客没法从外部访问加密服务。如果仅仅是客户服务被黑,想要盗取大量数据并且通过加密服务来解密而不被发现也很难。如果仅仅是加密服务服务被黑,因为加密服务本身并不拥有数据,被泄露的数据也不会很多。只有当加密服务和客户服务同时被黑,才会泄露大量数据,而同时能够侵入加密服务和客户服务的难度要高很多很多;
  • 灵动性。因为数据属于客户服务,不同的客户可以对数据进行不同的处理,比方说不同的有效性规则,数据交易完整性等等。

(2)其次是 granular control。假设支付服务要求加密/解密信用卡号码,用户服务要求加密/解密护照号码,如何能保证用户服务不能加密/解密信用卡号码。这就需要解决两个基本问题:客户认证(authentication)和权限控制(authorization)。怎么做客户认证(authentication)?客户认证要知道每一个请求是谁发出的。因为只有知道客户是谁才可以进行权限检查。常用的有基于客户证书的(如 client certificate over TLS),基于 OAuth(开放授权)的,或者各种各样定制的方案,如基于 Curve25519 [链接14] 等等。笔者一般遵循两个原则:
  1. 是不是业界公认的。如果不是,最好避免。业界公认的解决方案往往经过过严格的检验, 评价,批评,经得起考验;
  2. 有没有广泛的类库支持,以及多语言的支持。如果有,可以节省大量的开发以及维护的时间和精力。

(3)数据监控和预警。为了审计以及安全的需求,一般要对加密服务做很多的数据监控,预警的工作。这样可以知道谁在访问这些数据,何时访问的,怎么访问的,访问的模式是怎么样的。监控系统还需要侦测异常的流量变化,进行流量控制以及快速的反攻击保护。

(4)最后,重中之重,怎么保护 root key?无论是用哪种 envelope encryption的变种,都会涉及到怎么保护 root key。这是一个很有意思的话题,但篇幅关系就不展开来讲了。常见的有用 secret sharing [链接15] 的一些变种方式,也有通过公证人公证整个过程藏在银行保险箱的,也有藏在创始人地下室的:)怎么来 bootstrap 和 deploy 这个 root key 到加密服务里也是一个非常有意思和挑战的问题。

总的来说,数据加密不是一件神秘的事情,但要做好非常不容易,需要一定的技术积累和资源的投入。做任何一个安全系统,风险都不小。圈内人第一反应是怀疑,因为职业病;圈外人很多也觉得重要,但不关心,或者说不知道怎么关心。但不管如何,无论是因为潜在的法律风险还是用户的信任风险,这种安全上的投入从长期来说一定是值得的。


链接1:http://money.cnn.com/2016/12/14/technology/yahoo-breach-billion-users/
链接2:http://money.cnn.com/2016/09/22/technology/yahoo-data-breach/?iid=EL
链接3:https://techcrunch.com/2016/05/18/117-million-linkedin-emails-and-passwords-from-a-2012-hack-just-got-posted-online/
链接4:https://www.wired.com/2015/08/happened-hackers-posted-stolen-ashley-madison-data/
链接5:http://money.cnn.com/2015/10/14/pf/target-pin-credit-card/index.html?iid=EL
链接6:http://krebsonsecurity.com/2013/10/adobe-breach-impacted-at-least-38-million-users/
链接7:https://en.wikipedia.org/wiki/RC4
链接8:https://en.wikipedia.org/wiki/MD5
链接9:https://en.wikipedia.org/wiki/Bcrypt
链接10:https://en.wikipedia.org/wiki/Key_derivation_function
链接11:https://en.wikipedia.org/wiki/Rainbow_table
链接12:https://en.wikipedia.org/wiki/Dictionary_attack
链接13:https://en.wikipedia.org/wiki/Message_authentication_code
链接14:https://en.wikipedia.org/wiki/Curve25519
链接15:https://en.wikipedia.org/wiki/Secret_sharing

Popular posts from 产品随想的博客

Steve Jobs at 44, By Michael Krantz, 1999

Differences and Similarities Between Apple and Pixar Apple turns out many products--a dozen a year; if you count all the minor ones, probably a hundred. Pixar is striving to turn out one a year. But the converse of that is that Pixar's products will still be used fifty years from now, whereas I don't think you'll be using any product Apple brings to market this year fifty years from now. Pixar is making art for the ages. Kids will be watching Toy Story in the future. And Apple is much more of a constant race to continually improve things and stay ahead of the competition.  His Role At Pixar At Pixar my job is to help build the studio and recruit people and help create a situation where they can do the best work of their lives. And to some degree it's the same at Apple. But at Pixar, I don't direct the movies, whereas at Apple probably, if I had to pick a role out of a film production, I'd be the director. So it...

《Becoming Steve Jobs》Chapter 12 Two Decisions

Jim Collins, the bestselling author of the management classics Built to Last: Successful Habits of Visionary Companies and Good to Great: Why Some Companies Make the Leap … and Others Don’t, has a wonderful phrase to describe an essential characteristic of great leaders: deep restlessness. Collins applies the phrase to Steve, one of the two great leaders who inspire him the most (the other is Winston Churchill, the great English politician who was prime minister during most of World War II, from 1940 to 1945, and again from 1951 to 1955). Collins believes this restlessness is far more important and powerful than simple ambition or raw intelligence. It is the foundation of resilience, and self-motivation. It is fueled by curiosity, the ache to build something meaningful, and a sense of purpose to make the most of one’s entire life. 自己何其有幸,在不到30的年纪,就能感受到乔布斯的心境 “The things he was trying to do,” says Collins, “were always hard. Sometimes those things beat him up. But the response to fighti...

《Becoming Steve Jobs》Chapter 15 The Whole Widget

What the world did see was an effective and visionary leader at the height of his powers. These were complicated years for Apple, but Steve handled almost every challenge in exactly the manner he wanted. He had fallen into leadership at such a young age, but he was comfortable in that role now, and justifiably sure of his capacity to guide Apple’s tens of thousands of employees to the goals he set for them. During these years, he would ensure the company’s continued success in personal computers by engineering a deft switch to a new kind of microprocessor; ruthlessly and successfully managing some major transitions in his executive team; and optimizing and building upon the efficiency and ambition of the company’s product development “treadmill,” as Tim Cook describes it. This is also when he delivered what is likely to be remembered as the most notable product of his life, the iPhone, and then improved even that by pivoting once again into a strategy he personally had not wanted to pu...

产品随想 | 陪读《Make Something Wonderful:Steve Jobs in his own words》1976-1996

  There’s lots of ways to be, as a person. And some people express their deep appreciation in different ways. But one of the ways that I believe people express their appreciation to the rest of humanity is to make something wonderful and put it out there. And you never meet the people. You never shake their hands. You never hear their story or tell yours. But somehow, in the act of making something with a great deal of care and love, something’s transmitted there. And it’s a way of expressing to the rest of our species our deep appreciation. So we need to be true to who we are and remember what’s really important to us. ──Steve, 2007 Introduction by Laurene Powell Jobs Much of what’s in these pages reflects guiding themes of Steve’s life: his sense of the worlds that would emerge from marrying the arts and technology; his unbelievable rigor, which he imposed first and most strenuously on himself; his tenacity in pursuit of assembling and leading great teams; and perhaps, above all,...

360T7 刷机步骤及固件

https://cmi.hanwckf.top/p/360t7-firmware/   360T7的固件支持由immortalwrt-mt798x项目提供支持,请参考: https://cmi.hanwckf.top/p/immortalwrt-mt798x https://github.com/hanwckf/immortalwrt-mt798x 刷机步骤 参考 此处 的办法开启原厂固件的UART和telnet功能 在以下链接下载360T7测试固件(纯净版,无任何插件) https://wwd.lanzout.com/b0bt9idwd 密码:ezex (此固件已过时,请选择其它更新的固件) 接下来将刷入修改版uboot。修改版uboot的优点有: 固件分区可达108MB,原厂uboot只能使用36M 自带一个简单的webui恢复页面 到以下仓库的Release页面下载uboot,目前暂时仅支持360T7,后续将支持更多mt798x路由器。 推荐使用 mt7981_360t7-fip-fixed-parts.bin , fixed-parts 代表uboot分区表在编译期间固定,不会随着uboot环境变量变化。 https://github.com/hanwckf/bl-mt798x/releases/latest 将 mt7981_360t7-fip-fixed-parts.bin 通过HFS等方式上传到路由器,使用以下命令刷入uboot mtd write mt7981_360t7-fip-fixed-parts.bin fip 确认刷入完毕后,拔掉路由器电源。然后将电脑的IP地址设置为固定的 192.168.1.2 ,接着按住路由器的RESET按钮后通电开机,等待8s后用浏览器进入 192.168.1.1 在uboot恢复页面选择要刷入的固件。immortalwrt-mt798x目前编译两个版本的360T7固件。 建议修改版uboot直接使用 immortalwrt-mediatek-mt7981-mt7981-360-t7-108M-squashfs-factory.bin ,两种固件区别如下: mt7981-360-t7-108M 为108M固件分区,原厂uboot不可启动,需要修改版u...

ISSUU使用指南--木喵

作者: 木喵   出处: Wonderworks 问:issuu是什么? 答:Issuu是国外的一个在线文档共享网站,它是你的PDF文档发布专家。它类似于我们熟悉的youtube,但它共享的是文档、杂志之类的文本。 简而言之、同志们想看国外的各种杂志? 想找国外的汇报文本么? 想借鉴国外学生的作品集么? 那么你就要用到它啦~ 今天主要和大家讲两个方面 一、如何在pc端使用和下载issuu上的pdf文档 首先我们打开issuu的网址 https://issuu.com/ 我们可以很清楚的看到网页上呢都是国外的杂志以及一些作者自己制作的pdf文档 首先我们点击右上角的 sign up  然后填写相关信息注册一个账户: 注册完成之后我们就可以搜索我们想要找的资料: 比如说,我想找一些分析图的资料,我们就搜索: architecture diagram 然后我们就可以看到相关的文档了: 点击你所选择的文档, 好了问题来了: sorry,this publication is not available 这个时候!就需要在用pc端的我们做一件必不可少的事: 翻墙 然后我们就能将页面刷新粗来了 好、接下来是非常有建设性意义的一步 怎样把我们网页上的文件 下载下来 呢? 截图? no~no~no~ 接下来,让木喵告诉你怎么下载: 首先你需要复制上面的网址 然后将 https://wenfan.hk/issuu/index_link.php 在另一个网址中打开 将你之前复制的pdf的网址粘贴在下面的对话框中 点击 I‘m not a robot 再点击 get it 然后会出现一堆网址代码 我们 全选 打开你的迅雷点击 新建 将你之前的复制粘贴到下载链接里 然后呢~我们就全都下载成功啦~ 然后我们回到之前的网页向下看 我们可以看到有上传文档的作者(记得要关注哟) 然后还有 info   share   stack   ❤ 如果...

Foobar2000 组件安装教程

 原作者 博客地址   汉化作者 Asion博客   关于foobar 2000的一些资源 前言 foobar2000 由于其软件架构特点以及开放的姿态,使得第三方很容易开发组件(component)来拓展它的功能。由于在官网下载的默认安装文件只带了少量几个默认的组件,满足不了使用的需求,例如:默认不带 ape,tta,tak 等音频文件格式的解码器,很多无损压缩格式音乐没法播放。所以自己下载安装组件是必备的基本技能。 foobar2000 的中文汉化版(Asion 汉化)为了方便使用,集成了无损压缩文件解码器以及一些其它有用的插件,安装时选上即可,不喜欢折腾的建议使用汉化版。 这里组件指的是 foobar2000 标准组件(*.dll 文件),而非 vst 插件等其它插件,姑且把组件分为两类: 官方组件: 英文版安装包自带,安装时可选择; 第三方组件:非官方自带的组件 除了 foo_input_std.dll 和 foo_ui_std.dll 这两个组件是必须的外,其它的所有组件都 非必需 的,可以随需要增删。第三方组件可以去 官网 、 官方论坛 或者 官方 wiki 去找,也可以去贴吧等地逛逛。 下载 还是要强调一下,这里说的是 foobar2000 component ,不是中文网上通常说的 vst 插件。 下载好的组件包一般是 xxx.zip 或 xxx.fb2k-component 格式的文件,也有用 7z 打包的。前两种都是 zip 压缩(只要把 fb2k-component 改成 zip 文件就变成了 zip: 包)。标准状况下压缩包里的内容结构应该是 xxx.zip yyy.dll README.txt (可能没有) LICENCE.txt (可能没有) (其它杂七杂八) 除少数外一般只有一个 xxx.dll 文件.一定要注意压缩包结构不能是: xxx.zip yy folder (文件夹) zzz.dll … 否则要解压缩,提取那个 dll 文件。 安装 方法一(推荐) 打开 foobar2000 的菜单 文件 > 首选项(file >preferences) 的 组件(components...

巴菲特致股东信-1976年

 笔记: 为什么选择轻资产行业:当竞争疯狂时,不会强迫加入降价大战 最终选择了费雪的思想,选择能理解的优秀企业,以合理的价格买入并长期拥有 翻译: 雪球:https://xueqiu.com/6217262310/131440258 备份:https://archive.ph/XLK0S 原文: To the Stockholders of Berkshire Hathaway Inc, After two dismal years, operating results in 1976 improved significantly. Last year we said the degree of progress in insurance underwriting would determine whether our gain in earnings would be "moderate" or "major." As it turned out, earnings exceeded even the high end of our expectations. In large part, this was due to the outstanding efforts of Phil Liesche's managerial group at National Indemnity Company. In dollar terms, operating earnings came to $16,073,000, or $16.47 per share. While this is a record figure, we consider return on shareholders' equity to be a much more significant yardstick of economic performance. Here our result was 17.3%, moderately above our long-term average and even further above the average o...

《Becoming Steve Jobs》Chapter 11 Do Your Level Best

As a mass-market consumer electronics device, the iPod would eventually be sold, of course, all the usual places: Best Buy, Circuit City, big-box department stores, and even the computer retailers like CompUSA. Steve disdained all these outlets. His obsession with his products continued well after they’d been manufactured. The tacky, low-margin hustle of these chains ran completely against the minimalist aesthetic of his products and the clean exuberance of his marketing. There was only one place where he really enjoyed seeing his products sold to the public: his own Apple stores, which had debuted four months ahead of the iPod. 觉得那些喧嚣、嘈杂的零售渠道,配不上自己极美的产品 Going back as far as the debut of the Mac, Steve had always groused about the way Apple computers were sold in its resellers’ stores. The way his computers were displayed and sold represented the very worst of what could go wrong when things weren’t done his way. The salespeople, always interested in quick turnover, seemed to make litt...

《Becoming Steve Jobs》Chapter 16 Blind Spots, Grudges, and Sharp Elbows

Steve could be pretty thin-skinned when someone prominent criticized the aesthetics of his products. He took great umbrage that Neil would, as Steve put it, “pop off in public like that without coming to talk to us about his technical concerns first.” From that point on he had rebuffed all of Neil’s attempts to smoke the peace pipe. 有趣 He had blind spots, grating behavioral habits, and a tendency to give in to emotional impulse that persisted his entire life. These characteristics are often used to make the case that Steve was an “asshole” or a “jerk,” or perhaps simply “binary”—that odd adjective often used to convey the sense that he was half asshole/half genius from birth to death. These aren’t useful, interesting, or enlightening descriptions. What’s more illuminating is to take a look at the specific ways in which Steve failed to do an effective job of tempering some of his weaknesses and antisocial traits, and to consider how, when, and why some of them continued to flare up even...