Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Open With File Association #1016

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/7zip-15-05.vm/7zip-15-05.vm.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
<metadata>
<id>7zip-15-05.vm</id>
<version>15.05.0.20240308</version>
<version>15.05.0.20240425</version>
<authors>Igor Pavlov</authors>
<description>7-Zip file archiver. This version is able to extract NSIS scripts.</description>
<dependencies>
<dependency id="common.vm" version="0.0.0.20230926" />
<dependency id="common.vm" version="0.0.0.20240425" />
</dependencies>
</metadata>
</package>
2 changes: 2 additions & 0 deletions packages/7zip-15-05.vm/tools/chocolateyinstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Import-Module vm.common -Force -DisableNameChecking
try {
$toolName = '7z'
$category = 'Productivity Tools'

$url = 'https://sourceforge.net/projects/sevenzip/files/7-Zip/15.05/7z1505.exe/download'
$checksum = 'fa99d29283d9a6c501b70d2755cd06cf5bc3dd8e48acc73926b6e0f389885120'
$url64 = 'https://sourceforge.net/projects/sevenzip/files/7-Zip/15.05/7z1505-x64.exe/download'
Expand Down Expand Up @@ -34,6 +35,7 @@ try {
$extensions = @(".7z", ".bzip2", ".gzip", ".tar", ".wim", ".xz", ".txz", ".zip", ".rar")
foreach ($extension in $extensions) {
VM-Add-To-Right-Click-Menu $toolName 'unzip "infected"' "`"$7zExecutablePath`" e -pinfected `"%1`"" "$executablePath" -extension $extension
VM-Set-Open-With-Association $executablePath $extension
}
} catch {
VM-Write-Log-Exception $_
Expand Down
1 change: 1 addition & 0 deletions packages/7zip-15-05.vm/tools/chocolateyuninstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ VM-Uninstall-With-Uninstaller "7-Zip 15.05*" "EXE" "/S"
$extensions = @(".7z", ".bzip2", ".gzip", ".tar", ".wim", ".xz", ".txz", ".zip", ".rar")
foreach ($extension in $extensions) {
VM-Remove-From-Right-Click-Menu $toolName -extension $extension
VM-Remove-Open-With-Association "${toolName}FM" -extension $extension
}
4 changes: 2 additions & 2 deletions packages/7zip-nsis.vm/7zip-nsis.vm.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
<metadata>
<id>7zip-nsis.vm</id>
<version>23.01</version>
<version>23.01.0.20240425</version>
<authors>myfreeer</authors>
<description>7-zip build with nsis script decompiling</description>
<dependencies>
<dependency id="common.vm" />
<dependency id="common.vm" version="0.0.0.20240425" />
</dependencies>
</metadata>
</package>
2 changes: 1 addition & 1 deletion packages/7zip-nsis.vm/tools/chocolateyinstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ try {

# Make shortcut point to 7z File Manager so that it's more useful of a shortcut.
$executablePath = Join-Path $toolDir "${toolName}FM.exe" -Resolve

VM-Install-Shortcut $toolName $category $executablePath

# Add 7z unzip with password "infected" to the right menu for the most common extensions.
# 7z can unzip other file extensions like .docx but these don't likely use the infected password.
$extensions = @(".7z", ".bzip2", ".gzip", ".tar", ".wim", ".xz", ".txz", ".zip", ".rar")
foreach ($extension in $extensions) {
VM-Add-To-Right-Click-Menu $toolName 'unzip "infected"' "`"$7zExecutablePath`" e -pinfected `"%1`"" "$executablePath" -extension $extension
VM-Set-Open-With-Association $executablePath $extension
}
} catch {
VM-Write-Log-Exception $_
Expand Down
1 change: 1 addition & 0 deletions packages/7zip-nsis.vm/tools/chocolateyuninstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ Uninstall-BinFile -Name $toolName
$extensions = @(".7z", ".bzip2", ".gzip", ".tar", ".wim", ".xz", ".txz", ".zip", ".rar")
foreach ($extension in $extensions) {
VM-Remove-From-Right-Click-Menu $toolName -extension $extension
VM-Remove-Open-With-Association "${toolName}FM" -extension $extension
}
4 changes: 2 additions & 2 deletions packages/7zip.vm/7zip.vm.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
<metadata>
<id>7zip.vm</id>
<version>0.0.0.20240410</version>
<version>0.0.0.20240425</version>
<description>Metapackage for 7zip to ensure all packages use the same 7zip version.</description>
<authors>Mandiant</authors>
<dependencies>
<dependency id="common.vm" />
<dependency id="common.vm" version="0.0.0.20240425"/>
<dependency id="7zip-nsis.vm" />
</dependencies>
</metadata>
Expand Down
2 changes: 1 addition & 1 deletion packages/common.vm/common.vm.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
<metadata>
<id>common.vm</id>
<version>0.0.0.20240424</version>
<version>0.0.0.20240425</version>
<description>Common libraries for VM-packages</description>
<authors>Mandiant</authors>
</metadata>
Expand Down
76 changes: 76 additions & 0 deletions packages/common.vm/tools/vm.common/vm.common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,82 @@ function VM-Remove-From-Right-Click-Menu {
}
}

# Add associations to the file extension key
function VM-Set-Open-With-Association {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string] $executablePath,
[Parameter(Mandatory = $true)]
[string] $extension
)
try {
# Extract the executable name without path or extension
$exeName = [System.IO.Path]::GetFileNameWithoutExtension($executablePath)

ForEach ($hive in @("HKCU:", "HKLM:")) {
# Create the 'command' key and its default value
$commandKey = "${hive}\Software\Classes\${exeName}_auto_file\shell\open\command"
New-Item -Path $commandKey -Force
New-ItemProperty -Path $commandKey -Name '(Default)' -Value "`"$executablePath`" `"%1`""

# Create/update the file extension key
$extKey = "${hive}\Software\Classes\$extension"
New-Item -Path $extKey -Force
New-ItemProperty -Path $extKey -Name '(Default)' -Value "${exeName}_auto_file"

# Add to OpenWithProgids for visibility in "Open with..." menu
if (Get-ItemProperty -Path $extKey -Name 'OpenWithProgids' -ErrorAction Ignore) {
# If OpenWithProgids exists, update it
$existingProgIds = (Get-ItemProperty -Path $extKey -Name 'OpenWithProgids').OpenWithProgids
$newProgIds = "$existingProgIds ${exeName}_auto_file" | Select-Object -Unique # Ensure unique values
Set-ItemProperty -Path $extKey -Name 'OpenWithProgids' -Value $newProgIds -PropertyType ExpandString
} else {
# If OpenWithProgids doesn't exist, create it
New-ItemProperty -Path $extKey -Name 'OpenWithProgids' -Value "${exeName}_auto_file" -PropertyType ExpandString
}
}
} catch {
VM-Write-Log "ERROR" "Failed to add $exeName as file association. Error: $_"
}
}

