1010namespace
1111{
1212 // Resolve MSVC incremental-link jump stubs (ILT): E9 xx xx xx xx → target
13+
14+ [[nodiscard]]
1315 uint8_t * resolve_ilt (void * fn)
1416 {
1517 auto * p = static_cast <uint8_t *>(fn);
@@ -214,11 +216,12 @@ namespace
214216 if (delta == 0 )
215217 return ;
216218
217- auto & reloc_dir = nt_headers->OptionalHeader .DataDirectory [IMAGE_DIRECTORY_ENTRY_BASERELOC];
218- if (!reloc_dir.Size )
219+ // ReSharper disable once CppUseStructuredBinding
220+ const auto & relocation_directory = nt_headers->OptionalHeader .DataDirectory [IMAGE_DIRECTORY_ENTRY_BASERELOC];
221+ if (!relocation_directory.Size )
219222 return ;
220223
221- auto * block = reinterpret_cast <IMAGE_BASE_RELOCATION*>(local_image + reloc_dir .VirtualAddress );
224+ auto * block = reinterpret_cast <IMAGE_BASE_RELOCATION*>(local_image + relocation_directory .VirtualAddress );
222225 while (block->SizeOfBlock && block->VirtualAddress )
223226 {
224227 const size_t count = (block->SizeOfBlock - sizeof (IMAGE_BASE_RELOCATION)) / sizeof (WORD);
@@ -235,9 +238,11 @@ namespace
235238
236239 nt_headers->OptionalHeader .ImageBase = target_base;
237240 }
241+ [[nodiscard]]
238242 std::optional<std::uintptr_t > get_process_id_by_name (const std::string_view& process_name)
239243 {
240- const HANDLE snap = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0 );
244+ // ReSharper disable once CppLocalVariableMayBeConst
245+ HANDLE snap = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0 );
241246 if (snap == INVALID_HANDLE_VALUE)
242247 return 0 ;
243248
@@ -249,7 +254,7 @@ namespace
249254 {
250255 do
251256 {
252- if (_stricmp (pe.szExeFile , process_name. data ()) == 0 )
257+ if (std::string_view (pe.szExeFile ) == process_name )
253258 {
254259 pid = pe.th32ProcessID ;
255260 break ;
@@ -272,11 +277,12 @@ namespace yail
272277 return std::unexpected (" File is not in a Portable Executable format" );
273278
274279 // Open target process
275- const HANDLE h_process = OpenProcess (PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ
280+ // ReSharper disable once CppLocalVariableMayBeConst
281+ HANDLE process_handle = OpenProcess (PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ
276282 | PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION,
277- FALSE , process_id);
283+ FALSE , static_cast <DWORD>( process_id) );
278284
279- if (!h_process )
285+ if (!process_handle )
280286 return std::unexpected (" Failed to open target process (error " + std::to_string (GetLastError ()) + " )" );
281287
282288 const auto * dos = reinterpret_cast <const IMAGE_DOS_HEADER*>(raw_dll.data ());
@@ -285,11 +291,11 @@ namespace yail
285291
286292 // Allocate image memory in target process
287293 auto * remote_image = static_cast <uint8_t *>(
288- VirtualAllocEx (h_process , nullptr , image_size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE));
294+ VirtualAllocEx (process_handle , nullptr , image_size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE));
289295
290296 if (!remote_image)
291297 {
292- CloseHandle (h_process );
298+ CloseHandle (process_handle );
293299 return std::unexpected (" VirtualAllocEx failed for image (error " + std::to_string (GetLastError ()) + " )" );
294300 }
295301
@@ -310,10 +316,10 @@ namespace yail
310316 relocate_for_base (local_image.data (), reinterpret_cast <uintptr_t >(remote_image));
311317
312318 // Write image to target
313- if (!WriteProcessMemory (h_process , remote_image, local_image.data (), image_size, nullptr ))
319+ if (!WriteProcessMemory (process_handle , remote_image, local_image.data (), image_size, nullptr ))
314320 {
315- VirtualFreeEx (h_process , remote_image, 0 , MEM_RELEASE);
316- CloseHandle (h_process );
321+ VirtualFreeEx (process_handle , remote_image, 0 , MEM_RELEASE);
322+ CloseHandle (process_handle );
317323 return std::unexpected (" WriteProcessMemory failed for image" );
318324 }
319325
@@ -328,12 +334,12 @@ namespace yail
328334 const size_t total_shellcode = data_aligned + size_of_shell_code;
329335
330336 auto * remote_shellcode = static_cast <uint8_t *>(
331- VirtualAllocEx (h_process , nullptr , total_shellcode, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE));
337+ VirtualAllocEx (process_handle , nullptr , total_shellcode, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE));
332338
333339 if (!remote_shellcode)
334340 {
335- VirtualFreeEx (h_process , remote_image, 0 , MEM_RELEASE);
336- CloseHandle (h_process );
341+ VirtualFreeEx (process_handle , remote_image, 0 , MEM_RELEASE);
342+ CloseHandle (process_handle );
337343 return std::unexpected (" VirtualAllocEx failed for shellcode" );
338344 }
339345
@@ -358,25 +364,26 @@ namespace yail
358364 std::memcpy (shell_code_page.data () + data_aligned, shell_code_start, size_of_shell_code);
359365
360366 // Write shellcode page to target
361- if (!WriteProcessMemory (h_process , remote_shellcode, shell_code_page.data (), total_shellcode, nullptr ))
367+ if (!WriteProcessMemory (process_handle , remote_shellcode, shell_code_page.data (), total_shellcode, nullptr ))
362368 {
363- VirtualFreeEx (h_process , remote_shellcode, 0 , MEM_RELEASE);
364- VirtualFreeEx (h_process , remote_image, 0 , MEM_RELEASE);
365- CloseHandle (h_process );
369+ VirtualFreeEx (process_handle , remote_shellcode, 0 , MEM_RELEASE);
370+ VirtualFreeEx (process_handle , remote_image, 0 , MEM_RELEASE);
371+ CloseHandle (process_handle );
366372 return std::unexpected (" WriteProcessMemory failed for shellcode" );
367373 }
368374
369375 // Create remote thread: entry = shellcode code, param = RemoteLoaderData*
370- const HANDLE thread_handle = CreateRemoteThread (
371- h_process, nullptr , 0 , reinterpret_cast <LPTHREAD_START_ROUTINE>(remote_shellcode + data_aligned),
376+ // ReSharper disable once CppLocalVariableMayBeConst
377+ HANDLE thread_handle = CreateRemoteThread (
378+ process_handle, nullptr , 0 , reinterpret_cast <LPTHREAD_START_ROUTINE>(remote_shellcode + data_aligned),
372379 remote_shellcode, // lpParameter → points to RemoteLoaderData
373380 0 , nullptr );
374381
375382 if (!thread_handle)
376383 {
377- VirtualFreeEx (h_process , remote_shellcode, 0 , MEM_RELEASE);
378- VirtualFreeEx (h_process , remote_image, 0 , MEM_RELEASE);
379- CloseHandle (h_process );
384+ VirtualFreeEx (process_handle , remote_shellcode, 0 , MEM_RELEASE);
385+ VirtualFreeEx (process_handle , remote_image, 0 , MEM_RELEASE);
386+ CloseHandle (process_handle );
380387 return std::unexpected (" CreateRemoteThread failed (error " + std::to_string (GetLastError ()) + " )" );
381388 }
382389
@@ -387,8 +394,8 @@ namespace yail
387394 CloseHandle (thread_handle);
388395
389396 // Free shellcode page — no longer needed after init
390- VirtualFreeEx (h_process , remote_shellcode, 0 , MEM_RELEASE);
391- CloseHandle (h_process );
397+ VirtualFreeEx (process_handle , remote_shellcode, 0 , MEM_RELEASE);
398+ CloseHandle (process_handle );
392399
393400 if (exit_code != 0 )
394401 return std::unexpected (" Remote shellcode failed (exit code " + std::to_string (exit_code) + " )" );
0 commit comments