Add files

This commit is contained in:
2024-11-22 00:40:41 +08:00
commit ae89581628
8 changed files with 1493 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
build/

17
CMakeLists.txt Normal file
View File

@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.20)
project(jewena_patch)
if (MSVC)
add_compile_options(/utf-8)
endif()
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")
set(DETOURS_LIB "${CMAKE_CURRENT_SOURCE_DIR}/lib/detours.lib")
add_library(jewena_patch SHARED dllmain.cpp)
target_link_libraries(jewena_patch "${DETOURS_LIB}")
add_executable(jewena-chs main.cpp)
target_link_libraries(jewena-chs "${DETOURS_LIB}")

62
dllmain.cpp Normal file
View File

@@ -0,0 +1,62 @@
#include <Windows.h>
#include "detours.h"
#include <stdio.h>
char* to_utf8(char* target, const char* source, UINT cp) {
int count = MultiByteToWideChar(cp, MB_ERR_INVALID_CHARS, source, -1, NULL, 0);
if (!count) return nullptr;
WCHAR* ws = new WCHAR[count + 1];
MultiByteToWideChar(cp, MB_ERR_INVALID_CHARS, source, -1, ws, count);
char* result = nullptr;
int ncount = WideCharToMultiByte(CP_UTF8, 0, ws, -1, nullptr, 0, nullptr, nullptr);
if (ncount) {
if (!target) {
target = new char[ncount + 1];
}
result = target;
WideCharToMultiByte(CP_UTF8, 0, ws, -1, result, ncount, nullptr, nullptr);
}
delete[] ws;
return result;
}
char* WINAPI jis_to_utf8(char* target, const char* source) {
char* result = to_utf8(target, source, CP_UTF8);
if (!result) {
result = to_utf8(target, source, 932);
}
return result;
}
PVOID GetHandle() {
HMODULE hModule = GetModuleHandleA(NULL);
return (char*)hModule + 0xf3c20;
}
extern "C" __declspec(dllexport) void Attach() {
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
PVOID h = GetHandle();
DetourAttach(&h, (PVOID)jis_to_utf8);
DetourTransactionCommit();
}
extern "C" __declspec(dllexport) void Detach() {
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
PVOID h = GetHandle();
DetourDetach(&h, (PVOID)jis_to_utf8);
DetourTransactionCommit();
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID rev) {
switch (reason) {
case DLL_PROCESS_ATTACH:
Attach();
break;
case DLL_PROCESS_DETACH:
Detach();
break;
}
return TRUE;
}

1233
include/detours.h Normal file
View File

File diff suppressed because it is too large Load Diff

27
include/detver.h Normal file
View File

@@ -0,0 +1,27 @@
//////////////////////////////////////////////////////////////////////////////
//
// 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) #x
#define DETOURS_STRINGIFY(x) DETOURS_STRINGIFY_(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 DETOURS_STRINGIFY(DETOURS_BITS)

89
include/syelog.h Normal file
View File

@@ -0,0 +1,89 @@
//////////////////////////////////////////////////////////////////////////////
//
// 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.

BIN
lib/detours.lib Normal file
View File

Binary file not shown.

64
main.cpp Normal file
View File

@@ -0,0 +1,64 @@
#include <windows.h>
#include <iostream>
int main() {
// 要启动的进程名
const char* processName = "jewena.exe";
// 要注入的 DLL 路径
const char* dllPath = "jewena_patch.dll";
// 启动进程
STARTUPINFOA si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
si.cb = sizeof(si);
// 创建新进程
if (!CreateProcessA(processName, NULL, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi)) {
std::cerr << "CreateProcess failed: " << GetLastError() << std::endl;
return 1;
}
// 在新进程中分配内存以存放 DLL 路径
LPVOID pDllPath = VirtualAllocEx(pi.hProcess, NULL, strlen(dllPath) + 1, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!pDllPath) {
std::cerr << "VirtualAllocEx failed: " << GetLastError() << std::endl;
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 1;
}
// 将 DLL 路径写入新进程的内存
if (!WriteProcessMemory(pi.hProcess, pDllPath, (LPVOID)dllPath, strlen(dllPath) + 1, NULL)) {
std::cerr << "WriteProcessMemory failed: " << GetLastError() << std::endl;
VirtualFreeEx(pi.hProcess, pDllPath, 0, MEM_RELEASE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 1;
}
// 创建远程线程以加载 DLL
HANDLE hThread = CreateRemoteThread(pi.hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandleA("kernel32.dll"), "LoadLibraryA"), pDllPath, 0, NULL);
if (!hThread) {
std::cerr << "CreateRemoteThread failed: " << GetLastError() << std::endl;
VirtualFreeEx(pi.hProcess, pDllPath, 0, MEM_RELEASE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 1;
}
// 等待线程完成
WaitForSingleObject(hThread, INFINITE);
// 清理
VirtualFreeEx(pi.hProcess, pDllPath, 0, MEM_RELEASE);
CloseHandle(hThread);
ResumeThread(pi.hThread); // 恢复新进程的执行
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
std::cout << "DLL injected successfully." << std::endl;
return 0;
}