在 React-Native 应用程序中保护来自逆向工程的 API 密钥

Securing API key from reverse engineer in a React-Native app

我最近阅读了很多 post 和有关将敏感信息保护到 React Native 应用程序中的文章。据我了解,您无法完全保护您的敏感信息,只会让黑客更难获取这些信息。

因此,从这个角度来看,我想知道从外部服务器(即 Rest API).

我解释一下:

我知道 MitM 攻击,但让我的移动应用程序调用我的 API 以通过 HTTPS 根据请求获取 API 密钥是否更安全(更灵活)?这样,应用程序二进制文件中就不会保留任何敏感信息。

为了保护 MitM 攻击,我可以经常更改这些 API 键值,以便它们仅在短时间内保持有效。

我想听听任何人关于这种系统的优点和缺点。

APIs 误解

为了让您准备好我的回答,我将首先澄清一些关于 public/private APIs 和关于 who 的常见误解 真正在访问您的后端。

Public 和私人 APIs

我经常看到开发者认为他们的 API 是私有的,因为他们没有文档,没有在任何地方做广告,还有很多其他原因。

事实是,当您发布移动应用程序时,它与之通信的所有 API 现在都属于 public 域,如果这个 API 没有一个到位的身份验证和授权机制,然后它背后的所有数据都可以被互联网上的任何人访问,反向工程你的移动应用程序的工作方式。即使 APIs 有适当的身份验证,他们也可能容易受到错误实施的影响,并且根据 OWASP API Security Top 10 漏洞列表,有些人完全缺乏授权机制或有缺陷的机制。

访问 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 作为代表用户提出该请求的软件。

API 密钥服务

I know about MitM attacks, but would it be safer (and more flexible) to have my mobile app calling my API to get API keys on request thru HTTPS? This way, no sensitive info remains in the app binary files.

虽然您确实在应用程序二进制文件中没有任何敏感信息,但您还没有解决问题。在我看来,你暴露得更多,因为你现在从 public 获取 API 密钥并打开 API 端点。

我说它是开放的,因为您无法保证向它发出请求的确实是您的移动应用程序的真实且未被篡改的版本。

因此,现在攻击者需要做的就是中间人攻击您的移动应用程序或对其进行反编译,以查看您从哪个 API 端点获取 API 密钥来发出请求,然后从他们的自动化 scripts/bots 复制过程,因此您不再将它们硬编码在应用程序二进制文件中并不重要。

API 按键轮换

And to secure MitM attacks, I could frequently change those API key values so they would remains valid only on a short period of time.

根据上面的解释,在 API 密钥服务部分,您甚至可以将 API 密钥限制为仅用于攻击者仍然会成功的单个请求,因为攻击者将能够查询 API 端点以获取 API 密钥,就好像他是 后端所期望的 一样,您手机的真实且未被篡改的版本应用

所以,要明确一点,我支持 API 密钥轮换,但前提是您可以从安全的外部来源将它们放入您的移动应用程序,但您的方法是开放的,任何人都可以访问互联网。

I would like to hear anyone about PROS and CONS of such a system.

不建议实施您所描述的系统,因为如果不加以保护,这只是一场安全灾难。使用 API 密钥保护它,它只是回到最初的问题,缺点是您将您想要远离黑客的敏感信息还给移动设备。

对您来说最好的方法是使用反向代理来保护 API 密钥的私密性并防止被窥探。

反向代理方法

So, from that point of view, I would like to know if it wouldn't be "safer" to get those sensitive info (i.e. API keys) from an external server (i.e. Rest API).

您正在寻找的是实施反向代理,通常用于保护对第三方 API 和您自己的 API 的访问,方法是让移动应用委托API 请求反向代理,而不是要求 API 密钥从移动应用程序内部创建它们。

反向代理方法将避免在移动应用程序中对多个 API 密钥进行硬编码,但您仍然需要一个 API 密钥来保护对反向代理的访问,因此您仍然容易受到攻击MitM 攻击和移动应用程序的静态逆向工程。

现在的优势是您所有的敏感 API 密钥都是私有的,并且在您可以控制和使用所需的安全措施的环境中,以确保请求确实来自 what 您的后端期望您的移动应用程序是一个真实且未被篡改的版本。

通过阅读我写的文章了解更多关于使用反向代理的信息 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。

防止 MitM 攻击

当在移动应用程序中实施证书固定以保护 https 通道时,API 请求中的敏感数据将更加安全,不会被提取。

我建议您阅读 中的 Preventing MitM Attacks 部分我提出了另一个问题,您将在其中学习如何实现静态证书固定以及如何绕过它。

尽管可以绕过证书固定,但我仍然强烈建议实施它,因为它减少了移动应用程序的攻击面。

可能更好的解决方案

我建议您阅读 我给出的问题 How to secure an API REST for mobile app?,尤其是部分 加固和屏蔽移动应用程序保护API服务器可能更好的解决方案

解决方案是使用移动应用证明解决方案,让您的后端高度确信请求来自 它所期望的,一个真正的和未被篡改的移动应用程序版本。

您想加倍努力吗?

在任何对安全问题的回答中,我总是喜欢引用 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.