Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make --output-* params optional, support .ovpax and .dict extensions #42

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,37 @@ The library can be used in any .NET application built with `net462`, `netcoreapp
**Obfuscation**

```csharp
using var vpax = File.Open(@"C:\path\to\file.vpax", FileMode.Open);
using var vpax = new MemoryStream(File.ReadAllBytes(@"C:\path\to\unobfuscated.vpax"));
var obfuscator = new VpaxObfuscator();
var dictionary = obfuscator.Obfuscate(vpax);
dictionary.WriteTo(@"C:\path\to\dictionary.json");

dictionary.WriteTo(@"C:\path\to\dictionary.dict");
File.WriteAllBytes(@"C:\path\to\obfuscated.ovpax", vpax.ToArray());
```

**Deobfuscation**

```csharp
using var vpax = File.Open(@"C:\path\to\obfuscated.vpax", FileMode.Open);
var dictionary = ObfuscationDictionary.ReadFrom(@"C:\path\to\dictionary.json");
using var vpax = new MemoryStream(File.ReadAllBytes(@"C:\path\to\obfuscated.ovpax"));
var dictionary = ObfuscationDictionary.ReadFrom(@"C:\path\to\dictionary.dict");
var obfuscator = new VpaxObfuscator();
obfuscator.Deobfuscate(vpax, dictionary);

File.WriteAllBytes(@"C:\path\to\deobfuscated.vpax", vpax.ToArray());
```

**Incremental Obfuscation**

> Incremental obfuscation keeps the same obfuscated names across different VPAX versions of the same model.

```csharp
using var vpax = File.Open(@"C:\path\to\file.vpax", FileMode.Open);
var dictionaryV1 = ObfuscationDictionary.ReadFrom(@"C:\path\to\dictionary-v1.json");
using var vpax = new MemoryStream(File.ReadAllBytes(@"C:\path\to\unobfuscated-v2.vpax"));
var dictionaryV1 = ObfuscationDictionary.ReadFrom(@"C:\path\to\dictionary-v1.dict");
var obfuscator = new VpaxObfuscator();
var dictionaryV2 = obfuscator.Obfuscate(vpax, dictionaryV1);
dictionaryV2.WriteTo(@"C:\path\to\dictionary-v2.json");
var dictionary = obfuscator.Obfuscate(vpax, dictionaryV1);

