Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Collections.Generic;
- using static System.Console;
- using System.IO;
- using System.Windows.Forms;
- using System.Security.Cryptography;
- using System.Management;
- namespace UR2
- {
- class Program
- {
- static void Main(string[] args)
- {
- if (IsFirstUse())
- {
- CreateAndWriteParams();
- Remove_exe();
- }
- else
- {
- try
- {
- CheckCorrectParams();
- }
- catch (Exception e)
- {
- WriteLine(e.Message);
- ReadKey();
- return;
- }
- WriteLine("All parameters are correct!");
- while (true) { }
- }
- }
- private static void Remove_exe()
- {
- File.WriteAllLines("Remover.bat", new string[] { "CHCP 1251", "sleep 1", "del \"" + Application.ExecutablePath.Split('\\')[Application.ExecutablePath.Split('\\').Length - 1] + "\"", "rename " + constNameForRename + " \"" + Application.ExecutablePath.Split('\\')[Application.ExecutablePath.Split('\\').Length - 1] + "\"", "del Remover.bat" }, Encoding.Default);
- System.Diagnostics.Process.Start("Remover.bat");
- }
- private static bool IsFirstUse()
- {
- byte[] data = File.ReadAllBytes(Application.ExecutablePath);
- //for (int i = data.Length - 1, k = 0; k < 5; i--, k++)
- // if (data[i] != 0xFE)
- // return true;
- if (data[data.Length - 1] != 0xFE)
- return true;
- else return false;
- }
- private static void CheckCorrectParams()
- {
- ParseOverlay();
- if (!CheckCorrectParameter(overlayName, Encoding.Default.GetBytes(Application.ExecutablePath.Split('\\')[Application.ExecutablePath.Split('\\').Length - 1])))
- throw new Exception("Error file name!");
- if (!CheckCorrectParameter(overlayHash, GetHash(File.ReadAllBytes(Application.ExecutablePath), overlayLength)))
- throw new Exception("Error hash of file!");
- if (!CheckCorrectParameter(overlaySNDD, Encoding.Default.GetBytes(GetInformationOnAllDrives()[(Application.ExecutablePath.Split('\\')[0])])))
- throw new Exception("Error, different driver!");
- if (!CheckCorrectParameter(overlaySize, BitConverter.GetBytes(File.ReadAllBytes(Application.ExecutablePath).Length - overlayLength)))
- throw new Exception("Error size of file!");
- }
- private static void ParseOverlay()
- {
- byte[] data = File.ReadAllBytes(Application.ExecutablePath);
- //int i = data.Length - 6;
- int i = data.Length - 2;
- int size = 0;
- ParseOneOverlayParameter(ref i, ref size, ref data, ref overlaySize);
- ParseOneOverlayParameter(ref i, ref size, ref data, ref overlaySNDD);
- ParseOneOverlayParameter(ref i, ref size, ref data, ref overlayHash);
- ParseOneOverlayParameter(ref i, ref size, ref data, ref overlayName);
- overlayLength = data.Length - 1 - i;
- }
- private static void ParseOneOverlayParameter(ref int i, ref int size, ref byte[] data, ref byte[] overlayParameter)
- {
- for (; ; i--)
- {
- if (data[i] == 0xFE/* && data[i - 1] == 0xFE && data[i - 2] == 0xFE && data[i - 3] == 0xFE && data[i - 4] == 0xFE*/)
- break;
- size++;
- }
- overlayParameter = new byte[size - 1/* - 5*/];
- for (int j = 0, k = i + 1; j < size - 1/* - 5*/; j++, k++)
- overlayParameter[j] = data[k + 1/* + 5*/];
- //i -= 5;
- i--;
- size = 0;
- }
- private static bool CheckCorrectParameter(byte[] parameter, byte[] etalon)
- {
- if (parameter.Length != etalon.Length)
- return false;
- for (int i = 0; i < parameter.Length; i++)
- if (parameter[i] != etalon[i])
- return false;
- return true;
- }
- private static void CreateAndWriteParams()
- {
- byte[] data = File.ReadAllBytes(Application.ExecutablePath);
- byte[] res_data = ConcatDataAnsParams(data, GetNameField(), GetHashField(), GetSNDDField(), GetSizeField());
- File.WriteAllBytes(constNameForRename, res_data);
- }
- private static byte[] ConcatDataAnsParams(byte[] data, byte[] nameField, byte[] hashField, byte[] snddField, byte[] sizeField)
- {
- byte[] res = new byte[data.Length + nameField.Length + hashField.Length + snddField.Length + sizeField.Length];
- int i = 0;
- for (; i < data.Length; i++)
- res[i] = data[i];
- for (int j = 0; j < nameField.Length; j++, i++)
- res[i] = nameField[j];
- for (int j = 0; j < hashField.Length; j++, i++)
- res[i] = hashField[j];
- for (int j = 0; j < snddField.Length; j++, i++)
- res[i] = snddField[j];
- for (int j = 0; j < sizeField.Length; j++, i++)
- res[i] = sizeField[j];
- return res;
- }
- private static byte[] GetNameField()
- {
- string name = Application.ExecutablePath.Split('\\')[Application.ExecutablePath.Split('\\').Length - 1];
- byte[] nameField = new byte[/*10 + */1 + name.Length + 1/* + 5 */];
- //for (int i = 0; i < 5; i++)
- //{
- //nameField[i] = 0xFE;
- //nameField[nameField.Length - 1 - i] = 0xFE;
- //}
- nameField[0] = 0xFE;
- nameField[nameField.Length - 1] = 0xFE;
- //string temp = "Name:";
- //for (int i = 5, j = 0; i < 10; i++, j++)
- // nameField[i] = Encoding.Default.GetBytes(temp[j].ToString())[0];
- //for (int i = 10, j = 0; j < name.Length; j++, i++)
- // nameField[i] = Encoding.Default.GetBytes(name[j].ToString())[0];
- for (int i = 1, j = 0; j < name.Length; j++, i++)
- nameField[i] = Encoding.Default.GetBytes(name[j].ToString())[0];
- return nameField;
- }
- private static byte[] GetHashField()
- {
- byte[] data = File.ReadAllBytes(Application.ExecutablePath);
- byte[] hash = GetHash(data);
- byte[] hashField = new byte[/*5 + hash.Length + 5*/1 + hash.Length + 1];
- //for (int i = 0; i < 5; i++)
- // hashField[hashField.Length - 1 - i] = 0xFE;
- hashField[hashField.Length - 1] = 0xFE;
- //string temp = "Hash:";
- //for (int i = 0; i < 5; i++)
- // hashField[i] = Encoding.Default.GetBytes(temp[i].ToString())[0];
- //for (int i = 5, j = 0; j < hash.Length; j++, i++)
- // hashField[i] = hash[j];
- for (int i = 1, j = 0; j < hash.Length; j++, i++)
- hashField[i] = hash[j];
- return hashField;
- }
- private static byte[] GetSizeField()
- {
- byte[] data = File.ReadAllBytes(Application.ExecutablePath);
- byte[] size = BitConverter.GetBytes(data.Length);
- byte[] sizeField = new byte[/*5 + size.Length + 5*/1 + size.Length + 1];
- //for (int i = 0; i < 5; i++)
- // sizeField[sizeField.Length - 1 - i] = 0xFE;
- sizeField[sizeField.Length - 1] = 0xFE;
- //string temp = "Size:";
- //for (int i = 0; i < 5; i++)
- // sizeField[i] = Encoding.Default.GetBytes(temp[i].ToString())[0];
- //for (int i = 5, j = 0; j < size.Length; j++, i++)
- // sizeField[i] = size[j];
- for (int i = 1, j = 0; j < size.Length; j++, i++)
- sizeField[i] = size[j];
- return sizeField;
- }
- static byte[] overlayName;
- static byte[] overlayHash;
- static byte[] overlaySNDD;
- static byte[] overlaySize;
- static int overlayLength;
- static string constNameForRename = "Name.txt";
- // вернет словарь где имени логического диска соответствует серийный номер физического, на котором он лежит
- public static Dictionary<string, string> GetInformationOnAllDrives()
- {
- var driveQuery = new ManagementObjectSearcher("select * from Win32_DiskDrive");
- Dictionary<string, string> res = new Dictionary<string, string>();
- foreach (ManagementObject d in driveQuery.Get())
- {
- var partitionQueryText = string.Format("associators of {{{0}}} where AssocClass = Win32_DiskDriveToDiskPartition", d.Path.RelativePath);
- var partitionQuery = new ManagementObjectSearcher(partitionQueryText); // соответствие фищического диска и его разбиениям
- foreach (ManagementObject p in partitionQuery.Get())
- {
- var logicalDriveQueryText = string.Format("associators of {{{0}}} where AssocClass = Win32_LogicalDiskToPartition", p.Path.RelativePath);
- var logicalDriveQuery = new ManagementObjectSearcher(logicalDriveQueryText); // соответствие логического диска и его разбиениям
- foreach (ManagementObject ld in logicalDriveQuery.Get())
- {
- var selfPhisicalName = Convert.ToString(d.Properties["Name"].Value); // физическое имя текущего (именно имя!)
- var selfDriveName = Convert.ToString(ld.Properties["Name"].Value); // название логического с двоеточием!
- var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
- foreach (ManagementObject wmi_HD in searcher.Get())
- {
- if (wmi_HD.Properties["Name"].Value.ToString() == selfPhisicalName) // если совпадает, добавляем в словарь по названию логического серийник физического
- res.Add(selfDriveName, wmi_HD["SerialNumber"].ToString());
- }
- }
- }
- }
- return res;
- }
- public static byte[] GetSNDDField()
- {
- Dictionary<string, string> d = GetInformationOnAllDrives();
- byte[] SNDD = Encoding.Default.GetBytes(d[(Application.ExecutablePath.Split('\\')[0])]);
- byte[] SNDDField = new byte[/*5 + SNDD.Length + 5*/1 + SNDD.Length + 1];
- //for (int i = 0; i < 5; i++)
- // SNDDField[SNDDField.Length - 1 - i] = 0xFE;
- SNDDField[SNDDField.Length - 1] = 0xFE;
- //string temp = "SNDD:";
- //for (int i = 0; i < 5; i++)
- // SNDDField[i] = Encoding.Default.GetBytes(temp[i].ToString())[0];
- //for (int i = 5, j = 0; j < SNDD.Length; j++, i++)
- // SNDDField[i] = SNDD[j];
- for (int i = 1, j = 0; j < SNDD.Length; j++, i++)
- SNDDField[i] = SNDD[j];
- return SNDDField;
- }
- public static byte[] GetHash(byte[] data, int overlaySize = -1)
- {
- MD5 md5Hash = MD5.Create();
- if (overlaySize == -1)
- return md5Hash.ComputeHash(data);
- byte[] temp = new byte[data.Length - overlaySize];
- for (int i = 0; i < temp.Length; i++)
- temp[i] = data[i];
- return md5Hash.ComputeHash(temp);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement