Skip to content

Commit

Permalink
Merge pull request #168 from sunnamed434/fix/module-changes-to-anothe…
Browse files Browse the repository at this point in the history
…r-bits

Potential fix for #166
  • Loading branch information
sunnamed434 committed Apr 25, 2024
2 parents c78f80c + 460814e commit 5e6f5c9
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 19 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Read the **[docs][bitmono_docs]** to read protection, functionality, and more.

### Pre-Require

Set one of setting from `protections.json` to `true`
Set one of setting from `protections.json` to `true`.

### Using CLI

Expand Down Expand Up @@ -141,9 +141,8 @@ $ BitMono.CLI -f C:\specially_created_folder_for_obfuscation/your_app.exe -l C:\
Want more? Simply read the **[docs][bitmono_docs]**.

### Troubleshooting
Access Denied:

Try to set `OpenFileDestinationInFileExplorer` to `false` in `obfuscation.json`
Having issues? Get more help **[here][troubleshooting]**.

### Detailed build status

Expand All @@ -156,7 +155,7 @@ If you want to build the BitMono by your own - [click here for detailed info][bu

### Supported Frameworks

Mono is supported obviously (some protections don't support Mono), however if you use BitMono for .NET (Core) or higher versions be careful because some protections won't work - you will be notified about that by BitMono when using not supported protections for yours running target framework.
Feel free to use BitMono on frameworks which described below. Be careful using some protections because some might work on .NET Framework only, some on .NET (Core) only, some on all frameworks, some on Mono only - if the protection is unique to its platform/framework you will get a notification about that.

| Framework | Version |
|----------------|---------|
Expand Down Expand Up @@ -218,6 +217,7 @@ Credits
[appveyor_dev_build]: https://ci.appveyor.com/project/sunnamed434/bitmono/branch/dev
[bitmono_discord]: https://discord.gg/sFDHd47St4

[troubleshooting]: https://github.com/sunnamed434/BitMono/blob/main/troubleshooting.md
[build_info]: https://github.com/sunnamed434/BitMono/blob/main/build.md
[image_build]: https://ci.appveyor.com/api/projects/status/8jh35hfno6riq25j?svg=true&style=plastic
[image_test]: https://img.shields.io/appveyor/tests/sunnamed434/bitmono/main
Expand Down
3 changes: 3 additions & 0 deletions src/BitMono.Host/obfuscation.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
// Opens directory with protected file in file explorer in top of the screen, if set to true
"OpenFileDestinationInFileExplorer": true,

// Allow changes to next properties like: bits, machine type to the module (sometimes it cause issues so disabling it may help)
"AllowPotentialBreakingChangesToModule": true,

// Set to true to enable, in cases when you want to obfuscate only the specific namespace(s)
"SpecificNamespacesObfuscationOnly": false,
"SpecificNamespaces": [
Expand Down
33 changes: 20 additions & 13 deletions src/BitMono.Obfuscation/Modules/ModuleFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
public class ModuleFactory : IModuleFactory
{
private readonly byte[] _bytes;
private readonly ObfuscationSettings _obfuscationSettings;
private readonly IErrorListener _errorListener;
private readonly MetadataBuilderFlags _metadataBuilderFlags;

public ModuleFactory(byte[] bytes, IErrorListener errorListener,
public ModuleFactory(byte[] bytes, ObfuscationSettings obfuscationSettings, IErrorListener errorListener,
MetadataBuilderFlags metadataBuilderFlags = MetadataBuilderFlags.None)
{
_bytes = bytes;
_obfuscationSettings = obfuscationSettings;
_errorListener = errorListener;
_metadataBuilderFlags = metadataBuilderFlags;
}
Expand All @@ -21,20 +23,25 @@ public ModuleFactoryResult Create()
PEReaderParameters = new PEReaderParameters(_errorListener)
};
var module = ModuleDefinition.FromBytes(_bytes, moduleReaderParameters);
module.Attributes &= ~DotNetDirectoryFlags.ILOnly;
var x86 = module.MachineType == MachineType.I386;
if (x86)
if (_obfuscationSettings.AllowPotentialBreakingChangesToModule)
{
module.PEKind = OptionalHeaderMagic.PE32;
module.MachineType = MachineType.I386;
module.Attributes |= DotNetDirectoryFlags.Bit32Required;
module.Attributes &= ~DotNetDirectoryFlags.ILOnly;
var x86 = module.MachineType == MachineType.I386;
if (x86)
{
module.PEKind = OptionalHeaderMagic.PE32;
module.MachineType = MachineType.I386;
module.Attributes |= DotNetDirectoryFlags.Bit32Required;
}
else
{
module.PEKind = OptionalHeaderMagic.PE32Plus;
module.MachineType = MachineType.Amd64;
}
}
else
{
module.PEKind = OptionalHeaderMagic.PE32Plus;
module.MachineType = MachineType.Amd64;
}
var managedPEImageBuilder = new ManagedPEImageBuilder(new DotNetDirectoryFactory(_metadataBuilderFlags), _errorListener);

var managedPEImageBuilder =
new ManagedPEImageBuilder(new DotNetDirectoryFactory(_metadataBuilderFlags), _errorListener);

return new ModuleFactoryResult
{
Expand Down
4 changes: 2 additions & 2 deletions src/BitMono.Obfuscation/Starter/BitMonoStarter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ private async Task<bool> StartAsync(StarterContext context, IDataWriter dataWrit
public Task<bool> StartAsync(CompleteFileInfo info, CancellationToken cancellationToken)
{
return StartAsync(new FinalFileInfo(info.FileName, info.OutputDirectoryPath),
new ModuleFactory(info.FileData, new LogErrorListener(_logger, _obfuscationSettings)),
new ModuleFactory(info.FileData, _obfuscationSettings, new LogErrorListener(_logger, _obfuscationSettings)),
new FileDataWriter(), new AutomaticReferencesDataResolver(info.FileReferences), cancellationToken);
}
public Task<bool> StartAsync(IncompleteFileInfo info, CancellationToken cancellationToken)
{
return StartAsync(new FinalFileInfo(info.FilePath, info.OutputDirectoryPath),
new ModuleFactory(File.ReadAllBytes(info.FilePath), new LogErrorListener(_logger, _obfuscationSettings)),
new ModuleFactory(File.ReadAllBytes(info.FilePath), _obfuscationSettings, new LogErrorListener(_logger, _obfuscationSettings)),
new FileDataWriter(), new AutomaticPathReferencesDataResolver(info.ReferencesDirectoryPath), cancellationToken);
}
}
1 change: 1 addition & 0 deletions src/BitMono.Shared/Models/ObfuscationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class ObfuscationSettings
public bool FailOnNoRequiredDependency { get; set; }
public bool OutputRuntimeMonikerWarnings { get; set; }
public bool OpenFileDestinationInFileExplorer { get; set; }
public bool AllowPotentialBreakingChangesToModule { get; set; }
public bool SpecificNamespacesObfuscationOnly { get; set; }
public string[]? SpecificNamespaces { get; set; }
public string[]? RandomStrings { get; set; }
Expand Down
11 changes: 11 additions & 0 deletions troubleshooting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### Troubleshooting

Here we describe most known issues and its solution, something like Faq.

#### Access Denied:

Try to set `OpenFileDestinationInFileExplorer` to `false` in `obfuscation.json`

#### Output file changed its bits:

If your output file is changed from x32 to x64 or something similar, and it cause issues for you then set `AllowPotentialBreakingChangesToModule` to `false` in `obfuscation.json`, however be careful changing it, because some protections may be dependent on this configurations (for example UnmanagedString).

0 comments on commit 5e6f5c9

Please sign in to comment.