Skip to content

Commit

Permalink
Renaming BuildMemberRef to BuildCall and fix it
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Marcelli committed Jul 6, 2017
1 parent ab82d53 commit efd3dbd
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 39 deletions.
10 changes: 7 additions & 3 deletions Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ static void Main(string[] args)
Patcher p = new Patcher("Test.exe");
Instruction[] opcodesConsoleWriteLine = {
Instruction.Create(OpCodes.Ldstr, "Hello Sir"), // String to print
Instruction.Create(OpCodes.Call, p.BuildMemberRef("System", "Console", "WriteLine", Patcher.MemberRefType.Static)), // Console.WriteLine call
Instruction.Create(OpCodes.Ret) // Alaway return smth
Instruction.Create(OpCodes.Call, p.BuildCall(typeof(Console), "WriteLine", typeof(void), new[] { typeof(string) })), // Console.WriteLine call
Instruction.Create(OpCodes.Ret) // Always return smth
};
foreach (var memberRef in p.GetModule().GetMemberRefs())
{
Console.WriteLine(memberRef.Name);
}
Target target = new Target()
{
Namespace = "Test",
Expand Down Expand Up @@ -313,7 +317,7 @@ static void Main(string[] args)
obfTarget.Instructions = new Instruction[]
{
Instruction.Create(OpCodes.Ldstr, "Obfuscators cant beat my library :P"),
Instruction.Create(OpCodes.Call, op.BuildMemberRef("System", "Console", "WriteLine", Patcher.MemberRefType.Static)),
Instruction.Create(OpCodes.Call, op.BuildCall(typeof(Console), "WriteLine", typeof(void), new[]{typeof(string)})),
Instruction.Create(OpCodes.Ret)
};
obfTarget.Indices = null; // Replace whole body
Expand Down
2 changes: 1 addition & 1 deletion Test/Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<OutputPath>..\Example\bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
Expand Down
2 changes: 1 addition & 1 deletion TestObfuscated/TestObfuscated.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<OutputPath>..\ExampleDeobfuscation\bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
Expand Down
66 changes: 34 additions & 32 deletions dnpatch/PatchHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,38 @@ namespace dnpatch
{
internal class PatchHelper
{
private readonly ModuleDef _module;
public readonly ModuleDef Module;
private readonly string _file;
private readonly bool _keepOldMaxStack = false;

public PatchHelper(string file)
{
_file = file;
_module = ModuleDefMD.Load(file);
Module = ModuleDefMD.Load(file);
}

public PatchHelper(string file, bool keepOldMaxStack)
{
_file = file;
_module = ModuleDefMD.Load(file);
Module = ModuleDefMD.Load(file);
_keepOldMaxStack = keepOldMaxStack;
}

public PatchHelper(ModuleDefMD module, bool keepOldMaxStack)
{
_module = module;
Module = module;
_keepOldMaxStack = keepOldMaxStack;
}

public PatchHelper(ModuleDef module, bool keepOldMaxStack)
{
_module = module;
Module = module;
_keepOldMaxStack = keepOldMaxStack;
}

public PatchHelper(Stream stream, bool keepOldMaxStack)
{
_module = ModuleDefMD.Load(stream);
Module = ModuleDefMD.Load(stream);
_keepOldMaxStack = keepOldMaxStack;
}

Expand Down Expand Up @@ -124,9 +124,9 @@ public TypeDef FindType(string classPath, string[] nestedClasses)
{
if (classPath.First() == '.')
classPath = classPath.Remove(0, 1);
foreach (var module in _module.Assembly.Modules)
foreach (var module in Module.Assembly.Modules)
{
foreach (var type in _module.Types)
foreach (var type in Module.Types)
{
if (type.FullName == classPath)
{
Expand Down Expand Up @@ -207,12 +207,12 @@ public Target FixTarget(Target target)
public void Save(string name)
{
if (_keepOldMaxStack)
_module.Write(name, new ModuleWriterOptions(_module)
Module.Write(name, new ModuleWriterOptions(Module)
{
MetaDataOptions = {Flags = MetaDataFlags.KeepOldMaxStack}
});
else
_module.Write(name);
Module.Write(name);
}

public void Save(bool backup)
Expand All @@ -222,13 +222,13 @@ public void Save(bool backup)
throw new Exception("Assembly/module was loaded in memory, and no file was specified. Use Save(string) method to save the patched assembly.");
}
if (_keepOldMaxStack)
_module.Write(_file + ".tmp", new ModuleWriterOptions(_module)
Module.Write(_file + ".tmp", new ModuleWriterOptions(Module)
{
MetaDataOptions = { Flags = MetaDataFlags.KeepOldMaxStack }
});
else
_module.Write(_file + ".tmp");
_module.Dispose();
Module.Write(_file + ".tmp");
Module.Dispose();
if (backup)
{
if (File.Exists(_file + ".bak"))
Expand All @@ -248,7 +248,7 @@ public Target[] FindInstructionsByOperand(string[] operand)
{
List<ObfuscatedTarget> obfuscatedTargets = new List<ObfuscatedTarget>();
List<string> operands = operand.ToList();
foreach (var type in _module.Types)
foreach (var type in Module.Types)
{
if (!type.HasNestedTypes)
{
Expand Down Expand Up @@ -358,7 +358,7 @@ public Target[] FindInstructionsByOperand(int[] operand)
{
List<ObfuscatedTarget> obfuscatedTargets = new List<ObfuscatedTarget>();
List<int> operands = operand.ToList();
foreach (var type in _module.Types)
foreach (var type in Module.Types)
{
if (!type.HasNestedTypes)
{
Expand Down Expand Up @@ -468,7 +468,7 @@ public Target[] FindInstructionsByOpcode(OpCode[] opcode)
{
List<ObfuscatedTarget> obfuscatedTargets = new List<ObfuscatedTarget>();
List<string> operands = opcode.Select(o => o.Name).ToList();
foreach (var type in _module.Types)
foreach (var type in Module.Types)
{
if (!type.HasNestedTypes)
{
Expand Down Expand Up @@ -673,7 +673,7 @@ public Target[] FindMethodsByOpCodeSignature(OpCode[] signature)
{
HashSet<MethodDef> found = new HashSet<MethodDef>();

foreach (TypeDef td in _module.Types)
foreach (TypeDef td in Module.Types)
{
foreach (MethodDef md in td.Methods)
{
Expand Down Expand Up @@ -830,21 +830,23 @@ public Target[] FindInstructionsByRegex(Target target, string pattern, bool igno
return targets.ToArray();
}

public MemberRef BuildMemberRef(string ns, string cs, string name, Patcher.MemberRefType type)
private bool CheckParametersByType(ParameterInfo[] parameters, Type[] types)
{
TypeRef consoleRef = new TypeRefUser(_module, ns, cs, _module.CorLibTypes.AssemblyRef);
if (type == Patcher.MemberRefType.Static)
{
return new MemberRefUser(_module, name,
MethodSig.CreateStatic(_module.CorLibTypes.Void, _module.CorLibTypes.String),
consoleRef);
}
else
return !parameters.Where((t, i) => types[i] != t.ParameterType).Any();
}

public IMethod BuildCall(Type type, string method, Type returnType, Type[] parameters)
{
Importer importer = new Importer(Module);
foreach (var m in type.GetMethods())
{
return new MemberRefUser(_module, name,
MethodSig.CreateInstance(_module.CorLibTypes.Void, _module.CorLibTypes.String),
consoleRef);
if (m.Name == method && m.ReturnType == returnType && m.GetParameters().Length == parameters.Length && CheckParametersByType(m.GetParameters(), parameters))
{
IMethod meth = importer.Import(m);
return meth;
}
}
return null;
}

public void ReplaceInstruction(Target target)
Expand Down Expand Up @@ -1152,9 +1154,9 @@ public Target GetEntryPoint()
{
return new Target()
{
Namespace = _module.EntryPoint.DeclaringType.Namespace,
Class = _module.EntryPoint.DeclaringType.Name,
Method = _module.EntryPoint.Name
Namespace = Module.EntryPoint.DeclaringType.Namespace,
Class = Module.EntryPoint.DeclaringType.Name,
Method = Module.EntryPoint.Name
};
}
}
Expand Down
10 changes: 8 additions & 2 deletions dnpatch/Patcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using dnlib.DotNet;
using dnlib.DotNet.Emit;
using dnlib.DotNet.Writer;
using CallingConvention = dnlib.DotNet.CallingConvention;

namespace dnpatch
{
Expand Down Expand Up @@ -44,6 +45,11 @@ public Patcher(Stream stream, bool keepOldMaxStack)
_patcher = new PatchHelper(stream, keepOldMaxStack);
}

public ModuleDef GetModule()
{
return _patcher.Module;
}

public void Patch(Target target)
{
if ((target.Indices != null || target.Index != -1) &&
Expand Down Expand Up @@ -219,9 +225,9 @@ public int GetLdcI4Operand(Target target)
return _patcher.GetLdcI4Operand(target);
}

public MemberRef BuildMemberRef(string ns, string cs, string name, MemberRefType type)
public IMethod BuildCall(Type type, string method, Type returnType, Type[] parameters)
{
return _patcher.BuildMemberRef(ns, cs, name, type);
return _patcher.BuildCall(type, method, returnType, parameters);
}

public void RewriteProperty(Target target)
Expand Down

0 comments on commit efd3dbd

Please sign in to comment.