Network, bug fixes, more patters, GELua and a lot more

This commit is contained in:
Anonymous275
2022-01-31 23:39:42 +02:00
parent 6c11de2708
commit 6dfeba1e49
21 changed files with 1130 additions and 54 deletions

13
include/Compressor.h Normal file
View File

@@ -0,0 +1,13 @@
///
/// Created by Anonymous275 on 1/30/22
/// Copyright (c) 2021-present Anonymous275 read the LICENSE file for more info.
///
#pragma once
#include <string>
class Zlib {
public:
static std::string DeComp(std::string Compressed);
static std::string Comp(std::string Data);
};

View File

@@ -4,10 +4,12 @@
///
#pragma once
#include "Memory/IPC.h"
#include <filesystem>
#include <string>
#include "Server.h"
#include <thread>
namespace fs = std::filesystem;
struct VersionParser {
@@ -25,6 +27,7 @@ public: //constructors
public: //available functions
static void StaticAbort(Launcher* Instance = nullptr);
std::string Login(const std::string& fields);
void SendIPC(const std::string& Data, bool core = true);
void RunDiscordRPC();
void QueryRegistry();
void WaitForGame();
@@ -36,11 +39,15 @@ public: //Getters and Setters
void setDiscordMessage(const std::string& message);
static void setExit(bool exit) noexcept;
const std::string& getFullVersion();
const std::string& getMPUserPath();
static bool Terminated() noexcept;
const std::string& getPublicKey();
const std::string& getUserRole();
const std::string& getVersion();
static bool getExit() noexcept;
private: //functions
void HandleIPC(const std::string& Data);
std::string GetLocalAppdata();
void UpdatePresence();
void AdminRelaunch();
@@ -50,6 +57,7 @@ private: //functions
void ResetMods();
void EnableMP();
void Relaunch();
void ListenIPC();
void Abort();
private: //variables
uint32_t GamePID{0};
@@ -60,16 +68,20 @@ private: //variables
std::string BeamRoot{};
std::string UserRole{};
std::string PublicKey{};
std::thread IPCSystem{};
std::thread DiscordRPC{};
std::string MPUserPath{};
std::string BeamVersion{};
std::string BeamUserPath{};
std::string DiscordMessage{};
std::string Version{"3.0"};
Server ServerHandler{this};
std::string TargetBuild{"default"};
static std::atomic<bool> Shutdown, Exit;
std::string FullVersion{Version + ".0"};
VersionParser SupportedVersion{"0.24.1.1"};
IPC IPCToGame{"BeamMP_OUT", "BeamMP_Sem1", "BeamMP_Sem2", 0x1900000};
IPC IPCFromGame{"BeamMP_IN", "BeamMP_Sem3", "BeamMP_Sem4", 0x1900000};
};
class ShutdownException : public std::runtime_error {

View File

@@ -5,26 +5,23 @@
#pragma once
#include "Memory/Detours.h"
#include "Definitions.h"
#include <cstdint>
#include "Memory/GELua.h"
#include "Memory/IPC.h"
#include <memory>
#include <string>
class BeamNG {
public:
static void EntryPoint();
static void SendIPC(const std::string& Data);
private:
static std::unique_ptr<Detours> TickCountDetour;
static std::unique_ptr<Detours> OpenJITDetour;
static std::unique_ptr<IPC> IPCFromLauncher;
static std::unique_ptr<IPC> IPCToLauncher;
static int lua_open_jit_D(lua_State* State);
static void RegisterGEFunctions();
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

@@ -4,12 +4,20 @@
///
#pragma once
typedef struct lua_State lua_State;
#include <cstdint>
typedef struct lua_State lua_State;
typedef int (*lua_CFunction)(lua_State*);
extern int lua_gettop(lua_State *L);
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);
typedef int (*lua_p_call)(lua_State* L, int arg, int res, int err);
typedef void (*lua_pushcclosure)(lua_State* L, lua_CFunction fn, int n);
typedef void (*lua_settable)(lua_State* L, int idx);
typedef void (*lua_createtable)(lua_State* L, int narray, int nrec);
typedef void (*lua_setfield)(lua_State* L, int idx, const char* k);
typedef const char* (*lua_tolstring)(lua_State* L, int idx, size_t* len);
}

43
include/Memory/GELua.h Normal file
View File

@@ -0,0 +1,43 @@
///
/// Created by Anonymous275 on 1/30/22
/// Copyright (c) 2021-present Anonymous275 read the LICENSE file for more info.
///
#pragma once
#include "Definitions.h"
class GELua {
public:
static void FindAddresses();
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 def::lua_createtable lua_createtable;
static def::lua_pushcclosure lua_pushcclosure;
static def::lua_setfield lua_setfield;
static def::lua_settable lua_settable;
static def::lua_tolstring lua_tolstring;
static lua_State* State;
};
namespace GELuaTable {
inline void Begin(lua_State* L) {
GELua::lua_createtable(L, 0, 0);
}
inline void End(lua_State* L, const char* name) {
GELua::lua_setfield(L, -10002, name);
}
inline void BeginEntry(lua_State* L, const char* name) {
GELua::lua_push_fstring(L, "%s", name);
}
inline void EndEntry(lua_State* L) {
GELua::lua_settable(L, -3);
}
inline void InsertFunction(lua_State* L, const char* name, lua_CFunction func) {
BeginEntry(L, name);
GELua::lua_pushcclosure(L, func, 0);
EndEntry(L);
}
}

View File

