-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathpeproc.cc
120 lines (92 loc) · 3.14 KB
/
peproc.cc
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
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstring>
#include "pe-parse/pe-parser-library/include/parser-library/parse.h"
using namespace peparse;
typedef struct _SEARCH_PARAMS {
char *SymbolName;
uint64_t SymbolAddress;
} SEARCH_PARAMS, *PSEARCH_PARAMS;
static int SearchExports(void *user,
VA funcAddr,
std::string &mod,
std::string &func) {
auto params = static_cast<PSEARCH_PARAMS>(user);
auto address = static_cast<std::uint64_t>(funcAddr);
if (params->SymbolName == func) {
params->SymbolAddress = address;
}
return 0;
}
extern "C" bool GetSymbolInfo64(char *Filename,
char *Export,
bool *Is64,
uint64_t *ImageBase,
uint64_t *Address)
{
parsed_pe *p = ParsePEFromFile(Filename);
SEARCH_PARAMS Parameters;
if (p == NULL) {
return false;
}
*Is64 = p->peHeader.nt.OptionalMagic != NT_OPTIONAL_32_MAGIC;
*ImageBase = *Is64 ? p->peHeader.nt.OptionalHeader64.ImageBase
: p->peHeader.nt.OptionalHeader.ImageBase;
Parameters.SymbolName = Export;
Parameters.SymbolAddress = 0ULL;
IterExpVA(p, SearchExports, &Parameters);
*Address = Parameters.SymbolAddress;
DestructParsedPE(p);
if (Parameters.SymbolAddress)
return true;
return false;
}
typedef struct _SECTION_PARAMS {
char *SectionName;
uint64_t SectionBase;
image_section_header SectionHeader;
} SECTION_PARAMS, *PSECTION_PARAMS;
static int FindSection(void *user,
peparse::VA secBase,
std::string &secName,
peparse::image_section_header s,
peparse::bounded_buffer *data)
{
auto params = static_cast<PSECTION_PARAMS>(user);
auto address = static_cast<std::uint64_t>(secBase);
if (params->SectionName == secName) {
params->SectionHeader = s;
params->SectionBase = address;
}
return 0;
}
extern "C" bool GetSectionProperty(char *Filename,
char *Section,
char *Property,
uint64_t *Result)
{
SECTION_PARAMS Params;
parsed_pe *p = ParsePEFromFile(Filename);
if (p == NULL) {
return false;
}
Params.SectionName = Section;
Params.SectionBase = 0ULL;
IterSec(p, FindSection, &Params);
DestructParsedPE(p);
if (Params.SectionBase == 0)
return false;
if (strcmp(Property, "VirtualAddress") == 0) {
*Result = Params.SectionHeader.VirtualAddress;
} else if (strcmp(Property, "PointerToRawData") == 0) {
*Result = Params.SectionHeader.PointerToRawData;
} else if (strcmp(Property, "SizeOfRawData") == 0){
*Result = Params.SectionHeader.SizeOfRawData;
} else if (strcmp(Property, "Characteristics") == 0) {
*Result = Params.SectionHeader.Characteristics;
} else {
return false;
}
return true;
}