简体   繁体   English

将 Api 密钥安全地存储在 Flutter 中或将付款详细信息发送到我的服务器?

[英]Storing Api Keys Securely in Flutter or Sending Payment Details to my Server?

I'm building a flutter app where I will be taking a users' card details and tokenizing them.我正在构建一个 flutter 应用程序,我将在其中获取用户的卡详细信息并对其进行标记。 Currently, the API key for my payment provider is in the client app.目前,我的支付提供商的 API 密钥位于客户端应用程序中。 The reason for this is to prevent having to send the credit card details to my server and then sending them to the payment provider.这样做的原因是为了防止必须将信用卡详细信息发送到我的服务器,然后将它们发送到支付提供商。 All in the name of PCI compliance.一切都以 PCI 合规为名。

It is however, ubiquitously stated, that you never want to store any API Keys in the client app which I understand.但是,据我所知,您永远不想在客户端应用程序中存储任何 API 密钥。 The question is, what's the best way to go about this?问题是,关于这个问题,go 的最佳方法是什么? My instincts say to move the API key to the server, and just send the card details once for the tokenization.我的直觉说将 API 密钥移动到服务器,只需发送一次卡详细信息以进行标记化。 However, is this making the road to PCI compliance harder?然而,这是否会让 PCI 合规之路变得更加艰难?

Any guidance on this would be appreciated.对此的任何指导将不胜感激。

Thanks谢谢

you should implement a private/public key approach.. where server and client both have only one key so neither one or the other can do nothing if not paired.您应该实施私钥/公钥方法..其中服务器和客户端都只有一个密钥,因此如果不配对,则任何一个都不能做任何事情。

or try to give a look to jwt token api validation或尝试查看 jwt 令牌 api 验证

Three things can be done here.在这里可以做三件事。

  1. Use RemoteConfig to store all your config infos.使用 RemoteConfig 存储您的所有配置信息。 And, store it in Flutter Secure Storage.并且,将其存储在 Flutter 安全存储中。 Con: RemoteConfig is only fetched on App start.缺点:仅在 App 启动时获取 RemoteConfig。

  2. Authenticate user first using JWT Token, then send the required data for user in the token itself.首先使用 JWT 令牌对用户进行身份验证,然后在令牌本身中发送用户所需的数据。 Con: User must be authenticated first to receive data.缺点:用户必须首先通过身份验证才能接收数据。

  3. Authorize the user with permissions.授予用户权限。 So, that both authentication and authorization are handled and nothing needs to be done without a key.因此,身份验证和授权都得到处理,没有密钥就不需要做任何事情。

The #3 is the apporach we are using now. #3 是我们现在使用的方法。 Of, course you can also use OpenIDConnect protocols to bring apps, servers and clients under one ecosystem too.当然,您也可以使用 OpenIDConnect 协议将应用程序、服务器和客户端置于一个生态系统中。

In all of these cases, the one holding the physical device will always have access to your servers after authenticated.在所有这些情况下,持有物理设备的人在通过身份验证后始终可以访问您的服务器。 Make sure to check for device id during the access.确保在访问期间检查设备 ID。 In the end, user can still fake all of this except for authorized details.最后,除了授权的细节之外,用户仍然可以伪造所有这些。

Your Problem你的问题

It is however, ubiquitously stated, that you never want to store any API Keys in the client app which I understand.但是,据我所知,您永远不想在客户端应用程序中存储任何 API 密钥。 The question is, what's the best way to go about this?问题是,关于这个问题,go 的最佳方法是什么? My instincts say to move the API key to the server, and just send the card details once for the tokenization.我的直觉说将 API 密钥移动到服务器,只需发送一次卡详细信息以进行标记化。

Your instincts are correct, and I have wrote about the reasons why third-party secrets should be kept in a backend you can control, and you can read more in my article Using a Reverse Proxy to Protect Third Party APIs :你的直觉是正确的,我已经写了为什么第三方机密应该保存在你可以控制的后端的原因,你可以在我的文章使用反向代理保护第三方 API中阅读更多内容:

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.在本文中,您将首先了解第三方 API 是什么,以及为什么不应该直接从移动应用程序中访问它们。 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 的访问。

However, is this making the road to PCI compliance harder?然而,这是否会让 PCI 合规之路变得更加艰难?

I cannot help you here, because I am not versed in PCI compliance but my instinct says it will make it a lot harder, because your are now dealing with Personal Identifiable Information (PII) from you user in your backend.我在这里无法为您提供帮助,因为我不精通 PCI 合规性,但我的直觉认为这会使它变得更加困难,因为您现在正在处理后端用户的个人身份信息 (PII)。

Possible Solutions to your Problem您的问题的可能解决方案

Any guidance on this would be appreciated.对此的任何指导将不胜感激。

