-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInvoke-AsSystem.ps1
More file actions
149 lines (126 loc) · 5.39 KB
/
Copy pathInvoke-AsSystem.ps1
File metadata and controls
149 lines (126 loc) · 5.39 KB
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
Function Invoke-AsSystem {
<#
.SYNOPSIS
Will run a scriptblock with in the context of NT Authority\SYSTEM.
.DESCRIPTION
The function installs a service and runs it under the NT Authority\SYSTEM credentials.
.PARAMETER Process
The PROCESS script block to invoked as a service.
.PARAMETER Begin
The BEGIN script block to invoked as a service.
.PARAMETER End
The END script block to invoked as a service.
.PARAMETER Depth
Objects passed on the pipeline to the function will be serialized temporarily to disk with the Export-CliXml cmdlet.
The parameter specifies how many levels of objects passed in on the pipeline are included in the XML representation.
The default value is 3.
.EXAMPLE
Invoke-AsSystem -Process {whoami /all}
This gives us the whoami /all result from the execution context as NT Authority\SYSTEM
.EXAMPLE
"C:\System Volume Information" | Invoke-AsSystem {Get-ChildItem $_ -Force -Recurse}
Pass a path into Invoke-AsSyetm through the pipeline.
#>
param(
[Parameter(Mandatory=$true)]
[scriptblock]$Process={},
[scriptblock]$Begin={},
[scriptblock]$End={},
[parameter(ValueFromPipeline=$true)]
[object]$InputObject=$null,
[int]$Depth = 3
)
begin {
Function Test-Elevated {
$wid=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$prp=new-object System.Security.Principal.WindowsPrincipal($wid)
$adm=[System.Security.Principal.WindowsBuiltInRole]::Administrator
$prp.IsInRole($adm)
}
$code = @"
using System;
using System.ServiceProcess;
namespace GetRandom.Powershell.InvokeAsSystemSvc
{
class TempPowershellService : ServiceBase
{
static void Main()
{
ServiceBase.Run(new ServiceBase[] { new TempPowershellService() });
}
protected override void OnStart(string[] args)
{
string[] clArgs = Environment.GetCommandLineArgs();
try
{
string argString = String.Format(
"-command .{{import-clixml '{0}' | .'{1}' | export-clixml -Path '{2}' -Depth {3}}}",
clArgs[1],
clArgs[2],
clArgs[3],
clArgs[5]);
System.Diagnostics.Process.Start("powershell", argString).WaitForExit();
System.IO.File.AppendAllText(clArgs[4], "success");
}
catch (Exception e)
{
System.IO.File.AppendAllText(clArgs[4], "fail\r\n" + e.Message);
}
}
protected override void OnStop()
{
}
}
}
"@
if( -not (Test-Elevated)) {
throw "Process is not running as an eleveated process. Please run as elevated."
}
[void][System.Reflection.Assembly]::LoadWithPartialName("System.ServiceProcess")
$serviceNamePrefix = "MyTempPowershellSvc"
$timeStamp = get-date -Format yyyyMMdd-HHmmss
$serviceName = "{0}-{1}" -f $serviceNamePrefix,$timeStamp
$tempPSexe = "{0}.exe" -f $serviceName,$timeStamp
$tempPSout = "{0}.out" -f $serviceName,$timeStamp
$tempPSin = "{0}.in" -f $serviceName,$timeStamp
$tempPSscr = "{0}.ps1" -f $serviceName,$timeStamp
$tempPScomplete = "{0}.end" -f $serviceName,$timeStamp
$servicePath = Join-Path $env:temp $tempPSexe
$outPath = Join-Path $env:temp $tempPSout
$inPath = Join-Path $env:temp $tempPSin
$scrPath = Join-Path $env:temp $tempPSscr
$completePath = Join-Path $env:temp $tempPScomplete
$serviceImagePath = "`"{0}`" `"{1}`" `"{2}`" `"{3}`" `"{4}`" {5}" -f $servicePath,$inPath,$scrPath,$outPath,$completePath,$depth
Add-Type $code -ReferencedAssemblies "System.ServiceProcess" -OutputAssembly $servicePath -OutputType WindowsApplication | Out-Null
$objectsFromPipeline = new-object Collections.ArrayList
$script = "BEGIN {{{0}}}`nPROCESS {{{1}}}`nEND {{{2}}}" -f $Begin.ToString(),$Process.ToString(),$End.ToString()
$script.ToString() | Out-File -FilePath $scrPath -Force
}
process {
[void]$objectsFromPipeline.Add($InputObject)
}
end
{
$objectsFromPipeline | Export-Clixml -Path $inPath -Depth $Depth
New-Service -Name $serviceName -BinaryPathName $serviceImagePath -DisplayName $serviceName -Description $serviceName -StartupType Manual | out-null
$service = Get-Service $serviceName
$service | Start-Service
while ( -not (test-path $completePath)) {
Write-Progress -Activity "Executing"
start-sleep -Milliseconds 100
}
$service | Stop-Service -Force
do {
Write-Progress -Activity "Stopping temporary services $serviceName"
$service = Get-Service $serviceName
} while($service.Status -ne "Stopped")
$wmiSvcObj = Get-WmiObject win32_service -Filter "name='$serviceName'"
$wmiSvcObj.delete() | Out-Null
Import-Clixml -Path $outPath
Remove-Item $servicePath -Force -ErrorAction Continue
Remove-Item $inPath -Force -ErrorAction Continue
Remove-Item $outPath -Force -ErrorAction Continue
Remove-Item $scrPath -Force -ErrorAction Continue
Remove-Item $completePath -Force -ErrorAction Continue
}
}