Co-authored-by: InvoxiPlayGames <22731889+InvoxiPlayGames@users.noreply.github.com>
276 lines
11 KiB
C#
276 lines
11 KiB
C#
namespace LibTSforge
|
|
{
|
|
using Microsoft.Win32;
|
|
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.ServiceProcess;
|
|
using System.Text;
|
|
using LibTSforge.Crypto;
|
|
using LibTSforge.PhysicalStore;
|
|
using LibTSforge.SPP;
|
|
using LibTSforge.TokenStore;
|
|
|
|
public enum PSVersion
|
|
{
|
|
Vista,
|
|
Win7,
|
|
Win8Early,
|
|
Win8,
|
|
WinBlue,
|
|
WinModern
|
|
}
|
|
|
|
public static class Constants
|
|
{
|
|
public static readonly byte[] UniversalHWIDBlock =
|
|
{
|
|
0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x01, 0x0c, 0x01, 0x00
|
|
};
|
|
|
|
public static readonly byte[] KMSv4Response =
|
|
{
|
|
0x00, 0x00, 0x04, 0x00, 0x62, 0x00, 0x00, 0x00, 0x30, 0x00, 0x35, 0x00, 0x34, 0x00, 0x32, 0x00,
|
|
0x36, 0x00, 0x2D, 0x00, 0x30, 0x00, 0x30, 0x00, 0x32, 0x00, 0x30, 0x00, 0x36, 0x00, 0x2D, 0x00,
|
|
0x31, 0x00, 0x36, 0x00, 0x31, 0x00, 0x2D, 0x00, 0x36, 0x00, 0x35, 0x00, 0x35, 0x00, 0x35, 0x00,
|
|
0x30, 0x00, 0x36, 0x00, 0x2D, 0x00, 0x30, 0x00, 0x33, 0x00, 0x2D, 0x00, 0x31, 0x00, 0x30, 0x00,
|
|
0x33, 0x00, 0x33, 0x00, 0x2D, 0x00, 0x39, 0x00, 0x32, 0x00, 0x30, 0x00, 0x30, 0x00, 0x2E, 0x00,
|
|
0x30, 0x00, 0x30, 0x00, 0x30, 0x00, 0x30, 0x00, 0x2D, 0x00, 0x30, 0x00, 0x36, 0x00, 0x35, 0x00,
|
|
0x32, 0x00, 0x30, 0x00, 0x31, 0x00, 0x33, 0x00, 0x00, 0x00, 0xDE, 0x19, 0x02, 0xCF, 0x1F, 0x35,
|
|
0x97, 0x4E, 0x8A, 0x8F, 0xB8, 0x07, 0xB1, 0x92, 0xB5, 0xB5, 0x97, 0x42, 0xEC, 0x3A, 0x76, 0x84,
|
|
0xD5, 0x01, 0x32, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x60, 0x27, 0x00, 0x00, 0xC4, 0x1E,
|
|
0xAA, 0x8B, 0xDD, 0x0C, 0xAB, 0x55, 0x6A, 0xCE, 0xAF, 0xAC, 0x7F, 0x5F, 0xBD, 0xE9
|
|
};
|
|
|
|
public static readonly byte[] KMSv5Response =
|
|
{
|
|
0x00, 0x00, 0x05, 0x00, 0xBE, 0x96, 0xF9, 0x04, 0x54, 0x17, 0x3F, 0xAF, 0xE3, 0x08, 0x50, 0xEB,
|
|
0x22, 0xBA, 0x53, 0xBF, 0xF2, 0x6A, 0x7B, 0xC9, 0x05, 0x1D, 0xB5, 0x19, 0xDF, 0x98, 0xE2, 0x71,
|
|
0x4D, 0x00, 0x61, 0xE9, 0x9D, 0x03, 0xFB, 0x31, 0xF9, 0x1F, 0x2E, 0x60, 0x59, 0xC7, 0x73, 0xC8,
|
|
0xE8, 0xB6, 0xE1, 0x2B, 0x39, 0xC6, 0x35, 0x0E, 0x68, 0x7A, 0xAA, 0x4F, 0x28, 0x23, 0x12, 0x18,
|
|
0xE3, 0xAA, 0x84, 0x81, 0x6E, 0x82, 0xF0, 0x3F, 0xD9, 0x69, 0xA9, 0xDF, 0xBA, 0x5F, 0xCA, 0x32,
|
|
0x54, 0xB2, 0x52, 0x3B, 0x3E, 0x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8, 0x9C, 0x4C, 0x56, 0xB3, 0x57, 0x44, 0x62, 0x68,
|
|
0x8D, 0xAA, 0x40, 0x11, 0x9D, 0x84, 0x62, 0x0E, 0x43, 0x8A, 0x1D, 0xF0, 0x1C, 0x49, 0xD8, 0x56,
|
|
0xEF, 0x4C, 0xD3, 0x64, 0xBA, 0x0D, 0xEF, 0x87, 0xB5, 0x2C, 0x88, 0xF3, 0x18, 0xFF, 0x3A, 0x8C,
|
|
0xF5, 0xA6, 0x78, 0x5C, 0x62, 0xE3, 0x9E, 0x4C, 0xB6, 0x31, 0x2D, 0x06, 0x80, 0x92, 0xBC, 0x2E,
|
|
0x92, 0xA6, 0x56, 0x96
|
|
};
|
|
|
|
// 2^31 - 1 minutes
|
|
public static ulong TimerMax = (ulong)TimeSpan.FromMinutes(2147483647).Ticks;
|
|
|
|
public static readonly string ZeroCID = new string('0', 48);
|
|
}
|
|
|
|
public static class BinaryReaderExt
|
|
{
|
|
public static void Align(this BinaryReader reader, int to)
|
|
{
|
|
int pos = (int)reader.BaseStream.Position;
|
|
reader.BaseStream.Seek(-pos & (to - 1), SeekOrigin.Current);
|
|
}
|
|
|
|
public static string ReadNullTerminatedString(this BinaryReader reader, int maxLen)
|
|
{
|
|
return Encoding.Unicode.GetString(reader.ReadBytes(maxLen)).Split(new char[] { '\0' }, 2)[0];
|
|
}
|
|
}
|
|
|
|
public static class BinaryWriterExt
|
|
{
|
|
public static void Align(this BinaryWriter writer, int to)
|
|
{
|
|
int pos = (int)writer.BaseStream.Position;
|
|
writer.WritePadding(-pos & (to - 1));
|
|
}
|
|
|
|
public static void WritePadding(this BinaryWriter writer, int len)
|
|
{
|
|
writer.Write(Enumerable.Repeat((byte)0, len).ToArray());
|
|
}
|
|
|
|
public static void WriteFixedString(this BinaryWriter writer, string str, int bLen)
|
|
{
|
|
writer.Write(Encoding.ASCII.GetBytes(str));
|
|
writer.WritePadding(bLen - str.Length);
|
|
}
|
|
|
|
public static void WriteFixedString16(this BinaryWriter writer, string str, int bLen)
|
|
{
|
|
byte[] bstr = Utils.EncodeString(str);
|
|
writer.Write(bstr);
|
|
writer.WritePadding(bLen - bstr.Length);
|
|
}
|
|
|
|
public static byte[] GetBytes(this BinaryWriter writer)
|
|
{
|
|
return ((MemoryStream)writer.BaseStream).ToArray();
|
|
}
|
|
}
|
|
|
|
public static class ByteArrayExt
|
|
{
|
|
public static byte[] CastToArray<T>(this T data) where T : struct
|
|
{
|
|
int size = Marshal.SizeOf(typeof(T));
|
|
byte[] result = new byte[size];
|
|
GCHandle handle = GCHandle.Alloc(result, GCHandleType.Pinned);
|
|
try
|
|
{
|
|
Marshal.StructureToPtr(data, handle.AddrOfPinnedObject(), false);
|
|
}
|
|
finally
|
|
{
|
|
handle.Free();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static T CastToStruct<T>(this byte[] data) where T : struct
|
|
{
|
|
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
|
|
try
|
|
{
|
|
IntPtr ptr = handle.AddrOfPinnedObject();
|
|
return (T)Marshal.PtrToStructure(ptr, typeof(T));
|
|
}
|
|
finally
|
|
{
|
|
handle.Free();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class FileStreamExt
|
|
{
|
|
public static byte[] ReadAllBytes(this FileStream fs)
|
|
{
|
|
BinaryReader br = new BinaryReader(fs);
|
|
return br.ReadBytes((int)fs.Length);
|
|
}
|
|
|
|
public static void WriteAllBytes(this FileStream fs, byte[] data)
|
|
{
|
|
fs.Seek(0, SeekOrigin.Begin);
|
|
fs.SetLength(data.Length);
|
|
fs.Write(data, 0, data.Length);
|
|
}
|
|
}
|
|
|
|
public static class Utils
|
|
{
|
|
public static string DecodeString(byte[] data)
|
|
{
|
|
return Encoding.Unicode.GetString(data).Trim('\0');
|
|
}
|
|
|
|
public static byte[] EncodeString(string str)
|
|
{
|
|
return Encoding.Unicode.GetBytes(str + '\0');
|
|
}
|
|
|
|
[DllImport("kernel32.dll")]
|
|
public static extern uint GetSystemDefaultLCID();
|
|
|
|
public static uint CRC32(byte[] data)
|
|
{
|
|
const uint polynomial = 0x04C11DB7;
|
|
uint crc = 0xffffffff;
|
|
|
|
foreach (byte b in data)
|
|
{
|
|
crc ^= (uint)b << 24;
|
|
for (int bit = 0; bit < 8; bit++)
|
|
{
|
|
if ((crc & 0x80000000) != 0)
|
|
{
|
|
crc = (crc << 1) ^ polynomial;
|
|
}
|
|
else
|
|
{
|
|
crc <<= 1;
|
|
}
|
|
}
|
|
}
|
|
return ~crc;
|
|
}
|
|
|
|
public static string GetArchitecture()
|
|
{
|
|
string arch = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE", EnvironmentVariableTarget.Machine).ToUpperInvariant();
|
|
return arch == "AMD64" ? "X64" : arch;
|
|
}
|
|
|
|
public static PSVersion DetectVersion()
|
|
{
|
|
int build = Environment.OSVersion.Version.Build;
|
|
|
|
if (build >= 9600) return PSVersion.WinModern;
|
|
if (build >= 6000 && build <= 6003) return PSVersion.Vista;
|
|
if (build >= 7600 && build <= 7602) return PSVersion.Win7;
|
|
if (build == 9200) return PSVersion.Win8;
|
|
|
|
throw new NotSupportedException("Unable to auto-detect version info");
|
|
}
|
|
|
|
public static bool DetectCurrentKey()
|
|
{
|
|
SLApi.RefreshLicenseStatus();
|
|
|
|
using (RegistryKey wpaKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\WPA"))
|
|
{
|
|
foreach (string subKey in wpaKey.GetSubKeyNames())
|
|
{
|
|
if (subKey.StartsWith("8DEC0AF1"))
|
|
{
|
|
return subKey.Contains("P");
|
|
}
|
|
}
|
|
}
|
|
|
|
throw new FileNotFoundException("Failed to autodetect key type, specify physical store key with /prod or /test arguments.");
|
|
}
|
|
}
|
|
|
|
public static class Logger
|
|
{
|
|
public static bool HideOutput = false;
|
|
|
|
public static void WriteLine(string line)
|
|
{
|
|
if (!HideOutput) Console.WriteLine(line);
|
|
}
|
|
}
|
|
}
|