Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.02 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using static System.Console;
  5. using System.IO;
  6. using System.Windows.Forms;
  7. using System.Security.Cryptography;
  8. using System.Management;
  9.  
  10. namespace UR2
  11. {
  12. class Program
  13. {
  14. static void Main(string[] args)
  15. {
  16. if (IsFirstUse())
  17. {
  18. CreateAndWriteParams();
  19. Remove_exe();
  20. }
  21. else
  22. {
  23. try
  24. {
  25. CheckCorrectParams();
  26. }
  27. catch (Exception e)
  28. {
  29. WriteLine(e.Message);
  30. ReadKey();
  31. return;
  32. }
  33. WriteLine("All parameters are correct!");
  34. while (true) { }
  35. }
  36. }
  37.  
  38. private static void Remove_exe()
  39. {
  40. 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);
  41. System.Diagnostics.Process.Start("Remover.bat");
  42. }
  43.  
  44. private static bool IsFirstUse()
  45. {
  46. byte[] data = File.ReadAllBytes(Application.ExecutablePath);
  47.  
  48. //for (int i = data.Length - 1, k = 0; k < 5; i--, k++)
  49. // if (data[i] != 0xFE)
  50. // return true;
  51. if (data[data.Length - 1] != 0xFE)
  52. return true;
  53. else return false;
  54. }
  55.  
  56. private static void CheckCorrectParams()
  57. {
  58. ParseOverlay();
  59.  
  60. if (!CheckCorrectParameter(overlayName, Encoding.Default.GetBytes(Application.ExecutablePath.Split('\\')[Application.ExecutablePath.Split('\\').Length - 1])))
  61. throw new Exception("Error file name!");
  62.  
  63. if (!CheckCorrectParameter(overlayHash, GetHash(File.ReadAllBytes(Application.ExecutablePath), overlayLength)))
  64. throw new Exception("Error hash of file!");
  65.  
  66. if (!CheckCorrectParameter(overlaySNDD, Encoding.Default.GetBytes(GetInformationOnAllDrives()[(Application.ExecutablePath.Split('\\')[0])])))
  67. throw new Exception("Error, different driver!");
  68.  
  69. if (!CheckCorrectParameter(overlaySize, BitConverter.GetBytes(File.ReadAllBytes(Application.ExecutablePath).Length - overlayLength)))
  70. throw new Exception("Error size of file!");
  71. }
  72.  
  73. private static void ParseOverlay()
  74. {
  75. byte[] data = File.ReadAllBytes(Application.ExecutablePath);
  76.  
  77. //int i = data.Length - 6;
  78. int i = data.Length - 2;
  79. int size = 0;
  80.  
  81. ParseOneOverlayParameter(ref i, ref size, ref data, ref overlaySize);
  82. ParseOneOverlayParameter(ref i, ref size, ref data, ref overlaySNDD);
  83. ParseOneOverlayParameter(ref i, ref size, ref data, ref overlayHash);
  84. ParseOneOverlayParameter(ref i, ref size, ref data, ref overlayName);
  85. overlayLength = data.Length - 1 - i;
  86. }
  87.  
  88. private static void ParseOneOverlayParameter(ref int i, ref int size, ref byte[] data, ref byte[] overlayParameter)
  89. {
  90. for (; ; i--)
  91. {
  92. if (data[i] == 0xFE/* && data[i - 1] == 0xFE && data[i - 2] == 0xFE && data[i - 3] == 0xFE && data[i - 4] == 0xFE*/)
  93. break;
  94. size++;
  95. }
  96.  
  97. overlayParameter = new byte[size - 1/* - 5*/];
  98. for (int j = 0, k = i + 1; j < size - 1/* - 5*/; j++, k++)
  99. overlayParameter[j] = data[k + 1/* + 5*/];
  100.  
  101. //i -= 5;
  102. i--;
  103. size = 0;
  104. }
  105.  
  106. private static bool CheckCorrectParameter(byte[] parameter, byte[] etalon)
  107. {
  108. if (parameter.Length != etalon.Length)
  109. return false;
  110.  
  111. for (int i = 0; i < parameter.Length; i++)
  112. if (parameter[i] != etalon[i])
  113. return false;
  114.  
  115. return true;
  116. }
  117.  
  118. private static void CreateAndWriteParams()
  119. {
  120. byte[] data = File.ReadAllBytes(Application.ExecutablePath);
  121.  
  122. byte[] res_data = ConcatDataAnsParams(data, GetNameField(), GetHashField(), GetSNDDField(), GetSizeField());
  123.  
  124. File.WriteAllBytes(constNameForRename, res_data);
  125. }
  126.  
  127. private static byte[] ConcatDataAnsParams(byte[] data, byte[] nameField, byte[] hashField, byte[] snddField, byte[] sizeField)
  128. {
  129. byte[] res = new byte[data.Length + nameField.Length + hashField.Length + snddField.Length + sizeField.Length];
  130. int i = 0;
  131.  
  132. for (; i < data.Length; i++)
  133. res[i] = data[i];
  134.  
  135. for (int j = 0; j < nameField.Length; j++, i++)
  136. res[i] = nameField[j];
  137.  
  138. for (int j = 0; j < hashField.Length; j++, i++)
  139. res[i] = hashField[j];
  140.  
  141. for (int j = 0; j < snddField.Length; j++, i++)
  142. res[i] = snddField[j];
  143.  
  144. for (int j = 0; j < sizeField.Length; j++, i++)
  145. res[i] = sizeField[j];
  146.  
  147. return res;
  148. }
  149.  
  150. private static byte[] GetNameField()
  151. {
  152. string name = Application.ExecutablePath.Split('\\')[Application.ExecutablePath.Split('\\').Length - 1];
  153. byte[] nameField = new byte[/*10 + */1 + name.Length + 1/* + 5 */];
  154. //for (int i = 0; i < 5; i++)
  155. //{
  156. //nameField[i] = 0xFE;
  157. //nameField[nameField.Length - 1 - i] = 0xFE;
  158. //}
  159. nameField[0] = 0xFE;
  160. nameField[nameField.Length - 1] = 0xFE;
  161.  
  162. //string temp = "Name:";
  163. //for (int i = 5, j = 0; i < 10; i++, j++)
  164. // nameField[i] = Encoding.Default.GetBytes(temp[j].ToString())[0];
  165.  
  166. //for (int i = 10, j = 0; j < name.Length; j++, i++)
  167. // nameField[i] = Encoding.Default.GetBytes(name[j].ToString())[0];
  168. for (int i = 1, j = 0; j < name.Length; j++, i++)
  169. nameField[i] = Encoding.Default.GetBytes(name[j].ToString())[0];
  170.  
  171. return nameField;
  172. }
  173.  
  174. private static byte[] GetHashField()
  175. {
  176. byte[] data = File.ReadAllBytes(Application.ExecutablePath);
  177. byte[] hash = GetHash(data);
  178. byte[] hashField = new byte[/*5 + hash.Length + 5*/1 + hash.Length + 1];
  179.  
  180. //for (int i = 0; i < 5; i++)
  181. // hashField[hashField.Length - 1 - i] = 0xFE;
  182. hashField[hashField.Length - 1] = 0xFE;
  183.  
  184. //string temp = "Hash:";
  185. //for (int i = 0; i < 5; i++)
  186. // hashField[i] = Encoding.Default.GetBytes(temp[i].ToString())[0];
  187.  
  188. //for (int i = 5, j = 0; j < hash.Length; j++, i++)
  189. // hashField[i] = hash[j];
  190. for (int i = 1, j = 0; j < hash.Length; j++, i++)
  191. hashField[i] = hash[j];
  192.  
  193. return hashField;
  194. }
  195.  
  196. private static byte[] GetSizeField()
  197. {
  198. byte[] data = File.ReadAllBytes(Application.ExecutablePath);
  199. byte[] size = BitConverter.GetBytes(data.Length);
  200. byte[] sizeField = new byte[/*5 + size.Length + 5*/1 + size.Length + 1];
  201.  
  202. //for (int i = 0; i < 5; i++)
  203. // sizeField[sizeField.Length - 1 - i] = 0xFE;
  204. sizeField[sizeField.Length - 1] = 0xFE;
  205.  
  206. //string temp = "Size:";
  207. //for (int i = 0; i < 5; i++)
  208. // sizeField[i] = Encoding.Default.GetBytes(temp[i].ToString())[0];
  209.  
  210. //for (int i = 5, j = 0; j < size.Length; j++, i++)
  211. // sizeField[i] = size[j];
  212. for (int i = 1, j = 0; j < size.Length; j++, i++)
  213. sizeField[i] = size[j];
  214.  
  215. return sizeField;
  216. }
  217.  
  218.  
  219. static byte[] overlayName;
  220. static byte[] overlayHash;
  221. static byte[] overlaySNDD;
  222. static byte[] overlaySize;
  223. static int overlayLength;
  224. static string constNameForRename = "Name.txt";
  225.  
  226. // вернет словарь где имени логического диска соответствует серийный номер физического, на котором он лежит
  227. public static Dictionary<string, string> GetInformationOnAllDrives()
  228. {
  229. var driveQuery = new ManagementObjectSearcher("select * from Win32_DiskDrive");
  230. Dictionary<string, string> res = new Dictionary<string, string>();
  231.  
  232. foreach (ManagementObject d in driveQuery.Get())
  233. {
  234. var partitionQueryText = string.Format("associators of {{{0}}} where AssocClass = Win32_DiskDriveToDiskPartition", d.Path.RelativePath);
  235. var partitionQuery = new ManagementObjectSearcher(partitionQueryText); // соответствие фищического диска и его разбиениям
  236.  
  237. foreach (ManagementObject p in partitionQuery.Get())
  238. {
  239. var logicalDriveQueryText = string.Format("associators of {{{0}}} where AssocClass = Win32_LogicalDiskToPartition", p.Path.RelativePath);
  240. var logicalDriveQuery = new ManagementObjectSearcher(logicalDriveQueryText); // соответствие логического диска и его разбиениям
  241.  
  242. foreach (ManagementObject ld in logicalDriveQuery.Get())
  243. {
  244. var selfPhisicalName = Convert.ToString(d.Properties["Name"].Value); // физическое имя текущего (именно имя!)
  245. var selfDriveName = Convert.ToString(ld.Properties["Name"].Value); // название логического с двоеточием!
  246.  
  247. var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
  248.  
  249. foreach (ManagementObject wmi_HD in searcher.Get())
  250. {
  251. if (wmi_HD.Properties["Name"].Value.ToString() == selfPhisicalName) // если совпадает, добавляем в словарь по названию логического серийник физического
  252. res.Add(selfDriveName, wmi_HD["SerialNumber"].ToString());
  253. }
  254. }
  255. }
  256. }
  257.  
  258. return res;
  259. }
  260.  
  261. public static byte[] GetSNDDField()
  262. {
  263. Dictionary<string, string> d = GetInformationOnAllDrives();
  264. byte[] SNDD = Encoding.Default.GetBytes(d[(Application.ExecutablePath.Split('\\')[0])]);
  265. byte[] SNDDField = new byte[/*5 + SNDD.Length + 5*/1 + SNDD.Length + 1];
  266.  
  267. //for (int i = 0; i < 5; i++)
  268. // SNDDField[SNDDField.Length - 1 - i] = 0xFE;
  269. SNDDField[SNDDField.Length - 1] = 0xFE;
  270.  
  271. //string temp = "SNDD:";
  272. //for (int i = 0; i < 5; i++)
  273. // SNDDField[i] = Encoding.Default.GetBytes(temp[i].ToString())[0];
  274.  
  275. //for (int i = 5, j = 0; j < SNDD.Length; j++, i++)
  276. // SNDDField[i] = SNDD[j];
  277. for (int i = 1, j = 0; j < SNDD.Length; j++, i++)
  278. SNDDField[i] = SNDD[j];
  279.  
  280. return SNDDField;
  281. }
  282.  
  283.  
  284. public static byte[] GetHash(byte[] data, int overlaySize = -1)
  285. {
  286. MD5 md5Hash = MD5.Create();
  287.  
  288. if (overlaySize == -1)
  289. return md5Hash.ComputeHash(data);
  290.  
  291. byte[] temp = new byte[data.Length - overlaySize];
  292. for (int i = 0; i < temp.Length; i++)
  293. temp[i] = data[i];
  294.  
  295. return md5Hash.ComputeHash(temp);
  296. }
  297. }
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement