-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathproctype.c
168 lines (154 loc) · 4.76 KB
/
proctype.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Test for a valid process (i386 for x86; that or AMD64 for x64). We can get
that info from the image header, which means getting the process's base
address (which we need anyway, to modify the imports). The simplest way to
do that is to enumerate the pages, looking for an executable image. A .NET
AnyCPU process has a 32-bit structure, but will load as 64-bit when possible.
The 64-bit version (both DLLs) will say this is type 48 (halfway between 32 &
64); the 32-bit version will ignore it if run on a 64-bit OS.
*/
#include "ansicon.h"
#if !defined(_WIN64) && !defined(W32ON64)
static BOOL ProcessIs64( HANDLE hProcess )
{
BOOL wow;
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS)( HANDLE, PBOOL );
static LPFN_ISWOW64PROCESS fnIsWow64Process;
if (fnIsWow64Process == INVALID_HANDLE_VALUE)
return FALSE;
if (fnIsWow64Process == NULL)
{
fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(
GetModuleHandle( L"kernel32.dll" ), "IsWow64Process" );
if (fnIsWow64Process == NULL)
{
fnIsWow64Process = INVALID_HANDLE_VALUE;
return FALSE;
}
}
// If IsWow64Process fails, say it is 64, since injection probably wouldn't
// work, either.
return !(fnIsWow64Process( hProcess, &wow ) && wow);
}
#endif
int ProcessType( LPPROCESS_INFORMATION ppi, PBYTE* pBase, BOOL* gui )
{
PBYTE ptr;
MEMORY_BASIC_INFORMATION minfo;
IMAGE_DOS_HEADER dos_header;
IMAGE_NT_HEADERS nt_header;
PBYTE dummy_base;
BOOL dummy_gui;
int log;
// Don't log if we're only getting one value, as it's already been logged.
log = 1;
if (pBase == NULL)
{
pBase = &dummy_base;
log = 128;
}
if (gui == NULL)
{
gui = &dummy_gui;
log = 128;
}
*pBase = NULL;
*gui = FALSE;
for (ptr = NULL;
VirtualQueryEx( ppi->hProcess, ptr, &minfo, sizeof(minfo) );
ptr += minfo.RegionSize)
{
if (minfo.BaseAddress == minfo.AllocationBase
&& ReadProcVar( minfo.BaseAddress, &dos_header )
&& dos_header.e_magic == IMAGE_DOS_SIGNATURE
&& ReadProcVar( (PBYTE)minfo.BaseAddress + dos_header.e_lfanew,
&nt_header )
&& nt_header.Signature == IMAGE_NT_SIGNATURE
&& !(nt_header.FileHeader.Characteristics & IMAGE_FILE_DLL))
{
// Don't load into ansicon.exe, it's already imported.
if (nt_header.OptionalHeader.MajorImageVersion == 20033 && // 'AN'
nt_header.OptionalHeader.MinorImageVersion == 18771) // 'SI'
return -1;
*pBase = minfo.BaseAddress;
if (nt_header.OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
*gui = TRUE;
if (*gui ||
nt_header.OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
{
if (nt_header.FileHeader.Machine == IMAGE_FILE_MACHINE_I386)
{
PIMAGE_NT_HEADERS32 pNTHeader = (PIMAGE_NT_HEADERS32)&nt_header;
if (pNTHeader->DATADIRS > IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR &&
pNTHeader->COMDIR.VirtualAddress != 0)
{
IMAGE_COR20_HEADER ComHeader, *pComHeader;
pComHeader = (PIMAGE_COR20_HEADER)((PBYTE)minfo.BaseAddress
+ pNTHeader->COMDIR.VirtualAddress);
ReadProcVar( pComHeader, &ComHeader );
if ((ComHeader.Flags & COMIMAGE_FLAGS_ILONLY) &&
!(ComHeader.Flags & COMIMAGE_FLAGS_32BITREQUIRED))
{
DEBUGSTR( log, " AnyCPU %s (base = %q)",
(*gui) ? "GUI" : "console", minfo.BaseAddress );
#if defined(_WIN64) || defined(W32ON64)
return 48;
#else
if (ProcessIs64( ppi->hProcess ))
{
DEBUGSTR( log, " Unsupported (use x64\\ansicon)" );
return 0;
}
return 32;
#endif
}
}
DEBUGSTR( log, " 32-bit %s (base = %q)",
(*gui) ? "GUI" : "console", minfo.BaseAddress );
return 32;
}
if (nt_header.FileHeader.Machine == IMAGE_FILE_MACHINE_AMD64)
{
#ifdef _WIN64
DEBUGSTR( log, " 64-bit %s (base = %p)",
(*gui) ? "GUI" : "console", minfo.BaseAddress );
return 64;
#else
DEBUGSTR( log, " 64-bit %s (base = %P)",
(*gui) ? "GUI" : "console", minfo.BaseAddress );
#if defined(W32ON64)
return 64;
#else
DEBUGSTR( log, " Unsupported (use x64\\ansicon)" );
return 0;
#endif
#endif
}
DEBUGSTR( log, " Ignoring unsupported machine (0x%X)",
nt_header.FileHeader.Machine );
}
else
{
DEBUGSTR( log, " Ignoring unsupported subsystem (%u)",
nt_header.OptionalHeader.Subsystem );
}
return 0;
}
#ifndef _WIN64
// If a 32-bit process loads a 64-bit one, we may miss the base
// address. If the pointer overflows, assume 64-bit.
if (((DWORD)ptr >> 12) + ((DWORD)minfo.RegionSize >> 12) >= 0x100000)
{
#ifdef W32ON64
DEBUGSTR( log, " Pointer overflow: assuming 64-bit" );
return 64;
#else
DEBUGSTR( log, " Ignoring apparent 64-bit process (use x64\\ansicon)" );
return 0;
#endif
}
#endif
}
DEBUGSTR( log, " Ignoring non-Windows process" );
return 0;
}