Add the loader component.

This commit is contained in:
drof
2024-09-21 04:49:43 +02:00
committed by GitHub
parent 04909c1905
commit 1385b5811b
55 changed files with 13851 additions and 0 deletions
+13
View File
@@ -0,0 +1,13 @@
#include "inc/include.h"
Context *gctx = nullptr;
int main(int argc, char *argv[])
{
msg("<------ DrauseHook ------>");
gLoader = new Loader();
gLoader->start(argc, argv);
return 0;
}
+336
View File
@@ -0,0 +1,336 @@
#include "inc/include.h"
unsigned int hde64_disasm(const void* code, hde64s* hs)
{
uint8_t x, c, * p = (uint8_t*)code, cflags, opcode, pref = 0;
uint8_t* ht = hde64_table, m_mod, m_reg, m_rm, disp_size = 0;
uint8_t op64 = 0;
memset(hs, 0, sizeof(hde64s));
for (x = 16; x; x--)
switch (c = *p++) {
case 0xf3:
hs->p_rep = c;
pref |= PRE_F3;
break;
case 0xf2:
hs->p_rep = c;
pref |= PRE_F2;
break;
case 0xf0:
hs->p_lock = c;
pref |= PRE_LOCK;
break;
case 0x26: case 0x2e: case 0x36:
case 0x3e: case 0x64: case 0x65:
hs->p_seg = c;
pref |= PRE_SEG;
break;
case 0x66:
hs->p_66 = c;
pref |= PRE_66;
break;
case 0x67:
hs->p_67 = c;
pref |= PRE_67;
break;
default:
goto pref_done;
}
pref_done:
hs->flags = (uint32_t)pref << 23;
if (!pref)
pref |= PRE_NONE;
if ((c & 0xf0) == 0x40) {
hs->flags |= F_PREFIX_REX;
if ((hs->rex_w = (c & 0xf) >> 3) && (*p & 0xf8) == 0xb8)
op64++;
hs->rex_r = (c & 7) >> 2;
hs->rex_x = (c & 3) >> 1;
hs->rex_b = c & 1;
if (((c = *p++) & 0xf0) == 0x40) {
opcode = c;
goto error_opcode;
}
}
if ((hs->opcode = c) == 0x0f) {
hs->opcode2 = c = *p++;
ht += DELTA_OPCODES;
}
else if (c >= 0xa0 && c <= 0xa3) {
op64++;
if (pref & PRE_67)
pref |= PRE_66;
else
pref &= ~PRE_66;
}
opcode = c;
cflags = ht[ht[opcode / 4] + (opcode % 4)];
if (cflags == C_ERROR) {
error_opcode:
hs->flags |= F_ERROR | F_ERROR_OPCODE;
cflags = 0;
if ((opcode & -3) == 0x24)
cflags++;
}
x = 0;
if (cflags & C_GROUP) {
uint16_t t;
t = *(uint16_t*)(ht + (cflags & 0x7f));
cflags = (uint8_t)t;
x = (uint8_t)(t >> 8);
}
if (hs->opcode2) {
ht = hde64_table + DELTA_PREFIXES;
if (ht[ht[opcode / 4] + (opcode % 4)] & pref)
hs->flags |= F_ERROR | F_ERROR_OPCODE;
}
if (cflags & C_MODRM) {
hs->flags |= F_MODRM;
hs->modrm = c = *p++;
hs->modrm_mod = m_mod = c >> 6;
hs->modrm_rm = m_rm = c & 7;
hs->modrm_reg = m_reg = (c & 0x3f) >> 3;
if (x && ((x << m_reg) & 0x80))
hs->flags |= F_ERROR | F_ERROR_OPCODE;
if (!hs->opcode2 && opcode >= 0xd9 && opcode <= 0xdf) {
uint8_t t = opcode - 0xd9;
if (m_mod == 3) {
ht = hde64_table + DELTA_FPU_MODRM + t * 8;
t = ht[m_reg] << m_rm;
}
else {
ht = hde64_table + DELTA_FPU_REG;
t = ht[t] << m_reg;
}
if (t & 0x80)
hs->flags |= F_ERROR | F_ERROR_OPCODE;
}
if (pref & PRE_LOCK) {
if (m_mod == 3) {
hs->flags |= F_ERROR | F_ERROR_LOCK;
}
else {
uint8_t* table_end, op = opcode;
if (hs->opcode2) {
ht = hde64_table + DELTA_OP2_LOCK_OK;
table_end = ht + DELTA_OP_ONLY_MEM - DELTA_OP2_LOCK_OK;
}
else {
ht = hde64_table + DELTA_OP_LOCK_OK;
table_end = ht + DELTA_OP2_LOCK_OK - DELTA_OP_LOCK_OK;
op &= -2;
}
for (; ht != table_end; ht++)
if (*ht++ == op) {
if (!((*ht << m_reg) & 0x80))
goto no_lock_error;
else
break;
}
hs->flags |= F_ERROR | F_ERROR_LOCK;
no_lock_error:
;
}
}
if (hs->opcode2) {
switch (opcode) {
case 0x20: case 0x22:
m_mod = 3;
if (m_reg > 4 || m_reg == 1)
goto error_operand;
else
goto no_error_operand;
case 0x21: case 0x23:
m_mod = 3;
if (m_reg == 4 || m_reg == 5)
goto error_operand;
else
goto no_error_operand;
}
}
else {
switch (opcode) {
case 0x8c:
if (m_reg > 5)
goto error_operand;
else
goto no_error_operand;
case 0x8e:
if (m_reg == 1 || m_reg > 5)
goto error_operand;
else
goto no_error_operand;
}
}
if (m_mod == 3) {
uint8_t* table_end;
if (hs->opcode2) {
ht = hde64_table + DELTA_OP2_ONLY_MEM;
table_end = ht + sizeof(hde64_table) - DELTA_OP2_ONLY_MEM;
}
else {
ht = hde64_table + DELTA_OP_ONLY_MEM;
table_end = ht + DELTA_OP2_ONLY_MEM - DELTA_OP_ONLY_MEM;
}
for (; ht != table_end; ht += 2)
if (*ht++ == opcode) {
if (*ht++ & pref && !((*ht << m_reg) & 0x80))
goto error_operand;
else
break;
}
goto no_error_operand;
}
else if (hs->opcode2) {
switch (opcode) {
case 0x50: case 0xd7: case 0xf7:
if (pref & (PRE_NONE | PRE_66))
goto error_operand;
break;
case 0xd6:
if (pref & (PRE_F2 | PRE_F3))
goto error_operand;
break;
case 0xc5:
goto error_operand;
}
goto no_error_operand;
}
else
goto no_error_operand;
error_operand:
hs->flags |= F_ERROR | F_ERROR_OPERAND;
no_error_operand:
c = *p++;
if (m_reg <= 1) {
if (opcode == 0xf6)
cflags |= C_IMM8;
else if (opcode == 0xf7)
cflags |= C_IMM_P66;
}
switch (m_mod) {
case 0:
if (pref & PRE_67) {
if (m_rm == 6)
disp_size = 2;
}
else
if (m_rm == 5)
disp_size = 4;
break;
case 1:
disp_size = 1;
break;
case 2:
disp_size = 2;
if (!(pref & PRE_67))
disp_size <<= 1;
}
if (m_mod != 3 && m_rm == 4) {
hs->flags |= F_SIB;
p++;
hs->sib = c;
hs->sib_scale = c >> 6;
hs->sib_index = (c & 0x3f) >> 3;
if ((hs->sib_base = c & 7) == 5 && !(m_mod & 1))
disp_size = 4;
}
p--;
switch (disp_size) {
case 1:
hs->flags |= F_DISP8;
hs->disp.disp8 = *p;
break;
case 2:
hs->flags |= F_DISP16;
hs->disp.disp16 = *(uint16_t*)p;
break;
case 4:
hs->flags |= F_DISP32;
hs->disp.disp32 = *(uint32_t*)p;
}
p += disp_size;
}
else if (pref & PRE_LOCK)
hs->flags |= F_ERROR | F_ERROR_LOCK;
if (cflags & C_IMM_P66) {
if (cflags & C_REL32) {
if (pref & PRE_66) {
hs->flags |= F_IMM16 | F_RELATIVE;
hs->imm.imm16 = *(uint16_t*)p;
p += 2;
goto disasm_done;
}
goto rel32_ok;
}
if (op64) {
hs->flags |= F_IMM64;
hs->imm.imm64 = *(uint64_t*)p;
p += 8;
}
else if (!(pref & PRE_66)) {
hs->flags |= F_IMM32;
hs->imm.imm32 = *(uint32_t*)p;
p += 4;
}
else
goto imm16_ok;
}
if (cflags & C_IMM16) {
imm16_ok:
hs->flags |= F_IMM16;
hs->imm.imm16 = *(uint16_t*)p;
p += 2;
}
if (cflags & C_IMM8) {
hs->flags |= F_IMM8;
hs->imm.imm8 = *p++;
}
if (cflags & C_REL32) {
rel32_ok:
hs->flags |= F_IMM32 | F_RELATIVE;
hs->imm.imm32 = *(uint32_t*)p;
p += 4;
}
else if (cflags & C_REL8) {
hs->flags |= F_IMM8 | F_RELATIVE;
hs->imm.imm8 = *p++;
}
disasm_done:
if ((hs->len = (uint8_t)(p - (uint8_t*)code)) > 15) {
hs->flags |= F_ERROR | F_ERROR_LENGTH;
hs->len = 15;
}
return (unsigned int)hs->len;
}
+68
View File
@@ -0,0 +1,68 @@
#include "inc/include.h"
namespace hde
{
size_t disasm_buffer(void *data, size_t length, hde64s *list, size_t list_length)
{
size_t left = length;
size_t count = 0;
size_t offset = 0;
hde64s info{};
do
{
if (mem::addr(data).Add(offset).Base() >
mem::addr(data).Add(length).Base())
break;
if (!hde64_disasm(mem::addr(data).Add(offset).Ptr(), &info))
break;
const auto delta = left - info.len;
if (static_cast<int>(delta) <= 0)
break;
memcpy(&list[count], &info, sizeof(hde64s));
count++;
left -= info.len;
offset += info.len;
} while (left > 0 && (count + 1) < list_length);
return count;
}
std::vector<hde64s> disasm_buffer_ex(void *data, size_t length)
{
auto result = std::vector<hde64s>();
size_t left = length;
size_t count = 0;
size_t offset = 0;
hde64s info{};
do
{
if (mem::addr(data).Add(offset).Base() >
mem::addr(data).Add(length).Base())
break;
if (!hde64_disasm(mem::addr(data).Add(offset).Ptr(), &info))
break;
const auto delta = left - info.len;
if (static_cast<int>(delta) <= 0)
break;
result.push_back(info);
count++;
left -= info.len;
offset += info.len;
} while (left > 0);
return result;
}
}
+238
View File
@@ -0,0 +1,238 @@
#include "inc/include.h"
#if defined(R5I_PROD)
Hwid *gHwid = new Hwid();
#else
Hwid *gHwid = nullptr;
#endif
class File_Descriptor_Guard
{
public:
File_Descriptor_Guard() = delete;
File_Descriptor_Guard(int fd)
{
m_fd = fd;
}
~File_Descriptor_Guard()
{
close(m_fd);
}
private:
int m_fd;
};
Hwid::Hwid()
{
m_available = collect();
}
bool Hwid::collect()
{
if (!query_all_disks())
return false;
if(!query_arp())
return false;
if(this->m_vec_device_macs.empty())
{
error("HWE001");
return false;
}
if(this->m_vec_disks.empty())
{
error("HWE002");
return false;
}
return true;
}
bool Hwid::query_all_disks()
{
const char *prefix_ssd = "/dev/sd";
const char* prefix_nvme_device = "/dev/nvme";
const char letters[] =
{
'a',
'b',
'c',
'd',
'e',
'f',
};
for (int i = 0; i < 6; i++)
{
char fmt[32];
memset(fmt, 0, sizeof(fmt));
sprintf(fmt, "%s%c", prefix_ssd, letters[i]);
hd_driveid identity;
memset(&identity, 0, sizeof(identity));
if (!query_disk_identity(fmt, identity))
{
continue;
}
Disk_t rec{};
rec.model_name.resize(sizeof(identity.model));
rec.serial_number.resize(sizeof(identity.serial_no));
sprintf(rec.model_name.data(), "%.40s", identity.model);
sprintf(rec.serial_number.data(), "%.20s", identity.serial_no);
m_vec_disks.push_back(rec);
}
for (int i = 0; i < 6; i++)
{
char fmt[32];
memset(fmt, 0, sizeof(fmt));
sprintf(fmt, "%s%in1", prefix_nvme_device, i);
Disk_t rec{};
if (!query_nvme_identity(fmt, rec))
{
continue;
}
m_vec_disks.push_back(rec);
}
return true;
}
bool Hwid::query_arp()
{
char line[512];
char ip_address[256];
int hw_type;
int flags;
char mac_address[64];
char mask[64];
char device[64];
FILE *fp = fopen("/proc/net/arp", "r");
if (fp)
{
fgets(line, sizeof(line), fp);
while (fgets(line, sizeof(line), fp))
{
sscanf(line, "%s 0x%x 0x%x %s %s %s\n",
ip_address,
&hw_type,
&flags,
mac_address,
mask,
device);
ArpParticipant_t record;
record.device = ip_address;
record.mac_address_permanent = mac_address;
m_vec_device_macs.push_back(record);
}
} else
{
return false;
}
fclose(fp);
return true;
}
bool Hwid::query_disk_identity(const char *dev, hd_driveid &buffer)
{
static struct hd_driveid hd;
int fd;
if ((fd = open(dev, O_RDONLY | O_NONBLOCK)) < 0)
{
return false;
}
File_Descriptor_Guard _(fd);
if (!ioctl(fd, HDIO_GET_IDENTITY, &hd))
{
memcpy(&buffer, &hd, sizeof(buffer));
}
else
{
return false;
}
return true;
}
bool Hwid::query_nvme_identity(const char *dev, Disk_t &buffer)
{
int fd = open(dev, O_RDWR);
if (fd == 0 || fd < 0)
{
return false;
}
File_Descriptor_Guard _(fd);
char buf[4096] = {0};
struct nvme_admin_cmd mib = {0};
mib.opcode = 0x06; // identify
mib.nsid = 0;
mib.addr = (__u64)buf;
mib.data_len = sizeof(buf);
mib.cdw10 = 1; // controller
int ret = ioctl(fd, NVME_IOCTL_ADMIN_CMD, &mib);
if (ret)
{
return false;
}
buffer.serial_number.resize(21);
snprintf(buffer.serial_number.data(), 21, "%.20s", &buf[4]);
buffer.model_name.resize(41);
snprintf(buffer.model_name.data(), 41, "%.40s", &buf[24]);
return true;
}
#define SMBIOS_START_ADDRESS 0xE0000 // Start address of SMBIOS data in physical memory
#define SMBIOS_END_ADDRESS 0xFFFFF // End address of SMBIOS data in physical memory
#define BUFFER_SIZE 16 // Size of buffer for reading from /dev/mem
std::vector<uint8_t> hwid::dump_smbios_data()
{
std::vector<uint8_t> smbios_data;
unsigned char buffer[BUFFER_SIZE];
// Open /dev/mem for reading
int fd = open("/dev/mem", O_RDONLY);
if (fd < 0) {
return smbios_data;
}
// Seek to the start of SMBIOS data
if (lseek(fd, SMBIOS_START_ADDRESS, SEEK_SET) < 0) {
return smbios_data;
}
// Read and dump SMBIOS data until SMBIOS_END_ADDRESS is reached
while (lseek(fd, 0, SEEK_CUR) < SMBIOS_END_ADDRESS) {
ssize_t bytesRead = read(fd, buffer, BUFFER_SIZE);
if (bytesRead < 0) {
return smbios_data;
}
// Append the read buffer to the vector
smbios_data.insert(smbios_data.end(), buffer, buffer + bytesRead);
}
// Close /dev/mem
close(fd);
return smbios_data;
}
+47
View File
@@ -0,0 +1,47 @@
#pragma once
class console_spinner
{
public:
console_spinner() = delete;
console_spinner(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
int size = vsnprintf(NULL, 0, fmt, args) + 1;
char* buf = new char[size];
vsnprintf(buf, size, fmt, args);
va_end(args);
_text = buf;
delete[] buf;
_active = true;
std::thread(&console_spinner::th_think, this).detach();
}
~console_spinner() {
stop();
}
void stop() {
_active = false;
}
void th_think() {
const char spinner[] = "|/-\\";
int i = 0;
while (_active) {
printf(" \r%c %s", spinner[i], _text.c_str());
fflush(stdout);
i = (i + 1) % 4;
usleep(100000);
}
printf("\n");
}
private:
bool _active;
std::string _text;
};
+75
View File
@@ -0,0 +1,75 @@
#pragma once
#define USEC_IN_MS(X) (X * 1000)
#define FORWARD_DECLARE_CLASS(X) class X
#define BASE_OF(X) ((uint64_t)X)
#define PTR_OF(X) ((void *)X)
typedef unsigned char u8;
typedef unsigned short u16;
typedef uint32_t u32;
typedef unsigned long u64;
typedef u64 uptr;
typedef signed char n8;
typedef signed short n16;
typedef signed long n32;
typedef signed long long n64;
#define ZERO_MEM(dst, len) memset(dst, 0, len)
#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
#define MAX_INSTRUCTION_LEN 15
#define OBFUS_FUNC_DISABLE __attribute__((__annotate__(("nobcf")), __annotate__(("nofla")), \
__annotate__(("nosub")), __annotate__(("nosplit"))))
#define MAPPER_USE_COMPSTUI
#if defined(R5I_PROD)
#define CloudLog_Msg(X, ...) gNetSDK->push_message_to_cloud_log(X, ##__VA_ARGS__)
#endif
inline double bytesToKB(long long bytes)
{
return static_cast<double>(bytes) / 1024.0;
}
inline double bytesToMB(long long bytes)
{
return bytesToKB(bytes) / 1024.0;
}
__attribute__((always_inline)) inline void terminate_safe()
{
auto pretaddr = reinterpret_cast<u64 *>(BASE_OF((__builtin_return_address(0))));
for (int i = 0; i < 12; i++)
{
*pretaddr = u64{};
++pretaddr;
}
asm volatile(
"xorq %%rax, %%rax\n\t"
"movq %%rax, %%rbx\n\t"
"movq %%rax, %%rdi\n\t"
"movq %%rax, %%rbp\n\t"
"movq %%rax, %%rdx\n\t"
"movq %%rax, %%r8\n\t"
"movq %%rax, %%r9\n\t"
"movq %%rax, %%r10\n\t"
"movq %%rax, %%r11\n\t"
"movq %%rax, %%r12\n\t"
"movq %%rax, %%r13\n\t"
"movq %%rax, %%r14\n\t"
"movq %%rax, %%r15\n\t"
"jmpq *%%rax"
:
:
: "%rax", "%rbx", "%rdi", "%rbp", "%rdx", "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15");
}
#define QUOTE(str) #str
#define STRINGIFIED_DEF(str) QUOTE(str)
+152
View File
@@ -0,0 +1,152 @@
#pragma once
struct ELFSection
{
ELFSection() = delete;
ELFSection(const std::string& name, uptr addr, u32 len)
{
this->name = name;
this->addr = addr;
this->length = len;
}
std::string name;
uptr addr;
u32 length;
};
struct ELFSymbol
{
ELFSymbol() = delete;
ELFSymbol(const std::string &name, uptr addr)
{
this->name = name;
this->addr = addr;
}
std::string name;
uptr addr;
};
class ELFImage
{
public:
ELFImage() = delete;
ELFImage(const char *path)
{
_image = tools::aob_read(path);
populate_sections();
populate_symbols();
}
ELFImage(const std::vector<u8>& vec_contents)
{
_image = vec_contents;
populate_sections();
populate_symbols();
}
void populate_sections()
{
Elf *elf;
Elf_Scn *scn = nullptr;
GElf_Shdr shdr;
elf_version(EV_CURRENT);
elf = elf_memory(reinterpret_cast<char *>(_image.data()), _image.size());
size_t shstrndx;
elf_getshdrstrndx(elf, &shstrndx); // Get the section string table index
while ((scn = elf_nextscn(elf, scn)) != nullptr)
{
gelf_getshdr(scn, &shdr);
if (shdr.sh_type == SHT_PROGBITS && shdr.sh_flags == (SHF_ALLOC | SHF_EXECINSTR))
{
const char *section_name = elf_strptr(elf, shstrndx, shdr.sh_name);
if (strcmp(section_name, ".text") == 0)
{
// Obtain the address and size of the .text section
Elf64_Addr section_address = shdr.sh_addr;
Elf64_Xword section_size = shdr.sh_size;
// Use the text_section_address and text_section_size as needed
_sections.push_back( ELFSection(section_name, section_address, section_size) );
break;
}
}
}
elf_end(elf);
}
void populate_symbols()
{
Elf *elf;
Elf_Scn *scn = nullptr;
Elf_Data *data = nullptr;
GElf_Sym sym;
size_t num_symbols, i;
elf_version(EV_CURRENT);
elf = elf_memory(reinterpret_cast<char *>(_image.data()), _image.size());
while ((scn = elf_nextscn(elf, scn)) != nullptr)
{
GElf_Shdr shdr;
gelf_getshdr(scn, &shdr);
if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM)
{
data = elf_getdata(scn, data);
num_symbols = shdr.sh_size / shdr.sh_entsize;
for (i = 0; i < num_symbols; i++)
{
gelf_getsym(data, i, &sym);
const char *symbol_name = elf_strptr(elf, shdr.sh_link, sym.st_name);
Elf64_Addr symbol_address = sym.st_value;
_symbols.push_back(ELFSymbol(symbol_name, symbol_address));
}
}
}
elf_end(elf);
}
uintptr_t find_symbol(const char *name)
{
for (const auto &sym : _symbols)
{
if (sym.name == name)
{
return sym.addr;
}
}
return 0;
}
auto find_section(const char* name) -> const ELFSection* const
{
for(const auto& scn : _sections)
{
if(scn.name == name)
{
return &scn;
}
}
return nullptr;
}
auto buf() { return _image.data(); }
private:
std::vector<u8> _image;
std::vector<ELFSection> _sections;
std::vector<ELFSymbol> _symbols;
};
+11
View File
@@ -0,0 +1,11 @@
#pragma once
#include "table64.h"
#include "hde64.h"
namespace hde
{
size_t disasm_buffer(void *data, size_t length, hde64s *list, size_t list_length);
std::vector<hde64s> disasm_buffer_ex(void *data, size_t length);
}
+94
View File
@@ -0,0 +1,94 @@
#pragma once
#ifndef _HDE64_H_
#define _HDE64_H_
#define F_MODRM 0x00000001
#define F_SIB 0x00000002
#define F_IMM8 0x00000004
#define F_IMM16 0x00000008
#define F_IMM32 0x00000010
#define F_IMM64 0x00000020
#define F_DISP8 0x00000040
#define F_DISP16 0x00000080
#define F_DISP32 0x00000100
#define F_RELATIVE 0x00000200
#define F_ERROR 0x00001000
#define F_ERROR_OPCODE 0x00002000
#define F_ERROR_LENGTH 0x00004000
#define F_ERROR_LOCK 0x00008000
#define F_ERROR_OPERAND 0x00010000
#define F_PREFIX_REPNZ 0x01000000
#define F_PREFIX_REPX 0x02000000
#define F_PREFIX_REP 0x03000000
#define F_PREFIX_66 0x04000000
#define F_PREFIX_67 0x08000000
#define F_PREFIX_LOCK 0x10000000
#define F_PREFIX_SEG 0x20000000
#define F_PREFIX_REX 0x40000000
#define F_PREFIX_ANY 0x7f000000
#define PREFIX_SEGMENT_CS 0x2e
#define PREFIX_SEGMENT_SS 0x36
#define PREFIX_SEGMENT_DS 0x3e
#define PREFIX_SEGMENT_ES 0x26
#define PREFIX_SEGMENT_FS 0x64
#define PREFIX_SEGMENT_GS 0x65
#define PREFIX_LOCK 0xf0
#define PREFIX_REPNZ 0xf2
#define PREFIX_REPX 0xf3
#define PREFIX_OPERAND_SIZE 0x66
#define PREFIX_ADDRESS_SIZE 0x67
#pragma pack(push,1)
typedef struct {
uint8_t len;
uint8_t p_rep;
uint8_t p_lock;
uint8_t p_seg;
uint8_t p_66;
uint8_t p_67;
uint8_t rex;
uint8_t rex_w;
uint8_t rex_r;
uint8_t rex_x;
uint8_t rex_b;
uint8_t opcode;
uint8_t opcode2;
uint8_t modrm;
uint8_t modrm_mod;
uint8_t modrm_reg;
uint8_t modrm_rm;
uint8_t sib;
uint8_t sib_scale;
uint8_t sib_index;
uint8_t sib_base;
union {
uint8_t imm8;
uint16_t imm16;
uint32_t imm32;
uint64_t imm64;
} imm;
union {
uint8_t disp8;
uint16_t disp16;
uint32_t disp32;
} disp;
uint32_t flags;
} hde64s;
#pragma pack(pop)
#ifdef __cplusplus
extern "C" {
#endif
/* __cdecl */
unsigned int hde64_disasm(const void* code, hde64s* hs);
#ifdef __cplusplus
}
#endif
#endif /* _HDE64_H_ */
+33
View File
@@ -0,0 +1,33 @@
#pragma once
class Hwid
{
public:
struct Disk_t
{
std::string model_name;
std::string serial_number;
};
struct ArpParticipant_t
{
std::string mac_address_permanent;
std::string device;
};
public:
Hwid();
bool collect();
bool query_all_disks();
bool query_arp();
bool query_disk_identity(const char *dev, hd_driveid &buffer);
bool query_nvme_identity(const char* dev, Disk_t& buffer);
std::vector<uint8_t> dump_smbios_data();
bool m_available;
std::vector<Disk_t> m_vec_disks;
std::vector<ArpParticipant_t> m_vec_device_macs;
};
extern Hwid *gHwid;
+196
View File
@@ -0,0 +1,196 @@
#pragma once
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stddef.h>
#include <string>
#include <string.h>
#include <unistd.h>
#include <vector>
#include <fstream>
#include <thread>
#include <algorithm>
#include <iomanip>
#include <functional>
#include <iostream>
#include <immintrin.h>
#include <lzma.h>
#include <chrono>
using namespace std::chrono_literals;
#include <errno.h>
#include <sys/uio.h>
#include <sys/ptrace.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <math.h>
#include <elf.h>
#include <openssl/rand.h>
#include <fcntl.h> // Include this for O_RDONLY
#include <unistd.h> // Include this for open and close
#include <gelf.h>
#include <libelf.h>
#include <sodium.h>
#include <sys/ioctl.h>
#include <linux/hdreg.h>
#include <linux/nvme_ioctl.h>
#include <mutex>
#include "defs.h"
#include "sockets-wrapper.h"
#include "lzmapp.h"
#include "log.h"
#include <backend/include/defs.h>
#include <backend/include/crc32.h>
#include <backend/include/protocol.h>
#include <backend/include/databuf.h>
#include "murmur.h"
#include "string-table.h"
#include "netsdk.h"
#include "memory.h"
#include "libpmap.h"
#include "hde.h"
#include "xorstr.h"
#include "rotate.h"
#include "ntos.h"
#include "mm.h"
#include "pattern.h"
#include "offsets.h"
#include "pe.h"
#include "wptrace.h"
#include "vmt.h"
#include "tools.h"
#include "elfimage.h"
#include "mapper.h"
#include "console.h"
#include "pump.h"
#include "integrity.h"
#include "hwid.h"
namespace pe
{
struct Section
{
Section()
{
begin = 0;
length = 0;
end = 0;
name = "NA";
}
Section(u64 b, u64 l, const std::string &n)
{
begin = b;
length = l;
end = (begin + length);
name = n;
}
u64 begin;
u64 length;
u64 end;
std::string name;
};
}
struct Context
{
int m_pid;
u64 m_game_base;
u64 m_game_len;
u64 m_shell_base;
u64 m_ctx_base;
u64 m_strtbl_base;
u64 m_libc_base;
u64 m_kernel32_base;
u64 m_ntdll_base;
u32 m_libc_syscall_gadget_offset;
std::vector<u8> m_snap_game;
struct
{
std::vector<IMAGE_SECTION_HEADER> table;
std::vector<u8> snap_cs;
inline auto get_sec_hdr(const char* sname)
{
for(const auto& shdr : table)
{
auto name = (const char*)&shdr.Name;
if(!strcmp(name, sname))
{
return shdr;
}
}
return IMAGE_SECTION_HEADER{ };
}
} m_game_secs;
LoaderContext m_lctx;
struct Options
{
bool payload_debug;
bool debug_sigs;
bool dump_game_image;
bool analysis_from_dump;
bool use_custom_payload_path;
bool use_unsafe_memory;
std::string analysis_from_dump_path;
std::string custom_payload_path;
bool analysis_from_dump_vec;
std::vector<u8> vec_analysis_from_dump;
} m_opts;
struct Offsets
{
u32 iface_base_client;
} m_offsets;
struct Iface
{
u64 base_client;
} m_ix;
struct Hooks
{
u64 fn_init;
} m_hooks;
struct VftsRemote
{
u64 base_client;
} m_vfts_remote;
struct
{
uptr base_client;
} m_vft_orig_ptrs;
struct Payload
{
u64 length;
u64 entry;
std::vector<IMAGE_SECTION_HEADER> sections;
u64 ptr_new_loader_ctx;
} m_payload;
string_table::String_table* m_strtbl;
};
extern Context *gctx;
#include "respawn.h"
#include "loader.h"
#include "packet-handlers.h"
+24
View File
@@ -0,0 +1,24 @@
#pragma once
class Integrity
{
public:
Integrity();
virtual void check1_thread();
virtual void check2_thread();
virtual bool is_process_being_traced();
virtual bool is_process_being_patched();
virtual bool check_debugger_presence();
virtual bool check_blacklisted_program_presence();
virtual void push_violation(proto::msg::integrity_event::ids id, proto::msg::integrity_event::contents_t contents);
virtual u32 calc_self_code_checksum();
private:
u32 m_checksum_self_seg_text;
};
extern Integrity* gInteg;
+73
View File
@@ -0,0 +1,73 @@
/**
* @file procmaps.h
* @brief Header file for lib procmaps
* @author Lilian VERLHAC
* @version 1.0
* @date 2020-04-18
*/
#ifndef PROCMAPS_H_
#define PROCMAPS_H_
typedef struct dev_major_minor_s dev_major_minor_t;
/**
* @brief Hold the major minor id for the map's device
*/
struct dev_major_minor_s {
int major;
int minor;
};
typedef struct procmaps_row_s procmaps_row_t;
/**
* @brief Hold all the information of a map
*/
struct procmaps_row_s {
unsigned long long addr_begin;
unsigned long long addr_end;
unsigned char perms;
unsigned long long offset;
dev_major_minor_t dev;
int inode;
char *pathname;
};
/**
* @brief Permissions associated to a map
*/
enum MAPS_PERMS {
PERMS_READ = 1 << 0,
PERMS_WRITE = 1 << 1,
PERMS_EXECUTE = 1 << 2,
PERMS_PRIVATE = 1 << 3,
PERMS_SHARED = 1 << 4
};
typedef procmaps_row_t hr_procmaps;
#define MAX_PROCPATH_LEN 100
#define PROCMAPS_ARRAY_BASE_SIZE 32
/**
* @brief Parse /proc/pid/maps to create an array of struct ptr which
* hold of the information
*
* @param pid Process pid, if pid <= 0 use self process
*
* @return Array of filled procmaps_row_t *
*/
hr_procmaps **contruct_procmaps(int pid);
/**
* @brief Destroy hr_procmaps object, free all the allocated memory
*
* @param procmaps Array of procmaps_row_t *
*/
void destroy_procmaps(hr_procmaps **procmaps);
void procmaps_begin_transaction();
void procmaps_end_transaction();
#endif // PROCMAPS_H_
+16
View File
@@ -0,0 +1,16 @@
#pragma once
class Loader
{
public:
virtual void start(int argc, char* argv[]);
virtual bool setup_netsdk();
virtual std::string get_argument_param(const std::string &str);
virtual void parse_argument(const std::string &str);
virtual bool authenticate();
private:
};
extern Loader* gLoader;
+20
View File
@@ -0,0 +1,20 @@
#pragma once
namespace xlog
{
void __msg(const char* fmt, ...);
void __error(const char* fmt, ...);
void __safeLog_msg(const char* fmt, ...);
}
#define msg(X, ...) xlog::__msg(_XS(X), ##__VA_ARGS__)
#define error(X, ...) xlog::__error(_XS(X), ##__VA_ARGS__)
// TODO: implement!
#define dbg(X, ...) while(0){ }
#if defined(R5I_PROD)
#define safeLog_msg(X, ...) CloudLog_Msg(_XS(X), ##__VA_ARGS__)
#else
#define safeLog_msg msg
#endif
+15
View File
@@ -0,0 +1,15 @@
//
// Created by drof on 27.01.24.
//
#ifndef BACKEND_LZMAPP_H
#define BACKEND_LZMAPP_H
namespace lzma
{
extern std::vector<uint8_t> compress(const std::vector<uint8_t>& data);
extern std::vector<uint8_t> decompress(const std::vector<uint8_t>& data);
}
#endif //BACKEND_LZMAPP_H
+132
View File
@@ -0,0 +1,132 @@
#pragma once
#define TARGET_NAME _XS("r5apex") // R5Apex
#define MAPPING_NAME _XS("r5apex.exe") // r5apex.exe
struct relocation_info_t
{
uint16_t m_type;
uintptr_t m_address;
};
using relocation_list = std::vector<relocation_info_t>;
#pragma pack(push, 1)
struct LoaderContext
{
unsigned int magic;
u8 inited;
/*
* Keep this here and if you change anything above,
* then ENSURE to update our trampoline offsets!
*/
struct Origs
{
u64 init;
} origs;
u64 game_base;
u64 game_len;
u64 game_rdata_base;
u64 game_rdata_len;
u64 game_cs_base;
u64 game_cs_len;
u64 game_data_base;
u64 game_data_len;
u64 libc_base;
u64 libm_base;
string_table::String_table* strtbl;
u64 strtbl_decryption_key;
struct
{
uint32_t fopen;
uint32_t fputs;
uint32_t fclose;
uint32_t sprintf;
uint32_t sleep;
} linuxcrt_cpp;
struct
{
uint32_t acosf;
uint32_t atan2f;
uint32_t fabsf;
uint32_t sinf;
uint32_t cosf;
} linuxcrt_math;
struct
{
uptr create_file;
uptr get_file_size;
uptr close_handle;
uptr read_file;
uptr capture_stack_back_trace;
} winapis;
struct Offsets
{
uint32_t entity_list;
uint32_t signon_state;
uint32_t input_system;
uint32_t mp_gamemode;
uint32_t global_vars;
uint32_t game_movement;
uint32_t map_name;
uint32_t name_list;
uint32_t tag_list;
uint32_t client_class_head;
uint32_t mat_system_surface;
uint32_t base_client;
uint32_t view_render;
uint32_t client_state;
uint32_t client_mode;
uint32_t kwewed_render_ctx;
uint32_t rs_cvar_gamepad_checks_enabled;
uint32_t rs_gamepad_checks_last;
uint32_t string_dict_user_names;
struct
{
uint32_t view_offset;
uint32_t view_angle;
} player;
struct
{
uint32_t get_mem_alloc;
uint32_t mss_draw_filled_rect;
uint32_t math_ang2vec;
uint32_t math_vec2ang;
} fns;
struct
{
uint32_t generic_1;
} vft_caves;
struct
{
u64 mkrctx_unk1;
u64 create_move;
u64 override_mouse;
} ret_addrs;
} offsets;
struct
{
u64 base_client;
} vft_orig_ptrs;
};
#pragma pack(pop)
namespace mapper
{
extern relocation_list get_relocation_list(u64 image_base, PIMAGE_NT_HEADERS64 nt_header);
extern bool resolve_linux_crt();
extern bool setup_pre();
extern bool setup(int pid);
extern bool fetch_image(std::vector<u8>& buf);
extern bool load(std::vector<u8> &buf);
}
+108
View File
@@ -0,0 +1,108 @@
#pragma once
namespace mem
{
struct range {
uptr m_begin{};
uptr m_end{};
uptr m_size{};
};
class addr
{
public:
addr() = default;
~addr() = default;
bool operator==(const uptr& other) const
{
return (m_base == other);
}
bool operator==(const uptr& lhs)
{
return (lhs == m_base);
}
addr(const uptr base)
{
m_base = base;
}
addr(void* const ptr)
{
m_base = reinterpret_cast<uptr>(ptr);
}
[[nodiscard]] auto Get() const
{
return addr(m_base);
}
[[nodiscard]] auto Base() const
{
return m_base;
}
[[nodiscard]] auto Valid() const
{
return !(m_base == 0);
}
[[nodiscard]] auto Add(const uptr t) const
{
return addr(m_base + t);
}
[[nodiscard]] auto Sub(const uptr t) const
{
return addr(m_base - t);
}
[[nodiscard]] auto Deref() const
{
return addr(*reinterpret_cast<uptr*>(m_base));
}
[[nodiscard]] auto Ptr() const
{
return reinterpret_cast<void*>(m_base);
}
template < class T >
[[nodiscard]] auto As()
{
return reinterpret_cast<T>(m_base);
}
template < class T >
[[nodiscard]] auto Retrieve_as()
{
return *reinterpret_cast<T*>(m_base);
}
template < class T >
void Set(const T& value)
{
*As<T*>() = value;
}
private:
uptr m_base;
};
inline uptr resolve_operandof_lea(const u8 *rip)
{
u8 offset = 0;
u8 skip = 3;
if (rip[0] == 0x48)
{
/* REX.W PREFIX */
offset = 1;
--skip;
}
auto disp = *(int32_t *)(rip + offset + skip);
return reinterpret_cast<uptr>(rip + 7 + disp);
}
}
+64
View File
@@ -0,0 +1,64 @@
#pragma once
namespace mm::detail
{
char vm_read(int pid, void* dst, u64 src, u64 len);
char vm_write(int pid, u64 dst, const void* src, u64 len);
u64 aob_search_buf_single(void *pHayStack, void *pNeedle, size_t uHayStackLength, size_t uNeedleLength);
}
namespace mm
{
inline u64 aob_search_buf_single_u64(void *pHayStack, size_t uHayStackLength, u64 uNeedle)
{
return detail::aob_search_buf_single(pHayStack, &uNeedle, uHayStackLength, sizeof(uNeedle));
}
}
class MemoryManager
{
public:
MemoryManager() = delete;
MemoryManager(int pid)
{
m_pid = pid;
}
bool read(void* dst, u64 src, size_t len);
bool write(u64 dst, const void* src, size_t len);
bool fill(u64 dst, u8 ch, size_t len)
{
auto tmp = malloc(len);
if(tmp)
{
memset(tmp, ch, len);
auto rs = write(dst, tmp, len);
free(tmp);
return rs;
}
return false;
}
template < typename T >
T tread(u64 src)
{
T result;
memset(&result, 0, sizeof(result));
read(&result, src, sizeof(result));
return result;
}
template < typename T >
bool twrite(u64 dst, const T& src)
{
return write(dst, &src, sizeof(src));
}
private:
int m_pid;
};
extern MemoryManager* gmm;
+45
View File
@@ -0,0 +1,45 @@
#pragma once
inline unsigned int mmh_32(const void* key, int len, unsigned int seed)
{
const unsigned int m = 0x5bd1e995;
const int r = 24;
unsigned int h = seed ^ len;
const unsigned char* data = (const unsigned char*)key;
while (len >= 4)
{
unsigned int k = *(unsigned int*)data;
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
data += 4;
len -= 4;
}
switch (len)
{
case 3: h ^= data[2] << 16;
case 2: h ^= data[1] << 8;
case 1: h ^= data[0];
h *= m;
};
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
inline int string_hash(const char* text, size_t length)
{
return mmh_32((void*)text, static_cast<int>(length), 6);
}
+37
View File
@@ -0,0 +1,37 @@
#pragma once
FORWARD_DECLARE_CLASS(Pump);
class NetSDK
{
public:
NetSDK() = delete;
NetSDK(const std::string_view& hostname, int port)
{
m_hostname = hostname;
m_port = port;
}
bool start();
bool handshake();
bool wait_for_activation();
bool ping();
bool start_pump();
bool authenticate(const std::string& email, const std::string& password);
bool request_dlc(u32 id);
bool request_asset(u32 id);
void push_message_to_cloud_log(const char* contents, ...);
auto pump() { return m_pump; }
private:
int m_sck;
std::string m_hostname;
int m_port;
unsigned char m_client_pk[crypto_kx_PUBLICKEYBYTES], m_client_sk[crypto_kx_SECRETKEYBYTES];
unsigned char m_client_rx[crypto_kx_SESSIONKEYBYTES], m_client_tx[crypto_kx_SESSIONKEYBYTES];
unsigned char m_server_pk[crypto_kx_PUBLICKEYBYTES];
Pump* m_pump;
};
extern NetSDK* gNetSDK;
+150
View File
@@ -0,0 +1,150 @@
#pragma once
#define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16
#define IMAGE_DOS_SIGNATURE 0x5A4D
#define IMAGE_NT_SIGNATURE 0x4550
#define IMAGE_SIZEOF_SHORT_NAME 8
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef int32_t LONG;
typedef uint32_t DWORD;
typedef unsigned long long ULONGLONG;
typedef unsigned short int USHORT;
typedef struct _IMAGE_BASE_RELOCATION
{
DWORD VirtualAddress;
DWORD SizeOfBlock;
// WORD TypeOffset[1];
} __attribute__((aligned(1), packed)) IMAGE_BASE_RELOCATION;
typedef IMAGE_BASE_RELOCATION UNALIGNED, *PIMAGE_BASE_RELOCATION;
typedef struct BASE_RELOCATION_ENTRY
{
USHORT Offset : 12;
USHORT Type : 4;
} __attribute__((aligned(1), packed)) BASE_RELOCATION_ENTRY, *PBASE_RELOCATION_ENTRY;
typedef struct _IMAGE_DOS_HEADER
{
WORD e_magic;
WORD e_cblp;
WORD e_cp;
WORD e_crlc;
WORD e_cparhdr;
WORD e_minalloc;
WORD e_maxalloc;
WORD e_ss;
WORD e_sp;
WORD e_csum;
WORD e_ip;
WORD e_cs;
WORD e_lfarlc;
WORD e_ovno;
WORD e_res[4];
WORD e_oemid;
WORD e_oeminfo;
WORD e_res2[10];
LONG e_lfanew;
} __attribute__((aligned(1), packed)) IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;
typedef struct _IMAGE_FILE_HEADER
{
WORD Machine;
WORD NumberOfSections;
DWORD TimeDateStamp;
DWORD PointerToSymbolTable;
DWORD NumberOfSymbols;
WORD SizeOfOptionalHeader;
WORD Characteristics;
} __attribute__((aligned(1), packed)) IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;
typedef struct _IMAGE_DATA_DIRECTORY
{
DWORD VirtualAddress; // RVA of the data
DWORD Size; // Size of the data
} __attribute__((aligned(1), packed)) IMAGE_DATA_DIRECTORY;
typedef struct _IMAGE_DEBUG_DIRECTORY {
DWORD Characteristics;
DWORD TimeDateStamp;
WORD MajorVersion;
WORD MinorVersion;
DWORD Type;
DWORD SizeOfData;
DWORD AddressOfRawData;
DWORD PointerToRawData;
} __attribute__((aligned(1), packed)) IMAGE_DEBUG_DIRECTORY, *PIMAGE_DEBUG_DIRECTORY;
typedef struct _IMAGE_OPTIONAL_HEADER64
{
WORD Magic;
BYTE MajorLinkerVersion;
BYTE MinorLinkerVersion;
DWORD SizeOfCode;
DWORD SizeOfInitializedData;
DWORD SizeOfUninitializedData;
DWORD AddressOfEntryPoint;
DWORD BaseOfCode;
ULONGLONG ImageBase;
DWORD SectionAlignment;
DWORD FileAlignment;
WORD MajorOperatingSystemVersion;
WORD MinorOperatingSystemVersion;
WORD MajorImageVersion;
WORD MinorImageVersion;
WORD MajorSubsystemVersion;
WORD MinorSubsystemVersion;
DWORD Win32VersionValue;
DWORD SizeOfImage;
DWORD SizeOfHeaders;
DWORD CheckSum;
WORD Subsystem;
WORD DllCharacteristics;
ULONGLONG SizeOfStackReserve;
ULONGLONG SizeOfStackCommit;
ULONGLONG SizeOfHeapReserve;
ULONGLONG SizeOfHeapCommit;
DWORD LoaderFlags;
DWORD NumberOfRvaAndSizes;
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
} __attribute__((aligned(1), packed)) IMAGE_OPTIONAL_HEADER64, *PIMAGE_OPTIONAL_HEADER64;
typedef struct _IMAGE_NT_HEADERS_GENERIC
{
DWORD Signature;
IMAGE_FILE_HEADER FileHeader;
} __attribute__((aligned(1), packed)) IMAGE_NT_HEADERS_GENERIC, *PIMAGE_NT_HEADERS_GENERIC;
typedef struct _IMAGE_NT_HEADERS64
{
DWORD Signature;
IMAGE_FILE_HEADER FileHeader;
IMAGE_OPTIONAL_HEADER64 OptionalHeader;
} __attribute__((aligned(1), packed)) IMAGE_NT_HEADERS64, *PIMAGE_NT_HEADERS64;
typedef struct _IMAGE_SECTION_HEADER
{
BYTE Name[IMAGE_SIZEOF_SHORT_NAME];
union
{
DWORD PhysicalAddress;
DWORD VirtualSize;
} Misc;
DWORD VirtualAddress;
DWORD SizeOfRawData;
DWORD PointerToRawData;
DWORD PointerToRelocations;
DWORD PointerToLinenumbers;
WORD NumberOfRelocations;
WORD NumberOfLinenumbers;
DWORD Characteristics;
} __attribute__((aligned(1), packed)) IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;
#define FIELD_OFFSET(CLASS, FIELD) offsetof(CLASS, FIELD)
#define IMAGE_FIRST_SECTION64(ntheader) ((PIMAGE_SECTION_HEADER)((u64)(ntheader) + \
FIELD_OFFSET(IMAGE_NT_HEADERS64, OptionalHeader) + \
((ntheader))->FileHeader.SizeOfOptionalHeader))
#define IMAGE_DIRECTORY_ENTRY_BASERELOC 5
+110
View File
@@ -0,0 +1,110 @@
#pragma once
#define resolve_rva(addr, instr_off, instr_len) ((addr + *reinterpret_cast<int32_t *>(addr + instr_off)) + instr_len)
namespace offsets
{
namespace detail
{
inline uptr find_ptr(uptr start, uptr len, const char* pattern, u16 ioff, u16 ilen)
{
auto rs = find_pattern(start, len, pattern);
if(rs)
{
return resolve_rva(rs, ioff, ilen);
}
return 0;
}
}
inline u64 fix_scn_off(void *image, u32 sec_base, uptr off)
{
//msg("fix_scn_off(%p, %x, %p)", image, sec_base, off);
return ((BASE_OF(image)) + sec_base + off);
}
inline u64 find(u64 base, u64 start, u64 len, const char *name, const char *pattern)
{
auto initial_addr = find_pattern(start, len, pattern);
if (!initial_addr)
{
safeLog_msg("!! couldn't find an offset (pattern) -> %s", name);
exit(0);
}
auto corrected = initial_addr - base;
safeLog_msg("** pattern %s (final) -> %lx", name, corrected);
return corrected;
}
inline u64 find_ptr(u64 base, u64 start, u64 len, const char *name, const char *pattern, u64 ioff, u64 ilen)
{
auto initial_addr = find_pattern(start, len, pattern);
if (!initial_addr)
{
safeLog_msg("!! couldn't find an offset (pattern/PTR) -> %s", name);
exit(0);
}
auto corrected = resolve_rva(initial_addr, ioff, ilen);
if (!corrected)
{
safeLog_msg("!! couldn't correct an offset (pattern/PTR) -> %s", name);
exit(0);
}
corrected -= base;
safeLog_msg("** pattern_PTR %s (final) -> %lx", name, corrected);
return corrected;
}
inline u64 find_local_ptr_adj_remote(u64 start, u64 len, u64 remote_base,
u64 remote_scn_base, const char *name, const char *pattern, u64 ioff, u64 ilen, bool fix = true)
{
auto initial_addr = find_pattern(start, len, pattern);
if (!initial_addr)
{
safeLog_msg("!! couldn't find an offset (pattern/PTR) -> %s", name);
exit(0);
}
auto corrected = resolve_rva(initial_addr, ioff, ilen);
if (!corrected)
{
safeLog_msg("!! couldn't correct an offset (pattern/PTR) -> %s", name);
exit(0);
}
auto rs = fix ? (fix_scn_off(PTR_OF(remote_base), remote_scn_base, (corrected - start)) - remote_base) : corrected;
if(!fix) rs -= remote_base;
safeLog_msg("** pattern_PTR %s (final) -> %lx", name, rs);
return rs;
}
inline u64 find_local_adj_remote(u64 start, u64 len, u64 remote_base, u64 remote_scn_base, const char *name, const char *pattern, bool fix = true)
{
auto initial_addr = find_pattern(start, len, pattern);
if (!initial_addr)
{
safeLog_msg("!! couldn't find an offset (pattern) -> %s", name);
exit(0);
}
auto rs = fix ? (fix_scn_off(PTR_OF(remote_base), remote_scn_base, (initial_addr - start)) - remote_base) : initial_addr;
if(!fix) rs -= remote_base;
safeLog_msg("** pattern %s (final) -> %lx", name, rs);
return rs;
}
}
#define FIND_SIG(BASE, START, LEN, NAME, PAT) offsets::find(BASE, START, LEN, _XS(NAME), _XS(PAT))
#define FIND_PTR(BASE, START, LEN, NAME, PAT, IOFF, ILEN) offsets::find_ptr(BASE, START, LEN, _XS(NAME), _XS(PAT), IOFF, ILEN)
// GIVES AN RVA ONLY, WITH IMAGE BASE SUBTRACTED.
#define FIND_OFF_LAJR(START, LEN, REMOTE_BASE, REMOTE_SCN_BASE, NAME, PAT, FIX) offsets::find_local_adj_remote(START, LEN, REMOTE_BASE, REMOTE_SCN_BASE, _XS(NAME), _XS(PAT), FIX)
#define FIND_PTR_LAJR(START, LEN, REMOTE_BASE, REMOTE_SCN_BASE, NAME, PAT, IOFF, ILEN, FIX) offsets::find_local_ptr_adj_remote(START, LEN, \
REMOTE_BASE, REMOTE_SCN_BASE, _XS(NAME), _XS(PAT), IOFF, ILEN, FIX)
+33
View File
@@ -0,0 +1,33 @@
#pragma once
struct NetworkSessionContext_t
{
bool received_ping;
bool has_server_acknowledged_ping;
bool received_authentication_result;
proto::AUTH_STATES value_authentication_result;
u32 requested_asset_id;
bool received_requested_asset;
std::vector<u8> vec_asset_contents;
u32 requested_dlc_id;
bool received_request_dlc_response;
bool will_server_send_requested_dlc;
bool received_dlc;
std::vector<u8> vec_dlc_contents;
};
extern NetworkSessionContext_t* gNetSsnContext;
namespace packet_handlers
{
extern void handle_response_to_ping(int sck, proto::Packet_header_t hdr, std::vector<u8> vec_contents);
extern void handle_response_to_auth(int sck, proto::Packet_header_t hdr, std::vector<u8> vec_contents);
extern void handle_response_to_request_dlc(int sck, proto::Packet_header_t hdr, std::vector<u8> vec_contents);
extern void handle_server_drop_client(int sck, proto::Packet_header_t hdr, std::vector<u8> vec_contents);
extern void handle_server_data_transfer(int sck, proto::Packet_header_t hdr, std::vector<u8> vec_contents);
}
+120
View File
@@ -0,0 +1,120 @@
#pragma once
#define INRANGE(x,a,b) (x >= a && x <= b)
#define getBits( x ) (INRANGE(x,'0','9') ? (x - '0') : ((x&(~0x20)) - 'A' + 0xa))
#define getByte( x ) (getBits(x[0]) << 4 | getBits(x[1]))
namespace sig
{
OBFUS_FUNC_DISABLE inline
bool isMatch(const u8* addr, const u8* pat, const u8* msk)
{
size_t n = 0;
while (addr[n] == pat[n] || msk[n] == (u8)'?') {
if (!msk[++n]) {
return true;
}
}
return false;
}
OBFUS_FUNC_DISABLE inline const u8* find_pattern(const u8* rangeStart, u64 len, const char* pattern)
{
size_t l = strlen(pattern);
u8* patt_base = static_cast<u8*>(alloca(l >> 1));
u8* msk_base = static_cast<u8*>(alloca(l >> 1));
u8* pat = patt_base;
u8* msk = msk_base;
l = 0;
while (*pattern) {
if (*pattern == ' ')
pattern++;
if (!*pattern)
break;
if (*(u8*)pattern == (u8)'\?') {
*pat++ = 0;
*msk++ = '?';
pattern += ((*(u16*)pattern == (u16)'\?\?') ? 2 : 1);
}
else {
*pat++ = getByte(pattern);
*msk++ = 'x';
pattern += 2;
}
l++;
}
*msk = 0;
pat = patt_base;
msk = msk_base;
for (u32 n = 0; n < (len - l); ++n)
{
if (isMatch(rangeStart + n, patt_base, msk_base)) {
return rangeStart + n;
}
}
return NULL;
}
OBFUS_FUNC_DISABLE inline auto get_pattern_byte_length(const char* pattern)
{
size_t l = strlen(pattern);
u8* patt_base = static_cast<u8*>(alloca(l >> 1));
u8* msk_base = static_cast<u8*>(alloca(l >> 1));
u8* pat = patt_base;
u8* msk = msk_base;
u64 result = 0;
int fields = 0;
for (u64 i = 0; i < l; i++)
{
if (pattern[i] == ' ')
continue;
if (fields < 1)
fields++;
else
{
result++;
fields = 0;
}
}
return result;
}
OBFUS_FUNC_DISABLE inline auto find_pattern_rc(const u8* rangeStart, u64 len, const char* pattern)
{
auto result = std::vector<u64>();
auto pat_len = get_pattern_byte_length(pattern);
if (pat_len > 0)
{
const u8* last_hit = nullptr;
for (auto curr = rangeStart; (curr < (rangeStart + len)); )
{
const u8* rs = nullptr;
if (!last_hit)
rs = find_pattern(curr, len, pattern);
else
{
rs = find_pattern(curr, ((rangeStart + len) - last_hit - pat_len), pattern);
}
if (rs)
{
result.push_back(BASE_OF(rs));
curr = rs + pat_len;
last_hit = rs;
}
else
break;
}
}
return result;
}
}
#define find_pattern(base, length, pat) (u64)sig::find_pattern((u8*)base, length, pat)
+187
View File
@@ -0,0 +1,187 @@
#pragma once
typedef struct _IMAGE_EXPORT_DIRECTORY
{
DWORD Characteristics;
DWORD TimeDateStamp;
WORD MajorVersion;
WORD MinorVersion;
DWORD Name;
DWORD Base;
DWORD NumberOfFunctions;
DWORD NumberOfNames;
DWORD AddressOfFunctions; // RVA from base of image
DWORD AddressOfNames; // RVA from base of image
DWORD AddressOfNameOrdinals; // RVA from base of image
} IMAGE_EXPORT_DIRECTORY, *PIMAGE_EXPORT_DIRECTORY;
#define IMAGE_DIRECTORY_ENTRY_EXPORT 0
#define IMAGE_DIRECTORY_ENTRY_EXCEPTION 3
#define IMAGE_DIRECTORY_ENTRY_DEBUG 6
bool pe_dump_image(int pid, u64 base, u64 *out_len);
void pe_dump_fix(void *image);
namespace pe
{
template <class T>
PIMAGE_SECTION_HEADER GetEnclosingSectionHeader(
DWORD rva,
T *pNTHeader) // 'T' == PIMAGE_NT_HEADERS
{
PIMAGE_SECTION_HEADER section = IMAGE_FIRST_SECTION64(pNTHeader);
unsigned i;
for (i = 0; i < pNTHeader->FileHeader.NumberOfSections; i++, section++)
{
// This 3 line idiocy is because Watcom's linker actually sets the
// Misc.VirtualSize field to 0. (!!! - Retards....!!!)
DWORD size = section->Misc.VirtualSize;
if (0 == size)
size = section->SizeOfRawData;
// Is the RVA within this section?
if ((rva >= section->VirtualAddress) &&
(rva < (section->VirtualAddress + size)))
return section;
}
return 0;
}
template <class T>
void *get_ptr_from_va(
DWORD rva,
T *pNTHeader,
u8 *imageBase) // 'T' = PIMAGE_NT_HEADERS
{
PIMAGE_SECTION_HEADER pSectionHdr;
int delta;
pSectionHdr = GetEnclosingSectionHeader(rva, pNTHeader);
if (!pSectionHdr)
return 0;
delta = (int)(pSectionHdr->VirtualAddress - pSectionHdr->PointerToRawData);
return (void *)(imageBase + rva - delta);
}
template <typename _pt, typename _nth>
_pt *get_data_directory_ptr(uintptr_t base, _nth nt_hdr, int index)
{
const auto offset =
nt_hdr->OptionalHeader.DataDirectory[index].VirtualAddress;
return reinterpret_cast<_pt *>((base + offset));
}
template <typename _nth>
bool get_data_directory_info(uintptr_t base, _nth nt_hdr, int index, IMAGE_DATA_DIRECTORY &out)
{
out = nt_hdr->OptionalHeader.DataDirectory[index];
return true;
}
extern PIMAGE_SECTION_HEADER find_section_base_virt(void *image, const char *name);
inline u64 find_export_address(uintptr_t base, PIMAGE_DOS_HEADER dos_hdr, PIMAGE_NT_HEADERS64 nt_hdr, const char *exp_name, bool file = true)
{
auto export_ddir = &nt_hdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
if (!export_ddir->VirtualAddress && !export_ddir->Size)
return 0;
auto export_descriptor = reinterpret_cast<PIMAGE_EXPORT_DIRECTORY>(get_ptr_from_va(export_ddir->VirtualAddress, nt_hdr, (u8 *)base));
const auto export_descriptor_rva_raw = reinterpret_cast<uintptr_t>(export_descriptor) - base;
const auto export_descriptor_rva = get_ptr_from_va(export_descriptor_rva_raw, nt_hdr, (u8 *)base);
const uintptr_t names_rva = export_descriptor->AddressOfNames;
const uintptr_t addresses_rva = export_descriptor->AddressOfFunctions;
const uintptr_t ordinals_rva = export_descriptor->AddressOfNameOrdinals;
const auto names_address = get_ptr_from_va(static_cast<uintptr_t>(names_rva), nt_hdr, (u8 *)base);
const auto addresses_address = get_ptr_from_va(static_cast<uintptr_t>(addresses_rva), nt_hdr, (u8 *)base);
const auto ordinals_address = get_ptr_from_va(static_cast<uintptr_t>(ordinals_rva), nt_hdr, (u8 *)base);
auto *const names_array = reinterpret_cast<std::uint32_t *>(names_address);
auto *const addresses_array = reinterpret_cast<std::uint32_t *>(addresses_address);
auto *const ordinals_array = reinterpret_cast<std::uint16_t *>(ordinals_address);
const auto export_descriptor_rva_begin = (u64)export_descriptor_rva;
const auto export_descriptor_rva_end = (u64)(BASE_OF(export_descriptor_rva) + export_ddir->Size);
/*msg("export -> exportDescVA: %p", export_ddir->VirtualAddress);
msg("export -> exportDescLN: %p", export_ddir->Size);
msg("export -> aNames: %p", names_array);
msg("export -> aAddrs: %p", addresses_array);
msg("export -> aOrdinals: %p", ordinals_array);
msg("export-> nFuncs: %i", export_descriptor->NumberOfFunctions);*/
for (size_t i = 0; i < export_descriptor->NumberOfFunctions; i++)
{
auto address_virt = addresses_array[ordinals_array[i]];
auto address = (u64)get_ptr_from_va(address_virt, nt_hdr, (u8 *)base);
auto name = (char *)get_ptr_from_va(names_array[i], nt_hdr, (u8 *)base);
if (strcmp(name, exp_name) == 0)
{
if (file)
return address;
else
return base + address_virt;
}
}
return 0;
}
inline uptr find_export_address_virt(uptr base, PIMAGE_DOS_HEADER dos_hdr, PIMAGE_NT_HEADERS64 nt_hdr, const char *exp_name)
{
auto *const export_descriptor =
get_data_directory_ptr<IMAGE_EXPORT_DIRECTORY>(base, nt_hdr,
IMAGE_DIRECTORY_ENTRY_EXPORT);
IMAGE_DATA_DIRECTORY export_descriptor_info{};
if (export_descriptor == nullptr)
return 0;
if (!get_data_directory_info(base, nt_hdr,
IMAGE_DIRECTORY_ENTRY_EXPORT, export_descriptor_info))
return 0;
const auto export_descriptor_rva =
(reinterpret_cast<uptr>(export_descriptor) - base);
const uptr names_rva = export_descriptor->AddressOfNames;
const uptr addresses_rva = export_descriptor->AddressOfFunctions;
const uptr ordinals_rva = export_descriptor->AddressOfNameOrdinals;
const auto names_address = static_cast<uptr>(base + names_rva);
const auto addresses_address = static_cast<uptr>(base + addresses_rva);
const auto ordinals_address = static_cast<uptr>(base + ordinals_rva);
auto *const names_array = reinterpret_cast<u32 *>(names_address);
auto *const addresses_array = reinterpret_cast<u32 *>(addresses_address);
auto *const ordinals_array = reinterpret_cast<u16 *>(ordinals_address);
const auto export_descriptor_rva_begin = export_descriptor_rva;
const auto export_descriptor_rva_end = (export_descriptor_rva + export_descriptor_info.Size);
for (size_t i = 0; i < export_descriptor->NumberOfFunctions; i++)
{
const auto ordinal = ordinals_array[i];
if (ordinal == 0)
{
}
const auto address = static_cast<uptr>(base + addresses_array[i]);
auto *const name = reinterpret_cast<char *>(base + names_array[i]);
if(!strcmp(name, exp_name))
return address;
}
return 0;
}
}
File diff suppressed because it is too large Load Diff
+46
View File
@@ -0,0 +1,46 @@
//
// Created by drof on 12.02.24.
//
#ifndef CLIENT_PUMP_H
#define CLIENT_PUMP_H
#include <deque>
using Pump_read_callback_fn = void(*)(int /*SCK*/, proto::Packet_header_t /*PACK_HDR*/, std::vector<u8> /*VEC_DATA*/ );
struct Pump_read_handler_t
{
u32 target_packet_id;
Pump_read_callback_fn cbk;
};
struct Pump_queued_write_packet_t
{
u32 id;
std::vector<u8> vec_contents;
};
class Pump
{
public:
Pump() = delete;
Pump(int sck, unsigned char* client_pk, unsigned char* client_sk, unsigned char* server_pk);
void add_read_handler( u32 target_packet_id, Pump_read_callback_fn cbk );
void push_packet_to_write( u32 id, const std::vector<u8>& vec_contents );
void push_packet_to_write( u32 id, const void* data, u32 len );
private:
void packets_read();
void packets_write();
private:
int m_socket;
bool m_is_activated;
bool m_is_failed;
std::vector<Pump_read_handler_t> m_vec_read_handlers;
std::deque< Pump_queued_write_packet_t > m_vec_write_queue;
unsigned char m_client_pk[crypto_kx_PUBLICKEYBYTES], m_client_sk[crypto_kx_SECRETKEYBYTES];
unsigned char m_server_pk[crypto_kx_PUBLICKEYBYTES];
};
#endif //CLIENT_PUMP_H
+6
View File
@@ -0,0 +1,6 @@
#pragma once
namespace respawn
{
bool collect_offsets(Context* ctx, LoaderContext* lctx, uptr game_image_base, uptr game_cs_base, uptr game_cs_len, bool from_snap = false);
}
+84
View File
@@ -0,0 +1,84 @@
//
// Created by drof on 27.01.24.
//
#ifndef BACKEND_ROTATE_H
#define BACKEND_ROTATE_H
#pragma once
namespace rotate
{
template<class T>
constexpr T left(T value, int count)
{
const uint nbits = sizeof(T) * 8;
if (count > 0)
{
count %= nbits;
T high = value >> (nbits - count);
if (T(-1) < 0) // signed value
high &= ~((T(-1) << count));
value <<= count;
value |= high;
}
else
{
count = -count % nbits;
T low = value << (nbits - count);
value >>= count;
value |= low;
}
return value;
}
template<class T>
constexpr T right(T value, int count)
{
count = -count; // invert it for this op.
const uint nbits = sizeof(T) * 8;
if (count > 0)
{
count %= nbits;
T high = value >> (nbits - count);
if (T(-1) < 0) // signed value
high &= ~((T(-1) << count));
value <<= count;
value |= high;
}
else
{
count = -count % nbits;
T low = value << (nbits - count);
value >>= count;
value |= low;
}
return value;
}
inline auto encode(void* data, u32 len)
{
for (u32 i = 0; i < len; i++)
{
reinterpret_cast<u8*>(data)[i] =
left<u8>(reinterpret_cast<u8*>(data)[i], 1);
reinterpret_cast<u8*>(data)[i] ^= len;
}
}
inline auto decode(void* data, u32 len)
{
for (u32 i = 0; i < len; i++)
{
reinterpret_cast<u8*>(data)[i] ^= len;
reinterpret_cast<u8*>(data)[i] =
right<u8>(reinterpret_cast<u8*>(data)[i], 1);
}
}
}
#endif //BACKEND_ROTATE_H
+98
View File
@@ -0,0 +1,98 @@
//
// Created by drof on 26.01.24.
//
#ifndef BACKEND_SOCKETS_WRAPPER_H
#define BACKEND_SOCKETS_WRAPPER_H
namespace sock
{
inline int recursive_send(int socket, const void* pdata, size_t size)
{
auto data = (const char*)pdata;
if (size == 0) {
return 0; // Nothing to send
}
ssize_t sent_bytes = send(socket, data, size, 0);
if (sent_bytes == -1) {
// Handle error, e.g., print an error message or return an error code
return -1;
}
if (sent_bytes < static_cast<ssize_t>(size)) {
// Call send recursively for the remaining data
return sent_bytes + recursive_send(socket, data + sent_bytes, size - sent_bytes);
}
return sent_bytes;
}
inline int recursive_recv(int socket, void* pbuffer, size_t size)
{
auto buffer= (char*)pbuffer;
if (size == 0) {
return 0; // Nothing to receive
}
ssize_t received_bytes = recv(socket, buffer, size, 0);
if (received_bytes == -1) {
// Handle error, e.g., print an error message or return an error code
return -1;
}
if (received_bytes == 0) {
// Connection closed by the other side
return 0;
}
// Call recv recursively for the remaining data
return received_bytes + recursive_recv(socket, buffer + received_bytes, size - received_bytes);
}
inline int recursive_recv_buffered(int socket, void* pbuffer, int size, int chunksize, int chunkratio)
{
int bytes_received = 0;
int bytes_left = size;
auto data = reinterpret_cast<u8*>(pbuffer);
auto bytes_per_chunk = chunksize / chunkratio;
double last_percent = 0.f;
while (bytes_left > 0)
{
if(bytes_left < bytes_per_chunk)
bytes_per_chunk = bytes_left;
int result = recv(socket, &data[bytes_received], bytes_per_chunk, 0);
if (result > 0)
{
double current_percent = (static_cast<double>(bytes_received) / size) * 100;
if( (int)last_percent != (int)current_percent)
{
printf("[DL][BUFFERING] %.0f%%\n",
current_percent);
last_percent = current_percent;
}
bytes_left -= result;
bytes_received += result;
} else
{
if(result < 0)
{
return -1;
}
}
}
return bytes_received;
}
}
#endif //BACKEND_SOCKETS_WRAPPER_H
+30
View File
@@ -0,0 +1,30 @@
#pragma once
namespace string_table
{
extern void stream_encode(uint8_t *buffer, size_t length, uint64_t key);
extern void stream_decode(uint8_t *buffer, size_t length, uint64_t key);
#define STRREC_MAX_CONTENT_LENGTH 64
struct String_record
{
uint32_t length;
uint32_t hash;
char content[STRREC_MAX_CONTENT_LENGTH];
} __attribute__((packed));
struct String_table
{
uint32_t magic;
uint32_t table_total_length;
uint32_t table_records_count;
String_record table[1];
} __attribute__((packed));
extern String_table *Create(const std::vector<std::string> &vec_contents);
extern void Dump_records(String_table *table);
extern void Dump_contents(String_table *table, size_t table_len);
extern void Encrypt(String_table* table);
}
+69
View File
@@ -0,0 +1,69 @@
#pragma once
#define C_NONE 0x00
#define C_MODRM 0x01
#define C_IMM8 0x02
#define C_IMM16 0x04
#define C_IMM_P66 0x10
#define C_REL8 0x20
#define C_REL32 0x40
#define C_GROUP 0x80
#define C_ERROR 0xff
#define PRE_ANY 0x00
#define PRE_NONE 0x01
#define PRE_F2 0x02
#define PRE_F3 0x04
#define PRE_66 0x08
#define PRE_67 0x10
#define PRE_LOCK 0x20
#define PRE_SEG 0x40
#define PRE_ALL 0xff
#define DELTA_OPCODES 0x4a
#define DELTA_FPU_REG 0xfd
#define DELTA_FPU_MODRM 0x104
#define DELTA_PREFIXES 0x13c
#define DELTA_OP_LOCK_OK 0x1ae
#define DELTA_OP2_LOCK_OK 0x1c6
#define DELTA_OP_ONLY_MEM 0x1d8
#define DELTA_OP2_ONLY_MEM 0x1e7
inline unsigned char hde64_table[] = {
0xa5,0xaa,0xa5,0xb8,0xa5,0xaa,0xa5,0xaa,0xa5,0xb8,0xa5,0xb8,0xa5,0xb8,0xa5,
0xb8,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xac,0xc0,0xcc,0xc0,0xa1,0xa1,
0xa1,0xa1,0xb1,0xa5,0xa5,0xa6,0xc0,0xc0,0xd7,0xda,0xe0,0xc0,0xe4,0xc0,0xea,
0xea,0xe0,0xe0,0x98,0xc8,0xee,0xf1,0xa5,0xd3,0xa5,0xa5,0xa1,0xea,0x9e,0xc0,
0xc0,0xc2,0xc0,0xe6,0x03,0x7f,0x11,0x7f,0x01,0x7f,0x01,0x3f,0x01,0x01,0xab,
0x8b,0x90,0x64,0x5b,0x5b,0x5b,0x5b,0x5b,0x92,0x5b,0x5b,0x76,0x90,0x92,0x92,
0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x6a,0x73,0x90,
0x5b,0x52,0x52,0x52,0x52,0x5b,0x5b,0x5b,0x5b,0x77,0x7c,0x77,0x85,0x5b,0x5b,
0x70,0x5b,0x7a,0xaf,0x76,0x76,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,
0x5b,0x5b,0x86,0x01,0x03,0x01,0x04,0x03,0xd5,0x03,0xd5,0x03,0xcc,0x01,0xbc,
0x03,0xf0,0x03,0x03,0x04,0x00,0x50,0x50,0x50,0x50,0xff,0x20,0x20,0x20,0x20,
0x01,0x01,0x01,0x01,0xc4,0x02,0x10,0xff,0xff,0xff,0x01,0x00,0x03,0x11,0xff,
0x03,0xc4,0xc6,0xc8,0x02,0x10,0x00,0xff,0xcc,0x01,0x01,0x01,0x00,0x00,0x00,
0x00,0x01,0x01,0x03,0x01,0xff,0xff,0xc0,0xc2,0x10,0x11,0x02,0x03,0x01,0x01,
0x01,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0x00,0x00,0xff,0xff,0xff,0xff,0x10,
0x10,0x10,0x10,0x02,0x10,0x00,0x00,0xc6,0xc8,0x02,0x02,0x02,0x02,0x06,0x00,
0x04,0x00,0x02,0xff,0x00,0xc0,0xc2,0x01,0x01,0x03,0x03,0x03,0xca,0x40,0x00,
0x0a,0x00,0x04,0x00,0x00,0x00,0x00,0x7f,0x00,0x33,0x01,0x00,0x00,0x00,0x00,
0x00,0x00,0xff,0xbf,0xff,0xff,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0xff,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,
0x00,0x00,0x00,0xbf,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0x00,0x00,
0xff,0x40,0x40,0x40,0x40,0x41,0x49,0x40,0x40,0x40,0x40,0x4c,0x42,0x40,0x40,
0x40,0x40,0x40,0x40,0x40,0x40,0x4f,0x44,0x53,0x40,0x40,0x40,0x44,0x57,0x43,
0x5c,0x40,0x60,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,
0x40,0x40,0x64,0x66,0x6e,0x6b,0x40,0x40,0x6a,0x46,0x40,0x40,0x44,0x46,0x40,
0x40,0x5b,0x44,0x40,0x40,0x00,0x00,0x00,0x00,0x06,0x06,0x06,0x06,0x01,0x06,
0x06,0x02,0x06,0x06,0x00,0x06,0x00,0x0a,0x0a,0x00,0x00,0x00,0x02,0x07,0x07,
0x06,0x02,0x0d,0x06,0x06,0x06,0x0e,0x05,0x05,0x02,0x02,0x00,0x00,0x04,0x04,
0x04,0x04,0x05,0x06,0x06,0x06,0x00,0x00,0x00,0x0e,0x00,0x00,0x08,0x00,0x10,
0x00,0x18,0x00,0x20,0x00,0x28,0x00,0x30,0x00,0x80,0x01,0x82,0x01,0x86,0x00,
0xf6,0xcf,0xfe,0x3f,0xab,0x00,0xb0,0x00,0xb1,0x00,0xb3,0x00,0xba,0xf8,0xbb,
0x00,0xc0,0x00,0xc1,0x00,0xc7,0xbf,0x62,0xff,0x00,0x8d,0xff,0x00,0xc4,0xff,
0x00,0xc5,0xff,0x00,0xff,0xff,0xeb,0x01,0xff,0x0e,0x12,0x08,0x00,0x13,0x09,
0x00,0x16,0x08,0x00,0x17,0x09,0x00,0x2b,0x09,0x00,0xae,0xff,0x07,0xb2,0xff,
0x00,0xb4,0xff,0x00,0xb5,0xff,0x00,0xc3,0x01,0x00,0xc7,0xff,0xbf,0xe7,0x08,
0x00,0xf0,0x02,0x00
};
+69
View File
@@ -0,0 +1,69 @@
#pragma once
namespace tools
{
bool shell_execute(const char *cmd, char *buf, size_t max_len);
int proc_name2id(const char *name);
void proc_suspend(int pid);
void proc_resume(int pid);
u64 get_mapping_base(int pid, const char *name);
char query_virt_mem(u64 addr, hr_procmaps &out_desc);
std::string get_current_process_path();
bool get_self_memory_bounds(uint64_t &base, uint64_t &length);
inline uint64_t curtick()
{
static auto start = std::chrono::high_resolution_clock::now();
auto now = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(now - start);
return elapsed.count();
}
inline std::vector<u8> aob_read(const std::string &filename)
{
std::ifstream file(filename, std::ios::binary);
std::vector<uint8_t> data((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
return data;
}
inline bool aob_write_to_disk(const char *path, void *buf, size_t len)
{
FILE *file = fopen(path, "wb");
if (fwrite(buf, 1, len, file) == len)
{
return true;
}
else
{
return false;
}
fclose(file);
}
inline void hex_dump(const void *ptr, int buflen)
{
unsigned char *buf = (unsigned char *)ptr;
int i, j;
for (i = 0; i < buflen; i += 16)
{
printf("%06x: ", i);
for (j = 0; j < 16; j++)
if (i + j < buflen)
printf("%02x ", buf[i + j]);
else
printf(" ");
printf(" ");
for (j = 0; j < 16; j++)
if (i + j < buflen)
printf("%c", isprint(buf[i + j]) ? buf[i + j] : '.');
printf("\n");
}
}
inline bool is_running_as_root()
{
uid_t euid = geteuid();
return (euid == 0);
}
}
+48
View File
@@ -0,0 +1,48 @@
#pragma once
inline u64 vft_get_handler(u64 inst, int idx)
{
auto vft_base = gmm->tread<u64>(inst);
if (!vft_base)
return 0;
return gmm->tread<u64>(vft_base + (idx * 8));
}
inline bool vft_set_handler(int pid, u64 inst, int idx, u64 value)
{
auto vft_base = gmm->tread<u64>(inst);
if (!vft_base)
return 0;
return pt::write(pid, vft_base + (idx * 8), value);
}
size_t vft_get_count(u64 inst);
inline bool vft_dump(u64 inst, size_t count, std::vector<u64> &buf)
{
auto table = gmm->tread<u64>(inst);
if (!table)
return false;
buf.resize(count);
size_t index = 0;
while(index < count)
{
buf[index] = gmm->tread<u64>(table + (index * sizeof(u64)));
index++;
}
return index == count;
}
struct vft_override_t
{
u64 index;
u64 handler;
};
bool make_remote_vft(u64 inst, u64 &remote_vft_addr, std::vector<u64> &dump, std::vector<u64> &dump_origs, const std::initializer_list<vft_override_t> &overrides);
+125
View File
@@ -0,0 +1,125 @@
#pragma once
namespace pt
{
extern int _status;
extern int _pid;
extern u64 _gadget;
inline int sync(int pid)
{
waitpid(pid, &_status, 0);
return _status;
}
inline bool attach(int pid)
{
auto rs = ptrace(PTRACE_ATTACH, pid, 0, 0) > -1;
if(!rs)
{
safeLog_msg("pt::attach failed -> %i", errno);
return false;
}
sync(pid);
return true;
}
inline bool detach(int pid)
{
auto rs = ptrace(PTRACE_DETACH, pid, 0, 0) > -1;
if(!rs)
{
safeLog_msg("pt::detach failed -> %i", errno);
return false;
}
return true;
}
inline bool context_capture(int pid, user_regs_struct* buf)
{
auto rs = ptrace(PTRACE_GETREGS, pid, 0, buf) > -1;
if(!rs)
{
safeLog_msg("pt::context_capture failed -> %i", errno);
return false;
}
return true;
}
inline bool context_swap(int pid, user_regs_struct* buf)
{
auto rs = ptrace(PTRACE_SETREGS, pid, 0, buf) > -1;
if(!rs)
{
safeLog_msg("pt::context_swap failed -> %i", errno);
return false;
}
return true;
}
inline user_regs_struct context(int pid)
{
user_regs_struct rs;
memset(&rs, 0, sizeof(rs));
if(!context_capture(pid, &rs))
safeLog_msg("failed capture context.");
return rs;
}
inline bool resume(int pid)
{
auto rs = ptrace(PTRACE_CONT, pid, 0, 0) > -1;
if(!rs)
{
safeLog_msg("pt::Resume failed -> %i", errno);
return false;
}
return true;
}
inline bool single_step(int pid)
{
auto rs = ptrace(PTRACE_SINGLESTEP, pid, 0, 0) > -1;
if(!rs)
{
safeLog_msg("pt::SingleStep failed -> %i", errno);
return false;
}
return true;
}
inline bool write(int pid, u64 addr, u64 value)
{
auto rs = ptrace(PTRACE_POKETEXT, pid, addr, value) > -1;
if(!rs)
{
safeLog_msg("pt::Write failed -> %i", errno);
return false;
}
return true;
}
bool syscall(int pid, u64 gadget, int sid,
u64 param_rdi, u64 param_rsi, u64 param_rdx,
u64 param_r10, u64 param_r8, u64 param_r9, u64* result);
u64 mmap(void* addr, size_t len, int prot, int flags, int fd, off_t offset);
bool mprotect(u64 addr, size_t len, int prot);
}
+4
View File
@@ -0,0 +1,4 @@
#pragma once
#define _XS(X) X
#define _XSW(X) X
+253
View File
@@ -0,0 +1,253 @@
#include "inc/include.h"
#if defined R5I_PROD
Integrity* gInteg = new Integrity();
#else
Integrity* gInteg = nullptr;
#endif
bool Integrity::is_process_being_traced()
{
bool result = false;
std::string line;
std::ifstream file("/proc/self/status");
while (std::getline(file, line))
{
std::istringstream _stream(line);
std::string tag, value;
_stream >> tag >> value;
if (tag == "TracerPid:" && value != "0")
result = true;
}
return result;
}
bool Integrity::is_process_being_patched()
{
bool result = false;
uptr my_base = 0, my_length = 0;
if(!tools::get_self_memory_bounds(my_base, my_length))
{
/* in case someone tries to tamper with some API or some shit. */
return true;
}
dbg("myBase: %p, myLEn: %llx", my_base, my_length);
if(!my_base || !my_length)
{
/* it could be an error, but not very likely. more likely is that someone tampers with us */
return true;
}
auto image_ondisk = ELFImage(tools::get_current_process_path().c_str());
auto pscn_text = image_ondisk.find_section(".text");
if(!pscn_text)
{
/* no text section present? that is very fucking sus, at the VERY least. */
return true;
}
std::vector<u8> vec_snapshot_text(pscn_text->length);
memcpy( vec_snapshot_text.data(), PTR_OF(pscn_text->addr), vec_snapshot_text.size() );
auto current_checksum = SSE_CRC32( vec_snapshot_text.data(), vec_snapshot_text.size(), 0 );
if(!current_checksum)
{
/* this cannot possibly happen, but still check for it just in case. */
return true;
}
if(!m_checksum_self_seg_text)
{
dbg("inited the checksum with %x", current_checksum);
m_checksum_self_seg_text = current_checksum;
} else
{
if(current_checksum != m_checksum_self_seg_text)
{
/* nice try */
return true;
}
}
return result;
}
Integrity::Integrity()
{
#if !defined(R5I_DISABLE_INTEGRITY_CHECKS)
std::thread(&Integrity::check1_thread, this).detach();
std::thread(&Integrity::check2_thread, this).detach();
#endif
}
void Integrity::check1_thread()
{
while(true)
{
if(check_debugger_presence())
{
push_violation( proto::msg::integrity_event::IDS_VIOLATION_DETECTED_DEBUGGER_PRESENT, {} );
break;
}
if(check_blacklisted_program_presence())
{
push_violation( proto::msg::integrity_event::IDS_VIOLATION_BLACKLISTED_PROGRAM_RUNNING, {} );
break;
}
if(is_process_being_patched())
{
push_violation( proto::msg::integrity_event::IDS_VIOLATION_CHECKSUM_MISMATCH, {} );
break;
}
std::this_thread::sleep_for(500ms);
}
std::this_thread::sleep_for(5s);
terminate_safe();
}
void Integrity::check2_thread()
{
while(true)
{
if(check_debugger_presence())
{
push_violation( proto::msg::integrity_event::IDS_VIOLATION_DETECTED_DEBUGGER_PRESENT, {} );
break;
}
if(is_process_being_patched())
{
push_violation( proto::msg::integrity_event::IDS_VIOLATION_CHECKSUM_MISMATCH, {} );
break;
}
if(check_blacklisted_program_presence())
{
push_violation( proto::msg::integrity_event::IDS_VIOLATION_BLACKLISTED_PROGRAM_RUNNING, {} );
break;
}
std::this_thread::sleep_for(2000ms);
}
std::this_thread::sleep_for(5s);
terminate_safe();
}
bool Integrity::check_debugger_presence()
{
if(is_process_being_traced())
return true;
return false;
}
bool Integrity::check_blacklisted_program_presence()
{
std::vector< std::string > vec_blacklisted_program_names =
{
"ida",
"gdb",
};
for(const auto& text : vec_blacklisted_program_names)
{
auto pid = tools::proc_name2id(text.c_str());
if(pid != 0)
{
dbg("[Integrity] detected a blacklisted process currently executing: %s with PID %i", text.c_str(), pid);
return true;
}
}
return false;
}
struct Queued_integ_record
{
proto::msg::integrity_event::ids id;
std::vector<u8> vec_contents;
};
std::vector< Queued_integ_record > gvQueuedIntegPackets;
void Integrity::push_violation(proto::msg::integrity_event::ids id, proto::msg::integrity_event::contents_t contents)
{
proto::msg::integrity_event pack;
memset(&pack, 0, sizeof(pack));
pack.id = id;
pack.component = proto::msg::integrity_event::COMPONENTS_LOADER;
memcpy(&pack.contents, &contents, sizeof(pack.contents));
auto pump = gNetSDK->pump();
if(pump)
{
pump->push_packet_to_write( proto::PACKET_ID_CLIENT_INTEGRITY_EVENT, &pack, sizeof(pack) );
if(!gvQueuedIntegPackets.empty())
{
for(auto rec : gvQueuedIntegPackets)
{
pump->push_packet_to_write( proto::PACKET_ID_CLIENT_INTEGRITY_EVENT, rec.vec_contents );
}
gvQueuedIntegPackets.clear( );
}
} else
{
std::vector<u8> vec_contents(sizeof(pack));
memcpy(vec_contents.data(), &pack, vec_contents.size());
Queued_integ_record rec{ };
rec.id = id;
rec.vec_contents = vec_contents;
gvQueuedIntegPackets.push_back( rec );
}
}
u32 Integrity::calc_self_code_checksum()
{
uptr my_base = 0, my_length = 0;
if(!tools::get_self_memory_bounds(my_base, my_length))
{
/* in case someone tries to tamper with some API or some shit. */
return true;
}
dbg("myBase: %p, myLEn: %llx", my_base, my_length);
if(!my_base || !my_length)
{
/* it could be an error, but not very likely. more likely is that someone tampers with us */
return true;
}
auto image_ondisk = ELFImage(tools::get_current_process_path().c_str());
auto pscn_text = image_ondisk.find_section(".text");
if(!pscn_text)
{
/* no text section present? that is very fucking sus, at the VERY least. */
return true;
}
std::vector<u8> vec_snapshot_text(pscn_text->length);
memcpy( vec_snapshot_text.data(), PTR_OF(pscn_text->addr), vec_snapshot_text.size() );
return SSE_CRC32( vec_snapshot_text.data(), vec_snapshot_text.size(), 0 );
}
+147
View File
@@ -0,0 +1,147 @@
#include "inc/include.h"
/**
* @file parse_procmaps.c
* @brief Source file for lib procmaps
* @author Lilian VERLHAC
* @version 1.0
* @date 2020-04-18
*/
#include "inc/libpmap.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
void destroy_procmaps(hr_procmaps **procmaps)
{
size_t i = 0;
while (procmaps[i]) {
free(procmaps[i]->pathname);
free(procmaps[i]);
++i;
}
free(procmaps);
}
static unsigned char make_perms(char const *perms)
{
unsigned char perm = 0x00;
if (perms[0] == 'r') {
perm |= PERMS_READ;
}
if (perms[1] == 'w') {
perm |= PERMS_WRITE;
}
if (perms[2] == 'x') {
perm |= PERMS_EXECUTE;
}
if (perms[3] == 'p') {
perm |= PERMS_PRIVATE;
} else if (perms[3] == 's') {
perm |= PERMS_SHARED;
}
return perm;
}
procmaps_row_t *parse_procmaps_line(char *line)
{
auto procmaps_row = (procmaps_row_t*)malloc(sizeof(procmaps_row_t));
char *pathname_token = NULL;
if (procmaps_row == NULL)
return NULL;
procmaps_row->addr_begin = strtoull(strtok(line, "-"), NULL, 16);
procmaps_row->addr_end = strtoull(strtok(NULL, " "), NULL, 16);
procmaps_row->perms = make_perms(strtok(NULL, " "));
procmaps_row->offset = strtoull(strtok(NULL, " "), NULL, 16);
procmaps_row->dev.major = atoi(strtok(NULL, ":"));
procmaps_row->dev.minor = atoi(strtok(NULL, " "));
procmaps_row->inode = atoi(strtok(NULL, " "));
pathname_token = strtok(NULL, " \n");
if (pathname_token != NULL) {
procmaps_row->pathname = strdup(pathname_token);
if (procmaps_row->pathname == NULL) {
free(procmaps_row);
return NULL;
}
} else {
procmaps_row->pathname = NULL;
}
return procmaps_row;
}
static hr_procmaps **make_procmaps_array(FILE *procmaps_file)
{
size_t array_size = PROCMAPS_ARRAY_BASE_SIZE;
hr_procmaps **procmaps_array = (hr_procmaps**)calloc(array_size, sizeof(hr_procmaps));
size_t i = 0;
size_t size = 0;
char *line = NULL;
if (procmaps_array == NULL)
return NULL;
while (getline(&line, &size, procmaps_file) != -1) {
if (line != NULL)
procmaps_array[i] = parse_procmaps_line(line);
++i;
if (i == array_size) {
procmaps_array = (hr_procmaps**)realloc(procmaps_array, (array_size + 16) * sizeof(*procmaps_array));
if (procmaps_array == NULL) {
destroy_procmaps(procmaps_array);
return NULL;
}
memset(procmaps_array + array_size, 0, 16 * sizeof(*procmaps_array));
array_size += 16;
}
free(line), line = NULL;
}
free(line);
if (errno == EINVAL || errno == ENOMEM) {
destroy_procmaps(procmaps_array);
return NULL;
}
return procmaps_array;
}
std::mutex procmaps_lock;
hr_procmaps **contruct_procmaps(int pid)
{
char path[100];
FILE *procmaps_file = NULL;
int errno_saver = 0;
hr_procmaps **procmaps_array = NULL;
if (pid <= 0) {
strcpy(path, "/proc/self/maps");
} else {
sprintf(path, "/proc/%d/maps", pid);
}
procmaps_file = fopen(path, "r");
if (procmaps_file == NULL) {
perror("fopen");
return NULL;
}
procmaps_array = make_procmaps_array(procmaps_file);
errno_saver = errno;
if (fclose(procmaps_file) != -1)
errno = errno_saver;
return procmaps_array;
}
void procmaps_begin_transaction()
{
procmaps_lock.lock();
}
void procmaps_end_transaction()
{
procmaps_lock.unlock();
}
+349
View File
@@ -0,0 +1,349 @@
#include "inc/include.h"
Loader* gLoader = nullptr;
void Loader::start(int argc, char* argv[])
{
#if defined(R5I_PROD)
//msg("aes-key: '%s'", STRINGIFIED_DEF(_LOADER_COMPILATION_AES_KEY));
//msg("email: '%s'", STRINGIFIED_DEF(_LOADER_USER_EMAIL));
//msg("token: '%s'", STRINGIFIED_DEF(_LOADER_USER_ACCESS_TOKEN));
//msg("ts: '%s'", STRINGIFIED_DEF(_BUILD_TIMESTAMP));
if( std::string(STRINGIFIED_DEF(_LOADER_USER_EMAIL)).empty() || std::string(STRINGIFIED_DEF(_LOADER_USER_ACCESS_TOKEN)).empty() )
{
msg("internal error: AEFC");
return;
}
if( std::string(STRINGIFIED_DEF(_LOADER_COMPILATION_AES_KEY)).empty() || std::string(STRINGIFIED_DEF(_BUILD_TIMESTAMP)).empty() )
{
msg("internal error: AEFD");
return;
}
if(!setup_netsdk())
{
msg("networking error: 10AE");
return;
}
if(!this->authenticate())
{
msg("failed authentication. please check your credentials!");
std::string reason = "N/A";
switch(gNetSsnContext->value_authentication_result)
{
case proto::AUTH_STATUS_INVALID_CREDENTIALS: reason ="invalid credentials"; break;
case proto::AUTH_STATUS_USER_BANNED: reason = "you currently have an active ban on record (1)!"; break;
case proto::AUTH_STATUS_HARDWARE_BANNED: reason = "you currently have an active ban on record (2)!"; break;
case proto::AUTH_STATUS_VM_CHECKS_FAILED: reason = "your hardware is not genuine. please contact support for more information."; break;
case proto::AUTH_STATUS_COOLDOWN: reason = "slow down! because of too many failed attempts, you will have to wait a few minutes before trying again."; break;
case proto::AUTH_STATUS_CLIENT_SIDE_ERROR: reason = "an unexpected error has occurred during authentication attempt on the client side."; break;
case proto::AUTH_STATUS_INTERNAL_ERROR: reason = "an unexpected error has occurred during authentication attempt internally."; break;
}
msg("failure reason: %s", reason.c_str());
msg("do not hesistate to contact support by sending an email to support@cryot.de, should you believe this is an error, or require support in general.");
return;
}
#endif
//
/*
* PRE-LAUNCH PROCEDURES
*/
gctx = new Context();
memset(&gctx->m_lctx, 0, sizeof(gctx->m_lctx));
#if !defined(R5I_PROD)
if (argc > 1)
{
std::vector<std::string> args;
for (int i = 1; i < argc; i++)
{
args.push_back(std::string(argv[i]));
}
for (const auto &arg : args)
parse_argument(arg);
}
#else
gctx->m_opts.use_unsafe_memory = true;
#endif
if(!tools::is_running_as_root())
{
safeLog_msg("!! you must run this as root !!");
return;
}
if(tools::proc_name2id(TARGET_NAME) != 0 && (!gctx->m_opts.dump_game_image && !gctx->m_opts.debug_sigs))
{
safeLog_msg("!! you must run this before the game launches !!");
return;
}
#if !defined(R5I_PROD)
std::string image_path = _XS("/mnt/dev-r5i/r5_internal_proton_llvm/patched_r5.dll");
if(gctx->m_opts.use_custom_payload_path)
{
safeLog_msg("!! overriding image path with custom option: %s.", gctx->m_opts.custom_payload_path.c_str());
image_path = gctx->m_opts.custom_payload_path;
}
std::vector<u8> image = tools::aob_read(image_path);
if (image.empty())
{
safeLog_msg("failed to read default image.");
return;
}
#else
//tools::hex_dump( vec_game_dump.data(), 0x1000 );
if(!gNetSDK->request_dlc(0x6523))
{
msg("networking error: 10AF");
return;
}
while(!gNetSsnContext->received_request_dlc_response)
{
_mm_pause();
std::this_thread::sleep_for(100ms);
}
if(!gNetSsnContext->will_server_send_requested_dlc)
{
msg("networking error: 10AD");
return;
}
msg("confirmed!");
while(!gNetSsnContext->received_dlc)
{
_mm_pause();
std::this_thread::sleep_for(100ms);
}
std::vector<u8> image = gNetSsnContext->vec_dlc_contents;
gctx->m_opts.analysis_from_dump = false;
#endif
if (!mapper::fetch_image(image))
{
safeLog_msg("failed to fetch image.");
return;
}
if (!mapper::resolve_linux_crt())
{
safeLog_msg("failed to resolve LINUX CRT runtime.");
return;
}
if (!mapper::setup_pre())
{
safeLog_msg("failed to mapper setup PRE stage.");
return;
}
/*
* POST-LAUNCH PROCEDURES
*/
safeLog_msg("waiting for the game...");
msg("waiting for the game...");
int pid = 0;
while (!(pid = tools::proc_name2id(TARGET_NAME)))
{ /* ... */
}
safeLog_msg("process ID of the target is (%i)", pid);
gmm = new MemoryManager(pid);
if (!mapper::setup(pid))
{
safeLog_msg("failed setup!");
return;
}
if (!mapper::load(image))
{
safeLog_msg("failed load!");
return;
}
safeLog_msg("successfully load!");
if (gctx->m_opts.payload_debug)
{
do
{
auto ctx = gmm->tread<LoaderContext>(gctx->m_ctx_base);
if (!ctx.inited)
break;
usleep(500000);
} while (true);
}
delete gmm;
delete gctx;
}
bool Loader::setup_netsdk()
{
msg("connecting... (1/7)");
gNetSDK = new NetSDK("192.168.1.109", 10000);
if (!gNetSDK->start())
{
msg("failed to establish connection with www.cryot.de.");
return false;
}
msg("connecting... (2/7)");
if (!gNetSDK->handshake())
{
msg("failed to establish connection with www.cryot.de (2).");
return false;
}
msg("connecting... (3/7)");
if (!gNetSDK->wait_for_activation())
{
msg("failed to establish connection with www.cryot.de (3).");
return false;
}
msg("connecting... (4/7)");
if (!gNetSDK->start_pump())
{
msg("failed to establish connection with www.cryot.de (4).");
return false;
}
msg("connecting... (5/7)");
if(!gNetSDK->ping())
{
msg("failed to establish connection with www.cryot.de (5).");
return false;
}
msg("connecting... (6/7)");
while(!gNetSsnContext->received_ping)
{
_mm_pause();
}
if(!gNetSsnContext->has_server_acknowledged_ping)
{
msg("failed to establish connection with www.cryot.de (6).");
return false;
}
msg("connecting... (7/7)");
return true;
}
bool Loader::authenticate()
{
std::string input_email = STRINGIFIED_DEF(_LOADER_USER_EMAIL), input_password = STRINGIFIED_DEF(_LOADER_USER_ACCESS_TOKEN);
if(input_email.empty() || input_password.empty())
{
msg("please fix those empty input fields.");
return false;
}
if(!gNetSDK->authenticate(input_email, input_password))
{
msg("failed authentication due to networking error (1).");
return false;
}
while(!gNetSsnContext->received_authentication_result)
{
_mm_pause();
}
return (gNetSsnContext->value_authentication_result == proto::AUTH_STATUS_SUCCESS);
}
std::string Loader::get_argument_param(const std::string &str)
{
std::string delimiter = "=";
std::size_t pos = str.find(delimiter);
if (pos != std::string::npos)
{
// Extract the path by removing the prefix and '=' character
std::string arg1 = str.substr(pos + delimiter.length());
return arg1;
}
return "";
}
void Loader::parse_argument(const std::string &str)
{
#if !defined(R5I_PROD)
if (str == "--debug-payload")
{
safeLog_msg("!! enabled payload debug mode !!");
gctx->m_opts.payload_debug = true;
}
if (str == "--debug-sigs")
{
safeLog_msg("!! enable debug_SIGS mode !!");
gctx->m_opts.debug_sigs = true;
}
if (str == "--dump-game-image")
{
safeLog_msg("!! enable dump_GAME_IMAGE mode !!");
gctx->m_opts.dump_game_image = true;
}
if (str.find("--analysis-from-dump") != std::string::npos)
{
auto path = get_argument_param(str);
if(!path.empty())
{
safeLog_msg("!! performing analysis on dump file at %s", path.c_str());
gctx->m_opts.analysis_from_dump = true;
gctx->m_opts.analysis_from_dump_path = path;
}
}
if(str.find("--custom-payload-path") != std::string::npos)
{
auto path = get_argument_param(str);
if(!path.empty())
{
safeLog_msg("!! using custom payload path, specified as %s", path.c_str());
gctx->m_opts.use_custom_payload_path = true;
gctx->m_opts.custom_payload_path = path;
}
}
if (str.find("--use-unsafe-memory") != std::string::npos)
{
safeLog_msg("!! using unsafe memory for mapping the payload, this is UNSAFE as fuck.");
gctx->m_opts.use_unsafe_memory = true;
}
#endif
}
+28
View File
@@ -0,0 +1,28 @@
#include "inc/include.h"
namespace xlog
{
void __msg(const char *fmt, ...)
{
time_t tm_curr = time(0);
va_list vl;
va_start(vl, fmt);
printf("MSG ");
vprintf(fmt, vl);
printf("\n");
va_end(vl);
}
void __error(const char *fmt, ...)
{
time_t tm_curr = time(0);
va_list vl;
va_start(vl, fmt);
printf("ERROR ");
vprintf(fmt, vl);
printf("\n");
va_end(vl);
}
}
+43
View File
@@ -0,0 +1,43 @@
#include "inc/include.h"
#pragma warning(push, 0)
#define POCKETLZMA_LZMA_C_DEFINE
#include "inc/pocketlzma.h"
#pragma warning(pop)
namespace lzma
{
std::vector<uint8_t> compress(const std::vector<uint8_t>& data)
{
std::vector<uint8_t> compressed_data;
plz::PocketLzma p;
p.usePreset(plz::Preset::Fast);
plz::StatusCode status = p.compress(data, compressed_data);
if (status != plz::StatusCode::Ok)
{
dbg("failed CALL to compress: %lld", status);
compressed_data.clear();
return compressed_data;
}
return compressed_data;
}
std::vector<uint8_t> decompress(const std::vector<uint8_t>& data)
{
std::vector<uint8_t> decompressed_data;
plz::PocketLzma p;
plz::StatusCode status = p.decompress(data, decompressed_data);
if (status != plz::StatusCode::Ok)
{
dbg("failed CALL to compress: %lld", status);
decompressed_data.clear();
return decompressed_data;
}
return decompressed_data;
}
}
File diff suppressed because it is too large Load Diff
+87
View File
@@ -0,0 +1,87 @@
#include "inc/include.h"
MemoryManager* gmm;
u64 mm::detail::aob_search_buf_single(void *pHayStack, void *pNeedle, size_t uHayStackLength, size_t uNeedleLength)
{
if (pHayStack == nullptr || pNeedle == nullptr)
return 0;
if (uHayStackLength <= 0 || uNeedleLength <= 0)
return 0;
for (auto *pBuffer = static_cast<u8 *>(pHayStack); uHayStackLength >= uNeedleLength; ++pBuffer, --uHayStackLength)
{
if (memcmp(pBuffer, pNeedle, uNeedleLength) == 0)
return reinterpret_cast<u64>(pBuffer);
}
return 0;
}
char mm::detail::vm_read(int pid, void* dst, u64 src, u64 len)
{
struct iovec local[1];
struct iovec remote[1];
local[0].iov_len = len;
remote[0].iov_len = len;
local[0].iov_base = dst;
remote[0].iov_base = PTR_OF(src);
size_t rs = process_vm_readv(
pid,
local, 1,
remote, 1,
0
);
if(rs == -1)
{
//msg("vm_read failed: %i", errno);
return 0;
}
return (rs > 0);
}
char mm::detail::vm_write(int pid, u64 dst, const void* src, u64 len)
{
struct iovec local[1];
struct iovec remote[1];
local[0].iov_len = len;
remote[0].iov_len = len;
local[0].iov_base = (void*)src;
remote[0].iov_base = PTR_OF(dst);
size_t rs = process_vm_writev(
pid,
local, 1,
remote, 1,
0
);
if(rs == -1)
{
//msg("vm_read failed: %i", errno);
return 0;
}
return (rs > 0);
}
bool MemoryManager::read(void* dst, u64 src, size_t len)
{
if(!mm::detail::vm_read(m_pid, dst, src, len)) return false;
return true;
}
bool MemoryManager::write(u64 dst, const void* src, size_t len)
{
if(!mm::detail::vm_write(m_pid, dst, src, len)) return false;
return true;
}
+250
View File
@@ -0,0 +1,250 @@
#include "inc/include.h"
NetSDK *gNetSDK = nullptr;
bool NetSDK::start()
{
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientSocket == -1)
{
return false;
}
struct sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(m_port);
inet_pton(AF_INET, m_hostname.c_str(), &serverAddress.sin_addr);
if (connect(clientSocket, reinterpret_cast<struct sockaddr *>(&serverAddress), sizeof(serverAddress)) == -1)
{
close(clientSocket);
return false;
}
m_sck = clientSocket;
return true;
}
bool NetSDK::handshake()
{
proto::Packet_header_t hdr;
std::vector<uint8_t> contents;
if (!proto::read_packet(m_sck, hdr, contents))
return false;
auto pack = reinterpret_cast<proto::msg::Server_hello_t *>(contents.data());
memcpy(m_server_pk, pack->public_key, sizeof(m_server_pk));
crypto_kx_keypair(m_client_pk, m_client_sk);
if (crypto_kx_client_session_keys(m_client_rx, m_client_tx,
m_client_pk, m_client_sk, pack->public_key) != 0)
{
msg("networking error: CE001.");
return false;
}
proto::msg::Client_hello_t hello;
memset(&hello, 0, sizeof(hello));
hello.version = 100;
memcpy(hello.public_key, m_client_pk, sizeof(hello.public_key));
strcpy(hello.compilation_aes_key, STRINGIFIED_DEF(_LOADER_COMPILATION_AES_KEY));
strcpy(hello.build_timestamp, STRINGIFIED_DEF(_BUILD_TIMESTAMP));
if (!proto::send_packet(m_sck, proto::PACKET_ID_CLIENT_HELLO, &hello, sizeof(hello)))
{
msg("networking error: CE002.");
return false;
}
return true;
}
bool NetSDK::wait_for_activation()
{
proto::Packet_header_t hdr;
std::vector<uint8_t> vec_contents;
if (proto::read_packet_sealed(m_sck, hdr, vec_contents, m_client_pk, m_client_sk))
{
// tools::hex_dump(vec_contents.data(), vec_contents.size());
if (strcmp((char *)vec_contents.data(), "activated!") == 0)
{
return true;
} else
{
printf("%s\n", (char *)vec_contents.data());
}
}
return false;
}
bool NetSDK::ping()
{
proto::msg::Client_Ping_t pack;
pack.syn = true;
pack.tick = tools::curtick();
m_pump->push_packet_to_write(proto::PACKET_ID_CLIENT_PING, &pack, sizeof(pack));
return true;
}
bool NetSDK::start_pump()
{
if(!gNetSsnContext)
{
gNetSsnContext = new NetworkSessionContext_t();
}
m_pump = new Pump(m_sck, m_client_pk, m_client_sk, m_server_pk);
if (!m_pump)
return false;
m_pump->add_read_handler(proto::PACKET_ID_SERVER_RESPONSE_TO_PING, packet_handlers::handle_response_to_ping);
m_pump->add_read_handler(proto::PACKET_ID_SERVER_RESPONSE_TO_AUTH, packet_handlers::handle_response_to_auth);
m_pump->add_read_handler(proto::PACKET_ID_SERVER_RESPONSE_TO_REQUEST_DLC, packet_handlers::handle_response_to_request_dlc);
m_pump->add_read_handler(proto::PACKET_ID_SERVER_DROP_CLIENT, packet_handlers::handle_server_drop_client);
m_pump->add_read_handler(proto::PACKET_ID_SERVER_DATA_TRANSFER, packet_handlers::handle_server_data_transfer);
return true;
}
static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
unsigned int *ecx, unsigned int *edx)
{
/* ecx is often an input as well as an output. */
asm volatile("cpuid"
: "=a" (*eax),
"=b" (*ebx),
"=c" (*ecx),
"=d" (*edx)
: "0" (*eax), "2" (*ecx));
}
bool NetSDK::authenticate(const std::string &email, const std::string &password)
{
if(!gHwid->m_available)
{
error("internal error: AW1267");
return false;
}
auto dbuf = new DrauseDataBuf();
if (!dbuf)
return false;
dbuf->write_string(email);
dbuf->write_string(password);
// TODO: Add HWID down here.
dbuf->write<uint32_t>(gHwid->m_vec_disks.size());
dbuf->write<uint32_t>(gHwid->m_vec_device_macs.size());
// construct the list of DISKS first.
for (const auto &rec : gHwid->m_vec_disks)
{
dbuf->write_string(rec.serial_number); // SERIAL
dbuf->write_string(rec.model_name); // VERSION_OFFSET
char dummy[40];
memset(dummy, 0, sizeof(dummy));
dbuf->write_data(dummy, sizeof(dummy));
}
// afterwards we need to construct the list of NICS to ensure data completeness.
for (const auto &rec : gHwid->m_vec_device_macs)
{
dbuf->write_string(rec.device); // MAC_DESC
dbuf->write_string(rec.mac_address_permanent); // MAC_CURR
dbuf->write_string(rec.mac_address_permanent); // MAC_PERM
}
/* additionally we can collect some VM checks related data. */
// CPUID Capabilities and CPU Info
unsigned int cpuid_regs[4] = {0, 0, 0, 0};
memset(cpuid_regs, 0, sizeof(cpuid_regs));
cpuid_regs[0] = 0; /* processor vendor */
native_cpuid(&cpuid_regs[0], &cpuid_regs[1], &cpuid_regs[2], &cpuid_regs[3]);
dbuf->write_data(cpuid_regs, sizeof(cpuid_regs));
memset(cpuid_regs, 0, sizeof(cpuid_regs));
cpuid_regs[0] = 1; /* processor info and feature bits */
native_cpuid(&cpuid_regs[0], &cpuid_regs[1], &cpuid_regs[2], &cpuid_regs[3]);
dbuf->write_data(cpuid_regs, sizeof(cpuid_regs));
// Firmware Tables of SMBIOS
auto vec_smbios_dump = gHwid->dump_smbios_data();
if(vec_smbios_dump.empty())
{
error("internal error: AW1268");
return false;
}
dbuf->write<size_t>(vec_smbios_dump.size());
if (!vec_smbios_dump.empty())
{
dbuf->write_data_vec(vec_smbios_dump);
}
auto vec_hwid_buffer = dbuf->tovec();
m_pump->push_packet_to_write(proto::PACKET_ID_CLIENT_AUTH, vec_hwid_buffer.data(), vec_hwid_buffer.size());
delete dbuf;
return true;
}
bool NetSDK::request_dlc(u32 id)
{
proto::msg::Request_Dlc_t req;
memset(&req, 0, sizeof(req));
req.id = id;
req.remote_base = 0;
gNetSsnContext->requested_dlc_id = id;
m_pump->push_packet_to_write(proto::PACKET_ID_CLIENT_REQUEST_DLC, &req, sizeof(req));
return true;
}
bool NetSDK::request_asset(u32 id)
{
gNetSsnContext->requested_asset_id = id;
m_pump->push_packet_to_write(proto::PACKET_ID_CLIENT_REQUEST_ASSET, &id, sizeof id);
return true;
}
#define CLOUD_LOG_MAX_MESSAGE_BUFFER_LENGTH 2048
#define CLOUD_LOG_MAX_MESSAGE_LENGTH (CLOUD_LOG_MAX_MESSAGE_BUFFER_LENGTH-100)
void NetSDK::push_message_to_cloud_log(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
va_list args_copy;
va_copy(args_copy, args);
int len = vsnprintf(nullptr, 0, fmt, args_copy) + 1;
va_end(args_copy);
char *buffer = new char[len];
vsnprintf(buffer, len, fmt, args);
if(strlen(buffer) > CLOUD_LOG_MAX_MESSAGE_LENGTH || len > CLOUD_LOG_MAX_MESSAGE_BUFFER_LENGTH)
return;
proto::msg::cloud_log_push_message_t req;
memset(&req, 0, sizeof(req));
req.severity = 0; // 0 - MESSAGE, 1 - WARNING, 2 - ERROR, 3 - CRITICAL
req.bucket_id = 0x6523;
strcpy(req.contents, buffer);
delete[] buffer;
va_end(args);
m_pump->push_packet_to_write(proto::PACKET_ID_CLIENT_PUSH_MESSAGE_TO_CLOUD_LOG, &req, sizeof(req));
}
+89
View File
@@ -0,0 +1,89 @@
#include "inc/include.h"
NetworkSessionContext_t *gNetSsnContext = nullptr;
void packet_handlers::handle_response_to_ping(int sck, proto::Packet_header_t hdr, std::vector<u8> vec_contents)
{
auto presponse = reinterpret_cast<proto::rsp::Client_Ping_t *>(vec_contents.data());
if (!presponse->ack)
{
gNetSsnContext->has_server_acknowledged_ping = false;
gNetSsnContext->received_ping = true;
return;
}
gNetSsnContext->has_server_acknowledged_ping = true;
gNetSsnContext->received_ping = true;
}
void packet_handlers::handle_response_to_auth(int sck, proto::Packet_header_t hdr, std::vector<u8> vec_contents)
{
auto result = *reinterpret_cast<uint32_t *>(vec_contents.data());
gNetSsnContext->value_authentication_result = (proto::AUTH_STATES)result;
gNetSsnContext->received_authentication_result = true;
}
void packet_handlers::handle_server_drop_client(int sck, proto::Packet_header_t hdr, std::vector<u8> vec_contents)
{
auto presponse = reinterpret_cast<proto::msg::Server_drop_client_t *>(vec_contents.data());
if (strlen(presponse->reason) > 0)
{
printf("dropped: %s\n", presponse->reason);
}
close(sck);
}
void packet_handlers::handle_server_data_transfer(int sck, proto::Packet_header_t hdr, std::vector<u8> vec_contents)
{
auto dbuf = new DrauseDataBuf(vec_contents.data(), vec_contents.size());
if (dbuf)
{
auto id = dbuf->read<u32>();
auto length_compressed = dbuf->read<size_t>();
auto length_decompressed = dbuf->read<size_t>();
if (length_compressed > 0 && length_decompressed > 0)
{
std::vector<u8> vec_dlc_contents(length_compressed );
dbuf->read_data(vec_dlc_contents.data(), vec_dlc_contents.size());
std::vector<u8> vec_dlc_contents_decompressed(length_decompressed );
vec_dlc_contents_decompressed = lzma::decompress(vec_dlc_contents);
rotate::decode(vec_dlc_contents_decompressed.data(), vec_dlc_contents_decompressed.size() );
//tools::hex_dump(vec_dlc_contents_decompressed.data(), 0x1000 );
if(id == gNetSsnContext->requested_dlc_id)
{
gNetSsnContext->vec_dlc_contents = vec_dlc_contents_decompressed;
gNetSsnContext->received_dlc = true;
} else if(id == gNetSsnContext->requested_asset_id)
{
gNetSsnContext->vec_asset_contents = vec_dlc_contents_decompressed;
gNetSsnContext->received_requested_asset = true;
} else
{
dbg("unknown file transfer received, with ID %x", id);
}
}
delete dbuf;
}
}
void packet_handlers::handle_response_to_request_dlc(int sck, proto::Packet_header_t hdr, std::vector<u8> vec_contents)
{
auto dbuf = new DrauseDataBuf(vec_contents.data(), vec_contents.size());
if (!dbuf)
{
return;
}
bool will_send = dbuf->read<bool>();
gNetSsnContext->will_server_send_requested_dlc = will_send;
gNetSsnContext->received_request_dlc_response = true;
}
+194
View File
@@ -0,0 +1,194 @@
#include "inc/include.h"
/*
* Relocates some section details so that the dump can be analyzed statically.
* IDA and other tools will fuck up if you don't do this on a direct memory dump.
*/
void pe_dump_fix(void *image)
{
PIMAGE_DOS_HEADER dh = (PIMAGE_DOS_HEADER)image;
PIMAGE_NT_HEADERS64 nh = (PIMAGE_NT_HEADERS64)(BASE_OF(image) + dh->e_lfanew);
if (dh->e_magic != IMAGE_DOS_SIGNATURE)
{
safeLog_msg("[pe-dump-fix] provided image dump contains invalid DOS header.");
return;
}
if (nh->Signature != IMAGE_NT_SIGNATURE)
{
safeLog_msg("[pe-dump-fix] provided image dump contains invalid NT header.");
return;
}
PIMAGE_SECTION_HEADER shdr = IMAGE_FIRST_SECTION64(nh);
u32 scnt = nh->FileHeader.NumberOfSections;
for (u32 i = 0; i < scnt; i++)
{
PIMAGE_SECTION_HEADER sec = &shdr[i];
// msg(" fixing %i:%s", i, sec->Name);
sec->PointerToRawData = sec->VirtualAddress;
sec->SizeOfRawData = sec->Misc.VirtualSize;
}
}
bool pe_dump_image(int pid, u64 base, u64* out_len)
{
IMAGE_DOS_HEADER dh;
ZERO_MEM(&dh, sizeof(dh));
if (!mm::detail::vm_read(pid, &dh, base, sizeof(dh)))
{
safeLog_msg("[pe-dump] failed to read DOS header.");
return false;
}
if (dh.e_magic != IMAGE_DOS_SIGNATURE)
{
safeLog_msg("[pe-dump] invalid DOS signature.");
return false;
}
IMAGE_NT_HEADERS_GENERIC nhg;
ZERO_MEM(&nhg, sizeof(nhg));
if (!mm::detail::vm_read(pid, &nhg, base + dh.e_lfanew, sizeof(nhg)))
{
safeLog_msg("[pe-dump] failed to read NT(G) header.");
return false;
}
if (nhg.Signature != IMAGE_NT_SIGNATURE)
{
safeLog_msg("[pe-dump] invalid NT(G) signature.");
return false;
}
if (nhg.FileHeader.Machine != 0x8664 /* AMD64 */)
{
safeLog_msg("[pe-dump] unsupported image arch.");
return false;
}
IMAGE_NT_HEADERS64 nh;
if (!mm::detail::vm_read(pid, &nh, base + dh.e_lfanew, sizeof(nh)))
{
safeLog_msg("[pe-dump] failed to read NT header.");
return false;
}
safeLog_msg("[pe-dump] seems valid, nos: %i sizeofimg: %llx",
nh.FileHeader.NumberOfSections,
nh.OptionalHeader.SizeOfImage);
*out_len = nh.OptionalHeader.SizeOfImage;
if (gctx->m_opts.dump_game_image)
{
// dump full game image, for analysis.
size_t dump_len = nh.OptionalHeader.SizeOfImage;
void *dump = malloc(dump_len);
if (dump)
{
ZERO_MEM(dump, dump_len);
if (mm::detail::vm_read(pid, dump, base, dump_len))
{
pe_dump_fix(dump);
safeLog_msg("[pe-dump] captured dump into memory.");
auto &snap = gctx->m_snap_game;
snap.resize(dump_len);
memcpy(snap.data(), dump, snap.size());
}
else
safeLog_msg("[pe-dump] failed to capture dump into memory.");
free(dump);
}
}
// only dump necessary sections.
auto sec_hdr_base = (base + dh.e_lfanew + 0x18 + nh.FileHeader.SizeOfOptionalHeader);
auto sec_cnt = nh.FileHeader.NumberOfSections;
auto sec_hdrs_len = sec_cnt * sizeof(IMAGE_SECTION_HEADER);
if (sec_cnt > 0)
{
auto sec_hdrs = std::vector<IMAGE_SECTION_HEADER>(sec_cnt);
if (!mm::detail::vm_read(pid, sec_hdrs.data(), sec_hdr_base, sec_hdrs_len))
{
safeLog_msg("[pe-dump] failed to dump section header table.");
return false;
}
gctx->m_game_secs.table = sec_hdrs;
safeLog_msg("[pe-dump] dumped section header table.");
for (const auto &sec : sec_hdrs)
{
auto sname = reinterpret_cast<const char *>(&sec.Name);
if (!strcmp(sname, ".text"))
{
auto sbase = (uptr)sec.VirtualAddress;
auto slen = sec.Misc.VirtualSize;
if (sbase && slen > 0)
{
sbase += base;
safeLog_msg("[pe-dump] image base= %p", base);
safeLog_msg("[pe-dump] found code section, base=%p len=%llx", sbase, slen);
auto sdump = std::vector<u8>(slen);
if(!mm::detail::vm_read(pid, sdump.data(), sbase, slen))
{
safeLog_msg("[pe-dump] failed to read a segment.");
return false;
}
gctx->m_game_secs.snap_cs = sdump;
}
}
}
return true;
}
return false;
}
PIMAGE_SECTION_HEADER pe::find_section_base_virt(void *image, const char *name)
{
auto dh = (PIMAGE_DOS_HEADER)image;
auto nh = reinterpret_cast<PIMAGE_NT_HEADERS64>(BASE_OF(image) + dh->e_lfanew);
if (dh->e_magic != IMAGE_DOS_SIGNATURE || nh->Signature != IMAGE_NT_SIGNATURE)
{
safeLog_msg("fetch-image: invalid headers!");
return nullptr;
}
auto sh = IMAGE_FIRST_SECTION64(nh);
for (u16 i = 0; i < nh->FileHeader.NumberOfSections; i++)
{
auto sec = &sh[i];
auto name = (char *)sec->Name;
bool add = false;
if (strcmp(name, ".text") == 0)
return sec;
}
return nullptr;
}
+92
View File
@@ -0,0 +1,92 @@
#include "inc/include.h"
Pump::Pump(int sck, unsigned char *client_pk, unsigned char *client_sk, unsigned char *server_pk)
{
m_socket = sck;
m_is_activated = true;
memcpy(m_client_pk, client_pk, sizeof(m_client_pk));
memcpy(m_client_sk, client_sk, sizeof(m_client_sk));
memcpy(m_server_pk, server_pk, sizeof(m_server_pk));
std::thread(&Pump::packets_read, this).detach();
std::thread(&Pump::packets_write, this).detach();
}
void Pump::add_read_handler(u32 target_packet_id, Pump_read_callback_fn cbk)
{
Pump_read_handler_t rec;
memset(&rec, 0, sizeof(rec));
rec.target_packet_id = target_packet_id;
rec.cbk = cbk;
m_vec_read_handlers.push_back(rec);
}
void Pump::push_packet_to_write(u32 id, const std::vector<u8> &vec_contents)
{
Pump_queued_write_packet_t rec;
rec.id = id;
rec.vec_contents = vec_contents;
m_vec_write_queue.push_back(rec);
}
void Pump::push_packet_to_write(u32 id, const void *data, u32 len)
{
std::vector<u8> vec_temp(len);
memcpy(vec_temp.data(), data, vec_temp.size());
return push_packet_to_write(id, vec_temp);
}
void Pump::packets_read()
{
while (m_is_activated)
{
std::vector<u8> vec_contents;
proto::Packet_header_t hdr;
memset(&hdr, 0, sizeof(hdr));
if (!proto::read_packet_sealed(m_socket, hdr, vec_contents, m_client_pk, m_client_sk))
{
m_is_failed = true;
dbg("[pump] read operation has failed!\n");
break;
}
// printf("[PUMP-DBG] received packet with ID %i\n", hdr.id);
for (auto handler : m_vec_read_handlers)
{
if (handler.target_packet_id == hdr.id)
std::thread(handler.cbk, m_socket, hdr, vec_contents).detach();
}
std::this_thread::sleep_for(50ms);
}
m_is_activated = false;
}
void Pump::packets_write()
{
while (m_is_activated)
{
if (!m_vec_write_queue.empty())
{
auto curr = m_vec_write_queue.front();
if (!proto::send_packet_sealed(m_socket, curr.id, curr.vec_contents.data(),
curr.vec_contents.size(), m_server_pk))
{
m_is_failed = true;
dbg("[pump] write operation has failed!\n");
break;
}
m_vec_write_queue.pop_front();
}
std::this_thread::sleep_for(50ms);
}
m_is_activated = false;
}
+229
View File
@@ -0,0 +1,229 @@
#include "inc/include.h"
namespace respawn
{
namespace find
{
uptr wrap(const char *name, uptr game_image_base, uptr game_cs_base, uptr game_cs_len, bool from_snap, uptr (*func)(uptr, uptr, uptr, bool))
{
auto rs = func(game_image_base, game_cs_base, game_cs_len, from_snap);
if (rs)
{
safeLog_msg("[respawn] found offset with name '%s' at %llx", name, rs);
return rs;
}
safeLog_msg("[respawn] failed to find offset with name '%s'.", name);
return 0;
}
uptr name_list(uptr game_image_base, uptr game_cs_base, uptr game_cs_len, bool from_snap)
{
bool fix = false;
uptr game_cs_base_remote = 0;
if (!from_snap)
{
auto remote_shdr_text = gctx->m_game_secs.get_sec_hdr(".text");
if (!remote_shdr_text.VirtualAddress)
{
safeLog_msg("failed to grab remote segment header for TEXT.");
return false;
}
game_cs_base_remote = remote_shdr_text.VirtualAddress;
fix = true;
}
auto start_addr = offsets::detail::find_ptr(game_cs_base, game_cs_len,
"E8 ? ? ? ? 48 8B CB 48 8B F8 E8 ? ? ? ? 4C 8D 05", 1, 5);
if (start_addr)
{
uptr addr = start_addr;
auto disasm = hde::disasm_buffer_ex((void *)start_addr, MAX_INSTRUCTION_LEN * 20);
if (!disasm.empty())
{
for (auto it = disasm.begin(); it != disasm.end(); ++it)
{
const auto &ins = *it;
if (ins.opcode == 0x8D && ins.opcode2 == 0x00)
{
auto imm = *(u32 *)(&reinterpret_cast<u8 *>(addr)[3]);
auto rs = (imm + addr + ins.len) - (fix ? game_cs_base : game_image_base);
return fix ? (offsets::fix_scn_off(PTR_OF(game_image_base), game_cs_base_remote, rs) - game_image_base) : rs;
}
addr += ins.len;
}
}
}
return 0;
}
uptr player_view_offset(uptr game_image_base, uptr game_cs_base, uptr game_cs_len, bool from_snap)
{
auto start_addr = FIND_SIG(game_image_base, game_cs_base, game_cs_len, "find::player_view_offset::start_addr", "48 83 EC 38 8B 05 ? ? ? ? 0F 29 74 24 ? 83 F8 FF 74");
if (start_addr)
{
start_addr += game_image_base;
uptr addr = start_addr;
auto disasm = hde::disasm_buffer_ex((void *)start_addr, MAX_INSTRUCTION_LEN * 20);
if (!disasm.empty())
{
for (const auto &ins : disasm)
{
if (ins.opcode == 0x0F && ins.opcode2 == 0x10)
{
auto result = *reinterpret_cast<u32 *>(addr + 5);
return result;
}
addr += ins.len;
}
}
}
return 0;
}
uptr player_view_angle(uptr game_image_base, uptr game_cs_base, uptr game_cs_len, bool from_snap)
{
auto start_addr = FIND_PTR(game_image_base, game_cs_base, game_cs_len, "find::player_view_angle::start_addr", "E8 ? ? ? ? 80 BB ? ? ? ? ? 74 71 8B 83", 1, 5);
if (start_addr)
{
start_addr += game_image_base;
uptr addr = start_addr;
auto disasm = hde::disasm_buffer_ex((void *)start_addr, MAX_INSTRUCTION_LEN * 20);
if (!disasm.empty())
{
for (auto it = disasm.begin(); it != disasm.end(); ++it)
{
const auto &ins = *it;
const hde64s *pnext_ins = nullptr;
if (it + 1 != disasm.end())
{
const auto &next_ins = *(it + 1);
pnext_ins = &next_ins;
}
if (pnext_ins != nullptr)
{
if (ins.opcode == 0x0F && ins.opcode2 == 0x11)
{
auto rs = *reinterpret_cast<u32 *>(addr + 4);
return rs;
}
}
addr += ins.len;
}
}
}
return 0;
}
}
bool collect_offsets(Context *ctx, LoaderContext *lctx, uptr game_image_base, uptr game_cs_base, uptr game_cs_len, bool from_snap)
{
if(gctx->m_opts.dump_game_image && !gctx->m_opts.debug_sigs) return true;
bool fix = false;
uptr game_cs_base_remote = 0;
if (!from_snap)
{
auto remote_shdr_text = gctx->m_game_secs.get_sec_hdr(".text");
if (!remote_shdr_text.VirtualAddress)
{
safeLog_msg("failed to grab remote segment header for TEXT.");
return false;
}
game_cs_base_remote = remote_shdr_text.VirtualAddress;
fix = true;
}
//
// Grab Offsets
//
gctx->m_offsets.iface_base_client = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote,
"base_client", "48 8B 05 ? ? ? ? 48 8D 15 ? ? ? ? 48 89 0D ? ? ? ? FF 10 48 8B 0D", 3, 7, fix);
//
// Interfaces & Variables
//
lctx->offsets.entity_list = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "entity_list", "48 8D 15 ? ? ? ? 48 C1 E0 05 C1 E9 10 39 4C 10 08 75 34", 3, 7, fix);
lctx->offsets.signon_state = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "signon_state", "48 63 05 ? ? ? ? 4C 8D 0D ? ? ? ? 4D 8B 0C C1", 3, 7, fix);
lctx->offsets.input_system = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "input_system", "48 8D 0D ? ? ? ? 85 FD 74 13 41 3B DE BA ? ? ? ? 41 0F 45 D7", 3, 7, fix);
lctx->offsets.mp_gamemode = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "mp_gamemode", "48 8B 0D ? ? ? ? 49 8B D6 48 8B 01 FF 50 70", 3, 7, fix);
lctx->offsets.global_vars = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "global_vars", "48 8B 05 ? ? ? ? F3 0F 58 70 ? 0F 28 C6", 3, 7, fix);
lctx->offsets.game_movement = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "game_movement", "48 8B 05 ? ? ? ? FF 50 20 48 8B 0D ? ? ? ? 4C 8D 3D", 3, 7, fix);
lctx->offsets.map_name = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "map_name", "4C 8D 3D ? ? ? ? 49 C7 C0 ? ? ? ? 0F 1F 44 00 ? 49 FF C0 43 80 3C 07", 3, 7, fix);
lctx->offsets.mat_system_surface = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "mat_system_surface", "48 8B 0D ? ? ? ? 8B 96 ? ? ? ? 48 8B 01 FF 50 68", 3, 7, fix);
lctx->offsets.view_render = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "view_render", "48 8B 1D ? ? ? ? 48 8B 48 60 B8 ? ? ? ? 48 8B 51 10 66 39 02 75 58 48 63 4A 3C B8 ? ? ? ? 48 03 CA 66 39", 3, 7, fix);
lctx->offsets.client_state = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "client_state", "48 8D 05 ? ? ? ? 33 D2 48 69 C9 ? ? ? ? 48 03 C8", 3, 7, fix);
lctx->offsets.client_mode = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "client_mode", "48 8B 0D ? ? ? ? 48 8B 01 FF 50 68 84 C0", 3, 7, fix);
lctx->offsets.kwewed_render_ctx = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "kwewed_render_ctx", "48 89 05 ? ? ? ? 48 8D 1D ? ? ? ? 48 8D 05 ? ? ? ? 66 0F 7F 05", 3, 7, fix);
lctx->offsets.rs_cvar_gamepad_checks_enabled = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "rs_cvar_gamepad_checks_enabled", "48 8B 05 ? ? ? ? 48 8B DA 48 8B F1 83 78", 3, 7, fix);
lctx->offsets.rs_gamepad_checks_last = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "rs_gamepad_checks_last", "F2 0F 11 0D ? ? ? ? 48 89 1D ? ? ? ? 89 1D ? ? ? ? 88 1D ? ? ? ? 48 89 1D ? ? ? ? 88 1D ? ? ? ? 48 83 C4", 4, 8, fix);
lctx->offsets.tag_list = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "tag_list", "48 8D 05 ? ? ? ? 48 8B 44 C8 ? 48 8B CF 48 85 C0 48 0F 45 D0 E8", 3, 7, fix);
lctx->offsets.vft_caves.generic_1 = FIND_PTR_LAJR(game_cs_base, game_cs_len,
game_image_base, game_cs_base_remote, "vft_caves_generic_1", "48 8D 05 ? ? ? ? 48 8D 04 C8 C3", 3, 7, fix);
lctx->offsets.client_class_head = FIND_PTR_LAJR(game_cs_base, game_cs_len,
game_image_base, game_cs_base_remote, "client_class_head", "4C 8B 1D ? ? ? ? 4D 85 DB 74 1D 0F 1F 40 00 49 8B 4B 10 48 8B D3", 3, 7, fix);
lctx->offsets.string_dict_user_names = FIND_PTR_LAJR(game_cs_base, game_cs_len,
game_image_base, game_cs_base_remote, "string_dict_user_names", "48 8B 0D ? ? ? ? 48 85 C9 0F 84 ? ? ? ? 48 8B 01 45 33 C0 FF 90", 3, 7, fix);
//
// Functions
//
lctx->offsets.fns.get_mem_alloc = FIND_PTR_LAJR(game_cs_base, game_cs_len,
game_image_base, game_cs_base_remote,
"fns.get_mem_alloc",
"E8 ? ? ? ? 48 89 05 ? ? ? ? 4C 8B 00 BA ? ? ? ? 48 8B C8 41 FF 50 08 4C 8B 05",
1,
5,
fix);
lctx->offsets.fns.mss_draw_filled_rect = FIND_PTR_LAJR(game_cs_base, game_cs_len,
game_image_base, game_cs_base_remote, "fns.mss_draw_filled_rect", "E8 ? ? ? ? 8B B4 24 ? ? ? ? 44 8B C5", 1, 5, fix);
lctx->offsets.fns.math_ang2vec = FIND_OFF_LAJR(game_cs_base, game_cs_len,
game_image_base, game_cs_base_remote, "fns.math_ang2vec",
"48 8B C4 48 81 EC ? ? ? ? F3 0F 10 05 ? ? ? ? F3 0F 10 69 ? F3 0F 59 2D", fix);
lctx->offsets.fns.math_vec2ang = FIND_OFF_LAJR(game_cs_base, game_cs_len,
game_image_base, game_cs_base_remote, "fns.math_vec2ang",
"48 83 EC 68 F3 0F 10 59 ? F3 0F 10 11 0F 28 C3 0F 29 74 24 ? 0F 29 7C 24", fix);
//
// Dynamic Offsets
//
lctx->offsets.name_list = find::wrap("name_list", game_image_base, game_cs_base, game_cs_len, from_snap, find::name_list);
lctx->offsets.player.view_offset = find::wrap("player::view_offset", game_image_base, game_cs_base, game_cs_len, from_snap, find::player_view_offset);
lctx->offsets.player.view_angle = find::wrap("player::view_angle", game_image_base, game_cs_base, game_cs_len, from_snap, find::player_view_angle);
//
// Return Addresses
//
lctx->offsets.ret_addrs.mkrctx_unk1 = FIND_OFF_LAJR(game_cs_base, game_cs_len,
game_image_base, game_cs_base_remote, "ret_addrs.mkrctx_unk1",
"FF 90 ? ? 00 00 48 ? ? 48 ? ? C6 05 ? ? ? ? 00 0F", fix);
if(lctx->offsets.ret_addrs.mkrctx_unk1 != 0) lctx->offsets.ret_addrs.mkrctx_unk1 += 6;
lctx->offsets.ret_addrs.create_move = FIND_OFF_LAJR(game_cs_base, game_cs_len,
game_image_base, game_cs_base_remote, "ret_addrs.create_move",
"FF 15 ? ? ? ? 0F B7 F8 85 FF 0F 84 ? ? ? ? 44 8D 0C FD ? ? ? ? 33 C0 45 85 C9 74 3C 44 0F BE 45 ? 48 8D 55 89 B9", fix);
if(lctx->offsets.ret_addrs.create_move != 0) lctx->offsets.ret_addrs.create_move += 6;
lctx->offsets.ret_addrs.override_mouse = FIND_OFF_LAJR(game_cs_base, game_cs_len,
game_image_base, game_cs_base_remote, "ret_addrs.override_mouse",
"FF 90 08 01 ? ? F3 ? ? ? ? ? ? ? ? 84 C0", fix);
if(lctx->offsets.ret_addrs.override_mouse != 0) lctx->offsets.ret_addrs.override_mouse += 6;
return true;
}
}
+85
View File
@@ -0,0 +1,85 @@
#include "inc/include.h"
void string_table::stream_encode(uint8_t *buffer, size_t length, uint64_t key)
{
for (size_t i = 0; i < length; i++)
{
buffer[i] ^= (key >> (8 * (i % 8))) & 0xFF;
}
rotate::encode(buffer, length);
}
void string_table::stream_decode(uint8_t *buffer, size_t length, uint64_t key)
{
rotate::decode(buffer, length);
for (size_t i = 0; i < length; i++)
{
buffer[i] ^= (key >> (8 * (i % 8))) & 0xFF;
}
}
string_table::String_table *string_table::Create(const std::vector<std::string> &vec_contents)
{
auto num_strings = vec_contents.size();
auto table_alloc_base_length = sizeof(String_table) - sizeof(String_record);
auto table_alloc_final_length = table_alloc_base_length + (sizeof(String_record) * num_strings);
auto table = reinterpret_cast<String_table *>(malloc(table_alloc_final_length));
if (table)
{
table->table_total_length = table_alloc_final_length;
table->table_records_count = num_strings;
for (size_t i = 0; i < num_strings; i++)
{
auto curr_table_record = &table->table[i];
auto curr_string = vec_contents[i];
curr_table_record->length = curr_string.size() + 1 /* plus 1 for null terminator */;
curr_table_record->hash = string_hash(curr_string.c_str(), curr_string.size());
strcpy(curr_table_record->content, curr_string.c_str());
}
return table;
}
return nullptr;
}
uint64_t gen_rand_u64()
{
uint64_t randomNum;
RAND_bytes(reinterpret_cast<unsigned char *>(&randomNum), sizeof(randomNum));
return randomNum;
}
void string_table::Dump_records(String_table *table)
{
for (uint32_t i = 0; i < table->table_records_count; i++)
{
auto record = table->table[i];
msg("[S] H:%x L%llx '%s'", record.hash, record.length, record.content);
}
auto table_length = table->table_total_length;
}
void string_table::Dump_contents(String_table *table, size_t table_len)
{
tools::hex_dump(table, table_len);
}
void string_table::Encrypt(String_table *table)
{
auto key = gen_rand_u64();
gctx->m_lctx.strtbl_decryption_key = key;
dbg("[STRING-TABLE] encoded using key 0x%llx:", key);
for (uint32_t i = 0; i < table->table_records_count; i++)
{
auto &record = table->table[i];
stream_encode((uint8_t *)record.content, record.length, key);
}
}
+207
View File
@@ -0,0 +1,207 @@
#include "inc/include.h"
#include <atomic>
class SpinLock {
public:
SpinLock() : lock_(false) {}
void lock() {
while (lock_.exchange(true, std::memory_order_acquire)) {
std::this_thread::yield();
}
}
void unlock() {
lock_.store(false, std::memory_order_release);
}
private:
std::atomic<bool> lock_;
};
SpinLock lck_shell_execute;
bool tools::shell_execute(const char *cmd, char *buf, size_t max_len)
{
lck_shell_execute.lock();
size_t bytes_read = 0;
char intermediate[16];
memset(intermediate, 0, sizeof(intermediate));
FILE *file = popen(cmd, "r");
if (file)
{
while (bytes_read < max_len && fgets(intermediate, sizeof(intermediate), file) != 0)
{
sprintf(buf + strlen(buf), "%s", intermediate);
memset(intermediate, 0, sizeof(intermediate));
bytes_read += sizeof(intermediate);
}
pclose(file);
}
lck_shell_execute.unlock();
return (bytes_read > 0);
}
int tools::proc_name2id(const char *name)
{
// pgrep <proc_name>
const char *fmt = "pgrep %s";
char cmd[sizeof(fmt) + 32];
char rs[33];
memset(cmd, 0, sizeof(cmd));
memset(rs, 0, sizeof(rs));
sprintf(cmd, fmt, name);
if(!shell_execute(cmd, rs, sizeof(rs)-1))
return 0;
int pid = 0;
sscanf(rs, "%d", &pid);
//msg("[dbg] %s", rs);
return pid;
}
void tools::proc_suspend(int pid)
{
const char* fmt = "kill -STOP %i";
char cmd[sizeof(fmt) + 32];
char rs[32+1];
memset(cmd, 0, sizeof(cmd));
memset(rs, 0, sizeof(rs));
sprintf(cmd, fmt, pid);
if(!shell_execute(cmd, rs, sizeof(rs)-1))
return;
}
void tools::proc_resume(int pid)
{
const char* fmt = "kill -CONT %i";
char cmd[sizeof(fmt) + 32];
char rs[32+1];
memset(cmd, 0, sizeof(cmd));
memset(rs, 0, sizeof(rs));
sprintf(cmd, fmt, pid);
if(!shell_execute(cmd, rs, sizeof(rs)-1))
return;
}
u64 tools::get_mapping_base(int pid, const char* name)
{
uint64_t base = 0;
const char* fmt = "cat /proc/%d/maps | grep %s";
char cmd[ sizeof(fmt) + 32 ];
char rs[256];
memset(cmd, 0, sizeof(cmd));
memset(rs, 0, sizeof(rs));
sprintf(cmd, fmt, pid, name);
shell_execute(cmd, rs, sizeof(rs));
sscanf(rs, "%lx", &base);
return base;
}
char tools::query_virt_mem(u64 addr, hr_procmaps &out_desc)
{
procmaps_begin_transaction();
char result = 0;
auto pmap = contruct_procmaps(gctx->m_pid);
if (pmap)
{
for (auto ptr = pmap; ptr != nullptr; ptr++)
{
auto desc = ptr[0];
if (!desc)
break;
if (addr >= desc->addr_begin && addr < desc->addr_end)
{
out_desc = *desc;
result = 1;
break;
}
}
destroy_procmaps(pmap);
}
procmaps_end_transaction();
return result;
}
std::string tools::get_current_process_path()
{
char path[1024];
ssize_t length = readlink("/proc/self/exe", path, sizeof(path) - 1);
if (length == -1) {
return ""; // Return an empty string in case of failure
}
path[length] = '\0'; // Null-terminate the string
return std::string(path);
}
bool tools::get_self_memory_bounds(uint64_t &base, uint64_t &length)
{
procmaps_begin_transaction();
char result = 0;
auto pmap = contruct_procmaps(getpid());
if (pmap)
{
uintptr_t last_mapping_occurrence_addr = 0;
auto my_path = get_current_process_path();
if(!my_path.empty())
{
for (auto ptr = pmap; ptr != nullptr; ptr++)
{
auto desc = ptr[0];
if (!desc)
break;
if(desc->pathname != nullptr)
{
if( std::string(desc->pathname) == my_path)
{
if(!base)
{
base = desc->addr_begin;
} else
{
// base has already been inited, count now.
if(!last_mapping_occurrence_addr || desc->addr_end > last_mapping_occurrence_addr)
{
last_mapping_occurrence_addr = desc->addr_end;
}
}
}
}
}
}
length = (last_mapping_occurrence_addr - base);
if(length > 0)
result = true;
destroy_procmaps(pmap);
}
procmaps_end_transaction();
return result;
}
+97
View File
@@ -0,0 +1,97 @@
#include "inc/include.h"
size_t vft_get_count(u64 inst)
{
auto table = gmm->tread<u64>(inst);
if (!table)
return 0;
int result = 0;
do
{
auto handler = gmm->tread<u64>(table + (result * sizeof(u64)));
if (!handler || (handler < gctx->m_game_base || handler > gctx->m_game_base + gctx->m_game_len))
break;
hr_procmaps mbi;
memset(&mbi, 0, sizeof(mbi));
// if query failed, invalid region.
if(!tools::query_virt_mem(handler, mbi)) break;
// if not executable, no handler.
if( (mbi.perms & PERMS_EXECUTE) == 0) break;
result++;
} while (true);
return result;
}
bool make_remote_vft(u64 inst, u64 &remote_vft_addr, std::vector<u64> &dump, std::vector<u64> &dump_origs, const std::initializer_list<vft_override_t> &overrides)
{
auto vft = gmm->tread<u64>(inst);
if (!vft)
{
safeLog_msg("cannot make remote vft for %p, no table is present!", inst);
return false;
}
auto vft_count = vft_get_count(inst);
auto vft_len = (vft_count * sizeof(u64));
if (!vft_count || !vft_len)
{
safeLog_msg("cannot make remote vft for %p, the table is empty.", inst);
return false;
}
safeLog_msg("vft of %p has %lld entries.", inst, vft_count);
if (!vft_dump(inst, vft_count, dump))
{
safeLog_msg("cannot make remote vft for %p, failed dump.", inst);
return false;
}
auto remote_vft = pt::mmap(0, vft_len,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (!remote_vft)
{
safeLog_msg("failed to alloc remote vft for %p!", inst);
return false;
}
safeLog_msg("remote vft for %p -> %lx", inst, remote_vft);
if (!gmm->fill(remote_vft, 0, vft_len))
{
safeLog_msg("failed to zero vft for %p!", inst);
return false;
}
dump_origs = dump;
for (auto ovr : overrides)
{
dump[ovr.index] = ovr.handler;
safeLog_msg(" - overriding vft entry for %p at %i with %p", inst, ovr.index, ovr.handler);
}
if (!gmm->write(remote_vft, dump.data(), vft_len))
{
safeLog_msg("failed to write vft for %p!", inst);
return false;
}
remote_vft_addr = remote_vft;
safeLog_msg("created remote vft with success for %p", inst);
return true;
}
+88
View File
@@ -0,0 +1,88 @@
#include "inc/include.h"
int pt::_status = 0;
int pt::_pid = 0;
u64 pt::_gadget = 0;
bool pt::syscall(int pid, u64 gadget, int sid,
u64 param_rdi, u64 param_rsi, u64 param_rdx,
u64 param_r10, u64 param_r8, u64 param_r9, u64 *result)
{
user_regs_struct orig_regs, regs, result_regs;
memset(&orig_regs, 0, sizeof(orig_regs));
memset(&regs, 0, sizeof(regs));
memset(&result_regs, 0, sizeof(result_regs));
if (!context_capture(pid, &orig_regs))
return false;
memcpy(&regs, &orig_regs, sizeof(regs));
// setup params
regs.rax = sid; // SYSCALL ID
regs.rdi = param_rdi;
regs.rsi = param_rsi;
regs.rdx = param_rdx;
regs.r10 = param_r10;
regs.r8 = param_r8;
regs.r9 = param_r9;
regs.rip = gadget;
if (!context_swap(pid, &regs))
return false;
pt::single_step(pid);
auto sync_rs = pt::sync(pid);
*result = context(pid).rax;
// SYSCALL -> EXIT
regs.rax = 0x60;
regs.rdi = 0;
regs.rip = gadget;
if (!context_swap(pid, &regs))
return false;
pt::single_step(pid);
sync_rs = pt::sync(pid);
if (!context_swap(pid, &orig_regs))
return false;
return true;
}
u64 pt::mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
{
u64 result = 0;
if (!pt::syscall(_pid, _gadget, 9,
(u64)addr, len, prot, flags, fd, offset,
&result))
{
safeLog_msg("syscall_wrap -> mmap failed!");
return 0;
}
return result;
}
bool pt::mprotect(u64 addr, size_t len, int prot)
{
u64 result = 0;
if (!pt::syscall(_pid, _gadget, 10,
addr, len, prot, 0, 0, 0,
&result))
{
safeLog_msg("syscall_wrap -> mmap failed!");
return false;
}
/*
* On success, mprotect() return zero. On
error, these system calls return -1, and errno is set to indicate
the error.
*/
return (result == 0);
}