-
Notifications
You must be signed in to change notification settings - Fork 104
/
Install-NetspiAgent.psm1
319 lines (266 loc) · 10.9 KB
/
Install-NetspiAgent.psm1
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
function Install-NetspiAgent {
<#
.SYNOPSIS
This function can be used to install the NetSPI BAS Agent as a scheduled task.
It will do the following:
1. Verify the process and system meets the minimum requirements for the script.
2. Create and execute a scheduled task that will run the agent from a provided location as SYSTEM.
3. The task will start the task upon reboot.
4. The script will also verify that the process has started.
.PARAMETER AgentPath
Full path to the target agent executable.
.PARAMETER TaskName
Task name to be used.
.EXAMPLE
Install-NetspiAgent -AgentPath "C:\agent\NetSPI-BAS-Agent.exe"
Install-NetspiAgent -AgentPath "C:\agent\NetSPI-BAS-Agent.exe" -TaskName "NetSPI-BAS-Agent"
.NOTES
Author: Scott Sutherland (@_nullbind)
Version: 1.0
#>
param (
[Parameter(Mandatory = $true,
HelpMessage = 'Full path to the target agent executable.')]
[string]$AgentPath = "",
[Parameter(Mandatory = $false,
HelpMessage = 'Task name to be used. ')]
[string]$TaskName = "NetSPI-BAS-Agent"
)
begin {
# Simple banner
Write-Output ""
Write-Output "######################################"
Write-Output "NetSPI - www.netspi.com"
Write-Output "Breach and Attack Simluation (BAS)"
Write-Output "Agent Installation Script"
Write-Output "######################################"
Write-Output ""
Write-Output " Verifying Script Requirements"
# Check for admin privileges
function Test-Administrator {
$isElevated = ([System.Security.Principal.WindowsIdentity]::GetCurrent()).Groups -match "S-1-5-32-544"
return $isElevated
}
$isAdmin = Test-Administrator
if ($isAdmin) {
Write-Output " - Verified process has administrative privileges"
} else {
Write-Output "This process is not running with administrative privileges. Please relaunch PowerShell with the correct privileges."
break
}
# Check PowerShell version
$minimumVersion = [Version]"4.0"
$psVersion = $PSVersionTable.PSVersion
if ($psVersion -ge $minimumVersion) {
Write-Output " - Verified PowerShell version ($psVersion) meets min requirements"
} else {
Write-Output " - PowerShell version is older than $minimumVersion. Please run with $minimumVersion or later."
break
}
# Bypass Execution Policy
Set-ExecutionPolicy Bypass -Scope Process -Force
If((Get-ExecutionPolicy -Scope Process) -like "Bypass"){
Write-Output " - Verified PowerShell execution policy bypass"
}else{
Write-Output " - PowerShell execution policy bypass failed"
break
}
# Test path to agent executable
if((Test-Path "$AgentPath")){
Write-Output " - Verified target file exists: $AgentPath "
}else{
Write-Output " - The target file does not exist: $AgentPath "
break
}
# Get exe name
$AgentPathExe = (Get-Item $AgentPath).Name
}
process{
# Set Target Executable
$ExeTarget = "c:\agent\NetSPI-BAS-Agent.exe"
# Set Task Trigger
$trigger = New-ScheduledTaskTrigger -AtStartup
# Set Task Action
$action = New-ScheduledTaskAction -Execute "$AgentPath"
# Register Task
Write-Output " "
Write-Output " Creating scheduled task named '$TaskName'"
Write-Output " $AgentPath"
$null = Register-ScheduledTask -TaskName $taskName -Trigger $trigger -Action $action -User "SYSTEM" -RunLevel Highest -Force
# Run Task
Write-Output " "
Write-Output " Starting scheduled task named '$TaskName'"
Start-ScheduledTask -TaskName $TaskName
Write-Output " - Executed"
# Waiting 5 seconds
Write-Output " - Waiting 5 seconds"
sleep 5
# Check for running task
Write-Output " "
Write-Output " Checking for process '$AgentPathExe'"
$SanityCheck = Get-Process | Where path -like "*$AgentPathExe*"
If ($SanityCheck.count -eq 0){
Write-Output " Verified agent failed to lauch."
break
}else{
Write-Output " - Verified agent successfully launched."
Write-Output " - You should see it checking in."
}
}
end {
}
}
function Remove-NetspiAgent {
<#
.SYNOPSIS
This function can be used to uninstall the NetSPI BAS Agent as a scheduled task.
It will do the following:
1. Verify script requirements.
2. Remove a schedule task based on the provided name.
.PARAMETER TaskName
Task name to be used if it is not the default value "NetSPI-BAS-Agent".
.EXAMPLE
Remove-NetspiAgent -TaskName "NetSPI-BAS-Agent"
.NOTES
Author: Scott Sutherland (@_nullbind)
Version: 1.0
#>
param (
[Parameter(Mandatory = $false,
HelpMessage = 'Name of task to be removed. ')]
[string]$TaskName = "NetSPI-BAS-Agent"
)
begin {
# Simple banner
Write-Output ""
Write-Output "######################################"
Write-Output "NetSPI - www.netspi.com"
Write-Output "Breach and Attack Simluation (BAS)"
Write-Output "Agent Removal Script"
Write-Output "######################################"
Write-Output ""
Write-Output " Verifying Script Requirements"
# Check for admin privileges
function Test-Administrator {
$isElevated = ([System.Security.Principal.WindowsIdentity]::GetCurrent()).Groups -match "S-1-5-32-544"
return $isElevated
}
$isAdmin = Test-Administrator
if ($isAdmin) {
Write-Output " - Verified process has administrative privileges"
} else {
Write-Output "This process is not running with administrative privileges. Please relaunch PowerShell with the correct privileges."
break
}
# Check PowerShell version
$minimumVersion = [Version]"4.0"
$psVersion = $PSVersionTable.PSVersion
if ($psVersion -ge $minimumVersion) {
Write-Output " - Verified PowerShell version ($psVersion) meets min requirements"
} else {
Write-Output " - PowerShell version is older than $minimumVersion. Please run with $minimumVersion or later."
break
}
# Bypass Execution Policy
Set-ExecutionPolicy Bypass -Scope Process -Force
If((Get-ExecutionPolicy -Scope Process) -like "Bypass"){
Write-Output " - Verified PowerShell execution policy bypass"
}else{
Write-Output " - PowerShell execution policy bypass failed"
break
}
}
process{
# Get executable path from task
$task = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
if ($task) {
$actionPath = $task.Actions | Select Execute -ExpandProperty Execute
Write-Output " - Obtained executable path"
Write-Output " $actionPath"
$RootFile = (Get-Item "$actionPath").Name
$RootPath = $actionPath.Replace("$RootFile","")
} else {
Write-Output "Scheduled task '$taskName' not found."
break
}
# Stop the schedule task
if (Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue) {
try {
Stop-ScheduledTask -TaskName $taskName
# Check if the task is no longer running
$isRunning = (Get-ScheduledTask -TaskName $taskName).State -eq "Running"
if (-Not $isRunning) {
Write-Output " - Scheduled task '$taskName' has been stopped."
} else {
Write-Output " - Failed to stop the scheduled task '$taskName'."
}
} catch {
Write-Output "Error occurred while trying to stop the scheduled task: $_"
}
} else {
Write-Output " - Scheduled task '$taskName' not found."
break
}
# Remove scheduled task
if (Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue) {
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
Write-Output " - Scheduled task '$TaskName' has been removed."
} else {
Write-Output " - Scheduled task '$TaskName' not found."
break
}
# Stop related processes
$TargetProcesses = Get-Process | where path -like "$actionPath"
$TargetProcesses |
foreach {
$TargetId = $_.Id
Stop-Process -Id $TargetId -Force
}
$TargetProcesses = Get-Process | where path -like "$actionPath"
if(($TargetProcesses.count) -eq 0){
Write-Output " - Confirmed processes were terminated."
}else{
Write-Output " - Unable to terminate related processes."
}
# Remove exe
if (Test-Path $actionPath) {
try {
# Attempt to remove the file
Remove-Item $actionPath -Force
# Check if the file was successfully removed
if (-Not (Test-Path $actionPath)) {
Write-Output " - File removed successfully: $actionPath"
} else {
Write-Output " - Failed to remove the file: $actionPath"
}
} catch {
Write-Output " - Error occurred while trying to remove the file: $_"
}
} else {
Write-Output " - File not found: $actionPath"
}
# Remove config
$RootPathFull = "$RootPath" + "bas_agent_profile*.json"
if (Test-Path $RootPathFull ) {
try {
# Attempt to remove the file
Remove-Item "$RootPathFull" -Force
# Check if the file was successfully removed
if (-Not (Test-Path $RootPathFull)) {
Write-Output " - File removed successfully: $RootPathFull"
} else {
Write-Output " - Failed to remove the file: $RootPathFull"
}
} catch {
Write-Output " - Error occurred while trying to remove the file: $_"
}
} else {
Write-Output " - File not found: $RootPathFull"
}
Write-Output ""
Write-Output "PLEASE NOTE:"
Write-Output "YOU MAY HAVE TO SHUTDOWN THE IN MEMORY AGENT THROUGH THE AGENTS DASHBOARD."
}
end {
}
}