forked from taviso/ctftool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinutil.c
141 lines (119 loc) · 3.85 KB
/
winutil.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#define WIN32_LEAN_AND_MEAN
#define WIN32_NO_STATUS
#include <windows.h>
#include <winternl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <tlhelp32.h>
#include <psapi.h>
#include <assert.h>
#include <objbase.h>
#include <sddl.h>
#include <msctf.h>
#include <shlwapi.h>
#undef WIN32_NO_STATUS
#include <ntstatus.h>
#include "ntdll.h"
#include "ntalpctyp.h"
#include "ntalpc.h"
#include "ctfinternal.h"
#include "ctftool.h"
#include "marshal.h"
#include "util.h"
#include "winutil.h"
#pragma warning(disable: 6011 6387)
// This just duplicates the logic that ctfmon uses for identifying Windows.
BOOL GetActiveThreadInfo(PDWORD ThreadId, HWND *Window, PDWORD ProcessId)
{
HWND IMEWindow = 0;
WCHAR ClassName[32] = {0};
*Window = GetForegroundWindow();
if (*Window) {
RealGetWindowClassW(*Window, ClassName, _countof(ClassName) - 1);
if (wcscmp(ClassName, L"ConsoleWindowClass") == 0) {
//LogMessage(stdout, "The active window uses ConsoleWindowClass.");
IMEWindow = ImmGetDefaultIMEWnd(*Window);
}
}
if (IMEWindow) {
*ThreadId = GetWindowThreadProcessId(IMEWindow, ProcessId);
} else {
*ThreadId = GetWindowThreadProcessId(*Window, ProcessId);
}
return *Window != NULL;
}
DWORD GetFocusThread(void)
{
HWND Window;
DWORD ThreadId;
GetActiveThreadInfo(&ThreadId, &Window, NULL);
return ThreadId;
}
#define MAX_PROCESS 16384
PVOID QueryImageName(DWORD ProcessId)
{
CHAR ImageName[MAX_PATH];
ULONG ReturnLength;
PSYSTEM_PROCESS_INFORMATION ProcessArray, Current;
PVOID ReturnValue = NULL;
NTSTATUS Result;
ReturnLength = MAX_PROCESS * sizeof(SYSTEM_PROCESS_INFORMATION);
ProcessArray = malloc(ReturnLength);
Result = NtQuerySystemInformation(SystemProcessInformation, ProcessArray, ReturnLength, &ReturnLength);
if (Result != 0) {
LogMessage(stderr, "Unexpected NtQuerySystemInformation() result, %#x", Result);
return NULL;
}
for (Current = ProcessArray;; Current = (PVOID)((PBYTE)(Current) + Current->NextEntryOffset)) {
#pragma warning(suppress: 4047)
if (Current->UniqueProcessId == ProcessId) {
snprintf(ImageName, sizeof ImageName, "%wZ", &Current->ImageName);
ReturnValue = strdup(ImageName);
break;
}
if (Current->NextEntryOffset == 0) {
LogMessage(stderr, "QueryImageName, Unknown Process %u", ProcessId);
break;
}
}
free(ProcessArray);
return ReturnValue;
}
// This finds the first process with matching ImageName, and returns it's SessionId.
DWORD GetSessionIdByImageName(PCHAR ImageName)
{
CHAR CurrentImageName[MAX_PATH];
ULONG ReturnLength;
PSYSTEM_PROCESS_INFORMATION ProcessArray, Current;
DWORD ReturnValue = 0;
NTSTATUS Result;
ReturnLength = MAX_PROCESS * sizeof(SYSTEM_PROCESS_INFORMATION);
ProcessArray = malloc(ReturnLength);
Result = NtQuerySystemInformation(SystemProcessInformation, ProcessArray, ReturnLength, &ReturnLength);
if (Result != 0) {
LogMessage(stderr, "Unexpected NtQuerySystemInformation() result, %#x", Result);
return 0;
}
for (Current = ProcessArray;; Current = (PVOID)((PBYTE)(Current) + Current->NextEntryOffset)) {
snprintf(CurrentImageName, sizeof CurrentImageName, "%wZ", &Current->ImageName);
if (stricmp(CurrentImageName, ImageName) == 0) {
ReturnValue = Current->SessionId;
break;
}
if (Current->NextEntryOffset == 0) {
LogMessage(stderr, "QuerySessionIdByImageName, Unknown Process %s", ImageName);
break;
}
}
free(ProcessArray);
return ReturnValue;
}
UINT64 QueryModuleHandle32(PCHAR ModuleName)
{
HMODULE Module = LoadLibrary(ModuleName);
if (Module) {
FreeLibrary(Module);
}
return (UINT64) Module;
}