简体   繁体   English

如何在 Android 应用程序中保护 Azure 翻译 API 密钥

[英]How to secure Azure Translation API key in Android App

I am developing Android Translation App.我正在开发 Android 翻译应用程序。 The app uses Azure cognitive service text translation API key.该应用程序使用 Azure 认知服务文本翻译 API 密钥。

The API key is inside the App source code in Java file, and if I publish the app, people can crack the app apk file and use my API key, which will be quite expensive. API密钥在Java文件中的App源代码中,如果我发布该应用程序,人们可以破解该应用程序的apk文件并使用我的API密钥,这将是相当昂贵的。 Is there away how to protect my API key from being stolen?有没有办法保护我的 API 密钥不被盗? There is no login in the app, no sign in and anyone can download it from play store.该应用程序无需登录,无需登录,任何人都可以从Play商店下载。

How can I protect the API key from being stolen?如何保护 API 密钥不被盗?

You can use android Keystore which is there for a scenario like yours official docs您可以使用 android Keystore 用于像您的官方文档这样的场景

or can refer to a sample code here或者可以参考这里的示例代码

Reverse Engineering逆向工程

The API key is inside the App source code in Java file, and if I publish the app, people can crack the app apk file and use my API key API 密钥在 Java 文件中的应用程序源代码中,如果我发布该应用程序,人们可以破解该应用程序 apk 文件并使用我的 API 密钥

Yes, and its not hard to do when a lot of different open-sources exist to make this task easy to achieve, even by non technical people, like I demo with the Mobile Security Framework in my article How to Extract an API key from a Mobile App with Static Binary Analysis :是的,当存在许多不同的开源软件以使这项任务易于实现时,即使是非技术人员也可以做到这一点并不难,就像我在我的文章How to Extract an API带有 Static 二进制分析的移动应用程序

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 和其他地方使用的相同工具可以单独使用以实现相同的结果。

Also, you can use the grep command in the upload directory of MobSF to find other secrets that MobSF is not able to find:另外,您可以在 MobSF 的上传目录中使用grep命令来查找 MobSF 无法找到的其他秘密:

grep -irl '_key"' --include 'strings.xml' --include "AndroidManifest.xml"

and

grep -irn '_key' --include '*.java' --include "*.smali" ./java_source/tld/domain ./smali_source/tld/domain

Replace _key with whatever other pattern you may want to look for._key替换为您可能想要查找的任何其他模式。

Replace tld/domain with the one used by the mobile app being reverse engineered, eg: com/example .tld/domain替换为被反向工程的移动应用程序使用的域名,例如: com/example

Secret Hidden in Native C Code隐藏在原生 C 代码中的秘密

Secrets can be hidden in native C code as in the demo on the above linked article :秘密可以隐藏在本机 C 代码中,如上述链接文章的演示中所示:

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 密钥。

But then if you cannot find it with static analysis then you do a MitM attack, as I demo on this other article Steal that Api Key with a Man in the Middle Attack :但是,如果您无法通过 static 分析找到它,那么您将进行中间人攻击,正如我在另一篇文章中演示的那样,使用中间人攻击窃取 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.最后,您将了解如何缓解中间人攻击。

Secrets in Hardware Keystores or Vaults硬件密钥库或保险库中的秘密

An alternative to the MitM attack is to use an instrumentation framework to hook at runtime to the code that retrieves the secret, be it from the Android Hardware baked Keystore or from any other vault provided by your cloud provider of choice: MitM 攻击的替代方法是使用检测框架在运行时挂钩到检索密钥的代码,无论是来自 Android 硬件烘焙密钥库还是来自您选择的云提供商提供的任何其他保险库:

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.挂钩任何 function,监视加密 API 或跟踪私有应用程序代码,无需源代码。 Edit, hit save, and instantly see the results.编辑,点击保存,立即查看结果。 All without compilation steps or program restarts.所有这些都无需编译步骤或程序重新启动。

Third Party Services第三方服务

The API key is inside the App source code in Java file, and if I publish the app, people can crack the app apk file and use my API key, which will be quite expensive. API密钥在Java文件中的App源代码中,如果我发布该应用程序,人们可以破解该应用程序的apk文件并使用我的API密钥,这将是相当昂贵的。

Yes, it can be very expensive and you only discover it when the bill is already huge, despite you can set billing alerts, they don't work as you may think they do.是的,它可能非常昂贵,并且您只有在账单已经很大时才发现它,尽管您可以设置账单警报,但它们并不像您想象的那样起作用。

Reverse Proxy to the Rescue反向代理救援

Is there away how to protect my API key from being stolen?有没有办法保护我的 API 密钥不被盗?

Best practices don't recommend to use third party services directly from within a mobile app, instead they should be delegated to the API backend for the mobile app or to a Reverse Proxy, as I wrote in this my other article Using a Reverse Proxy to Protect Third Party APIs :最佳实践不建议直接从移动应用程序中使用第三方服务,而是应将它们委托给移动应用程序的 API 后端或反向代理,正如我在我的另一篇文章使用反向代理保护第三方 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 的访问。