Anything that is on the client side must be considered public, because they can be reverse engineered statically or dynamically at runtime, and guess what, I also wrote some articles on it.任何在客户端的东西都必须被认为是公开的,因为它们可以在运行时被静态或动态地逆向工程,你猜怎么着,我也写了一些关于它的文章。

Open source tools exist to make all this a lot easier and even possible to be done by non-developers as I show on my articles.正如我在我的文章中展示的那样,存在开源工具可以使所有这些变得更容易,甚至可以由非开发人员完成。

Let's start by learning how to extract the API key with static binary analysis:让我们从学习如何用 static 二进制分析提取 API 密钥开始:

How to Extract an API key from a Mobile App with Static Binary Analysis : 如何使用 Static 二进制分析从移动应用程序中提取 API 密钥

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)来演示如何逆向工程我们的移动应用程序的 APK。 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. MobSF 是一组开源工具,它们在一个有吸引力的仪表板中显示其结果,但在 MobSF 和其他地方使用的相同工具可以单独使用以实现相同的结果。

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.在本文中,我们将使用Android Hide Secrets研究存储库,它是一个虚拟移动应用程序,使用几种不同的技术隐藏了 API 密钥。

On this article the awesome MobSF tool was used to easily extract an API key from the binary, but if an attacker cannot extract it from the binary, because it was hidden in native C code or was provided via a remote configuration on App boot, then an attacker will probably use a MitM attack to extract the API key for you credit card provider, and yes I have an article to show you how its done:在这篇文章中,令人敬畏的 MobSF 工具用于轻松地从二进制文件中提取 API 密钥,但如果攻击者无法从二进制文件中提取它,因为它隐藏在本机 C 代码中,或者是通过应用程序启动时的远程配置提供的,那么攻击者可能会使用中间人攻击为您的信用卡提供商提取 API 密钥,是的,我有一篇文章向您展示它是如何完成的:

Steal that Api Key with a Man in the Middle Attack : 使用中间人攻击窃取 Api 密钥

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 . 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.因此,在本文中,您将学习如何在您控制的移动设备中设置和运行中间人攻击以拦截 https 流量,从而窃取 API 密钥。 Finally, you will see at a high level how MitM attacks can be mitigated.最后,您将了解如何缓解中间人攻击。

The next step on hardening your security against MitM attacks is to use certificate pinning, and I also have an article of mine on how to do it:加强对中间人攻击的安全性的下一步是使用证书固定,我也有一篇关于如何做到这一点的文章:

Securing HTTPS with Certificate Pinning : 使用证书固定保护 HTTPS

In order to demonstrate how to use certificate pinning for protecting the https traffic between your mobile app and your API server, we will use the same Currency Converter Demo mobile app that I used in the previous article.为了演示如何使用证书固定来保护您的移动应用程序和 API 服务器之间的 https 流量,我们将使用我在上一篇文章中使用的相同 货币转换器演示移动应用程序。

In this article we will learn what certificate pinning is, when to use it, how to implement it in an Android app, and how it can prevent a MitM attack.在本文中,我们将了解什么是证书固定、何时使用、如何在 Android 应用程序中实现它,以及它如何防止中间人攻击。

On this article you will learn how to use the Mobile Certificate Pinning Generator free tool to easily generate your Android and iOS configurations.在本文中,您将了解如何使用移动证书固定生成器免费工具轻松生成 Android 和 iOS 配置。

Now, you have a mobile app secured against MitM attacks, but as everything in software you often find ways to get around, and unfortunately this isn't an exception, because certificate pinning can be bypassed in a device the attacker controls, and yes I can show you how to do it:现在,您有一个针对中间人攻击的移动应用程序,但是作为软件中的所有内容,您经常会找到绕过的方法,不幸的是,这不是一个例外,因为可以在攻击者控制的设备中绕过证书固定,是的,我可以告诉你怎么做:

How to Bypass Certificate Pinning with Frida on an Android App : 如何在 Android 应用程序上绕过 Frida 的证书锁定

Today I will show how to use the Frida instrumentation framework to hook into the mobile app at runtime and instrument the code in order to perform a successful MitM attack even when the mobile app has implemented certificate pinning.今天我将展示如何使用 Frida 检测框架在运行时挂钩到移动应用程序并检测代码以执行成功的中间人攻击,即使移动应用程序已经实现了证书锁定。

Bypassing certificate pinning is not too hard, just a little laborious, and allows an attacker to understand in detail how a mobile app communicates with its API, and then use that same knowledge to automate attacks or build other services around it.绕过证书锁定并不太难,只是有点费力,并且允许攻击者详细了解移动应用程序如何与其 API 通信,然后使用相同的知识来自动化攻击或围绕它构建其他服务。

