220 lines
9.6 KiB
C#
220 lines
9.6 KiB
C#
// Decompiled with JetBrains decompiler
|
|||
|
|
// Type: MindLated.Services.InjectHelper
|
||
|
|
// Assembly: Server, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
|
||
|
|
// MVID: 33673F0A-7F43-4A2B-BC08-8E59A15CB7DA
|
||
|
|
// Assembly location: C:\Users\Admin\Desktop\Sheet RAT 2.5\Server.exe
|
||
|
|
|
||
|
|
using dnlib.DotNet;
|
||
|
|
using dnlib.DotNet.Emit;
|
||
|
|
using dnlib.Utils;
|
||
|
|
using System;
|
||
|
|
using System.Windows.Forms;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Linq;
|
||
|
|
|
||
|
|
//#nullable disable
|
||
|
|
namespace MindLated.Services {
|
||
|
|
|
||
|
|
public static class InjectHelper
|
||
|
|
{
|
||
|
|
private static TypeDefUser Clone(TypeDef origin)
|
||
|
|
{
|
||
|
|
TypeDefUser typeDefUser1 = new TypeDefUser(origin.Namespace, origin.Name);
|
||
|
|
typeDefUser1.Attributes = origin.Attributes;
|
||
|
|
TypeDefUser typeDefUser2 = typeDefUser1;
|
||
|
|
if (origin.ClassLayout != null)
|
||
|
|
typeDefUser2.ClassLayout = (ClassLayout) new ClassLayoutUser(origin.ClassLayout.PackingSize, origin.ClassSize);
|
||
|
|
foreach (GenericParam genericParameter in (IEnumerable<GenericParam>) origin.GenericParameters)
|
||
|
|
typeDefUser2.GenericParameters.Add((GenericParam) new GenericParamUser(genericParameter.Number, genericParameter.Flags, (UTF8String) "-"));
|
||
|
|
return typeDefUser2;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static MethodDefUser Clone(MethodDef origin)
|
||
|
|
{
|
||
|
|
MethodDefUser methodDefUser = new MethodDefUser(origin.Name, (MethodSig) null, origin.ImplAttributes, origin.Attributes);
|
||
|
|
foreach (GenericParam genericParameter in (IEnumerable<GenericParam>) origin.GenericParameters)
|
||
|
|
methodDefUser.GenericParameters.Add((GenericParam) new GenericParamUser(genericParameter.Number, genericParameter.Flags, (UTF8String) "-"));
|
||
|
|
return methodDefUser;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static FieldDefUser Clone(FieldDef origin)
|
||
|
|
{
|
||
|
|
return new FieldDefUser(origin.Name, (FieldSig) null, origin.Attributes);
|
||
|
|
}
|
||
|
|
|
||
|
|
private static TypeDef PopulateContext(TypeDef typeDef, InjectHelper.InjectContext ctx)
|
||
|
|
{
|
||
|
|
IDnlibDef dnlibDef;
|
||
|
|
TypeDef typeDef1;
|
||
|
|
if (!ctx.Mep.TryGetValue((IDnlibDef) typeDef, out dnlibDef))
|
||
|
|
{
|
||
|
|
typeDef1 = (TypeDef) InjectHelper.Clone(typeDef);
|
||
|
|
ctx.Mep[(IDnlibDef) typeDef] = (IDnlibDef) typeDef1;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
typeDef1 = (TypeDef) dnlibDef;
|
||
|
|
foreach (TypeDef nestedType in (IEnumerable<TypeDef>) typeDef.NestedTypes)
|
||
|
|
typeDef1.NestedTypes.Add(InjectHelper.PopulateContext(nestedType, ctx));
|
||
|
|
foreach (MethodDef method in (IEnumerable<MethodDef>) typeDef.Methods)
|
||
|
|
typeDef1.Methods.Add((MethodDef) (ctx.Mep[(IDnlibDef) method] = (IDnlibDef) InjectHelper.Clone(method)));
|
||
|
|
foreach (FieldDef field in (IEnumerable<FieldDef>) typeDef.Fields)
|
||
|
|
typeDef1.Fields.Add((FieldDef) (ctx.Mep[(IDnlibDef) field] = (IDnlibDef) InjectHelper.Clone(field)));
|
||
|
|
return typeDef1;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void CopyTypeDef(TypeDef typeDef, InjectHelper.InjectContext ctx)
|
||
|
|
{
|
||
|
|
TypeDef typeDef1 = (TypeDef) ctx.Mep[(IDnlibDef) typeDef];
|
||
|
|
typeDef1.BaseType = ctx.Importer.Import(typeDef.BaseType);
|
||
|
|
foreach (InterfaceImpl interfaceImpl in (IEnumerable<InterfaceImpl>) typeDef.Interfaces)
|
||
|
|
typeDef1.Interfaces.Add((InterfaceImpl) new InterfaceImplUser(ctx.Importer.Import(interfaceImpl.Interface)));
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void CopyMethodDef(MethodDef methodDef, InjectHelper.InjectContext ctx)
|
||
|
|
{
|
||
|
|
MethodDef methodDef1 = (MethodDef) ctx.Mep[(IDnlibDef) methodDef];
|
||
|
|
methodDef1.Signature = ctx.Importer.Import(methodDef.Signature);
|
||
|
|
methodDef1.Parameters.UpdateParameterTypes();
|
||
|
|
if (methodDef.ImplMap != null)
|
||
|
|
methodDef1.ImplMap = (ImplMap) new ImplMapUser((ModuleRef) new ModuleRefUser(ctx.TargetModule, methodDef.ImplMap.Module.Name), methodDef.ImplMap.Name, methodDef.ImplMap.Attributes);
|
||
|
|
foreach (CustomAttribute customAttribute in (LazyList<CustomAttribute>) methodDef.CustomAttributes)
|
||
|
|
methodDef1.CustomAttributes.Add(new CustomAttribute((ICustomAttributeType) ctx.Importer.Import((IMethod) customAttribute.Constructor)));
|
||
|
|
if (!methodDef.HasBody)
|
||
|
|
return;
|
||
|
|
methodDef1.Body = new CilBody(methodDef.Body.InitLocals, (IList<Instruction>) new List<Instruction>(), (IList<ExceptionHandler>) new List<ExceptionHandler>(), (IList<Local>) new List<Local>())
|
||
|
|
{
|
||
|
|
MaxStack = methodDef.Body.MaxStack
|
||
|
|
};
|
||
|
|
Dictionary<object, object> bodyMap = new Dictionary<object, object>();
|
||
|
|
foreach (Local variable in methodDef.Body.Variables)
|
||
|
|
{
|
||
|
|
Local local = new Local(ctx.Importer.Import(variable.Type));
|
||
|
|
methodDef1.Body.Variables.Add(local);
|
||
|
|
local.Name = variable.Name;
|
||
|
|
local.Attributes = variable.Attributes;
|
||
|
|
bodyMap[(object) variable] = (object) local;
|
||
|
|
}
|
||
|
|
foreach (Instruction instruction1 in (IEnumerable<Instruction>) methodDef.Body.Instructions)
|
||
|
|
{
|
||
|
|
Instruction instruction2 = new Instruction(instruction1.OpCode, instruction1.Operand)
|
||
|
|
{
|
||
|
|
SequencePoint = instruction1.SequencePoint
|
||
|
|
};
|
||
|
|
if (instruction2.Operand is IType)
|
||
|
|
{
|
||
|
|
instruction2.Operand = (object) ctx.Importer.Import((IType)instruction2.Operand);
|
||
|
|
}
|
||
|
|
else if (instruction2.Operand is IMethod)
|
||
|
|
{
|
||
|
|
instruction2.Operand = (object) ctx.Importer.Import((IMethod)instruction2.Operand);
|
||
|
|
}
|
||
|
|
else if (instruction2.Operand is IField)
|
||
|
|
{
|
||
|
|
instruction2.Operand = (object) ctx.Importer.Import((IField)instruction2.Operand);
|
||
|
|
}
|
||
|
|
methodDef1.Body.Instructions.Add(instruction2);
|
||
|
|
bodyMap[(object) instruction1] = (object) instruction2;
|
||
|
|
}
|
||
|
|
foreach (Instruction instruction3 in (IEnumerable<Instruction>) methodDef1.Body.Instructions)
|
||
|
|
{
|
||
|
|
if (instruction3.Operand != null && bodyMap.ContainsKey(instruction3.Operand))
|
||
|
|
instruction3.Operand = bodyMap[instruction3.Operand];
|
||
|
|
else if (instruction3.Operand is Instruction[] operand)
|
||
|
|
{
|
||
|
|
Instruction instruction4 = instruction3;
|
||
|
|
Func<Instruction, Instruction> selector = (Func<Instruction, Instruction>) (target => (Instruction) bodyMap[(object) target]);
|
||
|
|
Instruction[] array = ((IEnumerable<Instruction>) operand).Select<Instruction, Instruction>(selector).ToArray<Instruction>();
|
||
|
|
instruction4.Operand = (object) array;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
foreach (ExceptionHandler exceptionHandler in (IEnumerable<ExceptionHandler>) methodDef.Body.ExceptionHandlers)
|
||
|
|
methodDef1.Body.ExceptionHandlers.Add(new ExceptionHandler(exceptionHandler.HandlerType)
|
||
|
|
{
|
||
|
|
CatchType = exceptionHandler.CatchType == null ? (ITypeDefOrRef) null : ctx.Importer.Import(exceptionHandler.CatchType),
|
||
|
|
TryStart = (Instruction) bodyMap[(object) exceptionHandler.TryStart],
|
||
|
|
TryEnd = (Instruction) bodyMap[(object) exceptionHandler.TryEnd],
|
||
|
|
HandlerStart = (Instruction) bodyMap[(object) exceptionHandler.HandlerStart],
|
||
|
|
HandlerEnd = (Instruction) bodyMap[(object) exceptionHandler.HandlerEnd],
|
||
|
|
FilterStart = exceptionHandler.FilterStart == null ? (Instruction) null : (Instruction) bodyMap[(object) exceptionHandler.FilterStart]
|
||
|
|
});
|
||
|
|
methodDef1.Body.SimplifyMacros((IList<Parameter>) methodDef1.Parameters);
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void CopyFieldDef(FieldDef fieldDef, InjectHelper.InjectContext ctx)
|
||
|
|
{
|
||
|
|
((FieldDef) ctx.Mep[(IDnlibDef) fieldDef]).Signature = ctx.Importer.Import(fieldDef.Signature);
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void Copy(TypeDef typeDef, InjectHelper.InjectContext ctx, bool copySelf)
|
||
|
|
{
|
||
|
|
if (copySelf)
|
||
|
|
InjectHelper.CopyTypeDef(typeDef, ctx);
|
||
|
|
foreach (TypeDef nestedType in (IEnumerable<TypeDef>) typeDef.NestedTypes)
|
||
|
|
InjectHelper.Copy(nestedType, ctx, true);
|
||
|
|
foreach (MethodDef method in (IEnumerable<MethodDef>) typeDef.Methods)
|
||
|
|
InjectHelper.CopyMethodDef(method, ctx);
|
||
|
|
foreach (FieldDef field in (IEnumerable<FieldDef>) typeDef.Fields)
|
||
|
|
InjectHelper.CopyFieldDef(field, ctx);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static TypeDef Inject(TypeDef typeDef, ModuleDef target)
|
||
|
|
{
|
||
|
|
InjectHelper.InjectContext ctx = new InjectHelper.InjectContext(target);
|
||
|
|
InjectHelper.PopulateContext(typeDef, ctx);
|
||
|
|
InjectHelper.Copy(typeDef, ctx, true);
|
||
|
|
return (TypeDef) ctx.Mep[(IDnlibDef) typeDef];
|
||
|
|
}
|
||
|
|
|
||
|
|
public static MethodDef Inject(MethodDef methodDef, ModuleDef target)
|
||
|
|
{
|
||
|
|
InjectHelper.InjectContext ctx = new InjectHelper.InjectContext(target);
|
||
|
|
ctx.Mep[(IDnlibDef) methodDef] = (IDnlibDef) InjectHelper.Clone(methodDef);
|
||
|
|
InjectHelper.CopyMethodDef(methodDef, ctx);
|
||
|
|
return (MethodDef) ctx.Mep[(IDnlibDef) methodDef];
|
||
|
|
}
|
||
|
|
|
||
|
|
public static IEnumerable<IDnlibDef> Inject(TypeDef typeDef, TypeDef newType, ModuleDef target)
|
||
|
|
{
|
||
|
|
InjectHelper.InjectContext ctx = new InjectHelper.InjectContext(target);
|
||
|
|
ctx.Mep[(IDnlibDef) typeDef] = (IDnlibDef) newType;
|
||
|
|
InjectHelper.PopulateContext(typeDef, ctx);
|
||
|
|
InjectHelper.Copy(typeDef, ctx, false);
|
||
|
|
return ctx.Mep.Values.Except<IDnlibDef>((IEnumerable<IDnlibDef>) new TypeDef[1]
|
||
|
|
{
|
||
|
|
newType
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
private class InjectContext : ImportMapper
|
||
|
|
{
|
||
|
|
public readonly Dictionary<IDnlibDef, IDnlibDef> Mep = new Dictionary<IDnlibDef, IDnlibDef>();
|
||
|
|
public readonly ModuleDef TargetModule;
|
||
|
|
|
||
|
|
public InjectContext(ModuleDef target)
|
||
|
|
{
|
||
|
|
this.TargetModule = target;
|
||
|
|
this.Importer = new Importer(target, ImporterOptions.TryToUseTypeDefs, new GenericParamContext(), (ImportMapper) this);
|
||
|
|
}
|
||
|
|
|
||
|
|
public Importer Importer { get; }
|
||
|
|
|
||
|
|
public override ITypeDefOrRef Map(ITypeDefOrRef typeDefOrRef)
|
||
|
|
{
|
||
|
|
return !(typeDefOrRef is TypeDef key) || !this.Mep.ContainsKey((IDnlibDef) key) ? (ITypeDefOrRef) null : (ITypeDefOrRef) this.Mep[(IDnlibDef) key];
|
||
|
|
}
|
||
|
|
|
||
|
|
public override IMethod Map(MethodDef methodDef)
|
||
|
|
{
|
||
|
|
return !this.Mep.ContainsKey((IDnlibDef) methodDef) ? (IMethod) null : (IMethod) this.Mep[(IDnlibDef) methodDef];
|
||
|
|
}
|
||
|
|
|
||
|
|
public override IField Map(FieldDef fieldDef)
|
||
|
|
{
|
||
|
|
return !this.Mep.ContainsKey((IDnlibDef) fieldDef) ? (IField) null : (IField) this.Mep[(IDnlibDef) fieldDef];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|