sunnamed434 / BitMono

Obfuscator for .NET and Mono, with a customizable engine for building your own obfuscators.
https://bitmono.readthedocs.io/en/latest/
MIT License
316 stars 26 forks source link

Input PE image does not contain a .NET metadata directory #136

Closed darkSuperman closed 1 year ago

darkSuperman commented 1 year ago

Hello, thank you for your contribution to this very meaningful project. I had a problem after using it. I checked the issue list and found no similar problem to mine. The following is the error log. There are a few Chinese characters in it. Please don’t worry about it.(My application is written on .net 5, and I choose not to rely on the external environment and simplify unused code when publishing)

][BitMono.CLI.Modules.CLIObfuscationNeedsFactory] File successfully specified: *
**\Demo.exe
[2023-07-25 09:08:48 INF][BitMono.CLI.Modules.CLIObfuscationNeedsFactory] Depend
encies (libs) directory was automatically found in: ***\libs!
[2023-07-25 09:08:48 INF][BitMono.CLI.Program] File: ***\Demo.exe
[2023-07-25 09:08:48 INF][BitMono.CLI.Program] Dependencies (libs): ***\libs
[2023-07-25 09:08:48 INF][BitMono.CLI.Program] Everything is seems to be ok, sta
rting obfuscation..
[2023-07-25 09:08:48 INF][BitMono.CLI.Program]
       ___  _ __  __  ___
      / _ )(_) /_/  |/  /__  ___  ___
     / _  / / __/ /|_/ / _ \/ _ \/ _ \
    /____/_/\__/_/  /_/\___/_//_/\___/
    https://github.com/sunnamed434/BitMono
    BitMono v0.16.2.0
Something went wrong! System.BadImageFormatException: Input PE image does not contain a .NET metadata directory.
   在 AsmResolver.DotNet.Serialized.SerializedModuleDefinition..ctor(IPEImage pe
Image, ModuleReaderParameters readerParameters)
   在 AsmResolver.DotNet.ModuleDefinition.FromBytes(Byte[] buffer, ModuleReaderP
arameters readerParameters)
   在 BitMono.Obfuscation.Modules.ModuleFactory.Create()
   在 BitMono.Obfuscation.Starter.BitMonoStarter.StartAsync(FinalFileInfo info,
IModuleFactory moduleFactory, IDataWriter dataWriter, IReferencesDataResolver re
ferencesDataResolver, CancellationToken cancellationToken)
   在 BitMono.Obfuscation.Starter.BitMonoStarter.StartAsync(IncompleteFileInfo i
nfo, CancellationToken cancellationToken)
   在 BitMono.CLI.Program.<Main>d__3.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   在 BitMono.CLI.Program.<Main>d__3.MoveNext()
Enter anything to exit!
sunnamed434 commented 1 year ago

Hey!

Thanks! Me and contributors doing our best!

As far as I know, .NET 5 compiling into the native code, but the .NET 5 application should have a .dll file with all of your managed code inside. That means in the root folder of the Demo.exe should be a Demo.dll - which contains the managed code, and you need to protect this instead of Demo.exe

darkSuperman commented 1 year ago

I didn't find a file similar to Demo.dll, is it related to the configuration in the VStudio publish? Deployment Mode: Self-contained | Produce single file: checked. And the Demo.exe file is also very large, it seems that the code is all on it.

sunnamed434 commented 1 year ago

I think yes its related to the 'self-contained' publish option, try to publish it with non self contained option and the dll should appear or try to uncheck the single file option

darkSuperman commented 1 year ago

Hi, I unchecked Produce single file, Demo.dll was indeed generated, except Demo.exe andDemo.dll I put all files under libs, but a new error appeared

