当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] Windows Server DomainController 查找 LDAP 绑定

作者:精品下载站 日期:2024-12-14 07:13:12 浏览:14 分类:玩电脑

Windows Server DomainController 查找 LDAP 绑定


不久前,Microsoft 宣布更改 LDAP 和 LDAP 签名的默认域控制器行为。请参阅链接。
这会影响每个受支持的 Windows Server 版本(从 2008R2 到 2019)。还有另一个链接ADV190023有详细的解释。

我认为不应该讨论将域控制器更改为仅 LDAP 签名。但是第三方软件呢?如何找到这些软件访问您的 DC 并使用未签名的 ldap?

如果您的域控制器上没有 LDAP 的任何证书,请查看要求,这很重要。如何通过 SSL 启用 LDAP。

我建议在您环境中的每个域控制器上激活 LDAP 登录,并扩展事件日志“目录服务”,以便您可以返回过去查看大多数 LDAP 连接。

当 LDAP 连接被记录为未签名时,它看起来如何:

[玩转系统] Windows Server DomainController 查找 LDAP 绑定

应用程序和服务日志 -> 目录服务 -> 事件 ID 2889

如您所见,记录了执行 ldap 绑定的 IP 地址和用户。

首先,您必须在 DC 上启用 LDAP 登录。
我将使用 gpo 在测试环境中的所有 DC 上设置注册表项,但您也可以手动设置该注册表项:
REGKEY LDAP 登录(应该已经存在,但设置为 0): HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NTDS\Diagnostics -> DWORD -> “16 LDAP Interface Events” -> 值“2”

REGKEY 扩展事件日志:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet \Services\EventLog\Directory Service -> DWORD -> “MaxSize” -> Value= “2147483648” (2GB)

[玩转系统] Windows Server DomainController 查找 LDAP 绑定

您还可以使用 reg add 命令:

Reg Add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NTDS\Diagnostics /v "16 LDAP Interface Events" /t REG_DWORD /d 2

Reg Add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Directory Service" /v "MaxSize" /t REG_DWORD /d "2147483648"

添加此注册表项后,需要一些时间,以便 DC 可以记录连接并希望看到所有未签名的 ldap 连接。 (最好的情况是看不到 LDAP 连接)

如果您想尝试 ldap 和 ldaps 连接,您可以转到您的 dc 或任何其他 Windows 服务器并使用 LDP.exe 进行检查。它位于 C:\Windows\SYSTEM32\ 文件夹中。

[玩转系统] Windows Server DomainController 查找 LDAP 绑定

如果您还进行了简单的绑定,则连接将记录在您的事件日志中

[玩转系统] Windows Server DomainController 查找 LDAP 绑定

另请检查您是否可以使用 SSL 端口 636 连接 LDAP

[玩转系统] Windows Server DomainController 查找 LDAP 绑定

[玩转系统] Windows Server DomainController 查找 LDAP 绑定

完成后,您可以确保您的 DC 接受 LDAPS 并记录 LDAP 连接。现在您必须等待几天才能看到大部分 LDAP 连接(您可能看不到全部,因为如果设备不执行 LDAP 查询,则无需进行任何协议)。

现在是编写脚本的时间,因为搜索事件日志并查找事件消息中包含 IP 地址的所有事件可能非常痛苦。
请参阅以下脚本:

<#
#### requires ps-version 3.0 ####
<#
.SYNOPSIS
Analyses Eventlog from DomainControllers and searches for LDAP access 
.DESCRIPTION
Creates a working directory on your specified path. After creation it copies evtx files from all Domaincontrollers via robocopy into this working directory.
Every domaincontroller eventlog creates four files (cleared and raw) 
.PARAMETER WorkDir 
Path where you want to have your logs and results (please check diskspace, evtx file may have some GBs)
.INPUTS
-
.OUTPUTS
YYYY-MM-DD-HH-MM-SS-NameofDC.evtx (copied but untouched)
YYYY-MM-DD-HH-MM-SS-NameofDC-LDAP-cleared.csv
YYYY-MM-DD-HH-MM-SS-NameofDC-LDAP-raw.csv
YYYY-MM-DD-HH-MM-SS-NameofDC-LDAP-time.csv
YYYY-MM-DD-HH-MM-SS-NameofDC-LDAP-time-cleared.csv
.NOTES
   Version:        0.1
   Author:         Alexander Koehler
   Creation Date:  Tuesday, March 10th 2020, 11:07:01 pm
   File: ad-ldap-audit-0-1.ps1
   Copyright (c) 2020 blog.it-koehler.com
HISTORY:
Date      	          By	Comments
----------	          ---	----------------------------------------------------------
.LINK
 blog.it-koehler.com/en/Archive/2951
.COMPONENT
 Required Modules: none

.LICENSE
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the Software), to deal
in the Software without restriction, including without limitation the rights
to use copy, modify, merge, publish, distribute sublicense and /or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
 
.EXAMPLE
C:\temp\ad-ldap-audit-0-1.ps1 -WorkDir "C:\temp\ldapaudit"
#
#>

# define parameters (Working Directory) and check if it's a directory and if it does not exist, create it.
[CmdletBinding()]
Param(
    [Parameter(Mandatory = $True)]
    [String] $WorkDir)
