powershell在应急响应中的应用

2022-11-23 16:31:49 11 11475


0x00 powershell的作用
   今天发一篇关于powershell在应急中的一些应用小技巧。Windows PowerShell 是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用 .NET Framework的强大功能,目前应急中的大部分windows系统是带有powershell功能的, 并且在应急过程中,利用powershell的功能,可以在应急中起到事半功倍的作用,我把应急中常用的powershell功能进行了归纳。
0x01 应急排查中powershel的应用
应急响应作为网络安全工作中的一项,依据应急场景对安全事件进行排查、抑制、根除及溯源工作。下面列举一下在应急过程中,常见的使用powershell的方式。
1.进程排查
在应急中对于有守护进程的进程,需要理清楚子父进程之间的关系,可以使用powershell进行查看,一般powershell在查询的时候会调用WMI对象。如Get-WmiObject Win32_Process | select Name,ProcessId,ParentProcessId,Path

2.文件排查
一般windows下可以使用forfiles的使用方式对某一时间节点的文件进行查找,如可以使用下面的命令组合对2020/2/12后的exe新建文件进行搜索。
forfiles /m *.exe /d +2020/2/12 /s /p c:\  /c "cmd /c echo @path @fdate @ftime" 2>null

同时可以使用powershell对文件进行检索,如对2022.2.1以后生成的exe文件进行检索
Get-ChildItem -Path D:\ -Force -Recurse -Include *.exe -ErrorAction:SilentlyContinue | `Where-Object -FilterScript {($_.CreationTime -gt "2022-02-01")} | Select-Object FullName

    查询指定时间内的事件,也可以通过设置起终止时间变量来进行查询。

同时可以使用powershell对指定md5的文件进行检索,如已知恶意文件md5值为C82C56C1B0CD5604C7E07B3FA0B4AFDA
Get-FileHash .\*.exe -Algorithm md5 | Where -Property Hash -in -Value "C82C56C1B0CD5604C7E07B3FA0B4AFDA" 

3.计划任务排查
Powershell下使用Get-ScheduledTask,可以查看当前系统下所有的计划任务信息。

4.日志排查
    在使用powershell进行日志分析的时候,需要有管理员权限才可以对日志进行操作。
通过powershell进行查询最常用到两个命令是Get-EventLog和Get-WinEvent,两者的区别是Get-EventLog只获取传统的事件日志,而Get-WinEvent cmdlet 从包括传统日志(例如系统日志和应用程序日志)在内的事件日志和 Windows Vista 中引入的新 Windows 事件日志技术生成的事件日志中获取事件。它还获取 Windows 事件跟踪(ETW) 生成的日志文件中的事件。但是注意Get-WinEvent:Get-WinEvent 要求 Windows Vista、Windows Server 2008 R2 或更高版本的 Windows,并且要求 Microsoft .NET Framework 3.5及以上的版本。总体来说就是Get-WinEvent功能更强大,但是对系统和.NET的版本有要求。
列举一下windows下常用的一些
获取安全日志下事件ID为4625(登陆失败)的所有日志信息。
Get-EventLog Security -InstanceId 4625

5.恶意服务检测
  对已知的恶意服务进行检测
Get-WmiObject win32_service |?{ $_.name -eq 'svchost.exe' -and $_.PathName -notlike  '*C:\WINDOWS\System32\svchost.exe*' -and $_.PathName -not
like '*c:\Windows\SysWOW64\svchost.exe*'} | select Name, DisplayName, State, PathName
Get-WmiObject win32_service | ?{$_.PathName -like '*svchost.exe*'} | select Name, DisplayName, @{Name="Path"; Expression={$_.PathName.split('
')[0]}} | Format-List


0x02 powershell进行编解码
    在应急过程中经常会遇到攻击者使用powershell的方式进行权限维持或是代码注入执行等情况,常见的就是powershell -enc 方式执行,这时候需要对脚本进行解码,才能进一步进行应急处理。powershell的编解码的方式如下:
解码
[System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String("UnicodeBase64编码放到此处"))
编码
[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes("任意字符串放此处"))

应急中,有时候还会遇到压缩的恶意样本,可以使用gzip进行解码
gzip解码
function Get-gzip{
Param($base64data)
$data = [System.Convert]::FromBase64String($base64data)
$ms = New-Object System.IO.MemoryStream
$ms.Write($data, 0, $data.Length)
$ms.Seek(0,0) | Out-Null

$sr = New-Object System.IO.StreamReader(New-Object System.IO.Compression.GzipStream($ms, [System.IO.Compression.CompressionMode]::Decompress))

while ($line = $sr.ReadToEnd()) {  
    $line
    }
}

0x03 使用powershell脚本进行批量清理
  在应急的过程中,经常会遇到网络中有很多台机器中招,需要对多台机器进行清理,如果一台台的去处理很是浪费时间,这时候可以利用powershell进行批量处理,如果网络中有类似天擎这样的中控端,可以把写好的脚本依靠天擎统一运行处理。
  在需要对机器进行清理的时候,主要需要处理的就是以下几个方面:恶意进程、服务、计划任务、注册表项、恶意文件。这里把各个模块写一个简单的例子,按照实际情况组合一下就可以,原则就是复杂一点的可以写函数,简单的可以直接执行命令就行。在把样本的恶意特征理清楚后,可以把各个部分组合起来就是一个powershell版的专杀工具了。
  一般来说,对于恶意文件的删除可以通过md5计算,之后与恶意md5比对的方式进行删除。
  对文件夹中md5进行计算可以写成函数
function _file_md5{
   Param($path)
   if($path -ne $null){
       if(Test-Path $path -PathType Leaf ){
           try{
               $fs=(new-object system.io.fileinfo($path)).openread();
               $r=(new-object system.security.cryptography.md5cryptoserviceprovider).computehash($fs);$fs.close();
               [string]$hash=[bitconverter]::tostring($r).replace('-','')  
           }catch{}
           $retVal = New-Object -TypeName psobject -Property @{
               Path = $path
               Hash = $hash
           }
           return $retVal
       }
   } else{
       write-host [-] '_file_md5' $path -ForegroundColor Red
   }
}
对匹配到的md5文件进行删除
function _del_if_match {
   Param (
       $path
   )
   $md5info = _file_md5 $path
   if ($null -ne $md5info) {
       if ($evilHashes -eq $md5info.Hash) {
           write-host [-] 'deleting' $path -ForegroundColor Red
           Remove-Item -Path $path -Force
       }  
   }
}
删除服务
cmd.exe /c "sc.exe delete WebServers 2>nul>nul" 
删除启动项
write-host [i] delete startup -ForegroundColor Green
Get-ChildItem -Path 'C:\Users\*\AppData\Roaming\Microsoft\Windows\*' -force -Recurse  -ErrorAction 0 | ?{$_.name -eq "run.bat"} |Remove-Item
删除注册表值
write-host [i] delete Registry -ForegroundColor Green
Remove-ItemProperty "HKlM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" -name "键值名" -Force -ErrorAction 0
删除进程
write-host [i] delete process -ForegroundColor Green
wmic process where "name='svchost.exe' and ExecutablePath='C:\\windows\\system32\\drivers\\svchost.exe'" call Terminate |Out-Null
wmic process where "name='taskmgr.exe' and ExecutablePath='C:\\windows\\system32\\drivers\\taskmgr.exe'" call Terminate |Out-Null
删除计划任务
 计划任务及上面提到的恶意进程,有时候会遇到是随机计划任务名或是随机进程名的时候,这个时候可以找到命名的一般规律,之后利用正则表达式的方式把相关的恶意计划任务或是进程名进行删除。
schtasks /query /FO CSV /v | ConvertFrom-CSV| where {$_."Task To Run" -match 'powershell.*-e.*?\b(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\b'} |%{write-host write-host [-] 'Deleting Task:' $_.TaskName  -ForegroundColor Red ;schtasks /delete /tn $_.taskname /f}

0x04 总结
  powershell是个强大的脚本工具,不再是单单命令行工具,它是一门语言。随着无文件的攻击方式的兴起,powershell在攻防中用的越来越多,这次把powershell在应急中的一些用法归拢了一下,希望大家有其他奇淫技巧的随时交流。本来还有一些总结的常用的windows的事件ID表,但是一粘贴进来就变文字了,我把完整文章附在附件里面了。
TCV=1

关于作者

Key204篇文章110篇回复

评论11次

要评论?请先  登录  或  注册