How to load and assemble in memory in C # from a binary array as a resource

What I am trying to achieve is to secure a client-developed video game (with Unity3D) being executed with a license that expires after a certain amount of time. The thing is, my team no longer has access to the executable, so recompiling the game executable is out of the question. This is why I decided to embed the game executable as a resource in a wrapper program that first validates the client's license (via an online check - I used System.Net and System.Net.Http to achieve this). After the license server returns an "OK" response to my board game shell, I would like to run the game executable in memory without writing it to disk.

Here is some sample code to load my executable from a resource, but it gives me a "badimageformatexception" at runtime:

Stream stream = GetType().Assembly.GetManifestResourceStream("LicenseServerClient.testBinary.exe");
byte[] bytes = new byte[(int)stream.Length];
stream.Read(bytes, 0, bytes.Length);

Assembly assembly = Assembly.Load(bytes);//Assembly.Load(bytes);// The debbuger stops here, throwing a "badimageformatexception" 

assembly.EntryPoint.Invoke(null, new object[0]);

      

Now, if you think there is a better way to achieve the entire licensing architecture under these constraints, please feel free to comment and give feedback.

+3


source to share


1 answer


Can you use the Unity3d executable as a reference in your project? First, trying to do this will answer the question of whether the binary is even a valid assembly as far as .NET is concerned. Based on the error message, I doubt this is the case, and nothing you can do will change this (Unity is based on Mono, but as I understand it, standalone Unity programs are not driven by code assemblies).

If you can reference it from your own project, you should be able to load the in-memory assembly from the resource. Hence my skepticism about the possible.

Given the perceived incompatibility, the best approach is probably to write the binary back to disk (i.e. to the temp file) and then just execute it as an external process. Hosting binary code without managed code inside your managed process would probably not be feasible.



Of course, this will make it easier for people to get around your wrapper. If they are examining running processes, it shouldn't be difficult to trace the gameplay to the file you wrote, and they can just make a copy of the file at that point.

But then your current intended approach isn't much harder to hack. It won't be difficult for someone with only a little more computational skills to use the right challenge-seeking tool Assembly.Load()

, find the resource from which you launch the game, and then retrieve that resource as a standalone game without a wrapper.

Bottom line: Without modifying the game code itself, if you intend to execute the game code on the client machine, you cannot programmatically use your license. You can slow down the most naive users, but that's about it. (Of course, this holds true for even the most sophisticated copy protection with relatively little difference, so I think you could consider any license enforcement scheme that has any effect at all, "good enough" :)).

+1


source







All Articles