Skip to content

Commit

Permalink
Implement workaround for filepicker while running as admin
Browse files Browse the repository at this point in the history
  • Loading branch information
benwoo1110 committed Aug 7, 2023
1 parent fd34a23 commit e070544
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 21 deletions.
34 changes: 34 additions & 0 deletions MalwareToolbox.DesktopApp/Core/Utils/FileName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Runtime.InteropServices;
using System;

namespace MalwareToolbox.DesktopApp.Core.Utils;

// Workaround for File Pickers that don't work while running as admin, per:
// https://github.com/microsoft/WindowsAppSDK/issues/2504
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct FileName
{
public int StructSize;
public IntPtr HwndOwner;
public IntPtr Instance;
public string Filter;
public string CustomFilter;
public int MaxCustFilter;
public int FilterIndex;
public string File;
public int MaxFile;
public string FileTitle;
public int MaxFileTitle;
public string InitialDir;
public string Title;
public int Flags;
public short FileOffset;
public short FileExtension;
public string DefExt;
public IntPtr CustData;
public IntPtr Hook;
public string TemplateName;
public IntPtr PtrReserved;
public int Reserved;
public int FlagsEx;
}
34 changes: 34 additions & 0 deletions MalwareToolbox.DesktopApp/Core/Utils/OpenFilePicker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Runtime.InteropServices;

namespace MalwareToolbox.DesktopApp.Core.Utils;

// Workaround for File Pickers that don't work while running as admin, per:
// https://github.com/microsoft/WindowsAppSDK/issues/2504
public static partial class OpenFilePicker
{
[DllImport("comdlg32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool GetOpenFileName(ref FileName openFileName);

public static string ShowDialog(string[] filters, string dialogTitle)
{
FileName openFileName = default(FileName);
openFileName.StructSize = Marshal.SizeOf(openFileName);

var filterstr = string.Join("\0", filters) + "\0\0";

openFileName.Filter = filterstr;
openFileName.File = new string(new char[256]);
openFileName.MaxFile = openFileName.File.Length;
openFileName.FileTitle = new string(new char[64]);
openFileName.MaxFileTitle = openFileName.FileTitle.Length;
openFileName.Title = dialogTitle;

if (GetOpenFileName(ref openFileName))
{
return openFileName.File;
}

return string.Empty;
}
}
11 changes: 2 additions & 9 deletions MalwareToolbox.DesktopApp/Core/WindowFiles/WinFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using MalwareToolbox.DesktopApp.Core.Configurations;
using MalwareToolbox.LibraryC.Utils;
using Microsoft.UI.Xaml;
using MalwareToolbox.DesktopApp.Core.Utils;

namespace MalwareToolbox.DesktopApp.Core.WindowFiles;

Expand Down Expand Up @@ -54,15 +55,7 @@ private void AddRecentFile(string filePath)

public async Task<bool> OpenFromFilePicker()
{
var openPicker = new FileOpenPicker { ViewMode = PickerViewMode.List };
openPicker.FileTypeFilter.Add(".exe");
openPicker.FileTypeFilter.Add(".dll");
openPicker.FileTypeFilter.Add("*");

var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(App.Instance.WindowsManager.MainWindow);
WinRT.Interop.InitializeWithWindow.Initialize(openPicker, hwnd);

var file = await openPicker.PickSingleFileAsync();
var file = OpenFilePicker.ShowDialog(new[]{ "Executable (*.exe)", "*.exe", "Dynamic Link Library (*.dll)", "*.dll", "Everything Else (*.*)", "*.*" }, "Open a file to Analysis");
if (file == null)
{
return false;
Expand Down
19 changes: 7 additions & 12 deletions MalwareToolbox.DesktopApp/ViewModels/Tools/PEUnpackerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using CommunityToolkit.WinUI.UI;
using MalwareToolbox.Library.ImportExports;
using System.Collections.Generic;
using MalwareToolbox.DesktopApp.Core.Utils;
using MalwareToolbox.DesktopApp.UI.Notifications;

namespace MalwareToolbox.DesktopApp.ViewModels.Tools;
Expand Down Expand Up @@ -128,19 +129,13 @@ public async Task DetectPacker()
}
}

public async void SelectDatabaseFile()
public void SelectDatabaseFile()
{
var openPicker = new FileOpenPicker { ViewMode = PickerViewMode.List };
openPicker.FileTypeFilter.Add(".txt");
openPicker.FileTypeFilter.Add("*");

var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(App.Instance.WindowsManager.MainWindow);
WinRT.Interop.InitializeWithWindow.Initialize(openPicker, hwnd);

var file = await openPicker.PickSingleFileAsync();
if (file == null) return;

DatabasePath = file.Path;
var filePath = OpenFilePicker.ShowDialog(new[] { "Text file (*.txt)", "*.txt", "Everything Else (*.*)", "*.*" }, "Load Packer Database");
if (!string.IsNullOrEmpty(filePath))
{
DatabasePath = filePath;
}
}

public void OnSearch()
Expand Down

0 comments on commit e070544

Please sign in to comment.