[原创]PE加载过程 FileBuffer-ImageBuffer
2022-9-23 01:18:48 Author: bbs.pediy.com(查看原文) 阅读量:6 收藏

LPSTR ----> typedef CHAR *LPSTR, *PSTR; 是一个char*指针;在WINNT.H头文件里面
LPVOID ----> typedef void far *LPVOID; 是一个void*指针,在WINDEF.H头文件里面

它是别名一个void far *类型的指针,其中far是以前针对16位系统的,而现在基本都是32位以上系统
所以这个far已经没有意义了,可以忽略,总结下来 LPVOID就是个void*指针类型

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

DWORD ToLoaderPE(LPSTR file_path, PVOID* pFileBuffer);

DWORD CopyFileBufferToImageBuffer(PVOID pFileBuffer, PVOID* pImageBuffer);

DWORD CopyImageBufferToNewFileBuffer(PVOID pImageBuffer, PVOID* pNewFileBuffer);

BOOL MemoryToFile(PVOID pMemBuffer, DWORD size, LPSTR lpszFile);

char file_path[] = "E:\\Reverse\\吾爱破解工具包2.0\\吾爱破解工具包\\Tools\\Others\\ipmsg.exe";

char write_file_path[] = "C:\\Users\\whl\\Desktop\\1.exe";

//返回PE文件大小

DWORD ToLoaderPE(LPSTR file_path, PVOID* pFileBuffer)

{

    FILE* pFile = NULL;

    DWORD FileSize = 0;

    PVOID pFileBufferTemp = NULL;

    pFile = fopen(file_path, "rb");

    if (!pFile)

    {

        printf("(ToLoaderPE)Can't open file!\n");

        return 0;

    }

    fseek(pFile, 0, SEEK_END);

    FileSize = ftell(pFile);

    printf("FileBuffer: %#x\n", FileSize);

    fseek(pFile, 0, SEEK_SET);

    pFileBufferTemp = malloc(FileSize);

    if (!pFileBufferTemp)

    {

        printf("(ToLoaderPE)Allocate dynamic memory failed!\n");

        fclose(pFile);

        return 0;

    }

    DWORD n = fread(pFileBufferTemp, FileSize, 1, pFile);

    if (!n)

    {

        printf("(ToLoaderPE)Read file failed!\n");

        free(pFileBufferTemp);

        fclose(pFile);

        return 0;

    }

    *pFileBuffer = pFileBufferTemp;

    pFileBufferTemp = NULL;

    fclose(pFile);

    return FileSize;

}

DWORD CopyFileBufferToImageBuffer(PVOID pFileBuffer, PVOID* pImageBuffer)

{

    PIMAGE_DOS_HEADER pDosHeader = NULL;

    PIMAGE_NT_HEADERS pNTHeader = NULL;

    PIMAGE_FILE_HEADER pPEHeader = NULL;

    PIMAGE_OPTIONAL_HEADER32 pOptionHeader = NULL;

    PIMAGE_SECTION_HEADER pSectionHeader = NULL;

    PVOID pImageTemp = NULL;

    if (!pFileBuffer)

    {

        printf("(CopyFileBufferToImageBuffer)Can't open file!\n");

        return 0;

    }

    if (*((PWORD)pFileBuffer) != IMAGE_DOS_SIGNATURE)

    {

        printf("(CopyFileBufferToImageBuffer)No MZ flag, not exe file!\n");

        return 0;

    }

    pDosHeader = (PIMAGE_DOS_HEADER)pFileBuffer;

    if (*((LPDWORD)((DWORD)pFileBuffer + pDosHeader->e_lfanew)) != IMAGE_NT_SIGNATURE)

    {

        printf("(CopyFileBufferToImageBuffer)Not a valid PE flag!\n");

        return 0;

    }

    pNTHeader = (PIMAGE_NT_HEADERS)((DWORD)pFileBuffer + pDosHeader->e_lfanew);

    pPEHeader = (PIMAGE_FILE_HEADER)(((DWORD)pNTHeader) + 4);

    pOptionHeader = (PIMAGE_OPTIONAL_HEADER32)((DWORD)pPEHeader + IMAGE_SIZEOF_FILE_HEADER);

    pSectionHeader = (PIMAGE_SECTION_HEADER)((DWORD)pOptionHeader + pPEHeader->SizeOfOptionalHeader);

    pImageTemp = malloc(pOptionHeader->SizeOfImage);

    if (!pImageTemp)

    {

        printf("(CopyFileBufferToImageBuffer)Allocate dynamic memory failed!\n");

        free(pImageTemp);

        return 0;

    }

    memset(pImageTemp, 0, pOptionHeader->SizeOfImage);

    memcpy(pImageTemp, pDosHeader, pOptionHeader->SizeOfHeaders);

    PIMAGE_SECTION_HEADER pSectionHeaderTemp = pSectionHeader;

    for (int n = 0; n < pPEHeader->NumberOfSections; n++, pSectionHeaderTemp++)

    {

        memcpy((PVOID)((DWORD)pImageTemp + pSectionHeaderTemp->VirtualAddress), (PVOID)((DWORD)pFileBuffer + pSectionHeaderTemp->PointerToRawData), pSectionHeaderTemp->SizeOfRawData);

        printf("VirtualAddress%d: %#10x         PointerToRawData%d: %#10x\n", n, (DWORD)pImageTemp + pSectionHeader->VirtualAddress, n, (DWORD)pFileBuffer + pSectionHeader->PointerToRawData);

    }

    *pImageBuffer = pImageTemp;

    pImageTemp = NULL;

    return pOptionHeader->SizeOfImage;

}