So, by now you may think that you are just shifting from protecting the secret for accessing the translation API for the one to access the Reverse Proxy or API backend, and you are right, but with a huge difference, that makes all the difference, you are in control of the Reverse Proxy and/or API backend, therefore you can closely monitor the traffic, throtle/shutdown it and apply as many security defenses as necessary to keep things under control.所以,现在你可能认为你只是从保护访问翻译 API 的秘密转移到访问反向代理或 API 后端的秘密,你是对的,但有很大的不同,这一切都不同,您可以控制反向代理和/或 API 后端,因此您可以密切监视流量、限制/关闭它并根据需要应用尽可能多的安全防御来控制事情。

Open APIs开放 API

There is no login in the app, no sign in and anyone can download it from play store.该应用程序无需登录,无需登录,任何人都可以从Play商店下载。

So, you have created a friction-less user experience but you have also created a security nightmare for yourself to solve.因此,您创造了一种无摩擦的用户体验,但您也创造了一个需要自己解决的安全噩梦。

Before I delve in more details it's important to first clarify some misconception around the difference between who vs what is accessing the backend.在深入研究更多细节之前,重要的是首先澄清一些关于访问后端的人员与访问对象之间差异的误解。

The Difference Between WHO and WHAT is Accessing the API Server WHO和WHAT访问API服务器的区别

I wrote a series of articles around API and Mobile security, and in the article Why Does Your Mobile App Need An Api Key?我围绕 API 和移动安全写了一系列文章,并在文章为什么您的移动应用程序需要 Api 密钥? you can read in detail the difference between who and what is accessing your API server, but I will extract here the main takes from it:您可以详细阅读访问的 API 服务器的人员和对象之间的区别,但我将在这里提取主要内容:

The what is the thing making the request to the API server.向 API 服务器发出请求的原因是什么 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?它真的是您的移动应用程序的真实实例,还是机器人、自动脚本或攻击者使用 Postman 之类的工具手动在您的 API 服务器周围进行攻击?

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.是移动应用程序的用户,我们可以通过多种方式进行身份验证、授权和识别,例如使用 OpenID Connect 或 OAUTH2 流。

You can think about the who as the user your API backend or Reverse Proxy could be able to Authenticate and Authorize access to the data(if you were using it), and think about the what as the software making that request in behalf of the user.您可以考虑将作为用户您的 API 后端或反向代理能够验证和授权对数据的访问(如果您正在使用它),并考虑代表用户发出请求的软件是什么.

In an open API you are not able to identify the who in the request, but even if you were able it would not be enough to lock-down the mobile app with the API backend or Reverse Proxy.在打开的 API 中,您无法识别请求中的,但即使您能够识别,也不足以使用 API 后端或反向代理锁定移动应用程序。

So, what you need to strength the delegation of the third party service to your own backend or reverse proxy is to find a way of locking them down with the mobile app.因此,您需要加强将第三方服务委托给您自己的后端或反向代理的是找到一种使用移动应用程序锁定它们的方法。

A Possible Additional Solution一个可能的附加解决方案

Is there away how to protect my API key from being stolen?有没有办法保护我的 API 密钥不被盗?

The mobile app and the API backend and/or Reverse Proxy can be tight together by locking them down in a way that they only accept requests comming from a genuine and untampered version of your mobile app by introducing the Mobile App Attestation concept, and I recommend you to read this answer I gave to the question How to secure an API REST for mobile app?移动应用程序和 API 后端和/或反向代理可以通过引入移动应用程序证明概念以仅接受来自您的移动应用程序的真实和未篡改版本的请求的方式将它们锁定在一起,我建议你读这个答案我给了这个问题如何保护移动应用程序的 API REST? , especially the sections Hardening and Shielding the Mobile App , Securing the API Server and A Possible Better Solution to learn some more defense techniques and to learn about Mobile App Attestation. ,特别是强化和屏蔽移动应用程序保护 API 服务器可能更好的解决方案部分,以了解更多防御技术并了解移动应用程序证明。

In a nutshell the Mobile App Attestation solution will allow for any backend to assert with a very high degree of confidence that the request comes indeed from what you expect, a genuine and untampered version of your mobile app, aka not from a bot, script, cURL or any other tool.简而言之,移动应用程序证明解决方案将允许任何后端以非常高的信心断言请求确实来自的期望,即您的移动应用程序的真实且未经篡改的版本,也就是不是来自机器人、脚本, cURL 或任何其他工具。

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) 是移动应用安全开发、测试和逆向工程的综合手册。

Another way to make your keys hard to reverse engineer is to save them in the NDK as native code.使您的密钥难以进行逆向工程的另一种方法是将它们作为本机代码保存在 NDK 中。

One more solution is to create you own service proxy, which is your REST service that takes user requests, gets translation responses from AWS, and sends back responses to mobile devices.另一种解决方案是创建您自己的服务代理,这是您的 REST 服务,它接受用户请求,从 AWS 获取翻译响应,并将响应发送回移动设备。

The beauty is the secret key won't be stored on the mobile device while the downside is that your REST service becomes the single point of failure therefore you may need to provision redundant servers.优点是密钥不会存储在移动设备上,而缺点是您的 REST 服务成为单点故障,因此您可能需要配置冗余服务器。

However, as your service is light-weighted, it shall scale well.但是,由于您的服务是轻量级的,它应该可以很好地扩展。

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

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