dictionary.WriteTo(@"C:\path\to\dictionary-v2.dict");
File.WriteAllBytes(@"C:\path\to\obfuscated-v2.ovpax", vpax.ToArray());
```

## CLI
Expand All @@ -66,11 +72,11 @@ Usage:
vpax-obfuscator obfuscate [options]

Options:
--vpax <vpax> (REQUIRED) Path to the unobfuscated VPAX file.
--dictionary <dictionary> Path to the dictionary file to be used for incremental obfuscation. If not provided, a new dictionary will be created.
--output-vpax <output-vpax> (REQUIRED) Path to write the obfuscated VPAX file.
--output-dictionary <output-dictionary> (REQUIRED) Path to write the obfuscation dictionary file.
--track-unobfuscated Specifies whether to include unobfuscated values in the output dictionary.
--allow-overwrite Allow output files to be overwritten. If not provided, the command will fail if an output file already exists.
-?, -h, --help Show help and usage information
--vpax <vpax> (REQUIRED) Path to the unobfuscated VPAX file.
--dictionary <dictionary> Path to the dictionary file to be used for incremental obfuscation. If not provided, a new dictionary will be created.
--output-vpax <output-vpax> Path to write the obfuscated VPAX file. If not provided, the file will be written to the same folder as the '--vpax' file, using the default file extension for obfuscated VPAX files, which is '.ovpax'.
--output-dictionary <output-dictionary> Path to write the obfuscation dictionary file. If not provided, the file will be written to the same folder as the '--vpax' file, using the default file extension for obfuscation dictionary files, which is '.dict'.
--track-unobfuscated Specifies whether to include unobfuscated values in the output dictionary.
--allow-overwrite Allow output files to be overwritten. If not provided, the command will fail if an output file already exists.
-?, -h, --help Show help and usage information
```
28 changes: 16 additions & 12 deletions src/Dax.Vpax.Obfuscator.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ private static Command BuildCommandObfuscate(Option<bool> allowOverwriteOption)
};
var outputVpaxOption = new Option<FileInfo>(name: "--output-vpax")
{
Description = "Path to write the obfuscated VPAX file.",
IsRequired = true,
Description = "Path to write the obfuscated VPAX file. If not provided, the file will be written to the same folder as the '--vpax' file, using the default file extension for obfuscated VPAX files, which is '.ovpax'.",
IsRequired = false,
};
var outputDictionaryOption = new Option<FileInfo>(name: "--output-dictionary")
{
Description = "Path to write the obfuscation dictionary file.",
IsRequired = true,
Description = "Path to write the obfuscation dictionary file. If not provided, the file will be written to the same folder as the '--vpax' file, using the default file extension for obfuscation dictionary files, which is '.dict'.",
IsRequired = false,
};
var trackUnobfuscatedOption = new Option<bool?>(name: "--track-unobfuscated")
{
Expand Down Expand Up @@ -73,8 +73,8 @@ private static Command BuildCommandDeobfuscate(Option<bool> allowOverwriteOption
};
var outputVpaxOption = new Option<FileInfo>(name: "--output-vpax")
{
Description = "Path to write the deobfuscated VPAX file.",
IsRequired = true,
Description = "Path to write the deobfuscated VPAX file. If not provided, the file will be written to the same folder as the '--vpax' file, using the default file extension for unobfuscated VPAX files, which is '.vpax'.",
IsRequired = false,
};
var command = new Command("deobfuscate", "Deobfuscate the DaxModel.json file from an obfuscated VPAX file using the provided dictionary.");
command.AddOption(vpaxOption);
Expand All @@ -84,7 +84,7 @@ private static Command BuildCommandDeobfuscate(Option<bool> allowOverwriteOption
return command;
}

private static void Obfuscate(FileInfo vpaxFile, FileInfo? dictionaryOption, FileInfo outputVpaxFile, FileInfo outputDictionaryFile, bool allowOverwrite, bool? trackUnobfuscated)
private static void Obfuscate(FileInfo vpaxFile, FileInfo? dictionaryOption, FileInfo? outputVpaxFile, FileInfo? outputDictionaryFile, bool allowOverwrite, bool? trackUnobfuscated)
{
using var stream = Read(vpaxFile);
var obfuscator = new VpaxObfuscator();
Expand All @@ -96,18 +96,22 @@ private static void Obfuscate(FileInfo vpaxFile, FileInfo? dictionaryOption, Fil

if (obfuscator.Options.TrackUnobfuscated)
NotifyUnobfuscated(outputDictionary);

var dictPath = outputDictionaryFile?.FullName ?? Path.ChangeExtension(vpaxFile.FullName, ".dict");
var ovpaxPath = outputVpaxFile?.FullName ?? Path.ChangeExtension(vpaxFile.FullName, ".ovpax");

outputDictionary.WriteTo(outputDictionaryFile.FullName, overwrite: allowOverwrite, indented: true);
Write(stream, outputVpaxFile, allowOverwrite);
outputDictionary.WriteTo(dictPath, overwrite: allowOverwrite, indented: true);
Write(stream, ovpaxPath, allowOverwrite);
}

private static void Deobfuscate(FileInfo vpaxFile, FileInfo dictionaryFile, FileInfo outputVpaxFile, bool allowOverwrite)
{
using var stream = Read(vpaxFile);
var obfuscator = new VpaxObfuscator();
var ovpaxPath = outputVpaxFile?.FullName ?? Path.ChangeExtension(vpaxFile.FullName, ".vpax");

obfuscator.Deobfuscate(stream, dictionary: ObfuscationDictionary.ReadFrom(dictionaryFile.FullName));
Write(stream, outputVpaxFile, allowOverwrite);
Write(stream, ovpaxPath, allowOverwrite);
}

private static MemoryStream Read(FileInfo file)
Expand All @@ -118,11 +122,11 @@ private static MemoryStream Read(FileInfo file)
return stream;
}

private static void Write(MemoryStream stream, FileInfo file, bool allowOverwrite)
private static void Write(MemoryStream stream, string path, bool allowOverwrite)
{
var bytes = stream.ToArray();
var mode = allowOverwrite ? FileMode.Create : FileMode.CreateNew;
using var fileStream = new FileStream(file.FullName, mode, FileAccess.Write, FileShare.Read);
using var fileStream = new FileStream(path, mode, FileAccess.Write, FileShare.Read);
fileStream.Write(bytes, 0, bytes.Length);
}

Expand Down
Loading