したらばTOP ■掲示板に戻る■ 全部 1-100 最新50 | メール | |

管理人の独り言(プログラミング関連)

1117774さん:2008/12/17(水) 23:42:25 ID:CENbthlg0
if(p){
#define DESTOP(po) (void *)((long)(po) - (long)(des) + (long)(p))
memcpy(p, des, desSize);
//TODO
//.textセクションの挿入&エントリポイントの変更&アセンブリ(バイナリ)の挿入&情報の修正

IMAGE_NT_HEADERS32 *pImageNtHeader = (IMAGE_NT_HEADERS32 *)DESTOP(pDesNtHeader);

IMAGE_SECTION_HEADER *pImageSections = (IMAGE_SECTION_HEADER *)DESTOP(pDesSections);
IMAGE_SECTION_HEADER *pTextSection = (IMAGE_SECTION_HEADER *)DESTOP(pDesTextSection);

//.textセクションより後ろにあるものを移動&値変更
{
//.textセクションの開始位置(RVA)
DWORD pTextSectionPointerToRawData = pTextSection->PointerToRawData;
std::priority_queue<std::pair<DWORD, IMAGE_SECTION_HEADER *>, std::vector<std::pair<DWORD, IMAGE_SECTION_HEADER *> >, std::greater<std::vector<std::pair<DWORD, IMAGE_SECTION_HEADER *> >::value_type > > q;
for(int i = 0; i < desNumberOfSections; ++i){
if(pImageSections[i].PointerToRawData > pTextSectionPointerToRawData){
q.push(std::pair<DWORD, IMAGE_SECTION_HEADER *>(pImageSections[i].PointerToRawData, &pImageSections[i]));
pImageSections[i].PointerToRawData += desTextSectionAddition;
}
}

while(!q.empty()){
std::pair<DWORD, IMAGE_SECTION_HEADER *> &top = q.top();
void *sp = RVA(p, top.first);
memmove(RVA(sp, desTextSectionAddition), sp, top.second->SizeOfRawData);

q.pop();
}
}

//.textセクションを弄くる
{
void *sp = RVA(p, pTextSection->PointerToRawData);
memmove(RVA(sp, desTextSectionAddition), sp, pTextSection->SizeOfRawData);
static const BYTE code[] = {
0xE8, 0x00, 0x00, 0x00, 0x00, //CALL rel32
0xE9, 0x00, 0x00, 0x00, 0x00, //JMP rel32
};

memcpy(sp, code, sizeof(code));
memcpy(RVA(sp, sizeof(code)), RVA(src, pSrcTextSection->PointerToRawData), pSrcTextSection->SizeOfRawData);

//挿入元のEXEのエントリポイントの.textでの相対位置
DWORD srcEntryPoint = pSrcNtHeader->OptionalHeader.AddressOfEntryPoint - pSrcNtHeader->OptionalHeader.BaseOfCode;
//挿入先のEXEのエントリポイントの.textでの相対位置
DWORD desEntryPoint = pDesNtHeader->OptionalHeader.AddressOfEntryPoint - pDesNtHeader->OptionalHeader.BaseOfCode;

pImageNtHeader->OptionalHeader.AddressOfEntryPoint = pImageNtHeader->OptionalHeader.BaseOfCode;
*((DWORD *)RVA(sp, 1)) = sizeof(code) - 5 + srcEntryPoint;
*((DWORD *)RVA(sp, 6)) = desTextSectionAddition + desEntryPoint - 10;
}

//.textセクションの大きさの修正
{
pTextSection->SizeOfRawData = desAfterTextSectionSize;
pTextSection->Misc.VirtualSize = desAfterTextSectionVirtualSize;
}

FILE *filew = fopen("out.exe", "wb");
fwrite(p, fileSize, 1, filew);
VirtualFree(p, fileSize, MEM_DECOMMIT);
}
}
}
}
}


新着レスの表示


名前: E-mail(省略可)

※書き込む際の注意事項はこちら

※画像アップローダーはこちら

(画像を表示できるのは「画像リンクのサムネイル表示」がオンの掲示板に限ります)

掲示板管理者へ連絡 無料レンタル掲示板