forked from googleprojectzero/functionsimsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpecodesource.cpp
More file actions
122 lines (102 loc) · 3.58 KB
/
pecodesource.cpp
File metadata and controls
122 lines (102 loc) · 3.58 KB
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
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "third_party/pe-parse/parser-library/parse.h"
#include "pecodesource.hpp"
PECodeRegion::PECodeRegion(uint8_t* buffer, size_t length, Dyninst::Address
section_base, const std::string& name) : base_(section_base), size_(length) {
data_ = static_cast<uint8_t*>(malloc(length));
memcpy(data_, buffer, length);
is_code_ = true;
}
PECodeRegion::~PECodeRegion() {
free(data_);
}
bool PECodeSource::isValidAddress( const Dyninst::Address addr ) const {
Dyninst::ParseAPI::CodeRegion* region = getRegion(addr);
if (region != NULL) {
return true;
} else {
return false;
}
}
PECodeSource::PECodeSource(const std::string& filename) {
peparse::parsed_pe* parsed_file = peparse::ParsePEFromFile(filename.c_str());
if (parsed_file == 0) {
printf("[!] Failure to parse PE file!\n");
return;
}
peparse::iterSec section_callback = [](void* N, peparse::VA section_base,
std::string& section_name, peparse::image_section_header s,
peparse::bounded_buffer* data) -> int {
printf("[!] Adding new code region at address %lx (name %s, size %d)\n",
section_base, section_name.c_str(), data->bufLen);
PECodeRegion* new_region = new PECodeRegion(
data->buf, data->bufLen, static_cast<Dyninst::Address>(section_base),
section_name);
PECodeSource* pe_code_source = static_cast<PECodeSource*>(N);
// Debug print the new section?
pe_code_source->_regions.push_back(new_region);
pe_code_source->_region_tree.insert(new_region);
};
peparse::IterSec( parsed_file, section_callback, static_cast<void*>(this) );
parsed_ = true;
}
Dyninst::ParseAPI::CodeRegion* PECodeSource::getRegion(
const Dyninst::Address addr) const {
std::set<Dyninst::ParseAPI::CodeRegion*> regions;
_region_tree.find(addr, regions);
for (Dyninst::ParseAPI::CodeRegion* region : regions) {
return region;
}
return NULL;
}
void* PECodeSource::getPtrToInstruction(const Dyninst::Address addr) const {
return getRegion(addr)->getPtrToInstruction(addr);
}
void* PECodeSource::getPtrToData(const Dyninst::Address addr) const {
return getPtrToInstruction(addr);
}
unsigned int PECodeSource::getAddressWidth() const {
return _regions[0]->getArch();
}
bool PECodeSource::isCode(const Dyninst::Address addr) const {
Dyninst::ParseAPI::CodeRegion* region = getRegion(addr);
if (region && region->isCode(addr)) {
return true;
}
return false;
}
bool PECodeSource::isData(const Dyninst::Address addr) const {
Dyninst::ParseAPI::CodeRegion* region = getRegion(addr);
if (region && region->isData(addr)) {
return true;
}
return false;
}
bool PECodeSource::isReadOnly(const Dyninst::Address addr) const {
Dyninst::ParseAPI::CodeRegion* region = getRegion(addr);
if (region && region->isReadOnly(addr)) {
return true;
}
return false;
}
Dyninst::Address PECodeSource::offset() const {
return 0;
}
Dyninst::Address PECodeSource::length() const {
return 0;
}
Dyninst::Architecture PECodeSource::getArch() const {
return _regions[0]->getArch();
}