1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
| #include <windows.h> #include <stdio.h> #include <Tlhelp32.h> #include <winternl.h> #include <winnt.h> #include "resource.h"
#define MX (((z>>5^y<<2) + (y>>3^z<<4)) ^ ((sum^y) + (key[(p&3)^e] ^ z)))
typedef BOOL(WINAPI* PWRITEFILE)(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped);
void xxtea_enc(DWORD* v, int n, DWORD const key[4]); void save_res(); void debuger_main_loop(char* name, void* ppi); DWORD WINAPI MyWriteFile(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped); BOOL SetIATHook(DWORD dwOldFuncAddr, DWORD dwNewFuncAddr, HANDLE hApiModule); BOOL UnIATHook(DWORD dwOldFuncAddr, DWORD dwNewFuncAddr, HANDLE hApiModule); char * decstring(char * str); void print_console(const char* szMsg); void NTAPI TLS_CALLBACK1(PVOID DllHandle, DWORD Reason, PVOID Reserved); void NTAPI TLS_CALLBACK2(PVOID DllHandle, DWORD Reason, PVOID Reserved); void dec_res(char* buffer, int len); void nouse();
PWRITEFILE g_dwOldFuncAddr; DWORD g_dwNewFuncAddr; DWORD g_dwIATHookFlag = FALSE; HMODULE hAPIModule; DWORD DELTA = 0x9E3779B9; DWORD xxteaKey[] = { 0x12,0x34,0x56,0x78 }; char enc_flag2[20] = { 0x21, 0xa9, 0x21, 0x90, 0x60, 0x30, 0x3b, 0xf5, 0x4e, 0xa8, 0x88, 0x8e, 0xd5, 0x5a, 0x63, 0x43, 0x39, 0x92, 0x11, 0xac }; DWORD dwExitCode = 0; DEBUG_EVENT debug_event = { 0 }; DWORD tls_nouse = 0; PIMAGE_TLS_CALLBACK pTLS_CALLBACKs[] = { TLS_CALLBACK1, NULL, NULL }; IMAGE_TLS_DIRECTORY tls = { (DWORD)&tls_nouse, (DWORD)&tls_nouse, (DWORD)&tls_nouse, (DWORD)pTLS_CALLBACKs,0,0 }; char* pbuffer; HANDLE hFileMap;
int main() { char fake_enc[] = { 38, 17, 8, 35, 26, 8, 28, 39, 3, 25, 26, 43, 10, 29, 4, 30, 8, 49, 25, 4, 2, 25, 54, 1, 20, 57, 4, 59, 5, 3, 10, 5, 0, 56, 49, 61, 60, 123, 120, 121 }; char flag[100] = { 0 }; printf("Please input your flag: "); scanf("%s", flag); int i = 0; for (i = 0; i < 40; i++) { if ((flag[i] ^ 0x7F ^ i) != fake_enc[i]) break; } if (i == 40) { printf("correct\n"); } else { printf("wrong\n"); } return 0; }
void xxtea_enc(DWORD* v, int n, DWORD const key[4]) { DWORD y, z, sum; unsigned p, rounds, e; rounds = 6 + 52 / n; sum = 0; z = v[n - 1]; do { sum += DELTA; e = (sum >> 2) & 3; for (p = 0; p < n - 1; p++) { y = v[p + 1]; z = v[p] += MX; } y = v[0]; z = v[n - 1] += MX; } while (--rounds); }
void save_res() { char name[] = { 81, 80, 11, 18, 15,0 }; char resname[] = { 58, 39, 58, 45, 58, 44, 0 }; decstring(name); decstring(resname); HRSRC hRsrc = FindResource(NULL, MAKEINTRESOURCEA(IDR_EXERES1), TEXT(resname)); DWORD dwSize = SizeofResource(NULL, hRsrc); HGLOBAL hGlobal = LoadResource(NULL, hRsrc); LPVOID pBuffer = LockResource(hGlobal); dec_res((char *)pBuffer, dwSize); HANDLE hFile = CreateFile(name, GENERIC_WRITE | GENERIC_READ, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwWritenSize = 0; BOOL bRet = ::WriteFile(hFile, pBuffer, dwSize, &dwWritenSize, NULL); FlushFileBuffers(hFile); CloseHandle(hFile); }
void debuger_main_loop(char* name, void* ppi) { PROCESS_INFORMATION pi = *(PROCESS_INFORMATION*)ppi; BOOL bRunning = TRUE; __asm { call label; label: add dword ptr[esp], 5; ret; }
while (bRunning) { WaitForDebugEvent(&debug_event, INFINITE); if (debug_event.dwDebugEventCode == EXCEPTION_DEBUG_EVENT) { EXCEPTION_DEBUG_INFO exception = debug_event.u.Exception; switch (exception.ExceptionRecord.ExceptionCode) { case STATUS_ACCESS_VIOLATION: { CONTEXT ctx = { 0 }; ctx.ContextFlags = CONTEXT_FULL; GetThreadContext(pi.hThread, &ctx); ctx.Eip += 5; ctx.Eax ^= 111111; SetThreadContext(pi.hThread, &ctx); } default: break; } } if (debug_event.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) { dwExitCode = debug_event.u.ExitProcess.dwExitCode; bRunning = FALSE; }
ContinueDebugEvent(debug_event.dwProcessId, debug_event.dwThreadId, DBG_CONTINUE); }
Sleep(100); int a = DeleteFile(name); }
DWORD __stdcall MyWriteFile(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped) { char* buffer = (char*)lpBuffer; buffer[0x71C + 2] = 0x6; buffer[0x6AF + 2] = 0x6; g_dwOldFuncAddr(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, lpOverlapped); UnIATHook((DWORD)g_dwOldFuncAddr, (DWORD)MyWriteFile, hAPIModule); return 0; }
BOOL SetIATHook(DWORD dwOldFuncAddr, DWORD dwNewFuncAddr, HANDLE hApiModule) { HMODULE hModule = GetModuleHandle(NULL); IMAGE_DOS_HEADER* pIDH = (IMAGE_DOS_HEADER*)hModule; IMAGE_NT_HEADERS* pINH = (IMAGE_NT_HEADERS*)(pIDH->e_lfanew + (DWORD)hModule); IMAGE_IMPORT_DESCRIPTOR* pFirstIID = (IMAGE_IMPORT_DESCRIPTOR*)(pINH->OptionalHeader.DataDirectory[1].VirtualAddress + (DWORD)hModule); IMAGE_IMPORT_DESCRIPTOR* pTempIID = pFirstIID;
DWORD pOldProtect = 0;
while (pTempIID->FirstThunk && g_dwIATHookFlag == FALSE) { if (hApiModule == GetModuleHandle((char*)((DWORD)hModule + pTempIID->Name))) { DWORD* pTemp = (DWORD*)(pTempIID->FirstThunk + (DWORD)hModule); while (pTemp) { if (*pTemp == (DWORD)dwOldFuncAddr) { VirtualProtect(pTemp, 4, PAGE_READWRITE, &pOldProtect); *pTemp = dwNewFuncAddr; VirtualProtect(pTemp, 4, pOldProtect, NULL); g_dwIATHookFlag = TRUE; break; } pTemp++; } } pTempIID++; if (g_dwIATHookFlag) break; } return g_dwIATHookFlag; }
BOOL UnIATHook(DWORD dwOldFuncAddr, DWORD dwNewFuncAddr, HANDLE hApiModule) { HMODULE hModule = GetModuleHandle(NULL); IMAGE_DOS_HEADER* pIDH = (IMAGE_DOS_HEADER*)hModule; IMAGE_NT_HEADERS* pINH = (IMAGE_NT_HEADERS*)(pIDH->e_lfanew + (DWORD)hModule); IMAGE_IMPORT_DESCRIPTOR* pFirstIID = (IMAGE_IMPORT_DESCRIPTOR*)(pINH->OptionalHeader.DataDirectory[1].VirtualAddress + (DWORD)hModule); IMAGE_IMPORT_DESCRIPTOR* pTempIID = pFirstIID;
DWORD pOldProtect = 0;
while (pTempIID->FirstThunk && g_dwIATHookFlag == TRUE) { if (hApiModule == GetModuleHandle((char*)((DWORD)hModule + pTempIID->Name))) { DWORD* pTemp = (DWORD*)(pTempIID->FirstThunk + (DWORD)hModule); while (pTemp) { if (*pTemp == (DWORD)dwNewFuncAddr) { VirtualProtect(pTemp, 4, PAGE_READWRITE, &pOldProtect); *pTemp = dwOldFuncAddr; VirtualProtect(pTemp, 4, pOldProtect, NULL); g_dwIATHookFlag = FALSE; break; } pTemp++; } } pTempIID++; if (!g_dwIATHookFlag) break; } return g_dwIATHookFlag; }
char * decstring(char* str) { for (int i = 0; i < strlen(str); i++) { str[i] ^= 0x7F; } return str; }
void print_console(const char* szMsg) { HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); WriteConsoleA(hStdout, szMsg, strlen(szMsg), NULL, NULL); }
void NTAPI TLS_CALLBACK1(PVOID DllHandle, DWORD Reason, PVOID Reserved) { __asm { call label; label: add dword ptr[esp], 30; ret; _emit 87; _emit 101; _emit 108; _emit 99; _emit 111; _emit 109; _emit 101; _emit 95; _emit 116; _emit 111; _emit 95; _emit 50; _emit 48; _emit 50; _emit 50; _emit 95; _emit 109; _emit 105; _emit 110; _emit 105; _emit 76; _emit 67; _emit 84; _emit 70; _emit 0 } if (Reason == 1) { char szMsg[80] = { 0 }; print_console(szMsg); bool dwDebugPort = 0; __asm { mov eax, fs: [30h] mov al, BYTE PTR[eax + 2] mov dwDebugPort, al }
if (!dwDebugPort) { __asm { call label1; label1: add dword ptr[esp], 5; ret; } pTLS_CALLBACKs[1] = TLS_CALLBACK2; }
char sharememname[] = { 57, 51, 62, 56 ,0 }; decstring(sharememname); hFileMap = CreateFileMapping(NULL, NULL, PAGE_READWRITE, 0, 0x1000, sharememname); pbuffer = (char*)MapViewOfFile(hFileMap, FILE_MAP_ALL_ACCESS, 0, 0, 0X1000); char inputFlagstr[] = { 47,19,26,30,12,26,95,22,17,15,10,11,95,6,16,10,13,95,25,19,30,24,69,95,0 }; print_console(decstring(inputFlagstr)); char _formatstr[] = { 90, 12, 0 }; decstring(_formatstr); scanf_s(_formatstr, pbuffer, 41); } if (Reason == 0) { char name[] = { 81, 80, 11, 18, 15,0 }; decstring(name); save_res(); STARTUPINFO si = { 0 }; PROCESS_INFORMATION pi; si.cb = sizeof(si); CreateProcess( name, NULL, NULL, NULL, FALSE, DEBUG_ONLY_THIS_PROCESS | DEBUG_PROCESS, NULL, NULL, &si, &pi );
char correct[] = { 28, 16, 13, 13, 26, 28, 11, 117 ,0 }; char wrong[] = { 8, 13, 16, 17, 24, 117, 0 }; char checkdebugerstr[] = { 47, 19, 26, 30, 12, 26, 95, 28, 19, 16, 12, 26, 95, 11, 23, 26, 95, 27, 26, 29, 10, 24, 24, 26, 13, 95, 30, 17, 27, 95, 11, 13, 6, 95, 30, 24, 30, 22, 17, 117, 0 }; debuger_main_loop(name, &pi); if (dwExitCode == 1) { xxtea_enc((DWORD*)&pbuffer[20], 5, xxteaKey); if (!memcmp(&pbuffer[20], enc_flag2, 20)) { decstring(correct); print_console(correct); } else { decstring(wrong); print_console(wrong); } } else if (dwExitCode == -2) { decstring(checkdebugerstr); print_console(checkdebugerstr); } else { decstring(wrong); print_console(wrong); }
CloseHandle(hFileMap); } }
void NTAPI TLS_CALLBACK2(PVOID DllHandle, DWORD Reason, PVOID Reserved) { __asm { call label; label: add dword ptr[esp], 5; ret; } if (Reason == 1) { char szHookApi[] = { 40, 13, 22, 11, 26, 57, 22, 19, 26, 0 }; char szUser32[] = { 20, 26, 13, 17, 26, 19, 76, 77, 81, 27, 19, 19, 0 }; decstring(szHookApi); decstring(szUser32); hAPIModule = GetModuleHandle(szUser32); g_dwOldFuncAddr = (PWRITEFILE)GetProcAddress(hAPIModule, szHookApi); SetIATHook((DWORD)g_dwOldFuncAddr, (DWORD)MyWriteFile, hAPIModule); } ExitProcess(-1); }
void dec_res(char* buffer, int len) { for (int i = 0; i < len; i++) { buffer[i] ^= 0x55; } }
void nouse() { DWORD a = (DWORD)&tls; }
|