@@ -53,49 +53,56 @@ typedef struct {
53
53
WCHAR ReparseTarget[1 ];
54
54
} REPARSE_MOUNTPOINT_DATA_BUFFER, * PREPARSE_MOUNTPOINT_DATA_BUFFER;
55
55
56
- static void CreateJunction (LPCSTR szJunction, LPCSTR szPath) {
57
- BYTE buf[sizeof (REPARSE_MOUNTPOINT_DATA_BUFFER) + MAX_PATH * sizeof (WCHAR)];
56
+ static DWORD CreateJunction (LPCSTR szJunction, LPCSTR szPath)
57
+ {
58
+ DWORD LastError = ERROR_SUCCESS;
59
+ std::byte buf[sizeof (REPARSE_MOUNTPOINT_DATA_BUFFER) + MAX_PATH * sizeof (WCHAR)] = {};
58
60
REPARSE_MOUNTPOINT_DATA_BUFFER& ReparseBuffer = (REPARSE_MOUNTPOINT_DATA_BUFFER&)buf;
59
- char szTarget[MAX_PATH] = " \\ ?? \\ " ;
61
+ char szTarget[MAX_PATH] = {} ;
60
62
61
63
strcat_s (szTarget, szPath);
62
64
strcat_s (szTarget, " \\ " );
63
65
64
- if (!:: CreateDirectory (szJunction, NULL )) throw :: GetLastError ();
66
+ if ( ! CreateDirectory (szJunction, nullptr ) ) return GetLastError ();
65
67
66
68
// Obtain SE_RESTORE_NAME privilege (required for opening a directory)
67
- HANDLE hToken = NULL ;
69
+ HANDLE hToken = nullptr ;
68
70
TOKEN_PRIVILEGES tp;
69
71
try {
70
- if (!:: OpenProcessToken (:: GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES, &hToken)) throw :: GetLastError ();
71
- if (!:: LookupPrivilegeValue (NULL , SE_RESTORE_NAME, &tp.Privileges [0 ].Luid )) throw :: GetLastError ();
72
+ if ( ! OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES, &hToken)) throw GetLastError ();
73
+ if ( ! LookupPrivilegeValue (nullptr , SE_RESTORE_NAME, &tp.Privileges [0 ].Luid )) throw GetLastError ();
72
74
tp.PrivilegeCount = 1 ;
73
75
tp.Privileges [0 ].Attributes = SE_PRIVILEGE_ENABLED;
74
- if (!::AdjustTokenPrivileges (hToken, FALSE , &tp, sizeof (TOKEN_PRIVILEGES), NULL , NULL )) throw ::GetLastError ();
76
+ if ( !AdjustTokenPrivileges (hToken, false , &tp, sizeof (TOKEN_PRIVILEGES), nullptr , nullptr ) ) throw GetLastError ();
77
+ }
78
+ catch (DWORD LastError)
79
+ {
80
+ if ( hToken ) CloseHandle (hToken);
81
+ return LastError;
75
82
}
76
- catch (DWORD) {} // Ignore errors
77
- if (hToken) ::CloseHandle (hToken);
83
+ if ( hToken ) CloseHandle (hToken);
78
84
79
- HANDLE hDir = :: CreateFile (szJunction, GENERIC_WRITE, 0 , NULL , OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL );
80
- if ( hDir == INVALID_HANDLE_VALUE) throw :: GetLastError ();
85
+ const HANDLE hDir = CreateFile (szJunction, GENERIC_WRITE, 0 , nullptr , OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, nullptr );
86
+ if ( hDir == INVALID_HANDLE_VALUE ) return GetLastError ();
81
87
82
- memset (buf, 0 , sizeof (buf));
83
88
ReparseBuffer.ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
84
- int len = :: MultiByteToWideChar (CP_ACP, 0 , szTarget, -1 , ReparseBuffer.ReparseTarget , MAX_PATH);
85
- ReparseBuffer.ReparseTargetMaximumLength = ( len--) * sizeof (WCHAR);
86
- ReparseBuffer.ReparseTargetLength = len * sizeof (WCHAR);
89
+ int32_t len = MultiByteToWideChar (CP_ACP, 0 , szTarget, -1 , ReparseBuffer.ReparseTarget , MAX_PATH);
90
+ ReparseBuffer.ReparseTargetMaximumLength = static_cast <WORD>(( len--) * sizeof (WCHAR) );
91
+ ReparseBuffer.ReparseTargetLength = static_cast <WORD>( len * sizeof (WCHAR) );
87
92
ReparseBuffer.ReparseDataLength = ReparseBuffer.ReparseTargetLength + 12 ;
88
93
89
94
DWORD dwRet;
90
- if (!::DeviceIoControl (hDir, FSCTL_SET_REPARSE_POINT, &ReparseBuffer, ReparseBuffer.ReparseDataLength + REPARSE_MOUNTPOINT_HEADER_SIZE, NULL , 0 , &dwRet, NULL )) {
91
- DWORD dr = ::GetLastError ();
92
- ::CloseHandle (hDir);
93
- ::RemoveDirectory (szJunction);
94
- throw dr;
95
+ if ( !DeviceIoControl (hDir, FSCTL_SET_REPARSE_POINT, &ReparseBuffer, ReparseBuffer.ReparseDataLength + REPARSE_MOUNTPOINT_HEADER_SIZE, nullptr , 0 , &dwRet, nullptr ) )
96
+ {
97
+ LastError = GetLastError ();
98
+ CloseHandle (hDir);
99
+ RemoveDirectory (szJunction);
100
+ return LastError;
95
101
}
96
102
97
- ::CloseHandle (hDir);
98
- } // CreateJunction
103
+ CloseHandle (hDir);
104
+ return ERROR_SUCCESS;
105
+ }
99
106
100
107
101
108
void IterateThreads (ThreadCallback ThreadProc, std::uint32_t ProcessID, void * Data)
@@ -139,11 +146,11 @@ int main(int argc, char** argv, char** envp)
139
146
std::uint32_t ProcessID = 0 ;
140
147
std::filesystem::path TargetPath (" C:\\ " );
141
148
bool Logging = false ;
142
- if ( argc > 1 )
149
+ if ( argc > 1 )
143
150
{
144
- for ( std::size_t i = 1 ; i < argc; ++i)
151
+ for ( std::size_t i = 1 ; i < argc; ++i )
145
152
{
146
- if ( std::string_view (argv[i]) == " -h" )
153
+ if ( std::string_view (argv[i]) == " -h" )
147
154
{
148
155
std::cout << " To Set PID:\n " ;
149
156
std::cout << " -p {pid}\n " ;
@@ -156,9 +163,9 @@ int main(int argc, char** argv, char** envp)
156
163
system (" pause" );
157
164
return 0 ;
158
165
}
159
- else if ( std::string_view (argv[i]) == " -p" )
166
+ else if ( std::string_view (argv[i]) == " -p" )
160
167
{
161
- if ( i != argc)
168
+ if ( i != argc)
162
169
{
163
170
ProcessID = (std::uint32_t )atoi (argv[i + 1 ]);
164
171
}
@@ -169,13 +176,13 @@ int main(int argc, char** argv, char** envp)
169
176
return 0 ;
170
177
}
171
178
}
172
- else if ( std::string_view (argv[i]) == " -l" )
179
+ else if ( std::string_view (argv[i]) == " -l" )
173
180
{
174
181
Logging = true ;
175
182
}
176
- else if ( std::string_view (argv[i]) == " -d" )
183
+ else if ( std::string_view (argv[i]) == " -d" )
177
184
{
178
- if ( i != argc)
185
+ if ( i != argc )
179
186
{
180
187
TargetPath = argv[i + 1 ];
181
188
}
@@ -206,31 +213,29 @@ int main(int argc, char** argv, char** envp)
206
213
207
214
IPC::SetClientProcess (GetCurrentProcessId ());
208
215
209
- if ( ProcessID == 0 )
216
+ if ( ProcessID == 0 )
210
217
{
211
218
std::cout << " \033 [93mCurrently running UWP Apps:" << std::endl;
212
219
void * ProcessSnapshot = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0 );
213
220
PROCESSENTRY32 ProcessEntry;
214
221
ProcessEntry.dwSize = sizeof (PROCESSENTRY32);
215
222
216
- if ( Process32First (ProcessSnapshot, &ProcessEntry))
223
+ if ( Process32First (ProcessSnapshot, &ProcessEntry) )
217
224
{
218
- while (Process32Next (ProcessSnapshot, &ProcessEntry))
225
+ while (Process32Next (ProcessSnapshot, &ProcessEntry) )
219
226
{
220
227
void * ProcessHandle = OpenProcess (
221
228
PROCESS_QUERY_LIMITED_INFORMATION,
222
229
false ,
223
230
ProcessEntry.th32ProcessID
224
231
);
225
- if ( ProcessHandle)
232
+ if ( ProcessHandle)
226
233
{
227
234
std::uint32_t NameLength = 0 ;
228
235
std::int32_t ProcessCode = GetPackageFamilyName (
229
- ProcessHandle,
230
- &NameLength,
231
- nullptr
236
+ ProcessHandle, &NameLength, nullptr
232
237
);
233
- if ( NameLength)
238
+ if ( NameLength)
234
239
{
235
240
std::wcout
236
241
<< " \033 [92m"
@@ -249,7 +254,7 @@ int main(int argc, char** argv, char** envp)
249
254
PackageName.get ()
250
255
);
251
256
252
- if ( ProcessCode != ERROR_SUCCESS)
257
+ if ( ProcessCode != ERROR_SUCCESS)
253
258
{
254
259
std::wcout << " GetPackageFamilyName Error: " << ProcessCode;
255
260
}
@@ -275,31 +280,26 @@ int main(int argc, char** argv, char** envp)
275
280
// Get package name
276
281
std::wstring PackageFileName;
277
282
278
- if (
283
+ if (
279
284
HANDLE ProcessHandle = OpenProcess (
280
285
PROCESS_ALL_ACCESS | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
281
- false ,
282
- ProcessID
286
+ false , ProcessID
283
287
); ProcessHandle
284
288
)
285
289
{
286
290
std::uint32_t NameLength = 0 ;
287
291
std::int32_t ProcessCode = GetPackageFamilyName (
288
- ProcessHandle,
289
- &NameLength,
290
- nullptr
292
+ ProcessHandle, &NameLength, nullptr
291
293
);
292
- if ( NameLength)
294
+ if ( NameLength )
293
295
{
294
296
std::unique_ptr<wchar_t []> PackageName (new wchar_t [NameLength]());
295
297
296
298
ProcessCode = GetPackageFamilyName (
297
- ProcessHandle,
298
- &NameLength,
299
- PackageName.get ()
299
+ ProcessHandle, &NameLength, PackageName.get ()
300
300
);
301
301
302
- if ( ProcessCode != ERROR_SUCCESS)
302
+ if ( ProcessCode != ERROR_SUCCESS )
303
303
{
304
304
std::wcout << " GetPackageFamilyName Error: " << ProcessCode;
305
305
}
@@ -320,7 +320,7 @@ int main(int argc, char** argv, char** envp)
320
320
size_t len;
321
321
errno_t err = _dupenv_s (&LocalAppData, &len, " LOCALAPPDATA" );
322
322
323
- if ( TargetPath != std::filesystem::path (" C:\\ " ) )
323
+ if ( TargetPath != std::filesystem::path (" C:\\ " ) )
324
324
{
325
325
// get dump folder path
326
326
std::filesystem::path DumpFolderPath (LocalAppData);
@@ -340,7 +340,7 @@ int main(int argc, char** argv, char** envp)
340
340
IPC::SetTargetProcess (ProcessID);
341
341
342
342
std::cout << " \033 [93mInjecting into remote process: " ;
343
- if ( !DLLInjectRemote (ProcessID, GetRunningDirectory () + L' \\ ' + DLLFile))
343
+ if ( !DLLInjectRemote (ProcessID, GetRunningDirectory () + L' \\ ' + DLLFile) )
344
344
{
345
345
std::cout << " \033 [91mFailed" << std::endl;
346
346
system (" pause" );
@@ -350,9 +350,9 @@ int main(int argc, char** argv, char** envp)
350
350
351
351
std::cout << " \033 [93mWaiting for remote thread IPC:" << std::endl;
352
352
std::chrono::high_resolution_clock::time_point ThreadTimeout = std::chrono::high_resolution_clock::now () + std::chrono::seconds (5 );
353
- while ( IPC::GetTargetThread () == IPC::InvalidThread)
353
+ while ( IPC::GetTargetThread () == IPC::InvalidThread )
354
354
{
355
- if ( std::chrono::high_resolution_clock::now () >= ThreadTimeout)
355
+ if ( std::chrono::high_resolution_clock::now () >= ThreadTimeout )
356
356
{
357
357
std::cout << " \033 [91mRemote thread wait timeout: Unable to find target thread" << std::endl;
358
358
system (" pause" );
@@ -362,7 +362,7 @@ int main(int argc, char** argv, char** envp)
362
362
363
363
std::cout << " Remote Dumper thread found: 0x" << std::hex << IPC::GetTargetThread () << std::endl;
364
364
365
- if ( Logging )
365
+ if ( Logging )
366
366
{
367
367
std::filesystem::path LogFilePath = std::filesystem::current_path ();
368
368
// add package Name to logfile Path
@@ -383,7 +383,7 @@ int main(int argc, char** argv, char** envp)
383
383
LogFilePath.concat (" .txt" );
384
384
std::cout << LogFilePath << std::endl;
385
385
LogFile = std::wofstream (LogFilePath);
386
- if ( LogFile.is_open ())
386
+ if ( LogFile.is_open () )
387
387
{
388
388
std::cout << " \033 [92mLogging to File: " << LogFilePath << " \033 [0m" << std::endl;
389
389
}
@@ -403,15 +403,13 @@ int main(int argc, char** argv, char** envp)
403
403
while ( IPC::PopMessage (CurMessage) )
404
404
{
405
405
std::wcout << CurMessage << " \033 [0m" ;
406
- if ( Logging )
406
+ if ( Logging )
407
407
{
408
408
LogFile << CurMessage << std::flush;
409
409
}
410
410
}
411
411
}
412
- if ( Logging ) {
413
- LogFile.close ();
414
- }
412
+ if ( Logging ) LogFile.close ();
415
413
system (" pause" );
416
414
return EXIT_SUCCESS;
417
415
}
0 commit comments