Oct 29, 2013 01:37 PM
(This post was last modified: Oct 29, 2013 01:50 PM by Delirium™.)
Heya C# folks.
I wrote this snippet to help people who are interested in Mono.Cecil or generally in C# learn how to add resources to a compiled binary or generally an existing .NET executable.
Of course, in order to use the following code, you need to have referenced Mono.Cecil.dll in your project and add using Mono.Cecil; on top of it.
The code is the following:
Add the following statements on top of your source file:
Then within a class in the same source file, add the following:
Example Usage:
Deleting the generated resource file should be done in case you don't want it anymore or you're using the above code to inject resources on a crypter's (or generally any) stub. Also note, that I wrote an overload method which supports adding multiple resources! Using it should be easy, just follow the example above and instead of a byte[] parameter, pass a List<byte[]> parameter to it.
Enjoy.
I wrote this snippet to help people who are interested in Mono.Cecil or generally in C# learn how to add resources to a compiled binary or generally an existing .NET executable.
Of course, in order to use the following code, you need to have referenced Mono.Cecil.dll in your project and add using Mono.Cecil; on top of it.
The code is the following:
Add the following statements on top of your source file:
Code
using Mono.Cecil;
using System.Resources;Then within a class in the same source file, add the following:
Code
public static string CreateResourceFile(byte[] FileData)
{
string ResPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "res.resources");
using (ResourceWriter Writer = new ResourceWriter(ResPath))
{
Writer.AddResource("Res", FileData);
Writer.Generate();
}
return ResPath;
}
public static string CreateResourceFile(List<byte[]> Files)
{
string ResPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "res.resources");
using (ResourceWriter Writer = new ResourceWriter(ResPath))
{
int index = 0;
foreach (byte[] ByteArray in Files)
{
Writer.AddResource(string.Format("Res{0}", index), ByteArray);
}
Writer.Generate();
}
return ResPath;
}
public static void InjectResourceFile(string TargetPE, string ResName, string ResourceFile)
{
AssemblyDefinition ASMDef = AssemblyDefinition.ReadAssembly(TargetPE);
EmbeddedResource EmbeddedRes = new EmbeddedResource(ResName + ".resources",
ManifestResourceAttributes.Public, File.ReadAllBytes(ResourceFile));
ASMDef.MainModule.Resources.Add(EmbeddedRes);
ASMDef.Write(TargetPE);
}Example Usage:
Code
string ResourceFile = CreateResourceFile(File.ReadAllBytes("C:/Users/Coder/Desktop/ImageExample.jpg"));
InjectResourceFile("C:/Users/Coder/Desktop/TargetDotNetExe.exe", "Image", ResourceFile);
File.Delete(ResourceFile);Deleting the generated resource file should be done in case you don't want it anymore or you're using the above code to inject resources on a crypter's (or generally any) stub. Also note, that I wrote an overload method which supports adding multiple resources! Using it should be easy, just follow the example above and instead of a byte[] parameter, pass a List<byte[]> parameter to it.
Enjoy.

