

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 Transfer Family、Amazon Cognito 和 GuardDuty
<a name="secure-file-transfers"></a>

*Manoj Kumar，Amazon Web Services*

## Summary
<a name="secure-file-transfers-summary"></a>

此解决方案可帮助您使用 AWS Transfer Family安全地通过 SFTP 服务器传输文件。它包括通过 [Amazon 的一项功能 S3 恶意软件防护](https://docs.aws.amazon.com/guardduty/latest/ug/gdu-malware-protection-s3.html)提供的自动恶意软件扫描功能 GuardDuty。它专为需要与外部各方安全地交换文件且要求所有传入文件在处理前均经过恶意软件扫描验证的组织而设计。

此模式提供的基础设施即代码（IaC）模板可帮助您部署以下资源：
+ 通过亚马逊 Cognito 身份验证的安全 SFTP 服务器 AWS Lambda
+ 用于存储经过恶意软件扫描的上传和传入文件的 Amazon Simple Storage Service（Amazon S3）存储桶
+ 跨多个可用区配置公有子网和私有子网的基于虚拟私有云（VPC）的架构
+ 基于 IP 的入口和出口流量访问控制，具有可配置的允许和拒绝列表
+ 通过自动扫描恶意软件 GuardDuty
+ 根据扫描结果通过 Amazon EventBridge 和 Lambda 进行智能文件路由
+ 通过 Amazon Simple Notification Service（Amazon SNS）实现的安全事件实时通知
+ 通过 () 对 Amazon S3 存储桶和 Lambda 环境变量 AWS Key Management Service 进行加密AWS KMS
+ 无需互联网暴露即可访问的 Amazon Virtual Private Cloud（Amazon VPC）端点
+ 通过 Amazon CloudWatch 集成进行全面登录

## 先决条件和限制
<a name="secure-file-transfers-prereqs"></a>

**先决条件**
+ 活跃的 AWS 账户
+  AWS Identity and Access Management (IAM) 中执行此模式中描述的操作的权限，包括部署预置 IAM 角色的 AWS CloudFormation 模板
+ GuardDuty，[已在目标账户中启用](https://docs.aws.amazon.com/guardduty/latest/ug/guardduty_settingup.html)
+ 在目标账户中[已启用](https://docs.aws.amazon.com/guardduty/latest/ug/malware-protection-s3-get-started-independent.html)的 S3 恶意软件防护
+ 服务配额允许您在目标账户中创建以下资源：
  + 一个 VPC
  + 一个私有子网
  + 一个公有子网
  + 三个弹性 IP 地址
  + 足够的 Lambda 并发限制
+ 用于接收安全相关通知的有效电子邮件地址
+ （可选）您希望允许或拒绝的 IP 地址或 CIDR 范围的列表
+ （可选） AWS Command Line Interface （AWS CLI），[已安装](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html)并[配置](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)

**限制**
+ S3 恶意软件防护受配额限制，例如最大文件大小。有关更多信息，请参阅 GuardDuty 文档[中的 S3 恶意软件防护配额](https://docs.aws.amazon.com/guardduty/latest/ug/malware-protection-s3-quotas-guardduty.html)和 [Amazon S3 功能的支持性](https://docs.aws.amazon.com/guardduty/latest/ug/supported-s3-features-malware-protection-s3.html)。
+ 此解决方案仅使用 Amazon Cognito 用户名和密码身份验证。此模板不支持基于证书的身份验证方法或其他身份验证方法。默认情况下，此解决方案不配置多重身份验证（MFA）。
+ 该解决方案仅通过安全组实现基于 IP 的访问控制。

## 架构
<a name="secure-file-transfers-architecture"></a>

以下架构图显示在此模式中部署的资源。此解决方案使用 Amazon Cognito 进行用户身份验证和授权。 AWS Transfer Family SFTP 服务器用于文件上传。文件存储在 Amazon S3 存储桶中，亚马逊 GuardDuty 会扫描文件中是否存在恶意软件。如果检测到恶意软件，Amazon SNS 会发送电子邮件通知。

![使用 GuardDuty 和 Cognito 将文件安全地传输到 Amazon S3 存储桶。](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/39d98ebe-2844-4ccd-a497-9b796b7da5e8/images/05567010-e189-40e7-acab-74e77c4f8525.png)


下图显示了如下工作流：

1. 用户连接到中的 SFTP 服务器端点。 AWS Transfer Family此操作将启动使用 Amazon Cognito 用户池进行的身份验证流程。

1. Lambda 函数启动身份验证和授权流程，并通过 Amazon Cognito 验证用户的凭证。

1. Lambda 函数返回 `UploadBucket` Amazon S3 存储桶作为主目录。用户担任 Transfer Family 服务器的 IAM 角色，Lambda 函数会通知用户他们已成功通过身份验证。

1. 用户将文件上传到 Transfer Family SFTP 服务器。该文件存储在 `UploadBucket` Amazon S3 存储桶中。

1. GuardDuty 扫描文件中是否有恶意软件。可能的扫描结果包括 `NO_THREATS_FOUND`、`THREATS_FOUND`、`UNSUPPORTED`、`ACCESS_DENIED` 和 `FAILED`。有关示例结果，请参阅 GuardDuty 文档[中的 S3 对象扫描结果](https://docs.aws.amazon.com/guardduty/latest/ug/monitor-with-eventbridge-s3-malware-protection.html#s3-object-scan-status-malware-protection-s3-ev)。

1.  EventBridge 规则检测扫描结果事件。

1. EventBridge 启动文件路由 Lambda 函数。

1. Lambda 函数处理该事件并根据扫描结果筛选文件，如下所示：
   + 包含 `NO_THREATS_FOUND` 扫描结果的文件发送至 `CleanBucket` Amazon S3 存储桶。
   + 包含 `THREATS_FOUND` 扫描结果的文件发送至 `MalwareBucket` Amazon S3 存储桶。
   + 包含 `UNSUPPORTED` 扫描结果的文件发送至 `ErrorBucket` Amazon S3 存储桶。
   + 包含 `ACCESS_DENIED` 扫描结果的文件发送至 `ErrorBucket` Amazon S3 存储桶。
   + 包含 `FAILED` 扫描结果的文件发送至 `ErrorBucket` Amazon S3 存储桶。

   所有文件都使用加密 AWS KMS key。

1. 如有文件发送至 `MalwareBucket` Amazon S3 存储桶，则 Lambda 函数会启动 Amazon SNS 主题。Amazon SNS 主题将向您配置的电子邮件地址发送电子邮件通知。

## 工具
<a name="secure-file-transfers-tools"></a>

**AWS 服务**
+ [Amazon CloudWatch](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/WhatIsCloudWatch.html) 可帮助您实时监控您的 AWS 资源和运行的应用程序 AWS 的指标。
+ [Amazon Cognito](https://docs.aws.amazon.com/cognito/latest/developerguide/what-is-amazon-cognito.html) 为您的 Web 和移动应用程序提供身份验证、授权和用户管理。
+ [Amazon EventBridge](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-what-is.html) 是一项无服务器事件总线服务，可帮助您将应用程序与来自各种来源的实时数据连接起来。例如， AWS Lambda 函数、使用 API 目的地的 HTTP 调用端点或其他 AWS 账户目的地的事件总线。
+ [Amazon GuardDuty](https://docs.aws.amazon.com/guardduty/latest/ug/what-is-guardduty.html) 是一项持续的安全监控服务，可分析和处理日志，以识别您的 AWS 环境中意外和可能未经授权的活动。
+ [AWS Key Management Service (AWS KMS)](https://docs.aws.amazon.com/kms/latest/developerguide/overview.html) 可帮助您创建和控制加密密钥以帮助保护您的数据。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一项计算服务，可帮助您运行代码，无需预调配或管理服务器。它只在需要时运行您的代码，并自动进行扩展，因此您只需为使用的计算时间付费。
+ [Amazon Simple Notiﬁcation Service (Amazon SNS)](https://docs.aws.amazon.com/sns/latest/dg/welcome.html) 可帮助您协调和管理发布者与客户端（包括 Web 服务器和电子邮件地址）之间的消息交换。
+ [Amazon Simple Storage Service（Amazon S3）](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html)是一项基于云的对象存储服务，可帮助您存储、保护和检索任意数量的数据。
+ [AWS Transfer Family](https://docs.aws.amazon.com/transfer/latest/userguide/what-is-aws-transfer-family.html)帮助您通过 SFTP、FTPS 或 FTP 协议将文件传入和传出 AWS 存储服务。
+ [Amazon Virtual Private Cloud（亚马逊 VPC）](https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html)可帮助您将 AWS 资源启动到您定义的虚拟网络中。该虚拟网络类似于您在数据中心中运行的传统网络，并具有使用 AWS的可扩展基础设施的优势。

**代码存储库**

此模式的代码可在 GitHub [AWS Transfer Family 和 GuardDuty 恶意软件扫描解决方案](https://github.com/aws-samples/sample-secure-transfer-family-code)存储库中找到。

## 最佳实践
<a name="secure-file-transfers-best-practices"></a>

提供的 CloudFormation 模板旨在整合许多 AWS 最佳实践，例如 IAM 角色和策略的最低权限权限、静态和传输中的加密以及自动密钥轮换。对于生产环境，请考虑实施以下其他建议：
+ 为 Amazon Cognito 用户启用 [MFA](https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-mfa.html)
+ [AWS Shield](https://docs.aws.amazon.com/waf/latest/developerguide/shield-chapter.html)实现分布式拒绝服务 (DDoS) 保护
+ 配置 [AWS Config](https://docs.aws.amazon.com/config/latest/developerguide/WhatIsConfig.html) 以实现持续合规性监控
+ 实施 [AWS CloudTrail](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-user-guide.html) 以实现全面的 API 日志记录
+ 将 [Amazon](https://docs.aws.amazon.com/guardduty/latest/ug/what-is-guardduty.html) 设置 GuardDuty为除恶意软件扫描之外的威胁检测
+ 实施 [AWS Security Hub CSPM](https://docs.aws.amazon.com/securityhub/latest/userguide/what-is-securityhub-v2.html) 以实现集中式安全管理
+ 使用 [AWS Secrets Manager](https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html) 进行凭证管理
+ 通过[流量镜像](https://docs.aws.amazon.com/vpc/latest/mirroring/what-is-traffic-mirroring.html)实现网络流量监控
+ 配置 [Amazon Macie](https://docs.aws.amazon.com/macie/latest/user/what-is-macie.html) 以在 Amazon S3 中发现和保护敏感数据
+ 定期进行安全评估和渗透测试
+ 制定正式的事件响应计划
+ 对所有组件实施自动化修补
+ 为管理员开展定期安全培训
+ 设置 [AWS Organizations](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_introduction.html) 以实现多账户安全管理

## 操作说明
<a name="secure-file-transfers-epics"></a>

### 部署资源
<a name="deploy-the-resources"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 克隆存储库。 | 输入以下命令将[ GuardDuty 恶意软件扫描解决方案](https://github.com/aws-samples/sample-secure-transfer-family-code)存储库克隆到本地工作站：AWS Transfer Family <pre>git clone https://github.com/aws-samples/sample-secure-transfer-family-code.git</pre> | 应用程序开发者、 DevOps 工程师 | 
| 创建 CloudFormation 堆栈。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/secure-file-transfers.html) | 云管理员、 DevOps 工程师 | 

### 配置资源
<a name="configure-the-resources"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 开启恶意软件防护功能。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/secure-file-transfers.html) | 云管理员、AWS 管理员 | 
| 向用户池中添加用户。 | 向 Amazon Cognito 用户池中添加一个或多个用户。有关说明，请参阅 Amazon Cognito 文档中的[管理用户池中的用户](https://docs.aws.amazon.com/cognito/latest/developerguide/managing-users.html)。 | 云管理员、AWS 管理员 | 

### 测试 SFTP 服务器。
<a name="test-the-sftp-server"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 连接到 SFTP 服务器端点。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/secure-file-transfers.html) | 应用程序开发人员、云管理员、云架构师、 DevOps 工程师 | 

## 问题排查
<a name="secure-file-transfers-troubleshooting"></a>


| 问题 | 解决方案 | 
| --- | --- | 
| 用户身份验证失败 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/secure-file-transfers.html)有关可以帮助您执行这些疑难解答步骤的 AWS CLI 命令列表，请参阅 “[其他信息](#secure-file-transfers-additional)” 部分中的*用于故障排除的有用命令*。 | 
| SFTP 身份验证失败 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/secure-file-transfers.html)有关可以帮助您执行这些疑难解答步骤的 AWS CLI 命令列表，请参阅 “[其他信息](#secure-file-transfers-additional)” 部分中的*用于故障排除的有用命令*。 | 
| 文件上传访问被拒绝 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/secure-file-transfers.html)有关可以帮助您执行这些疑难解答步骤的 AWS CLI 命令列表，请参阅 “[其他信息](#secure-file-transfers-additional)” 部分中的*用于故障排除的有用命令*。 | 
| 未执行恶意软件扫描 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/secure-file-transfers.html)有关可以帮助您执行这些疑难解答步骤的 AWS CLI 命令列表，请参阅 “[其他信息](#secure-file-transfers-additional)” 部分中的*用于故障排除的有用命令*。 | 
| Lambda 函数错误 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/secure-file-transfers.html)有关可以帮助您执行这些疑难解答步骤的 AWS CLI 命令列表，请参阅 “[其他信息](#secure-file-transfers-additional)” 部分中的*用于故障排除的有用命令*。 | 

## 相关资源
<a name="secure-file-transfers-resources"></a>
+ [Transfer Family Web 应用程序](https://docs.aws.amazon.com/transfer/latest/userguide/web-app.html)（Transfer Family 文档）

## 附加信息
<a name="secure-file-transfers-additional"></a>

**用于问题排查的有用命令**

检查 CloudFormation 堆栈的状态：

```
aws cloudformation describe-stacks \
  --stack-name <STACK_NAME>
```

列出 Amazon Cognito 用户池中的所有用户：

```
aws cognito-idp list-users \
  --user-pool-id <USER_POOL_ID>
```

查看 Lambda 函数的日志：

```
aws logs describe-log-groups \
  --log-group-name-prefix /aws/lambda/
```

检查以下各项的状态 GuardDuty：

```
aws guardduty list-detectors
```

检查安全组规则：

```
aws ec2 describe-security-groups \
  --group-ids <SECURITY_GROUP_ID> \
  --output table
```

检查 AWS Transfer Family 服务器的状态：

```
aws transfer describe-server \
  --server-id <SERVER_ID>
```

列出 Amazon S3 存储桶中的所有文件：

```
aws s3 ls s3://<BUCKET_NAME>/ \
  --recursive
```

检查 EventBridge 规则的状态：

```
aws events describe-rule \
  --name <RULE_NAME>
```