rm detours

This commit is contained in:
恍兮惚兮 2024-10-22 00:44:15 +08:00
parent 7c2055e8a2
commit bab75cb352
15 changed files with 1025 additions and 2182 deletions

View File

@ -61,8 +61,8 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/version)
include(generate_product_version)
set(VERSION_MAJOR 3)
set(VERSION_MINOR 15)
set(VERSION_PATCH 1)
set(VERSION_MINOR 16)
set(VERSION_PATCH 0)
set(VERSION_REVISION 0)
if(BUILD_CORE)

View File

@ -51,4 +51,4 @@ target_precompile_headers(LunaHook REUSE_FROM pchhook)
set_target_properties(LunaHook PROPERTIES OUTPUT_NAME "LunaHook${bitappendix}")
target_link_libraries(LunaHook Version httpapi ws2_32 Shlwapi pch minhook commonengine utils ${YY_Thunks_for_WinXP} ${Detours})
target_link_libraries(LunaHook Version httpapi ws2_32 Shlwapi pch minhook commonengine utils ${YY_Thunks_for_WinXP})

View File

@ -1,3 +1,5 @@
#include "MinHook.h"
DynamicShiftJISCodec *dynamiccodec = new DynamicShiftJISCodec(932);
void cast_back(const HookParam &hp, void *data, size_t *len, const std::wstring &trans, bool normal)
@ -72,14 +74,19 @@ bool _1f()
return 0;
}
bool _1 = _1f();
void ReplaceFunction(PVOID *oldf, PVOID newf)
bool ReplaceFunction(PVOID oldf, PVOID newf, PVOID *pOrigin)
{
RemoveHook((uintptr_t)*oldf);
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((PVOID *)oldf, (PVOID)newf);
DetourTransactionCommit();
PVOID oldx;
if (!pOrigin)
pOrigin = &oldx;
RemoveHook((uintptr_t)oldf);
if (MH_OK == MH_CreateHook(oldf, newf, pOrigin))
return MH_OK == MH_EnableHook(oldf);
else
{
MH_RemoveHook(oldf);
return false;
}
}
void attachFunction(uintptr_t _hook_font_flag)
{
@ -89,23 +96,23 @@ void attachFunction(uintptr_t _hook_font_flag)
{
if (_func.second.attached)
continue;
if (ReplaceFunction((PVOID)_func.second.addr, (PVOID)_func.second.newFunction, (PVOID *)_func.second.oldFunction))
{
_func.second.attached = true;
*_func.second.oldFunction = _func.second.addr;
replacedfuns.push_back(_func.first);
ReplaceFunction((PVOID *)_func.second.oldFunction, (PVOID)_func.second.newFunction);
}
}
}
}
void detachall()
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
for (auto _flag : replacedfuns)
{
auto info = funcs.at(_flag);
DetourDetach((PVOID *)info.oldFunction, (PVOID)info.newFunction);
if (MH_OK == MH_DisableHook((LPVOID)info.addr))
MH_RemoveHook((LPVOID)info.addr);
}
DetourTransactionCommit();
}
void solvefont(HookParam hp)
{

View File

@ -28,7 +28,7 @@ namespace Engine
};
}
inline std::atomic<void (*)()> patch_fun = nullptr;
void ReplaceFunction(PVOID *oldf, PVOID newf);
bool ReplaceFunction(PVOID oldf, PVOID newf, PVOID *pOrigin = nullptr);
bool check_embed_able(const ThreadParam &tp);
bool checktranslatedok(void *data, size_t len);
#endif

View File

@ -827,7 +827,7 @@ namespace
hp.hook_font = F_GetGlyphOutlineA;
patch_fun = []()
{
ReplaceFunction((PVOID *)&p, (PVOID)(ULONG)Patch::Private::isLeadByteChar);
ReplaceFunction((PVOID)p, (PVOID)(ULONG)Patch::Private::isLeadByteChar);
};
}

View File

