There's a .NET malware sample I'm trying to analyze, which goes more or less like this:
internal static class Class1
{
public static byte[] Code = new byte[]
{
9,
249,
131,
225,
...,
}
private static void Main()
{
// AFAIU this copies the marshaled code in Code to freshly allocated memory
IntPtr ptr = Class1.Alloc((uint)Class1.Code.Length);
for (int i = 0; i < Class1.Code.Length; i++)
{
int num = (int)Marshal.ReadByte(Class1.Code, i);
Marshal.WriteByte(ptr, i, (byte)num);
}
Class1.newObject newObject = (Class1.newObject)Marshal.GetDelegateForFunctionPointer(ptr, typeof(Class1.newObject));
object.Equals(null, null);
[...snip...]
object.Equals(null, null); // probably some timing mechanism
Marshal.GetFunctionPointerForDelegate(newObject);
newObject();
}
}
I'm trying to get to the code behind newObject(), which is apparently instantiated from marshaled code. I'm no .Net expert, but from what I read marshaled code is some kind of serialization that can be applied on objects to transfer functional code over binary channels (such as a TCP connection, for example). From what I understand, I should be able to reverse it to an understandable, or at least bytecode-like format.
I tried software like ILSpy and dotPeek but they don't seem to recognize the code in Code
as being managed code, and provide no usable output.