***\Demo.dll
[2023-07-26 08:59:39 INF][BitMono.CLI.Modules.CLIObfuscationNeedsFactory] Dependencies (libs) directory was automatically found in: ***\libs!
[2023-07-26 08:59:39 INF][BitMono.CLI.Program] File: ***\Demo.dll
[2023-07-26 08:59:39 INF][BitMono.CLI.Program] Dependencies (libs): ***\libs
[2023-07-26 08:59:39 INF][BitMono.CLI.Program] Everything is seems to be ok, starting obfuscation..
[2023-07-26 08:59:39 INF][BitMono.CLI.Program]
       ___  _ __  __  ___
      / _ )(_) /_/  |/  /__  ___  ___
     / _  / / __/ /|_/ / _ \/ _ \/ _ \
    /____/_/\__/_/  /_/\___/_//_/\___/
    https://github.com/sunnamed434/BitMono
    BitMono v0.16.2.0
[2023-07-26 08:59:40 INF][BitMono.Obfuscation.BitMonoObfuscator] Module Demo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
[2023-07-26 08:59:40 INF][BitMono.Obfuscation.BitMonoObfuscator] Module Target Framework: .NETCoreApp
[2023-07-26 08:59:40 INF][BitMono.Obfuscation.BitMonoObfuscator] PE TimeDateStamp: 08/18/2071 20:05:34
[2023-07-26 08:59:40 INF][BitMono.Obfuscation.BitMonoObfuscator] Module culture: ["u", "n", "k", "n", "o", "w", "n"]
[2023-07-26 08:59:41 ERR][BitMono.Obfuscation.Starter.BitMonoStarter] Registered error!
System.BadImageFormatException: Underlying enum type Newtonsoft.Json.NullValueHandling could not be resolved. Assuming System.Int32 for custom attribu
te argument.
[2023-07-26 08:59:41 ERR][BitMono.Obfuscation.Starter.BitMonoStarter] Registered error!
System.BadImageFormatException: Underlying enum type Newtonsoft.Json.DefaultValueHandling could not be resolved. Assuming System.Int32 for custom attr
ibute argument.
[2023-07-26 08:59:41 ERR][BitMono.Obfuscation.Starter.BitMonoStarter] Registered error!
System.BadImageFormatException: Underlying enum type Newtonsoft.Json.NullValueHandling could not be resolved. Assuming System.Int32 for custom attribu
te argument.
[2023-07-26 08:59:41 ERR][BitMono.Obfuscation.Starter.BitMonoStarter] Registered error!
System.BadImageFormatException: Underlying enum type Newtonsoft.Json.DefaultValueHandling could not be resolved. Assuming System.Int32 for custom attr
ibute argument.
[2023-07-26 08:59:41 ERR][BitMono.Obfuscation.Starter.BitMonoStarter] Registered error!
System.BadImageFormatException: Underlying enum type Newtonsoft.Json.NullValueHandling could not be resolved. Assuming System.Int32 for custom attribu
te argument.
[2023-07-26 08:59:41 ERR][BitMono.Obfuscation.Starter.BitMonoStarter] Registered error!
System.BadImageFormatException: Underlying enum type Newtonsoft.Json.DefaultValueHandling could not be resolved. Assuming System.Int32 for custom attr
ibute argument.
[2023-07-26 08:59:41 ERR][BitMono.Obfuscation.Starter.BitMonoStarter] Registered error!
System.BadImageFormatException: Underlying enum type Newtonsoft.Json.NullValueHandling could not be resolved. Assuming System.Int32 for custom attribu
te argument.
[2023-07-26 08:59:41 ERR][BitMono.Obfuscation.Starter.BitMonoStarter] Registered error!
System.BadImageFormatException: Underlying enum type Newtonsoft.Json.DefaultValueHandling could not be resolved. Assuming System.Int32 for custom attr
ibute argument.
[2023-07-26 08:59:41 ERR][BitMono.Obfuscation.Starter.BitMonoStarter] Registered error!
System.BadImageFormatException: Underlying enum type Newtonsoft.Json.NullValueHandling could not be resolved. Assuming System.Int32 for custom attribu
te argument.
[2023-07-26 08:59:41 ERR][BitMono.Obfuscation.Starter.BitMonoStarter] Registered error!
System.BadImageFormatException: Underlying enum type Newtonsoft.Json.DefaultValueHandling could not be resolved. Assuming System.Int32 for custom attr
ibute argument.
[2023-07-26 08:59:41 ERR][BitMono.Obfuscation.Starter.BitMonoStarter] Registered error!
System.BadImageFormatException: Underlying enum type Newtonsoft.Json.NullValueHandling could not be resolved. Assuming System.Int32 for custom attribu
te argument.
[2023-07-26 08:59:41 ERR][BitMono.Obfuscation.Starter.BitMonoStarter] Registered error!
System.BadImageFormatException: Underlying enum type Newtonsoft.Json.DefaultValueHandling could not be resolved. Assuming System.Int32 for custom attr
ibute argument.
[2023-07-26 08:59:41 FTL][BitMono.Obfuscation.BitMonoObfuscator] No one protection were detected, please specify or enable them in protections.json!
[2023-07-26 08:59:41 FTL][BitMono.Obfuscation.BitMonoObfuscator] Obfuscation stopped! Something went wrong!
Enter anything to exit!
sunnamed434 commented 1 year ago

