Files
PowerToys/src/modules/FileLocksmith/FileLocksmithLibInterop/NtdllExtensions.cpp
ivanstosic-janea db191b8b75 [New PowerToy] File Locksmith (#20930)
* Imported offline solution

* Make solution compile

* Add Windows sample, doesn't work?

* Added new project to implement the dll

* Remove unneeded header

* Implemented IUnknown part of ExplorerCommand

* Implemented IExplorerCommand methods

* Implemented ClassFactory

* Implemented DLL register/unregister

* Implemented other DLL exports, not working?

* Implemented IShellExtInit inferface

* Implemented IContextMenu, it works!

* Implement command data fetching

* Make sample project compile on VS 2022

* Add plan

* Implement Lib as separate project

* Implemented IPC, not tested

* Console UI project skeleton

* Implemented basic console UI

* Implemented piping, there are bugs

* Prototype works

* Remove old project

* Added GUI project skeleton

* Mitigate issue with WinUI3

* Added a control for displaying results

* Add button

* Implement core functions in lib project

* Call new library function from console main

* Implement showing results

* Improve UI

* Implemented subdirectory search

* Remove useless code

* Set window size

* UI adjustments

* Implement killing process

* Rename variable

* Add lib project to main solution

* Add Ext and GUI projects to solution

* Tweak packages for GUI project

* Add a settings page

* Add a few resource strings

* Add one more resources string

* VS keeps trying to correct this

* Add references to File Locksmith in /,github

* Implement some parts of FileLocksmithModule

* Change output directory

* Change target name and add to runner

* Add logger

* Started implementing settings backend

* Fix log folder

* Settings work

* Add some basic tracing

* Attempt at adding resources

* Remove junk files

* Added missing defines

* Replaced some constants with resources

Something's not working

* Move resources to the Ext project

* Remove experiment

* Add binaries for signing

* Improve tracing

* Remove old Settings calls

* Show something when there are no results

* Change window title

* Move computation to another thread, improve UX

* Increase font size for default text

* Remove entries for killed processes

* Show user name

* Remove nonrecursive implementation

* Implement back end for getting file names

* Show list of files, UI tweaks

* Remove useless includes

* Implement back end for getting full process path

* Dark title bar on dark themes

* Using Expander, other UI adjustments

* Show "No results" after killing all processes

* Show progress ring

* Update configuration mapping

* Revert "Update configuration mapping"

This reverts commit d8e13206f3c7de3c6dbf880299bfff3bf9f27a37.

* Fixed solution configuration, ARM64 should build

* Backend for refreshing

* Variable window size

* Add refresh button

* New WinUI3 C# project for FL

* Started porting functionality

* Add Interop project

* Move IPC to Ext project

* Ported native functions to Interop

* Ported finding processes

* Ported most of Main Window functionality

* Display paths of files

* Implement killing processes

* Use resource string for "End Task"

* Remove entries for terminated processes

* Show User name

* Set default window size

* Make the new UI the default

* Reading paths from stdin, completed port to C#

* Fix small bug

* Moving to MVVM

* Adding Labs

* Merge branch 'ivan/file-locksmith' of https://github.com/microsoft/PowerToys into ivan/file-locksmith

Removing one parent commit for cleaner history

Co-Authored-By: Niels Laute <niels.laute@live.nl>

* Reintroducing features

* Moving UI strings to resources file

* Restored functionality

* Add missing dlls

* Add FIle Locksmith to publish.cmd

* Rebase fixes

* Try updating nuget.config

* Fix copy-paste blunder

* Add File Locksmith UI for publishing

* Add .pubxml file in FileLocksmith

* Change build output folder

* Fix installer build issues

Remove old projects from solution so MSBuild doesn't build them.
Downgrade target framework to what most other projects are using.
Fix publishing profile and project runtimes.
Remove unused CsWinRT references.

* [CI] Add clear to nuget packages

* Fix module reference counting

* Fix nuget for release CI

* Fix version and signing

* Fix path for resources

* Fix incorrect results when running 2 instances

* Fix default nuget source

* Windows 10 icon and fallback for UI

* Code clean-up and spaces instead of tabs

* Add gif showcasing FL

* Add screenshot of File Locksmith for Settings

* Add new files to the installer

* Add OOBE page

* Showing selected paths in the header

* Tweak path list

* Added new, wider gif

* Add GPO

* Add some logs

* [CI]Get CommunityToolkit.Labs from BigPark feed

* [CI]Use azure package feed for Nuget in release

* [CI]Another try for the labs source

* Revert changes to feed

* Use RestoreAdditionalProjectSources

* Add tooltip to file list

* Change tooltip to not trim the lines

* Add Tips and tricks section mentioning elevated

* Add some more logs messages.

* Grammar fix

* Add to bug report tool

* Fix UI virtualization not working

* Disable virtualization to avoid crashes

* Get better virtualization

* Add dialog instead of tooltip to show list of items

* No results refresh icon is now a button too

* Use managed methods for handling processes

* Remove registry code from Ext.

* Support drives too

Co-authored-by: Niels Laute <niels.laute@live.nl>
Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
2022-10-28 14:51:21 +01:00

267 lines
7.7 KiB
C++

#include "pch.h"
#include "NtdllExtensions.h"
#define STATUS_INFO_LENGTH_MISMATCH ((LONG)0xC0000004)
// Calls NtQuerySystemInformation and returns a buffer containing the result.
namespace
{
std::wstring_view unicode_to_view(UNICODE_STRING unicode_str)
{
return std::wstring_view(unicode_str.Buffer, unicode_str.Length / sizeof(WCHAR));
}
std::wstring unicode_to_str(UNICODE_STRING unicode_str)
{
return std::wstring(unicode_str.Buffer, unicode_str.Length / sizeof(WCHAR));
}
// Implementation adapted from src/common/utils
inline std::wstring get_module_name(HANDLE process, HMODULE mod)
{
wchar_t buffer[MAX_PATH + 1];
DWORD actual_length = GetModuleFileNameExW(process, mod, buffer, MAX_PATH + 1);
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
const DWORD long_path_length = 0xFFFF; // should be always enough
std::wstring long_filename(long_path_length, L'\0');
actual_length = GetModuleFileNameW(mod, long_filename.data(), long_path_length);
long_filename.resize(std::wcslen(long_filename.data()));
long_filename.shrink_to_fit();
return long_filename;
}
return { buffer, (UINT)lstrlenW(buffer) };
}
constexpr size_t DefaultModulesResultSize = 512;
std::vector<std::wstring> process_modules(DWORD pid)
{
HANDLE process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
if (!process)
{
return {};
}
std::vector<std::wstring> result;
bool completed = false;
std::vector<HMODULE> modules(DefaultModulesResultSize);
while (!completed)
{
DWORD needed;
auto status = EnumProcessModules(process, modules.data(), static_cast<DWORD>(modules.size() * sizeof(HMODULE)), &needed);
if (!status)
{
// Give up
return {};
}
if (needed > modules.size() * sizeof(HMODULE))
{
// Array is too small
modules.resize(needed / sizeof(HMODULE));
continue;
}
// Okay
modules.resize(needed / sizeof(HMODULE));
for (auto mod : modules)
{
result.push_back(get_module_name(process, mod));
}
completed = true;
}
CloseHandle(process);
return result;
}
}
NtdllExtensions::MemoryLoopResult NtdllExtensions::NtQuerySystemInformationMemoryLoop(ULONG SystemInformationClass)
{
MemoryLoopResult result;
result.memory.resize(DefaultResultBufferSize);
while (result.memory.size() <= MaxResultBufferSize)
{
ULONG result_len;
result.status = NtQuerySystemInformation(SystemInformationClass, result.memory.data(), (ULONG)result.memory.size(), &result_len);
if (result.status == STATUS_INFO_LENGTH_MISMATCH)
{
result.memory.resize(result.memory.size() * 2);
continue;
}
if (NT_ERROR(result.status))
{
result.memory.clear();
}
return result;
}
result.status = STATUS_INFO_LENGTH_MISMATCH;
result.memory.clear();
return result;
}
std::wstring NtdllExtensions::file_handle_to_kernel_name(HANDLE file_handle, std::vector<BYTE>& buffer)
{
if (GetFileType(file_handle) != FILE_TYPE_DISK)
{
return L"";
}
ULONG return_length;
auto status = NtQueryObject(file_handle, ObjectNameInformation, buffer.data(), (ULONG)buffer.size(), &return_length);
if (NT_SUCCESS(status))
{
auto object_name_info = (UNICODE_STRING*)buffer.data();
return unicode_to_str(*object_name_info);
}
return L"";
}
std::wstring NtdllExtensions::file_handle_to_kernel_name(HANDLE file_handle)
{
std::vector<BYTE> buffer(DefaultResultBufferSize);
return file_handle_to_kernel_name(file_handle, buffer);
}
std::wstring NtdllExtensions::path_to_kernel_name(LPCWSTR path)
{
HANDLE file_handle = CreateFileW(path, 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (file_handle == INVALID_HANDLE_VALUE)
{
return {};
}
auto kernel_name = file_handle_to_kernel_name(file_handle);
CloseHandle(file_handle);
return kernel_name;
}
std::vector<NtdllExtensions::HandleInfo> NtdllExtensions::handles() noexcept
{
auto get_info_result = NtQuerySystemInformationMemoryLoop(SystemHandleInformation);
if (NT_ERROR(get_info_result.status))
{
return {};
}
auto info_ptr = (SYSTEM_HANDLE_INFORMATION*)get_info_result.memory.data();
std::map<DWORD, HANDLE> pid_to_handle;
std::vector<HandleInfo> result;
std::vector<BYTE> object_info_buffer(DefaultResultBufferSize);
for (ULONG i = 0; i < info_ptr->HandleCount; i++)
{
auto handle_info = info_ptr->Handles + i;
DWORD pid = handle_info->ProcessId;
HANDLE process_handle = NULL;
auto iter = pid_to_handle.find(pid);
if (iter != pid_to_handle.end())
{
process_handle = iter->second;
}
else
{
process_handle = OpenProcess(PROCESS_DUP_HANDLE, FALSE, pid);
if (!process_handle)
{
continue;
}
pid_to_handle[pid] = process_handle;
}
// According to this:
// https://stackoverflow.com/questions/46384048/enumerate-handles
// NtQueryObject could hang
// TODO uncomment and investigate
// if (handle_info->GrantedAccess == 0x0012019f) {
// continue;
// }
HANDLE handle_copy;
auto dh_result = DuplicateHandle(process_handle, (HANDLE)handle_info->Handle, GetCurrentProcess(), &handle_copy, 0, 0, DUPLICATE_SAME_ACCESS);
if (dh_result == 0)
{
// Ignore this handle.
continue;
}
ULONG return_length;
auto status = NtQueryObject(handle_copy, ObjectTypeInformation, object_info_buffer.data(), (ULONG)object_info_buffer.size(), &return_length);
if (NT_ERROR(status))
{
// Ignore this handle.
CloseHandle(handle_copy);
continue;
}
auto object_type_info = (OBJECT_TYPE_INFORMATION*)object_info_buffer.data();
auto type_name = unicode_to_str(object_type_info->Name);
std::wstring file_name = file_handle_to_kernel_name(handle_copy, object_info_buffer);
if (type_name == L"File")
{
file_name = file_handle_to_kernel_name(handle_copy, object_info_buffer);
}
result.push_back(HandleInfo{ pid, handle_info->Handle, type_name, file_name });
CloseHandle(handle_copy);
}
for (auto [pid, handle] : pid_to_handle)
{
CloseHandle(handle);
}
return result;
}
// Returns the list of all processes.
// On failure, returns an empty vector.
std::vector<NtdllExtensions::ProcessInfo> NtdllExtensions::processes() noexcept
{
auto get_info_result = NtQuerySystemInformationMemoryLoop(SystemProcessInformation);
if (NT_ERROR(get_info_result.status))
{
return {};
}
std::vector<ProcessInfo> result;
auto info_ptr = (PSYSTEM_PROCESS_INFORMATION)get_info_result.memory.data();
while (info_ptr->NextEntryOffset)
{
info_ptr = decltype(info_ptr)((LPBYTE)info_ptr + info_ptr->NextEntryOffset);
ProcessInfo item;
item.name = unicode_to_str(info_ptr->ImageName);
item.pid = (DWORD)(uintptr_t)info_ptr->UniqueProcessId;
item.modules = process_modules(item.pid);
result.push_back(item);
}
return result;
}