/
picnicbasket.boxstarter
257 lines (191 loc) · 8.18 KB
/
picnicbasket.boxstarter
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
Write-Verbose "$(Get-Date -format g) Creating (or emptying) top directory c:\opt"
Remove-Item -Recurse -ErrorAction continue -Force -Path c:\opt
New-Item -Path c:\opt -Type directory -Force -ErrorAction stop
Write-Verbose "$(Get-Date -format g) Unzipping PicnicBasket to c:\opt"
$shell = new-object -com shell.application
[Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem")
[System.AppDomain]::CurrentDomain.GetAssemblies()
[System.IO.Compression.ZipFile]::ExtractToDirectory((Get-Item "c:\PicnicBasket\opt1.0.1.zip"), (Get-Item "c:\"))
Write-Verbose "$(Get-Date -format g) Changing c:\opt\** ownership"
function Set-LHSTokenPrivilege
{
<#
.SYNOPSIS
Enables or disables privileges in a specified access token.
.DESCRIPTION
Enables or disables privileges in a specified access token.
.PARAMETER Privilege
The privilege to adjust. This set is taken from
http://msdn.microsoft.com/en-us/library/bb530716(VS.85).aspx
.PARAMETER $ProcessId
The process on which to adjust the privilege. Defaults to the current process.
.PARAMETER Disable
Switch to disable the privilege, rather than enable it.
.EXAMPLE
Set-LHSTokenPrivilege -Privilege SeRestorePrivilege
To set the 'Restore Privilege' for the current Powershell Process.
.EXAMPLE
Set-LHSTokenPrivilege -Privilege SeRestorePrivilege -Disable
To disable 'Restore Privilege' for the current Powershell Process.
.EXAMPLE
Set-LHSTokenPrivilege -Privilege SeShutdownPrivilege -ProcessId 4711
To set the 'Shutdown Privilege' for the Process with Process ID 4711
.INPUTS
None to the pipeline
.OUTPUTS
System.Boolean, True if the privilege could be enabled
.NOTES
to check privileges use whoami
PS:\> whoami /priv
PRIVILEGES INFORMATION
----------------------
Privilege Name Description State
============================= ==================================== ========
SeShutdownPrivilege Shut down the system Disabled
SeChangeNotifyPrivilege Bypass traverse checking Enabled
SeUndockPrivilege Remove computer from docking station Disabled
SeIncreaseWorkingSetPrivilege Increase a process working set Disabled
AUTHOR: Pasquale Lantella
LASTEDIT:
KEYWORDS: Token Privilege
.LINK
http://www.leeholmes.com/blog/2010/09/24/adjusting-token-privileges-in-powershell/
The privilege to adjust. This set is taken from
http://msdn.microsoft.com/en-us/library/bb530716(VS.85).aspx
pinvoke AdjustTokenPrivileges (advapi32)
http://www.pinvoke.net/default.aspx/advapi32.AdjustTokenPrivileges
#Requires -Version 2.0
#>
[cmdletbinding(
ConfirmImpact = 'low',
SupportsShouldProcess = $false
)]
[OutputType('System.Boolean')]
Param(
[Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$False)]
[ValidateSet(
"SeAssignPrimaryTokenPrivilege", "SeAuditPrivilege", "SeBackupPrivilege",
"SeChangeNotifyPrivilege", "SeCreateGlobalPrivilege", "SeCreatePagefilePrivilege",
"SeCreatePermanentPrivilege", "SeCreateSymbolicLinkPrivilege", "SeCreateTokenPrivilege",
"SeDebugPrivilege", "SeEnableDelegationPrivilege", "SeImpersonatePrivilege", "SeIncreaseBasePriorityPrivilege",
"SeIncreaseQuotaPrivilege", "SeIncreaseWorkingSetPrivilege", "SeLoadDriverPrivilege",
"SeLockMemoryPrivilege", "SeMachineAccountPrivilege", "SeManageVolumePrivilege",
"SeProfileSingleProcessPrivilege", "SeRelabelPrivilege", "SeRemoteShutdownPrivilege",
"SeRestorePrivilege", "SeSecurityPrivilege", "SeShutdownPrivilege", "SeSyncAgentPrivilege",
"SeSystemEnvironmentPrivilege", "SeSystemProfilePrivilege", "SeSystemtimePrivilege",
"SeTakeOwnershipPrivilege", "SeTcbPrivilege", "SeTimeZonePrivilege", "SeTrustedCredManAccessPrivilege",
"SeUndockPrivilege", "SeUnsolicitedInputPrivilege")]
[String]$Privilege,
[Parameter(Position=1)]
$ProcessId = $pid,
[Switch]$Disable
)
BEGIN {
Set-StrictMode -Version Latest
${CmdletName} = $Pscmdlet.MyInvocation.MyCommand.Name
## Taken from P/Invoke.NET with minor adjustments.
$definition = @'
using System;
using System.Runtime.InteropServices;
public class AdjPriv
{
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int SE_PRIVILEGE_DISABLED = 0x00000000;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
public static bool EnablePrivilege(long processHandle, string privilege, bool disable)
{
bool retVal;
TokPriv1Luid tp;
IntPtr hproc = new IntPtr(processHandle);
IntPtr htok = IntPtr.Zero;
retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
tp.Count = 1;
tp.Luid = 0;
if(disable)
{
tp.Attr = SE_PRIVILEGE_DISABLED;
}
else
{
tp.Attr = SE_PRIVILEGE_ENABLED;
}
retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid);
retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
return retVal;
}
}
'@
} # end BEGIN
PROCESS {
$processHandle = (Get-Process -id $ProcessId).Handle
$type = Add-Type $definition -PassThru
$type[0]::EnablePrivilege($processHandle, $Privilege, $Disable)
} # end PROCESS
END { Write-Verbose "Function ${CmdletName} finished." }
} # end Function Set-LHSTokenPrivilege
function Set-Ownership
{
[cmdletbinding(
ConfirmImpact = 'low',
SupportsShouldProcess = $false
)]
[OutputType('System.Boolean')]
Param(
[String]$UserToReceiveOwnership,
[String]$FileSystemObject,
$ProcessId = $pid
)
BEGIN {
Set-StrictMode -Version Latest
${CmdletName} = $Pscmdlet.MyInvocation.MyCommand.Name
} # end BEGIN
PROCESS {
$processHandle = (Get-Process -id $ProcessId).Handle
Write-Verbose "$(Get-Date -format g) Establishing $UserToReceiveOwnership as owner of $FileSystemObject"
Set-LHSTokenPrivilege -Privilege SeRestorePrivilege
$UserToReceiveOwnershipSecurityPrincipal = new-object System.Security.Principal.NTAccount($UserToReceiveOwnership)
Get-ChildItem -LiteralPath $FileSystemObject -recurse -force |
ForEach {
$fullpath = Get-Item -LiteralPath (Get-Item -LiteralPath $_.PSPath)
$aclobject = (Get-Item -LiteralPath $fullpath).GetAccessControl()
$aclobject.SetOwner($UserToReceiveOwnershipSecurityPrincipal)
set-acl -aclobject $aclobject -LiteralPath $fullpath.FullName
Write-Verbose "Took ownership of $fullpath.FullName"
}
} # end PROCESS
END { Write-Verbose "Function ${CmdletName} finished." }
} # end Function Set-Ownership
# Function definition, needed for a few installers that don't create their own desktop shortcuts.
# Puts the shortcut on the Public Desktop for everyone to use.
function Create-DesktopShortcut {
[CmdletBinding()] Param(
[Parameter(Mandatory=$True)] [string]$shortcutName,
[Parameter(Mandatory=$True)] [string]$targetPath
)
$objShell = New-Object -ComObject WScript.Shell
$objShortcut = $objShell.CreateShortcut($env:HOMEDRIVE + "\Users\Public\Desktop\" + $shortcutName + ".lnk")
$objShortcut.TargetPath = $targetPath
$objShortcut.Save()
Remove-Variable objShortcut
[Runtime.Interopservices.Marshal]::ReleaseComObject($objShell)
Remove-Variable objShell
}
$Success = Set-Ownership JDev 'c:\opt'
Write-Verbose "Set-Ownership success = $Success"
Write-Verbose "Create shortcut"
Create-DesktopShortcut -shortcutName "MyEclipse" -targetPath "C:\opt\el\eclipse\myeclipse.exe"
Write-Verbose "Created shortcut"