Skip to content

Commit

Permalink
improvements, fix, Indexes are now called Indices (sry for bad englis…
Browse files Browse the repository at this point in the history
…h :P) and added scripting engine
  • Loading branch information
ion committed Dec 31, 2016
1 parent 1e9b128 commit 6e4b9e4
Show file tree
Hide file tree
Showing 16 changed files with 470 additions and 69 deletions.
12 changes: 6 additions & 6 deletions Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static void Main(string[] args)
Instruction.Create(OpCodes.Ldstr, "Place easter egg here 1"),
Instruction.Create(OpCodes.Ldstr, "Place easter egg here 2")
};
int[] indexes = {
int[] Indices = {
4,
8
};
Expand All @@ -51,7 +51,7 @@ static void Main(string[] args)
Class = "Program",
Method = "PrintAlot",
Instructions = opCodesManipulateOffset,
Indexes = indexes
Indices = Indices
};
p.Patch(target);
p.Save("Test2.exe");
Expand Down Expand Up @@ -137,15 +137,15 @@ static void Main(string[] args)


/*
* Removes the instrutions at the given indexes
* Removes the instrutions at the given Indices
*/
p = new Patcher("Test.exe");
target = new Target()
{
Namespace = "Test",
Class = "Program",
Method = "RemoveMe",
Indexes = new[]{0,1}
Indices = new[]{0,1}
};
p.RemoveInstruction(target);
p.Save("Test7.exe");
Expand Down Expand Up @@ -211,7 +211,7 @@ static void Main(string[] args)


/*
* Tries to find indexes in a obfuscated assembly by string operands
* Tries to find Indices in a obfuscated assembly by string operands
*/
var op = new Patcher("TestObfuscated.exe", true);
string[] operands = {
Expand Down Expand Up @@ -266,7 +266,7 @@ static void Main(string[] args)
Instruction.Create(OpCodes.Call, op.BuildMemberRef("System", "Console", "WriteLine", Patcher.MemberRefType.Static)),
Instruction.Create(OpCodes.Ret)
};
obfTarget.Indexes = null; // Replace whole body
obfTarget.Indices = null; // Replace whole body
}
op.Patch(obfuscatedTargets);
op.Save("TestObfuscated2.exe");
Expand Down
Binary file added Libraries/Newtonsoft.Json.dll
Binary file not shown.
6 changes: 6 additions & 0 deletions TestScript/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
19 changes: 19 additions & 0 deletions TestScript/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using dnpatch.script;

namespace TestScript
{
class Program
{
static void Main(string[] args)
{
Script script = new Script("script.json");
script.Patch();
script.Save("scripted.exe");
}
}
}
36 changes: 36 additions & 0 deletions TestScript/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("TestScript")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TestScript")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("1c0fa309-87cb-48d3-8c0e-be2155b05e86")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
66 changes: 66 additions & 0 deletions TestScript/TestScript.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{1C0FA309-87CB-48D3-8C0E-BE2155B05E86}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TestScript</RootNamespace>
<AssemblyName>TestScript</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\dnpatch.script\dnpatch.script.csproj">
<Project>{34aac8d2-ec71-43a4-b107-3085f07d6b35}</Project>
<Name>dnpatch.script</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
4 changes: 2 additions & 2 deletions UnpackMe1/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static void Main(string[] args)
Class = "Form1",
Namespace = "Confuser_1._7",
Method = "Form1_Load",
Indexes = new []
Indices = new []
{
0,
1,
Expand All @@ -36,7 +36,7 @@ static void Main(string[] args)
}
};
patcher.RemoveInstruction(target);
target.Indexes = null;
target.Indices = null;
target.Index = 2;
patcher.PatchOperand(target, "Patched!");
patcher.Save("UnpackMe_Confuser 1.7.deob.patch.exe");
Expand Down
36 changes: 36 additions & 0 deletions dnpatch.script/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("dnpatch.script")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("dnpatch.script")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("34aac8d2-ec71-43a4-b107-3085f07d6b35")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
136 changes: 136 additions & 0 deletions dnpatch.script/Script.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using dnlib.DotNet.Emit;
using Newtonsoft.Json.Linq;

namespace dnpatch.script
{
public class Script
{
private string _scriptFile; // Filepath
private JObject _script; // Script
private Patcher _patcher; // Patcher
private readonly Dictionary<string, Target> _targets = new Dictionary<string, Target>(); // Target
private string _optional; // Optional arguments

public Script(string path)
{
_scriptFile = path;
_script = JObject.Parse(File.ReadAllText(path));
BuildTarget();
_patcher = new Patcher(_script.GetValue("target").ToString());
}

public void Patch()
{
foreach (var target in _targets)
{
if (target.Key == "empty")
_patcher.WriteEmptyBody(target.Value);
else if (target.Key == "return")
_patcher.WriteReturnBody(target.Value, Convert.ToBoolean(_optional));
else if (target.Key == "replace")
_patcher.ReplaceInstruction(target.Value);
}
}

public void Save(bool backup)
{
_patcher.Save(backup);
}

public void Save(string name)
{
_patcher.Save(name);
}

public void LoadScript(string path)
{
_scriptFile = path;
_script = JObject.Parse(path);
BuildTarget();
_patcher = new Patcher(_script.GetValue("target").ToString());
}

private void BuildTarget()
{
JArray targets = (JArray) _script.GetValue("targets");
foreach (var t in targets)
{
Target target = new Target
{
Namespace = t["ns"].ToString(),
Class = t["cl"].ToString(),
Method = t["me"].ToString()
};
if (t["index"] != null)
target.Index = Convert.ToInt32(t["index"]);
if (t["indices"] != null)
target.Indices = null; // to int[]
if (t["optional"] != null)
_optional = t["optional"].ToString();
if (t["instructions"] != null)
{
JArray instructions = (JArray) t["instructions"];
if (instructions.Count == 1)
{
if (instructions[0].First != null && instructions[0].First.ToString() != "")
{
var operand = instructions[0].Last.Last;
if (operand.Type == JTokenType.Integer)
{
target.Instruction =
Instruction.Create(
(OpCode)
GetInstructionField(instructions[0].First.First.ToString()).GetValue(this),
operand.Value<int>());
}
else if (operand.Type == JTokenType.String)
{
target.Instruction =
Instruction.Create(
(OpCode)
GetInstructionField(instructions[0].First.First.ToString()).GetValue(this),
operand.Value<string>());
}
}
else
{
target.Instruction =
Instruction.Create(
(OpCode) GetInstructionField(instructions[0].First.First.ToString()).GetValue(this));
}
}
else
{
foreach (var instruction in instructions)
{
if (instruction.First != null && instruction.First.Value<string>() != "")
target.Instruction =
Instruction.Create((OpCode)GetInstructionField(instruction.First.ToString()).GetValue(this),
instruction.Last.Last.Value<dynamic>());
else
target.Instruction =
Instruction.Create(
(OpCode)GetInstructionField(instruction.First.First.ToString()).GetValue(this));
}
}
}
_targets.Add(t["ac"].ToString(), target);
}
}

private FieldInfo GetInstructionField(string name)
{
var type = typeof(OpCodes);
var field = type.GetField(name, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Static);
return field;
}
}
}
12 changes: 12 additions & 0 deletions dnpatch.script/ScriptConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace dnpatch.script
{
internal class ScriptConfig
{
}
}
Loading

0 comments on commit 6e4b9e4

Please sign in to comment.