Skip to content

Commit

Permalink
feat: add DecryptParameter
Browse files Browse the repository at this point in the history
  • Loading branch information
tautcony committed May 12, 2022
1 parent 24c0344 commit 1680ea0
Show file tree
Hide file tree
Showing 4 changed files with 768 additions and 17 deletions.
8 changes: 4 additions & 4 deletions ISTA-Patcher/CecilUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ public static bool IsJump(OpCode oc)

public static MethodDefinition? GetMethod(this AssemblyDefinition asm, string type, string name, string desc)
{
var tds = asm.Modules.Where(m => m.GetType(type) != null).Select(m => m.GetType(type));
var tds = asm.Modules.Where(m => m.GetType(type) != null).Select(m => m.GetType(type)).ToList();
if (!tds.Any())
{
return null;
}
if (tds.Count() != 1)
if (tds.Count != 1)
{
throw new Exception();
}
Expand All @@ -81,12 +81,12 @@ public static bool IsJump(OpCode oc)

public static FieldDefinition? GetField(this AssemblyDefinition asm, string type, string name, string fieldType)
{
var tds = asm.Modules.Where(m => m.GetType(type) != null).Select(m => m.GetType(type));
var tds = asm.Modules.Where(m => m.GetType(type) != null).Select(m => m.GetType(type)).ToList();
if (!tds.Any())
{
return null;
}
if (tds.Count() > 1)
if (tds.Count > 1)
{
throw new Exception();
}
Expand Down
90 changes: 84 additions & 6 deletions ISTA-Patcher/PatchUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Mono.Cecil;
using System.Reflection.Emit;
using Mono.Cecil;
using Mono.Cecil.Cil;
using OpCodes = Mono.Cecil.Cil.OpCodes;

namespace ISTA_Patcher
{
Expand All @@ -8,8 +11,7 @@ public static AssemblyDefinition LoadAssembly(string fileName)
{
var assemblyResolver = new DefaultAssemblyResolver();
assemblyResolver.AddSearchDirectory(Path.GetDirectoryName(fileName));
assemblyResolver.AddSearchDirectory("C:/Windows/Microsoft.NET/Framework64/v4.0.30319");
var assembly = AssemblyDefinition.ReadAssembly(fileName, new ReaderParameters() { AssemblyResolver = assemblyResolver, InMemory = true });
var assembly = AssemblyDefinition.ReadAssembly(fileName, new ReaderParameters { AssemblyResolver = assemblyResolver, InMemory = true });
return assembly;
}

Expand Down Expand Up @@ -180,7 +182,10 @@ public static void SetPatchedMark(AssemblyDefinition assembly)
"Patched.By", "TC",
TypeAttributes.NestedPrivate,
assembly.MainModule.ImportReference(typeof(object)));
var dateField = new FieldDefinition("date", FieldAttributes.Private | FieldAttributes.Static, assembly.MainModule.ImportReference(typeof(string)))
var dateField = new FieldDefinition(
"date",
FieldAttributes.Private | FieldAttributes.Static,
assembly.MainModule.ImportReference(typeof(string)))
{
Constant = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
};
Expand All @@ -189,10 +194,10 @@ public static void SetPatchedMark(AssemblyDefinition assembly)
assembly.MainModule.Types.Add(patchedType);
}

public static string DecryptString(string value, int seed)
public static string DecryptString(string value, int baseSeed, int seed)
{
char[] charArray = value.ToCharArray();
int key = 825083450 + seed;
int key = baseSeed + seed;
for (var i = 0; i < charArray.Length; i++)
{
char ch = charArray[i];
Expand All @@ -208,5 +213,78 @@ public static string DecryptString(string value, int seed)
}
return string.Intern(new string(charArray));
}

public static bool DecryptParameter(AssemblyDefinition assembly)
{
var b = assembly.GetMethod(
"BMW.Rheingold.CoreFramework.LicenseManagement.LicenseWizardHelper",
"b",
"(System.String,System.Int32)System.String");
if (b == null)
{
return false;
}

var baseSeed = 0;
foreach (var instruction in b.Body.Instructions)
{
if (instruction.OpCode == OpCodes.Stloc_1)
{
break;
}
if (instruction.OpCode == OpCodes.Ldc_I4)
{
baseSeed += (int) instruction.Operand;
}
}

foreach (var type in assembly.MainModule.Types)
{
foreach (var method in type.Methods)
{
if (method.Body == null)
{
continue;
}
var decodedStrings = new List<KeyValuePair<int, string>>();
for (var i = 0; i < method.Body.Instructions.Count; ++i)
{
var instruction = method.Body.Instructions[i];
if (instruction.OpCode != OpCodes.Call || instruction.Operand != b) continue;
if (instruction.Previous.OpCode != OpCodes.Ldloc ||
instruction.Previous.Previous.OpCode != OpCodes.Ldstr)
{
continue;
}
var seed = (VariableDefinition) instruction.Previous.Operand;
var seedValue = int.MaxValue;
var instruction2 = method.Body.Instructions.FirstOrDefault(inst =>
inst.OpCode == OpCodes.Stloc && inst.Operand == seed);
if (instruction2 != null)
{
seedValue = (int) instruction2.Previous.Operand;
}
if (seedValue == int.MaxValue) continue;
var str = (string) instruction.Previous.Previous.Operand;
decodedStrings.Add(new KeyValuePair<int, string>(i, PatchUtils.DecryptString(str, baseSeed, seedValue)));
}

var processor = method.Body.GetILProcessor();
decodedStrings.Reverse();
foreach (var pair in decodedStrings)
{
// ldstr
// ldloc
// call -> ldstr
// 7 8 9
processor.Replace(pair.Key, Instruction.Create(OpCodes.Ldstr, pair.Value));
processor.RemoveAt(pair.Key - 1);
processor.RemoveAt(pair.Key - 2);
}
}
}

return true;
}
}
}
13 changes: 6 additions & 7 deletions ISTA-Patcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace ISTA_Patcher
{
class Patcher
internal static class Patcher
{
static void PatchISTA(string basePath, string[] pendingPatchList, string outputDir = "modded")
{
Expand Down Expand Up @@ -153,6 +153,7 @@ static void PatchISTA(string basePath, string[] pendingPatchList, string outputD
Console.Write(" ");
if (isPatched)
{
// PatchUtils.DecryptParameter(assembly);
Console.WriteLine("[patched]");
PatchUtils.SetPatchedMark(assembly);
var moddedDir = Path.Join(basePath, outputDir);
Expand Down Expand Up @@ -183,21 +184,19 @@ public static void Main(string[] args)
Console.WriteLine("no path provided");
return;
}
string path = args[0];
string cwd = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
var path = args[0];
var cwd = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;

string[]? includeList = null;

try
{
using FileStream stream = new(Path.Join(cwd, "patchConfig.json"), FileMode.Open, FileAccess.Read);
Dictionary<string, string[]>? patchConfig = JsonSerializer.Deserialize<Dictionary<string, string[]>>(stream);
var patchConfig = JsonSerializer.Deserialize<Dictionary<string, string[]>>(stream);
includeList = patchConfig?.GetValueOrDefault("include");
}
catch (Exception ex) when (
ex is FileNotFoundException ||
ex is IOException ||
ex is JsonException
ex is FileNotFoundException or IOException or JsonException
)
{
Console.WriteLine($"Failed to load config file: {ex.Message}");
Expand Down
Loading

0 comments on commit 1680ea0

Please sign in to comment.