@@ -14,15 +14,20 @@ public:
[[nodiscard]] char* c_str() const noexcept;
void send(const std::string& msg) noexcept;
[[nodiscard]] void* raw() const noexcept;
[[nodiscard]] bool receive_timed_out() const noexcept;
[[nodiscard]] bool send_timed_out() const noexcept;
const std::string& msg() noexcept;
void confirm_receive() noexcept;
void receive();
void try_receive() noexcept;
void receive() noexcept;
~IPC() noexcept;
private:
void* SemConfHandle_;
void* MemoryHandle_;
void* SemHandle_;
std::string Msg_;
bool SendTimeout;
bool RcvTimeout;
size_t Size_;
char* Data_;
};

View File

@@ -8,7 +8,7 @@
class Memory{
public:
static uint64_t FindPattern(const char* module, const char* Pattern, const char* Mask);
static uint64_t FindPattern(const char* module, const char* Pattern[]);
static uint64_t GetModuleBase(const char* Name);
static void Print(const std::string& msg);
static void Inject(uint32_t PID);

View File

@@ -25,4 +25,24 @@ namespace Patterns {
"\x48\x89\x5c\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xec\x00\x48\x8b\x59\x00\x41\x8b\xf0\x4c\x63\xda",
"xxxx?xxxx?xxxx?xxx?xxxxxx"
};
const char* lua_setfield[2] {
"\x48\x89\x5c\x24\x00\x57\x48\x83\xec\x00\x4d\x8b\xd0\x48\x8b\xd9\xe8\x00\x00\x00\x00\x48\x8b\xf8\x49\xc7\xc0\x00\x00\x00\x00\x90\x49\xff\xc0\x43\x80\x3c\x02\x00\x75\x00\x49\x8b\xd2\x48\x8b\xcb\xe8\x00\x00\x00\x00\x48\xb9\x00\x00\x00\x00\x00\x00\x00\x00\x4c\x8d\x44\x24\x00\x48\x0b\xc1\x48\x8b\xd7\x48\x8b\xcb\x48\x89\x44\x24\x00\xe8\x00\x00\x00\x00\x48\x8b\x53",
"xxxx?xxxx?xxxxxxx????xxxxxx????xxxxxxxx?x?xxxxxxx????xx????????xxxx?xxxxxxxxxxxxx?x????xxx"
};
const char* lua_createtable[2] {
"\x48\x89\x5c\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xec\x00\x4c\x8b\x49\x00\x41\x8b\xf8",
"xxxx?xxxx?xxxx?xxx?xxx"
};
const char* lua_settable[2] {
"\x40\x53\x48\x83\xec\x00\x48\x8b\xd9\xe8\x00\x00\x00\x00\x4c\x8b\x43\x00\x48\x8b\xd0\x49\x83\xe8\x00\x48\x8b\xcb\xe8\x00\x00\x00\x00\x48\x8b\x53",
"xxxxx?xxxx????xxx?xxxxxx?xxxx????xxx"
};
const char* lua_pushcclosure[2] {
"\x48\x89\x5c\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xec\x00\x48\x8b\xd9\x49\x63\xf8\x48\x8b\x49\x00\x48\x8b\xf2",
"xxxx?xxxx?xxxx?xxxxxxxxx?xxx"
};
const char* lua_tolstring[2] {
"\x48\x89\x5c\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xec\x00\x49\x8b\xf8\x8b\xda\x48\x8b\xf1\xe8",
"xxxx?xxxx?xxxx?xxxxxxxxx"
};
}

72
include/Server.h Normal file
View File

@@ -0,0 +1,72 @@
///
/// Created by Anonymous275 on 1/30/22
/// Copyright (c) 2021-present Anonymous275 read the LICENSE file for more info.
///
#pragma once
#include <string>
#include <atomic>
#include <thread>
#include <chrono>
struct sockaddr_in;
class Launcher;
class Server {
public:
Server() = delete;
explicit Server(Launcher* Instance);
~Server();
public:
void ServerSend(std::string Data, bool Rel);
void Connect(const std::string& Data);
const std::string& getModList();
const std::string& getUIStatus();
const std::string& getMap();
void setModLoaded();
bool Terminated();
int getPing() const;
void Close();
private:
std::chrono::time_point<std::chrono::high_resolution_clock> PingStart, PingEnd;
std::string MultiDownload(uint64_t DSock, uint64_t Size, const std::string& Name);
void AsyncUpdate(uint64_t& Rcv,uint64_t Size,const std::string& Name);
std::atomic<bool> Terminate{false}, ModLoaded{false};
char* TCPRcvRaw(uint64_t Sock, uint64_t& GRcv, uint64_t Size);
std::string GetAddress(const std::string& Data);
void InvalidResource(const std::string& File);
void UpdateUl(bool D, const std::string& msg);
std::unique_ptr<sockaddr_in> UDPSockAddress;
void ServerParser(const std::string& Data);
void TCPSend(const std::string& Data);
void UDPParser(std::string Packet);
void SendLarge(std::string Data);
void UDPSend(std::string Data);
void UUl(const std::string& R);
bool CheckBytes(int32_t Bytes);
int KillSocket(uint64_t Dead);
void MultiKill(uint64_t Sock);
Launcher* LauncherInstance;
std::thread TCPConnection;
std::thread UDPConnection;
std::thread AutoPing;
uint64_t TCPSocket = -1;
uint64_t UDPSocket = -1;
void WaitForConfirm();
std::string UStatus{};
std::string MStatus{};
std::string ModList{};
void TCPClientMain();
void SyncResources();
std::string TCPRcv();
uint64_t InitDSock();
std::string Auth();
std::string IP{};
void UDPClient();
void PingLoop();
int ClientID{0};
void UDPMain();
void UDPRcv();
void Abort();
int Port{0};
int Ping{0};
};