Frida is a very powerful tool and when used by a skilled attacker it will allow him to even hook to your app code to extract any secret from it without the need to disable pinning to perform the MitM attack. Frida 是一个非常强大的工具,当熟练的攻击者使用它时,他甚至可以连接到您的应用程序代码以从中提取任何秘密,而无需禁用 pinning 来执行 MitM 攻击。 The attacker only needs to figure out the name of the function that uses or retrieves the secret in order to hook on it at runtime and extract such secret.攻击者只需要弄清楚使用或检索秘密的 function 的名称,以便在运行时挂钩并提取该秘密。 To find the name of the function the attacker will statically reverse the mobile app binary and read your source code, even if the code is obfuscated.为了找到 function 的名称,攻击者将静态反转移动应用程序二进制文件并读取您的源代码,即使代码被混淆。

While certificate pinning can be bypassed it is an important security addition to your mobile app security, because it's increasing the level of defences an attacker needs to bypass and increasing the skills set required.虽然可以绕过证书锁定,但它是对您的移动应用安全性的重要安全补充,因为它提高了攻击者需要绕过的防御级别并增加了所需的技能集。 Your mobile app must use as many security defences as possible, just like it was done in medieval castles.您的移动应用程序必须使用尽可能多的安全防御措施,就像在中世纪城堡中所做的那样。

Until now you saw how you can progressively enhance the security of your mobile app to make more difficult to extract the card provider API key, but you are still vulnerable to its extraction.到目前为止,您已经了解了如何逐步增强移动应用程序的安全性,使提取卡提供商 API 密钥变得更加困难,但您仍然容易受到其提取的攻击。

You now need to find a solution that allows you to detect MitM attacks, tampered binaries, Frida at runtime and that at the same time time can deliver a runtime secret to mobile apps that pass a mobile app attestation that guarantees with a very high degree of confidence that such threats are not present.您现在需要找到一种解决方案,允许您在运行时检测 MitM 攻击、被篡改的二进制文件、Frida,同时可以向通过移动应用程序证明的移动应用程序提供运行时秘密,该证明以非常高的程度保证相信不存在此类威胁。 Unfortunately I don't know any open-source project that can deliver all this features, but a commercial solution exists (I work there), and if you want to learn more about you can read the article:不幸的是,我不知道任何可以提供所有这些功能的开源项目,但是存在商业解决方案(我在那里工作),如果您想了解更多信息,可以阅读这篇文章:

Hands-on Mobile App and API Security - Runtime Secrets Protection 动手移动应用程序和 API 安全 - 运行时机密保护

In a previous article we saw how to protect API keys by using Mobile App Attestation and delegating the API requests to a Proxy.在上一篇文章中,我们了解了如何通过使用移动应用证明并将 API 请求委托给代理来保护 API 密钥。 This blog post will cover the situation where you can't delegate the API requests to the Proxy, but where you want to remove the API keys (secrets) from being hard-coded in your mobile app to mitigate against the use of static binary analysis and/or runtime instrumentation techniques to extract those secrets.这篇博文将介绍您无法将 API 请求委托给代理的情况,但您希望删除 API 密钥(密钥)以防止在移动应用程序中硬编码以减少使用 ZA81259CEF1 二进制文件分析456E959C6274E和/或运行时检测技术来提取这些秘密。

We will show how to have your secrets dynamically delivered to genuine and unmodified versions of your mobile app, that are not under attack, by using Mobile App Attestation to secure the just-in-time runtime secret delivery.我们将展示如何通过使用移动应用程序证明来确保即时运行时秘密交付,将您的秘密动态传送到不受攻击的移动应用程序的正版和未修改版本。 We will demonstrate how to achieve this with the same Astropiks mobile app from the previous article.我们将演示如何使用上一篇文章中相同的 Astropiks 移动应用程序来实现这一点。 The app uses NASA's picture of the day API to retrieve images and descriptions, which requires a registered API key that will be initially hard-coded into the app.该应用程序使用 NASA 当天的图片 API 来检索图像和描述,这需要一个已注册的 API 密钥,该密钥最初将硬编码到应用程序中。

Do You Want To Go The Extra Mile?您想要额外的里程吗?

In any response to a security question I always like to reference the excellent work from the OWASP foundation.在回答安全问题时,我总是喜欢参考 OWASP 基金会的出色工作。

For APIS对于 APIS

OWASP API Security Top 10 OWASP API 安全前 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. OWASP API 安全项目旨在通过强调不安全 API 中的潜在风险并说明如何减轻这些风险,为软件开发人员和安全评估人员提供价值。 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 API 安全项目将创建和维护前 10 名 API 安全风险文档,以及创建或评估 API 时最佳实践的文档门户。

For Mobile Apps对于移动应用

OWASP Mobile Security Project - Top 10 risks OWASP 移动安全项目 - 十大风险

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. OWASP 移动安全项目是一个集中资源,旨在为开发人员和安全团队提供构建和维护安全移动应用程序所需的资源。 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 : OWASP - 移动安全测试指南

The Mobile Security Testing Guide (MSTG) is a comprehensive manual for mobile app security development, testing and reverse engineering.移动安全测试指南 (MSTG) 是移动应用安全开发、测试和逆向工程的综合手册。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM