




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、从内存中加载并启动一个exe(C+ 版 )ccf6.html2009年 08 月 07 日 星期五上午01:00原理 :1. 把你的程序读要内存2. 以 CREATE_SUSPENDED 模式 CreateProcess 打开svchost.exe3. 修改 svchost.exe 页面的属性, 然后把要运行的那个程序的内容拷贝到 svchost.exe 页面4. 然后再运行实质想当于是披着 /svchost.exe进程的相关信息 /这张皮,而皮里面的肉都被改了原文来自哪里忘记了, 。呵呵 /#include <stdio.h>#include <windows.h>#
2、include <tlhelp32.h>/#include ntpsapi.hstruct PEHeaderunsigned long signature;unsigned short machine;unsigned short numSections;unsigned long timeDateStamp;unsigned long pointerToSymbolTable;unsigned long numOfSymbols;unsigned short sizeOfOptionHeader;unsigned short characteristics;typedef str
3、uct PEHeader PE_Header;struct PEExtHeaderunsigned short magic;unsigned char majorLinkerVersion;unsigned char minorLinkerVersion;unsigned long sizeOfCode;unsigned long sizeOfInitializedData;unsigned long sizeOfUninitializedData;unsigned long addressOfEntryPoint;unsigned long baseOfCode;unsigned long
4、baseOfData;unsigned long imageBase;unsigned long sectionAlignment;unsigned long fileAlignment;unsigned short majorOSVersion;unsigned short minorOSVersion;unsigned short majorImageVersion;unsigned short minorImageVersion;unsigned short majorSubsystemVersion;unsigned short minorSubsystemVersion;unsign
5、ed long reserved1;unsigned long sizeOfImage;unsigned long sizeOfHeaders;unsigned long checksum;unsigned short subsystem;unsigned short DLLCharacteristics;unsigned long sizeOfStackReserve;unsigned long sizeOfStackCommit;unsigned long sizeOfHeapReserve;unsigned long sizeOfHeapCommit;unsigned long load
6、erFlags;unsigned long numberOfRVAAndSizes;unsigned long exportTableAddress;unsigned long exportTableSize;unsigned long importTableAddress;unsigned long importTableSize;unsigned long resourceTableAddress;unsigned long resourceTableSize;unsigned long exceptionTableAddress;unsigned long exceptionTableS
7、ize;unsigned long certFilePointer;unsigned long certTableSize;unsigned long relocationTableAddress;unsigned long relocationTableSize;unsigned long debugDataAddress;unsigned long debugDataSize;unsigned long archDataAddress;unsigned long archDataSize;unsigned long globalPtrAddress;unsigned long global
8、PtrSize;unsigned long TLSTableAddress;unsigned long TLSTableSize;unsigned long loadConfigTableAddress;unsigned long loadConfigTableSize;unsigned long boundImportTableAddress;unsigned long boundImportTableSize;unsigned long importAddressTableAddress;unsigned long importAddressTableSize;unsigned long
9、delayImportDescAddress;unsigned long delayImportDescSize;unsigned long COMHeaderAddress;unsigned long COMHeaderSize;unsigned long reserved2;unsigned long reserved3;typedef struct PEExtHeader PE_ExtHeader;struct Section_Headerunsigned char sectionName8;unsigned long virtualSize;unsigned long virtualA
10、ddress;unsigned long sizeOfRawData;unsigned long pointerToRawData;unsigned long pointerToRelocations;unsigned long pointerToLineNumbers;unsigned short numberOfRelocations;unsigned short numberOfLineNumbers;unsigned long characteristics;typedef struct Section_Header SectionHeader;struct MZ_Headerunsi
11、gned short signature;unsigned short partPag;unsigned short pageCnt;unsigned short reloCnt;unsigned short hdrSize;unsigned short minMem;unsigned short maxMem;unsigned short reloSS;unsigned short exeSP;unsigned short chksum;unsigned short exeIP;unsigned short reloCS;unsigned short tablOff;unsigned sho
12、rt overlay;unsigned char reserved32;unsigned long offsetToPE;typedef struct MZ_Header MZHeader;struct Import_DirEntryDWORD importLookupTable;DWORD timeDateStamp;DWORD fowarderChain;DWORD nameRVA;DWORD importAddressTable;typedef struct Import_DirEntry ImportDirEntry;struct Fixup_Blockunsigned long pa
13、geRVA;unsigned long blockSize;typedef struct Fixup_Block FixupBlock;#define TARGETPROC svchost.exetypedef struct _PROCINFODWORD baseAddr;DWORD imageSize; PROCINFO;BOOL EXPD = FALSE;CHAR*PID;/*/ This function reads the MZ, PE, PE extended andSection Headers froman EXE file./*/ 解析 PE 文件,得到 PE 结构/BOOL
14、readPEInfo(FILE *fp, MZHeader *outMZ,PE_Header *outPE,PE_ExtHeader *outpeXH,SectionHeader *outSecHdr)MZHeader mzH;long fileSize;PE_Header peH;PE_ExtHeader peXH;SectionHeader *secHdr;fseek(fp, 0, SEEK_END);fileSize = ftell(fp);fseek(fp, 0, SEEK_SET); if(fileSize < sizeof(MZHeader)printf(File size
15、too smalln);return FALSE; / read MZ Headerfread(&mzH, sizeof(MZHeader), 1, fp);if(mzH.signature != 0x5a4d)/ MZprintf(File does not have MZ headern);return FALSE; printf(Offset to PE Header = %Xn,mzH.offsetToPE);if(unsigned long)fileSize <mzH.offsetToPE + sizeof(PE_Header)printf(File size too
16、smalln);return FALSE; / read PE Headerfseek(fp, mzH.offsetToPE, SEEK_SET);fread(&peH, sizeof(PE_Header), 1, fp); printf(Size of option header = %dn, peH.sizeOfOptionHeader);printf(Number of sections = %dn, peH.numSections); if(peH.sizeOfOptionHeader != sizeof(PE_ExtHeader)printf(Unexpected optio
17、n header size.n);return FALSE; / read PE Ext Headerfread(&peXH, sizeof(PE_ExtHeader), 1, fp);printf(Import table address = %Xn,peXH.importTableAddress);printf(Import table size = %Xn,peXH.importTableSize);printf(Import address table address = %Xn, peXH.importAddressTableAddress);printf(Import ad
18、dress table size = %Xn,peXH.importAddressTableSize);/ read the sectionssecHdr =(SectionHeader*)malloc( sizeof(SectionHeader)*(peH.numSections) ); fread(secHdr, sizeof(SectionHeader) * peH.numSections, 1, fp); *outMZ = mzH;*outPE = peH;*outpeXH = peXH;*outSecHdr = secHdr;return TRUE;/*/ This function
19、 calculates the size required to load an EXE into memorywith proper alignment./*/ 返回文件所占用的内存空间/int calcTotalImageSize(MZHeader *inMZ, PE_Header*inPE, PE_ExtHeader*inpeXH,SectionHeader *inSecHdr)int result = 0;int val, i;int alignment = inpeXH->sectionAlignment;if(inpeXH->sizeOfHeaders % alignm
20、ent = 0)/ PE 头对齐result += inpeXH->sizeOfHeaders;elseval = inpeXH->sizeOfHeaders / alignment;val+;result += (val * alignment);for(i = 0; i < inPE->numSections; i+) /节对齐if(inSecHdri.virtualSize)if(inSecHdri.virtualSize % alignment = 0)result += inSecHdri.virtualSize;elseint val = inSecHdri
21、.virtualSize / alignment;val+;result += (val * alignment); return result;/*/ This function calculates the aligned size of a section/*/ 返回真实在内存中占用的大小/unsigned long getAlignedSize(unsigned long curSize,unsigned longalignment)if(curSize % alignment = 0)return curSize;elseint val = curSize / alignment;v
22、al+;return (val * alignment);/*/ This function loads a PE file into memory with proper alignment./ Enough memory must be allocated at ptrLoc./*/ 加载 PE 文件到内存中/BOOL loadPE(FILE *fp, MZHeader *inMZ, PE_Header *inPE, PE_ExtHeader*inpeXH,SectionHeader *inSecHdr, LPVOID ptrLoc)unsigned long headerSize, re
23、adSize;int i;char *outPtr = (char *)ptrLoc; SEEK_SET);fseek(fp, 0,headerSize = inpeXH->sizeOfHeaders;PE files have sectionHeaderSize value > size of PEfile itself./ certain/ this loop handles this situation by find the section thatisnearest to the/ PE header./ 如果文件太小,以至与 PE 头中还包括了节的内容,这样就先不拷贝节
24、的内容/ 当然这种情况很少见/for(i = 0; i < inPE->numSections; i+)if(inSecHdri.pointerToRawData < headerSize)headerSize = inSecHdri.pointerToRawData; / read the PE headerreadSize = fread(outPtr, 1, headerSize, fp);printf(HeaderSize = %dn, headerSize);if(readSize != headerSize)printf(Error reading headers
25、 (%d %d)n, readSize, headerSize);return FALSE; / getAlignedSize返回真实占用的内存的大小/outPtr += getAlignedSize(inpeXH->sizeOfHeaders,inpeXH->sectionAlignment);/ read the sectionsfor(i = 0; i < inPE->numSections; i+)if(inSecHdri.sizeOfRawData > 0)unsigned long toRead =inSecHdri.sizeOfRawData;if(
26、toRead > inSecHdri.virtualSize)toRead = inSecHdri.virtualSize; fseek(fp, inSecHdri.pointerToRawData, SEEK_SET);readSize = fread(outPtr, 1, toRead, fp); if(readSize != toRead)printf(Error reading section %dn, i);return FALSE;outPtr += getAlignedSize(inSecHdri.virtualSize, inpeXH->sectionAlignme
27、nt);else/ this handles the case where the PE file has anemptysection. E.g. UPX0 section/ in UPXed files.if(inSecHdri.virtualSize)outPtr +=getAlignedSize(inSecHdri.virtualSize,inpeXH->sectionAlignment); return TRUE;/*/ This function loads a PE file into memory with proper alignment./ Enough memory
28、 must be allocated at ptrLoc./*void doRelocation(MZHeader *inMZ, PE_Header *inPE, PE_ExtHeader *inpeXH,SectionHeader *inSecHdr, LPVOID ptrLoc, DWORD newBase)long delta;int numEntries,i, relocType;unsigned short *offsetPtr;DWORD *codeLoc;FixupBlock *fixBlk;if(inpeXH->relocationTableAddress &&a
29、mp;inpeXH->relocationTableSize)fixBlk = (FixupBlock *)(char *)ptrLoc + inpeXH->relocationTableAddress);delta = newBase - inpeXH->imageBase; while(fixBlk->blockSize)printf(Addr = %Xn, fixBlk->pageRVA);printf(Size = %Xn, fixBlk->blockSize); numEntries = (fixBlk->blockSize - sizeof
30、(FixupBlock) >> 1;printf(Num Entries = %dn, numEntries);offsetPtr = (unsigned short *)(fixBlk + 1); for(i = 0; i < numEntries; i+)codeLoc = (DWORD *)(char *)ptrLoc +fixBlk->pageRVA +(*offsetPtr & 0x0FFF);relocType = (*offsetPtr & 0xF000)>> 12;printf(Val = %Xn, *offsetPtr);p
31、rintf(Type = %Xn, relocType); if(relocType = 3)*codeLoc = (DWORD)*codeLoc) + delta;elseprintf(Unknown relocation type = %dn,relocType);offsetPtr+;fixBlk = (FixupBlock *)offsetPtr;/*/ Creates the original EXE in suspended mode and returns its info inthe PROCINFO structure./*BOOL createChild(PPROCESS_
32、INFORMATION pi, / OUTPCONTEXT ctx,/OUTPROCINFO *outChildProcInfo/OUT)PROCINFO *outChildProcInfo2 = NULL;STARTUPINFO si = 0;DWORD read;DWORD *pebInfo;DWORD curAddr;MEMORY_BASIC_INFORMATION memInfo, memInfo2;DEBUG_EVENT DBEvent;DWORD read2, curAddr2;DWORD *pebInfo2;if(!EXPD)if(CreateProcess(NULL,TARGE
33、TPROC,NULL,NULL,0,CREATE_SUSPENDED,NULL,NULL,&si,pi)ctx->ContextFlags=CONTEXT_FULL;GetThreadContext(pi->hThread, ctx);/ /获取外壳进程运行状态,ctx.Ebx+8 内存处存的是外壳进程的加载基址, ctx.Eax 存放有外壳进程的入口地址pebInfo = (DWORD *)ctx->Ebx;ReadProcessMemory(pi->hProcess,&pebInfo2,(LPVOID)&(outChildProcInfo->baseAddr),sizeof(DWORD), &read);curAddr = outChildProcInfo->baseAddr;/在SVCHOST.EXE中寻找MEM_FREE的内存地址wh
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024自考专业(建筑工程)试题预测试卷(真题汇编)附答案详解
- 2025海外融资租赁合同
- 难点解析人教版9年级数学上册【旋转】专题攻克试题(含详细解析)
- 江西报考安全员模拟及答案
- 安全员考试测及答案
- 2024-2025学年度监理工程师高频难、易错点题及参考答案详解【达标题】
- 2023年度收银审核员综合提升测试卷含答案详解【达标题】
- 2025年自考专业(小学教育)试卷及参考答案【夺分金卷】
- 2024-2025学年度事业单位工勤技能考试题库检测试题打印【重点】附答案详解
- 自考专业(计算机信息管理)题库(轻巧夺冠)附答案详解
- 2025年成都市中考物理真题(含答案)
- 第8课《回忆鲁迅先生》课件+++2025-2026学年统编版语文八年级上册
- 库欣综合征护理查房
- 证券投资2025新改实训指导
- 弱猪护理培训课件
- 新能源项目开发专员岗位面试问题及答案
- 人人享有心理健康
- 积极向上树立正确人生态度主题班会课件
- 冷冻肺活检课件
- 大学生心理健康十六讲(第3版) 课件全套 第1讲 心理健康知多少-大学生心理健康导论-第16讲 珍爱生命-危机干预与幸福人生
- 催收管理培训课件
评论
0/150
提交评论