在 Android 应用程序中不应该硬编码的内容

What NOT to hard-code in an Android Application

我知道将带有凭据的数据库连接字符串放入 API-Key 等中非常糟糕,因为您可以反编译它以查看值...

那么母鹿的一切都需要在 API 后面吗?

例如,实际上需要放在 Web 服务后面的是什么:

SQL 查询是否需要放在 Web 服务后面,或者我们可以对存储过程进行参数化查询吗?我假设这也需要在网络服务后面,只是 return 类似 JSON 数组等的东西

我还没有真正看到任何关于移动应用程序的 Web 服务不应该放置什么的清单。

我来自 Web 开发职位,所以直到现在这还不是真正的问题。

我们是否基本上需要通过网络服务 运行 我们的应用程序?如果需要,我们如何着手将网络服务与应用程序结合起来,因为我们无法为首先是 Web 服务?

总结:

我们是否需要创建一个包罗万象的 Web 服务,以便在数据库和 android 应用程序客户端之间进行交换,对于任何第 3 方都一样 API?

此外,我们如何授权 android 应用程序与我们的网络服务交互,因为该网络服务的目的首先是防止硬编码密钥?

通常您会创建一个后端服务,该服务会公开一些 API 供您的应用使用

EG 你可以在我的服务器上有一个 API。com/something 这将是一个 PHP 脚本或一些其他服务器端语言。这将 return 应用程序需要显示的数据。您使用 JSON 之类的方式对数据进行编码,应用程序可以使用合适的库读取这些数据。

发送数据的工作方式类似,制作一个 API 可以通过 HTTP 接受 POST 请求并将数据存储回数据库。登录名和 SQL 都应该在服务器端。

例如,应用向服务器发送请求。com/storeInformation 带有参数 someInfo="hello world"

服务器端(例如php)然后执行查询 INSERT INTO myTable VALUES (...whatever..)

重点是客户端应用程序永远不需要直接发送 SQL(因为这不可信),而是它应该发送它想要完成的事情,服务器决定如何完成。

通常,请记住永远不能信任客户。任何人都可以使用任何参数调用您的服务器 APIs。

I understand that putting DB connection strings with the credentials in, API-Key's etc is really bad as you can de-compile it to see the values...

是的,这在任何类型的应用程序中都非常糟糕,无论是移动应用程序、网络应用程序还是物联网应用程序。

So doe's everything need to be behind an API?

是的,您的应用程序需要尽可能地愚蠢。换句话说,您的应用程序中的代码应尽可能多地关注表示,任何业务逻辑都应委托给 API 服务器。这种方法有一个巨大的优势,只要你有一个错误,你只需要更新 API 服务器,而不是应用程序本身,并等待所有客户端在他们的移动设备上更新它们。对于 Web 应用程序,您不想向在浏览器中按 F12 并开始在开发人员工具面板中调试代码的任何人透露您的业务逻辑。

Does an SQL query need to be put behind a web-service or can we do parameterized query's to stored procedures and that would be okay?

正如我之前所说,应用程序必须尽可能地愚蠢并且永远不要 SQL 从客户端向任何类型的后端进行查询,否则您将在您的服务中打开一个巨大的安全漏洞。

任何从客户端到你后端的东西都必须被认为是潜在的恶意,因为即使你认为你知道 WHO 正在发出请求,你也可能不知道知道 WHAT 是什么。为了更好地理解 WHOWHAT 正在访问您的服务器之间的区别,请阅读您这张图片所在部分的 this article :

所以简而言之,WHO 表示用户,WHAT 表示 application/device 执行请求,作为图像表明它可以被中间人攻击的攻击者篡改。

I'm assuming this also needs to be behind a web service and just return something like a JSON array etc.

Json 是 API 对 return 响应最常用的格式,因为它很容易被人和机器阅读,但也存在其他格式。

I've not really seen any sort of checklist of what not to put behind a web service for a mobile app.

虽然您要求的列表可能不存在,但由于范围太广,您可以从避免在移动应用程序中使用 OWASP Mobile Security Project - Top 10 risks 开始。

I'm coming from the position of web development so this hasn't really been an issue until now.

虽然您可能没有在您的网络应用程序中使用 API 键,但如果您正在从它们进行 SQL 查询,那么您就有 SQL 注入的风险,最暴露在互联网上的任何应用程序中存在的共同风险,在 OWASP Top 10 - 2017(pdf) 中继续处于第一位。

Do we basically need to run our apps through a web-service

是的,您需要通过 API 服务器 运行 您的移动应用程序。

and if so how do we go about then incorporating the web service with the app since we can't hard-code a key for the web-service in the first place?

正如我在文章 How to Extract an API Key from a Mobile App by Static binary analysis it can be extracted with the help of several open source tools, like by using the Mobile Security Framework, but you can also grab the API key with a MitM attack, as I show in the article Steal that API Key with a Man in the Middle Attack, that uses the open source tool MiTM Proxy 中所展示的那样。

