Skip to content

Commit

Permalink
Refactor win32 code
Browse files Browse the repository at this point in the history
  • Loading branch information
shmocz committed Aug 21, 2023
1 parent d4e072d commit 9070f00
Show file tree
Hide file tree
Showing 23 changed files with 561 additions and 434 deletions.
6 changes: 5 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ IncludeCategories:
Priority: 3
- Regex: "<c(stdio|stdint|string|stddef)>"
Priority: 4
- Regex: "<.+>"
- Regex: "<(errhandling|handle|libloader|memory|processthreads|ps|synch)api.h>"
Priority: 5
- Regex: "<(direct|malloc|minwindef|tlhelp32|winbase).h>"
Priority: 5
- Regex: "<.+>"
Priority: 6
8 changes: 6 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ if(NOT PROTO_LIB)
endif()
target_link_libraries(ra2yrcpp_core PUBLIC fmt::fmt "${PROTO_LIB}" protocol)
if(WIN32)
target_link_libraries(ra2yrcpp_core PUBLIC ${LIB_WSOCK32} ${LIB_WS2_32})
add_library(windows_utils STATIC win32/win_message.cpp
win32/windows_utils.cpp)
target_link_libraries(ra2yrcpp_core PUBLIC windows_utils ${LIB_WSOCK32}
${LIB_WS2_32})
endif()

target_compile_options(ra2yrcpp_core PUBLIC ${RA2YRCPP_EXTRA_FLAGS})
Expand All @@ -44,7 +47,6 @@ if(RA2YRCPP_BUILD_MAIN_DLL)
commands_yr.cpp
context.cpp
dll_inject.cpp
exprocess.cpp
hooks_yr.cpp
is_context.cpp
ra2/abi.cpp
Expand All @@ -62,6 +64,8 @@ if(RA2YRCPP_BUILD_MAIN_DLL)
fmt::fmt
YRpp)

target_link_libraries(yrclient PUBLIC windows_utils)

if(WIN32)
target_link_libraries(yrclient PUBLIC ${LIB_WSOCK32} ${LIB_WS2_32})
endif()
Expand Down
3 changes: 2 additions & 1 deletion src/addscn/addscn.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Based on this: https://github.com/hMihaiDavid/addscn
#include <cstring>

#include <memoryapi.h>

#include <exception>
#include <iostream>
#include <memory>
#include <memoryapi.h>
#include <string>
#include <windows.h>
#include <winnt.h>
Expand Down
6 changes: 4 additions & 2 deletions src/commands_builtin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ std::map<std::string, command::Command::handler_t> get_commands_nn() {
Q->I(), "", 0u);
}),
get_cmd<ra2yrproto::commands::CreateHooks>([](auto* Q) {
// TODO(shmocz): put these to utility function and share code with
// Hook code.
// TODO(shmocz): put these to utility function and share code with
// Hook code.
#ifdef _WIN32
auto P = process::get_current_process();
std::vector<process::thread_id_t> ns(Q->I()->get_connection_threads());

Expand All @@ -117,6 +118,7 @@ std::map<std::string, command::Command::handler_t> get_commands_nn() {
if (!a.no_suspend_threads()) {
P.resume_threads(ns);
}
#endif
})};
}

Expand Down
8 changes: 2 additions & 6 deletions src/dll_inject.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#include "dll_inject.hpp"

#include "process.hpp"
#include "types.h"
#include "utility/time.hpp"

#include <handleapi.h>
#include <stdexcept>
#include <tlhelp32.h>
#include <winnt.h>

using namespace dll_inject;
using process::x86Reg;

