示例#1
0
        public void ApplyHook(MemberInfo member)
        {
            HookAttribute[] hooks = HookInfo.GetHookAttributes(member);
            foreach (var hook in hooks)
            {
                var info = new HookInfo(member, hook);
                Logger.Log("appling {3} hook: {0:X}, {1}, {2:X}", hook.Address, member.Name, hook.Size, hook.Type);

                try
                {
                    CheckHookRace(info);

                    int key = hook.Address;

                    HookTransferStation station = null;

                    // use old station if exist
                    if (transferStations.ContainsKey(key))
                    {
                        station = transferStations[key];
                        if (station.Match(hook.Type))
                        {
                            Logger.Log("insert hook to key '{0:X}'", key);
                            station.SetHook(info);
                        }
                        else if (station.HookInfos.Count <= 0)
                        {
                            Logger.LogWarning("remove key '{0:X}' because of hook type mismatch. ", key);
                            transferStations.Remove(key);
                            station = null;
                        }
                        else
                        {
                            throw new InvalidOperationException("hook type mismatch.");
                        }
                    }

                    // create new station
                    if (station == null)
                    {
                        Logger.Log("add key '{0:X}'", key);
                        switch (hook.Type)
                        {
                        case HookType.AresHook:
                            station = new AresHookTransferStation(info);
                            break;

                        case HookType.SimpleJumpToRet:
                        case HookType.DirectJumpToHook:
                            station = new JumpHookTransferStation(info);
                            break;

                        case HookType.WriteBytesHook:
                            station = new WriteBytesHookTransferStation(info);
                            break;

                        default:
                            Logger.LogError("found unkwnow hook: " + member.Name);
                            return;
                        }

                        transferStations.Add(key, station);
                    }

                    ASMWriter.FlushInstructionCache(hook.Address, Math.Max(hook.Size, ASM.Jmp.Length));
                    maxHookSize = Math.Max(hook.Size, maxHookSize);
                }
                catch (Exception e)
                {
                    Logger.LogError("hook applied error!");
                    Logger.PrintException(e);
                }
            }
        }