使用 PowerShell 更新 Teams 外部访问域
使用外部访问允许列表阻止恶意入站聊天
在我关于 GIFShell 概念验证攻击以危害 Teams 数据的文章中,我说过,阻止来自未知租户(可能是攻击者的入口点)的不需要的聊天消息的一种简单方法是创建外部访问允许列表。如果存在允许列表,Teams 将仅接受来自允许列表中的域的入站聊天消息。
通常,租户通过团队管理中心的用户部分管理外部访问。可以轻松地在允许列表中添加或删除域。但是,这假设您知道要添加或删除哪些域。这就是本文提出的问题 - 如何确定哪些域应位于允许列表中?
我的直觉是使用租户中已有的访客帐户集作为指导。毕竟,如果您允许其他租户的某人成为您租户中的来宾帐户,则可以合理地假设您也愿意接受来自该域的入站聊天消息。如果您不这样做,来宾可以参与在施加限制之前开始的频道对话和聊天,但他们无法开始从其租户到您租户中的人员的新聊天。
更新团队外部访问配置的步骤
如果我们认为来宾帐户是控制外部访问的良好基础,我们可以使用 PowerShell 来:
- 查找租户中的所有客人。
- 从每个访客帐户的电子邮件地址中提取域。
- 从来宾帐户中提取的域中构建域列表。
- 获取 Teams 的当前外部访问配置。
- 查找尚未包含在外部访问配置中的域。
- 使用缺少的域更新外部访问配置。
每个租户将以自己的方式处理如何完成此处理的细节,但基本命令保持不变。这是我为完成这项工作而创建的代码。您可以从 GitHub 下载该脚本的最新版本。
Connect-MgGraph -NoWelcome -Scopes User.Read.All
[array]$Guests = Get-MgUser -All -Filter "usertype eq 'Guest'"
Write-Host ("{0} guest accounts found" -f $Guests.Count)
$GuestList = [System.Collections.Generic.List[Object]]::new()
ForEach ($Guest in $Guests) {
$Domain = $Guest.Mail.Split("@")[1]
$ReportLine = [PSCustomObject][Ordered]@{
Guest = $Guest.Mail
Domain = $Domain
Name = $Guest.DisplayName }
$GuestList.Add($ReportLine)
}
Write-Host ""
Write-Host "Guest accounts found for the following domains"
Write-Host "----------------------------------------------"
$GuestList | Group-Object Domain | Sort-Object Name | Select-Object Name, Count
$Domains = $GuestList | Sort-Object Domain -Unique | Select-Object -ExpandProperty Domain
Write-Host "Connecting to Microsoft Teams to check current external access configuration"
Connect-MicrosoftTeams
# Get current set of domains configured for Teams extrenal access
$DomainConfiguration = Get-CsTenantFederationConfiguration | Select-Object -ExpandProperty AllowedDomains
# Check the set of domains that aren't in the current configuration
[array]$DomainsToAdd = $Domains | Where-Object {$_ -notin $DomainConfiguration.AllowedDomain.Domain}
$Prompt = "Do you want to add the following domains to the list allowed for Teams external access? " + $DomainsToAdd -join ", "
$Choice = Read-Host $Prompt
If (($Choice.ToUpper()) -eq "Y") {
$i = 0
ForEach ($Domain in $DomainsToAdd) {
$i++
Write-Host ("Adding {0} to the allowed domains list... ({1}/{2})" -f $Domain, $i, $DomainsToAdd.Count)
Set-CsTenantFederationConfiguration -AllowedDomainsAsAList @{Add=$Domain} -ErrorAction SilentlyContinue
}
$DomainConfiguration = Get-CsTenantFederationConfiguration | Select-Object -ExpandProperty AllowedDomains
Write-Host ("External access for Teams now includes {0} domains" -f $DomainConfiguration.AllowedDomain.Domain.count)
}
外部访问域白名单可容纳约4000条条目。
所需的 PowerShell 模块
该代码非常简单,并使用 Microsoft Graph PowerShell SDK 中的 Get-MgUser cmdlet 从 Microsoft Teams PowerShell 模块中查找来宾帐户和 cmdlet 集,以获取和更新外部访问配置。这两个模块都在积极开发中,微软大约每月都会发布新版本。最好使用最新版本,以确保您不会遇到当前版本中已修复的问题。
除此之外,唯一使用的 cmdlet 是基本的 PowerShell cmdlet,例如 Group-Object、Sort-Object 和 Select-Object。换句话说,没有黑魔法或复杂的代码。
阻止列表的缺点
任何阻止列表的缺点是它们会停止通信。即使在允许来宾帐户来自的每个域之后,当用户无法与其他联系人通信时,他们也可能会感到沮丧。也许您需要一种方法来允许用户请求将新域(检查后)添加到团队外部访问配置中。我将把这场辩论留给你。
在 12 月 6 日至 7 日举行的 2022 年专家会议上与托尼·雷德蒙德 (Tony Redmond) 和其他 Microsoft MVP 会面。
100% 免费和虚拟。您不想错过年度 Microsoft 培训活动!