Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
guys i'm building a keygen and patcher but evertime i put the ostrisch key to the license i get Length of the data to decrypt is invalid. anyone got a idea, i give up haha?

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace eCtuneKeygen
{
	/// <summary>
	/// Handles cryptography.
	/// </summary>
	public static class Crypto
	{
		/// <summary>
		/// Password for v0.0.2.3 (r3 - r17).
		/// </summary>
		public static readonly String Password = "h0nd@eC7un€";

		/// <summary>
		/// Encrypt some string with the default password.
		/// </summary>
		/// <param name="input">String to encrypt</param>
		/// <returns>Encrypted string</returns>
		public static String Encrypt(String input)
		{
			return Encrypt(input, Password);
		}

		/// <summary>
		/// Encrypt some string.
		/// </summary>
		/// <param name="input">String to encrypt</param>
		/// <param name="password">Password to encrypt with</param>
		/// <returns>Encrypted string</returns>
		public static String Encrypt(String input, String password)
		{
			return OriginalEncrypt(input, password, "473470", "SHA1", 2, "@1B2c3D4e5F6g7H8", 256);
		}

		/// <summary>
		/// Original     method from decompiled source.
		/// </summary>
		public static String OriginalEncrypt(string input, string password, string rgbSaltStr, string strHashName, int iterations, string ivStr, int int_2)
		{
			byte[] iv = Encoding.ASCII.GetBytes(ivStr);
			byte[] rgbSalt = Encoding.ASCII.GetBytes(rgbSaltStr);
			byte[] inputBytes = Encoding.UTF8.GetBytes(input);
			PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(password, rgbSalt, strHashName, iterations);
			byte[] bytes4 = passwordDeriveBytes.GetBytes(int_2 / 8);
			ICryptoTransform stream = new RijndaelManaged
			{
				Mode = CipherMode.CBC
			}.CreateEncryptor(bytes4, iv);
			MemoryStream memoryStream = new MemoryStream();
			CryptoStream cryptoStream = new CryptoStream(memoryStream, stream, CryptoStreamMode.Write);
			cryptoStream.Write(inputBytes, 0, inputBytes.Length);
			cryptoStream.FlushFinalBlock();
			byte[] inArray = memoryStream.ToArray();
			memoryStream.Close();
			cryptoStream.Close();
			return Convert.ToBase64String(inArray);
		}
	}
}


using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;

namespace eCtuneKeygen
{
	/// <summary>
	/// License.
	/// </summary>
	public class License
	{
		public String Name { get; set; }
		public String Org { get; set; }
		public String Email { get; set; }
		public String LicID { get; set; }
		public String LicType { get; set; }
		public String LicExpDate { get; set; }
		public String LicKey { get; set; }
		public String LicPayType { get; set; }
		public String[] Ostriches { get; set; }

		public LicenseData Data
		{
			get
			{
				LicenseData data = new LicenseData();
				data.Name = this.Name;
				data.Org = this.Org;
				data.Email = this.Email;
				data.LicID = Crypto.Encrypt(this.LicID);
				data.LicType = Crypto.Encrypt(this.LicType);
				data.LicExpDate = Crypto.Encrypt(this.LicExpDate);
				data.LicKey = this.LicKey;
				data.LicPayType = Crypto.Encrypt(this.LicPayType);
				data.Ostrich0 = this.Ostriches[0];
				data.Ostrich1 = this.Ostriches[1];
				data.Ostrich2 = this.Ostriches[2];
				data.Ostrich3 = this.Ostriches[3];
				data.Ostrich4 = this.Ostriches[4];
				data.Ostrich5 = this.Ostriches[5];
				data.Ostrich6 = this.Ostriches[6];
				data.Ostrich7 = this.Ostriches[7];
				data.Ostrich8 = this.Ostriches[8];
				data.Ostrich9 = this.Ostriches[9];
				return data;
			}
		}

		public LicenseType Type
		{
			get { return (LicenseType)(Int32.Parse(this.LicType)); }
			set { this.LicType = ((int)value).ToString(); }
		}

		public License()
		{
			this.Initialize();
		}

		private void Initialize()
		{
			this.Name = System.Environment.UserName;
			this.Org = System.Environment.MachineName;
			this.Email = "";
			this.LicID = "0241";
			this.LicType = ((int)LicenseType.ProOstrich).ToString();
			this.LicExpDate = DateTime.MaxValue.ToString();
			this.LicKey = "FakeKey";
			this.LicPayType = "0";
			this.Ostriches = new String[10];
		}

