C
C#10mo ago
tolik518 🦀

✅ Reflection independent from .Net version and bitness?

I'm working on a tool (https://github.com/tolik518/JackTheEnumRipper) that dumps all the enums from a (.Net) binary and I figured out, pretty early in the developement, that the reflection on the assembly only works if the bitness and somewhat the .Net (framework) version corresponds to the binary. is there a way for me to detect the bitness and the needed framework early? Are there better ways then the way I'm currently doing it? In the first release I shipped 4 binaries, 2 of them are x64 and the other 2 are x86 - while one of them is always in* .Net 8.0 *and the other one is compiled with .Net Framework 4.8. But currently its a trial and error to find out which version one needs to run.
GitHub
GitHub - tolik518/JackTheEnumRipper: A CLI tool that extarcts Enums...
A CLI tool that extarcts Enums from an .exe/.dll which was written using C# - tolik518/JackTheEnumRipper
13 Replies
tolik518 🦀
tolik518 🦀OP10mo ago
My current idea would be to create a new solution with reads the pe header to determine the bitness and probably find out the needed .Net version and forward it to the correct executable. Otherwise I'm out of ideas
reflectronic
reflectronic10mo ago
yes, the problem is that you are loading the assembly. but you do not need to do that if you are just going to do reflection
reflectronic
reflectronic10mo ago
System.Reflection.MetadataLoadContext 8.0.0
Provides read-only reflection on assemblies in an isolated context with support for assemblies that target different processor architectures and runtimes. Using MetadataLoadContext enables you to inspect assemblies without loading them into the main execution context. Assemblies in MetadataLoadContext are treated only as metadata, that is, you c...
reflectronic
reflectronic10mo ago
the README on the package explains you would not need to change your code because it uses the same System.Type, System.Reflection.FieldInfo, etc. classes
tolik518 🦀
tolik518 🦀OP10mo ago
Looks very promising, I'll gotta read into it, thank you :)
reflectronic
reflectronic10mo ago
ok, well, actually there is at least one issue, so your code will have to change slightly Enum.GetValues will not work, because that returns instances of the enum type, which means the enum type would need to be loaded
Cali_West
Cali_West10mo ago
bitness? Like, endianness? what is bitness
reflectronic
reflectronic10mo ago
the architecture, presumably
Cali_West
Cali_West10mo ago
oh oh gotcha
reflectronic
reflectronic10mo ago
what you will need is something like
IEnumerable<(string Name, object Value)> GetEnumValues(Type type)
{
return type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static).Select(f => (f.Name, f.GetRawConstantValue());
}
IEnumerable<(string Name, object Value)> GetEnumValues(Type type)
{
return type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static).Select(f => (f.Name, f.GetRawConstantValue());
}
tolik518 🦀
tolik518 🦀OP10mo ago
Thanks for being so helpful and not only pushing me into the right direction but straight out giving me solutions to my problem :) I couldn't get up with a better term, but yeah, architecture makes sense
Cali_West
Cali_West10mo ago
yeah it's a bit late here too, I couldn't think of the name of it either hehe I think the info you're looking for is available, in some form, packed in the assembly metadata you'll have to just do some digging around. I get lost whenever I try and look through all that stuff
tolik518 🦀
tolik518 🦀OP10mo ago
Yeah but then I still would have to have multiple executable, I think I'll try the solution which @reflectronic proposed - this could solve all my issues at once I just did some quick test and it looks like I'll have to adjust some parts since it doesn't load all the dependencies like LoadFrom. Because I'm getting that exception when loading an assembly thats using xna
The specified assembly was not found.
Could not find assembly 'Microsoft.Xna.Framework.Game, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553'. Either explicitly load this assembly using a method such as LoadFromAssemblyPath() or use a MetadataAssemblyResolver that returns a valid assembly.
The specified assembly was not found.
Could not find assembly 'Microsoft.Xna.Framework.Game, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553'. Either explicitly load this assembly using a method such as LoadFromAssemblyPath() or use a MetadataAssemblyResolver that returns a valid assembly.
Okay I used Mono.Cecil and it worked like a charm!
Want results from more Discord servers?
Add your server