域渗透——Adsisearcher和ADSI初探
字数 1190 2025-08-09 15:23:10
Active Directory 渗透测试:Adsisearcher 和 ADSI 深度指南
1. Adsisearcher 和 ADSI 概述
1.1 Adsisearcher 简介
Adsisearcher 是 PowerShell 中的类型加速器(Type Accelerator),直接指向 .NET 的 DirectoryServices.DirectorySearcher 类。关键特性:
- 内置性:存在于所有 Windows 环境中,无需额外安装
- 功能:通过 LDAP 查询枚举 Active Directory 对象
- 优势:相比 PowerView 等工具更具隐蔽性
1.2 ADSI 简介
ADSI 是另一个类型加速器,指向 DirectoryServices.DirectoryEntry 类:
- 用于查询特定 AD 对象的详细信息
- 常与 Adsisearcher 配合使用(先用 Adsisearcher 发现对象,再用 ADSI 深入查询)
2. 环境准备与验证
在执行操作前,应先验证环境:
# 检查语言模式(FullLanguage 才能执行所有命令)
$ExecutionContext.SessionState.LanguageMode
# 检查 PowerShell 版本
Get-Host | select version
3. Adsisearcher 深入使用
3.1 创建 Adsisearcher 对象
$test = new-object adsisearcher
验证类型:
$test.GetType()
3.2 关键属性解析
SearchRoot 属性
# 查询域名称和路径
$test.SearchRoot
# 查询域控制器名称
$test.SearchRoot.dc
# 查询前10个域成员
$test.SearchRoot.Children | select -first 10
Filter 属性
使用 LDAP 过滤语法:
# 过滤用户和组对象
$test.Filter = "(|(objectclass=user)(objectclass=group))"
# 查找所有管理员账户(admincount=1)
$test.Filter = "(admincount=1)"
# 执行查询
$test.FindAll()
两种查询方法:
FindOne()- 返回第一个匹配结果FindAll()- 返回所有匹配结果
PropertiesToLoad 属性
控制返回的属性,提高查询效率:
# 指定只返回cn和admincount属性
$test.PropertyToLoad.Add("cn")
$test.PropertyToLoad.Add("admincount")
# 过滤用户对象
$test.Filter = "(objectclass=user)"
# 执行查询
$test.FindAll()
4. ADSI 深入使用
4.1 创建 ADSI 对象
# 先使用Adsisearcher找到目标路径
$test.Filter = "(cn=目标用户)"
$userPath = ($test.FindAll()).Path
# 创建ADSI对象
$ADSI = [ADSI]$userPath
4.2 查询特定属性
# 查询常用属性
$ADSI.cn
$ADSI.memberOf
$ADSI.objectCategory
$ADSI.objectClass
$ADSI.Parent
5. 实战渗透技巧
5.1 成员信息收集
发现域成员后深入查询:
$test.Filter = "(cn=admintest)"
$test.FindAll()
($test.FindAll()).Properties
5.2 通用域枚举技术
列举用户和组
# 列举所有用户
$test.Filter = "(&(objectclass=user)(givenname=*))"
# 列举所有组
$test.Filter = "(&(samaccounttype=268435456))"
查找SQL服务器
$test.Filter = "(&(cn=MSSQL*))"
$test.FindAll()
查询密码策略
$ADSI = [ADSI]"LDAP://DC=域名称,DC=后缀"
$ADSI | Format-List *pwd*,*lockout*
6. 高级技巧与注意事项
- LDAP 查询语法:熟练掌握 LDAP 过滤语法可大幅提高查询效率
- 属性选择:合理使用 PropertiesToLoad 减少网络流量和提升速度
- 结果处理:PowerShell 的 Select-String 可用于结果过滤
- 隐蔽性:相比 PowerView 等工具,Adsisearcher 产生的日志更少
- 权限要求:不同查询需要不同的 AD 权限,普通用户权限通常足够基本信息收集
7. 防御建议
- 监控异常 LDAP 查询:特别是来自非管理员的频繁查询
- 限制敏感属性读取:如 admincount 等敏感属性
- 加强日志收集:记录 PowerShell 活动,特别是 Adsisearcher 使用情况
- 实施最小权限原则:减少普通用户对 AD 的读取权限
8. 参考资源
- Microsoft 官方 Adsisearcher 文档
- Alkane Solutions 的 ADSI 查询指南
- LDAP 过滤语法官方文档