-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
137 lines (110 loc) · 4.66 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace XexDecryptor {
public static class Program {
public static void Abort (string format, params object[] args) {
Console.Error.WriteLine(format, args);
Environment.Exit(1);
}
public static int? FindByteString (byte[] bytes, int startOffset, int? limit, byte[] searchString) {
int i = startOffset;
int end = limit.GetValueOrDefault(bytes.Length - i) + i;
int matchStart = 0, matchPos = 0;
while (i < end) {
var current = bytes[i];
if (current != searchString[matchPos]) {
if (matchPos != 0) {
matchPos = 0;
continue;
}
} else {
if (matchPos == 0)
matchStart = i;
matchPos += 1;
if (matchPos == searchString.Length)
return matchStart;
}
i++;
}
return 0;
}
public static int? FindExecutableHeader (byte[] bytes, int startOffset) {
int? offset = FindByteString(bytes, startOffset, null, new byte[] { 0x4D, 0x5A });
if (!offset.HasValue)
return null;
int? offset2 = FindByteString(bytes, offset.Value + 2, 512, new byte[] { 0x50, 0x45, 0x00, 0x00 });
if (!offset2.HasValue)
return null;
return offset;
}
public static void Main (string[] args) {
if (args.Length < 1) {
Abort("Usage: XexDecryptor [xex filenames]");
}
var filenames = new List<string>();
foreach (var filename in args) {
if (filename.IndexOfAny(new char[] { '*', '?' }) < 0) {
filenames.Add(filename);
} else {
var dirname = Path.GetDirectoryName(filename);
if (String.IsNullOrWhiteSpace(dirname))
dirname = Environment.CurrentDirectory;
filenames.AddRange(Directory.GetFiles(dirname, Path.GetFileName(filename)));
}
}
foreach (var sourceFile in filenames) {
Console.WriteLine("Decrypting {0}...", sourceFile);
if (!File.Exists(sourceFile)) {
Abort("File not found: {0}", sourceFile);
}
string outputFile = Path.GetFullPath(sourceFile);
if (outputFile.ToLower().EndsWith(".xex")) {
outputFile = Path.Combine(
Path.GetDirectoryName(outputFile),
Path.GetFileNameWithoutExtension(outputFile)
);
} else {
outputFile += ".decrypted";
}
var tempFilePath = Path.GetTempFileName();
File.Delete(tempFilePath);
string stdError;
byte[] stdOut;
Util.RunProcess(
"xextool.exe",
String.Format(
"-c u -e u -o \"{0}\" \"{1}\"",
tempFilePath,
sourceFile
),
null, out stdError, out stdOut
);
if (!String.IsNullOrWhiteSpace(stdError)) {
File.Delete(tempFilePath);
Abort("XexTool reported error: {0}", stdError);
}
var xexBytes = File.ReadAllBytes(tempFilePath);
File.Delete(tempFilePath);
var firstHeader = FindExecutableHeader(xexBytes, 0);
if (!firstHeader.HasValue) {
Abort("File is not a valid executable.");
}
var secondHeader = FindExecutableHeader(xexBytes, firstHeader.Value + 128);
if (!secondHeader.HasValue) {
Abort("File does not contain an embedded executable.");
}
Console.Write("Extracting to '{0}'... ", outputFile);
using (var fs = File.OpenWrite(outputFile)) {
fs.Write(xexBytes, secondHeader.Value, xexBytes.Length - secondHeader.Value);
}
Console.WriteLine("done.");
Console.WriteLine("Rewriting assembly... ");
AssemblyRewriter.Rewrite(outputFile);
}
Console.WriteLine("{0} assemblies processed.", filenames.Count);
}
}
}