简体   繁体   English

无需用户注册的安全 API - php/Laravel

[英]Secure API without a user registration - php/Laravel

I have an API in Laravel with mostly GET endpoints and an android application.我在 Laravel 中有一个 API,主要是 GET 端点和一个 android 应用程序。

The application is meant to be open without the need to authenticate, ie like booking.com where you can browse hotels without the need to login or register.该应用程序旨在无需身份验证即可打开,例如,您可以在无需登录或注册的情况下浏览酒店的 Booking.com。

Anyone can hit my endpoints and get raw JSON data or even make an app that utilize my endpoints in their own app.任何人都可以访问我的端点并获取原始 JSON 数据,甚至可以制作一个在他们自己的应用程序中利用我的端点的应用程序。

How can I secure my endpoint?如何保护我的端点? For example with a token based or any other signature to trust my client app only.例如,使用基于令牌或任何其他签名来仅信任我的客户端应用程序。

actually I copy pasted this question from stack exchange, but this is exact my question实际上我从堆栈交换复制粘贴了这个问题,但这正是我的问题

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

The application is meant to be open without the need to authenticate, ie like booking.com where you can browse hotels without the need to login or register.该应用程序旨在无需身份验证即可打开,例如,您可以在无需登录或注册的情况下浏览酒店的 Booking.com。

Bear in mind that even when user authentication is used the API is still vulnerable to be used from other scripts, apps, botnets, etc., provided that they have a user authentication token, and how this can be done this is out of scope for this answer.请记住,即使使用用户身份验证,API 仍然容易被其他脚本、应用程序、僵尸网络等使用,前提是它们具有用户身份验证令牌,并且如何做到这一点超出了这个答案。 User authentication only serves to identify who is in a request, not what is making it, therefore, even if your mobile app had user authentication, the API backend would not be locked down to the genuine and untampered versions of your mobile app.用户认证只用于识别是在请求,没有什么使得它,因此,即使您的移动应用有用户认证,API的后端不会被锁定到您的移动应用的真正和篡改版本。

The difference between who and what is accessing an API backend is a usual misconception among developers of any seniority, therefore don't feel "guilty" if you don't get it yet ;)什么是访问的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登录连接或流的oauth2的用户。

Think about the who as the user your API server will be able to Authenticate and Authorize access to the data, and think about the what as the software making that request in behalf of the user.考虑是您的 API 服务器将能够对数据进行身份验证和授权访问的用户,并考虑代表用户发出该请求的软件是什么

So, once you don't have user authentication in your mobile app you cannot authenticate and authorize who is in the request, therefore you can only authorize what is doing the request to your API backend.所以,一旦你没有在您的移动应用用户的身份验证您不能身份验证和授权的要求,所以你只能授权是什么做请求您的API后端。

Lockdown the API to the Android App将 API 锁定到 Android 应用程序

How can I secure my endpoint?如何保护我的端点? For example with a token based or any other signature to trust my client app only.例如,使用基于令牌或任何其他签名来仅信任我的客户端应用程序。

This is a very hard task to achieve, but not an impossible one, and I recommend you to read this answer I gave to the question How to secure an API REST for mobile app?这是一个非常艰巨的任务来实现,但并非不可能一个,我建议你阅读这个答案我给这个问题如何保证一个REST API移动应用? , especially the sections Securing the API Server and A Possible Better Solution , that will let you know about some basic and advanced techniques or link to resources to learn about them, like for example: ,尤其是Securing the API ServerA possible Better Solution 部分,它将让您了解一些基本和高级技术或资源链接以了解它们,例如:

  • Certificate Pinning证书固定
  • HMAC HMAC
  • reCAPTCHA V3验证码 V3
  • WAF's WAF的
  • UBA UBA
  • Mobile app hardening and shielding移动应用加固和屏蔽
  • Mobile App Attestation移动应用认证

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

You could use the OAuth 2.0 client credentials grant, which is suitable for machine-to-machine authentication.您可以使用适用于机器对机器身份验证的 OAuth 2.0 客户端凭据授权。

This would mean that your Android app (or any other first-party app) can use a client ID and secret to generate an access token to authenticate against your API, and your API can be locked down to only return responses to requests that contain a valid Bearer token (ie no longer public).这意味着您的 Android 应用程序(或任何其他第一方应用程序)可以使用客户端 ID 和密钥生成访问令牌以针对您的 API 进行身份验证,并且您的 API 可以被锁定以仅返回对包含有效的不记名令牌(即不再公开)。

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

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