Skip to content

Commit

Permalink
Fix assertion and add tests
Browse files Browse the repository at this point in the history
- The jump table is only generated to OUR executable, not e.g. a linked library :-D
  • Loading branch information
visuve committed Apr 2, 2024
1 parent dc968b2 commit 6dfa99c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
13 changes: 7 additions & 6 deletions HackLib/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,13 +517,14 @@ std::vector<uint8_t> Process::ReadFunction(void(*function)(void), size_t size)
uint8_t jump[5] = {};
process.ReadProcessMemory(address, jump, sizeof(jump));

_ASSERTE(jump[0] == X86::JmpJz);

// Make absolute
address += jump[1] | (jump[2] << 8) | (jump[3] << 16) | (jump[4] << 24);
address += 5; // Size of relative jump
if (jump[0] == X86::JmpJz)
{
// Make absolute
address += jump[1] | (jump[2] << 8) | (jump[3] << 16) | (jump[4] << 24);
address += 5; // Size of relative jump

LogDebug << function << "jumps to" << address;
LogDebug << function << "jumps to" << address;
}
#endif

size_t bytesRead = 0;
Expand Down
56 changes: 52 additions & 4 deletions HackLibTests/ProcessTests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include "Process.hpp"

extern "C" __declspec(dllexport) int ThisFunctionActuallyExists(int x)
{
return x * 2;
}

TEST(ProcessTests, Header)
{
DWORD pid = GetCurrentProcessId();
Expand Down Expand Up @@ -47,7 +52,7 @@ TEST(ProcessTests, ModuleNotFound)
"Neither does this function"), std::range_error);
}

TEST(ProcessTests, FunctionNotFound)
TEST(ProcessTests, ImportNotFound)
{
DWORD pid = GetCurrentProcessId();
Process currentProcess(pid);
Expand All @@ -57,16 +62,59 @@ TEST(ProcessTests, FunctionNotFound)
"Neither does this function"), std::range_error);
}

TEST(ProcessTests, FunctionFound)
TEST(ProcessTests, ImportFound)
{
DWORD pid = GetCurrentProcessId();
Process currentProcess(pid);

// It's just called above. It has to be found
Pointer fnptr =
Pointer actual =
currentProcess.FindImportAddress("KERNEL32.dll", "GetCurrentProcessId");

EXPECT_NE(fnptr, Pointer());
EXPECT_NE(actual, Pointer());
}

TEST(ProcessTests, FunctionNotFound)
{
DWORD pid = GetCurrentProcessId();
Process currentProcess(pid);

EXPECT_THROW(currentProcess.FindFunctionAddress("Derp", "GetCurrentProcessId"), std::runtime_error);
EXPECT_THROW(currentProcess.FindFunctionAddress("KERNEL32.dll", "Derp"), std::runtime_error);
}

TEST(ProcessTests, FunctionFoundExternal)
{
void* getCurrentProcessId = GetCurrentProcessId;

DWORD pid = GetCurrentProcessId();
Process currentProcess(pid);

Pointer actual =
currentProcess.FindFunctionAddress("KERNEL32.dll", "GetCurrentProcessId");

EXPECT_NE(actual, Pointer());

Pointer expected(getCurrentProcessId);

EXPECT_EQ(actual, expected);
}

TEST(ProcessTests, FunctionFoundInternal)
{
void* thisFunctionActuallyExists = ThisFunctionActuallyExists;

DWORD pid = GetCurrentProcessId();
Process currentProcess(pid);

Pointer actual =
currentProcess.FindFunctionAddress("HackLibTests.exe", "ThisFunctionActuallyExists");

EXPECT_NE(actual, Pointer());

Pointer expected(thisFunctionActuallyExists);

EXPECT_EQ(actual, expected);
}

TEST(ProcessTests, ResolveSecret)
Expand Down

0 comments on commit 6dfa99c

Please sign in to comment.