Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://waleedassar.blogspot.com
- //http://www.twitter.com/waleedassar
- #include "stdafx.h"
- #include "windows.h"
- #include "stdio.h"
- #include "resource.h"
- typedef void(__stdcall *FUNC)(char*);
- extern "C"
- {
- IMAGE_NT_HEADERS* __stdcall RtlImageNtHeader(unsigned long ImageBase);
- void __stdcall walied(char* string)
- {
- printf("Hey %s\r\n",string);
- }
- }
- void ConvertHeader(IMAGE_NT_HEADERS* pNT)
- {
- pNT->FileHeader.Machine=0x8664; //Change Machine
- unsigned long numSections=pNT->FileHeader.NumberOfSections;
- unsigned long szOptional=pNT->FileHeader.SizeOfOptionalHeader;
- IMAGE_OPTIONAL_HEADER* pOpt32=&(pNT->OptionalHeader);
- IMAGE_SECTION_HEADER* pSec=(IMAGE_SECTION_HEADER*)(((unsigned char*)(pOpt32))+szOptional);
- IMAGE_DATA_DIRECTORY* pDD =(IMAGE_DATA_DIRECTORY*)((unsigned char*)pOpt32+0x60);
- //---------Backup Data Directories------------
- unsigned long szDD=((char*)pSec)-((char*)pDD);
- IMAGE_DATA_DIRECTORY* pBDD=(IMAGE_DATA_DIRECTORY*)LocalAlloc(LMEM_ZEROINIT,szDD);
- memcpy(pBDD,pDD,szDD);
- //---------Backup section table---------------
- unsigned szSections=numSections*sizeof(IMAGE_SECTION_HEADER);
- IMAGE_SECTION_HEADER* pBSections=(IMAGE_SECTION_HEADER*)LocalAlloc(LMEM_ZEROINIT,szSections);
- memcpy(pBSections,pSec,szSections);
- //---------------------------------------------
- pOpt32->Magic=0x020B;
- pOpt32->BaseOfData=pOpt32->ImageBase;
- pOpt32->ImageBase=0;
- unsigned long StkRsv=(pOpt32->SizeOfStackReserve);
- unsigned long StkCmt=(pOpt32->SizeOfStackCommit);
- unsigned long HpRsv=(pOpt32->SizeOfHeapReserve);
- unsigned long HpCmt=(pOpt32->SizeOfHeapCommit);
- unsigned long LoaderFlags=pOpt32->LoaderFlags;
- unsigned long NumberRVAs=pOpt32->NumberOfRvaAndSizes;
- IMAGE_OPTIONAL_HEADER64* pOpt64=(IMAGE_OPTIONAL_HEADER64*)pOpt32;
- *(unsigned long*)(&(pOpt64->SizeOfStackReserve))=StkRsv;
- *(unsigned long*)(&(pOpt64->SizeOfStackCommit)) =StkCmt;
- *(unsigned long*)(&(pOpt64->SizeOfHeapReserve)) =HpRsv;
- *(unsigned long*)(&(pOpt64->SizeOfHeapCommit)) =HpCmt;
- *(((unsigned long*)(&(pOpt64->SizeOfStackReserve)))+1)=0;
- *(((unsigned long*)(&(pOpt64->SizeOfStackCommit)))+1)=0;
- *(((unsigned long*)(&(pOpt64->SizeOfHeapReserve)))+1)=0;
- *(((unsigned long*)(&(pOpt64->SizeOfHeapCommit)))+1)=0;
- pOpt64->LoaderFlags=LoaderFlags;
- pOpt64->NumberOfRvaAndSizes=NumberRVAs;
- //---------------------------------------------
- memcpy(((char*)pDD)+0x10,pBDD,szDD);
- memcpy(((char*)pSec)+0x10,pBSections,szSections);
- //---------------------------------------------
- LocalFree(pBSections);
- LocalFree(pBDD);
- }
- int main(int argc, char* argv[])
- {
- unsigned long IB=(unsigned long)GetModuleHandle(0);
- unsigned long old;
- VirtualProtect((void*)IB,0x1000,PAGE_READWRITE,&old);
- //memset((void*)IB,0x0,0x1000);
- ConvertHeader(RtlImageNtHeader(IB));
- VirtualProtect((void*)IB,0x1000,old,&old);
- //----------------------To make sure PE header is usable-------------
- FUNC walied_=(FUNC)GetProcAddress((HMODULE)IB,"walied");
- char String1[0x100]={0};
- LoadString((HINSTANCE)IB,IDS_STRING1,String1,0x101);
- walied_(String1);
- memset(String1,0,0x100);
- LoadString((HINSTANCE)IB,IDS_STRING2,String1,0x101);
- walied_(String1);
- //-------------------------------------------------------------------
- int i=0;
- while(9)
- {
- printf("walied %x\r\n",i++);
- Sleep(1000);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement