您如何在移动应用程序中保护项目范围的 API 密钥

How do you secure project-wide API keys in a mobile app

关于 S.E 的许多其他问题都问了同样的问题。

一些评分最高的问题被关闭为基于意见,例如Best practice for storing and protecting private API keys in applications

此处发布了一些最佳做法: https://support.google.com/googleapi/answer/6310037?hl=en

总结:

  1. 不要直接在代码中嵌入 API 键
  2. 不要将 API 密钥存储在应用程序源代码树中的文件中
  3. 限制您的 API 密钥仅供 IP 地址、引荐来源网址和移动设备使用 需要它们的应用
    (如果您不知道移动应用程序上的客户端 运行 将使用密钥连接到哪里,则这是不可能的)
  4. 限制您的 API 密钥仅可用于某些 APIs
    (当您不是 API 的创建者时,这是不可能的)
  5. 删除不需要的API键
  6. 定期重新生成您的 API 密钥
    (第 5 条和第 6 条对于移动应用程序也不实用,除非您强制用户每隔几天更新一次应用程序。)

如果我们注意那些最佳实践,它给出了“不”但没有明确“需要访问”的移动应用程序的“做”,例如,股票交易 API。

依靠ProGuard或DexGuard等混淆并不能使上述#1或#2无效,那么如何解决这个问题?

TL/DR:你没有。

长答案:

应用程序可以读取随应用程序分发的任何密钥以供其使用。因此,该应用程序具有读取密钥所需的内容,即使它已被加密或混淆。攻击者可以使用与应用程序相同的技术来获取密钥。

同样,从外部来源获取密钥并不能保护它。同样,攻击者可以使用相同的渠道获取密钥的副本。

除了攻击应用程序获取密钥的渠道(从包内的加密存储,或从外部源),攻击者还可以从应用程序的内存或通过拦截网络传输来获取它。

唯一安全的解决方案是永远不要在最终用户设备上保留密钥副本。

密钥应保存在安全的服务器上,该服务器将充当用户设备和最终服务之间的中间人。客户端设备对终端服务的任何请求都需要通过此服务器进行路由。

具有“全局项目密钥”的服务器应代表最终用户向最终服务发出请求,并 return 将结果(绝不是任何密钥)发送给客户端。对于使用此服务器的客户端,必须使用每个用户经过身份验证的会话。在将请求转发到最终服务之前,服务器必须为每个请求验证此会话。

总结:

在客户端和终端服务之间使用安全服务器,使用全局密钥代表客户端发出请求。

编辑: 旁注:需要区分每个用户的密钥和项目范围的密钥。在该用户的设备上保留特定于一个人的密钥是可以接受的。

隐藏 API 键

Relying on obfuscation such as ProGuard or DexGuard does not void #1 or #2 above, so how does one solve this issue?

虽然 ProGuard 或 DexGuard 无法解决问题,但它们会通过重命名 API 键关联的名称来解决问题,而如果 API 键隐藏在本机中C 代码的字符串在执行静态二进制分析时不会与任何引用相关联,因此如我在文章 How to Extract an API key from a Mobile App with Static Binary Analysis:

中所示,它更难提取

The range of open source tools available for reverse engineering is huge, and we really can't scratch the surface of this topic in this article, but instead we will focus in using the Mobile Security Framework(MobSF) to demonstrate how to reverse engineer the APK of our mobile app. MobSF is a collection of open source tools that present their results in an attractive dashboard, but the same tools used under the hood within MobSF and elsewhere can be used individually to achieve the same results.

During this article we will use the Android Hide Secrets research repository that is a dummy mobile app with API keys hidden using several different techniques.

虽然此解决方案使得通过二进制分析提取 API 密钥变得非常困难,但这并非不可能,但存在更简单的方法来提取 API 密钥,例如通过执行我在文章 Steal that Api Key with a Man in the Middle Attack:

中演示的 MitM 攻击

In order to help to demonstrate how to steal an API key, I have built and released in Github the Currency Converter Demo app for Android, which uses the same JNI/NDK technique we used in the earlier Android Hide Secrets app to hide the API key.

So, in this article you will learn how to setup and run a MitM attack to intercept https traffic in a mobile device under your control, so that you can steal the API key. Finally, you will see at a high level how MitM attacks can be mitigated.

