Skip to content

Commit

Permalink
add circus picture support
Browse files Browse the repository at this point in the history
  • Loading branch information
regomne committed Jun 10, 2018
1 parent c39bf44 commit 605bc6b
Show file tree
Hide file tree
Showing 27 changed files with 3,213 additions and 3 deletions.
65 changes: 65 additions & 0 deletions Circus/hook_proj/FileReader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "FileReader.h"
#include <windows.h>
#include <stdio.h>

NakedMemory MyFileReader::ReadToMem(const char* fileName)
{
FILE* fp = nullptr;
fopen_s(&fp, fileName, "rb");
if (!fp)
{
return NakedMemory();
}

fseek(fp, 0, SEEK_END);
auto fsize = ftell(fp);
fseek(fp, 0, SEEK_SET);

NakedMemory mem(fsize);
auto buff = mem.Get();

auto bytesRead = fread(buff, 1, fsize, fp);
fclose(fp);
if (bytesRead != fsize)
{
return NakedMemory();
}
return std::move(mem);
}

NakedMemory MyFileReader::ReadToMem(const wchar_t* fileName)
{
FILE* fp = nullptr;
_wfopen_s(&fp, fileName, L"rb");
if (!fp)
{
return NakedMemory();
}

fseek(fp, 0, SEEK_END);
auto fsize = ftell(fp);
fseek(fp, 0, SEEK_SET);

NakedMemory mem(fsize);
auto buff = mem.Get();

auto bytesRead = fread(buff, 1, fsize, fp);
fclose(fp);
if (bytesRead != fsize)
{
return NakedMemory();
}
return std::move(mem);
}

bool exists_file(const char* file_name)
{
auto attr = GetFileAttributesA(file_name);
return (attr != (DWORD)INVALID_HANDLE_VALUE) && !(attr & FILE_ATTRIBUTE_DIRECTORY);
}

bool exists_file(const wchar_t* file_name)
{
auto attr = GetFileAttributesW(file_name);
return (attr != (DWORD)INVALID_HANDLE_VALUE) && !(attr & FILE_ATTRIBUTE_DIRECTORY);
}
37 changes: 37 additions & 0 deletions Circus/hook_proj/FileReader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once
#include <stdint.h>
#include <memory>
#include "NakedMemory.h"

class IInterface
{
public:
virtual void Dispose() = 0;
};

class IFileReader :public IInterface
{
public:
virtual NakedMemory ReadToMem(const char* fileName) = 0;
virtual NakedMemory ReadToMem(const wchar_t* fileName) = 0;
};

class MyFileReader :public IFileReader
{
public:
MyFileReader() {}
~MyFileReader()
{
}
NakedMemory ReadToMem(const char* fileName) override;
NakedMemory ReadToMem(const wchar_t* fileName) override;
void Dispose() override
{
delete this;
}

private:
};

bool exists_file(const char* file_name);
bool exists_file(const wchar_t* file_name);
244 changes: 244 additions & 0 deletions Circus/hook_proj/FuncHelper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
#include "FuncHelper.h"
#include <windows.h>
#include <stdio.h>
#include "ilhook.h"

__declspec(naked) uint32_t ThiscallFunction0(void* addr, void* this_p)
{
__asm
{
mov ecx, [esp + 8];
mov eax, [esp + 4];
call eax;
ret;
}
}

__declspec(naked) uint32_t ThiscallFunction1(void* addr, void* thisp, uint32_t arg1)
{
__asm
{
push ebp;
mov ebp, esp;
push arg1;
mov ecx, thisp;
call addr;
pop ebp;
ret;
}
}
__declspec(naked) uint32_t ThiscallFunction2(void* addr, void* thisp, uint32_t arg1, uint32_t arg2)
{
__asm
{
push ebp;
mov ebp, esp;
push arg2;
push arg1;
mov ecx, thisp;
call addr;
pop ebp;
ret;
}
}

__declspec(naked) uint32_t ThiscallFunction3(void* addr, void* thisp, uint32_t arg1, uint32_t arg2, uint32_t arg3)
{
__asm
{
push ebp;
mov ebp, esp;
push arg3;
push arg2;
push arg1;
mov ecx, thisp;
call addr;
pop ebp;
ret;
}
}

__declspec(naked) uint32_t ThiscallFunction4(void* addr, void* thisp, uint32_t arg1, uint32_t arg2, uint32_t arg3, uint32_t arg4)
{
__asm
{
push ebp;
mov ebp, esp;
push arg4;
push arg3;
push arg2;
push arg1;
mov ecx, thisp;
call addr;
pop ebp;
ret;
}
}

void Log(wchar_t* format, ...)
{
wchar_t buffer[0x1000];
va_list ap;
va_start(ap, format);
auto char_cnt = vswprintf_s(buffer, format, ap);
FILE* fp = nullptr;
auto err = fopen_s(&fp, "log.log", "ab+");
fwrite(buffer, 1, char_cnt * 2, fp);
fwrite(L"\r\n", 1, 4, fp);
fclose(fp);
va_end(ap);
}

