如何保护一个简单的 Node.js RESTful API
How to secure a simple Node.js RESTful API
我正在开发一个简单的新闻应用程序,用户无需登录即可查看最新消息,我不知道如何确保它的安全,我读过应该使用带有 OAuth2 的 JWT,但据我了解,用户有登录提供商以获取令牌,但我希望用户无需登录即可读取数据。
我现在关心的是如何防止其他人在他们的应用程序中使用我的 API,我也担心 DoS 攻击。
挑战
i'm developing a simple news App where users can see latest news without login, i don't know how to make it secure
好吧,你给自己带来了一个非常巨大的挑战,因为即使你要求你的用户登录,这仍然是一个巨大的挑战,因为我们所知道的网络在他们早期的设计中天真地假设只有善意的人才会使用它,因此要确保最初并非设计为安全的东西成为一项非常艰巨的任务。
将 API 服务器保护和锁定到特定应用程序需要在每个请求中使用某种秘密,以便验证 正在做什么请求,但是一旦你将网络应用程序或移动应用程序发布到 public 中,它的任何秘密现在都在 public 域中,因此不再是秘密,因为你最好将它用作弱标识符。
网络应用程序
网络应用程序在浏览器中运行,因此攻击者需要做的就是检查页面源并提取秘密,然后将其用于自动化脚本或来自 curl
或工具的手动请求像邮递员。
移动应用程序
一些开发人员认为,因为移动应用程序是作为二进制文件发布的,所以他们可以将秘密放在那里,没有人会找到它......好吧,我不得不说,过去我就是其中之一,但后来我开始了解到存在许多开源工具可以使这项任务变得微不足道,即使是脚本小子也可以完成。我首选的工具是 MobSF, and you can see an example of using it in this article I wrote How to Extract an API Key from a Mobile App with Static Binary Analysis:
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 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 - Mobile Security Framework:
Mobile Security Framework is an automated, all-in-one mobile application (Android/iOS/Windows) pen-testing framework capable of performing static analysis, dynamic analysis, malware analysis and web API testing.
重要的是,一旦应用发布,其中包含的任何敏感信息都必须被视为已泄露。
JWT 和 OAUTH2
i have read that JWT with OAuth2 should be used but from my understanding the user have to login into a provider to get the token
是的,用户需要登录才能获得 OAuth 令牌,但是来自用户成功登录的任何类型的令牌仅标识请求中的谁,不是 What 正在执行请求,根据我的经验,这是开发人员中非常普遍的误解,无论他们是初级开发人员还是高级开发人员。
访问 API 服务器的 WHO 和 WHAT 之间的区别
我写了一系列关于移动API安全的文章,在文章Why Does Your Mobile App Need An Api Key?中你可以详细阅读Who和什么 正在向您的 API 服务器发出请求:
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.
您可能会感到惊讶,有时甚至您的合法用户也可能会攻击您的 API 服务器,以便尝试从您的服务中获益,否则他们将无法访问。
保护和锁定 API 服务器到应用程序
but i want users to be able to read data without login.
my concern right now is how to prevent other people from using my API in their apps
要在您的应用中实现此要求,您需要找到保护和锁定您的 API 服务器的方法,以仅处理来自您的 Web 和移动应用的真实实例的请求。
我不得不说,对于移动应用程序,当采用移动应用程序证明概念时,可以非常自信地实现这一点,但对于 Web 应用程序,我们只能尽最大努力通过使用人工智能解决方案。
要了解如何为 Web 应用程序执行此操作,请阅读 this answer 我提出的问题 保护 api 来自应用程序调用的数据,特别是标题为 Defending The API Server.
的部分
对于移动应用程序,您应该阅读 this answer 我提出的问题 如何保护移动应用程序的 API REST?,更具体地说, 保护API服务器部分和可能更好的解决方案.
部分
如果您已经阅读了我上面链接的两个答案,您现在可能会更好地理解为什么我最初说这是一个 巨大的挑战。
DoS 攻击
u'm concerned also about DoS attacks.
这通常不会在应用程序级别解决,最好在基础架构级别处理,例如在防火墙中,可以应用规则来识别和丢弃 DoS 数据包,但如果它们太严格,它们可能也会阻止合法用户,而如果他们过于放松,他们会让 DoS 数据包通过。要获得更有效的 DoS 攻击保护,您需要求助于专用软件,即使是这种软件也可能需要在大规模 DoS 攻击期间进行人工干预。
所以我的建议是,首先向防火墙添加一些规则以丢弃 DoS 数据包,and/or 如果您使用的是云提供商,您可能想看看他们可以提供什么级别的 DoS 保护在您的帐户中。例如,我知道一些 CDN 提供商在一定程度上免费提供 DoS 保护,因此您可能值得考虑让所有流量都通过 CDN 路由,这也带来了 CDN 的其他传统优势,也就是从最近的服务资产定位到您的客户。
您想加倍努力吗?
在任何对安全问题的回答中,我都会参考 OWASP 基金会的优秀作品。
对于网络应用程序
The OWASP Top 10 is a powerful awareness document for web application security. It represents a broad consensus about the most critical security risks to web applications. Project members include a variety of security experts from around the world who have shared their expertise to produce this list.
[=55=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.
对于移动应用程序
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.
对于APIS
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.
我正在开发一个简单的新闻应用程序,用户无需登录即可查看最新消息,我不知道如何确保它的安全,我读过应该使用带有 OAuth2 的 JWT,但据我了解,用户有登录提供商以获取令牌,但我希望用户无需登录即可读取数据。 我现在关心的是如何防止其他人在他们的应用程序中使用我的 API,我也担心 DoS 攻击。
挑战
i'm developing a simple news App where users can see latest news without login, i don't know how to make it secure
好吧,你给自己带来了一个非常巨大的挑战,因为即使你要求你的用户登录,这仍然是一个巨大的挑战,因为我们所知道的网络在他们早期的设计中天真地假设只有善意的人才会使用它,因此要确保最初并非设计为安全的东西成为一项非常艰巨的任务。
将 API 服务器保护和锁定到特定应用程序需要在每个请求中使用某种秘密,以便验证 正在做什么请求,但是一旦你将网络应用程序或移动应用程序发布到 public 中,它的任何秘密现在都在 public 域中,因此不再是秘密,因为你最好将它用作弱标识符。
网络应用程序
网络应用程序在浏览器中运行,因此攻击者需要做的就是检查页面源并提取秘密,然后将其用于自动化脚本或来自 curl
或工具的手动请求像邮递员。
移动应用程序
一些开发人员认为,因为移动应用程序是作为二进制文件发布的,所以他们可以将秘密放在那里,没有人会找到它......好吧,我不得不说,过去我就是其中之一,但后来我开始了解到存在许多开源工具可以使这项任务变得微不足道,即使是脚本小子也可以完成。我首选的工具是 MobSF, and you can see an example of using it in this article I wrote How to Extract an API Key from a Mobile App with Static Binary Analysis:
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 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 - Mobile Security Framework:
Mobile Security Framework is an automated, all-in-one mobile application (Android/iOS/Windows) pen-testing framework capable of performing static analysis, dynamic analysis, malware analysis and web API testing.
重要的是,一旦应用发布,其中包含的任何敏感信息都必须被视为已泄露。
JWT 和 OAUTH2
i have read that JWT with OAuth2 should be used but from my understanding the user have to login into a provider to get the token
是的,用户需要登录才能获得 OAuth 令牌,但是来自用户成功登录的任何类型的令牌仅标识请求中的谁,不是 What 正在执行请求,根据我的经验,这是开发人员中非常普遍的误解,无论他们是初级开发人员还是高级开发人员。
访问 API 服务器的 WHO 和 WHAT 之间的区别
我写了一系列关于移动API安全的文章,在文章Why Does Your Mobile App Need An Api Key?中你可以详细阅读Who和什么 正在向您的 API 服务器发出请求:
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.
您可能会感到惊讶,有时甚至您的合法用户也可能会攻击您的 API 服务器,以便尝试从您的服务中获益,否则他们将无法访问。
保护和锁定 API 服务器到应用程序
but i want users to be able to read data without login. my concern right now is how to prevent other people from using my API in their apps
要在您的应用中实现此要求,您需要找到保护和锁定您的 API 服务器的方法,以仅处理来自您的 Web 和移动应用的真实实例的请求。
我不得不说,对于移动应用程序,当采用移动应用程序证明概念时,可以非常自信地实现这一点,但对于 Web 应用程序,我们只能尽最大努力通过使用人工智能解决方案。
要了解如何为 Web 应用程序执行此操作,请阅读 this answer 我提出的问题 保护 api 来自应用程序调用的数据,特别是标题为 Defending The API Server.
的部分对于移动应用程序,您应该阅读 this answer 我提出的问题 如何保护移动应用程序的 API REST?,更具体地说, 保护API服务器部分和可能更好的解决方案.
部分如果您已经阅读了我上面链接的两个答案,您现在可能会更好地理解为什么我最初说这是一个 巨大的挑战。
DoS 攻击
u'm concerned also about DoS attacks.
这通常不会在应用程序级别解决,最好在基础架构级别处理,例如在防火墙中,可以应用规则来识别和丢弃 DoS 数据包,但如果它们太严格,它们可能也会阻止合法用户,而如果他们过于放松,他们会让 DoS 数据包通过。要获得更有效的 DoS 攻击保护,您需要求助于专用软件,即使是这种软件也可能需要在大规模 DoS 攻击期间进行人工干预。
所以我的建议是,首先向防火墙添加一些规则以丢弃 DoS 数据包,and/or 如果您使用的是云提供商,您可能想看看他们可以提供什么级别的 DoS 保护在您的帐户中。例如,我知道一些 CDN 提供商在一定程度上免费提供 DoS 保护,因此您可能值得考虑让所有流量都通过 CDN 路由,这也带来了 CDN 的其他传统优势,也就是从最近的服务资产定位到您的客户。
您想加倍努力吗?
在任何对安全问题的回答中,我都会参考 OWASP 基金会的优秀作品。
对于网络应用程序
[=55=The Web Security Testing Guide:The OWASP Top 10 is a powerful awareness document for web application security. It represents a broad consensus about the most critical security risks to web applications. Project members include a variety of security experts from around the world who have shared their expertise to produce this list.
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.
对于移动应用程序
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.
对于APIS
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.