Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.53 KB | None | 0 0
  1. #if !defined(InviDropper)
  2. #include <direct.h>
  3. #endif
  4. #include <Windows.h>
  5. #if !defined(InviDropper)
  6. #include <stdio.h>
  7. #include <Shlwapi.h>
  8. #include <Psapi.h>
  9. #endif
  10.  
  11. char TBD[256];
  12. #if !defined(InviDropper)
  13. #include <algorithm>
  14. #include <string>
  15. #include <regex>
  16.  
  17. EXTERN_C IMAGE_DOS_HEADER __ImageBase;
  18.  
  19. char IniPath[2048], IniFullPath[2048];
  20. char ServerName[2048], WorkingDir[2048];
  21. int iClientMod;
  22. HANDLE hProcess;
  23. DWORD ProcessID;
  24.  
  25. std::string GetEditText(HWND hControl);
  26. #endif
  27. std::string to_hexstring(unsigned int _Val);
  28.  
  29.  
  30. __forceinline bool DataCompare(const unsigned char* address, const unsigned char* signature, const char* mask);
  31. DWORD FindPatternInternal(const unsigned char* address, size_t size, const unsigned char* signature, const char* mask, unsigned short ignore);
  32. DWORD FindPattern(const unsigned char* address, size_t size, const unsigned char* signature, const char* mask, unsigned short ignore);
  33. DWORD RelativeToAbsolute(DWORD Offset);
  34.  
  35. #if !defined(InviDropper)
  36. BOOL CheckIni(BOOL Loading);
  37. bool ForceValidation(bool bAsk);
  38. bool ValidateIni();
  39. void SetNewIniLocation(char OldLocation[2048]);
  40. #endif
  41. void PrintDbg(std::string sText, std::string sTitle);
  42. void InitComponents();
  43. void SetAddies();
  44.  
  45. #include "..\..\Classes\GUI Class.h"
  46. #include "..\..\Classes\Metin2 Class.h"
  47. #include "GUI.h"
  48.  
  49. #if !defined(InviDropper)
  50. #pragma comment(lib, "Shlwapi.lib")
  51. #pragma comment(lib, "Psapi.lib")
  52. #endif
  53. #pragma comment(lib, "Version.lib")
  54.  
  55. #if (!XP)
  56. #pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
  57. #endif
  58.  
  59. __declspec(dllexport) BOOL WINAPI Init()
  60. {
  61. return 0;
  62. }
  63.  
  64. std::string to_hexstring(unsigned int _Val)
  65. {
  66. char _Buf[2 * _MAX_INT_DIG];
  67. _CSTD _TOSTRING(_Buf, "%x", _Val);
  68. return (std::string(_Buf));
  69. }
  70.  
  71. #if (DbgSession)
  72. void PrintDbg(std::string sText, std::string sTitle)
  73. {
  74. MessageBox(0, sText.c_str(), sTitle.c_str(), MB_ICONASTERISK);
  75. return;
  76. }
  77. #endif
  78.  
  79. #if !defined(InviDropper)
  80. std::string GetEditText(HWND hControl)
  81. {
  82. char tValue[2048];
  83. GetWindowText(hControl, tValue, 2048);
  84. return (std::string)tValue;
  85. }
  86.  
  87. BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lparam)
  88. {
  89. char *toIgnore[] = {"", "DIEmWin"};
  90. char buffer[256];
  91. DWORD id = 0;
  92. GetWindowThreadProcessId(hwnd, &id);
  93. if (ProcessID == id)
  94. {
  95. GetWindowText(hwnd, buffer, 255);
  96. for (int i = 0; i < _countof(toIgnore);i++)
  97. {
  98. if (StrCmp(buffer, toIgnore[i]) == 0)
  99. return true;
  100.  
  101. }
  102. GetWindowText(hwnd, ServerName, 2048);
  103. return false;
  104. }
  105.  
  106. return true;
  107. }
  108.  
  109. bool GetServerNameByModule()
  110. {
  111. //forbidden
  112. const char *Replacements[] = {"metin2", "mt2", " ", ",", "64bit", "client", "starter", "vista", "win7", "win8", "-"};
  113. std::regex e = (std::regex)"^\\S{3,}mt\\d?$";
  114. //bool bReplaced = false;
  115. std::string PName, tPName;
  116. std::smatch m;
  117.  
  118. GetModuleFileName(0, ServerName, 2048);
  119. PathStripPath(ServerName);
  120. PathRemoveExtension(ServerName);
  121.  
  122. PName = ServerName;
  123. sprintf_s(TBD, "M-%s", PName.c_str());
  124. tPName = PName;
  125. std::transform(tPName.begin(), tPName.end(), tPName.begin(), tolower);
  126. if (std::regex_search(PName, m, e))
  127. {
  128. sprintf_s(ServerName, 2048, "%s\0", PName.c_str());
  129. sprintf_s(TBD, "#M-%s", PName.c_str());
  130. return true;
  131. }
  132. if (tPName.find(Replacements[0]) == std::string::npos && tPName.find(Replacements[1]) == std::string::npos)
  133. {
  134. for (int i = 0;i < _countof(Replacements);i++)
  135. {
  136. if (tPName.length())
  137. {
  138. int Pos = tPName.find(Replacements[i]);
  139. if (Pos != std::string::npos)
  140. {
  141. if (i == _countof(Replacements) - 1 && Pos != PName.length() - 1)//last replacement and not last pos
  142. break;
  143. PName.erase(Pos, strlen(Replacements[i]));
  144. tPName = PName;
  145. std::transform(tPName.begin(), tPName.end(), tPName.begin(), tolower);
  146. //bReplaced = true;
  147. i--;
  148. }
  149. }
  150. else
  151. return false;
  152. }
  153. PName[0] = toupper(PName[0]);
  154. sprintf_s(ServerName, 2048, "%s\0", PName.c_str());
  155.  
  156. sprintf_s(TBD, "#M-%s", PName.c_str());
  157. return true;
  158. }
  159. return false;
  160. }
  161.  
  162. bool GetServerNameByWindow()
  163. {
  164. const char *Replacements[] = {"metin2", "client", "starter", "pvp", "fun", ",", ":", ".", "!"};
  165. const char *e[4] = {"\\d{2,}", "\\s\\S{1}\\s", "\\s", "\\S{2,}\\-\\S{3,}"};
  166. std::smatch m;
  167. //bool bReplaced = false;
  168. std::string PName, tPName;
  169. sprintf_s(ServerName, "");
  170. while (StrCmp(ServerName, "") == 0)
  171. {
  172. EnumDesktopWindows(0, &EnumWindowsProc, 0);
  173. Sleep(1000);
  174. }
  175.  
  176. sprintf_s(TBD, "W-%s", PName.c_str());
  177. PName = ServerName;
  178. tPName = PName;
  179. std::transform(tPName.begin(), tPName.end(), tPName.begin(), tolower);
  180. if (tPName.find(Replacements[0]) == std::string::npos)
  181. {
  182. for (int i = 0;i < _countof(Replacements);i++)
  183. {
  184. if (tPName.length())
  185. {
  186. int Pos = tPName.find(Replacements[i]);
  187. if (Pos != std::string::npos)
  188. {
  189. PName.erase(Pos, Pos + strlen(Replacements[i]));
  190. tPName = PName;
  191. std::transform(tPName.begin(), tPName.end(), tPName.begin(), tolower);
  192. //bReplaced = true;
  193. }
  194. }
  195. else
  196. return false;
  197. }
  198.  
  199. if (std::regex_search(PName, m, (std::regex)e[3]))
  200. PName = m[0];
  201. PName[0] = toupper(PName[0]);
  202. sprintf_s(ServerName, 2048, "%s\0", PName.c_str());
  203.  
  204. sprintf_s(TBD, "#W-%s", PName.c_str());
  205. return true;
  206. }
  207. return false;
  208. }
  209.  
  210. bool GetServerName()
  211. {
  212. if (GetServerNameByModule())
  213. return true;
  214. else if (GetServerNameByWindow())
  215. return true;
  216.  
  217. return false;
  218. }
  219. #endif
  220.  
  221. DWORD GetModuleSize()
  222. {
  223. DWORD ModuleSize;
  224.  
  225. DWORD Base = (DWORD)hPInstance;
  226. DWORD PEOffset = *(DWORD*)(Base + 0x3C);
  227.  
  228. MODULEINFO module_info;
  229. memset(&module_info, 0, sizeof(module_info));
  230. if (GetModuleInformation(GetCurrentProcess(), hPInstance, &module_info, sizeof(module_info)))
  231. ModuleSize = module_info.SizeOfImage;
  232. else
  233. ModuleSize = *(DWORD*)(Base + PEOffset + 0x1C) + Base;
  234.  
  235. return ModuleSize - 0x1000;
  236. }
  237.  
  238. __forceinline bool DataCompare(const unsigned char* address, const unsigned char* signature, const char* mask)
  239. {
  240. while (*mask)
  241. {
  242. if (*reinterpret_cast<const unsigned int*>(mask) == 'xxxx')
  243. {
  244. if (*reinterpret_cast<const unsigned int*>(address) != *reinterpret_cast<const unsigned int*>(signature))
  245. return false;
  246.  
  247. address += 4;
  248. signature += 4;
  249. mask += 4;
  250. continue;
  251. }
  252. else if (*reinterpret_cast<const unsigned short*>(mask) == 'xx')
  253. {
  254. if (*reinterpret_cast<const unsigned short*>(address) != *reinterpret_cast<const unsigned short*>(signature))
  255. return false;
  256.  
  257. address += 2;
  258. signature += 2;
  259. mask += 2;
  260. continue;
  261. }
  262. else if(*mask == 'x' && *address != *signature)
  263. {
  264. return false;
  265. }
  266.  
  267. ++address;
  268. ++signature;
  269. ++mask;
  270. }
  271.  
  272. return *mask == 0;
  273. }
  274.  
  275. DWORD FindPatternInternal(const unsigned char* address, size_t size, const unsigned char* signature, const char* mask, unsigned short ignore)
  276. {
  277. WORD Ign = 0;
  278. for(size_t i = 0; i < size; i++)
  279. {
  280. if(DataCompare(address + i, signature, mask))
  281. {
  282. if (Ign < ignore)
  283. Ign++;
  284. else
  285. return (DWORD)address + i;
  286. }
  287. }
  288. return 0;
  289. }
  290.  
  291. DWORD FindPattern(const unsigned char* address, size_t size, const unsigned char* signature, const char* mask, unsigned short ignore)
  292. {
  293. size_t mask_length = strlen(mask);
  294. return FindPatternInternal(address, size - mask_length, signature, mask, ignore);
  295. }
  296.  
  297. DWORD RelativeToAbsolute (DWORD Offset)
  298. {
  299. return *(DWORD*)(Offset) + Offset + 0x4;
  300. }
  301.  
  302. #if !defined(InviDropper)
  303. BOOL CheckIni(BOOL Loading)
  304. {
  305. BOOL FileSelected;
  306. CreateDirectory(IniPath, 0);
  307.  
  308. OPENFILENAME ofn = {0};
  309. ofn.lStructSize = sizeof(ofn);
  310. ofn.hwndOwner = 0;
  311. ofn.lpstrFilter = "Einstellungen(*.ini)\0*.ini\0";
  312. ofn.nMaxFile = 2048;
  313. ofn.lpstrFile = IniFullPath;
  314. ofn.lpstrTitle = (Loading) ? "Konfigurationsdatei laden..." : "Konfigurationsdatei speichern";
  315. ofn.Flags = (Loading) ? OFN_DONTADDTORECENT + OFN_FILEMUSTEXIST + OFN_NONETWORKBUTTON : OFN_DONTADDTORECENT + OFN_NONETWORKBUTTON;
  316. ofn.lpstrDefExt = sExt;
  317. FileSelected = (Loading) ? GetOpenFileName(&ofn) : GetSaveFileName(&ofn);
  318. if (!FileSelected)
  319. PathRemoveExtension(IniFullPath);
  320. else
  321. CloseHandle(CreateFile(IniFullPath, GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0));
  322.  
  323. #if (DbgSession)
  324. DbgOutput = "File selected:" + std::to_string(FileSelected) + " (" + (std::string)IniFullPath + ")";
  325. MessageBox(0, DbgOutput.c_str(), sDbgOutput, MB_ICONASTERISK);
  326. #endif
  327.  
  328. SetCurrentDirectory(WorkingDir);
  329. return FileSelected;
  330. #if (DbgSession)
  331. DbgOutput = "No ini found in default dir (" + (std::string)IniPath + ")";
  332. MessageBox(0, DbgOutput.c_str(), sDbgOutput, MB_ICONASTERISK);
  333. #endif
  334. return 0;
  335. }
  336.  
  337. bool ValidateIni()
  338. {
  339. if (PathFindExtension(IniFullPath) != sExt)
  340. {
  341. if (!CheckIni(FALSE))
  342. return false;
  343. }
  344. return true;
  345. }
  346.  
  347. bool ForceValidation(bool bAsk)
  348. {
  349. if (!bAsk && (PathFindExtension(IniFullPath) != sExt))
  350. {
  351. std::string sPathEx;
  352. sPathEx = (std::string)IniFullPath + (std::string)sExt;
  353. if (PathFileExists(sPathEx.c_str()))
  354. {
  355. PathAddExtension(IniFullPath, sExt);
  356. #if (DbgSession)
  357. DbgOutput = "ForceValidation returns true!";
  358. MessageBox(0, DbgOutput.c_str(), sDbgOutput, MB_ICONASTERISK);
  359. #endif
  360. return true;
  361. }
  362. }
  363. #if (DbgSession)
  364. PrintDbg("ForceValidation returns false!", DbgOutput);
  365. #endif
  366. return false;
  367. }
  368.  
  369. void GetIniFullPath()
  370. {
  371. hProcess = GetCurrentProcess();
  372. ProcessID = GetCurrentProcessId();
  373. GetModuleFileName((HINSTANCE)&__ImageBase, IniPath, 2048);
  374. PathRemoveFileSpec(IniPath);
  375. PathAppend(IniPath, sSettings);
  376. if (GetServerName())
  377. PathCombine(IniFullPath, IniPath, ServerName);
  378. else
  379. PathCombine(IniFullPath, IniPath, "Servername\0");
  380.  
  381. #if (DbgSession)
  382. MessageBox(0, IniFullPath, "", 0);
  383. #endif
  384. }
  385.  
  386. void CleanupOldSettings()
  387. {
  388. bool IsOldIncompatibleIni, IsOldCompatibleIni, IsCompatibleIni;
  389. std::string OldIncompatibleIni, OldCompatibleDir, OldCompatibleIni, CompatibleDir, CompatibleIni;
  390.  
  391. if (GetCurrentDirectory(2048, WorkingDir))//check for error while getting workingdir
  392. {
  393. PathCombine(IniFullPath, WorkingDir, "Ultimate-Tool.ini");
  394. // Case 1
  395. if (PathFileExists(IniFullPath))
  396. {
  397. OldIncompatibleIni = IniFullPath;
  398. IsOldIncompatibleIni = true;
  399. }
  400. // Case 2
  401. PathCombine(IniPath, WorkingDir, "Ultimate-Tool");
  402. PathCombine(IniFullPath, WorkingDir, "Ultimate-Tool\\Settings.ini");
  403. if (PathFileExists(IniFullPath))
  404. {
  405. OldCompatibleDir = IniPath;
  406. OldCompatibleIni = IniFullPath;
  407. IsOldCompatibleIni = true;
  408. }
  409. //Case 3
  410. PathCombine(IniPath, WorkingDir, "Tools by Unpublished");
  411. PathCombine(IniFullPath, WorkingDir, "Tools by Unpublished\\Settings.ini");
  412. if (PathFileExists(IniFullPath))
  413. {
  414. CompatibleDir = IniPath;
  415. CompatibleIni = IniFullPath;
  416. IsCompatibleIni = true;
  417. }
  418. GetIniFullPath();
  419. }
  420. else
  421. {
  422. MessageBox(0, sErrorGettingWorkingDir, sError, MB_ICONERROR);
  423. }
  424. if (IsOldIncompatibleIni)
  425. {
  426. if (MessageBox(0, "Es wurde eine alte inkompatible Konfigurationsdatei gefunden (Ultimate-Tool.ini).\n\nSoll diese nun gelöscht werden (empfohlen)?", sConfigFile, MB_YESNO | MB_ICONQUESTION) == IDYES)
  427. {
  428. DeleteFile(OldIncompatibleIni.c_str());
  429. }
  430. }
  431. if (IsOldCompatibleIni && !IsCompatibleIni)
  432. {
  433. SetNewIniLocation(&CompatibleIni[0]);
  434. }
  435. else if (IsOldCompatibleIni && IsCompatibleIni)
  436. {
  437. DeleteFile(OldCompatibleIni.c_str());
  438. RemoveDirectory(OldCompatibleDir.c_str());
  439. }
  440. if (IsCompatibleIni && PathFileExists(IniFullPath))
  441. {
  442. int Antwort = MessageBox(0, "Es wurde eine alte Konfigurationsdatei gefunden (Tools by Unpublished\\Settings.ini).\n\nJa um diese zu löschen.\n\nNein um die aktuelle Konfigurations zu ersetzen.\n\nAbbrechen um die Entscheidung zu überspringen.", sConfigFile, MB_YESNOCANCEL | MB_ICONQUESTION);
  443. if (Antwort == IDYES)
  444. {
  445. DeleteFile(CompatibleIni.c_str());
  446. RemoveDirectory(CompatibleDir.c_str());
  447. }
  448. else if (Antwort == IDNO)
  449. {
  450. MoveFileEx(CompatibleDir.c_str(), IniFullPath, MOVEFILE_WRITE_THROUGH + MOVEFILE_REPLACE_EXISTING);
  451. }
  452. }
  453. else if (IsCompatibleIni && PathFindExtension(IniFullPath) == sExt)
  454. {
  455. MoveFileEx(CompatibleIni.c_str(), IniFullPath, MOVEFILE_WRITE_THROUGH + MOVEFILE_REPLACE_EXISTING);
  456. RemoveDirectory(CompatibleDir.c_str());
  457. }
  458. else if (IsCompatibleIni)
  459. {
  460. SetNewIniLocation(&CompatibleIni[0]);
  461. }
  462. if (PathFileExists((CompatibleDir + "vote.mix").c_str()))
  463. iClientMod = 1;
  464. }
  465.  
  466. void SetNewIniLocation(char OldLocation[2048])
  467. {
  468. MessageBox(0, "Es wurde eine veraltete Konfigurationsdatei gefunden.\r\n\r\nIm nächsten Schritt kann ein neuer Speicherort festgelegt werden.", sConfigFile, 0);
  469. if (CheckIni(FALSE))
  470. {
  471. MoveFileEx(OldLocation, IniFullPath, MOVEFILE_WRITE_THROUGH + MOVEFILE_REPLACE_EXISTING);
  472. PathRemoveFileSpec(OldLocation);
  473. RemoveDirectory(OldLocation);
  474. }
  475. }
  476. #endif
  477.  
  478. void InitComponents()
  479. {
  480. #if !defined(InviDropper)
  481. CleanupOldSettings();
  482. #endif
  483. int i = 2000;
  484. for (int j = 0; j < _countof(IDC_BUTTON); j++)
  485. {
  486. IDC_BUTTON[j] = i;
  487. i++;
  488. }
  489. #if !defined(InviDropper)
  490. #if defined(TAB)
  491. for (int j = 0;j < _countof(IDC_TAB);j++)
  492. {
  493. IDC_TAB[j] = i;
  494. i++;
  495. }
  496. #endif
  497. #if defined(LABEL)
  498. for (int j = 0; j < _countof(IDC_STATIC); j++)
  499. {
  500. IDC_STATIC[j] = i;
  501. i++;
  502. }
  503. #endif
  504. #if defined(COMBO)
  505. for (int j = 0;j < _countof(IDC_COMBO);j++)
  506. {
  507. IDC_COMBO[j] = i;
  508. i++;
  509. }
  510. #endif
  511. #if defined(EDIT)
  512. for (int j = 0; j < _countof(IDC_EDIT); j++)
  513. {
  514. IDC_EDIT[j] = i;
  515. i++;
  516. }
  517. #endif
  518. #endif
  519. i = 0;
  520. MainWindow();
  521. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement