本帖最后由 aswcy815174418 于 2021-6-16 12:42 编辑
我写的代码:貌似不会修复,任意程序都可以拿来测试
软件:
点击下载
#include <iostream>
#include <windows.h>
DWORD RVATranformFOA(IMAGE_NT_HEADERS* nt ,IMAGE_SECTION_HEADER** sectionArr ,DWORD virAddr) {
if ( virAddr == 0 ) {
return 0;
}
int i = 0;
for ( ; i < nt->FileHeader.NumberOfSections; i++ ) {
if ( sectionArr[i]->VirtualAddress >= virAddr ) {
//printf("%x %x %x\n", virAddr, sectionArr[i]->VirtualAddress, sectionArr[i]->PointerToRawData);
if ( sectionArr[i]->VirtualAddress == virAddr ) {
return sectionArr[i]->PointerToRawData;
}
i == 0 ? i++ : i;
return virAddr - sectionArr[i - 1]->VirtualAddress + sectionArr[i - 1]->PointerToRawData;
}
}
return virAddr - sectionArr[i - 1]->VirtualAddress + sectionArr[i - 1]->PointerToRawData;
}
VOID gengXin(IMAGE_NT_HEADERS* nt ,IMAGE_SECTION_HEADER**& sectionArr) {
sectionArr = (IMAGE_SECTION_HEADER**)malloc(4 * nt->FileHeader.NumberOfSections);
for ( int i = 0; i < nt->FileHeader.NumberOfSections; i++ ) {
if ( nt->OptionalHeader.Magic == 0x10b ) {
sectionArr[i] = (IMAGE_SECTION_HEADER*)((UINT)nt + sizeof(*nt) + 40 * i);
}
else {
sectionArr[i] = (IMAGE_SECTION_HEADER*)((UINT)nt + sizeof(*nt) + 40 * i + 0x10);
}
//printf("%x\n", sectionArr[i]);
}
}
int main(){
STARTUPINFOA si = { 0 };
si.cb = sizeof(si);
PROCESS_INFORMATION pi = { 0 };
HANDLE handle = CreateFile("D:\\小工具\\ttt.exe" ,GENERIC_READ | GENERIC_WRITE ,FILE_SHARE_WRITE ,0 ,OPEN_EXISTING ,0 ,0);
HANDLE handleMap = CreateFileMapping(handle ,0 ,PAGE_READWRITE ,0 ,0 ,0);
LPVOID handleView = MapViewOfFile(handleMap ,FILE_SHARE_WRITE ,0 ,0 ,0);
IMAGE_DOS_HEADER* dos = (IMAGE_DOS_HEADER*)handleView;
IMAGE_NT_HEADERS* nt = (IMAGE_NT_HEADERS*)((UINT)dos + dos->e_lfanew);
DWORD SizeOfImage = nt->OptionalHeader.SizeOfImage;
DWORD ImageBase = nt->OptionalHeader.ImageBase;
CloseHandle(handle);
CloseHandle(handleMap);
UnmapViewOfFile(handleView);
char* readAddr = (char*)malloc(SizeOfImage);
ZeroMemory(readAddr ,SizeOfImage);
BOOL isAccess = CreateProcess("D:\\小工具\\ttt.exe" ,0 ,0 ,0 ,0 ,CREATE_NEW_CONSOLE ,0 ,0 ,&si ,&pi);
if ( !isAccess ) {
printf("打开进程失败");
return -1;
}
ReadProcessMemory(pi.hProcess ,(LPVOID)ImageBase ,readAddr ,SizeOfImage ,0);
dos = (IMAGE_DOS_HEADER*)readAddr;
nt = (IMAGE_NT_HEADERS*)((UINT)dos + dos->e_lfanew);
IMAGE_SECTION_HEADER** sectionArr;
gengXin(nt ,sectionArr);
DWORD RVA = nt->OptionalHeader.DataDirectory[1].VirtualAddress;
IMAGE_IMPORT_DESCRIPTOR* import = (IMAGE_IMPORT_DESCRIPTOR*)( RVA + (UINT)dos);
char* strtt = (char*)(import->Name + (UINT)dos);
printf("%s\n" ,strtt);
DWORD* addr = (DWORD*)(import->FirstThunk + (UINT)dos);
if ( *addr & 0x80000000 ) {
printf("导出序号为:%d\n" ,*addr & 0x7FFFFFFF);
}
else {
IMAGE_IMPORT_BY_NAME* name = (IMAGE_IMPORT_BY_NAME*)(*addr + (UINT)dos);
printf("%s\n" ,name);
}
CloseHandle(handleMap);
UnmapViewOfFile(handleView);
}
|