<commit_before>#define WIN32_LEAN_AND_MEAN

#include "Error.h"
#include "Path.h"

#include <Windows.h>
#include <stdio.h>
#include <MinHook.h>

WCHAR g_ModuleName[MAX_PATH];

//////////////////////////////////////////////////////////////////
//
// CreateFileW
//
//////////////////////////////////////////////////////////////////

// Pointer to original function
HANDLE (WINAPI *fpCreateFileW)(LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);

HANDLE WINAPI CreateFileW_Detour(
  _In_      LPCTSTR lpFileName,
  _In_      DWORD dwDesiredAccess,
  _In_      DWORD dwShareMode,
  _In_opt_  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  _In_      DWORD dwCreationDisposition,
  _In_      DWORD dwFlagsAndAttributes,
  _In_opt_  HANDLE hTemplateFile)
{
	LPCWSTR NewPath = CanonizePath(lpFileName);
	if (NewPath == NULL)
	{
		SetLastError(ERROR_OUTOFMEMORY);
		return INVALID_HANDLE_VALUE;
	}
	HANDLE hFile = fpCreateFileW(NewPath, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
	if (NewPath != lpFileName)
		delete []NewPath;
	return hFile;
}

//////////////////////////////////////////////////////////////////
//
// GetFileAttributesW
//
//////////////////////////////////////////////////////////////////

// Pointer to original function
DWORD (WINAPI *fpGetFileAttributesW)(LPCWSTR lpFileName);

DWORD WINAPI GetFileAttributesW_Detour(
    __in LPCWSTR lpFileName)
{
	LPCWSTR NewPath = CanonizePath(lpFileName);
	if (NewPath == NULL)
	{
		SetLastError(ERROR_OUTOFMEMORY);
		return INVALID_FILE_ATTRIBUTES;
	}
	DWORD ret = fpGetFileAttributesW(NewPath);
	if (NewPath != lpFileName)
		delete []NewPath;
	return ret;
}

//////////////////////////////////////////////////////////////////
//
// CreateProcessW
//
//////////////////////////////////////////////////////////////////

// Pointer to original function
BOOL (WINAPI *fpCreateProcessW)(LPCWSTR lpApplicationName, LPWSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation);

// Inject a DLL into the target process by creating a new thread at LoadLibrary.
// Waits for injected thread to finish and returns its exit code.
bool LoadLibraryInjection(HANDLE hProcess, LPCWSTR LibraryName)
{
	size_t LibraryNameSize = (wcslen(LibraryName) + 1) * sizeof(WCHAR);
	LPVOID RemoteLibraryName = (LPVOID)VirtualAllocEx(hProcess, NULL, LibraryNameSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
	if (RemoteLibraryName == NULL)
	{
		return false;
	}

	if (WriteProcessMemory(hProcess, (LPVOID)RemoteLibraryName, LibraryName, LibraryNameSize, NULL) == 0)
	{
		DWORD error = GetLastError();
		VirtualFreeEx(hProcess, RemoteLibraryName, 0, MEM_RELEASE); // free the memory we were going to use
		SetLastError(error);
		return false;
	}

	HANDLE hThread = CreateRemoteThread(hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibraryW, (LPVOID)RemoteLibraryName, 0, NULL);
	if (hThread == NULL)
	{
		DWORD error = GetLastError();
		VirtualFreeEx(hProcess, RemoteLibraryName, 0, MEM_RELEASE);
		SetLastError(error);
		return false;
	}

	// Wait for the thread to finish.
	WaitForSingleObject(hThread, INFINITE);

	// Lets see what it says...
	DWORD dwThreadExitCode;
	if (!GetExitCodeThread(hThread,  &dwThreadExitCode))
	{
		DWORD error = GetLastError();
		CloseHandle(hThread);
		SetLastError(error);
		return false;
	}

	// No need for this handle anymore, lets get rid of it.
	CloseHandle(hThread);

	// Lets clear up that memory we allocated earlier.
	VirtualFreeEx(hProcess, RemoteLibraryName, 0, MEM_RELEASE);

	if (dwThreadExitCode == 0) // LoadLibrary failed
	{
		hThread = CreateRemoteThread(hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)GetLastError, (LPVOID)NULL, 0, NULL);
		if (hThread == NULL)
		{
			return false;
		}

		// Get last error
		WaitForSingleObject(hThread, INFINITE);
		if (!GetExitCodeThread(hThread,  &dwThreadExitCode))
		{
			DWORD error = GetLastError();
			CloseHandle(hThread);
			SetLastError(error);
			return false;
		}

		SetLastError(dwThreadExitCode);
		return false;
	}

	return true;
}

BOOL WINAPI CreateProcessW_Detour(
    __in_opt    LPCWSTR lpApplicationName,
    __inout_opt LPWSTR lpCommandLine,
    __in_opt    LPSECURITY_ATTRIBUTES lpProcessAttributes,
    __in_opt    LPSECURITY_ATTRIBUTES lpThreadAttributes,
    __in        BOOL bInheritHandles,
    __in        DWORD dwCreationFlags,
    __in_opt    LPVOID lpEnvironment,
    __in_opt    LPCWSTR lpCurrentDirectory,
    __in        LPSTARTUPINFOW lpStartupInfo,
    __out       LPPROCESS_INFORMATION lpProcessInformation
    )
{
	PROCESS_INFORMATION pi;
	if (lpProcessInformation == NULL)
		lpProcessInformation = &pi;

	// Start our new process with a suspended main thread
	BOOL CreateProcessResult = fpCreateProcessW(lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags | CREATE_SUSPENDED, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation);

	if (CreateProcessResult)
	{
		// Inject our DLL
		// This method returns only when injection thread returns.
		if (!LoadLibraryInjection(lpProcessInformation->hProcess, g_ModuleName))
		{
			PrintError(L"WARNING: Failed to inject DLL %s", g_ModuleName);
		}

		// Once the injection thread has returned it is safe to resume the main thread.
		ResumeThread(lpProcessInformation->hThread);
	}

	return CreateProcessResult;
}

//////////////////////////////////////////////////////////////////<commit_msg>Bugfix: Only resume thread if caller not requested CREATE_SUSPENDED<commit_after>#define WIN32_LEAN_AND_MEAN

#include "Error.h"
#include "Path.h"

#include <Windows.h>
#include <stdio.h>
#include <MinHook.h>

WCHAR g_ModuleName[MAX_PATH];

//////////////////////////////////////////////////////////////////
//
// CreateFileW
//
//////////////////////////////////////////////////////////////////

// Pointer to original function
HANDLE (WINAPI *fpCreateFileW)(LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);

HANDLE WINAPI CreateFileW_Detour(
  _In_      LPCTSTR lpFileName,
  _In_      DWORD dwDesiredAccess,
  _In_      DWORD dwShareMode,
  _In_opt_  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  _In_      DWORD dwCreationDisposition,
  _In_      DWORD dwFlagsAndAttributes,
  _In_opt_  HANDLE hTemplateFile)
{
	LPCWSTR NewPath = CanonizePath(lpFileName);
	if (NewPath == NULL)
	{
		SetLastError(ERROR_OUTOFMEMORY);
		return INVALID_HANDLE_VALUE;
	}
	HANDLE hFile = fpCreateFileW(NewPath, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
	if (NewPath != lpFileName)
		delete []NewPath;
	return hFile;
}

//////////////////////////////////////////////////////////////////
//
// GetFileAttributesW
//
//////////////////////////////////////////////////////////////////

// Pointer to original function
DWORD (WINAPI *fpGetFileAttributesW)(LPCWSTR lpFileName);

DWORD WINAPI GetFileAttributesW_Detour(
    __in LPCWSTR lpFileName)
{
	LPCWSTR NewPath = CanonizePath(lpFileName);
	if (NewPath == NULL)
	{
		SetLastError(ERROR_OUTOFMEMORY);
		return INVALID_FILE_ATTRIBUTES;
	}
	DWORD ret = fpGetFileAttributesW(NewPath);
	if (NewPath != lpFileName)
		delete []NewPath;
	return ret;
}

//////////////////////////////////////////////////////////////////
//
// CreateProcessW
//
//////////////////////////////////////////////////////////////////

// Pointer to original function
BOOL (WINAPI *fpCreateProcessW)(LPCWSTR lpApplicationName, LPWSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation);

// Inject a DLL into the target process by creating a new thread at LoadLibrary.
// Waits for injected thread to finish and returns its exit code.
bool LoadLibraryInjection(HANDLE hProcess, LPCWSTR LibraryName)
{
	size_t LibraryNameSize = (wcslen(LibraryName) + 1) * sizeof(WCHAR);
	LPVOID RemoteLibraryName = (LPVOID)VirtualAllocEx(hProcess, NULL, LibraryNameSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
	if (RemoteLibraryName == NULL)
	{
		return false;
	}

	if (WriteProcessMemory(hProcess, (LPVOID)RemoteLibraryName, LibraryName, LibraryNameSize, NULL) == 0)
	{
		DWORD error = GetLastError();
		VirtualFreeEx(hProcess, RemoteLibraryName, 0, MEM_RELEASE); // free the memory we were going to use
		SetLastError(error);
		return false;
	}

	HANDLE hThread = CreateRemoteThread(hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibraryW, (LPVOID)RemoteLibraryName, 0, NULL);
	if (hThread == NULL)
	{
		DWORD error = GetLastError();
		VirtualFreeEx(hProcess, RemoteLibraryName, 0, MEM_RELEASE);
		SetLastError(error);
		return false;
	}

	// Wait for the thread to finish.
	WaitForSingleObject(hThread, INFINITE);

	// Lets see what it says...
	DWORD dwThreadExitCode;
	if (!GetExitCodeThread(hThread,  &dwThreadExitCode))
	{
		DWORD error = GetLastError();
		CloseHandle(hThread);
		SetLastError(error);
		return false;
	}

	// No need for this handle anymore, lets get rid of it.
	CloseHandle(hThread);

	// Lets clear up that memory we allocated earlier.
	VirtualFreeEx(hProcess, RemoteLibraryName, 0, MEM_RELEASE);

	if (dwThreadExitCode == 0) // LoadLibrary failed
	{
		hThread = CreateRemoteThread(hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)GetLastError, (LPVOID)NULL, 0, NULL);
		if (hThread == NULL)
		{
			return false;
		}

		// Get last error
		WaitForSingleObject(hThread, INFINITE);
		if (!GetExitCodeThread(hThread,  &dwThreadExitCode))
		{
			DWORD error = GetLastError();
			CloseHandle(hThread);
			SetLastError(error);
			return false;
		}

		SetLastError(dwThreadExitCode);
		return false;
	}

	return true;
}

BOOL WINAPI CreateProcessW_Detour(
    __in_opt    LPCWSTR lpApplicationName,
    __inout_opt LPWSTR lpCommandLine,
    __in_opt    LPSECURITY_ATTRIBUTES lpProcessAttributes,
    __in_opt    LPSECURITY_ATTRIBUTES lpThreadAttributes,
    __in        BOOL bInheritHandles,
    __in        DWORD dwCreationFlags,
    __in_opt    LPVOID lpEnvironment,
    __in_opt    LPCWSTR lpCurrentDirectory,
    __in        LPSTARTUPINFOW lpStartupInfo,
    __out       LPPROCESS_INFORMATION lpProcessInformation
    )
{
	PROCESS_INFORMATION pi;
	if (lpProcessInformation == NULL)
		lpProcessInformation = &pi;

	// Start our new process with a suspended main thread
	BOOL CreateProcessResult = fpCreateProcessW(lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags | CREATE_SUSPENDED, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation);

	if (CreateProcessResult)
	{
		// Inject our DLL
		// This method returns only when injection thread returns.
		if (!LoadLibraryInjection(lpProcessInformation->hProcess, g_ModuleName))
		{
			PrintError(L"WARNING: Failed to inject DLL %s", g_ModuleName);
		}

		// Once the injection thread has returned it is safe to resume the main thread,
		// but only if the caller hasn't requested CREATE_SUSPENDED themselves.
		if ((dwCreationFlags & CREATE_SUSPENDED) == 0)
			ResumeThread(lpProcessInformation->hThread);
	}

	return CreateProcessResult;
}

//////////////////////////////////////////////////////////////////
{'fraction_non_alphanumeric': 0.15474974463738508, 'fraction_numerical': 0.001787538304392237, 'mean_word_length': 3.2942251461988303, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7426368', 'n_tokens_mistral': 3734, 'n_tokens_neox': 3515, 'n_words': 879}