DWORD CopyImageBufferToNewFileBuffer(PVOID pImageBuffer, PVOID* pNewFileBuffer)

{

    PIMAGE_DOS_HEADER pDosHeader = NULL;

    PIMAGE_NT_HEADERS pNTHeader = NULL;

    PIMAGE_FILE_HEADER pPEHeader = NULL;

    PIMAGE_OPTIONAL_HEADER32 pOptionHeader = NULL;

    PIMAGE_SECTION_HEADER pSectionHeader = NULL;

    LPVOID pTempNewbuffer = NULL;

    if (!pImageBuffer)

    {

        printf("(CopyImageBufferToNewBuffer)Can't open file!\n");

        return 0;

    }

    if (*((PWORD)pImageBuffer) != IMAGE_DOS_SIGNATURE)

    {

        printf("(CopyImageBufferToNewBuffer)No MZ flag, not exe file!\n");

        return 0;

    }

    pDosHeader = (PIMAGE_DOS_HEADER)pImageBuffer;

    if (*((PDWORD)((DWORD)pImageBuffer + pDosHeader->e_lfanew)) != IMAGE_NT_SIGNATURE)

    {

        printf("(CopyImageBufferToNewBuffer)Not a valid PE flag!\n");

        return 0;

    }

    pNTHeader = (PIMAGE_NT_HEADERS)((DWORD)pImageBuffer + pDosHeader->e_lfanew);

    pPEHeader = (PIMAGE_FILE_HEADER)((DWORD)pNTHeader + 4); // 这里必须强制类型转换

    pOptionHeader = (PIMAGE_OPTIONAL_HEADER32)((DWORD)pPEHeader + IMAGE_SIZEOF_FILE_HEADER);

    pSectionHeader = (PIMAGE_SECTION_HEADER)((DWORD)pOptionHeader + pPEHeader->SizeOfOptionalHeader);

    //获取new_buffer的大小

    int new_buffer_size = pOptionHeader->SizeOfHeaders;

    for (DWORD i = 0; i < pPEHeader->NumberOfSections; i++)

    {

        new_buffer_size += pSectionHeader[i].SizeOfRawData;  // pSectionHeader[i]另一种加法

    }

    // 分配内存(newbuffer)

    pTempNewbuffer = malloc(new_buffer_size);

    if (!pTempNewbuffer)

    {

        printf("(CopyImageBufferToNewBuffer)Allocate dynamic memory failed!\n");

        return 0;

    }

    memset(pTempNewbuffer, 0, new_buffer_size);

    memcpy(pTempNewbuffer, pDosHeader, pOptionHeader->SizeOfHeaders);

    // 循环拷贝节区

    PIMAGE_SECTION_HEADER pTempSectionHeader = pSectionHeader;

    for (DWORD j = 0; j < pPEHeader->NumberOfSections; j++, pTempSectionHeader++)

    {    //PointerToRawData节区在文件中的偏移,VirtualAddress节区在内存中的偏移地址,SizeOfRawData节在文件中对齐后的尺寸

        memcpy((PDWORD)((DWORD)pTempNewbuffer + pTempSectionHeader->PointerToRawData), (PDWORD)((DWORD)pImageBuffer + pTempSectionHeader->VirtualAddress), pTempSectionHeader->SizeOfRawData);

    }

    //返回数据

    *pNewFileBuffer = pTempNewbuffer; //暂存的数据传给参数后释放

    pTempNewbuffer = NULL;

    return new_buffer_size;  // 返回计算得到的分配内存的大小

}

BOOL MemoryToFile(PVOID pMemBuffer, DWORD size, LPSTR lpszFile)

{

    FILE* fp;

    fp = fopen(lpszFile, "wb");

    if (fp != NULL)

    {

        fwrite(pMemBuffer, size, 1, fp);

    }

    fclose(fp);

    return 1;

}

VOID operate()

{

    LPVOID pFileBuffer = NULL;

    LPVOID pNewFileBuffer = NULL;

    LPVOID pImageBuffer = NULL;

    DWORD ret1 = ToLoaderPE(file_path, &pFileBuffer);  // &pFileBuffer(void**类型) 传递地址对其值可以进行修改

    printf("exe->filebuffer  返回值为计算所得文件大小:%#x\n", ret1);

    DWORD ret2 = CopyFileBufferToImageBuffer(pFileBuffer, &pImageBuffer);

    printf("filebuffer -> imagebuffer返回值为计算所得文件大小:%#x\n", ret2);

    DWORD ret3 = CopyImageBufferToNewFileBuffer(pImageBuffer, &pNewFileBuffer);

    printf("imagebuffer -> newfilebuffer返回值为计算所得文件大小:%#x\n", ret3);

    MemoryToFile(pNewFileBuffer, ret3, write_file_path);

    free(pFileBuffer);

    free(pNewFileBuffer);

    free(pImageBuffer);

}

int main()

{

    operate();

    getchar();

    return 0;

}


文章来源: https://bbs.pediy.com/thread-274506.htm
如有侵权请联系:admin#unsafe.sh