// Decompiled with JetBrains decompiler
// Type: Office_2010_Toolkit.Logic.KMSEditor
// Assembly: AutoKMS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: D1CED9E3-1FC7-49B8-B3AC-44976AB7F6E4
// Assembly location: C:\Users\Administrateur\Downloads\Virusshare-00000-msil\HackTool.Win32.KMSAuto.i-f317ba4d4051fad64a0aa45b587fa3dcea795bac30acec2872779abe31a07cbe.exe

using ProcessMemoryReaderLib;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace Office_2010_Toolkit.Logic
{
  internal class KMSEditor
  {
    public static Stream ConvertByteArrayToStream(byte[] input) => (Stream) new MemoryStream(input);

    public static byte[] StrToByteArray(string str) => Encoding.Unicode.GetBytes(str);

    public static string ReadPIDFromByteArray(byte[] KMSEmulator)
    {
      Stream stream = KMSEditor.ConvertByteArrayToStream(KMSEmulator);
      byte[] bytes = new byte[48];
      int index = 0;
      for (stream.Position = 136932L; stream.Position != 137028L; ++stream.Position)
      {
        bytes[index] = (byte) stream.ReadByte();
        ++index;
      }
      return Encoding.ASCII.GetString(bytes);
    }

    public static string ReadPIDFromFile(string path)
    {
      byte[] bytes = new byte[48];
      int index = 0;
      FileStream fileStream1;
      using (FileStream fileStream2 = new FileStream(path, FileMode.Open, FileAccess.ReadWrite))
      {
        for (fileStream2.Position = 136932L; fileStream2.Position != 137028L; ++fileStream1.Position)
        {
          bytes[index] = (byte) fileStream2.ReadByte();
          ++index;
          fileStream1 = fileStream2;
        }
      }
      return Encoding.ASCII.GetString(bytes);
    }

    public static string ReadPIDFromMemory()
    {
      int bytesRead = 0;
      ProcessMemoryReader processMemoryReader = new ProcessMemoryReader();
      Process process = (Process) null;
      Process[] processes = Process.GetProcesses();
      IntPtr MemoryAddress = (IntPtr) 4331234;
      for (int index = 0; index < processes.Length; ++index)
      {
        if (processes[index].ProcessName == "KMSEmulator")
          process = processes[index];
      }
      processMemoryReader.ReadProcess = process;
      processMemoryReader.OpenProcess();
      return Encoding.Unicode.GetString(processMemoryReader.ReadProcessMemory(MemoryAddress, 98U, out bytesRead));
    }

    public static void WritePIDToMemory(string PID)
    {
      int bytesWritten = 0;
      ProcessMemoryReader processMemoryReader = new ProcessMemoryReader();
      Process process = (Process) null;
      Process[] processes = Process.GetProcesses();
      IntPtr MemoryAddress = (IntPtr) 4331236;
      byte[] byteArray = KMSEditor.StrToByteArray(PID);
      for (int index = 0; index < processes.Length; ++index)
      {
        if (processes[index].ProcessName == "KMSEmulator")
          process = processes[index];
      }
      processMemoryReader.ReadProcess = process;
      processMemoryReader.OpenProcess();
      processMemoryReader.WriteProcessMemory(MemoryAddress, byteArray, out bytesWritten);
    }

    public static void WritePIDToFile(string path, string PID)
    {
      int index = 0;
      FileStream fileStream1;
      using (FileStream fileStream2 = new FileStream(path, FileMode.Open, FileAccess.ReadWrite))
      {
        for (fileStream2.Position = 136932L; fileStream2.Position != 137028L; ++fileStream1.Position)
        {
          fileStream2.WriteByte((byte) PID[index]);
          ++index;
          fileStream1 = fileStream2;
        }
      }
    }
  }
}