那么现在怎么办...如果我使用 API 密钥,它可以被提取,但如果我不使用密钥,我无法识别 WHAT 正在做什么请求。

您现在可以采用多层防御,从 reCaptcha V3, followed by Web Application Firewall(WAF) and finally if you can afford it a User Behavior Analytics(UBA) 解决方案开始。

Google reCAPTCHA V3:

reCAPTCHA is a free service that protects your website from spam and abuse. reCAPTCHA uses an advanced risk analysis engine and adaptive challenges to keep automated software from engaging in abusive activities on your site. It does this while letting your valid users pass through with ease.

...helps you detect abusive traffic on your website without any user friction. It returns a score based on the interactions with your website and provides you more flexibility to take appropriate actions.

WAF - Web Application Firewall:

A web application firewall (or WAF) filters, monitors, and blocks HTTP traffic to and from a web application. A WAF is differentiated from a regular firewall in that a WAF is able to filter the content of specific web applications while regular firewalls serve as a safety gate between servers. By inspecting HTTP traffic, it can prevent attacks stemming from web application security flaws, such as SQL injection, cross-site scripting (XSS), file inclusion, and security misconfigurations.

UBA - User Behavior Analytics:

User behavior analytics (UBA) as defined by Gartner is a cybersecurity process about detection of insider threats, targeted attacks, and financial fraud. UBA solutions look at patterns of human behavior, and then apply algorithms and statistical analysis to detect meaningful anomalies from those patterns—anomalies that indicate potential threats. Instead of tracking devices or security events, UBA tracks a system's users. Big data platforms like Apache Hadoop are increasing UBA functionality by allowing them to analyze petabytes worth of data to detect insider threats and advanced persistent threats.

所有这些解决方案都基于负面识别模型,换句话说,它们通过识别什么是坏的而不是什么是好的来尽力区分好坏,因此它们很容易出现误报,尽管他们中的一些人使用的先进技术,如机器学习和人工智能。

因此,您可能经常发现自己不得不放宽对 API 服务器的访问权限,以免影响好的用户。这也意味着此解决方案需要持续监控,以验证误报不会阻止您的合法用户,同时它们会适当地阻止未经授权的用户。

关于 API 为移动应用程序提供服务的积极识别模型可以通过使用移动应用程序证明解决方案来使用,该解决方案向 API 服务器保证请求可以被信任而没有虚假的可能性正面。

移动应用认证

使用移动应用证明解决方案使 API 服务器知道只接收来自正版移动应用的请求。

移动应用认证服务的作用是在 run-time 保证您的移动应用未被篡改或未通过 运行在已获得 root 权限的设备中 运行后台 SDK 将与云中 运行ning 服务通信,以证明移动应用程序和设备的完整性运行ning。

在成功证明移动应用程序完整性后,将发布一个短期 JWT 令牌并使用只有 API 服务器和云中的移动应用程序证明服务知道的秘密进行签名。如果移动应用证明失败,JWT 令牌将使用 API 服务器不知道的秘密进行签名。

现在,应用程序必须在每次 API 调用请求的 headers 中发送 JWT 令牌。这将允许 API 服务器仅在可以验证 JWT 令牌中的签名和过期时间时才为请求提供服务,并在验证失败时拒绝请求。

一旦移动应用程序不知道移动应用程序证明服务使用的秘密,即使应用程序被篡改,也无法在 run-time 对其进行逆向工程,运行宁有根设备r 通过成为中间人攻击目标的连接进行通信。

Approov(我在这里工作)的移动应用证明服务已经作为 SAAS 解决方案存在,它为多个平台提供 SDK,包括 iOS、Android、React Native 等。集成还需要在 API 服务器代码中进行少量检查,以验证云服务发布的 JWT 令牌。此检查对于 API 服务器能够决定处理哪些请求以及拒绝哪些请求是必要的。

正在回复您的摘要

Do we need to create an all-encompasing web-service for the purpose of exchanging between the database and the android app client, the same for any 3rd party API?

YES,并且您应该始终在任何类型的应用程序、移动设备、Web 或 IOT 应用程序中委托对第三方服务的访问权限,否则当攻击者获取凭据来访问它们并开始使用它们时,您将会遇到麻烦代表您,而您将是支付账单的人。

Also, how do we authorize an android app to interact with our web-service since the purpose of this web service was to prevent hard-coding keys in the first place?

移动应用认证解决方案看起来最适合这里。

总结

任何 运行 在客户端需要一些秘密才能访问 API 的东西都可能以不同的方式被滥用,您必须将访问权限委托给所有第三方 API s 到您控制的后端,这样您就可以减少攻击面,同时保护他们的秘密不被 public 窥探。

最后,为了保护您的 API 服务器而使用的解决方案必须根据您要保护的内容的价值和该类型数据的法律要求来选择,例如欧洲的 GDPR 法规。