检测框架也可以与 MitM 攻击结合使用,以在移动应用程序使用它时绕过证书固定。一个流行的是 Frida:

Inject your own scripts into black box processes. Hook any function, spy on crypto APIs or trace private application code, no source code needed. Edit, hit save, and instantly see the results. All without compilation steps or program restarts.

反向代理服务器

If we pay attention to those best practices it gives Don't but no clear Do's for Mobile apps that need access to, for example, a stock trading API.

解决在移动应用程序代码中存储 API 密钥问题的另一种常见方法是将访问第三方 API 的责任委托给反向代理,就像我在文章中写的那样 Using a Reverse Proxy to Protect Third Party APIs:

In this article you will start by learning what Third Party APIs are, and why you shouldn’t access them directly from within your mobile app. Next you will learn what a Reverse Proxy is, followed by when and why you should use it to protect the access to the Third Party APIs used in your mobile app.

现在,这只会将问题从保护第三方服务的 API 密钥转移到保护 API 密钥或任何其他 secret/identifier 用于访问反向代理,但优点是您现在可以控制第三方 API 的访问方式,同时让 API 密钥无法访问它移动应用程序攻击者。

使用用户身份验证来保护对反向代理的访问将限制攻击范围,但不会消除攻击,因为反向代理服务器需要能够区分 来自 what 正在执行请求,根据我的经验,我发现任何资历的开发人员都不会经常意识到这种差异或对此有误解。

访问 API 服务器的 WHO 和 WHAT 之间的区别

我写了一系列关于API和移动安全的文章,在文章Why Does Your Mobile App Need An Api Key?中你可以详细阅读who和之间的区别什么 正在访问您的后端,无论是 API 服务器还是反向代理服务器。我将在这里提取主要内容:

The what is the thing making the request to the API server. Is it really a genuine instance of your mobile app, or is it a bot, an automated script or an attacker manually poking around your API server with a tool like Postman?

The who is the user of the mobile app that we can authenticate, authorize and identify in several ways, like using OpenID Connect or OAUTH2 flows.

因此,考虑 who 作为用户,反向代理服务器将能够验证和授权访问第三方服务(您的股票交易 API例如),然后将 what 视为代表用户发出该请求的软件。

加固反向代理服务器

If we pay attention to those best practices it gives Don't but no clear Do's for Mobile apps that need access to, for example, a stock trading API.

反向代理服务器可以配备用户行为分析 (UBA) 解决方案,使其能够区分 whowhat 正在执行请求,但这将在最大努力的基础上完成,基于人工智能算法使用的历史数据来对是人类还是不执行请求的概率进行评分,也就是 who.

虽然 UBA 解决方案会显着减少攻击面,但它仍然是一个负面识别模型,因为会出现假阴性和阳性,并且需要持续监控算法使用的 policies/rules在不阻止太多合法用户和不让太多攻击通过之间取得最佳平衡。

可以使用积极的识别模型,通过采用我在 中推荐的移动应用证明概念,我给出了 如何保护 API REST 的问题对于移动应用程序?,在 可能更好的解决方案

部分

因此,简而言之,移动应用程序认证可以描述为一种解决方案,能够以非常高的置信度锁定具有移动应用程序的反向代理服务器,因为反向代理服务器现在只能接受来自它所期望的的请求,也就是移动应用程序的真实且未被篡改的版本。

您想加倍努力吗?

在任何对安全问题的回答中,我总是喜欢引用 OWASP 基金会的出色工作。

对于APIS

OWASP API Security Top 10

The OWASP API Security Project seeks to provide value to software developers and security assessors by underscoring the potential risks in insecure APIs, and illustrating how these risks may be mitigated. In order to facilitate this goal, the OWASP API Security Project will create and maintain a Top 10 API Security Risks document, as well as a documentation portal for best practices when creating or assessing APIs.

对于移动应用程序

OWASP Mobile Security Project - Top 10 risks

The OWASP Mobile Security Project is a centralized resource intended to give developers and security teams the resources they need to build and maintain secure mobile applications. Through the project, our goal is to classify mobile security risks and provide developmental controls to reduce their impact or likelihood of exploitation.

OWASP - Mobile Security Testing Guide:

The Mobile Security Testing Guide (MSTG) is a comprehensive manual for mobile app security development, testing and reverse engineering.