Az - Function Apps
Tip
学习并练习 AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
学习并练习 GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
学习并练习 Az Hacking:HackTricks Training Azure Red Team Expert (AzRTE)
支持 HackTricks
- 查看 subscription plans!
- 加入 💬 Discord group 或者 telegram group 或 关注 我们的 Twitter 🐦 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud github 仓库 提交 PRs 来分享 hacking tricks。
基本信息
Azure Function Apps 是一种 无服务器计算服务,允许您运行小段代码,称为 函数,而无需管理底层基础设施。它们旨在响应各种触发器执行代码,例如 HTTP 请求、定时器或来自其他 Azure 服务(如 Blob 存储或事件中心)的事件。Function Apps 支持多种编程语言,包括 C#、Python、JavaScript 和 Java,使其在构建 事件驱动应用程序、自动化工作流或集成服务方面具有多功能性。它们具有成本效益,因为您通常只需为代码运行时使用的计算时间付费。
Note
请注意,Functions 是 App Services 的一个子集,因此,这里讨论的许多功能也将被作为 Azure Apps 创建的应用程序使用(在 cli 中为
webapp)。
不同计划
- 灵活消费计划:提供 动态、事件驱动的扩展,采用按需付费定价,根据需求添加或删除函数实例。它支持 虚拟网络 和 预配置实例 以减少冷启动,使其适合 不需要容器支持的可变工作负载。
- 传统消费计划:默认的无服务器选项,您 仅在函数运行时为计算资源付费。它根据传入事件自动扩展,并包括 冷启动优化,但不支持容器部署。适合需要自动扩展的 间歇性工作负载。
- 高级计划:旨在提供 一致的性能,具有 预热工作者 以消除冷启动。它提供 延长的执行时间、虚拟网络,并支持 自定义 Linux 镜像,非常适合需要高性能和高级功能的 关键任务应用程序。
- 专用计划:在专用虚拟机上运行,具有 可预测的计费,支持手动或自动扩展。它允许在同一计划上运行多个应用程序,提供 计算隔离,并通过应用服务环境确保 安全网络访问,非常适合需要一致资源分配的 长时间运行的应用程序。
- 容器应用:允许在受管理的环境中部署 容器化函数应用,与微服务和 API 一起使用。它支持自定义库、遗留应用迁移和 GPU 处理,消除了 Kubernetes 集群管理。非常适合 事件驱动、可扩展的容器化应用程序。
存储桶
在创建一个新的非容器化的 Function App 时(但提供要运行的代码),代码和其他与函数相关的数据将存储在存储帐户中。默认情况下,Web 控制台将为每个函数创建一个新的存储帐户以存储代码。
此外,修改存储桶中的代码(以不同格式存储),应用的代码将被修改为新的代码,并在下次调用函数时执行。
Caution
从攻击者的角度来看,这非常有趣,因为 对该存储桶的写入访问 将允许攻击者 破坏代码并提升权限 到 Function App 内的托管身份。
更多信息请参见 权限提升部分。
还可以在存储帐户的容器 azure-webjobs-secrets 中找到存储的 主密钥和函数密钥,在 <app-name> 文件夹中的 JSON 文件中可以找到。
请注意,Functions 还允许将代码存储在远程位置,只需指明其 URL。
网络
使用 HTTP 触发器:
- 可以 允许所有互联网用户访问函数,而无需任何身份验证或基于 IAM 的访问。尽管也可以限制此访问。
- 还可以 授予或限制 从 内部网络 (VPC) 访问 Function App。
Caution
从攻击者的角度来看,这非常有趣,因为可能可以 从暴露于互联网的脆弱函数 进行 内部网络的横向移动。
Function App 设置与环境变量
可以在应用内配置环境变量,这些变量可能包含敏感信息。此外,默认情况下会创建环境变量 AzureWebJobsStorage 和 WEBSITE_CONTENTAZUREFILECONNECTIONSTRING(以及其他变量)。这些变量特别有趣,因为它们 包含控制存储帐户的帐户密钥,具有完全权限,该存储帐户包含应用程序的数据。这些设置在从存储帐户执行代码时也很重要。
这些环境变量或配置参数还控制函数如何执行代码,例如,如果存在 WEBSITE_RUN_FROM_PACKAGE,则会指示应用程序代码所在的 URL。
Function 沙箱
在 Linux 沙箱中,源代码位于 /home/site/wwwroot 的文件 function_app.py(如果使用 Python),运行代码的用户是 app(没有 sudo 权限)。
在使用 NodeJS 的 Windows 函数中,代码位于 C:\home\site\wwwroot\HttpTrigger1\index.js,用户名是 mawsFnPlaceholder8_f_v4_node_20_x86,并属于以下 组:Mandatory Label\High Mandatory Level Label、Everyone、BUILTIN\Users、NT AUTHORITY\INTERACTIVE、CONSOLE LOGON、NT AUTHORITY\Authenticated Users、NT AUTHORITY\This Organization、BUILTIN\IIS_IUSRS、LOCAL、10-30-4-99\Dwas Site Users。
托管身份与元数据
与 虚拟机 一样,Functions 可以具有 托管身份,分为两种类型:系统分配和用户分配。
系统分配 的身份是一个托管身份,只有分配了该身份的函数 可以使用,而 用户分配 的托管身份是 任何其他 Azure 服务都可以使用的托管身份。
Note
与 虚拟机 一样,Functions 可以具有 1 个系统分配 的托管身份和 多个用户分配 的托管身份,因此如果您破坏了该函数,始终重要的是尝试找到所有托管身份,因为您可能能够通过一个函数提升到多个托管身份的权限。
如果未使用系统托管身份,但一个或多个用户托管身份附加到函数,默认情况下您将无法获取任何令牌。
可以使用 PEASS 脚本 从元数据端点获取默认托管身份的令牌。或者您可以 手动 获取,如下所述:
请注意,您需要找到一种方法来 检查函数附加的所有托管身份,因为如果您不指明,元数据端点将 仅使用默认身份(有关更多信息,请查看前面的链接)。
访问密钥
Note
请注意,没有 RBAC 权限可以授予用户调用函数的访问权限。函数调用取决于创建时选择的触发器,如果选择了 HTTP 触发器,可能需要使用 访问密钥。
在使用 HTTP 触发器 的函数中创建端点时,可以指明触发函数所需的 访问密钥授权级别。提供三种选项:
- ANONYMOUS:每个人都可以通过 URL 访问该函数。
- FUNCTION:端点仅对使用 函数、主机或主密钥 的用户可访问。
- ADMIN:端点仅对使用 主密钥 的用户可访问。
密钥类型:
- 函数密钥:函数密钥可以是默认的或用户定义的,旨在仅授予对 Function App 中 特定函数端点 的访问权限,从而允许对端点进行更细粒度的访问。
- 主机密钥:主机密钥也可以是默认的或用户定义的,提供对 Function App 中 所有函数端点的访问,具有 FUNCTION 访问级别。
- 主密钥:主密钥(
_master)作为管理密钥,提供提升的权限,包括对所有函数端点的访问(包括 ADMIN 访问级别)。此 密钥无法被撤销。 - 系统密钥:系统密钥由 特定扩展管理,并且在访问内部组件使用的 webhook 端点时是必需的。示例包括事件网格触发器和可持久化函数,它们利用系统密钥与各自的 API 安全交互。
Tip
使用密钥访问函数 API 端点的示例:
https://<function_uniq_name>.azurewebsites.net/api/<endpoint_name>?code=<access_key>
基本身份验证
与应用服务一样,Functions 也支持基本身份验证,以连接到 SCM 和 FTP,使用 Azure 提供的 用户名和密码的 URL 部署代码。有关更多信息,请参见:
基于 Github 的部署
当函数从 Github 仓库生成时,Azure Web 控制台允许 在特定仓库中自动创建 Github 工作流,因此每当该仓库更新时,函数的代码也会更新。实际上,Python 函数的 Github Action yaml 如下所示:
Github Action Yaml
```yaml # Docs for the Azure Web Apps Deploy action: https://github.com/azure/functions-action # More GitHub Actions for Azure: https://github.com/Azure/actions # More info on Python, GitHub Actions, and Azure Functions: https://aka.ms/python-webapps-actionsname: Build and deploy Python project to Azure Function App - funcGithub
on: push: branches:
- main workflow_dispatch:
env: AZURE_FUNCTIONAPP_PACKAGE_PATH: “.” # set this to the path to your web app project, defaults to the repository root PYTHON_VERSION: “3.11” # set this to the python version to use (supports 3.6, 3.7, 3.8)
jobs: build: runs-on: ubuntu-latest steps:
-
name: Checkout repository uses: actions/checkout@v4
-
name: Setup Python version uses: actions/setup-python@v5 with: python-version: ${{ env.PYTHON_VERSION }}
-
name: Create and start virtual environment run: | python -m venv venv source venv/bin/activate
-
name: Install dependencies run: pip install -r requirements.txt
Optional: Add step to run tests here
-
name: Zip artifact for deployment run: zip release.zip ./* -r
-
name: Upload artifact for deployment job uses: actions/upload-artifact@v4 with: name: python-app path: | release.zip !venv/
deploy: runs-on: ubuntu-latest needs: build
permissions: id-token: write #This is required for requesting the JWT
steps:
-
name: Download artifact from build job uses: actions/download-artifact@v4 with: name: python-app
-
name: Unzip artifact for deployment run: unzip release.zip
-
name: Login to Azure uses: azure/login@v2 with: client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_6C3396368D954957BC58E4C788D37FD1 }} tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_7E50AEF6222E4C3DA9272D27FB169CCD }} subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_905358F484A74277BDC20978459F26F4 }}
-
name: “Deploy to Azure Functions” uses: Azure/functions-action@v1 id: deploy-to-function with: app-name: “funcGithub” slot-name: “Production” package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}
</details>
此外,**托管身份**也会被创建,以便来自仓库的Github Action能够使用它登录到Azure。这是通过在**托管身份**上生成一个联邦凭证来完成的,允许**发行者** `https://token.actions.githubusercontent.com` 和**主题标识符** `repo:<org-name>/<repo-name>:ref:refs/heads/<branch-name>`。
> [!CAUTION]
> 因此,任何妥协该仓库的人都将能够妥协该函数及其附加的托管身份。
### 基于容器的部署
并非所有计划都允许部署容器,但对于允许的计划,配置将包含容器的URL。在API中,**`linuxFxVersion`** 设置将类似于: `DOCKER|mcr.microsoft.com/...`,而在Web控制台中,配置将显示**镜像设置**。
此外,**与该函数相关的存储账户中不会存储任何源代码**,因为不需要。
## 枚举
{{#tabs }}
{{#tab name="az cli" }}
```bash
# List all the functions
az functionapp list
# List functions in an function-app (endpoints)
az functionapp function list \
--name <app-name> \
--resource-group <res-group>
# Get details about the source of the function code
az functionapp deployment source show \
--name <app-name> \
--resource-group <res-group>
## If error like "This is currently not supported."
## Then, this is probalby using a container
# Get more info if a container is being used
az functionapp config container show \
--name <name> \
--resource-group <res-group>
# Get settings (and privesc to the sorage account)
az functionapp config appsettings list --name <app-name> --resource-group <res-group>
# Get access restrictions
az functionapp config access-restriction show --name <app-name> --resource-group <res-group>
# Check if a domain was assigned to a function app
az functionapp config hostname list --webapp-name <app-name> --resource-group <res-group>
# Get SSL certificates
az functionapp config ssl list --resource-group <res-group>
# Get network restrictions
az functionapp config access-restriction show --name <app-name> --resource-group <res-group>
# Get acess restrictions
az functionapp config access-restriction show --name <app-name> --resource-group <res-group>
# Get connection strings
az rest --method POST --uri "https://management.azure.com/subscriptions/<subscription>/resourceGroups/<res-group>/providers/Microsoft.Web/sites/<app-name>/config/connectionstrings/list?api-version=2022-03-01"
az rest --method GET --uri "https://management.azure.com/subscriptions/<subscription>/resourceGroups/<res-group>/providers/Microsoft.Web/sites/<app-name>/config/configreferences/connectionstrings?api-version=2022-03-01"
# Get SCM credentials
az functionapp deployment list-publishing-credentials --name <app-name> --resource-group <res-group>
# Get function, system and master keys
az functionapp keys list --name <app-name> --resource-group <res-group>
# Get Host key
az rest --method POST --uri "https://management.azure.com/<subscription>/resourceGroups/<res-group>/providers/Microsoft.Web/sites/<app-name>/functions/<function-endpoint-name>/listKeys?api-version=2022-03-01"
# Get source code with Master Key of the function
curl "<script_href>?code=<master-key>"
curl "https://<func-app-name>.azurewebsites.net/admin/vfs/home/site/wwwroot/function_app.py?code=<master-key>" -v
# Get source code using SCM access (Azure permissions or SCM creds)
az rest --method GET \
--url "https://<func-app-name>.azurewebsites.net/admin/vfs/home/site/wwwroot/function_app.py?code=<master-key>" \
--resource "https://management.azure.com/"
# Get source code with Azure permissions
az rest --url "https://management.azure.com/subscriptions/<subscription>/resourceGroups/<res-group>/providers/Microsoft.Web/sites/<app-name>/hostruntime/admin/vfs/function_app.py?relativePath=1&api-version=2022-03-01"
## Another example
az rest --url "https://management.azure.com/subscriptions/9291ff6e-6afb-430e-82a4-6f04b2d05c7f/resourceGroups/Resource_Group_1/providers/Microsoft.Web/sites/ConsumptionExample/hostruntime/admin/vfs/HttpExample/index.js?relativePath=1&api-version=2022-03-01"
{{#endtab }}
{{#tab name=“Az Powershell” }}
Get-Command -Module Az.Functions
# Lists all Function Apps in the current subscription or in a specific resource group.
Get-AzFunctionApp -ResourceGroupName <String>
# Displays the regions where Azure Function Apps are available for deployment.
Get-AzFunctionAppAvailableLocation
# Retrieves details about Azure Function App plans in a subscription or resource group.
Get-AzFunctionAppPlan -ResourceGroupName <String> -Name <String>
# Retrieves the app settings for a specific Azure Function App.
Get-AzFunctionAppSetting -Name <FunctionAppName> -ResourceGroupName <ResourceGroupName>
{{#endtab }} {{#endtabs }}
权限提升
参考
Tip
学习并练习 AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
学习并练习 GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
学习并练习 Az Hacking:HackTricks Training Azure Red Team Expert (AzRTE)
支持 HackTricks
- 查看 subscription plans!
- 加入 💬 Discord group 或者 telegram group 或 关注 我们的 Twitter 🐦 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud github 仓库 提交 PRs 来分享 hacking tricks。
HackTricks Cloud