void Log(char* format, ...)
{
char buffer[0x1000];
va_list ap;
va_start(ap, format);
auto char_cnt = vsprintf_s(buffer, format, ap);
FILE* fp = nullptr;
auto err = fopen_s(&fp, "log.log", "ab+");
fwrite(buffer, 1, char_cnt, fp);
fwrite("\r\n", 1, 2, fp);
fclose(fp);
va_end(ap);
}

bool PatchMemory(PatchStruct* psts, uint32_t cnt)
{
for (size_t i = 0;i < cnt;i++)
{
PatchStruct& st = psts[i];
auto mod = GetModuleHandleA(st.mod_name);
if (!mod)
{
mod = LoadLibraryA(st.mod_name);
if (!mod)
{
LOGERROR("Patch: Can't find module: %s", st.mod_name);
return false;
}
}
auto patch_addr = (uint8_t*)mod + st.offset;

if (st.pattern != nullptr)
{
if (memcmp(patch_addr, st.pattern, st.len) != 0)
{
return false;
}
}
DWORD oldProt;
if (!VirtualProtect(patch_addr, st.len, PAGE_EXECUTE_READWRITE, &oldProt))
{
return false;
}
memcpy(patch_addr, st.hex, st.len);
}
return true;
}


bool HookFunctions(const HookPointStruct* hooks, uint32_t cnt)
{
auto buff = (uint8_t*)VirtualAlloc(0, cnt * 100, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (!buff)
{
LOGERROR("Hook: no memory!");
return false;
}

HookSrcObject src;
HookStubObject stub;

for (size_t i = 0;i < cnt;i++)
{
auto hook = &hooks[i];
auto mod = GetModuleHandleA(hook->module_name);
if (!mod)
{
mod = LoadLibraryA(hook->module_name);
if (!mod)
{
LOGERROR("Hook: Can't find module: %s", hook->module_name);
return false;
}
}
auto addr = (uint8_t*)mod + hook->offset;
auto opt_data = hook->options&STUB_JMP_ADDR_AFTER_RETURN ?
hook->dest_rva + (ptrdiff_t)mod :
hook->ret_value;
if (!InitializeHookSrcObject(&src, addr, true) ||
!InitializeStubObject(&stub, buff + i * 100, 100, opt_data, hook->options) ||
!Hook32(&src, 0, &stub, hook->hook_routine, hook->reg_tags))
{
LOGERROR("Hook: Can't hook module: %s, offset: 0x%x", hook->module_name, hook->offset);
return false;
}
}
return true;
}

bool HookFunctions(const HookPointStructWithName* hooks, uint32_t cnt)
{
auto buff = (uint8_t*)VirtualAlloc(0, cnt * 100, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (!buff)
{
LOGERROR("Hook: no memory!");
return false;
}

HookSrcObject src;
HookStubObject stub;

for (size_t i = 0;i < cnt;i++)
{
auto hook = &hooks[i];
auto mod = GetModuleHandleA(hook->module_name);
if (!mod)
{
mod = LoadLibraryA(hook->module_name);
if (!mod)
{
LOGERROR("Hook: Can't find module: %s", hook->module_name);
return false;
}
}
auto addr = GetProcAddress(mod, hook->proc_name);
if (!addr)
{
LOGERROR("Hook: Can't find %s in module: %s", hook->proc_name, hook->module_name);
return false;
}
auto opt_data = hook->options&STUB_JMP_ADDR_AFTER_RETURN ?
hook->dest_rva + (ptrdiff_t)mod :
hook->ret_value;
if (!InitializeHookSrcObject(&src, addr, true) ||
!InitializeStubObject(&stub, buff + i * 100, 100, opt_data, hook->options) ||
!Hook32(&src, 0, &stub, hook->hook_routine, hook->reg_tags))
{
LOGERROR("Hook: Can't hook module: %s, name: %s", hook->module_name, hook->proc_name);
return false;
}
}
return true;
}

std::wstring decode_string(const char* s, int cp)
{
auto len = MultiByteToWideChar(cp, 0, s, -1, 0, 0);
auto buff = new wchar_t[len];
len = MultiByteToWideChar(cp, 0, s, -1, buff, len);
std::wstring str(buff);
delete[] buff;
return std::move(str);
}

std::wstring decode_string(const char* s, uint32_t slen, int cp)
{
auto len = MultiByteToWideChar(cp, 0, s, slen, 0, 0);
auto buff = new wchar_t[len];
len = MultiByteToWideChar(cp, 0, s, slen, buff, len);
std::wstring str(buff, len);
delete[] buff;
return std::move(str);
}
Loading

0 comments on commit 605bc6b

Please sign in to comment.