AWS - Step Functions Enum

支持 HackTricks

Step Functions

AWS Step Functions 是一种工作流服务,使您能够协调和编排多个 AWS 服务到无服务器工作流中。通过使用 AWS Step Functions,您可以设计和运行连接各种 AWS 服务(如 AWS Lambda、Amazon S3、Amazon DynamoDB 等)的工作流,按步骤顺序进行。这种编排服务提供了可视化工作流界面,并提供 状态机 功能,允许您使用基于 JSON 的 Amazon States Language (ASL) 以声明性方式定义工作流的每个步骤。

关键概念

标准与快速工作流

AWS Step Functions 提供两种类型的 状态机工作流:标准和快速。

  • 标准工作流:这种默认工作流类型旨在用于长期、持久和可审计的过程。它支持 精确一次执行,确保任务仅执行一次,除非指定重试。它非常适合需要详细执行历史的工作流,并且可以运行长达一年。

  • 快速工作流:这种类型非常适合高容量、短时任务,运行时间最长可达五分钟。它们支持 至少一次执行,适合幂等任务,如数据处理。这些工作流经过优化,具有成本和性能优势,按执行次数、持续时间和内存使用收费。

状态

状态是状态机的基本单元。它们定义工作流中的各个步骤,能够根据其类型执行多种功能:

  • 任务: 执行作业,通常使用 AWS 服务,如 Lambda。

  • 选择: 根据输入做出决策。

  • 失败/成功: 以失败或成功结束执行。

  • 传递: 将输入传递到输出或注入数据。

  • 等待: 延迟执行一段时间。

  • 并行: 启动并行分支。

  • 映射: 动态迭代步骤。

任务

任务 状态表示由状态机执行的单个工作单元。任务可以调用各种资源,包括活动、Lambda 函数、AWS 服务或第三方 API。

  • 活动:您管理的自定义工作者,适合长期运行的过程。

  • 资源:arn:aws:states:region:account:activity:name

  • Lambda 函数:执行 AWS Lambda 函数。

  • 资源:arn:aws:lambda:region:account:function:function-name

  • AWS 服务:直接与其他 AWS 服务集成,如 DynamoDB 或 S3。

  • 资源:arn:partition:states:region:account:servicename:APIname

  • HTTP 任务:调用第三方 API。

  • 资源字段:arn:aws:states:::http:invoke。然后,您应提供 API 端点配置详细信息,如 API URL、方法和身份验证详细信息。

以下示例显示了一个调用名为 HelloWorld 的 Lambda 函数的任务状态定义:

"HelloWorld": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"Payload.$": "$",
"FunctionName": "arn:aws:lambda:<region>:<account-id>:function:HelloWorld"
},
"End": true
}

Choice

一个 Choice 状态为工作流添加条件逻辑,使其能够根据输入数据做出决策。它评估指定的条件,并根据结果转换到相应的状态。

  • Comparison: 每个选择规则包括一个比较运算符(例如,NumericEqualsStringEquals),用于将输入变量与指定值或另一个变量进行比较。

  • Next Field: Choice 状态不支持 End 字段,而是定义 Next 状态,以便在比较为真时进行转换。

Choice 状态的示例:

{
"Variable": "$.timeStamp",
"TimestampEquals": "2000-01-01T00:00:00Z",
"Next": "TimeState"
}

Fail/Succeed

一个 Fail 状态停止状态机的执行并将其标记为失败。它用于指定错误名称和原因,提供有关失败的详细信息。此状态是终端状态,意味着它结束执行流程。

一个 Succeed 状态成功地停止执行。它通常用于在工作流成功完成时终止工作流。此状态不需要 Next 字段。

"FailState": {
"Type": "Fail",
"Error": "ErrorName",
"Cause": "Error details"
}

Pass

一个 Pass 状态将其输入传递给输出,既不执行任何工作,也不使用过滤器转换 JSON 状态输入,然后将转换后的数据传递给下一个状态。它对于测试和构建状态机非常有用,允许您注入静态数据或对其进行转换。

