Files

204 lines
6.8 KiB
C
Raw Permalink Normal View History

/* vfy 引导 stub(x64)—— 裸代码,无 CRT、无导入表
* 解析自身 PE → 找 .vfy → PEB 拿 kernel32 → 解析导出 → 释放并加载 shell DLL
* → VerifyMain(oep, cfg) → 退出
* 头布局 40 字节(.vfy 节起始): magic"VFY3"|ver|dllOff|dllSize|cfgOff|cfgSize|oepRva|blobSize|flags|rsv
*/
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef void *HMODULE;
typedef void *HANDLE;
typedef int BOOL;
typedef HMODULE (*fnLoadLibraryA)(const char *);
typedef void *(*fnGetProcAddress)(HMODULE, const char *);
typedef u32 (*fnGetTempPathA)(u32, char *);
typedef HANDLE (*fnCreateFileA)(const char *, u32, u32, void *, u32, u32, HANDLE);
typedef BOOL (*fnWriteFile)(HANDLE, const void *, u32, u32 *, void *);
typedef BOOL (*fnCloseHandle)(HANDLE);
typedef u32 (*fnGetCurrentProcessId)(void);
typedef void (*fnExitProcess)(u32);
typedef struct {
char magic[4];
u32 ver;
u32 dllOff, dllSize;
u32 cfgOff, cfgSize;
u32 oepRva;
u32 blobSize;
u32 flags;
u32 reserved;
} VFYHDR;
/* UTF-16 模块名 vs ASCII 常量比较(不区分大小写) */
static int uname_eq(const u16 *w, const char *a, int n) {
for (int i = 0; i < n; i++) {
u16 c = w[i];
if (c >= 'A' && c <= 'Z') c = (u16)(c + 32);
if (c != (u8)a[i]) return 0;
}
return 1;
}
__attribute__((section(".blobhead"), used)) int blob_main(void) {
u64 peb = 0;
__asm__ volatile("movq %%gs:0x60, %0" : "=r"(peb));
if (!peb) return 1;
u64 imageBase = *(u64 *)(peb + 0x10);
if (!imageBase) return 1;
u8 *img = (u8 *)imageBase;
u32 lfanew = *(u32 *)(img + 0x3C);
u8 *nt = img + lfanew;
u16 numSec = *(u16 *)(nt + 6);
u16 optSize = *(u16 *)(nt + 20);
u8 *sec = nt + 24 + optSize;
VFYHDR *hdr = 0;
for (u16 i = 0; i < numSec; i++) {
u8 *s = sec + i * 40;
if (s[0] == '.' && s[1] == 'v' && s[2] == 'f' && s[3] == 'y') {
hdr = (VFYHDR *)(img + *(u32 *)(s + 12));
break;
}
}
if (!hdr) return 2;
if (!(hdr->magic[0] == 'V' && hdr->magic[1] == 'F' && hdr->magic[2] == 'Y' && hdr->magic[3] == '3')) return 2;
u8 *secBase = (u8 *)hdr;
u8 *dllBytes = secBase + hdr->dllOff;
const char *cfg = (const char *)(secBase + hdr->cfgOff);
/* kernel32 from PEB->Ldr->InMemoryOrderModuleList */
u64 ldr = *(u64 *)(peb + 0x18);
u64 head = ldr + 0x20; /* InMemoryOrderModuleList */
u64 cur = *(u64 *)(head);
u64 kern = 0;
for (int guard = 0; guard < 64 && cur && cur != head; guard++) {
u8 *entry = (u8 *)(cur - 0x10);
u64 dllBase = *(u64 *)(entry + 0x30);
u16 nameLen = *(u16 *)(entry + 0x58);
u64 nameBuf = *(u64 *)(entry + 0x60);
if (nameLen == 24 && nameBuf && uname_eq((const u16 *)nameBuf, "kernel32.dll", 12)) {
kern = dllBase;
break;
}
cur = *(u64 *)(cur);
}
if (!kern) return 3;
/* kernel32 export table */
u8 *kb = (u8 *)kern;
u32 kl = *(u32 *)(kb + 0x3C);
u8 *knt = kb + kl;
u32 expRVA = *(u32 *)(knt + 24 + 112);
if (!expRVA) return 4;
u8 *exp = kb + expRVA;
u32 numNames = *(u32 *)(exp + 24);
u32 *names = (u32 *)(kb + *(u32 *)(exp + 32));
u16 *ords = (u16 *)(kb + *(u32 *)(exp + 36));
u32 *funcs = (u32 *)(kb + *(u32 *)(exp + 28));
fnGetProcAddress pGPA = 0;
fnLoadLibraryA pLLA = 0;
static const char nGPA[] = "GetProcAddress";
static const char nLLA[] = "LoadLibraryA";
for (u32 i = 0; i < numNames; i++) {
const char *nm = (const char *)(kb + names[i]);
int ok1 = 1, ok2 = 1;
for (int k = 0; k < 14; k++) {
if (nm[k] != nGPA[k]) { ok1 = 0; break; }
if (!nGPA[k]) break;
}
for (int k = 0; k < 12; k++) {
if (nm[k] != nLLA[k]) { ok2 = 0; break; }
if (!nLLA[k]) break;
}
if (ok1) pGPA = (fnGetProcAddress)(kb + funcs[ords[i]]);
if (ok2) pLLA = (fnLoadLibraryA)(kb + funcs[ords[i]]);
if (pGPA && pLLA) break;
}
if (!pGPA || !pLLA) return 4;
static const char nTemp[] = "GetTempPathA";
static const char nCreate[] = "CreateFileA";
static const char nWrite[] = "WriteFile";
static const char nClose[] = "CloseHandle";
static const char nPid[] = "GetCurrentProcessId";
static const char nExit[] = "ExitProcess";
fnGetTempPathA pTemp = (fnGetTempPathA)pGPA((HMODULE)kern, nTemp);
fnCreateFileA pCreate = (fnCreateFileA)pGPA((HMODULE)kern, nCreate);
fnWriteFile pWrite = (fnWriteFile)pGPA((HMODULE)kern, nWrite);
fnCloseHandle pClose = (fnCloseHandle)pGPA((HMODULE)kern, nClose);
fnGetCurrentProcessId pPid = (fnGetCurrentProcessId)pGPA((HMODULE)kern, nPid);
fnExitProcess pExit = (fnExitProcess)pGPA((HMODULE)kern, nExit);
if (!pTemp || !pCreate || !pWrite || !pClose || !pPid || !pExit) return 5;
/* 阶段打点:写到 %TEMP%\vfy_boot.txt(覆盖) */
char tdir[260];
u32 tl = pTemp(260, tdir);
char dbg[300];
int dp = 0;
if (tl && tl < 250) {
for (u32 i = 0; i < tl; i++) dbg[dp++] = tdir[i];
static const char dn[] = "vfy_boot.txt";
for (int i = 0; dn[i]; i++) dbg[dp++] = dn[i];
dbg[dp] = 0;
} else dp = 0;
/* 释放 DLL */
char path[300];
int p = 0;
for (u32 i = 0; i < tl; i++) path[p++] = tdir[i];
static const char pre[] = "vfycore_";
for (int i = 0; pre[i]; i++) path[p++] = pre[i];
u32 pid = pPid();
char digits[12];
int dn2 = 0;
if (!pid) digits[dn2++] = '0';
while (pid && dn2 < 11) { digits[dn2++] = (char)('0' + pid % 10); pid /= 10; }
for (int i = dn2 - 1; i >= 0; i--) path[p++] = digits[i];
static const char ext[] = ".dll";
for (int i = 0; ext[i]; i++) path[p++] = ext[i];
path[p] = 0;
HANDLE hf = pCreate(path, 0x40000000u, 0, 0, 2, 0x80, 0);
if (hf == (HANDLE)-1 || !hf) return 7;
u32 wrote = 0;
if (!pWrite(hf, dllBytes, hdr->dllSize, &wrote, 0) || wrote != hdr->dllSize) {
pClose(hf);
return 7;
}
pClose(hf);
/* 加载并调用 */
HMODULE mod = pLLA(path);
static const char nMain[] = "VerifyMain";
typedef int (*PFN)(void *, const char *);
PFN fn = 0;
if (mod) fn = (PFN)pGPA(mod, nMain);
if (dp) {
HANDLE hd = pCreate(dbg, 0x40000000u, 0, 0, 2, 0x80, 0);
if (hd && hd != (HANDLE)-1) {
char line[8];
line[0] = mod ? 'M' : 'm';
line[1] = fn ? 'F' : 'f';
line[2] = '\n';
u32 w2 = 0;
pWrite(hd, line, 3, &w2, 0);
pClose(hd);
}
}
if (!mod) return 8;
if (!fn) return 9;
void *oep = (void *)(imageBase + hdr->oepRva);
int rc = fn(oep, cfg);
if (rc != 0) {
if (hdr->flags & 1) ((void (*)(void))oep)();
pExit(0);
}
return 0;
}