This repository was archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary_parse.cpp
107 lines (88 loc) · 2.62 KB
/
binary_parse.cpp
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
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <exec.hpp>
#include <msexec.h>
#include <macho.h>
#include <memory>
#include "../kernel.h"
namespace Execute
{
BinaryType GetBinaryType(FileNode *Node)
{
debug("Checking binary type of %s", Node->Path.c_str());
BinaryType Type;
if (Node == nullptr)
ReturnLogError((BinaryType)-ENOENT, "Node is null");
Elf32_Ehdr ELFHeader;
Node->Read(&ELFHeader, sizeof(Elf32_Ehdr), 0);
mach_header MachHeader;
Node->Read(&MachHeader, sizeof(mach_header), 0);
IMAGE_DOS_HEADER MZHeader;
Node->Read(&MZHeader, sizeof(IMAGE_DOS_HEADER), 0);
/* Check ELF header. */
if (ELFHeader.e_ident[EI_MAG0] == ELFMAG0 &&
ELFHeader.e_ident[EI_MAG1] == ELFMAG1 &&
ELFHeader.e_ident[EI_MAG2] == ELFMAG2 &&
ELFHeader.e_ident[EI_MAG3] == ELFMAG3)
{
debug("Image - ELF");
Type = BinaryType::BinTypeELF;
goto Success;
}
if (MachHeader.magic == MH_MAGIC || MachHeader.magic == MH_CIGAM)
{
debug("Image - Mach-O");
Type = BinaryType::BinTypeMachO;
goto Success;
}
/* Check MZ header. */
else if (MZHeader.e_magic == IMAGE_DOS_SIGNATURE)
{
IMAGE_NT_HEADERS PEHeader;
Node->Read(&PEHeader, sizeof(IMAGE_NT_HEADERS), MZHeader.e_lfanew);
IMAGE_OS2_HEADER NEHeader;
Node->Read(&NEHeader, sizeof(IMAGE_OS2_HEADER), MZHeader.e_lfanew);
/* TODO: LE, EDOS */
if (PEHeader.Signature == IMAGE_NT_SIGNATURE)
{
debug("Image - PE");
Type = BinaryType::BinTypePE;
goto Success;
}
else if (NEHeader.ne_magic == IMAGE_OS2_SIGNATURE)
{
debug("Image - NE");
Type = BinaryType::BinTypeNE;
goto Success;
}
else
{
debug("Image - MZ");
Type = BinaryType::BinTypeMZ;
goto Success;
}
}
/* ... */
Type = BinaryType::BinTypeUnknown;
Success:
return Type;
}
BinaryType GetBinaryType(std::string Path)
{
FileNode *node = fs->GetByPath(Path.c_str(), nullptr);
debug("Checking binary type of %s (returning %p)", Path.c_str(), node);
assert(node != nullptr);
return GetBinaryType(node);
}
}