Vorwort
Bei der Malware-Analyse tritt häufig Shellcode auf. Der separat ausgegebene Shellcode ist in IDA nur schwer statisch zu analysieren. Da der Windows Loader jedoch keinen unabhängigen Shellcode ausführen kann, müssen Tools zum Laden des Shellcodes für das dynamische Debuggen verwendet werden. .
Es gibt zwar bereits ein sehr nützliches Tool BlobRunner , aber ich bin immer noch bereit, eines zu schreiben, um besser zu lernen und zu verstehen
Code
#include <windows.h>
#include <stdio.h>
LPVOID read_shellcodefile_into_memory(char* shellcode)
{
FILE* hFile = NULL;
DWORD dwFileSize = 0;
hFile = fopen(shellcode, "rb");
if (!hFile)
{
printf(" [!] File open fail\n");
return NULL;
}
fseek(hFile, 0, SEEK_END);
dwFileSize = ftell(hFile) + 1;
printf(" [*] Shellcode Size: 0x%04x\n", dwFileSize);
fseek(hFile, 0, SEEK_SET);
LPVOID lpBase = VirtualAlloc(NULL, dwFileSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
printf(" [*] Allocated Address: 0x%08x\n", lpBase);
fread(lpBase, dwFileSize, 1, hFile);
fclose(hFile);
return lpBase;
}
int execute(int entry)
{
DWORD dwId;
DWORD dwStatus;
LPVOID bReadBuffer;
SIZE_T nReadSize = 0;
HANDLE hHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)entry, NULL, 0x4, &dwId);
if (!hHandle)
{
printf(" [!] CreateThread Failed!\n");
return -1;
}
printf(" [*] Please jmp to 0x%08x set a breakpoint\n\ Then press any key to resume the thread\n", entry);
getchar();
ResumeThread(hHandle);
while (1)
{
dwStatus = WaitForSingleObject(hHandle, 0);
if (dwStatus == WAIT_FAILED || dwStatus == WAIT_OBJECT_0)
{
CloseHandle(hHandle);
printf(" [*] Thread Exited!\n");
ExitThread(-1);
}
}
}
int main(int argc, char* argv[])
{
int nEntry = 0;
if (argc < 2)
{
printf(" [!] Please input the shellcode filename on the parameter\n");
return -1;
}
printf(" [*] Shellcode File: %s\n", argv[1]);
LPVOID lpBase = read_shellcodefile_into_memory(argv[1]);
if (!lpBase)
{
printf(" [!] Allocated memory failed!\n");
return -2;
}
nEntry = (int)lpBase;
printf(" [*] Shellcode EntryPoint: 0x%08x\n", nEntry);
execute(nEntry);
return 0;
}
Anleitung
Verwenden Sie zunächst das Metasploit-Tool msfvenom, um schnell den Shellcode zu generieren, der mit winexec einen Taschenrechner erstellt
msfvenom -a x86 --platform windows -p windows/exec cmd=calc.exe -o calc.bin
Verwenden Sie OD, um das kompilierte Tool zu öffnen und den Namen der Shellcode-Datei als Parameter einzugeben
Um zu programmieren, springen Sie zum Shellcode-Einstiegspunkt, um Haltepunkte festzulegen
Drücken Sie nach dem Festlegen des Haltepunkts eine beliebige Taste, um das Programm im Shellcode zu unterbrechen, und Sie können dann debuggen