与在 Android 上提供 API 的 nodejs 服务器建立安全连接
Make secure connection with a nodejs server providing API on Android
我有一个存储私人信息的网站,可以通过请求使用秘密api密钥访问。
我的 Android 应用程序必须访问该私人信息,为此它使用存储和使用秘密的代理服务器 api键与网站进行通信
问题是 使用 Wireshark,或者在应用程序资源文件中找到字符串,有人可以看到代理服务器 url 并使用它来获取来自网站的私人数据
我怎样才能使这个系统安全?我如何确定 none else 可以使用除 Android 应用程序以外的代理?
谢谢!
I have a website that store private information that can be accessed by request with a secret api key.
从你把秘密放在客户端的那一刻起就不再是秘密了,因为现在 public 任何人都可以通过对应用程序进行反向工程或通过代理拦截流量或仅通过使用您提到的工具 Wireshark 观察流量。
My Android application have to access that private information, and to do that it use a proxy server that store and use the secret api key to communicate with the website
使用代理服务器并不能解决问题,因为代理服务器不知道什么在调用它。正如您已经知道并指出的那样,代理服务器对知道如何调用它的 public 开放:
The problem is that just using Wireshark, or finding the string in the app resources file, someone can see the proxy server url and use it to get the private data from the website.
另一种方法是使用像 MiTM Proxy that in my opinion will allow to extract more easily the API key, as you can see in the article Steal that API Key with a Man in the Middle Attack:
这样的代理工具
While we can use advanced techniques, like JNI/NDK, to hide the API key in the mobile app code, it will not impede someone from performing a MitM attack in order to steal the API key. In fact a MitM attack is easy to the point that it can even be achieved by non developers.
解决您的问题
How can i make this system secure? how can i be sure that none else can use the proxy except the Android app?
好吧,你给自己买了一个很难解决的问题,很多人会说这只会让攻击者更难解决,在某种程度上这是真的,因为很多开发人员都不知道移动应用证明概念,它允许 back-end 服务器仅服务器来自原始应用的请求。
在我向您解释这个概念之前,我想澄清开发人员之间关于 WHO 与 WHAT 正在访问您的一个常见误解back-end 服务器。
访问 API 服务器的 WHO 和 WHAT 的区别
为了更好地理解 WHO 和 WHAT 访问 API 服务器的区别,让我们使用这张图片:
预期通信渠道表示移动应用正按您的预期使用,由合法用户无恶意使用,使用移动应用的未篡改版本,并直接与 API 服务器通信中间的人被攻击了。
实际的渠道可能代表几种不同的场景,比如有恶意的合法用户可能正在使用重新打包的移动应用程序版本,黑客使用正版移动应用程序,而中间人攻击它,了解移动应用程序和 API 服务器之间的通信是如何进行的,以便能够自动攻击您的 API。还有很多其他可能的情况,但我们不会在这里一一列举。
我希望现在您可能已经知道为什么 WHO 和 WHAT 不一样,但如果不一样一会儿就清楚了。
WHO 是移动应用程序的用户,我们可以通过多种方式对其进行身份验证、授权和识别,例如使用 OpenID Connect 或 OAUTH2 流程。
Generally, OAuth provides to clients a "secure delegated access" to server resources on behalf of a resource owner. It specifies a process for resource owners to authorize third-party access to their server resources without sharing their credentials. Designed specifically to work with Hypertext Transfer Protocol (HTTP), OAuth essentially allows access tokens to be issued to third-party clients by an authorization server, with the approval of the resource owner. The third party then uses the access token to access the protected resources hosted by the resource server.
OpenID Connect 1.0 is a simple identity layer on top of the OAuth 2.0 protocol. It allows Clients to verify the identity of the End-User based on the authentication performed by an Authorization Server, as well as to obtain basic profile information about the End-User in an interoperable and REST-like manner.
虽然用户身份验证可以让 API 服务器知道 WHO 在使用 API,但不能保证请求来自 如你所愿,移动应用的原版。
现在我们需要一种方法来识别什么 正在调用API 服务器,这里事情变得比大多数开发人员想象的更棘手。 WHAT 是向 API 服务器发出请求的东西。它真的是移动应用程序的真实实例,还是机器人、自动脚本或攻击者使用 Postman 等工具手动浏览 API 服务器?
令您惊讶的是,您最终可能会发现它可能是使用重新打包版本的移动应用程序或试图游戏化并利用应用程序提供的服务的自动化脚本的合法用户之一。
好吧,为了识别 WHAT,开发人员倾向于求助于 API 键,通常他们 hard-code 在他们的移动应用程序代码中。一些开发人员更加努力地在移动应用程序中计算 run-time 处的密钥,因此它成为一个运行时秘密,而不是前一种方法,即在代码中嵌入静态秘密。
以上write-up摘自我写的一篇文章,标题为WHY DOES YOUR MOBILE APP NEED AN API KEY?,你可以完整阅读 here,这是关于 API 键的系列文章中的第一篇。
第一题
How can i make this system secure?
根据您的预算和资源,您可以采用一系列不同的方法和技术来保护您的 API 服务器,我将开始列举一些最常用的方法和技术,但在我这样做之前我想留下这张纸条:
As a best practice a mobile app or a web app should only communicate with an API server that is under your control and any access to third party APIs services must be done by this same API server you control. This way you limit the attack surface to only one place, where you will employ as many layers of defense as what you are protecting is worth.
您可以从 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 服务器保证请求可以被信任而没有虚假的可能性积极的,我会解释它作为对你第二个问题的答复。
第二题
how can i be sure that none else can use the proxy except the Android app?
正如我在回答开头提到的,移动应用证明概念可能是解决问题的最佳选择。
移动应用证明解决方案的作用是在 run-time 保证您的移动应用未被篡改,未 运行 在已获得 root 权限的设备中,未被类似框架检测xPosed 或 Frida,不被 MitM 攻击,这是通过后台的 运行 SDK 实现的。云中的服务 运行 将挑战应用程序,并根据响应证明移动应用程序和设备的完整性 运行,因此 SDK 永远不会对任何决定负责。
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.
Xposed is a framework for modules that can change the behavior of the system and apps without touching any APKs. That's great because it means that modules can work for different versions and even ROMs without any changes (as long as the original code was not changed too much). It's also easy to undo.
An interactive TLS-capable intercepting HTTP proxy for penetration testers and software developers.
在成功证明移动应用程序完整性后,将发布一个短期 JWT 令牌并使用只有 API 服务器和云中的移动应用程序证明服务知道的秘密进行签名。如果移动应用证明失败,JWT 令牌将使用 API 服务器不知道的秘密进行签名。
现在,应用程序必须在每次 API 请求的 headers 调用中发送 JWT 令牌。这将允许 API 服务器仅在可以验证 JWT 令牌中的签名和过期时间时才为请求提供服务,并在验证失败时拒绝它们。
一旦移动应用程序不知道移动应用程序证明服务使用的秘密,即使应用程序被篡改,也无法在 run-time 对其进行逆向工程,运行 root 设备或通过成为中间人攻击目标的连接进行通信。
Approov(我在这里工作)的移动应用证明服务已经作为 SAAS 解决方案存在,它为多个平台提供 SDK,包括 iOS、Android、React Native 等。集成还需要在 API 服务器代码中进行少量检查,以验证云服务发布的 JWT 令牌。此检查对于 API 服务器来说是必要的,以便能够决定处理哪些请求以及拒绝哪些请求。
结论
最后,为了保护您的 API 服务器而使用的解决方案必须根据您要保护的内容的价值和该类型数据的法律要求来选择,例如欧洲的 GDPR 法规。
您想加倍努力吗?
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.
我有一个存储私人信息的网站,可以通过请求使用秘密api密钥访问。
我的 Android 应用程序必须访问该私人信息,为此它使用存储和使用秘密的代理服务器 api键与网站进行通信
问题是 使用 Wireshark,或者在应用程序资源文件中找到字符串,有人可以看到代理服务器 url 并使用它来获取来自网站的私人数据
我怎样才能使这个系统安全?我如何确定 none else 可以使用除 Android 应用程序以外的代理?
谢谢!
I have a website that store private information that can be accessed by request with a secret api key.
从你把秘密放在客户端的那一刻起就不再是秘密了,因为现在 public 任何人都可以通过对应用程序进行反向工程或通过代理拦截流量或仅通过使用您提到的工具 Wireshark 观察流量。
My Android application have to access that private information, and to do that it use a proxy server that store and use the secret api key to communicate with the website
使用代理服务器并不能解决问题,因为代理服务器不知道什么在调用它。正如您已经知道并指出的那样,代理服务器对知道如何调用它的 public 开放:
The problem is that just using Wireshark, or finding the string in the app resources file, someone can see the proxy server url and use it to get the private data from the website.
另一种方法是使用像 MiTM Proxy that in my opinion will allow to extract more easily the API key, as you can see in the article Steal that API Key with a Man in the Middle Attack:
这样的代理工具While we can use advanced techniques, like JNI/NDK, to hide the API key in the mobile app code, it will not impede someone from performing a MitM attack in order to steal the API key. In fact a MitM attack is easy to the point that it can even be achieved by non developers.
解决您的问题
How can i make this system secure? how can i be sure that none else can use the proxy except the Android app?
好吧,你给自己买了一个很难解决的问题,很多人会说这只会让攻击者更难解决,在某种程度上这是真的,因为很多开发人员都不知道移动应用证明概念,它允许 back-end 服务器仅服务器来自原始应用的请求。
在我向您解释这个概念之前,我想澄清开发人员之间关于 WHO 与 WHAT 正在访问您的一个常见误解back-end 服务器。
访问 API 服务器的 WHO 和 WHAT 的区别
为了更好地理解 WHO 和 WHAT 访问 API 服务器的区别,让我们使用这张图片:
预期通信渠道表示移动应用正按您的预期使用,由合法用户无恶意使用,使用移动应用的未篡改版本,并直接与 API 服务器通信中间的人被攻击了。
实际的渠道可能代表几种不同的场景,比如有恶意的合法用户可能正在使用重新打包的移动应用程序版本,黑客使用正版移动应用程序,而中间人攻击它,了解移动应用程序和 API 服务器之间的通信是如何进行的,以便能够自动攻击您的 API。还有很多其他可能的情况,但我们不会在这里一一列举。
我希望现在您可能已经知道为什么 WHO 和 WHAT 不一样,但如果不一样一会儿就清楚了。
WHO 是移动应用程序的用户,我们可以通过多种方式对其进行身份验证、授权和识别,例如使用 OpenID Connect 或 OAUTH2 流程。
Generally, OAuth provides to clients a "secure delegated access" to server resources on behalf of a resource owner. It specifies a process for resource owners to authorize third-party access to their server resources without sharing their credentials. Designed specifically to work with Hypertext Transfer Protocol (HTTP), OAuth essentially allows access tokens to be issued to third-party clients by an authorization server, with the approval of the resource owner. The third party then uses the access token to access the protected resources hosted by the resource server.
OpenID Connect 1.0 is a simple identity layer on top of the OAuth 2.0 protocol. It allows Clients to verify the identity of the End-User based on the authentication performed by an Authorization Server, as well as to obtain basic profile information about the End-User in an interoperable and REST-like manner.
虽然用户身份验证可以让 API 服务器知道 WHO 在使用 API,但不能保证请求来自 如你所愿,移动应用的原版。
现在我们需要一种方法来识别什么 正在调用API 服务器,这里事情变得比大多数开发人员想象的更棘手。 WHAT 是向 API 服务器发出请求的东西。它真的是移动应用程序的真实实例,还是机器人、自动脚本或攻击者使用 Postman 等工具手动浏览 API 服务器?
令您惊讶的是,您最终可能会发现它可能是使用重新打包版本的移动应用程序或试图游戏化并利用应用程序提供的服务的自动化脚本的合法用户之一。
好吧,为了识别 WHAT,开发人员倾向于求助于 API 键,通常他们 hard-code 在他们的移动应用程序代码中。一些开发人员更加努力地在移动应用程序中计算 run-time 处的密钥,因此它成为一个运行时秘密,而不是前一种方法,即在代码中嵌入静态秘密。
以上write-up摘自我写的一篇文章,标题为WHY DOES YOUR MOBILE APP NEED AN API KEY?,你可以完整阅读 here,这是关于 API 键的系列文章中的第一篇。
第一题
How can i make this system secure?
根据您的预算和资源,您可以采用一系列不同的方法和技术来保护您的 API 服务器,我将开始列举一些最常用的方法和技术,但在我这样做之前我想留下这张纸条:
As a best practice a mobile app or a web app should only communicate with an API server that is under your control and any access to third party APIs services must be done by this same API server you control. This way you limit the attack surface to only one place, where you will employ as many layers of defense as what you are protecting is worth.
您可以从 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 服务器保证请求可以被信任而没有虚假的可能性积极的,我会解释它作为对你第二个问题的答复。
第二题
how can i be sure that none else can use the proxy except the Android app?
正如我在回答开头提到的,移动应用证明概念可能是解决问题的最佳选择。
移动应用证明解决方案的作用是在 run-time 保证您的移动应用未被篡改,未 运行 在已获得 root 权限的设备中,未被类似框架检测xPosed 或 Frida,不被 MitM 攻击,这是通过后台的 运行 SDK 实现的。云中的服务 运行 将挑战应用程序,并根据响应证明移动应用程序和设备的完整性 运行,因此 SDK 永远不会对任何决定负责。
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.
Xposed is a framework for modules that can change the behavior of the system and apps without touching any APKs. That's great because it means that modules can work for different versions and even ROMs without any changes (as long as the original code was not changed too much). It's also easy to undo.
An interactive TLS-capable intercepting HTTP proxy for penetration testers and software developers.
在成功证明移动应用程序完整性后,将发布一个短期 JWT 令牌并使用只有 API 服务器和云中的移动应用程序证明服务知道的秘密进行签名。如果移动应用证明失败,JWT 令牌将使用 API 服务器不知道的秘密进行签名。
现在,应用程序必须在每次 API 请求的 headers 调用中发送 JWT 令牌。这将允许 API 服务器仅在可以验证 JWT 令牌中的签名和过期时间时才为请求提供服务,并在验证失败时拒绝它们。
一旦移动应用程序不知道移动应用程序证明服务使用的秘密,即使应用程序被篡改,也无法在 run-time 对其进行逆向工程,运行 root 设备或通过成为中间人攻击目标的连接进行通信。
Approov(我在这里工作)的移动应用证明服务已经作为 SAAS 解决方案存在,它为多个平台提供 SDK,包括 iOS、Android、React Native 等。集成还需要在 API 服务器代码中进行少量检查,以验证云服务发布的 JWT 令牌。此检查对于 API 服务器来说是必要的,以便能够决定处理哪些请求以及拒绝哪些请求。
结论
最后,为了保护您的 API 服务器而使用的解决方案必须根据您要保护的内容的价值和该类型数据的法律要求来选择,例如欧洲的 GDPR 法规。
您想加倍努力吗?
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.