"PassState": {
"Type": "Pass",
"Result": {"key": "value"},
"ResultPath": "$.newField",
"Next": "NextState"
}

Wait

一个 Wait 状态会延迟状态机的执行,直到指定的持续时间。配置等待时间的主要方法有三种:

  • X Seconds: 固定的等待秒数。

"WaitState": {
"Type": "Wait",
"Seconds": 10,
"Next": "NextState"
}
  • Absolute Timestamp: 等待直到的确切时间。

"WaitState": {
"Type": "Wait",
"Timestamp": "2024-03-14T01:59:00Z",
"Next": "NextState"
}
  • Dynamic Wait: 基于输入使用 SecondsPathTimestampPath

jsonCopiar código
"WaitState": {
"Type": "Wait",
"TimestampPath": "$.expirydate",
"Next": "NextState"
}

Parallel

一个 Parallel 状态允许您在工作流中并发执行多个任务分支。每个分支独立运行并处理自己的状态序列。执行会等待所有分支完成后再继续到下一个状态。其关键字段包括:

  • Branches: 定义并行执行路径的数组。每个分支是一个单独的状态机。

  • ResultPath: 定义将分支的合并输出放置在输入中的位置。

  • Retry and Catch: 并行状态的错误处理配置。

"ParallelState": {
"Type": "Parallel",
"Branches": [
{
"StartAt": "Task1",
"States": { ... }
},
{
"StartAt": "Task2",
"States": { ... }
}
],
"Next": "NextState"
}

Map

一个 Map 状态允许对数据集中的每个项目执行一组步骤。它用于数据的并行处理。根据您希望如何处理数据集中的项目,Step Functions 提供以下模式:

  • Inline Mode: 对每个 JSON 数组项执行一组状态的子集。适用于并行迭代少于 40 次的小规模任务,在包含 Map 状态的工作流上下文中运行每个任务。

"MapState": {
"Type": "Map",
"ItemsPath": "$.arrayItems",
"ItemProcessor": {
"ProcessorConfig": {
"Mode": "INLINE"
},
"StartAt": "AddState",
"States": {
"AddState": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"OutputPath": "$.Payload",
"Parameters": {
"FunctionName": "arn:aws:lambda:<region>:<account-id>:function:add-function"
},
"End": true
}
}
},
"End": true
"ResultPath": "$.detail.added",
"ItemsPath": "$.added"
}
  • Distributed Mode: 设计用于大规模并行处理,具有高并发性。支持处理大型数据集,例如存储在 Amazon S3 中的数据集,允许高达 10,000 个并行子工作流执行,这些子工作流作为单独的子执行运行。

"DistributedMapState": {
"Type": "Map",
"ItemReader": {
"Resource": "arn:aws:states:::s3:getObject",
"Parameters": {
"Bucket": "my-bucket",
"Key": "data.csv"
}
},
"ItemProcessor": {
"ProcessorConfig": {
"Mode": "DISTRIBUTED",
"ExecutionType": "EXPRESS"
},
"StartAt": "ProcessItem",
"States": {
"ProcessItem": {
"Type": "Task",
"Resource": "arn:aws:lambda:region:account-id:function:my-function",
"End": true
}
}
},
"End": true
"ResultWriter": {
"Resource": "arn:aws:states:::s3:putObject",
"Parameters": {
"Bucket": "myOutputBucket",
"Prefix": "csvProcessJobs"
}
}
}

Versions and aliases

Step Functions 还允许您通过状态机的 versionsaliases 管理工作流部署。版本表示可以执行的状态机快照。别名作为指向最多两个版本的状态机的指针。

  • Versions: 这些不可变的状态机快照是从该状态机的最新修订版创建的。每个版本由一个唯一的 ARN 标识,该 ARN 将状态机 ARN 与版本号组合,之间用冒号分隔 (arn:aws:states:region:account-id:stateMachine:StateMachineName:version-number)。版本不能被编辑,但您可以更新状态机并发布新版本,或使用所需的状态机版本。

  • Aliases: 这些指针可以引用同一状态机的最多两个版本。可以为单个状态机创建多个别名,每个别名由一个唯一的 ARN 标识,该 ARN 通过将状态机 ARN 与别名名称组合,之间用冒号分隔 (arn:aws:states:region:account-id:stateMachine:StateMachineName:aliasName)。别名使得在状态机的两个版本之间路由流量成为可能。或者,别名可以指向状态机的单个特定版本,但不能指向其他别名。它们可以根据需要更新以重定向到状态机的不同版本,从而促进受控部署和工作流管理。

