保护 Web API 端点的最佳实践

Best practice for securing web API endpoint

这是一个关于保护 Web API 端点的最佳实践的问题。

比如说,我在一家允许用户使用移动应用程序的银行工作。我是为该银行开发移动应用程序的开发人员。该应用程序获取 OAuth 访问令牌并访问由银行托管的网络 API。该应用已发布。

在银行拥有有效账户的普通用户安装了移动应用程序。通过手机APP,用户可以查看余额、转账等

该用户有开发技能,注意到他在使用用户名和密码登录银行后可以获得访问令牌。

因为此用户拥有有效的银行帐户,所以访问令牌可有效调用 API 端点。用户在他的 Visual Studio 中反复试验以确定需要发送哪些请求才能从 API 获得有效响应。他可以根据需要使用官方移动应用程序手动多次刷新访问令牌,并最终找到一种方法来从他的开发工具中对 API 进行有效调用。

问题是,是否有任何机制可以用来防止用户在不通过官方移动应用程序的情况下调用端点?例如,web API 可以用 [RequiredScope] 属性标记,但是如果他能够登录,他是否应该拥有普通用户允许做的所有权限,例如转账?

我在网上搜索过这个主题,因为它似乎是一个常见的主题,但还没有找到参考资料。

在开发 UI 客户端时,您应该始终假设用户可以从移动应用程序获取令牌或从 Web 应用程序保护 cookie,然后针对 API 重放它们。两者都只是 HTTP headers。使用 HTTP 代理工具,任何 semi-technical 用户都可以轻松做到这一点。

范围

APIs 应该验证每个请求收到的 JWT 访问令牌。然后使用范围来防止访问无效操作。例如,如果使用没有 money_write 范围的访问令牌来尝试该操作,它将失败并出现 403 错误。

索赔

主要保护通常总是在 API 的逻辑验证 JWT 访问令牌中收到的声明时完成。这确保了用户永远无法提升自己的权限。考虑以下示例:

sub: hd80423r2f
tenant-id: 123
role: user
subscription-level: silver

如果 API 收到此消息,那么它通常会应用代码拒绝其他用户、角色、租户或订阅级别访问资源。在这些情况下,我最常 return 404 resource not found for user 响应。

高权限操作

汇款等操作通常伴随着具有多种因素的强身份验证和短期访问令牌。通常还涉及用户同意,也许是针对特定的支付交易。

有时您需要在 API 秒内进行动态授权行为,例如,只有在使用强身份验证时才允许转账。在这些情况下,使用声明而不是固定范围,这可能会导致 JWT 访问令牌中出现以下额外声明:

authentication_strength: high
payment_transaction_id: 123

API 的代码可能会拒绝访问,除非也存在诸如此类的运行时声明。

摘要

您不能基于移动应用的使用进行授权,因为 APIs 无法区分来自移动应用或手动发送的 HTTP 请求。要确保的主要事情是,如果用户使用工具获取令牌,他们只能访问他们在 UI 用户 session.

中看到的完全相同的数据

我的回答背景

The web API can be marked with [RequiredScope] attribute, for example, but if he was able to sign in, should he have all the permissions to do what the normal users are allowed to do, such as transferring money?

我不会尝试回答这个问题,因为已接受的答案已经做得很好。

我的回复将帮助您了解您的 API 如何能够对 正在做的事情有很高的信心请求,因此在您的其他问题的上下文中更多:

Question is, are there any mechanisms that can be utilized to prevent the user from calling the endpoint without going through the official mobile app?

访问 API 服务器的 WHO 和 WHAT 的区别

首先,让我们区分一下 在请求中与 什么 向你的 API 发出请求的区别, 在我围绕 API 和移动安全撰写的一系列文章中,我在文章 Why Does Your Mobile App Need An Api Key? 中对其进行了解释,您可以在其中阅读:

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 安全性,并能够看到您以前从未想过的攻击面。

捍卫 API

Question is, are there any mechanisms that can be utilized to prevent the user from calling the endpoint without going through the official mobile app?

现在您了解了 whowhat 之间的区别正在访问您的 API 服务器,您可能想使用一些可以帮助您区分 API.

处理的每个请求的机制

对于网络请求

您可以学习一些有用的技术来帮助您的 API 后端尝试仅响应来自 您期望的 的请求,您的正版网络应用程序,并这样做 我邀请您阅读 my answer 问题 保护 api 来自应用程序调用的数据 ,尤其是致力于捍卫 API 的部分] 服务器.

您还可以使用 fingerprintjs 库来帮助您更加确信 所做的请求确实是一个正版浏览器:

FingerprintJS is a browser fingerprinting library that queries browser attributes and computes a hashed visitor identifier from them. Unlike cookies and local storage, a fingerprint stays the same in incognito/private mode and even when browser data is purged.

对于移动应用程序

为了非常确定 正在执行的请求确实是您的真实且未修改的移动应用程序,我建议您阅读 我给的问题 如何保护移动应用程序的 API REST?,尤其是 加固和屏蔽移动应用程序保护 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.

对于网络应用程序

The Web Security Testing Guide:

The OWASP Web Security Testing Guide includes a "best practice" penetration testing framework which users can implement in their own organizations and a "low level" penetration testing guide that describes techniques for testing most common web application and web service security issues.