void dll_inject::inject_code(process::Process* P, int thread_id,
vecu8 shellcode) {
Expand All @@ -20,8 +17,7 @@ void dll_inject::inject_code(process::Process* P, int thread_id,
esp -= sizeof(esp);
P->write_memory(reinterpret_cast<void*>(esp), &eip, sizeof(eip));
// Allocate memory for shellcode
auto sc_addr =
P->allocate_memory(shellcode.size(), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
auto sc_addr = P->allocate_code(shellcode.size());
// Write shellcode
P->write_memory(sc_addr, shellcode.data(), shellcode.size());
// Set ESP
Expand Down
17 changes: 3 additions & 14 deletions src/errors.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "errors.hpp"

#include <cstddef>
#ifdef _WIN32
#include <windows.h>
#include "win32/win_message.hpp"
#elif __linux__
#include <cstring>

Expand All @@ -13,7 +12,7 @@ using namespace yrclient;

int yrclient::get_last_error() {
#ifdef _WIN32
return GetLastError();
return static_cast<int>(windows_utils::get_last_error());
#elif __linux__
return errno;
#else
Expand All @@ -39,17 +38,7 @@ std::string yrclient::get_error_message(const int error_code) {
return std::string();
}
#ifdef _WIN32
char* buf = nullptr;
std::size_t size = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&buf,
0, NULL);

std::string message(buf, size);
LocalFree(buf);
// Remove \r\n
return message.substr(0, message.find("\r\n"));
return windows_utils::get_error_message(error_code);
#elif __linux__
return strerror(error_code);
#else
Expand Down
61 changes: 0 additions & 61 deletions src/exprocess.cpp

This file was deleted.

27 changes: 0 additions & 27 deletions src/exprocess.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion src/hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ unsigned int num_threads_at_tgt(const process::Process& P, const u8* target,
P.for_each_thread([&ips, &main_tid](process::Thread* T, void* ctx) {
(void)ctx;
if (T->id() != main_tid) {
ips.push_back(*T->get_pgpr(process::x86Reg::eip));
ips.push_back(*T->get_pgpr(x86Reg::eip));
}
});
unsigned int res = 0;
Expand Down
12 changes: 4 additions & 8 deletions src/is_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "types.h"
#include "utility/sync.hpp"
#include "utility/time.hpp"
#include "win32/windows_utils.hpp"
#include "x86.hpp"

#include <fmt/core.h>
Expand All @@ -36,16 +37,11 @@ using namespace std::chrono_literals;
using namespace is_context;
using x86::bytes_to_stack;

u32 is_context::get_proc_address(const std::string addr) {
return reinterpret_cast<u32>(
GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), addr.c_str()));
}

// TODO(shmocz): utilize in tests
ProcAddrs is_context::get_procaddrs() {
ProcAddrs A;
A.p_LoadLibrary = get_proc_address("LoadLibraryA");
A.p_GetProcAddress = get_proc_address("GetProcAddress");
A.p_LoadLibrary = windows_utils::get_proc_address("LoadLibraryA");
A.p_GetProcAddress = windows_utils::get_proc_address("GetProcAddress");
return A;
}

Expand Down Expand Up @@ -119,7 +115,7 @@ DLLLoader::DLLLoader(u32 p_LoadLibrary, u32 p_GetProcAddress,
x86::restore_regs(this);
}

void is_context::get_procaddr(Xbyak::CodeGenerator* c, HMODULE m,
void is_context::get_procaddr(Xbyak::CodeGenerator* c, void* m,
const std::string name,
const u32 p_GetProcAddress) {
using namespace Xbyak::util;
Expand Down
4 changes: 1 addition & 3 deletions src/is_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ class Context;
namespace is_context {
using context::Context;

u32 get_proc_address(const std::string addr);

struct DLLInjectOptions {
unsigned delay_pre;
unsigned delay_post;
Expand All @@ -40,7 +38,7 @@ struct ProcAddrs {
ProcAddrs get_procaddrs();
vecu8 vecu8cstr(const std::string s);

void get_procaddr(Xbyak::CodeGenerator* c, HMODULE m, const std::string name,
void get_procaddr(Xbyak::CodeGenerator* c, void* m, const std::string name,
const u32 p_GetProcAddress);

struct DLLLoader : Xbyak::CodeGenerator {
Expand Down
Loading

0 comments on commit 9070f00

Please sign in to comment.