Hey, can you please send the Demo.dll to my email: sunnamed434@proton.me

sunnamed434 commented 1 year ago

I guess the problem is more that the BitMono TargetFramework is .NET Framework 461, it would be great to add support for the .NET 5/6, etc

darkSuperman commented 1 year ago

I have sent you an email, please check

sunnamed434 commented 1 year ago

Will check tommorow

sunnamed434 commented 1 year ago

You can ignore those exceptions, the problem can be caused due to BitMono using .NET Framework. I specified a few protections and the file is obfuscated successfully that you provided in my email, to make it work simply configure protections.json

The solution is to add a .NET 6/5 to the bitmono target frameworks, I'll figure it out when I'll have some free time, or someone can make a PR, and temp solution is just ignore it

darkSuperman commented 1 year ago

Thank you, to be honest I'm not good at windows application development, I'm only good at Android and Rust, so sorry I can't make a PR :)

And how to use the protections.json, is it placed in the Demo project file or as a command line parameter? What does the name field in protections.json refer to? Sorry I read the docs but still not quite clear using

sunnamed434 commented 1 year ago

Name is the name of the protection Enabled is a state of protection (enabled or not)

Here's my best preset for obfuscation, but, since your app is forms, don't use my preset because it works only on Mono runtime, and your application won't run without specific runtime (i.e Mono)

{
  "Protections": [
    {
      "Name": "AntiILdasm",
      "Enabled": false
    },
    {
      "Name": "AntiDe4dot",
      "Enabled": false
    },
    {
      "Name": "ObjectReturnType",
      "Enabled": false
    },
    {
      "Name": "NoNamespaces",
      "Enabled": false
    },
    {
      "Name": "FullRenamer",
      "Enabled": false
    },
    {
      "Name": "AntiDebugBreakpoints",
      "Enabled": false
    },
    {
      "Name": "StringsEncryption",
      "Enabled": false
    },
    {
      "Name": "UnmanagedString",
      "Enabled": false
    },
    {
      "Name": "DotNetHook",
      "Enabled": false
    },
    {
      "Name": "CallToCalli",
      "Enabled": false
    },
    {
      "Name": "AntiDecompiler",
      "Enabled": true
    },
    {
      "Name": "BitMethodDotnet",
      "Enabled": true
    },
    {
      "Name": "BitTimeDateStamp",
      "Enabled": true
    },
    {
      "Name": "BitDotNet",
      "Enabled": true
    },
    {
      "Name": "BitMono",
      "Enabled": true
    }
  ]
}

I recommend using this preset for you: DotNetHook , CallToCalli, BitTimeDateStamp, and UnmanagedString

darkSuperman commented 1 year ago

Where should the protections.json file be placed?

sunnamed434 commented 1 year ago

Here: Release\net462\protections.json

darkSuperman commented 1 year ago

Thanks, I already figured it out. Does this issues need to be closed?

sunnamed434 commented 1 year ago

For now yes, I'll create another issue