forked from komashchenko/DynLibUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule_linux.cpp
More file actions
236 lines (190 loc) · 6.63 KB
/
Copy pathmodule_linux.cpp
File metadata and controls
236 lines (190 loc) · 6.63 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
// DynLibUtils
// Copyright (C) 2023 komashchenko (Phoenix)
// https://github.com/komashchenko/DynLibUtils
#include "module.h"
#include "memaddr.h"
#include <cstring>
#include <regex>
#include <link.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
using namespace libmodule;
CModule::~CModule()
{
if (m_pModuleHandle)
dlclose(m_pModuleHandle);
}
//-----------------------------------------------------------------------------
// Purpose: Initializes the module from module name
// Input : svModuleName
// bExtension
// Output : bool
//-----------------------------------------------------------------------------
bool CModule::InitFromName(const std::string_view svModuleName, bool bExtension)
{
if (m_pModuleHandle)
return false;
if (svModuleName.empty())
return false;
std::string sModuleName(svModuleName);
if (!bExtension)
sModuleName.append(".so");
struct dl_data
{
ElfW(Addr) addr;
const char* moduleName;
const char* modulePath;
} dldata{ 0, sModuleName.c_str(), {} };
dl_iterate_phdr([](dl_phdr_info* info, size_t /* size */, void* data)
{
dl_data* dldata = reinterpret_cast<dl_data*>(data);
if (std::strstr(info->dlpi_name, dldata->moduleName) != nullptr)
{
dldata->addr = info->dlpi_addr;
dldata->modulePath = info->dlpi_name;
}
return 0;
}, &dldata);
if (!dldata.addr)
return false;
std::string fixedModulePath = GetFixedServerPath(dldata.modulePath);
if (!Init(fixedModulePath))
return false;
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Initializes the module from module memory
// Input : pModuleMemory
// Output : bool
//-----------------------------------------------------------------------------
bool CModule::InitFromMemory(const CMemory pModuleMemory)
{
if (m_pModuleHandle)
return false;
if (!pModuleMemory)
return false;
Dl_info info;
if (!dladdr(pModuleMemory, &info) || !info.dli_fbase || !info.dli_fname)
return false;
if (!Init(info.dli_fname))
return false;
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Initializes a module descriptors
//-----------------------------------------------------------------------------
bool CModule::Init(const std::string_view svModelePath)
{
void* handle = dlopen(svModelePath.data(), RTLD_LAZY | RTLD_NOLOAD);
if (!handle)
return false;
link_map* lmap;
if (dlinfo(handle, RTLD_DI_LINKMAP, &lmap) != 0)
{
dlclose(handle);
return false;
}
int fd = open(lmap->l_name, O_RDONLY);
if (fd == -1)
{
dlclose(handle);
return false;
}
struct stat st;
if (fstat(fd, &st) == 0)
{
void* map = mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (map != MAP_FAILED)
{
ElfW(Ehdr)* ehdr = static_cast<ElfW(Ehdr)*>(map);
ElfW(Shdr)* shdrs = reinterpret_cast<ElfW(Shdr)*>(reinterpret_cast<uintptr_t>(ehdr) + ehdr->e_shoff);
const char* strTab = reinterpret_cast<const char*>(reinterpret_cast<uintptr_t>(ehdr) + shdrs[ehdr->e_shstrndx].sh_offset);
for (auto i = 0; i < ehdr->e_shnum; ++i) // Loop through the sections.
{
ElfW(Shdr)* shdr = reinterpret_cast<ElfW(Shdr)*>(reinterpret_cast<uintptr_t>(shdrs) + i * ehdr->e_shentsize);
if (*(strTab + shdr->sh_name) == '\0')
continue;
m_vModuleSections.emplace_back(strTab + shdr->sh_name, static_cast<uintptr_t>(lmap->l_addr + shdr->sh_addr), shdr->sh_size);
}
munmap(map, st.st_size);
}
}
close(fd);
m_pModuleHandle = handle;
m_sModulePath.assign(svModelePath);
m_ExecutableCode = GetSectionByName(".text");
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Gets an address of a virtual method table by rtti type descriptor name
// Input : svTableName
// bDecorated
// Output : CMemory
//-----------------------------------------------------------------------------
CMemory CModule::GetVirtualTableByName(const std::string_view svTableName, bool bDecorated) const
{
if (svTableName.empty())
return CMemory();
CModule::ModuleSections_t readOnlyData = GetSectionByName(".rodata"), readOnlyRelocations = GetSectionByName(".data.rel.ro");
if (!readOnlyData.IsSectionValid() || !readOnlyRelocations.IsSectionValid())
return CMemory();
std::string sDecoratedTableName(bDecorated ? svTableName : std::to_string(svTableName.length()) + std::string(svTableName));
std::string sMask(sDecoratedTableName.length() + 1, 'x');
CMemory pStartAddr {};
CMemory typeInfoName, referenceTypeName {};
do {
typeInfoName = FindPattern(sDecoratedTableName.data(), sMask, pStartAddr, &readOnlyData);
if (!typeInfoName)
return CMemory();
referenceTypeName = FindPattern(&typeInfoName, "xxxxxxxx", nullptr, &readOnlyRelocations); // Get reference to type name.
if (referenceTypeName)
break;
pStartAddr = typeInfoName.Offset(sDecoratedTableName.length());
} while (typeInfoName);
CMemory typeInfo = referenceTypeName.Offset(-0x8); // Offset -0x8 to typeinfo.
for (const auto& sectionName : { std::string_view(".data.rel.ro"), std::string_view(".data.rel.ro.local") })
{
CModule::ModuleSections_t section = GetSectionByName(sectionName);
if (!section.IsSectionValid())
continue;
CMemory reference;
while ((reference = FindPattern(&typeInfo, "xxxxxxxx", reference, §ion))) // Get reference typeinfo in vtable
{
if (reference.Offset(-0x8).GetValue<int64_t>() == 0) // Offset to this.
{
return reference.Offset(0x8);
}
reference.OffsetSelf(0x8);
}
}
return CMemory();
}
//-----------------------------------------------------------------------------
// Purpose: Gets an address of a virtual method table by rtti type descriptor name
// Input : svFunctionName
// Output : CMemory
//-----------------------------------------------------------------------------
CMemory CModule::GetFunctionByName(const std::string_view svFunctionName) const noexcept
{
if (!m_pModuleHandle)
return CMemory();
if (svFunctionName.empty())
return CMemory();
return dlsym(m_pModuleHandle, svFunctionName.data());
}
//-----------------------------------------------------------------------------
// Purpose: Returns the module base
//-----------------------------------------------------------------------------
CMemory CModule::GetModuleBase() const noexcept
{
return static_cast<link_map*>(m_pModuleHandle)->l_addr;
}
std::string CModule::GetFixedServerPath(const std::string_view svRawPath) const {
if (svRawPath.find("addons") == std::string::npos || svRawPath.find("server") == std::string::npos) {
return svRawPath.data();
}
std::regex pattern(R"((.*csgo/).*(bin/.*))");
return std::regex_replace(svRawPath.data(), pattern, "$1$2");
}