RDP登录事件详细溯源分析脚本(兼容Windows PowerShell版本)
脚本功能详解
- RDP登录事件提取
从Windows安全日志中筛选事件ID 4624(成功登录)且登录类型为10(远程交互/RDP)的事件
提取关键信息:时间、账户名、源IP、认证方式、进程名和会话ID - IP情报分析
地理位置查询:使用ip-api.com 免费API获取国家、城市、ISP等信息
WHOIS查询:模拟查询IP注册信息(需配置有效API密钥)
威胁情报检查:模拟查询AbuseIPDB等威胁情报源(需配置有效API密钥) - 网络警察方法模拟
证据链构建:完整记录所有登录事件的时间线和上下文
ISP协作:通过WHOIS信息识别网络服务提供商
威胁评分:基于公开威胁情报评估风险等级
调查流程:提供标准网络犯罪调查流程参考 - HTML报告生成
专业排版的可视化报告
颜色编码的风险等级标识
地理分布可视化(需实际部署时添加Leaflet.js )
调查建议和后续步骤
实际应用注意事项
API密钥配置:脚本中的WHOIS和威胁情报查询需要有效API密钥才能工作
法律合规:
在真实环境中查询IP注册信息需遵守相关法律法规
非执法机构可能无法获取完整的注册用户信息
数据隐私:
处理包含个人数据的日志时需遵守GDPR等数据保护法规
报告应妥善保管,防止敏感信息泄露
扩展功能:
可添加VT(VirusTotal)等更多威胁情报源查询
可集成企业内部的SIEM系统数据
可添加自动化的威胁响应动作(如防火墙封锁)
<#
.SYNOPSIS
RDP登录事件详细溯源分析脚本(兼容Windows PowerShell版本).DESCRIPTION
此脚本兼容Windows PowerShell 5.1,分析本机安全日志中的真实RDP登录事件,
提取详细信息并进行网络定位,结果导出为HTML报告。.NOTES
文件名: RDP_Login_Forensics_Compatible.ps1
更新日期: 2025年11月13日
版本: 1.3
#># 初始化报告路径
$reportPath = "$env:USERPROFILE\Desktop\RDP_Login_Forensics_Compatible_$(Get-Date -Format 'yyyyMMdd_HHmmss').html"# HTML报告头部
$htmlHeader = @"
<!DOCTYPE html>
<html>
<head><title>RDP登录事件溯源报告(兼容版) - $env:COMPUTERNAME</title><style>body { font-family: 'Segoe UI', Arial, sans-serif; margin: 20px; color: #333; }h1 { color: #2b579a; border-bottom: 1px solid #2b579a; padding-bottom: 10px; }h2 { color: #3a6ea5; margin-top: 25px; }table { border-collapse: collapse; width: 100%; margin-bottom: 20px; box-shadow: 0 1px 3px rgba(0,0,0,0.1); }th { background-color: #2b579a; color: white; padding: 12px; text-align: left; }td { padding: 10px; border: 1px solid #ddd; }tr:nth-child(even) { background-color: #f2f2f2; }.critical { background-color: #ffdddd; }.warning { background-color: #fff3cd; }.info { background-color: #d4edda; }.ip-info { background-color: #e7f3fe; margin-bottom: 20px; padding: 15px; border-left: 5px solid #2b579a; }.timestamp { color: #6c757d; font-size: 0.9em; }.no-data { color: #dc3545; font-weight: bold; }</style>
</head>
<body><h1>RDP登录事件溯源分析报告(兼容版)</h1><p class="timestamp">报告生成时间: $(Get-Date -Format "yyyy-MM-dd HH:mm:ss")</p><p>计算机名称: $env:COMPUTERNAME</p>
"@ # 1. 查询真实RDP登录事件
function Get-RdpEvents {try {$events = Get-WinEvent -FilterHashtable @{LogName='Security'ID=4624 StartTime=(Get-Date).AddDays(-30)} -MaxEvents 100 -ErrorAction Stop | Where-Object {$_.Properties[8].Value -eq 10 # 登录类型10 = 远程交互(RDP)} | Select-Object @{Name='TimeCreated'Expression={$_.TimeCreated}}, @{Name='Account'Expression={$_.Properties[5].Value}}, @{Name='SourceIP'Expression={$_.Properties[18].Value}}, @{Name='LogonType'Expression={$_.Properties[8].Value}}, @{Name='AuthenticationPackage'Expression={$_.Properties[10].Value}}, @{Name='ProcessName'Expression={$_.Properties[17].Value}}, @{Name='SessionID'Expression={$_.Properties[3].Value}}return $events }catch {Write-Warning "查询安全日志时出错: $_"return @()}
}$rdpEvents = Get-RdpEvents # 2. IP情报收集函数(兼容旧版PowerShell)
function Get-IPInformation {param ([string]$ip )$result = @{IP = $ip Country = "未知"Region = "未知"City = "未知"ISP = "未知"Organization = "未知"ASN = "未知"ThreatLevel = "信息"}if ($ip -eq "::1" -or $ip -eq "127.0.0.1" -or $ip -match '^(10\.|172\.(1[6-9]|2[0-9]|3[0-1])\.|192\.168\.)') {$result.Country = "本地网络"$result.Region = "内部地址"$result.ISP = "内部网络"return [PSCustomObject]$result }try {$geoResponse = Invoke-RestMethod -Uri "http://ip-api.com/json/$ip" -ErrorAction Stop Start-Sleep -Milliseconds 100 if ($geoResponse -and $geoResponse.status -eq "success") {$result.Country = if ($geoResponse.country) { $geoResponse.country } else { "未知" }$result.Region = if ($geoResponse.regionName) { $geoResponse.regionName } else { "未知" }$result.City = if ($geoResponse.city) { $geoResponse.city } else { "未知" }$result.ISP = if ($geoResponse.isp) { $geoResponse.isp } else { "未知" }$result.Organization = if ($geoResponse.org) { $geoResponse.org } else { "未知" }$result.ASN = if ($geoResponse.as) { $geoResponse.as } else { "未知" }}}catch {Write-Warning "IP查询失败: $_"}return [PSCustomObject]$result
}# 3. 处理IP信息
$ipAnalysisResults = @()
if ($rdpEvents) {foreach ($event in $rdpEvents) {$ip = $event.SourceIP if (-not $ip -or $ip -eq "-") { continue }$ipAnalysisResults += Get-IPInformation -ip $ip }
}# 4. 生成HTML报告内容
$htmlBody = @"<h2>RDP登录事件概览</h2>
"@ if ($rdpEvents) {$htmlBody += @"<p>共发现 $($rdpEvents.Count) 条真实RDP登录事件</p><table><tr><th>时间</th><th>账户</th><th>来源IP</th><th>认证方式</th><th>进程名</th><th>会话ID</th></tr>
"@ foreach ($event in $rdpEvents) {$htmlBody += @"<tr><td>$($event.TimeCreated)</td><td>$($event.Account)</td><td>$($event.SourceIP)</td><td>$($event.AuthenticationPackage)</td><td>$($event.ProcessName)</td><td>$($event.SessionID)</td></tr>
"@ }$htmlBody += @"</table>
"@
} else {$htmlBody += @"<p class="no-data">未在安全日志中找到RDP登录事件</p><p>可能原因:<ul><li>系统未启用远程桌面服务</li><li>过去30天内无远程登录记录</li><li>当前账户无权限访问安全日志</li></ul></p>
"@
}if ($ipAnalysisResults) {$htmlBody += @"<h2>IP地址分析</h2><p>以下是对发现的来源IP地址的基础分析:</p>
"@ foreach ($result in $ipAnalysisResults) {$threatClass = switch ($result.ThreatLevel) {"高危" { "critical" }"中危" { "warning" }"低危" { "info" }default { "" }}$htmlBody += @"<div class="ip-info"><h3>IP地址: $($result.IP)</h3><table class="$threatClass"><tr><th>地理位置</th><td>$($result.City), $($result.Region), $($result.Country)</td></tr><tr><th>网络提供商</th><td>$($result.ISP) (AS$($result.ASN))</td></tr><tr><th>注册组织</th><td>$($result.Organization)</td></tr></table></div>
"@ }
}$htmlBody += @"<h2>后续行动建议</h2><ul><li>使用<strong>PowerShell 7+</strong>版本可获得更完整的威胁情报分析</li><li>配置防火墙规则限制RDP端口(默认3389)的访问来源</li><li>启用<strong>网络级别认证(NLA)</strong>提高安全性</li><li>定期审计<strong>安全日志</strong>中的异常登录活动</li></ul>
"@ # HTML报告尾部
$htmlFooter = @"<footer style="margin-top: 30px; padding-top: 10px; border-top: 1px solid #ddd; color: #666;"><p>报告生成工具: RDP登录溯源分析脚本(兼容版) v1.3</p><p>生成于: $(Get-Date -Format "yyyy年M月d日 HH:mm") 农历乙巳年九月廿四</p></footer>
</body>
</html>
"@ # 合并并输出HTML报告
$htmlContent = $htmlHeader + $htmlBody + $htmlFooter
$htmlContent | Out-File -FilePath $reportPath -Encoding UTF8 # 打开生成的报告
try {Start-Process $reportPath Write-Host "RDP登录溯源报告(兼容版)已生成: $reportPath" -ForegroundColor Green
}
catch {Write-Host "报告已生成到桌面但无法自动打开,请手动访问。" -ForegroundColor Yellow
}