if (-not (Test-Path $WorkDir -PathType Container)) {
    
    try {
        New-Item -Path $WorkDir -ItemType Directory -ErrorAction Stop | Out-Null
    }
    catch {
        Write-Error -Message "Unable to create directory '$WorkDir'. Error was: $_" -ErrorAction Stop
    }
    "Successfully created directory '$WorkDir'."
}
else {
    "Directory already existes"
}
#get date as string (is needed for filenames)
$date=((Get-Date).ToString('yyyy-MM-dd-HH-mm-ss'))
#getting all DCs in environment without AD PowerSehll Module 
$DCs = ([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites | % { $_.Servers } | select Name).Name
#executing code for every domaincontroller
foreach($DC in $DCs){
#copy evtx file in working dir with robocopy
$path = "\$DC\c$\Windows\System32\winevt\Logs\"
robocopy "$path" "$workdir" "Directory Service.evtx" 
Rename-Item "$workdir\Directory Service.evtx" "$date-$DC.evtx"
$evtxfile = "$workdir$date-$DC.evtx"
#define output file prefix
$csvoutputpath = "$workdir$date-$DC-LDAP"
#searching for event 2889
$Events = Get-WinEvent @{Path=$evtxfile;Id=2889}
#define array for outputs
$eventarraytime = @()
$eventarrayraw =@()
  ForEach ($Event in $Events) {           
    # Convert the event to XML           
    #see https://docs.microsoft.com/de-de/archive/blogs/ashleymcglone/powershell-get-winevent-xml-madness-getting-details-from-event-logs
    $eventXML = [xml]$Event.ToXml()
    #getting timestamp
    $time = ($Event.TimeCreated)
    #getting user from eventlog
    $ldapconn = ($eventXML.Event.EventData.Data)
    #creating custom object and put all together
    #customobject with timestamp
    $eventobj = New-Object System.Object
    $eventobj | Add-Member -type NoteProperty -name AccessTime -Value $time
    $eventobj | Add-Member -type NoteProperty -name LDAPAccess -Value (@($ldapconn) -join " ")
    #custom object witout timestamp
    $eventraw = New-Object System.Object
    $eventraw | Add-Member -type NoteProperty -name LDAPAccess -Value (@($ldapconn) -join " ")
    #appending content to array
    $eventarraytime += $eventobj
    $eventarrayraw += $eventraw
        }    
    #output data  in rawformat to csv
    $eventarraytime  | Export-Csv -Path "$csvoutputpath-time.csv" -Encoding UTF8 -Delimiter ";" -NoTypeInformation
    $eventarrayraw  | Export-Csv -Path "$csvoutputpath-raw.csv" -Encoding UTF8 -Delimiter ";" -NoTypeInformation
    #cleaning up double entries
    $eventstimecleared =  $eventarraytime | Sort-Object -Unique LDAPAccess -Descending
    $eventstimecleared | Export-Csv -Path "$csvoutputpath-time-cleared.csv" -Encoding UTF8 -Delimiter ";" -NoTypeInformation
    $eventarraycleared =  $eventarrayraw | Sort-Object -Unique LDAPAccess -Descending
    $eventarraycleared  | Export-Csv -Path "$csvoutputpath-cleared.csv" -Encoding UTF8 -Delimiter ";" -NoTypeInformation
 }

您可以将完整的脚本复制到 ps1 文件中,并使用其参数 -WorkDir“指定复制所有 outpout 和 evtx 文件的路径”来触发它

[玩转系统] Windows Server DomainController 查找 LDAP 绑定

注意:如果 evtx 文件非常大(1-2GB),则此脚本的执行可能需要几个小时,请耐心等待!

如果您是 powershell 专业人士,您可以在此处停止阅读,但如果您想知道它是如何工作的,我会给出一些解释。

第 82 行:
$DCs = ([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites | % { $_.Servers } | 选择名称).Name
这是一个 .Net 函数,用于查找所有 DC 及其 FQDN,此行之后的所有代码都将为变量 $DCs 中的每个 dc 执行(ForEach 循环)

LINE93:
$Events = Get-WinEvent @{Path=$evtxfile;Id=2889}
在 evtx 文件中搜索事件 ID 2889 并将其获取到变量 $Events
此变量可用后,我们需要使用另一个 foreach 循环查看每个事件。

第 100 行:
$eventXML=[xml]$Event.ToXml()
我们需要将事件转换为 XML,以便获得可以通过 PowerShell 搜索和过滤的对象。

LINE 104:
$ldapconn = ($eventXML.Event.EventData.Data)
在这种情况下,ldap 连接的信息位于此对象中(取决于结构)

所有其他代码主要负责转换数据并为 CSV 文件创建可读内容

在我的测试环境中,手动生成的 LDAP 连接很少。

[玩转系统] Windows Server DomainController 查找 LDAP 绑定

IP 和用户用分号分隔,因此在 Excel 中导入和排序应该很容易。

也可以多次运行该脚本,因为它将时间戳添加到每个文件中,不会覆盖任何内容。如果您有任何疑问,请随时与我联系,如果您喜欢这篇文章,请点击“有帮助”。
锁定您的 AD 并关闭 LDAP!

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