代码逆向之旅1
代码逆向之旅1
1 // EjectDll_逆向.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 2 // 3 4 #include<windows.h> 5 #include<stdio.h> 6 #include<stdlib.h> 7 #include<Tlhelp32.h> 8 9 int Injection() 10 { 11 HANDLE hProcess = NULL; 12 DWORD dwProcessId = 0; 13 ///*.text:004011B0 sub_4011B0 proc near; CODE XREF : _wmain + 4E↓p 14 // .text : 004011B0 15 // .text : 004011B0 me = MODULEENTRY32W ptr - 430h 16 MODULEENTRY32 me; 17 // .text : 004011C6 push 424h; size_t 18 // .text:004011CB lea eax, [ebp + me.th32ModuleID] 19 // .text : 004011D1 push 0; int 20 // .text:004011D3 push eax; void* 21 // .text:004011D4 mov esi, ecx 22 // .text : 004011D6 mov[ebp + me.dwSize], 428h 23 me.dwSize = 0x428; 24 // .text : 004011E0 call _memset 25 // .text : 004011E5 add esp, 0Ch 26 memset(&me.th32ModuleID, 0, 0x424); 27 // .text : 004011E8 push esi; th32ProcessID 28 // .text:004011E9 push 8; dwFlags 29 // .text:004011EB call ds : CreateToolhelp32Snapshot 30 HANDLE hSnapshot = CreateToolhelp32Snapshot(8,me.th32ProcessID); 31 // .text : 004011F1 lea ecx, [ebp + me] 32 // .text : 004011F7 mov ebx, eax 33 34 // .text : 004011F9 push ecx; lpme 35 // .text:004011FA push ebx; hSnapshot 36 // .text:004011FB call ds : Module32FirstW 37 38 // .text : 00401201 test eax, eax 39 // .text : 00401203 jz short loc_40124E 40 if (Module32First(hSnapshot, &me) == NULL) 41 { 42 CloseHandle(hSnapshot); 43 return 0; 44 } 45 while (_wcsicmp(me.szExePath, L"myhack.dll") && _wcsicmp(me.szModule, L"myhack.dll")) 46 { 47 // .text : 00401205 mov edi, ds : Module32NextW 48 // .text : 0040120B jmp short loc_401210 49 // .text : 0040120B; -------------------------------------------------------------------------- - 50 // .text:0040120D align 10h 51 // .text : 00401210 52 // .text : 00401210 loc_401210 : ; CODE XREF : sub_4011B0 + 5B↑j 53 // .text : 00401210; sub_4011B0 + 9C↓j 54 // .text:00401210 lea edx, [ebp + me.szModule] 55 // .text : 00401216 push offset aMyhackDll; "myhack.dll" 56 // .text:0040121B push edx; wchar_t* 57 // .text:0040121C call __wcsicmp 58 // .text : 00401221 add esp, 8 59 60 // .text : 00401224 test eax, eax 61 // .text : 00401226 jz short loc_401268 62 63 // .text : 00401228 lea eax, [ebp + me.szExePath] 64 // .text:0040122E push offset aMyhackDll; "myhack.dll" 65 // .text:00401233 push eax; wchar_t* 66 // .text:00401234 call __wcsicmp 67 // .text : 00401239 add esp, 8 68 69 // .text:0040123C test eax, eax 70 // .text : 0040123E jz short loc_401268 71 72 // .text : 00401240 lea ecx, [ebp + me] 73 // .text : 00401246 push ecx; lpme 74 // .text:00401247 push ebx; hSnapshot 75 // .text:00401248 call edi; Module32NextW 76 77 // .text:0040124A test eax, eaxs 78 // .text : 0040124C jnz short loc_401210 79 if (Module32Next(hSnapshot, &me) == NULL) 80 { 81 // .text : 0040124E loc_40124E : ; CODE XREF : sub_4011B0 + 53↑j 82 // .text : 0040124E push ebx; hObject 83 // .text:0040124F call ds : CloseHandle 84 CloseHandle(hSnapshot); 85 // .text : 00401255 xor eax, eax 86 // .text : 00401257 pop edi 87 // .text : 00401258 pop esi 88 // .text:00401259 pop ebx 89 // .text:0040125A mov ecx, [ebp + var_4] 90 // .text : 0040125D xor ecx, ebp 91 // .text : 0040125F call sub_401395 92 // .text : 00401264 mov esp, ebp 93 // .text : 00401266 pop ebp 94 // .text : 00401267 retn 95 return 0; 96 } 97 98 } 99 100 // .text:00401268 loc_401268 : ; CODE XREF : sub_4011B0 + 76↑j 101 // .text : 00401268; sub_4011B0 + 8E↑j 102 // .text:00401268 push esi; dwProcessId 103 // .text:00401269 push 0; bInheritHandle 104 // .text:0040126B push 1FFFFFh; dwDesiredAccess 105 // .text:00401270 call ds : OpenProcess 106 hProcess = OpenProcess(0x1FFFFF, 0, dwProcessId); 107 // .text : 00401276 mov edi, eax 108 // .text : 00401278 test edi, edi 109 // .text:0040127A jnz short loc_4012A4 110 if (hProcess == NULL) 111 { 112 // .text : 0040127C call ds : GetLastError 113 // .text : 00401282 push eax 114 // .text:00401283 push esi 115 // .text:00401284 push offset aOpenprocessDFa; "OpenProcess(%d) failed!!! [%d]\n" 116 // .text:00401289 call _wprintf 117 // .text:0040128E add esp, 0Ch 118 wprintf(L"OpenProcess(%d) failed!!! [%d]\n", dwProcessId,GetLastError()); 119 // .text : 00401291 xor eax, eax 120 // .text:00401293 pop edi 121 // .text:00401294 pop esi 122 // .text:00401295 pop ebx 123 // .text:00401296 mov ecx, [ebp + var_4] 124 // .text:00401299xor ecx, ebp 125 // .text:0040129B call sub_401395 126 // .text:004012A0 mov esp, ebp 127 // .text : 004012A2 pop ebp 128 // .text : 004012A3 retn 129 return 0; 130 } 131 // .text : 004012A4 loc_4012A4 : ; CODE XREF : sub_4011B0 + CA↑j 132 // .text : 004012A4 push offset ModuleName; "kernel32.dll" 133 // .text:004012A9 call ds : GetModuleHandleW 134 // 135 // .text : 004012AF push offset ProcName; "FreeLibrary" 136 // .text:004012B4 push eax; hModule 137 // .text:004012B5 call ds : GetProcAddress 138 LPTHREAD_START_ROUTINE hModule = (LPTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandleW(L"kernel32.dll"),"FreeLibrary"); 139 // .text : 004012BB mov edx, [ebp + me.modBaseAddr] 140 // .text : 004012C1 push 0; lpThreadId 141 // .text:004012C3 push 0; dwCreationFlags 142 // .text:004012C5 push edx; lpParameter 143 // .text:004012C6 push eax; lpStartAddress 144 // .text:004012C7 push 0; dwStackSize 145 // .text:004012C9 push 0; lpThreadAttributes 146 // .text:004012CB push edi; hProcess 147 // .text:004012CC call ds : CreateRemoteThread 148 HANDLE hHandle = CreateRemoteThread(hProcess,0,0, hModule,me.modBaseAddr,0,0); 149 // .text : 004012D2 mov esi, eax 150 // .text : 004012D4 push 0FFFFFFFFh; dwMilliseconds 151 // .text:004012D6 push esi; hHandle 152 // .text:004012D7 call ds : WaitForSingleObject 153 WaitForSingleObject(hHandle,0x0FFFFFFFF); 154 // .text : 004012DD push esi; hObject 155 // .text:004012DE mov esi, ds : CloseHandle 156 CloseHandle(hSnapshot); 157 // .text : 004012E4 call esi; CloseHandle 158 // .text:004012E6 push edi; hObject 159 // .text:004012E7 call esi; CloseHandle 160 CloseHandle(hProcess); 161 // .text:004012E9 push ebx; hObject 162 // .text:004012EA call esi; CloseHandle 163 // 164 CloseHandle(hHandle); 165 // .text:004012EC mov ecx, [ebp + var_4] 166 // .text : 004012EF pop edi 167 // .text : 004012F0 pop esi 168 // .text : 004012F1 xor ecx, ebp 169 // .text : 004012F3 mov eax, 1 170 // .text : 004012F8 pop ebx 171 // .text : 004012F9 call sub_401395 172 // .text : 004012FE mov esp, ebp 173 // .text : 00401300 pop ebp 174 // .text : 00401301 retn 175 // .text : 00401301 sub_4011B0 endp*/ 176 return 1; 177 } 178 179 bool SetPrivilege() 180 { 181 182 ///*.text:00401090 sub_401090 proc near; CODE XREF : _wmain + 43↓p 183 // .text : 00401090 184 // .text:00401090 Luid = _LUID ptr - 20h 185 // .text:00401090 TokenHandle = dword ptr - 18h 186 // .text:00401090 NewState = _TOKEN_PRIVILEGES ptr - 14h 187 TOKEN_PRIVILEGES NewState; 188 HANDLE ProcessHandle; 189 LUID Luid; 190 HANDLE TokenHandle; 191 // .text:004010A0 lea eax, [ebp + TokenHandle] 192 // .text : 004010A3 push eax; TokenHandle 193 // .text:004010A4 push 28h; DesiredAccess 194 // .text:004010A6 call ds : GetCurrentProcess 195 ProcessHandle = GetCurrentProcess(); 196 // .text : 004010AC push eax; ProcessHandle 197 // .text:004010AD call ds : OpenProcessToken 198 199 // .text : 004010B3 test eax, eax 200 // .text : 004010B5 jnz short loc_4010DB 201 if (OpenProcessToken(ProcessHandle, 0x28, &TokenHandle) == NULL) 202 { 203 // .text : 004010B7 call ds : GetLastError 204 // .text : 004010BD push eax 205 // .text : 004010BE push offset aOpenprocesstok; "OpenProcessToken error: %u\n" 206 // .text:004010C3 call _wprintf 207 // .text : 004010C8 add esp, 8 208 wprintf(L"OpenProcessToken error: %u\n", GetLastError()); 209 // .text : 004010CB xor eax, eax 210 // .text : 004010CD mov ecx, [ebp + var_4] 211 // .text : 004010D0 xor ecx, ebp 212 // .text : 004010D2 call sub_401395 213 // .text : 004010D7 mov esp, ebp 214 // .text : 004010D9 pop ebp 215 // .text : 004010DA retn 216 return 0; 217 } 218 // .text : 004010DB loc_4010DB : ; CODE XREF : sub_401090 + 25↑j 219 // .text : 004010DB lea ecx, [ebp + Luid] 220 // .text : 004010DE push ecx; lpLuid 221 // .text:004010DF push offset Name; "SeDebugPrivilege" 222 // .text:004010E4 push 0; lpSystemName 223 // .text:004010E6 call ds : LookupPrivilegeValueW 224 LookupPrivilegeValueW(0, L"SeDebugPrivilege", &Luid); 225 // .text : 004010EC test eax, eax 226 // .text : 004010EE jnz short loc_401114 227 if (LookupPrivilegeValueW(0, L"SeDebugPrivilege", &Luid) == NULL) 228 { 229 // .text : 004010F0 call ds : GetLastError 230 // .text : 004010F6 push eax 231 // .text : 004010F7 push offset aLookupprivileg; "LookupPrivilegeValue error: %u\n" 232 // .text:004010FC call _wprintf 233 // .text : 00401101 add esp, 8 234 wprintf(L"LookupPrivilegeValue error: %u\n", GetLastError()); 235 // .text : 00401104 xor eax, eax 236 // .text : 00401106 mov ecx, [ebp + var_4] 237 // .text : 00401109xor ecx, ebp 238 // .text:0040110B call sub_401395 239 // .text : 00401110 mov esp, ebp 240 // .text : 00401112 pop ebp 241 // .text : 00401113 retn 242 return 0; 243 } 244 // .text : 00401114 loc_401114 : ; CODE XREF : sub_401090 + 5E↑j 245 // .text : 00401114 mov edx, [ebp + Luid.LowPart] 246 // .text : 00401117 mov eax, [ebp + Luid.HighPart] 247 // .text : 0040111A push 0; ReturnLength 248 // .text:0040111C push 0; PreviousState 249 // .text:0040111E push 10h; BufferLength 250 251 // .text:00401120 lea ecx, [ebp + NewState] 252 // .text : 00401123 push ecx; NewState 253 254 // .text:00401124 mov[ebp + NewState.Privileges.Luid.LowPart], edx 255 NewState.Privileges->Luid.LowPart = Luid.LowPart; 256 // .text : 00401127 mov edx, [ebp + TokenHandle] 257 // .text : 0040112A push 0; DisableAllPrivileges 258 // .text:0040112C push edx; TokenHandle 259 260 // .text:0040112D mov[ebp + NewState.PrivilegeCount], 1 261 NewState.PrivilegeCount = 1; 262 // .text : 00401134 mov[ebp + NewState.Privileges.Luid.HighPart], eax 263 NewState.Privileges->Luid.HighPart = Luid.HighPart; 264 // .text : 00401137 mov[ebp + NewState.Privileges.Attributes], 2 265 NewState.Privileges->Attributes = 2; 266 // .text : 0040113E call ds : AdjustTokenPrivileges 267 268 // .text : 00401144 test eax, eax 269 // .text : 00401146 jnz short loc_40116C 270 if (AdjustTokenPrivileges(TokenHandle, 0, &NewState, 0x10, 0, 0) == NULL) 271 { 272 273 // .text : 00401148 call ds : GetLastError 274 // .text : 0040114E push eax 275 // .text : 0040114F push offset aAdjusttokenpri; "AdjustTokenPrivileges error: %u\n" 276 // .text:00401154 call _wprintf 277 // .text : 00401159 add esp, 8 278 wprintf(L"AdjustTokenPrivileges error: %u\n", GetLastError()); 279 // .text:0040115C xor eax, eax 280 // .text : 0040115E mov ecx, [ebp + var_4] 281 // .text : 00401161 xor ecx, ebp 282 // .text : 00401163 call sub_401395 283 // .text : 00401168 mov esp, ebp 284 // .text:0040116A pop ebp 285 // .text : 0040116B retn 286 return 0; 287 288 } 289 290 // .text : 0040116C loc_40116C : ; CODE XREF : sub_401090 + B6↑j 291 // .text : 0040116C call ds : GetLastError 292 // .text : 00401172 cmp eax, 514h 293 // .text : 00401177 jnz short loc_401196 294 if(GetLastError() != 0x514) 295 { 296 // .text : 00401179 push offset aTheTokenDoesNo; "The token does not have the specified p"... 297 // .text:0040117E call _wprintf 298 // .text : 00401183 add esp, 4 299 wprintf(L"The token does not have the specified privilege. "); 300 // .text:00401186 xor eax, eax 301 // .text:00401188 mov ecx, [ebp + var_4] 302 // .text:0040118Bxor ecx, ebp 303 // .text:0040118D call sub_401395 304 // .text:00401192 mov esp, ebp 305 // .text:00401194 pop ebp 306 // .text:00401195 retn 307 return 0; 308 } 309 310 // .text:00401196 loc_401196 : ; CODE XREF : sub_401090 + E7↑j 311 // .text : 00401196 mov ecx, [ebp + var_4] 312 // .text:00401199xor ecx, ebp 313 // .text:0040119B mov eax, 1 314 // .text:004011A0 call sub_401395 315 // .text : 004011A5 mov esp, ebp 316 // .text : 004011A7 pop ebp 317 // .text : 004011A8 retn 318 // .text : 004011A8 sub_401090 endp*/ 319 return 1; 320 } 321 322 int GetProcessPid() 323 { 324 HANDLE hSnapshot = NULL; 325 // .text : 00401000 pe = PROCESSENTRY32W ptr - 230h 326 // .text : 00401000 var_4 = dword ptr - 4 327 PROCESSENTRY32 pe = { NULL }; 328 329 // .text : 00401016 push 0; th32ProcessID 330 // .text:00401018 push 0Fh; dwFlags 331 // .text:0040101A or ebx, 0FFFFFFFFh 332 // .text : 0040101D mov[ebp + pe.dwSize], 22Ch 333 pe.dwSize = sizeof(PROCESSENTRY32); 334 // .text : 00401027 call ds : CreateToolhelp32Snapshot 335 // .text : 0040102D mov esi, eax 336 hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL,0); 337 338 // .text : 0040102F lea eax, [ebp + pe] 339 // .text : 00401035 push eax; lppe 340 // .text:00401036 push esi; hSnapshot 341 // .text:00401037 call ds : Process32FirstW 342 Process32First(hSnapshot, &pe); 343 // .text : 0040103D mov edi, ds : Process32NextW 344 do 345 { 346 347 // .text : 00401043 loc_401043 : ; CODE XREF : sub_401000 + 67↓j 348 // .text : 00401043 lea ecx, [ebp + pe.szExeFile] 349 // .text : 00401049 push ecx; wchar_t* 350 // .text:0040104A push offset aNotepadExe; "notepad.exe" 351 // .text:0040104F call __wcsicmp 352 // .text : 00401054 add esp, 8 353 // .text : 00401057 test eax, eax 354 // .text : 00401059 jz short loc_40106B 355 356 // .text:0040105B lea edx, [ebp + pe] 357 // .text : 00401061 push edx; lppe 358 // .text:00401062 push esi; hSnapshot 359 // .text:00401063 call edi; Process32NextW 360 // .text:00401065 test eax, eax 361 362 // .text : 00401067 jnz short loc_401043 363 if (Process32NextW(hSnapshot, &pe) == FALSE) 364 { 365 CloseHandle(hSnapshot); 366 return 0; 367 } 368 // .text : 00401069 jmp short loc_401071 369 370 } while (_wcsicmp(L"QQ.exe", pe.szExeFile)); 371 // .text : 0040106B loc_40106B : ; CODE XREF : sub_401000 + 59↑j 372 // .text : 0040106B mov ebx, [ebp + pe.th32ProcessID] 373 // .text : 00401071 374 // .text : 00401071 loc_401071 : ; CODE XREF : sub_401000 + 69↑j 375 // .text : 00401071 push esi; hObject 376 // .text:00401072 call ds : CloseHandle 377 CloseHandle(hSnapshot); 378 // .text : 00401078 mov ecx, [ebp + var_4] 379 // .text:0040107B pop edi 380 // .text : 0040107C pop esi 381 // .text : 0040107D mov eax, ebx 382 // .text : 0040107F xor ecx, ebp 383 // .text : 00401081 pop ebx 384 // .text:00401082 call sub_401395 385 // .text:00401087 mov esp, ebp 386 // .text:00401089 pop ebp 387 // .text:0040108A retn 388 // .text:0040108A sub_401000 endp*/ 389 return pe.th32ProcessID; 390 } 391 392 int main() 393 { 394 const char* FileName = "QQ.exe"; 395 const char* DllName = "I:\\reversecore密example\\03\\24\\bin\\myhack.dll"; 396 // .text : 00401318 call sub_401000 397 int nRet = GetProcessPid(); 398 // .text:0040131D mov esi, eax 399 // .text : 0040131F cmp esi, 0FFFFFFFFh 400 // .text : 00401322 jnz short loc_401340 401 if(nRet == 0x0FFFFFFFF) 402 { 403 // .text : 00401324 push offset aNotepadExe; "notepad.exe" 404 // .text:00401329 push offset aThereIsNoSProc; "There is no <%s> process!\n" 405 // .text:0040132E call _wprintf 406 // .text : 00401333 add esp, 8 407 wprintf(L"There is no <%s> process!\n", FileName); 408 // .text : 00401336 loc_401336 : ; CODE XREF : _wmain + 4A↓j 409 // .text : 00401336 mov eax, 1 410 // .text : 0040133B pop esi 411 // .text : 0040133C mov esp, ebp 412 // .text : 0040133E pop ebp 413 // .text : 0040133F retn 414 return 1; 415 } 416 // .text : 00401340 loc_401340 : ; CODE XREF : _wmain + 12↑j 417 // .text : 00401340 push esi 418 // .text : 00401341 push offset aNotepadExe; "notepad.exe" 419 // .text:00401346 push offset aPidOfSIsD; "PID of \"%s\" is %d\n" 420 // .text:0040134B call _wprintf 421 // .text : 00401350 add esp, 0Ch 422 wprintf(L"PID of \"%s\" is %d\n", FileName, nRet); 423 424 // .text : 00401353 call sub_401090 425 // .text : 00401358 test eax, eax 426 // .text:0040135A jz short loc_401336 427 if(SetPrivilege()== 0) 428 { 429 return 1; 430 } 431 // .text : 0040135C mov ecx, esi; dwProcessId 432 // .text:0040135E call sub_4011B0 433 434 // .text : 00401363 push offset aMyhackDll; "myhack.dll" 435 // .text:00401368 push esi 436 437 // .text:00401369 test eax, eax 438 // .text:0040136B jz short loc_401381 439 if(Injection() != 0) 440 { 441 // .text : 0040136D push offset aEjectdllDSSucc; "EjectDll(%d, \"%s\") success!!!\n" 442 // .text:00401372 call _wprintf 443 // .text : 00401377 add esp, 0Ch 444 wprintf(L"EjectDll(%d, \"%s\") success!!!\n", nRet, DllName); 445 // .text : 0040137A xor eax, eax 446 // .text : 0040137C pop esi 447 // .text : 0040137D mov esp, ebp 448 // .text : 0040137F pop ebp 449 // .text : 00401380 retn 450 return 0; 451 } 452 453 // .text:00401381 loc_401381 : ; CODE XREF : _wmain + 5B↑j 454 // .text : 00401381 push offset aEjectdllDSFail; "EjectDll(%d, \"%s\") failed!!!\n" 455 // .text:00401386 call _wprintf 456 // .text:0040138B add esp, 0Ch 457 wprintf(L"EjectDll(%d, \"%s\") failed!!!\n", nRet, DllName); 458 // .text:0040138E xor eax, eax 459 // .text : 00401390 pop esi 460 // .text:00401391 mov esp, ebp 461 // .text:00401393 pop ebp 462 // .text:00401394 retn 463 // .text:00401394 _wmain */endp 464 return 0; 465 }
如有大佬路过请在评论区给我讲解一些,谢谢!
逆向中遇到的问题:
1.在窄字符api逆向时变成了宽字符api这个怎么判断
2.一些系统宏又如何还原
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!