		public override string ToString()
		{
			return this.Data.ToString();
		}
	}

	/// <summary>
	/// Serializable license data.
	/// </summary>
	[XmlType("License")]
	public class LicenseData
	{
		[XmlAttribute]
		public String Name { get; set; }

		[XmlAttribute]
		public String Org { get; set; }

		[XmlAttribute]
		public String Email { get; set; }

		[XmlAttribute]
		public String LicID { get; set; }

		[XmlAttribute]
		public String LicType { get; set; }

		[XmlAttribute]
		public String LicExpDate { get; set; }

		[XmlAttribute]
		public String LicKey { get; set; }

		[XmlAttribute]
		public String LicPayType { get; set; }

		[XmlAttribute]
		public String Ostrich0 { get; set; }

		[XmlAttribute]
		public String Ostrich1 { get; set; }

		[XmlAttribute]
		public String Ostrich2 { get; set; }

		[XmlAttribute]
		public String Ostrich3 { get; set; }

		[XmlAttribute]
		public String Ostrich4 { get; set; }

		[XmlAttribute]
		public String Ostrich5 { get; set; }

		[XmlAttribute]
		public String Ostrich6 { get; set; }

		[XmlAttribute]
		public String Ostrich7 { get; set; }

		[XmlAttribute]
		public String Ostrich8 { get; set; }

		[XmlAttribute]
		public String Ostrich9 { get; set; }

		public static LicenseData Default
		{
			get
			{
				LicenseData data = new LicenseData();
				data.Name = "";
				data.Org = "";
				data.Email = "";
				data.LicID = "";
				data.LicType = "";
				data.LicExpDate = "";
				data.LicKey = "";
				data.LicPayType = "";
				data.Ostrich0 = "";
				data.Ostrich1 = "";
				data.Ostrich2 = "";
				data.Ostrich3 = "";
				data.Ostrich4 = "";
				data.Ostrich5 = "";
				data.Ostrich6 = "";
				data.Ostrich7 = "";
				data.Ostrich8 = "";
				data.Ostrich9 = "";
				return data;
			}
		}

		public String NodeText
		{
			get
			{
				XmlDocument doc = new XmlDocument();
				System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(this.GetType());
				System.IO.MemoryStream stream = new System.IO.MemoryStream();

				try
				{
					serializer.Serialize(stream, this);
					stream.Position = 0;
					doc.Load(stream);
					return doc.InnerXml;
				}
				catch
				{
					throw;
				}
				finally
				{
					stream.Close();
					stream.Dispose();
				}
			}
		}

		public override string ToString()
		{
			Regex versionPattern = new Regex("\\<\\?xml version=\".*\"\\?\\>");
			Regex xmlnsPattern = new Regex("\\s*xmlns:[^=]+=\"[^\"]+\"");

			// Fix up the string
			String nodeText = this.NodeText;
			nodeText = versionPattern.Replace(nodeText, "");
			nodeText = xmlnsPattern.Replace(nodeText, "");
			return Crypto.Encrypt(nodeText);
		}
	}

	public enum LicenseType : int
	{
		DataLogger = -1,
		Free = 0,
		ProDemon = 1,
		ProOstrich = 2,
		Tuner = 3,
		Developer = 4
	}
}


using System;
using System.IO;
using System.Collections.Generic;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
using dnlib.DotNet.Writer;

namespace eCtuneKeygen
{
	public class Patcher
	{
		public ModuleDefMD Module { get; private set; }

		/// <summary>
		/// Construct a patcher for a file.
		/// </summary>
		/// <param name="filepath">Filepath</param>
		public Patcher(String filepath)
		{
			this.Module = ModuleDefMD.Load(filepath);
		}

		/// <summary>
		/// Attempt to patch the loaded module.
		/// </summary>
		/// <returns>Results of patching</returns>
		public PatchResults Patch()
		{
			if (this.Module == null)
				return null;

			Boolean sigCheckPatched = false, keyCheckPatched = false;

			var types = this.Module.GetTypes();
			foreach(var type in types)
			{
				if (this.PatchLicenseSignatureCheck(type))
				{
					sigCheckPatched = true;

					// Only check for key check methods if in the same class as signature check methods
					if(this.PatchLicenseKeyChecks(type))
					{
						keyCheckPatched = true;
					}
				}
			}

			return new PatchResults(sigCheckPatched, keyCheckPatched);
		}

