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
169
170
171
172
173
174
|
/********************************************************/
/* ntapi: Native API core library */
/* Copyright (C) 2013--2021 SysDeer Technologies, LLC */
/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
/********************************************************/
#include <psxtypes/psxtypes.h>
#include <ntapi/nt_memory.h>
#include <ntapi/nt_process.h>
#include <ntapi/ntapi.h>
#include "ntapi_impl.h"
typedef struct _nt_process_basic_information nt_pbi;
int32_t __stdcall __ntapi_tt_create_remote_runtime_data(
__in void * hprocess,
__in_out nt_runtime_data_block * rtblock)
{
int32_t status;
size_t bytes_written;
nt_pbi rpbi;
nt_process_parameters * rprocess_params;
nt_unicode_string rcmd_line;
uint32_t runtime_arg_hash;
nt_runtime_data * rtdata;
void * hserver;
#if (__SIZEOF_POINTER__ == 4)
wchar16_t runtime_arg[8] = {
'i','n','t','e','g','r','a','l'};
#elif (__SIZEOF_POINTER__ == 8)
wchar16_t runtime_arg[16] = {
'i','n','t','e','g','r','a','l',
'-','r','u','n','t','i','m','e'};
#endif
/* validation */
if (!hprocess)
return NT_STATUS_INVALID_PARAMETER_1;
else if (!rtblock)
return NT_STATUS_INVALID_PARAMETER_2;
else if (!rtblock->addr)
return NT_STATUS_INVALID_PARAMETER_2;
else if (!rtblock->size)
return NT_STATUS_INVALID_PARAMETER_2;
runtime_arg_hash = __ntapi->tt_buffer_crc32(
0,
(char *)runtime_arg,
sizeof(runtime_arg));
/* obtain process information */
if ((status = __ntapi->zw_query_information_process(
hprocess,
NT_PROCESS_BASIC_INFORMATION,
(void *)&rpbi,
sizeof(nt_process_basic_information),
0)))
return status;
if ((status = __ntapi->zw_read_virtual_memory(
hprocess,
pe_va_from_rva(
rpbi.peb_base_address,
(uintptr_t)&(((nt_peb *)0)->process_params)),
(char *)&rprocess_params,
sizeof(uintptr_t),
&bytes_written)))
return status;
if ((status = __ntapi->zw_read_virtual_memory(
hprocess,
&rprocess_params->command_line,
(char *)&rcmd_line,
sizeof(nt_unicode_string),
&bytes_written)))
return status;
if (rcmd_line.buffer == 0)
return NT_STATUS_BUFFER_TOO_SMALL;
if (rcmd_line.strlen < sizeof(runtime_arg) + 4*sizeof(wchar16_t))
return NT_STATUS_INVALID_USER_BUFFER;
if ((status = __ntapi->zw_read_virtual_memory(
hprocess,
pe_va_from_rva(
rcmd_line.buffer,
rcmd_line.strlen - sizeof(runtime_arg)),
(char *)&runtime_arg,
sizeof(runtime_arg),
&bytes_written)))
return status;
/* verify remote process compatibility */
runtime_arg_hash ^= __ntapi->tt_buffer_crc32(
0,(char *)runtime_arg,
sizeof(runtime_arg));
if (runtime_arg_hash)
return NT_STATUS_INVALID_SIGNATURE;
/* remote block */
rtblock->remote_size = rtblock->size;
if ((status = __ntapi->zw_allocate_virtual_memory(
hprocess,
&rtblock->remote_addr,
0,
&rtblock->remote_size,
NT_MEM_RESERVE | NT_MEM_COMMIT,
NT_PAGE_READWRITE)))
return status;
/* session handles */
rtdata = 0;
hserver = 0;
if (rtblock->flags & NT_RUNTIME_DATA_DUPLICATE_SESSION_HANDLES) {
rtdata = (nt_runtime_data *)rtblock->addr;
hserver = rtdata->hserver;
if ((status = __ntapi->zw_duplicate_object(
NT_CURRENT_PROCESS_HANDLE,
hserver,
hprocess,
&rtdata->hserver,
0,0,
NT_DUPLICATE_SAME_ATTRIBUTES
|NT_DUPLICATE_SAME_ACCESS)))
return status;
}
/* copy local block to remote process */
status = __ntapi->zw_write_virtual_memory(
hprocess,
rtblock->remote_addr,
(char *)rtblock->addr,
rtblock->size,
&bytes_written);
/* restore rtdata */
if (rtdata)
rtdata->hserver = hserver;
/* verify above remote write */
if (status)
return status;
/* runtime_arg */
__ntapi->tt_uintptr_to_hex_utf16(
(uintptr_t)rtblock->remote_addr,
runtime_arg);
/* update remote runtime arg */
status = __ntapi->zw_write_virtual_memory(
hprocess,
pe_va_from_rva(
rcmd_line.buffer,
rcmd_line.strlen - sizeof(runtime_arg)),
(char *)&runtime_arg,
sizeof(runtime_arg),
&bytes_written);
if (status)
__ntapi->zw_free_virtual_memory(
hprocess,
&rtblock->remote_addr,
&rtblock->remote_size,
NT_MEM_RELEASE);
return status;
}
|