C#hook代码如下

using Celeste;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using static System.Net.WebRequestMethods;

namespace ClassLibrary1
{
    public class Class1
    {
        public static int EntryPoint(string arg)
        {
            //加载hook lib
            System.IO.File.AppendAllText("E:\\Celeste\\log.txt", "----------------start log----------------\r\n");
            var harmony = new Harmony("com.example.patch");
            harmony.PatchAll();

            return 0;
        }

        //hook 发送数据的类和方法
        [HarmonyPatch(typeof(Steamworks.InteropHelp))]  //类
        [HarmonyPatch("TestIfAvailableClient")]  //方法,防止写错尽量用nameof()
        class Patch01
        {

            //Prefix返回一个bool,如果false,则不执行后续Prefix,不执行原始方法
            [HarmonyPrefix]
            static bool Prefix()
            {
                
                System.IO.File.AppendAllText("E:\\Celeste\\log.txt", "TestIfAvailableClient\r\n");
                return false;
            }
            Postfix永远执行
            //[HarmonyPostfix]
            //static void Postfix(ref int __result)  //__result表示Hook方法的返回值
            //{
            //    __result = 0;
            //}
        }

        //hook 发送数据的类和方法
        [HarmonyPatch(typeof(Steamworks.SteamUserStats))]  //类
        [HarmonyPatch("RequestCurrentStats")]  //方法,防止写错尽量用nameof()
        class Patch02
        {

            //Prefix返回一个bool,如果false,则不执行后续Prefix,不执行原始方法
            [HarmonyPrefix]
            static bool Prefix()
            {
                //Achievements.Register(Achievement.PICO8);
                System.IO.File.AppendAllText("E:\\Celeste\\log.txt", "RequestCurrentStats\r\n");
                return false;
            }
            //Postfix永远执行
            [HarmonyPostfix]
            static void Postfix(ref bool __result)  //__result表示Hook方法的返回值
            {
                __result = false;
            }
        }

        //hook 发送数据的类和方法
        [HarmonyPatch(typeof(Steamworks.SteamUserStats))]  //类
        [HarmonyPatch("RequestGlobalStats")]  //方法,防止写错尽量用nameof()
        class Patch03
        {

            //Prefix返回一个bool,如果false,则不执行后续Prefix,不执行原始方法
            [HarmonyPrefix]
            static bool Prefix()
            {
                System.IO.File.AppendAllText("E:\\Celeste\\log.txt", "RequestGlobalStats\r\n");
                return false;
            }

        }



        //hook 发送数据的类和方法
        [HarmonyPatch(typeof(Steamworks.SteamUserStats))]  //类
        [HarmonyPatch("GetAchievement")]  //方法,防止写错尽量用nameof()
        class Patch04
        {

            //Prefix返回一个bool,如果false,则不执行后续Prefix,不执行原始方法
            [HarmonyPrefix]
            static bool Prefix()
            {
                System.IO.File.AppendAllText("E:\\Celeste\\log.txt", "GetAchievement\r\n");
                return false;
            }

            //Postfix永远执行
            [HarmonyPostfix]
            static void Postfix(ref bool __result)  //__result表示Hook方法的返回值
            {
                __result = false;
            }
        }

        //hook 发送数据的类和方法
        [HarmonyPatch(typeof(Steamworks.SteamUserStats))]  //类
        [HarmonyPatch("SetAchievement")]  //方法,防止写错尽量用nameof()
        class Patch05
        {

            //Prefix返回一个bool,如果false,则不执行后续Prefix,不执行原始方法
            [HarmonyPrefix]
            static bool Prefix()
            {
                System.IO.File.AppendAllText("E:\\Celeste\\log.txt", "SetAchievement\r\n");
                return false;
            }

        }

        //不知道为什么这个不能进行hook
        hook 发送数据的类和方法
        //[HarmonyPatch(typeof(Celeste.Achievements))]  //类
        //[HarmonyPatch("Has")]  //方法,防止写错尽量用nameof()
        //class Patch06
        //{
        //    //Prefix返回一个bool,如果false,则不执行后续Prefix,不执行原始方法
        //    [HarmonyPrefix]
        //    static bool Prefix()
        //    {

        //        System.IO.File.AppendAllText("E:\\Celeste\\log.txt", "Has\r\n");
        //        return false;
        //    }
        //    //Postfix永远执行
        //    [HarmonyPostfix]
        //    static void Postfix(ref bool __result)  //__result表示Hook方法的返回值
        //    {
        //        __result = true;
        //    }
        //}

    }
}

注入代码如下


#include <Windows.h>
#include <stdio.h>
#include <iostream>
#include <mscoree.h>
#include <metahost.h>
#include <assert.h>

#define BUFFER_SIZE 500
typedef long (__stdcall *CLRCreateInstance_Des)(REFCLSID clsid, REFIID riid, /*iid_is(riid)*/ LPVOID* ppInterface);


static CLRCreateInstance_Des CLRCreateInstance_Fun;

void StartTheDotNetRuntime(LPCWSTR runtimeVersion, LPCWSTR dllPath, LPCWSTR startClass, LPCWSTR startMethod, LPCWSTR startArgument)
{
	ICLRMetaHost* pMetaHost = NULL;
	ICLRMetaHostPolicy* pMetaHostPolicy = NULL;
	ICLRDebugging* pCLRDebugging = NULL;

	
	CLRCreateInstance_Fun(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&pMetaHost);
	CLRCreateInstance_Fun(CLSID_CLRMetaHostPolicy, IID_ICLRMetaHostPolicy, (LPVOID*)&pMetaHostPolicy);
	CLRCreateInstance_Fun(CLSID_CLRDebugging, IID_ICLRDebugging, (LPVOID*)&pCLRDebugging);

	DWORD dwVersion = 0;
	DWORD dwImageVersion = 0;
	ICLRRuntimeInfo* pRuntimeInfo;
	HRESULT result;
	result = pMetaHost->GetRuntime(runtimeVersion, IID_ICLRRuntimeInfo, (LPVOID*)&pRuntimeInfo);
	assert(SUCCEEDED(result));

	ICLRRuntimeHost* pRuntimeHost = NULL;
	result = pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (LPVOID*)&pRuntimeHost);
	assert(SUCCEEDED(result));

	result = pRuntimeHost->Start();
	assert(SUCCEEDED(result));

	DWORD dwRetCode = 0;
	result = pRuntimeHost->ExecuteInDefaultAppDomain(dllPath, startClass, startMethod, startArgument, &dwRetCode);
	assert(SUCCEEDED(result));
	pRuntimeHost->Stop();
	pRuntimeHost->Release();
	pRuntimeInfo->Release();
	pCLRDebugging->Release();
	pMetaHostPolicy->Release();
	pMetaHost->Release();
}

extern "C" void Loader()
{
	HMODULE hValve = LoadLibrary(L"mscoree.dll");
	if (hValve!=NULL) {
		CLRCreateInstance_Fun = (CLRCreateInstance_Des)GetProcAddress((HMODULE)hValve, "CLRCreateInstance");
		if (CLRCreateInstance_Fun != nullptr) {
			StartTheDotNetRuntime(L"v4.0.30319", L"ClassLibrary1.dll", L"ClassLibrary1.Class1", L"EntryPoint", L"");
		}
	}
	
}


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值