		/// <summary>
		/// Write the modified module to a file.
		/// </summary>
		/// <param name="filepath">Filepath</param>
		public void Write(String filepath)
		{
			var options = new ModuleWriterOptions(this.Module);
			options.MetaDataOptions.Flags |= MetaDataFlags.PreserveAll;
			options.MetaDataOptions.Flags |= MetaDataFlags.KeepOldMaxStack;
			options.Logger = DummyLogger.NoThrowInstance; // Avoid "ResolutionScope is null" exception
			this.Module.Write(filepath, options);
		}

		/// <summary>
		/// Check for and patch the license signature check to always return true.
		/// </summary>
		/// <param name="type">Type (Class) to inspect and patch if detected</param>
		/// <returns>true if patched, false if not modified</returns>
		private Boolean PatchLicenseSignatureCheck(TypeDef type)
		{
			if (type == null)
				return false;

			MethodDef sigCheckMethod = null, rsaProviderMethod = null;

			IList<MethodDef> methods = type.Methods;
			foreach(var method in methods)
			{
				if (method == null)
					continue;

				// Check for signature verification method
				// Looks like: <private bool method(RSA)>
				if (!method.IsStatic
				&& method.ReturnType.FullName.Equals("System.Boolean")
				&& method.Parameters.Count == 2 && method.Parameters[1].Type.FullName.Equals("System.Security.Cryptography.RSA"))
				{
					sigCheckMethod = method;
				}

				// Check for static RSA service provider method
				// Looks like: <private static RSACryptoServiceProvider method()>
				if(method.IsPrivate && method.IsStatic
				&& method.ReturnType.FullName.Equals("System.Security.Cryptography.RSACryptoServiceProvider")
				&& method.Parameters.Count == 0)
				{
					rsaProviderMethod = method;
				}

				if (sigCheckMethod != null && rsaProviderMethod != null)
				{
					// If found, replace signature verification body with `return true;`
					sigCheckMethod.Body = CreateReturnTrueBody(sigCheckMethod.Body);
					return true;
				}
			}

			return false;
		}

		/// <summary>
		/// Patch LicKey checks.
		/// </summary>
		/// <param name="type">Type (Class)</param>
		/// <returns>true if patched, false if not</returns>
		private Boolean PatchLicenseKeyChecks(TypeDef type)
		{
			Int32 patchCount = 0;

			IList<MethodDef> methods = type.Methods;
			foreach (var method in methods)
			{
				if (method == null)
					continue;

				// Check for key check method
				// Looks like: <bool method()>
				if (!method.IsStatic
				&& method.ReturnType.FullName.Equals("System.Boolean")
				&& method.Parameters.Count == 1
				&& method.Body.Instructions.Count > 10) // Using instruction count is a bit hacky
				{
					method.Body = CreateReturnTrueBody(method.Body);
					patchCount++;
				}
			}

			return patchCount == 2;
		}

		/// <summary>
		/// Create a method body equivalent to `return true;`.
		/// </summary>
		/// <param name="body">Optional existing method body</param>
		/// <returns>Method body</returns>
		public static CilBody CreateReturnTrueBody(CilBody body = null)
		{
			if (body == null)
				body = new CilBody();
			else
			{
				body.KeepOldMaxStack = true;
				body.Instructions.Clear();
			}

			body.Instructions.Add(OpCodes.Ldc_I4_1.ToInstruction()); // ldc.i4.1
			body.Instructions.Add(OpCodes.Ret.ToInstruction());      // ret

			return body;
		}

		/// <summary>
		/// Get the path to X86 Program Files directory.
		/// </summary>
		public static string ProgramFilesX86
		{
			get
			{
				if (8 == IntPtr.Size
					|| (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
				{
					return Environment.GetEnvironmentVariable("ProgramFiles(x86)");
				}

				return Environment.GetEnvironmentVariable("ProgramFiles");
			}
		}

		/// <summary>
		/// Get the default location of eCtune.EXE if it is installed.
		/// </summary>
		public static String GetDefaultInstalledExePath
		{
			get
			{
				return Path.Combine(Path.Combine(ProgramFilesX86, "eCtune"), "eCtune.EXE");
			}
		}
	}

	/// <summary>
	/// Patch results.
	/// </summary>
	public class PatchResults
	{
		public Boolean SignatureCheck { get; private set; }
		public Boolean KeyCheck { get; private set; }

		public PatchResults(Boolean sigCheck, Boolean keyCheck)
		{
			this.SignatureCheck = sigCheck;
			this.KeyCheck = keyCheck;
		}
	}
}
Posted
Comments
Richard MacCutchan 5-Sep-15 4:09am    
Please edit your question, remove all the code that does not relate to the problem, and add proper details of what you are trying to do and where the error occurs.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900