Files
verify/shell/boot/bootstrap2.c
T

258 lines
8.5 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* vfy 引导 stub(x64)v2 —— 反射式加载:壳 DLL 从 .vfy 节直接映射进内存,不落盘
*
* 流程:解析自身 PE → 找 .vfy → 校验 CRC32 → 手动映射内嵌 DLL(头/节/重定位/导入)
* → 调 DllMain(ATTACH) → 找导出 VerifyMain → 调用(oep, cfg) → 退出
*
* 头布局 40 字节(.vfy 节起始):
* 0..3 magic "VFY3" | 4..7 ver
* 8..11 dllOff | 12..15 dllSize
* 16..19 cfgOff | 20..23 cfgSize
* 24..27 oepRva | 28..31 blobSize
* 32..35 flags | 36..39 dllCrc32
*/
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 void *(*fnVirtualAlloc)(void *, u64, u32, u32);
typedef BOOL (*fnVirtualProtect)(void *, u64, u32, u32 *);
typedef void (*fnExitProcess)(u32);
typedef struct {
char magic[4];
u32 ver;
u32 dllOff, dllSize;
u32 cfgOff, cfgSize;
u32 oepRva;
u32 blobSize;
u32 flags;
u32 dllCrc32;
} VFYHDR;
/* ---------- 小工具 ---------- */
static int mem_eq(const u8 *a, const char *b) {
for (int i = 0; b[i]; i++) if (a[i] != (u8)b[i]) return 0;
return 1;
}
static void mem_cpy(u8 *d, const u8 *s, u64 n) {
for (u64 i = 0; i < n; i++) d[i] = s[i];
}
static void mem_zero(u8 *d, u64 n) {
for (u64 i = 0; i < n; i++) d[i] = 0;
}
static u32 crc32_of(const u8 *p, u32 n) {
u32 c = 0xFFFFFFFFu;
for (u32 i = 0; i < n; i++) {
c ^= p[i];
for (int k = 0; k < 8; k++) c = (c & 1) ? (0xEDB88320u ^ (c >> 1)) : (c >> 1);
}
return c ^ 0xFFFFFFFFu;
}
__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;
/* 自身 PE → .vfy */
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 *dllSrc = secBase + hdr->dllOff;
const char *cfg = (const char *)(secBase + hdr->cfgOff);
/* kernel32 */
u64 ldr = *(u64 *)(peb + 0x18);
u64 head = ldr + 0x20;
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) {
const u16 *w = (const u16 *)nameBuf;
int ok = 1;
for (int k = 0; k < 12; k++) {
u16 c = w[k];
if (c >= 'A' && c <= 'Z') c = (u16)(c + 32);
if (c != (u16)"kernel32.dll"[k]) { ok = 0; break; }
}
if (ok) { kern = dllBase; break; }
}
cur = *(u64 *)(cur);
}
if (!kern) return 3;
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;
for (u32 i = 0; i < numNames; i++) {
const u8 *nm = kb + names[i];
if (mem_eq(nm, "GetProcAddress")) pGPA = (fnGetProcAddress)(kb + funcs[ords[i]]);
if (mem_eq(nm, "LoadLibraryA")) pLLA = (fnLoadLibraryA)(kb + funcs[ords[i]]);
if (pGPA && pLLA) break;
}
if (!pGPA || !pLLA) return 4;
fnVirtualAlloc pAlloc = (fnVirtualAlloc)pGPA((HMODULE)kern, "VirtualAlloc");
fnVirtualProtect pProt = (fnVirtualProtect)pGPA((HMODULE)kern, "VirtualProtect");
fnExitProcess pExit = (fnExitProcess)pGPA((HMODULE)kern, "ExitProcess");
if (!pAlloc || !pExit) return 5;
/* 完整性:.vfy 里内嵌 DLL 的 CRC32 */
if (hdr->dllCrc32) {
u32 got = crc32_of(dllSrc, hdr->dllSize);
if (got != hdr->dllCrc32) return 6;
}
/* ---- 手动映射 DLL ---- */
u8 *src = dllSrc;
u32 dl = *(u32 *)(src + 0x3C);
u8 *dnt = src + dl;
u16 dsecs = *(u16 *)(dnt + 6);
u16 doptSize = *(u16 *)(dnt + 20);
u8 *dopt = dnt + 24;
u64 prefBase = *(u64 *)(dopt + 24);
u32 sizeOfImage = *(u32 *)(dopt + 56);
u32 sizeOfHeaders = *(u32 *)(dopt + 60);
u32 entryRVA = *(u32 *)(dopt + 16);
u32 importRVA = *(u32 *)(dopt + 112 + 8);
u32 relocRVA = *(u32 *)(dopt + 112 + 40);
u32 relocSize = *(u32 *)(dopt + 112 + 44);
u8 *dsec = dopt + doptSize;
u8 *base = (u8 *)pAlloc((void *)0, sizeOfImage, 0x3000 /*COMMIT|RESERVE*/, 0x40 /*RWX*/);
if (!base) return 7;
/* 头 + 节 */
mem_cpy(base, src, sizeOfHeaders);
for (u16 i = 0; i < dsecs; i++) {
u8 *s = dsec + i * 40;
u32 va = *(u32 *)(s + 12);
u32 rawSize = *(u32 *)(s + 16);
u32 rawPtr = *(u32 *)(s + 20);
if (rawSize && rawPtr) mem_cpy(base + va, src + rawPtr, rawSize);
}
/* 重定位 */
if (relocRVA && relocSize && (u64)base != prefBase) {
u64 delta = (u64)base - prefBase;
u8 *r = base + relocRVA;
u8 *rend = r + relocSize;
while (r + 8 <= rend) {
u32 pageRVA = *(u32 *)r;
u32 blockSize = *(u32 *)(r + 4);
if (!blockSize) break;
u16 *ent = (u16 *)(r + 8);
int cnt = (int)(blockSize - 8) / 2;
for (int i = 0; i < cnt; i++) {
u16 e = ent[i];
u16 type = (u16)(e >> 12);
u16 off = (u16)(e & 0x0FFF);
if (type == 10) { /* DIR64 */
u64 *p = (u64 *)(base + pageRVA + off);
*p += delta;
} else if (type == 0) {
/* pad */
}
}
r += blockSize;
}
}
/* 导入 */
if (importRVA) {
u8 *desc = base + importRVA;
for (;;) {
u32 oft = *(u32 *)desc;
u32 nameRVA = *(u32 *)(desc + 12);
u32 ft = *(u32 *)(desc + 16);
if (!nameRVA && !ft && !oft) break;
const char *dllName = (const char *)(base + nameRVA);
HMODULE hm = pLLA(dllName);
if (!hm) return 8;
u64 *thunk = (u64 *)(base + (oft ? oft : ft));
u64 *iat = (u64 *)(base + ft);
for (int k = 0; thunk[k]; k++) {
u64 t = thunk[k];
if (t & 0x8000000000000000ull) {
iat[k] = (u64)pGPA(hm, (const char *)(t & 0xFFFF));
} else {
const char *fnName = (const char *)(base + (u32)t + 2); /* 跳过 Hint */
iat[k] = (u64)pGPA(hm, fnName);
}
if (!iat[k]) return 9;
}
desc += 20;
}
}
/* DllMain(DLL_PROCESS_ATTACH) */
typedef BOOL (*fnDllMain)(HMODULE, u32, void *);
if (entryRVA) {
fnDllMain dm = (fnDllMain)(base + entryRVA);
dm((HMODULE)base, 1, 0);
}
/* 导出 VerifyMain(在映射镜像里查导出表) */
u32 dexpRVA = *(u32 *)(dopt + 112);
if (!dexpRVA) return 10;
u8 *dexp = base + dexpRVA;
u32 dnumNames = *(u32 *)(dexp + 24);
u32 *dnames = (u32 *)(base + *(u32 *)(dexp + 32));
u16 *dords = (u16 *)(base + *(u32 *)(dexp + 36));
u32 *dfuncs = (u32 *)(base + *(u32 *)(dexp + 28));
u32 vmRVA = 0;
for (u32 i = 0; i < dnumNames; i++) {
const u8 *nm = base + dnames[i];
if (mem_eq(nm, "VerifyMain")) { vmRVA = dfuncs[dords[i]]; break; }
}
if (!vmRVA) return 11;
typedef int (*PFN)(void *, const char *);
PFN fn = (PFN)(base + vmRVA);
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;
}