|
| 1 | + ;; |
| 2 | + ;; Copyright 2015 Gu Zhengxiong <rectigu@gmail.com> |
| 3 | + ;; |
| 4 | + ;; For those curious heads |
| 5 | + ;; striving to figure out what's under the hood. |
| 6 | + ;; |
| 7 | + ;; GPL |
| 8 | + ;; |
| 9 | + |
| 10 | + |
| 11 | + .model flat, stdcall |
| 12 | + |
| 13 | + option casemap:none |
| 14 | + assume fs:nothing |
| 15 | + |
| 16 | + .code |
| 17 | + |
| 18 | +start: |
| 19 | + ;; ecx = NtCurrentTeb()->ProcessEnvironmentBlock; |
| 20 | + xor ecx, ecx |
| 21 | + mov ecx, dword ptr fs:[ecx + 30h] |
| 22 | + |
| 23 | + ;; ecx = ecx->Ldr; |
| 24 | + mov ecx, dword ptr [ecx + 0ch] |
| 25 | + |
| 26 | + ;; ecx = ecx->InInitializationOrderModuleList; |
| 27 | + mov ecx, dword ptr [ecx + 1ch] |
| 28 | + |
| 29 | + |
| 30 | +find_kernel32_dll_base: |
| 31 | + ;; ebx = ecx->DllBase; |
| 32 | + mov ebx, dword ptr [ecx + 8h] |
| 33 | + |
| 34 | + ;; eax = ecx->BaseDllName.Buffer; |
| 35 | + mov eax, dword ptr [ecx + 20h] |
| 36 | + |
| 37 | + ;; ecx = ecx->InInitializationOrderLinks; |
| 38 | + mov ecx, dword ptr [ecx] |
| 39 | + |
| 40 | + ;; if (eax[6] == '3') |
| 41 | + cmp byte ptr [eax + 0ch], 33h |
| 42 | + jne find_kernel32_dll_base |
| 43 | + |
| 44 | + |
| 45 | + ;; nonvolatile ebx = LoadLibrary("kernel32.dll"); |
| 46 | + |
| 47 | + |
| 48 | + mov ebp, ebx |
| 49 | + ;; ebp = ebx->e_lfanew; |
| 50 | + add ebp, dword ptr [ebp + 3ch] |
| 51 | + ;; ebp = ebp->OptionalHeader.DataDirectory[0].VirtualAddress; |
| 52 | + mov ebp, dword ptr [ebp + 78h] |
| 53 | + add ebp, ebx |
| 54 | + |
| 55 | + |
| 56 | + ;; nonvolatile ebp = IMAGE_EXPORT_DIRECTORY; |
| 57 | + |
| 58 | + |
| 59 | + ;; eax = ebp->AddressOfNames; |
| 60 | + mov eax, dword ptr [ebp + 20h] |
| 61 | + add eax, ebx |
| 62 | + |
| 63 | + |
| 64 | + xor edx, edx |
| 65 | +find_get_proc_address: |
| 66 | + ;; esi = eax[edx]; // eax is ExportNamePointerTable, a dword array |
| 67 | + mov esi, dword ptr [eax + edx * 4] |
| 68 | + add esi, ebx |
| 69 | + |
| 70 | + inc edx |
| 71 | + |
| 72 | + ;; if (memcmp(esi, 'PteG', 4)) |
| 73 | + cmp dword ptr [esi], 'PteG' |
| 74 | + jne find_get_proc_address |
| 75 | + |
| 76 | + ;; if (memcmp(esi + 4, 'Acor', 4)) |
| 77 | + cmp dword ptr [esi + 4], 'Acor' |
| 78 | + jne find_get_proc_address |
| 79 | + |
| 80 | + |
| 81 | + ;; esi = ebp->AddressOfNameOrdinals; |
| 82 | + mov esi, dword ptr [ebp + 24h] |
| 83 | + add esi, ebx |
| 84 | + |
| 85 | + ;; dx = esi[edx]; // esi is ExportOrdinalTable, a _word_ array |
| 86 | + mov dx, word ptr [esi + edx * 2] |
| 87 | + |
| 88 | + ;; esi = ebp->AddressOfFunctions; |
| 89 | + mov esi, dword ptr [ebp + 1ch] |
| 90 | + add esi, ebx |
| 91 | + |
| 92 | + ;; esi = esi[edx]; // esi is is ExportAddressTable, a dword array |
| 93 | + mov esi, dword ptr [esi + edx * 4 - 4] |
| 94 | + add esi, ebx |
| 95 | + |
| 96 | + |
| 97 | + ;; nonvolatile esi = GetProcAddress; |
| 98 | + |
| 99 | + |
| 100 | + xor edi, edi |
| 101 | + |
| 102 | + |
| 103 | + ;; nonvolatile edi = NULL; |
| 104 | + |
| 105 | + |
| 106 | + ;; eax = GetProcAddress(ebx, "LoadLibraryA"); |
| 107 | + push edi |
| 108 | + push 'Ayra' |
| 109 | + push 'rbiL' |
| 110 | + push 'daoL' |
| 111 | + push esp |
| 112 | + push ebx |
| 113 | + call esi |
| 114 | + |
| 115 | + ;; eax = LoadLibrary("user32"); |
| 116 | + xor ecx, ecx |
| 117 | + push edi |
| 118 | + mov cx, '23' |
| 119 | + push ecx |
| 120 | + push 'resu' |
| 121 | + push esp |
| 122 | + call eax |
| 123 | + |
| 124 | + ;; eax = GetProcAddress(eax, "MessageBoxA") |
| 125 | + push edi |
| 126 | + push 0141786fh |
| 127 | + dec byte ptr [esp + 3h] |
| 128 | + push 'Bega' |
| 129 | + push 'sseM' |
| 130 | + push esp |
| 131 | + push eax |
| 132 | + call esi |
| 133 | + |
| 134 | + ;; eax = MessageBoxA(NULL, "Hello World!", NULL, MB_OK); |
| 135 | + push edi |
| 136 | + push '!dlr' |
| 137 | + push 'oW o' |
| 138 | + push 'lleH' |
| 139 | + mov ecx, esp |
| 140 | + push edi |
| 141 | + push edi |
| 142 | + push ecx |
| 143 | + push edi |
| 144 | + call eax |
| 145 | + |
| 146 | + ;; eax = GetProcAddress(ebx, "ExitProcess") |
| 147 | + push edi |
| 148 | + push 01737365h |
| 149 | + dec byte ptr [esp + 3h] |
| 150 | + push 'corP' |
| 151 | + push 'tixE' |
| 152 | + push esp |
| 153 | + push ebx |
| 154 | + call esi |
| 155 | + |
| 156 | + ;; ExitProcess(NULL); |
| 157 | + push edi |
| 158 | + call eax |
| 159 | +end start |
0 commit comments