-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
144 lines (117 loc) · 4.75 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
using System.IO.Compression;
using System.Reflection;
using CommandLine;
using CommandLine.Text;
namespace libz_unpacker
{
internal class Program
{
static void Main(string[] args)
{
Parser.Default.ParseArguments<Options>(args)
.WithParsed(Run)
.WithNotParsed(HandleParsingErrors);
}
static void Run(Options options)
{
if (Directory.Exists(options.InputPath))
{
DirectoryInfo d = new DirectoryInfo(options.InputPath);
foreach (var file in d.GetFiles())
{
if (TryLoadAssembly(file.FullName, out Assembly? assembly))
{
SaveEmbeddedResources(assembly, options.OutputPath, options.Recursive);
}
}
}
else if (TryLoadAssembly(options.InputPath, out Assembly? assembly))
{
SaveEmbeddedResources(assembly, options.OutputPath, options.Recursive);
}
else
{
Console.WriteLine($"{options.InputPath} is neither a directory nor a valid assembly.");
}
}
static void HandleParsingErrors(IEnumerable<Error> errors)
{
Console.WriteLine("Failed to parse command line arguments.");
Console.WriteLine(HelpText.AutoBuild(Parser.Default.ParseArguments<Options>(["--help"])));
}
private static void SaveEmbeddedResources(Assembly? assembly, string outFolder, bool recursive)
{
if (assembly == null)
{
return;
}
IEnumerable<byte[]> embeddedAssembliesBytes = GetEmbeddedAssemblies(assembly);
foreach (var embeddedAssemblyBytes in embeddedAssembliesBytes)
{
SaveAssembly(embeddedAssemblyBytes, outFolder);
if (recursive)
{
Assembly subAssembly = Assembly.Load(embeddedAssemblyBytes);
SaveEmbeddedResources(subAssembly, outFolder, recursive);
}
}
}
private static IEnumerable<byte[]> GetEmbeddedAssemblies(Assembly assembly)
{
string[] resourceNames = assembly.GetManifestResourceNames();
IEnumerable<string> asmzResources = resourceNames.Where(x => x.StartsWith("asmz://"));
foreach (var asmzResource in asmzResources)
{
string[] resourceIdentifier = asmzResource.Replace("asmz://", "").Split("/");
string flags = resourceIdentifier[2];
bool compressed = flags.Contains("z");
using (Stream? resourceStream = assembly.GetManifestResourceStream(asmzResource))
{
if (resourceStream == null)
{
Console.WriteLine($"Resource '{asmzResource}' not found.");
continue;
}
using (MemoryStream memoryStream = new MemoryStream())
{
using (Stream deflateStream = compressed ? new DeflateStream(resourceStream, CompressionMode.Decompress) : resourceStream)
{
deflateStream.CopyTo(memoryStream);
}
byte[] resourceBytes = memoryStream.ToArray();
yield return resourceBytes;
}
}
}
}
private static void SaveAssembly(byte[] bytes, string outFolder)
{
Assembly embeddedAssembly = Assembly.Load(bytes);
string fileName = embeddedAssembly.GetName().Name ?? "UnknownAssembly";
Console.WriteLine($"Saving {fileName}.dll");
File.WriteAllBytes(outFolder + "\\" + fileName + ".dll", bytes);
}
private static bool TryLoadAssembly(string filePath, out Assembly? assembly)
{
// Check if the file has a .dll or .exe extension
string extension = Path.GetExtension(filePath).ToLower();
assembly = null;
if (extension == ".dll" || extension == ".exe")
{
// Attempt to load the assembly to verify if it's valid
try
{
assembly = Assembly.LoadFrom(filePath);
return true;
}
catch (Exception ex)
{
Console.WriteLine($"Failed to load {filePath}: {ex.Message}");
// Thrown if the file is not a valid assembly
return false;
}
}
return false;
}
}
}