@ -1,20 +1,27 @@
#include"LCScript.h"
namespace { // unnamed
namespace ScenarioHook {
namespace Private {
#include "LCScript.h"
namespace
{ // unnamed
namespace ScenarioHook
{
namespace Private
{
// Skip trailing 0203
LPCSTR trim(LPCSTR text, int *size)
{
auto length = *size;
while (length && (UINT8)text[0] <= 127) { // remove all leading ASCII characters including zeros
while (length && (UINT8)text[0] <= 127)
{ // remove all leading ASCII characters including zeros
text++;
length--;
}
while (length && (UINT8)text[length - 1] == 0) // remove all trailing zeros
length--;
// remove all trailing illegal double-characters
enum { MinimumByte = 0x6 }; // the same as dynamicEncodingMinimumByte
enum
{
MinimumByte = 0x6
}; // the same as dynamicEncodingMinimumByte
while (length >= 2 && (UINT8)text[length - 1] < MinimumByte && (UINT8)text[length - 2] < MinimumByte)
length -= 2;
*size = length;
@ -190,7 +197,7 @@ namespace Private {
* 01D6B367 5F 57 61 7A 61 5F 4B 6F 63 68 75 00 1C 00 00 00 _Waza_Kochu....
*/
bool hook1(hook_stack*s,void* data, size_t* len1,uintptr_t*role)
bool hook1(hook_stack *s, void *data, size_t *len1, uintptr_t *role)
{
data_.clear();
@ -222,7 +229,7 @@ namespace Private {
//|| ::strlen(text) != size
|| text[size] // text length not verified since there could be trailing zeros
|| ::isalpha(text[0]) && ::isalpha(text[1]) // Sample system text in 恋姫無双: bcg_剣道場a
||all_ascii(text))
|| all_ascii(text))
return false;
auto trimmedSize = size;
@ -230,18 +237,18 @@ namespace Private {
if (trimmedSize <= 0)
return false;
//auto size = s->ecx * 4;
//auto dst = (LPSTR)s->edi;
* role = Engine::OtherRole;
// auto size = s->ecx * 4;
// auto dst = (LPSTR)s->edi;
*role = Engine::OtherRole;
auto retaddr = s->stack[8];
//if ((*(DWORD *)retaddr & 0xffffff) == 0x0cc483) // 0041C295 83C4 0C ADD ESP,0xC
// if ((*(DWORD *)retaddr & 0xffffff) == 0x0cc483) // 0041C295 83C4 0C ADD ESP,0xC
// role = Engine::ScenarioRole;
auto arg3 = s->stack[8 + 3];
if (arg3 == 0x400)
*role = Engine::ScenarioRole;
// 8/7/2015: Here, I could also split choice and scenario from the retaddr.
// But I didn't so that choice can also be display the same way asn scenario.
//sig = retaddr;
// sig = retaddr;
std::string oldData(trimmedText, trimmedSize);
@ -249,23 +256,25 @@ namespace Private {
const char *zero_str = LCSE_0;
bool containsZeros = false;
if (oldData.find('\0')!=oldData.npos) {
if (oldData.find('\0') != oldData.npos)
{
containsZeros = true;
strReplace(oldData, zero_bytes, zero_str);
//oldData.replace(zero_bytes, zero_str);
// oldData.replace(zero_bytes, zero_str);
*role = Engine::OtherRole;
// FIXME: There could be individual ascii letters before zeros (such as "k" and "n")
// They should be escaped here.
// Escaping not implemented since I am lazy.
}
write_string_overwrite(data,len1,oldData);
write_string_overwrite(data, len1, oldData);
return true;
}
void hookafter(hook_stack*s,void* data, size_t len1){
void hookafter(hook_stack *s, void *data, size_t len1)
{
int size = s->eax - 1;
if (size <= 0)
return ;
return;
ULONG edx, esi;
{
@ -280,64 +289,63 @@ namespace Private {
//|| ::strlen(text) != size
|| text[size] // text length not verified since there could be trailing zeros
|| ::isalpha(text[0]) && ::isalpha(text[1]) // Sample system text in 恋姫無双: bcg_剣道場a
||all_ascii(text))
return ;
|| all_ascii(text))
return;
auto trimmedSize = size;
auto trimmedText = trim(text, &trimmedSize);
if (trimmedSize <= 0)
return ;
return;
auto retaddr = s->stack[8];
//if ((*(DWORD *)retaddr & 0xffffff) == 0x0cc483) // 0041C295 83C4 0C ADD ESP,0xC
// if ((*(DWORD *)retaddr & 0xffffff) == 0x0cc483) // 0041C295 83C4 0C ADD ESP,0xC
// role = Engine::ScenarioRole;
auto arg3 = s->stack[8 + 3];
std::string oldData(trimmedText, trimmedSize);
static const std::string zero_bytes(1, '\0');
const char *zero_str = LCSE_0;
bool containsZeros = false;
if (oldData.find('\0')!=oldData.npos) {
if (oldData.find('\0') != oldData.npos)
{
containsZeros = true;
strReplace(oldData, zero_bytes, zero_str);
//oldData.replace(zero_bytes, zero_str);
// oldData.replace(zero_bytes, zero_str);
// FIXME: There could be individual ascii letters before zeros (such as "k" and "n")
// They should be escaped here.
// Escaping not implemented since I am lazy.
}
std::string newData = std::string((char*)data,len1);
std::string newData = std::string((char *)data, len1);
if (newData.empty() || newData == oldData)
return ;
return;
if (containsZeros)
strReplace(newData, zero_str, zero_bytes);
//newData.replace(zero_str, zero_bytes);
// newData.replace(zero_str, zero_bytes);
int prefixSize = trimmedText - text,
suffixSize = size - prefixSize - trimmedSize;
if (prefixSize)
newData.insert(0,std::string(text, prefixSize));
newData.insert(0, std::string(text, prefixSize));
if (suffixSize)
newData.append(trimmedText + trimmedSize, suffixSize);
data_ = newData;
s->eax = data_.size() + 1;
return ;
return;
}
bool hook2(hook_stack*s,void* data, size_t* len1,uintptr_t*role)
bool hook2(hook_stack *s, void *data, size_t *len1, uintptr_t *role)
{
if (!data_.empty())
s->esi = (ULONG)data_.c_str();
return false;
}
} // namespace Private
} // namespace Private
/**
/**
* Sample game:
*
* 0042FB1E CC INT3
@ -613,17 +621,17 @@ namespace Private {
* 00430E33 CC INT3
* 00430E34 CC INT3
*/
bool isLeadByteChar(const char *s)
bool isLeadByteChar(const char *s)
{
return dynsjis::isleadstr(s);
//return ::IsDBCSLeadByte(HIBYTE(testChar));
// return ::IsDBCSLeadByte(HIBYTE(testChar));
}
bool attach(ULONG startAddress, ULONG stopAddress,ULONG dyna)
{
bool attach(ULONG startAddress, ULONG stopAddress, ULONG dyna)
{
const uint8_t bytes[] = {
0x8d,0x74,0x32, 0x04, // 0042fc21 8d7432 04 lea esi,dword ptr ds:[edx+esi+0x4]
0xc1,0xe9, 0x02, // 0042fc25 c1e9 02 shr ecx,0x2
0xf3,0xa5 // 0042fc28 f3:a5 rep movs dword ptr es:[edi],dword ptr ds[esi] ; jichi: text accessed here from esi to edi
0x8d, 0x74, 0x32, 0x04, // 0042fc21 8d7432 04 lea esi,dword ptr ds:[edx+esi+0x4]
0xc1, 0xe9, 0x02, // 0042fc25 c1e9 02 shr ecx,0x2
0xf3, 0xa5 // 0042fc28 f3:a5 rep movs dword ptr es:[edi],dword ptr ds[esi] ; jichi: text accessed here from esi to edi
};
ULONG addr2 = MemDbg::findBytes(bytes, sizeof(bytes), startAddress, stopAddress);
if (!addr2)
@ -668,43 +676,47 @@ bool attach(ULONG startAddress, ULONG stopAddress,ULONG dyna)
Private::textOffset_ = *(DWORD *)addr;
}
HookParam hp;
hp.address=addr1;
hp.hook_before=Private::hook1;
hp.hook_after=Private::hookafter;
hp.type=EMBED_ABLE;
hp.newlineseperator=L"\x01";
hp.hook_font=F_GetGlyphOutlineA;
if(dyna){
hp.address = addr1;
hp.hook_before = Private::hook1;
hp.hook_after = Private::hookafter;
hp.type = EMBED_ABLE;
hp.newlineseperator = L"\x01";
hp.hook_font = F_GetGlyphOutlineA;
if (dyna)
{
static ULONG dynas;
dynas=dyna;
hp.type|=EMBED_DYNA_SJIS;
hp.hook_font=F_GetGlyphOutlineA;
patch_fun=[](){
ReplaceFunction((PVOID*)&dynas, (PVOID)(ULONG)isLeadByteChar);
dynamiccodec->setMinimumSecondByte(6);//// skip 0x1,0x2,0x3 in case dynamic encoding could crash the game
dynas = dyna;
hp.type |= EMBED_DYNA_SJIS;
hp.hook_font = F_GetGlyphOutlineA;
patch_fun = []()
{
ReplaceFunction((PVOID)dynas, (PVOID)(ULONG)isLeadByteChar);
dynamiccodec->setMinimumSecondByte(6); //// skip 0x1,0x2,0x3 in case dynamic encoding could crash the game
};
}
auto succ=NewHook(hp,"EmbedLCSE");
hp.address=addr2+4;
hp.hook_before=Private::hook2;
hp.type=EMBED_ABLE|HOOK_EMPTY;
succ|=NewHook(hp,"EmbedLCSE");
auto succ = NewHook(hp, "EmbedLCSE");
hp.address = addr2 + 4;
hp.hook_before = Private::hook2;
hp.type = EMBED_ABLE | HOOK_EMPTY;
succ |= NewHook(hp, "EmbedLCSE");
return succ;
}
} // namespace ScenarioHook
}
} // namespace ScenarioHook
namespace Patch {
namespace Patch
{
namespace Private {
namespace Private
{
bool isLeadByteChar(const char *s)
{
return dynsjis::isleadstr(s);
//return ::IsDBCSLeadByte(HIBYTE(testChar));
// return ::IsDBCSLeadByte(HIBYTE(testChar));
}
} // namespace Private
} // namespace Private
/**
/**
* Sample game:
*
* Debugging method: Find text in memory, and then insert hardware breakpoint.
@ -976,25 +988,25 @@ namespace Private {
* 00416E3F 8B86 EC000000 MOV EAX,DWORD PTR DS:[ESI+0xEC]
*/
ULONG patchEncoding(ULONG startAddress, ULONG stopAddress)
{
ULONG patchEncoding(ULONG startAddress, ULONG stopAddress)
{
const uint8_t bytes[] = {
0x8b,0x4c,0x24, 0x04, // 0040a380 8b4c24 04 mov ecx,dword ptr ss:[esp+0x4]
0x8a,0x01, // 0040a384 8a01 mov al,byte ptr ds:[ecx]
0x8a,0x49, 0x01, // 0040a386 8a49 01 mov cl,byte ptr ds:[ecx+0x1]
0x8b, 0x4c, 0x24, 0x04, // 0040a380 8b4c24 04 mov ecx,dword ptr ss:[esp+0x4]
0x8a, 0x01, // 0040a384 8a01 mov al,byte ptr ds:[ecx]
0x8a, 0x49, 0x01, // 0040a386 8a49 01 mov cl,byte ptr ds:[ecx+0x1]
0x3c, 0x81 // 0040a389 3c 81 cmp al,0x81
};
ULONG addr = MemDbg::findBytes(bytes, sizeof(bytes), startAddress, stopAddress);
return addr;// && winhook::replace_fun(addr, (ULONG)Private::isLeadByteChar);
}
return addr; // && winhook::replace_fun(addr, (ULONG)Private::isLeadByteChar);
}
} // namespace Patch
} // namespace Patch
} // unnamed namespace
bool LCScript::attach_function()
{
if (!ScenarioHook::attach(processStartAddress, processStopAddress,Patch::patchEncoding(processStartAddress, processStopAddress)))
if (!ScenarioHook::attach(processStartAddress, processStopAddress, Patch::patchEncoding(processStartAddress, processStopAddress)))
return false;
return true;

View File

@ -21,7 +21,6 @@ enum
#include "disasm/disasm.h"
#include "engine.h"
#include "embed_util.h"
#include "detours.h"
#include "hijackfuns.h"
#include "Lang/Lang.h"

File diff suppressed because it is too large Load Diff

View File

@ -1,27 +0,0 @@
//////////////////////////////////////////////////////////////////////////////
//
// Common version parameters.
//
// Microsoft Research Detours Package, Version 4.0.1
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
#define _USING_V110_SDK71_ 1
#include "winver.h"
#if 0
#include <windows.h>
#include <detours.h>
#else
#ifndef DETOURS_STRINGIFY
#define DETOURS_STRINGIFY(x) DETOURS_STRINGIFY_(x)
#define DETOURS_STRINGIFY_(x) #x
#endif
#define VER_FILEFLAGSMASK 0x3fL
#define VER_FILEFLAGS 0x0L
#define VER_FILEOS 0x00040004L
#define VER_FILETYPE 0x00000002L
#define VER_FILESUBTYPE 0x00000000L
#endif
#define VER_DETOURS_BITS DETOUR_STRINGIFY(DETOURS_BITS)

View File

@ -1,89 +0,0 @@
//////////////////////////////////////////////////////////////////////////////
//
// Detours Test Program (syelog.h of syelog.lib)
//
// Microsoft Research Detours Package
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
#pragma once
#ifndef _SYELOGD_H_
#define _SYELOGD_H_
#include <stdarg.h>
#pragma pack(push, 1)
#pragma warning(push)
#pragma warning(disable: 4200)
//////////////////////////////////////////////////////////////////////////////
//
//
#define SYELOG_PIPE_NAMEA "\\\\.\\pipe\\syelog"
#define SYELOG_PIPE_NAMEW L"\\\\.\\pipe\\syelog"
#ifdef UNICODE
#define SYELOG_PIPE_NAME SYELOG_PIPE_NAMEW
#else
#define SYELOG_PIPE_NAME SYELOG_PIPE_NAMEA
#endif
//////////////////////////////////////////////////////////////////////////////
//
#define SYELOG_MAXIMUM_MESSAGE 4086 // 4096 - sizeof(header stuff)
typedef struct _SYELOG_MESSAGE
{
USHORT nBytes;
BYTE nFacility;
BYTE nSeverity;
DWORD nProcessId;
FILETIME ftOccurance;
BOOL fTerminate;
CHAR szMessage[SYELOG_MAXIMUM_MESSAGE];
} SYELOG_MESSAGE, *PSYELOG_MESSAGE;
// Facility Codes.
//
#define SYELOG_FACILITY_KERNEL 0x10 // OS Kernel
#define SYELOG_FACILITY_SECURITY 0x20 // OS Security
#define SYELOG_FACILITY_LOGGING 0x30 // OS Logging-internal
#define SYELOG_FACILITY_SERVICE 0x40 // User-mode system daemon
#define SYELOG_FACILITY_APPLICATION 0x50 // User-mode application
#define SYELOG_FACILITY_USER 0x60 // User self-generated.
#define SYELOG_FACILITY_LOCAL0 0x70 // Locally defined.
#define SYELOG_FACILITY_LOCAL1 0x71 // Locally defined.
#define SYELOG_FACILITY_LOCAL2 0x72 // Locally defined.
#define SYELOG_FACILITY_LOCAL3 0x73 // Locally defined.
#define SYELOG_FACILITY_LOCAL4 0x74 // Locally defined.
#define SYELOG_FACILITY_LOCAL5 0x75 // Locally defined.
#define SYELOG_FACILITY_LOCAL6 0x76 // Locally defined.
#define SYELOG_FACILITY_LOCAL7 0x77 // Locally defined.
#define SYELOG_FACILITY_LOCAL8 0x78 // Locally defined.
#define SYELOG_FACILITY_LOCAL9 0x79 // Locally defined.
// Severity Codes.
//
#define SYELOG_SEVERITY_FATAL 0x00 // System is dead.
#define SYELOG_SEVERITY_ALERT 0x10 // Take action immediately.
#define SYELOG_SEVERITY_CRITICAL 0x20 // Critical condition.
#define SYELOG_SEVERITY_ERROR 0x30 // Error
#define SYELOG_SEVERITY_WARNING 0x40 // Warning
#define SYELOG_SEVERITY_NOTICE 0x50 // Significant condition.
#define SYELOG_SEVERITY_INFORMATION 0x60 // Informational
#define SYELOG_SEVERITY_AUDIT_FAIL 0x66 // Audit Failed
#define SYELOG_SEVERITY_AUDIT_PASS 0x67 // Audit Succeeeded
#define SYELOG_SEVERITY_DEBUG 0x70 // Debugging
// Logging Functions.
//
VOID SyelogOpen(PCSTR pszIdentifier, BYTE nFacility);
VOID Syelog(BYTE nSeverity, PCSTR pszMsgf, ...);
VOID SyelogV(BYTE nSeverity, PCSTR pszMsgf, va_list args);
VOID SyelogClose(BOOL fTerminate);
#pragma warning(pop)
#pragma pack(pop)
#endif // _SYELOGD_H_
//
///////////////////////////////////////////////////////////////// End of File.

View File

@ -1,18 +1,18 @@
if(${CMAKE_SIZEOF_VOID_P} EQUAL 8)
set(Detours ${CMAKE_CURRENT_LIST_DIR}/Detours-4.0.1/lib.X64/detours.lib)
#set(Detours ${CMAKE_CURRENT_LIST_DIR}/Detours-4.0.1/lib.X64/detours.lib)
else()
if(NOT DEFINED WINXP)
set(YY_Thunks_for_WinXP)
else()
set(YY_Thunks_for_WinXP ${CMAKE_CURRENT_LIST_DIR}/YY-Thunks-1.0.7-Binary/objs/X86/YY_Thunks_for_WinXP.obj)
endif()
set(Detours ${CMAKE_CURRENT_LIST_DIR}/Detours-4.0.1/lib.X86/detours.lib)
#set(Detours ${CMAKE_CURRENT_LIST_DIR}/Detours-4.0.1/lib.X86/detours.lib)
endif()
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/minhook)
include_directories(${CMAKE_CURRENT_LIST_DIR})
include_directories(${CMAKE_CURRENT_LIST_DIR}/Detours-4.0.1/include)
#include_directories(${CMAKE_CURRENT_LIST_DIR}/Detours-4.0.1/include)