有关 ASL 的更多详细信息,请查看: Amazon States Language

IAM Roles for State machines

AWS Step Functions 利用 AWS 身份和访问管理 (IAM) 角色来控制对状态机内资源和操作的访问。以下是与 AWS Step Functions 中的安全性和 IAM 角色相关的关键方面:

  • Execution Role: AWS Step Functions 中的每个状态机都与一个 IAM 执行角色相关联。该角色定义了状态机可以代表您执行的操作。当状态机在与 AWS 服务交互的状态之间转换时(例如调用 Lambda 函数、访问 DynamoDB 等),它会假定此执行角色以执行这些操作。

  • Permissions: IAM 执行角色必须配置具有允许在其他 AWS 服务上执行必要操作的权限。例如,如果您的状态机需要调用 AWS Lambda 函数,则 IAM 角色必须具有 lambda:InvokeFunction 权限。同样,如果它需要写入 DynamoDB,则必须授予适当的权限(dynamodb:PutItemdynamodb:UpdateItem 等)。

Enumeration

ReadOnlyAccess 策略足以满足以下所有枚举操作。

# State machines #

## List state machines
aws stepfunctions list-state-machines
## Retrieve informatio about the specified state machine
aws stepfunctions describe-state-machine --state-machine-arn <value>

## List versions for the specified state machine
aws stepfunctions list-state-machine-versions --state-machine-arn <value>
## List aliases for the specified state machine
aws stepfunctions list-state-machine-aliases --state-machine-arn <value>
## Retrieve information about the specified state machine alias
aws stepfunctions describe-state-machine-alias --state-machine-alias-arn <value>

## List executions of a state machine
aws stepfunctions list-executions --state-machine-arn <value> [--status-filter <RUNNING | SUCCEEDED | FAILED | TIMED_OUT | ABORTED | PENDING_REDRIVE>] [--redrive-filter <REDRIVEN | NOT_REDRIVEN>]
## Retrieve information and relevant metadata about a state machine execution (output included)
aws stepfunctions describe-execution --execution-arn <value>
## Retrieve information about the state machine associated to the specified execution
aws stepfunctions describe-state-machine-for-execution --execution-arn <value>
## Retrieve the history of the specified execution as a list of events
aws stepfunctions get-execution-history --execution-arn <value> [--reverse-order | --no-reverse-order] [--include-execution-data | --no-include-execution-data]

## List tags for the specified step Functions resource
aws stepfunctions list-tags-for-resource --resource-arn <value>

## Validate the definition of a state machine without creating the resource
aws stepfunctions validate-state-machine-definition --definition <value> [--type <STANDARD | EXPRESS>]

# Activities #

## List existing activities
aws stepfunctions list-activities
## Retrieve information about the specified activity
aws stepfunctions describe-activity --activity-arn <value>

# Map Runs #

## List map runs of an execution
aws stepfunctions list-map-runs --execution-arn <value>
## Provide information about the configuration, progress and results of a Map Run
aws stepfunctions describe-map-run --map-run-arn <value>
## Lists executions of a Map Run
aws stepfunctions list-executions --map-run-arn <value> [--status-filter <RUNNING | SUCCEEDED | FAILED | TIMED_OUT | ABORTED | PENDING_REDRIVE>] [--redrive-filter <REDRIVEN | NOT_REDRIVEN>]

Privesc

在以下页面中,您可以查看如何滥用 Step Functions 权限以提升特权

AWS - Step Functions Privesc

Post Exploitation

AWS - Step Functions Post Exploitation

Persistence

AWS - Step Functions Persistence

References

支持 HackTricks

Last updated