-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathteview.cpp
More file actions
405 lines (360 loc) · 12.8 KB
/
teview.cpp
File metadata and controls
405 lines (360 loc) · 12.8 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// BinaryView for EFI Terse Executable images
//
// Resources:
// - https://uefi.org/specs/PI/1.8/V1_TE_Image.html
// - edk2/BaseTools/Source/C/GenFw/GenFw.c
#include "teview.h"
using namespace BinaryNinja;
using namespace std;
static TEViewType* g_teViewType = nullptr;
void BinaryNinja::InitTEViewType()
{
static TEViewType type;
BinaryViewType::Register(&type);
g_teViewType = &type;
}
void TEView::ReadTEImageHeader(BinaryReader& reader, struct TEImageHeader& header)
{
header.magic = reader.Read16();
header.machine = reader.Read16();
header.numberOfSections = reader.Read8();
header.subsystem = reader.Read8();
header.strippedSize = reader.Read16();
header.addressOfEntrypoint = reader.Read32();
header.baseOfCode = reader.Read32();
header.imageBase = reader.Read64();
header.dataDirectory[0].virtualAddress = reader.Read32();
header.dataDirectory[0].size = reader.Read32();
header.dataDirectory[1].virtualAddress = reader.Read32();
header.dataDirectory[1].size = reader.Read32();
m_logger->LogDebug(
"TEImageHeader:\n"
"\tmagic: 0x%04x\n"
"\tmachine: 0x%04x\n"
"\tnumberOfSections: 0x%02x\n"
"\tsubsystem: 0x%02x\n"
"\tstrippedSize: 0x%04x\n"
"\taddressOfEntrypoint: 0x%08x\n"
"\tbaseOfCode: 0x%08x\n"
"\timageBase: 0x%016x\n"
"\tdataDirectory[0].virtualAddress: 0x%08x\n"
"\tdataDirectory[0].size: 0x%08x\n"
"\tdataDirectory[1].virtualAddress: 0x%08x\n"
"\tdataDirectory[1].size: 0x%08x\n",
header.magic,
header.machine,
header.numberOfSections,
header.subsystem,
header.strippedSize,
header.addressOfEntrypoint,
header.baseOfCode,
header.imageBase,
header.dataDirectory[0].virtualAddress,
header.dataDirectory[0].size,
header.dataDirectory[1].virtualAddress,
header.dataDirectory[1].size
);
}
void TEView::ReadTEImageSectionHeaders(BinaryReader& reader, uint32_t numSections)
{
for (uint32_t i = 0; i < numSections; i++)
{
TEImageSectionHeader section;
section.name = reader.ReadString(8);
section.virtualSize = reader.Read32();
section.virtualAddress = reader.Read32();
section.sizeOfRawData = reader.Read32();
section.pointerToRawData = reader.Read32();
section.pointerToRelocations = reader.Read32();
section.pointerToLineNumbers = reader.Read32();
section.numberOfRelocations = reader.Read16();
section.numberOfLineNumbers = reader.Read16();
section.characteristics = reader.Read32();
m_logger->LogDebug(
"TEImageSectionHeader[%i]\n"
"\tname: %s\n"
"\tvirtualSize: %08x\n"
"\tvirtualAddress: %08x\n"
"\tsizeOfRawData: %08x\n"
"\tpointerToRawData: %08x\n"
"\tpointerToRelocations: %08x\n"
"\tpointerToLineNumbers: %08x\n"
"\tnumberOfRelocations: %04x\n"
"\tnumberOfLineNumbers: %04x\n"
"\tcharacteristics: %08x\n",
i,
section.name.c_str(),
section.virtualSize,
section.virtualAddress,
section.sizeOfRawData,
section.pointerToRawData,
section.pointerToRelocations,
section.pointerToLineNumbers,
section.numberOfRelocations,
section.numberOfLineNumbers,
section.characteristics
);
m_sections.push_back(section);
}
}
void TEView::CreateSections()
{
BeginBulkAddSegments();
for (size_t i = 0; i < m_sections.size(); i++)
{
auto section = m_sections[i];
uint32_t flags = 0;
if (section.characteristics & EFI_IMAGE_SCN_MEM_WRITE)
flags |= SegmentWritable;
if (section.characteristics & EFI_IMAGE_SCN_MEM_READ)
flags |= SegmentReadable;
if (section.characteristics & EFI_IMAGE_SCN_MEM_EXECUTE)
flags |= SegmentExecutable;
AddAutoSegment(
section.virtualAddress + m_imageBase,
section.virtualSize,
section.pointerToRawData - m_headersOffset,
section.sizeOfRawData,
flags
);
BNSectionSemantics semantics = DefaultSectionSemantics;
uint32_t pFlags = flags & 0x7;
if (pFlags == (SegmentReadable | SegmentExecutable))
semantics = ReadOnlyCodeSectionSemantics;
else if (pFlags == SegmentReadable)
semantics = ReadOnlyDataSectionSemantics;
else if (pFlags == (SegmentReadable | SegmentWritable))
semantics = ReadWriteDataSectionSemantics;
AddAutoSection(section.name, section.virtualAddress + m_imageBase, section.virtualSize, semantics);
}
EndBulkAddSegments();
}
void TEView::AssignHeaderTypes()
{
StructureBuilder dataDirectoryBuilder;
dataDirectoryBuilder.AddMember(Type::IntegerType(4, false), "virtualAddress");
dataDirectoryBuilder.AddMember(Type::IntegerType(4, false), "size");
auto dataDirectoryStruct = dataDirectoryBuilder.Finalize();
auto dataDirectoryType = Type::StructureType(dataDirectoryStruct);
QualifiedName dataDirectoryName = string("TE_Data_Directory_Entry");
auto dataDirectoryTypeId = Type::GenerateAutoTypeId("te", dataDirectoryName);
QualifiedName dataDirectoryTypeName = DefineType(dataDirectoryTypeId, dataDirectoryName, dataDirectoryType);
StructureBuilder headerBuilder;
headerBuilder.AddMember(Type::ArrayType(Type::IntegerType(1, true), 2), "signature");
headerBuilder.AddMember(Type::IntegerType(2, false), "machine");
headerBuilder.AddMember(Type::IntegerType(1, false), "numberOfSections");
headerBuilder.AddMember(Type::IntegerType(1, false), "subsystem");
headerBuilder.AddMember(Type::IntegerType(2, false), "strippedSize");
headerBuilder.AddMember(Type::IntegerType(4, false), "addressOfEntryPoint");
headerBuilder.AddMember(Type::IntegerType(4, false), "baseOfCode");
headerBuilder.AddMember(Type::IntegerType(8, false), "imageBase");
headerBuilder.AddMember(Type::NamedType(this, dataDirectoryTypeName), "baseRelocationTableEntry");
headerBuilder.AddMember(Type::NamedType(this, dataDirectoryTypeName), "debugEntry");
auto headerStruct = headerBuilder.Finalize();
auto headerType = Type::StructureType(headerStruct);
QualifiedName headerName = string("TE_Header");
auto headerTypeId = Type::GenerateAutoTypeId("te", headerName);
QualifiedName headerTypeName = DefineType(headerTypeId, headerName, headerType);
DefineDataVariable(m_imageBase + m_headersOffset, Type::NamedType(this, headerTypeName));
DefineAutoSymbol(new Symbol(DataSymbol, "__te_header", m_imageBase + m_headersOffset, NoBinding));
StructureBuilder sectionBuilder;
sectionBuilder.AddMember(Type::ArrayType(Type::IntegerType(1, true), 8), "name");
sectionBuilder.AddMember(Type::IntegerType(4, false), "virtualSize");
sectionBuilder.AddMember(Type::IntegerType(4, false), "virtualAddress");
sectionBuilder.AddMember(Type::IntegerType(4, false), "sizeOfRawData");
sectionBuilder.AddMember(Type::IntegerType(4, false), "pointerToRawData");
sectionBuilder.AddMember(Type::IntegerType(4, false), "pointerToRelocations");
sectionBuilder.AddMember(Type::IntegerType(4, false), "pointerToLineNumbers");
sectionBuilder.AddMember(Type::IntegerType(2, false), "numberOfRelocations");
sectionBuilder.AddMember(Type::IntegerType(2, false), "numberOfLineNumbers");
sectionBuilder.AddMember(Type::IntegerType(4, false), "characteristics");
auto sectionStruct = sectionBuilder.Finalize();
auto sectionType = Type::StructureType(sectionStruct);
QualifiedName sectionName = string("TE_Section_Header");
auto sectionTypeId = Type::GenerateAutoTypeId("te", sectionName);
QualifiedName sectionTypeName = DefineType(sectionTypeId, sectionName, sectionType);
DefineDataVariable(
m_imageBase + m_headersOffset + EFI_TE_IMAGE_HEADER_SIZE,
Type::ArrayType(Type::NamedType(this, sectionTypeName), m_sections.size())
);
DefineAutoSymbol(new Symbol(DataSymbol, "__section_headers", m_imageBase + m_headersOffset + EFI_TE_IMAGE_HEADER_SIZE, NoBinding));
}
TEView::TEView(BinaryView* bv, bool parseOnly) : BinaryView("TE", bv->GetFile(), bv), m_parseOnly(parseOnly)
{
CreateLogger("BinaryView");
m_logger = CreateLogger("BinaryView.TEView");
m_backedByDatabase = bv->GetFile()->IsBackedByDatabase("TE");
}
bool TEView::Init()
{
BinaryReader reader(GetParentView(), LittleEndian);
struct TEImageHeader header;
Ref<Platform> platform;
try
{
// Read image header and section headers
ReadTEImageHeader(reader, header);
ReadTEImageSectionHeaders(reader, header.numberOfSections);
m_headersOffset = header.strippedSize - EFI_TE_IMAGE_HEADER_SIZE;
// m_imageBase represents the base of the original PE image (before headers were stripped), not the base of the
// TE image (bv.start)
m_imageBase = header.imageBase;
// Set architecture and platform
auto settings = GetLoadSettings(GetTypeName());
if (settings)
{
if (settings->Contains("loader.imageBase"))
{
uint64_t baseOverride = settings->Get<uint64_t>("loader.imageBase", this);
// Apply the headers offset adjustment to compute the base of the original PE image
m_imageBase = baseOverride - m_headersOffset;
}
if (settings->Contains("loader.platform"))
platform = Platform::GetByName(settings->Get<string>("loader.platform", this));
}
if (!platform)
{
switch (header.machine)
{
case IMAGE_FILE_MACHINE_I386:
platform = Platform::GetByName("efi-x86");
m_addressSize = 4;
break;
case IMAGE_FILE_MACHINE_AMD64:
platform = Platform::GetByName("efi-x86_64");
m_addressSize = 8;
break;
case IMAGE_FILE_MACHINE_ARM64:
platform = Platform::GetByName("efi-aarch64");
m_addressSize = 8;
break;
case IMAGE_FILE_MACHINE_ARM:
platform = Platform::GetByName("efi-armv7");
m_addressSize = 4;
break;
case IMAGE_FILE_MACHINE_THUMB:
platform = Platform::GetByName("efi-thumb2");
m_addressSize = 4;
break;
default:
LogError("TE platform '0x%x' is not supported", header.machine);
m_addressSize = 4;
if (!m_parseOnly)
m_logger->LogWarn("Unable to determine architecture. Please open the file with options and select a valid architecture.");
return false;
}
}
if (!platform)
{
LogError("Platform not supported by this version of Binary Ninja");
return false;
}
m_arch = platform->GetArchitecture();
if (!m_arch)
{
LogError("Architecture not supported by this version of Binary Ninja");
return false;
}
m_addressSize = m_arch->GetAddressSize();
SetDefaultPlatform(platform);
SetDefaultArchitecture(m_arch);
// Create a segment for the header so that it can be viewed and create sections
uint64_t headerSegmentSize = reader.GetOffset();
AddAutoSegment(m_imageBase + m_headersOffset, headerSegmentSize, 0, headerSegmentSize, SegmentReadable);
CreateSections();
AssignHeaderTypes();
// Finished for parse only mode
if (m_parseOnly)
return true;
m_entryPoint = m_imageBase + header.addressOfEntrypoint;
DefineAutoSymbol(new Symbol(FunctionSymbol, "_start", m_entryPoint));
AddEntryPointForAnalysis(platform, m_entryPoint);
}
catch (std::exception& e)
{
m_logger->LogError("Failed to parse TE headers: %s\n", e.what());
return false;
}
return true;
}
uint64_t TEView::PerformGetEntryPoint() const
{
return m_entryPoint;
}
size_t TEView::PerformGetAddressSize() const
{
return m_addressSize;
}
TEViewType::TEViewType() : BinaryViewType("TE", "TE")
{
m_logger = LogRegistry::CreateLogger("BinaryView");
}
Ref<BinaryView> TEViewType::Create(BinaryView* bv)
{
try
{
return new TEView(bv);
}
catch (std::exception& e)
{
m_logger->LogErrorForException(
e, "%s<BinaryViewType> failed to create view! '%s'", GetName().c_str(), e.what());
return nullptr;
}
}
Ref<BinaryView> TEViewType::Parse(BinaryView* bv)
{
try
{
return new TEView(bv, true);
}
catch (std::exception& e)
{
m_logger->LogErrorForException(
e, "%s<BinaryViewType> failed to create view! '%s'", GetName().c_str(), e.what());
return nullptr;
}
}
bool TEViewType::IsTypeValidForData(BinaryView* bv)
{
// Check the VZ signature
DataBuffer sig = bv->ReadBuffer(0, 2);
if (sig.GetLength() != 2)
return false;
if (memcmp(sig.GetData(), "VZ", 2))
return false;
// Check section header names for .text section
BinaryReader reader(bv, LittleEndian);
reader.Seek(0x4);
uint8_t numSections;
if (!reader.TryRead8(numSections))
return false;
for (uint8_t i = 0; i < numSections; i++)
{
reader.Seek(EFI_TE_IMAGE_HEADER_SIZE + (i * EFI_TE_SECTION_HEADER_SIZE));
uint64_t name;
if (!reader.TryRead64(name))
return false;
if (name == 0x747865742e) // .text
return true;
}
return false;
}
Ref<Settings> TEViewType::GetLoadSettingsForData(BinaryView* data)
{
Ref<BinaryView> viewRef = Parse(data);
if (!viewRef || !viewRef->Init())
{
m_logger->LogWarn("Failed to initialize view of type '%s'. Generating default load settings.", GetName().c_str());
viewRef = data;
}
// specify default load settings that can be overridden
Ref<Settings> settings = GetDefaultLoadSettingsForData(viewRef);
vector<string> overrides = {"loader.platform", "loader.imageBase"};
for (const auto& override : overrides)
{
if (settings->Contains(override))
settings->UpdateProperty(override, "readOnly", false);
}
return settings;
}