-
-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathinstaller.ps1
More file actions
272 lines (229 loc) · 9.37 KB
/
installer.ps1
File metadata and controls
272 lines (229 loc) · 9.37 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
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
# Coercer Installer
$PythonVersion = '3.13.0'
$StartingDirectory = Get-Location
$PythonInstallerPath = Join-Path -Path $Env:TEMP -ChildPath "python-$PythonVersion.exe"
$RepositoryArchivePath = Join-Path -Path $Env:TEMP -ChildPath "coercer.zip"
$RepositoryFolderPath = Join-Path -Path $Env:TEMP -ChildPath "coercer-windows-support"
$MachinePythonKey = "HKLM:\Software\Python\PythonCore"
$UserPythonKey = "HKCU:\Software\Python\PythonCore"
$FoundPython = $False
$PythonVersionParts = $PythonVersion.Split(".")
$TruncatedPythonVersion = "$($PythonVersionParts[0]).$($PythonVersionParts[1])"
$BuildToolsKey = 'HKLM:\Software\Microsoft\VisualStudio'
$BuildToolsMinVersion = '14.0'
$Options = New-object System.Collections.Hashtable
$Options['OutputDir'] = @{
Name = 'Output Directory'
Desc = 'Set the output directory for the built script'
Keywords = @('-o', '--output-dir')
Value = $StartingDirectory
Type = 'Path'
}
$Flags = New-object System.Collections.Hashtable
$Flags['OverridePython'] = @{
Name = 'Override Installed Python'
Desc = "Install Python $PythonVersion even if an existing python version is installed"
Keywords = @('-P', '--override-python')
Value = $False
}
$Flags['LeavePython'] = @{
Name = 'Leave Installed Python'
Desc = "If installed, do not uninstall Python $PythonVersion from the system"
Keywords = @('-L', '--leave-python')
Value = $False
}
$Flags['InstallSystemWide'] = @{
Name = 'Install Script System-Wide'
Desc = 'Install script to C:\Program Files\ and add them to the PATH (Ignores Output Directory)'
Keywords = @('-I', '--install-systemwide')
Value = $False
}
function GetKeyByKeyword {
param (
[hashtable]$HashTable,
[string]$Keyword
)
foreach ($Key in $HashTable.Keys) {
$Item = $HashTable[$Key]
if ($Item.Keywords -contains $Keyword) {
return $Key
}
}
return $Null
}
$HelpMenuPadding = 25
function Show-HelpMenu {
Write-Host '=== Coercer Installer ==='
Write-Host 'Downloads, builds, and installs scripts from the Coercer repository'
Write-Host ''
Write-Host 'Usage: installer.ps1 [FLAGS] [OPTIONS]'
Write-Host ''
Write-Host 'Flags:'
foreach ($Flag in $Flags.Values) {
$FormattedKeywords = $Flag['Keywords'] -join ' '
Write-Host " $($FormattedKeywords.PadRight($HelpMenuPadding)) $($Flag['Desc'])"
}
Write-Host ''
Write-Host 'Options:'
Write-Host " $('-h --help'.PadRight($HelpMenuPadding)) Display this menu"
foreach ($Option in $Options.Values) {
$FormattedKeywords = $Option['Keywords'] -join ' '
Write-Host " $($FormattedKeywords.PadRight($HelpMenuPadding)) $($Option['Desc']) (default: $($Option['Value']))"
}
Write-Host ''
}
for ($I = 0; $I -lt $Args.Count; $I++) {
if ($Args[$I] -eq '-h' -or $Args[$I] -eq '--help') {
Show-HelpMenu
exit 0
}
elseif ($Args[$I].startsWith('-')) {
$ArgParts = $Args[$I] -split '='
$Keyword = $ArgParts[0]
$Value = $Null
$FlagsKey = GetKeyByKeyword -HashTable $Flags -Keyword $Keyword
$OptionsKey = GetKeyByKeyword -HashTable $Options -Keyword $Keyword
if ($ArgParts.Count -eq 2) {
$Value = $ArgParts[1]
}
elseif ($ArgParts.Count -gt 1) {
throw "Error in $($Options[$OptionsKey]['Name']): Multiple equals signs (Use -h or --help for help)"
}
if ($FlagsKey) {
$Flags[$FlagsKey]['Value'] = $True
}
elseif ($OptionsKey) {
if (-not $Value) {
$I++
$Value = $Args[$I]
}
if (-not $Value) {
throw "Error in $($Options[$OptionsKey]['Name']): No value recieved (Use -h or --help for help)"
}
if ($Options[$OptionsKey]['type'] -eq 'Path' -and -not (Test-Path $Value)) {
throw "Error in $($Options[$OptionsKey]['Name']): Path does not exist (Use -h or --help for help)"
}
$Options[$OptionsKey]['Value'] = $Value
}
else {
throw "Error: Unrecognized argument (Use -h or --help for help)"
}
}
}
# Check Local Machine Registry
if (Test-Path $MachinePythonKey) {
Get-ChildItem $MachinePythonKey | ForEach-Object {
if ($_.PSChildName -eq $TruncatedPythonVersion) {
$FoundPython = $True
Write-Host "Python $($_.PSChildName) found in Local Machine"
}
}
}
# Check Current User Registry
if (Test-Path $UserPythonKey) {
Get-ChildItem $UserPythonKey | ForEach-Object {
if ($_.PSChildName -eq $TruncatedPythonVersion) {
$FoundPython = $True
Write-Host "Python $($_.PSChildName) found in Current User"
}
}
}
# Download and install Python
if (-not $FoundPython -or $Flags['OverridePython']['Value']) {
Write-Host "Python $PythonVersion is not installed, installing now..."
Invoke-WebRequest -Uri "https://www.python.org/ftp/python/$PythonVersion/python-$PythonVersion-amd64.exe" -OutFile $PythonInstallerPath
Start-Process $PythonInstallerPath -ArgumentList "/quiet PrependPath=1 Include_launcher=0" -Wait
# Refresh PATH
$Env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
}
# Check for vaild build tools installed
Write-Host 'Checking build tools install...'
$VersionPattern = "^\d+\.\d+"
$VersionKeys = Get-ChildItem -Path $BuildToolsKey | Where-Object { $_.PSChildName -match $versionPattern }
$FoundValidBuildTools = $False
foreach ($version in $versionKeys) {
if ($FoundValidBuildTools) { break }
try {
$versionNumber = [version]$version.PSChildName
if ($versionNumber -lt [version]$BuildToolsMinVersion) {
continue
}
$runtimePath = "$($version.PSPath)\VC\Runtimes\debug\X64"
if (Test-Path $runtimePath) {
$FoundValidBuildTools = $True
}
}
catch {}
}
if (-not $FoundValidBuildTools) {
Write-Host 'Microsoft C++ Build Tools not found, version 14.0+ is required, installing...'
try {
$BuildToolsPath = Join-Path $env:TEMP 'buildtools.exe'
Invoke-WebRequest -Uri 'https://aka.ms/vs/17/release/vs_BuildTools.exe' -OutFile $BuildToolsPath
$BuildArgs = '--quiet --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended'
Start-Process $BuildToolsPath -ArgumentList $BuildArgs -Wait
}
catch {
throw 'Error installing build tools, download it manually (https://aka.ms/vs/17/release/vs_BuildTools.exe)'
}
}
# Download and unzip repository
Write-Host 'Downloading repository...'
Invoke-WebRequest -Uri "https://github.com/p0dalirius/coercer/archive/refs/heads/master.zip" -OutFile $RepositoryArchivePath
Expand-Archive -Path $RepositoryArchivePath -DestinationPath $Env:TEMP -Force
Remove-Item $RepositoryArchivePath
# Begin build process
Write-Host 'Beginning build process...'
Set-Location -Path $RepositoryFolderPath
try {
# Create and activate virtual environment
python -m venv .venv
.venv\Scripts\Activate.ps1
pip install pyinstaller
# Setup
pip install -r requirements.txt
python setup.py install
Write-Host "Building..."
$Arguments = @('--onefile', '--collect-all', 'impacket', '--add-data', 'coercer;coercer')
pyinstaller $Arguments "Coercer.py"
$BuiltScriptPath = Join-Path -Path $RepositoryFolderPath -ChildPath "dist\Coercer.exe"
if ($Flags['InstallSystemWide']['Value']) {
# Prepare destination folder
Write-Host 'Copying executable to Program Files...'
New-Item -ItemType Directory -Path 'C:\Program Files\Coercer' -Force
# Copy built executable into program files
Copy-Item -Path $BuiltScriptPath -Destination 'C:\Program Files\Coercer' -Force
}
else {
Copy-Item -Path $BuiltScriptPath -Destination $Options['OutputDir']['Value'] -Force
}
if ($Flags['InstallSystemWide']['Value']) {
# Get the current PATH environment variable
Write-Host "Updating PATH..."
$CurrentPath = [System.Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::Machine)
# Check if the path already exists in PATH
if ($CurrentPath -notlike "*C:\Program Files\Coercer*") {
# Append the new path to the existing PATH variable
$NewPath = $CurrentPath + ';' + 'C:\Program Files\Coercer'
# Set the new PATH variable
[System.Environment]::SetEnvironmentVariable('Path', $NewPath, [System.EnvironmentVariableTarget]::Machine)
Write-Host 'Successfully added C:\Program Files\Coercer to PATH.'
}
else {
Write-Host 'C:\Program Files\Coercer is already in PATH.'
}
}
}
catch {
Write-Error 'Build Failed. Make sure you have Microsoft Visual Studio C++ Build Tools installed (Check MSVC, Windows 11 SDK)'
Write-Error "$($_.Exception.Message)"
}
Write-Host 'Cleaning up...'
deactivate
Set-Location -Path $StartingDirectory
Remove-Item -Recurse -Force $RepositoryFolderPath
if (-not $Flags['LeavePython']['Value'] -and (-not $FoundPython -or $Flags['OverridePython']['Value'])) {
Write-Host 'Uninstalling Python...'
Start-Process $PythonInstallerPath -ArgumentList "/uninstall /quiet PrependPath=1" -Wait
}
Write-Host 'Done!'