Repository navigation
Expand file tree
/
Copy pathInstall.ps1
More file actions
111 lines (90 loc) · 4.43 KB
/
Copy pathInstall.ps1
File metadata and controls
111 lines (90 loc) · 4.43 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
#Requires -RunAsAdministrator
param (
[string] $InstallationFiles = $null,
[string] $InstallDirectory = "C:\Program Files\DownloadBlocker",
[switch] $Chrome,
[switch] $Edge,
[Parameter()] [Alias("H")] [Switch] $Help
)
function AddManifestToRegistry{
param (
[Parameter(Mandatory)] [string] $RegistryPath,
[Parameter(Mandatory)] [string] $ManifestPath
)
try{
New-Item $RegistryPath -Name "securityjosh.download_blocker" -Force | Out-Null
Set-ItemProperty -Path "$RegistryPath\securityjosh.download_blocker" -Name "(Default)" -Value $ManifestPath -Force
Write-Host "[*] Installed Manifest for key '$RegistryPath\securityjosh.download_blocker'"
}catch{
Write-Host "[!] Failed to set value for '$RegistryPath\securityjosh.download_blocker'"
}
}
if($Help -or (!$Chrome -and !$Edge)){
Write-Output "$(split-path $MyInvocation.InvocationName -Leaf) [https://github.com/SecurityJosh/DownloadBlockerNativeMessagingHost]`nUsage:`n`t-h, -help `t`t`t`tDisplays this help message`n`t-InstallationFiles <File Path>`t`tRuns the installer with a local copy of the installation files. If omitted, the latest version is downloaded from GitHub.`n`t-InstallDirectory <Folder Path>`t`tThe installation directory. If omitted, defaults to C:\Program Files\DownloadBlocker`n`t-Chrome`t`t`t`t`tInstalls the Native Messaging Host for the Chrome Browser`n`t-Edge`t`t`t`t`tInstalls the Native Messaging Host for the Microsoft Edge Browser`n`tNote: At least one of -Chrome, -Edge are required"
return;
}
# Make sure that the install directory exists / we can create it
if (!(Test-Path -PathType Container -Path $InstallDirectory)){
$InstallationFolder = New-Item -ItemType Directory -Force -Path $InstallDirectory -ErrorAction SilentlyContinue
if(!$InstallationFolder){
Write-Host "[!] Could not create the installation folder"
return;
}
}
# Download the NativeMessagingHost if the -InstallationFiles parameter is not specified
if(!$InstallationFiles){
try{
$LatestArchiveUrl = (Invoke-WebRequest "https://api.github.com/repos/SecurityJosh/DownloadBlockerNativeMessagingHost/releases/latest" | ConvertFrom-Json).assets.browser_download_url
$InstallationFiles = (New-TemporaryFile | ForEach-Object {$_ | Rename-Item -NewName "$($_.BaseName).zip" -PassThru}).FullName
Invoke-WebRequest $LatestArchiveUrl -OutFile $InstallationFiles
Write-Host "[*] Downloaded installation files to '$InstallationFiles'"
}catch{
Write-Host "[!] Failed to download installation files"
return;
}
}
# Make sure that $InstallationFiles exists
if(!(Test-Path -PathType Leaf -Path $InstallationFiles)){
Write-Host "[!] Could not find installation files"
return;
}
# Extract the files into the install folder
try{
Expand-Archive -Path $InstallationFiles -DestinationPath $InstallDirectory -Force
Write-Host "[*] File extraction completed successfully"
}catch{
Write-Host "[!] File extraction failed"
return;
}
# Create the NativeMessagingHost manifest file
$Manifest = [ordered]@{
name = "securityjosh.download_blocker";
description = "Native Helper for Download Blocker";
path = ($InstallDirectory + "\" + "DownloadBlockerNativeMessagingHost.exe");
type ="stdio";
allowed_origins = @("chrome-extension://lpolaoejidbgebbkpphcpmocjkpgopak/", "chrome-extension://kippogcnigegkjidkpfpaeimabcoboak/")
} | ConvertTo-Json
$ManifestPath = ($InstallDirectory + "\" + "Manifest.json")
try{
$Manifest | Out-File -FilePath $ManifestPath -Force -Encoding ascii
Write-Host "[*] Created manifest file '$ManifestPath'"
}catch{
Write-Host "[!] Failed to create manifest file '$ManifestPath'"
}
# Point the correct registry keys to the manifest file
if($Chrome){
AddManifestToRegistry -RegistryPath "HKLM:\SOFTWARE\Google\Chrome\NativeMessagingHosts" -ManifestPath $ManifestPath
}
if($Edge){
AddManifestToRegistry -RegistryPath "HKLM:\SOFTWARE\Microsoft\Edge\NativeMessagingHosts" -ManifestPath $ManifestPath
}
try{
if((Get-Item -Path ($InstallationFiles)).DirectoryName -eq $env:TEMP){
Remove-Item -Path $InstallationFiles -ErrorAction Stop
Write-Host "[*] Removed temporary installation file '$InstallationFiles'"
}else{
Write-Host "[*] Not removing user-supplied installation file"
}
}catch{
Write-Host "[!] Failed to remove temporary installation file '$InstallationFiles'"
}