Added IPC and lua definitions

This commit is contained in:
Anonymous275 2022-01-27 02:43:35 +02:00
parent bea720d0b7
commit 6c11de2708
8 changed files with 181 additions and 17 deletions

View File

@ -31,10 +31,11 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG")
add_executable(${PROJECT_NAME}
src/main.cpp include/easyloggingpp/src/easylogging++.cc
src/Launcher.cpp include/Launcher.h
src/Launcher.cpp include/Launcher.h include/Memory/Definitions.h
src/Memory/Memory.cpp include/Memory/Memory.h include/Memory/Patterns.h
src/Memory/Detours.cpp include/Memory/Detours.h
src/Memory/BeamNG.cpp include/Memory/BeamNG.h
src/Memory/IPC.cpp include/Memory/IPC.h
src/Logger.cpp include/Logger.h
src/gui/Gui.cpp include/Json.h
src/gui/gifs.cpp src/gui/gifs.h

View File

@ -4,9 +4,27 @@
///
#pragma once
#include "Memory/Detours.h"
#include "Definitions.h"
#include <cstdint>
#include <memory>
class BeamNG {
public:
static void EntryPoint();
private:
static std::unique_ptr<Detours> TickCountDetour;
static std::unique_ptr<Detours> OpenJITDetour;
static int lua_open_jit_D(lua_State* State);
static uint32_t GetTickCount_D();
static uint64_t GameBaseAddr;
static uint64_t DllBaseAddr;
static def::GetTickCount GetTickCount;
static def::lua_open_jit lua_open_jit;
static def::lua_push_fstring lua_push_fstring;
static def::lua_get_field lua_get_field;
static def::lua_p_call lua_p_call;
static const char* GameModule;
static const char* DllModule;
static lua_State* GEState;
};

View File

@ -0,0 +1,15 @@
///
/// Created by Anonymous275 on 1/27/22
/// Copyright (c) 2021-present Anonymous275 read the LICENSE file for more info.
///
#pragma once
typedef struct lua_State lua_State;
namespace def {
typedef unsigned long (*GetTickCount)();
typedef int (*lua_open_jit)(lua_State* L);
typedef void (*lua_get_field)(lua_State* L, int idx, const char* k);
typedef const char* (*lua_push_fstring)(lua_State* L, const char* fmt, ...);
typedef int(*lua_p_call)(lua_State* L, int arg, int res, int err);
}

28
include/Memory/IPC.h Normal file
View File

@ -0,0 +1,28 @@
///
/// Created by Anonymous275 on 1/26/22
/// Copyright (c) 2021-present Anonymous275 read the LICENSE file for more info.
///
#pragma once
#include <string>
class IPC {
public:
IPC() = delete;
IPC(const char* MemID, const char* SemID, const char* SemID2, size_t Size) noexcept;
[[nodiscard]] size_t size() const noexcept;
[[nodiscard]] char* c_str() const noexcept;
void send(const std::string& msg) noexcept;
[[nodiscard]] void* raw() const noexcept;
const std::string& msg() noexcept;
void confirm_receive() noexcept;
void receive();
~IPC() noexcept;
private:
void* SemConfHandle_;
void* MemoryHandle_;
void* SemHandle_;
std::string Msg_;
size_t Size_;
char* Data_;
};

View File

@ -8,10 +8,11 @@
class Memory{
public:
static uint64_t FindByPattern(const char* module, const char* Pattern, const char* Mask);
static uint64_t FindPattern(const char* module, const char* Pattern, const char* Mask);
static uint64_t GetModuleBase(const char* Name);
static void Print(const std::string& msg);
static void Inject(uint32_t PID);
static uint32_t GetTickCount();
static uint32_t GetBeamNGPID();
static uint32_t EntryPoint();
static uint32_t GetPID();

View File

@ -7,22 +7,51 @@
#include "Memory/BeamNG.h"
#include "Memory/Memory.h"
std::string GetHex(uint64_t num) {
char buffer[30];
sprintf(buffer, "%llx", num);
return std::string{buffer};
uint32_t BeamNG::GetTickCount_D() {
if(GEState != nullptr){
lua_get_field(GEState, -10002, "print");
lua_push_fstring(GEState, "Helloooooo");
lua_p_call(GEState, 1, 0, 0);
}
return Memory::GetTickCount();
}
int BeamNG::lua_open_jit_D(lua_State* State) {
Memory::Print("Got lua State");
GEState = State;
OpenJITDetour->Detach();
int r = lua_open_jit(State);
OpenJITDetour->Attach();
return r;
}
void BeamNG::EntryPoint() {
auto GameBaseAddr = Memory::GetModuleBase("BeamNG.drive.x64.exe");
auto DllBaseAddr = Memory::GetModuleBase("libbeamng.x64.dll");
Memory::Print("PID : " + std::to_string(Memory::GetPID()));
auto res = Memory::FindByPattern("BeamNG.drive.x64.exe", Patterns::GetTickCount[0], Patterns::GetTickCount[1]);
auto res2 = Memory::FindByPattern("BeamNG.drive.x64.exe", Patterns::open_jit[0], Patterns::open_jit[1]);
auto res3 = Memory::FindByPattern("BeamNG.drive.x64.exe", Patterns::get_field[0], Patterns::get_field[1]);
auto res4 = Memory::FindByPattern("BeamNG.drive.x64.exe", Patterns::push_fstring[0], Patterns::push_fstring[1]);
auto res5 = Memory::FindByPattern("BeamNG.drive.x64.exe", Patterns::p_call[0], Patterns::p_call[1]);
GameModule = "BeamNG.drive.x64.exe";
DllModule = "libbeamng.x64.dll";
GEState = nullptr;
GameBaseAddr = Memory::GetModuleBase(GameModule);
DllBaseAddr = Memory::GetModuleBase(DllModule);
GetTickCount = reinterpret_cast<def::GetTickCount>(Memory::FindPattern(GameModule, Patterns::GetTickCount[0],Patterns::GetTickCount[1]));
lua_open_jit = reinterpret_cast<def::lua_open_jit>(Memory::FindPattern(GameModule, Patterns::open_jit[0], Patterns::open_jit[1]));
lua_push_fstring = reinterpret_cast<def::lua_push_fstring>(Memory::FindPattern(GameModule, Patterns::push_fstring[0], Patterns::push_fstring[1]));
lua_get_field = reinterpret_cast<def::lua_get_field>(Memory::FindPattern(GameModule, Patterns::get_field[0], Patterns::get_field[1]));
lua_p_call = reinterpret_cast<def::lua_p_call>(Memory::FindPattern(GameModule, Patterns::p_call[0], Patterns::p_call[1]));
TickCountDetour = std::make_unique<Detours>((void*)GetTickCount, (void*)GetTickCount_D);
TickCountDetour->Attach();
OpenJITDetour = std::make_unique<Detours>((void*)lua_open_jit, (void*)lua_open_jit_D);
OpenJITDetour->Attach();
}
std::unique_ptr<Detours> BeamNG::TickCountDetour;
std::unique_ptr<Detours> BeamNG::OpenJITDetour;
uint64_t BeamNG::GameBaseAddr;
uint64_t BeamNG::DllBaseAddr;
def::GetTickCount BeamNG::GetTickCount;
def::lua_open_jit BeamNG::lua_open_jit;
def::lua_push_fstring BeamNG::lua_push_fstring;
def::lua_get_field BeamNG::lua_get_field;
def::lua_p_call BeamNG::lua_p_call;
const char* BeamNG::GameModule;
const char* BeamNG::DllModule;
lua_State* BeamNG::GEState;

68
src/Memory/IPC.cpp Normal file
View File

@ -0,0 +1,68 @@
///
/// Created by Anonymous275 on 1/26/22
/// Copyright (c) 2021-present Anonymous275 read the LICENSE file for more info.
///
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "Memory/IPC.h"
IPC::IPC(const char* MemID, const char* SemID, const char* SemID2, size_t Size) noexcept : Size_(Size) {
SemHandle_ = OpenSemaphoreA(SYNCHRONIZE | SEMAPHORE_MODIFY_STATE, FALSE, SemID);
if(SemHandle_ == nullptr) {
SemHandle_ = CreateSemaphoreA(nullptr, 0, 2, SemID);
}
SemConfHandle_ = OpenSemaphoreA(SYNCHRONIZE | SEMAPHORE_MODIFY_STATE, FALSE, SemID2);
if(SemConfHandle_ == nullptr) {
SemConfHandle_ = CreateSemaphoreA(nullptr, 0, 2, SemID2);
}
MemoryHandle_ = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, MemID);
if(MemoryHandle_ == nullptr) {
MemoryHandle_ = CreateFileMappingA(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, DWORD(Size), MemID);
}
Data_ = (char*)MapViewOfFile(MemoryHandle_, FILE_MAP_ALL_ACCESS, 0, 0, Size);
}
void IPC::confirm_receive() noexcept {
ReleaseSemaphore(SemConfHandle_, 1, nullptr);
}
void IPC::send(const std::string& msg) noexcept {
size_t Size = msg.size();
memcpy(Data_, &Size, sizeof(size_t));
memcpy(Data_ + sizeof(size_t), msg.c_str(), Size);
memset(Data_ + sizeof(size_t) + Size, 0, 3);
ReleaseSemaphore(SemHandle_, 1, nullptr);
WaitForSingleObject(SemConfHandle_, 5000);
}
void IPC::receive() {
WaitForSingleObject(SemHandle_, INFINITE);
}
size_t IPC::size() const noexcept {
return Size_;
}
char* IPC::c_str() const noexcept {
return Data_ + sizeof(size_t);
}
void* IPC::raw() const noexcept {
return Data_ + sizeof(size_t);
}
const std::string& IPC::msg() noexcept {
size_t Size;
memcpy(&Size, Data_, sizeof(size_t));
Msg_ = std::string(c_str(), Size);
return Msg_;
}
IPC::~IPC() noexcept {
UnmapViewOfFile(Data_);
CloseHandle(SemHandle_);
CloseHandle(MemoryHandle_);
}

View File

@ -40,7 +40,7 @@ uint32_t Memory::GetPID() {
return GetCurrentProcessId();
}
uint64_t Memory::FindByPattern(const char* module, const char* Pattern, const char* Mask) {
uint64_t Memory::FindPattern(const char* module, const char* Pattern, const char* Mask) {
MODULEINFO mInfo{nullptr};
GetModuleInformation(GetCurrentProcess(), GetModuleHandleA(module), &mInfo, sizeof(MODULEINFO));
auto base = uint64_t(mInfo.lpBaseOfDll);
@ -129,3 +129,7 @@ uint32_t Memory::EntryPoint() {
BeamNG::EntryPoint();
return 0;
}
uint32_t Memory::GetTickCount() {
return ::GetTickCount();
}