# Remove associations from the file extension key
function VM-Remove-Open-With-Association {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string] $exeName,
[Parameter(Mandatory = $true)]
[string] $extension
)
ForEach ($hive in @("HKCU:", "HKLM:")) {
$extKey = "${hive}\Software\Classes\$extension"

# Check if the key exists before attempting removal
if (Test-Path $extKey) {
$expectedDefault = "${exeName}_auto_file"

# Remove the default value if it matches
$currentDefault = Get-ItemPropertyValue -Path $extKey -Name '(Default)' -ErrorAction SilentlyContinue
if ($currentDefault -and $currentDefault -eq $expectedDefault) {
New-ItemProperty -Path $extKey -Name '(Default)' -Value "" | Out-Null
}

# Remove from OpenWithProgids if present
if ((Get-ItemProperty -Path $extKey -Name 'OpenWithProgids' -ErrorAction Ignore).OpenWithProgids -contains $expectedDefault) {
$newProgIds = (Get-ItemProperty -Path $extKey -Name 'OpenWithProgids').OpenWithProgids -split ' ' | Where-Object { $_ -ne $expectedDefault }
Set-ItemProperty -Path $extKey -Name 'OpenWithProgids' -Value ($newProgIds -join ' ')
}
}

# Remove the 'command' key and the auto_file key
$commandKey = "${hive}\Software\Classes\${exeName}_auto_file\shell\open\command"
$autoFileKey = "${hive}\Software\Classes\${exeName}_auto_file"
Remove-Item -Path $commandKey,$autoFileKey -Recurse -ErrorAction SilentlyContinue
}
}

function VM-Get-Host-Info {
$survey = @"
Host Information
Expand Down
4 changes: 2 additions & 2 deletions packages/googlechrome.vm/googlechrome.vm.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
<metadata>
<id>googlechrome.vm</id>
<version>0.0.0.20240405</version>
<version>0.0.0.20240425</version>
<authors>Google LLC.</authors>
<description>Chrome is a popular web browser.</description>
<dependencies>
<dependency id="common.vm" version="0.0.0.20240111" />
<dependency id="common.vm" version="0.0.0.20240425" />
</dependencies>
</metadata>
</package>
13 changes: 12 additions & 1 deletion packages/googlechrome.vm/tools/chocolateyinstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ try {
}
Install-ChocolateyInstallPackage @packageArgs -ErrorAction SilentlyContinue

VM-Assert-Path "${env:ProgramFiles}\Google\Chrome\Application\chrome.exe"
$exePath = "${env:ProgramFiles}\Google\Chrome\Application\chrome.exe"
VM-Assert-Path $exePath
} catch {
VM-Write-Log-Exception $_
}

# Try to set configuration, but do not fail the package if it fails
$ErrorActionPreference = 'Continue'

VM-Remove-Desktop-Shortcut "Google Chrome"

# Expand the path to the Chrome User Data folder and create the "User Data" folder if it doesn't exist.
$userDataPath = ${Env:LOCALAPPDATA} + "\Google\Chrome\User Data"
New-Item -ItemType Directory -Path $userDataPath -Force -ea 0 | Out-Null
Expand All @@ -52,3 +55,11 @@ $contentOptions = @{
Value = "`{`"privacy_sandbox`":{`"m1`":{`"row_notice_acknowledged`":true}}`}"
}
Set-Content @contentOptions

# Remove Edge from being default for file extensions so Chrome can be the default
Ana06 marked this conversation as resolved.
Show resolved Hide resolved
ForEach ($hive in @("HKCU:", "HKLM:")) {
Remove-Item -Path "${hive}\SOFTWARE\Classes\MSEdgeHTM" -Recurse -ErrorAction SilentlyContinue
}

# Make Chrome the default for .html files
VM-Set-Open-With-Association $exePath ".html"