This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
145 lines (143 loc) · 5.89 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.Parsing;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.XPath;
using decfix;
// For future reference, hn is appid 365450
Argument<FileInfo> saveFileArg = new("save-file", "Save file to create patched versions of");
Option<string> passwordFileOption = new(new[] { "-p", "--password-file" }, "Password file (containing JSON array of strings)");
Option<string> outputDirectoryOption = new(new[] { "-o", "--output-directory" }, "Directory to output");
var rootCommand = new RootCommand("Patch hacknet save file") { saveFileArg, passwordFileOption, outputDirectoryOption };
rootCommand.SetHandler(ProcessAsync);
return rootCommand.Invoke(args);
async Task<int> ProcessAsync(InvocationContext invocationContext)
{
var log = new ConsoleLog();
List<string> defaultPasswords = new()
{
"",
"19474-217316293",
"dx122DX",
"84833mmn1",
"DANGER",
"Obi-Wan",
"ax229msjA",
"beepbeep",
"decryptionPassword",
"divingsparrow",
"dleatrou",
"password",
"quinnoq",
"test",
"yuna"
};
char[] separators = { '\r', '\n' };
FileInfo saveFile = invocationContext.ParseResult.GetValueForArgument(saveFileArg);
string? outputDirectory = invocationContext.ParseResult.HasOption(outputDirectoryOption) ? invocationContext.ParseResult.GetValueForOption(outputDirectoryOption) : null;
string? passwordFile = invocationContext.ParseResult.HasOption(passwordFileOption) ? invocationContext.ParseResult.GetValueForOption(passwordFileOption) : null;
if (!saveFile.Exists)
{
log.LogError($"File {saveFile} does not exist.");
Environment.Exit(118);
}
if (outputDirectory == null)
{
outputDirectory = saveFile.DirectoryName!;
}
Directory.CreateDirectory(outputDirectory);
List<string> passwords = new(defaultPasswords);
if (passwordFile != null)
{
if (!File.Exists(passwordFile))
{
Util.PrintErrorMessage($"File {passwordFile} does not exist.");
return 119;
}
await using FileStream fs = new(passwordFile, FileMode.Open, FileAccess.Read);
string[]? filePasswords = await JsonSerializer.DeserializeAsync(fs, SourceGenerationContext.Default.StringArray);
if (filePasswords == null)
{
Util.PrintWarningMessage($"File {passwordFile} doesn't contain password array. Ignoring.");
}
else
{
passwords.AddRange(passwords);
}
}
Dictionary<int, HashSet<HashEntry>> dict = Util.GenerateHashDictionary(passwords);
XmlDocument document = new() { PreserveWhitespace = true };
byte[] buf = File.ReadAllBytes(saveFile.FullName);
foreach (DecType decType in new[] { DecType.Windows, DecType.Mono })
{
string decTypeName = decType.GetDisplayName();
log.Log($"-- Processing for {decTypeName} output");
MemoryStream ms = new(buf, false);
document.Load(ms);
XPathNavigator navigator = document.CreateNavigator()!;
bool changed = false;
foreach (XPathNavigator n in navigator.Select("//file"))
{
string fileData = n.Value;
int loc = fileData.IndexOf("#DEC_ENC::", StringComparison.Ordinal);
if (loc == -1)
continue;
string oname = n.GetAttribute("name", "");
string[] data = fileData[loc..].Split(separators, StringSplitOptions.RemoveEmptyEntries);
if (data.Length != 2)
continue;
string? tvalue;
try
{
if (!Util.Analyze(oname, data, dict, log, out DecFile? decfile, out DecType? originType, out string? originPassword))
throw new DecException("DEC file not valid, parsing failed");
string originTypeName = originType?.GetDisplayName() ?? "<unknown platform>";
if (originPassword != null)
{
DecHeader header = decfile.Header;
// only skip if we already have known / usable pw. otherwise dump the content
if (originType == decType)
{
log.Log($"Skipped {originTypeName} file {oname} (same platform)");
continue;
}
tvalue = Util.Encrypt(decfile.Message, header.Header, header.Signature, header.Extension, Util.s_hashFunctions[decType](originPassword), Util.s_emptyHashes[decType]);
log.Log($"Re-encoded {originTypeName} file {oname}");
}
else
{
DecHeader header = decfile.Header;
tvalue = $"Header:\n{header.Header}\nIP:\n{header.Signature}\n";
if (header.Extension != null)
tvalue += $"Extension:\n{header.Extension}";
tvalue += $"Key:\n{header.Key}";
tvalue += $"\n{decfile.Message}";
log.Log($"No password for {originTypeName} file {oname} - dumped fallback");
}
}
catch (DecException e)
{
log.LogError($"Error for file {oname}\nData:\n{fileData}\nMessage: {e.Message}");
return 3;
}
n.SetValue(fileData[..loc] + tvalue);
changed = true;
}
string targetPath = Util.GeneratePath(outputDirectory, saveFile.FullName, decType);
if (changed)
{
log.Log($">> Saving {decTypeName} file to {targetPath}");
document.Save(targetPath);
}
else
{
log.Log($"Skipping write of {decTypeName} file (no changes needed)");
}
}
return 0;
}