定位服务帐户以方便管理和清理
这篇文章解释了如何收集 Active Directory 服务帐户的详细列表,以便您可以实施适当的治理来减少攻击面。
具体来说,它详细介绍了如何枚举以下各项使用的服务帐户:
- 视窗服务
- 计划任务
- IIS 应用程序池
这将使您能够识别服务帐户的很大一部分。但是,请注意,服务帐户也可以在虚拟目录、身份验证设置等中使用。本文不介绍如何枚举这些服务帐户。
视窗服务
查找正在使用的服务帐户的最明显位置是 Windows 服务 (Services.msc)。只需单击“开始”菜单,在搜索框中键入“服务”,然后单击服务。您将看到服务列表以及用于运行它们的帐户:
或者,您可以使用 PowerShell 和 WMI 检索此信息并将其存储在 CSV 文件中以供以后处理。您可以选择以下三个脚本:
# WMI Query to Retrieve All Services, Get the Accounts Used to Log on to Them, and Export Them to a CSV File on the Local Computer
$CSV_File_Path = "" #Path to excel file to export information to
$Services = Get-WmiObject Win32_Service
$Services_Info = $Services | Select-Object Name, Caption, StartName, StartMode
$Services_Info | Export-Csv $CSV_File_Path
# WMI Query to a Remote Computer to Retrieve Service Information
$CSV_File_Path = "" #Path to excel file to export information to
$Computer_Name = "" #Name or IP Address of the computer to query
$Services = Get-WmiObject Win32_Service -ComputerName $Computer_Name
$Services_Info = $Services | Select-Object PSComputerName, Name, Caption, StartName, StartMode
$Services_Info | Export-Csv $CSV_File_Path
# Query Multiple Computers using a CSV List
$CSV_Input_File = "" # Path to CSV File where A1 is a header called ComputerName and A2 and below are Computer Names
$CSV_File_Path = "" #Path to excel file to export information to
$Computer_Names_Import = Import-Csv $CSV_Input_File
foreach ($Computer_Name in $Computer_Names_Import)
{
$Computer_Name = $Computer_Name.ComputerName
$Services = Get-WmiObject Win32_Service -ComputerName $Computer_Name
$Services_Info = $Services | Select-Object PSComputerName, Name, Caption, StartName, StartMode
$Services_Info | Export-Csv $CSV_File_Path -Append
}然后,您可以在 Excel 中打开该文件并使用筛选器来细化如下所示的关键详细信息:
确保在所有服务器上运行脚本。
计划任务
服务帐户通常用于运行计划任务;这样,管理员可以仅向帐户授予任务所需的权限。
如果您使用 GUI 来查找这些服务帐户,则需要检查用于运行它的帐户的每个计划任务:
较新版本的 Windows 会为所有登录用户创建任务,这意味着可以安全地忽略类似于上述任务 (User_Feed_Synchronization) 的任务。但是,应检查所有其他任务的服务帐户。
有两种自动方式来收集计划任务使用的服务帐户:
- 使用 ScheduledTasks PowerShell 模块。但是,我不相信这个选项可以用来查询旧系统。
- 使用 C:WindowsSystem32Tasks 中的文件
使用计划任务 Cmdlet
如果 ScheduledTasks cmdlet 可以在您需要查询的所有服务器上运行,那么使用 ScheduledTasks cmdlet 是更简单的选择。以下是用于创建 CSV 文件的 PowerShell 代码段,其中列出了具有关联用户名和登录类型的任务:
# Getting Scheduled Tasks using Get-ScheduledTask
$CSV_File_Path = "" #Path to excel file to export information to
$All_Scheduled_Tasks = Get-ScheduledTask #This will also include the default microsoft ones
$Scheduled_Tasks_ExMicrosoft = $All_Scheduled_Tasks | Where-Object { $_.TaskPath -notlike "Microsoft*" } # Exclude the Microsoft Tasks
$Scheduled_Tasks_Info = $Scheduled_Tasks_ExMicrosoft | Select-Object TaskPath, TaskName, @{ Name = "Username"; Expression = { $_.Principal.UserID } }, @{ Name = "LogonType"; Expression = { $_.Principal.LogonType } }
$Scheduled_Tasks_Info | Export-Csv $CSV_File_Path使用任务 XML 数据
使用任务文件获取数据似乎在所有操作系统上都能很好地工作,但它有点乏味:
# Getting Scheduled Tasks using XML Files in C:WindowsSystem32Tasks
$CSV_File_Path = "" #Path to excel file to export information to
$Task_Folder_Location = "C:WindowsSystem32Tasks" # Path to the Scheduled Tasks folder
$Task_XML_Files_All = Get-ChildItem $Task_XML_Files -Recurse
$Task_XML_Files_Filtered = $Task_XML_Files_All | Where-Object { $_.FullName -notlike "$Task_Folder_LocationMicrosoft*" }
foreach ($Task_XML_File in $Task_XML_Files_Filtered)
{
$Task_XML_File_Path = $Task_XML_File.FullName
#Getting the Principal and Logon Type
[XML]$XML_Content = Get-Content $Task_XML_File_Path
$Task_Principal = $XML_Content.Task.Principals.Principal.UserID
$Task_LogonType = $XML_Content.Task.Principals.Principal.LogonType
$Output = $Task_XML_File | Select-Object Name, FullName, @{ Name = "Task_Principal"; Expression = { $Task_Principal } }, @{ Name = "Task_LogonType"; Expression = { $Task_LogonType } }
$Output | Export-Csv $CSV_File_Path -Append
}IIS 应用程序池
IIS 应用程序池仍然很常见,尤其是对于运行内部网站而言。组织可以使用服务帐户对每个应用程序池强制执行最低权限访问。
要使用 GUI 查找用于运行应用程序池的服务帐户,请按照以下说明操作:
使用 PowerShell 查找此信息有点麻烦。首先,您需要确保要询问的服务器安装了 IIS 管理脚本和工具功能。使用以下 PowerShell 片段:
# Check whether the IIS Management Scripts and Tools feature is installed. If not, then install it.
$Feature_Name = "Web-Scripting-Tools"
$Feature = Get-WindowsFeature -Name $Feature_Name
If (-not ($Feature.Installed))
{
Install-WindowsFeature -Name $Feature_Name
}现在,您可以使用 WebAdministration 模块获取一台或多台计算机上 IIS 应用程序池的服务帐户信息:
# Get Application Pools using WebAdministration Module on a Single Machine
$CSV_File_Path = "" #Path to excel file to export information to
$Application_Pools = Get-ChildItem "IIS:AppPools"
$Application_Pools_WithUsername = $Application_Pools | Select-Object Name, State, @{ Name = "Username"; Expression = { $_.ProcessModel.Username } }
$Application_Pools_WithUsername | Export-Csv $CSV_File_Path
# Get Application Pools Using WebAdministration on Multiple Machines
$CSV_Input_File = "" # Path to CSV file with A1 set to ComputerName and then A2 and below being the actual computer names
$CSV_File_Path = "" # Path to excel file to export information to
$Output_Folder = "" # Path to a folder to import all the export files to
$CSV_Computers = Import-Csv $CSV_Input_File
$Computers = $CSV_Computers.ComputerName
Invoke-Command -ComputerName $Computers -ArgumentList $CSV_File_Path -ScriptBlock {
param($CSV_File_Path)
$Application_Pools = Get-ChildItem "IIS:AppPools"
$Application_Pools_WithUsername = $Application_Pools | Select-Object Name, State, @{ Name = "Username"; Expression = { $_.ProcessModel.Username } }
$Application_Pools_WithUsername | Export-Csv $CSV_File_Path
}
# Collecting the output files by using Admin shares and modifying the $CSV_File_Path parameter to get the location on target machine
foreach ($Computer in $Computers)
{
$UNC_File_Path = "\$Computer$($CSV_File_Path -replace ':', '$')"
Move-Item -Path $UNC_File_Path -Destination "$Output_Folder$Computer.csv"
}
# Merging the files into a single CSV file called Merge.csv
$All_CSV_To_Merge = Get-ChildItem -Path $Output_Folder
foreach ($CSV in $All_CSV_To_Merge)
{
$CSV_Import = Import-Csv $CSV.FullName
$CSV_Import_Update = $CSV_Import | Select-Object Name, State, Username, @{ Name = "ComputerName"; Expression = { $CSV.Name } }
$CSV_Import_Update | Export-Csv "$Output_FolderMerge.csv" -Append
}使用收集的服务帐户列表
现在我们有 CSV 文件列出了以下用户使用的服务帐户:
- 视窗服务
- 计划任务
- IIS 应用程序池
每个文件都有一个 ComputerName 或 PSComputerName 字段,可用于更好地查看服务器上的配置内容。每个帐户还有一个用户名字段,可用于确定特定帐户可以在多少台服务器上使用。
为了简化分析,您可以使用 Excel VLOOKUP 公式来组织数据。或者,您可以将 CSV 文件提取到数据库中,设置字段之间的关系,并使用查询来获取所需的信息。
Netwrix 如何提供帮助?
即使使用 PowerShell,收集和整理服务帐户的数据也可能相当麻烦。 Netwrix StealthAUDIT 提供易于使用的服务帐户报告,以及原始数据(如果您需要)。
此外,Netwrix StealthAUDIT 将使您能够:
- 识别攻击者可利用的漏洞来危害 Windows 系统并获取您的数据。
- 通过快速有效地评估系统补丁级别来修复安全漏洞。
- 通过基线配置分析实施安全和运营策略。
- 审计和管理特权帐户。
- 通过预构建的报告和完整的系统透明度更轻松地证明合规性。





