From cdad3c74d35863c0269f27c7d38a44b5a09aa7b3 Mon Sep 17 00:00:00 2001 From: Elliot Chernofsky Date: Thu, 25 Apr 2024 14:42:39 -0400 Subject: [PATCH 1/2] Add VM-Set-Open-With-Association Add `VM-Set-Open-With-Association` helper function to `common.vm` and use it to open zip extensions with 7z by default. --- packages/7zip-15-05.vm/7zip-15-05.vm.nuspec | 4 +- .../7zip-15-05.vm/tools/chocolateyinstall.ps1 | 2 + .../tools/chocolateyuninstall.ps1 | 1 + packages/7zip-nsis.vm/7zip-nsis.vm.nuspec | 4 +- .../7zip-nsis.vm/tools/chocolateyinstall.ps1 | 2 +- .../tools/chocolateyuninstall.ps1 | 1 + packages/7zip.vm/7zip.vm.nuspec | 4 +- packages/common.vm/common.vm.nuspec | 2 +- .../common.vm/tools/vm.common/vm.common.psm1 | 76 +++++++++++++++++++ 9 files changed, 88 insertions(+), 8 deletions(-) diff --git a/packages/7zip-15-05.vm/7zip-15-05.vm.nuspec b/packages/7zip-15-05.vm/7zip-15-05.vm.nuspec index e30d9c406..f66001c18 100644 --- a/packages/7zip-15-05.vm/7zip-15-05.vm.nuspec +++ b/packages/7zip-15-05.vm/7zip-15-05.vm.nuspec @@ -2,11 +2,11 @@ 7zip-15-05.vm - 15.05.0.20240308 + 15.05.0.20240425 Igor Pavlov 7-Zip file archiver. This version is able to extract NSIS scripts. - + diff --git a/packages/7zip-15-05.vm/tools/chocolateyinstall.ps1 b/packages/7zip-15-05.vm/tools/chocolateyinstall.ps1 index 3b8f2c8de..a06c70688 100644 --- a/packages/7zip-15-05.vm/tools/chocolateyinstall.ps1 +++ b/packages/7zip-15-05.vm/tools/chocolateyinstall.ps1 @@ -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' @@ -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 $_ diff --git a/packages/7zip-15-05.vm/tools/chocolateyuninstall.ps1 b/packages/7zip-15-05.vm/tools/chocolateyuninstall.ps1 index 96df2fde2..93621f917 100644 --- a/packages/7zip-15-05.vm/tools/chocolateyuninstall.ps1 +++ b/packages/7zip-15-05.vm/tools/chocolateyuninstall.ps1 @@ -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 } diff --git a/packages/7zip-nsis.vm/7zip-nsis.vm.nuspec b/packages/7zip-nsis.vm/7zip-nsis.vm.nuspec index 781548965..bdce9c088 100644 --- a/packages/7zip-nsis.vm/7zip-nsis.vm.nuspec +++ b/packages/7zip-nsis.vm/7zip-nsis.vm.nuspec @@ -2,11 +2,11 @@ 7zip-nsis.vm - 23.01 + 23.01.0.20240425 myfreeer 7-zip build with nsis script decompiling - + diff --git a/packages/7zip-nsis.vm/tools/chocolateyinstall.ps1 b/packages/7zip-nsis.vm/tools/chocolateyinstall.ps1 index abec53c52..7a046e6d2 100644 --- a/packages/7zip-nsis.vm/tools/chocolateyinstall.ps1 +++ b/packages/7zip-nsis.vm/tools/chocolateyinstall.ps1 @@ -28,7 +28,6 @@ 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. @@ -36,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 $_ diff --git a/packages/7zip-nsis.vm/tools/chocolateyuninstall.ps1 b/packages/7zip-nsis.vm/tools/chocolateyuninstall.ps1 index 031d0f90d..bd17d181d 100644 --- a/packages/7zip-nsis.vm/tools/chocolateyuninstall.ps1 +++ b/packages/7zip-nsis.vm/tools/chocolateyuninstall.ps1 @@ -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 } diff --git a/packages/7zip.vm/7zip.vm.nuspec b/packages/7zip.vm/7zip.vm.nuspec index 2ecde9f75..f58c7a9e7 100644 --- a/packages/7zip.vm/7zip.vm.nuspec +++ b/packages/7zip.vm/7zip.vm.nuspec @@ -2,11 +2,11 @@ 7zip.vm - 0.0.0.20240410 + 0.0.0.20240425 Metapackage for 7zip to ensure all packages use the same 7zip version. Mandiant - + diff --git a/packages/common.vm/common.vm.nuspec b/packages/common.vm/common.vm.nuspec index d6e2c42b8..234e5f9c4 100755 --- a/packages/common.vm/common.vm.nuspec +++ b/packages/common.vm/common.vm.nuspec @@ -2,7 +2,7 @@ common.vm - 0.0.0.20240424 + 0.0.0.20240425 Common libraries for VM-packages Mandiant diff --git a/packages/common.vm/tools/vm.common/vm.common.psm1 b/packages/common.vm/tools/vm.common/vm.common.psm1 index af91de97f..146ae123e 100755 --- a/packages/common.vm/tools/vm.common/vm.common.psm1 +++ b/packages/common.vm/tools/vm.common/vm.common.psm1 @@ -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 From 07e548f90e9523dc6cf30090844bbf531c9bc693 Mon Sep 17 00:00:00 2001 From: Elliot Chernofsky Date: Thu, 25 Apr 2024 20:51:49 -0400 Subject: [PATCH 2/2] Make Chrome the Default for .html Use the new `VM-Set-Open-With-Association` helper function to open .html files with Chrome by default. Remove also the Desktop shortcut that is created during installation. --- packages/googlechrome.vm/googlechrome.vm.nuspec | 4 ++-- .../googlechrome.vm/tools/chocolateyinstall.ps1 | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/googlechrome.vm/googlechrome.vm.nuspec b/packages/googlechrome.vm/googlechrome.vm.nuspec index 38e02806e..b93e9873d 100644 --- a/packages/googlechrome.vm/googlechrome.vm.nuspec +++ b/packages/googlechrome.vm/googlechrome.vm.nuspec @@ -2,11 +2,11 @@ googlechrome.vm - 0.0.0.20240405 + 0.0.0.20240425 Google LLC. Chrome is a popular web browser. - + diff --git a/packages/googlechrome.vm/tools/chocolateyinstall.ps1 b/packages/googlechrome.vm/tools/chocolateyinstall.ps1 index ebb97e19c..b01626503 100644 --- a/packages/googlechrome.vm/tools/chocolateyinstall.ps1 +++ b/packages/googlechrome.vm/tools/chocolateyinstall.ps1 @@ -22,7 +22,8 @@ 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 $_ } @@ -30,6 +31,8 @@ try { # 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 @@ -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 +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"