forked from falltergeist/int2ssl
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ProcTable.cpp
316 lines (261 loc) · 8.37 KB
/
ProcTable.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
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
/**
*
* Copyright (c) 2005-2009 Anchorite (TeamX), <anchorite2001@yandex.ru>
* Copyright (c) 2014-2015 Nirran, phobos2077
* Copyright (c) 2015 alexeevdv <mail@alexeevdv.ru>
* Distributed under the GNU GPL v3. For full terms see the file license.txt
*
*/
// C++ standard includes
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <algorithm>
// int2ssl includes
#include "ProcTable.h"
#include "Utility.h"
#include "ObjectAttributes.h"
// Third party includes
extern std::ifstream g_ifstream;
extern std::ofstream g_ofstream;
CProcDescriptor::CProcDescriptor() :
m_ulNameOffset(0),
m_ulType(0),
m_ulTime(0),
m_ulExpressionOffset(0),
m_ulBodyOffset(0),
m_ulNumArgs(0)
{
}
CProcDescriptor::CProcDescriptor(const CProcDescriptor& Item) :
m_ulNameOffset(Item.m_ulNameOffset),
m_ulType(Item.m_ulType),
m_ulTime(Item.m_ulTime),
m_ulExpressionOffset(Item.m_ulExpressionOffset),
m_ulBodyOffset(Item.m_ulBodyOffset),
m_ulNumArgs(Item.m_ulNumArgs)
{
}
CProcDescriptor::~CProcDescriptor()
{
}
CProcDescriptor& CProcDescriptor::operator = (const CProcDescriptor& Item)
{
if (&Item != this)
{
m_ulNameOffset = Item.m_ulNameOffset;
m_ulType = Item.m_ulType;
m_ulTime = Item.m_ulTime;
m_ulExpressionOffset = Item.m_ulExpressionOffset;
m_ulBodyOffset = Item.m_ulBodyOffset;
m_ulNumArgs = Item.m_ulNumArgs;
}
return (*this);
}
void CProcDescriptor::Serialize()
{
g_ifstream.read((char*)&m_ulNameOffset, sizeof(m_ulNameOffset));
std::reverse((char*)&m_ulNameOffset, (char*)&m_ulNameOffset + sizeof(m_ulNameOffset));
g_ifstream.read((char*)&m_ulType, sizeof(m_ulType));
std::reverse((char*)&m_ulType, (char*)&m_ulType + sizeof(m_ulType));
g_ifstream.read((char*)&m_ulTime, sizeof(m_ulTime));
std::reverse((char*)&m_ulTime, (char*)&m_ulTime + sizeof(m_ulTime));
g_ifstream.read((char*)&m_ulExpressionOffset, sizeof(m_ulExpressionOffset));
std::reverse((char*)&m_ulExpressionOffset, (char*)&m_ulExpressionOffset + sizeof(m_ulExpressionOffset));
g_ifstream.read((char*)&m_ulBodyOffset, sizeof(m_ulBodyOffset));
std::reverse((char*)&m_ulBodyOffset, (char*)&m_ulBodyOffset + sizeof(m_ulBodyOffset));
g_ifstream.read((char*)&m_ulNumArgs, sizeof(m_ulNumArgs));
std::reverse((char*)&m_ulNumArgs, (char*)&m_ulNumArgs + sizeof(m_ulNumArgs));
if (!g_ifstream)
{
std::cout << "Error: Unable read procedure descriptor" << std::endl;
throw std::exception();
}
}
void CProcDescriptor::Dump()
{
std::string strType;
if (m_ulType != 0x00000000)
{
strType = "( ";
if (m_ulType & P_TIMED)
{
strType += "timed ";
}
if (m_ulType & P_CONDITIONAL)
{
strType += "conditional ";
}
if (m_ulType & P_IMPORT)
{
strType += "import ";
}
if (m_ulType & P_EXPORT) {
strType += "export ";
}
if (m_ulType & P_CRITICAL) {
strType += "critical ";
}
strType += ")";
}
else
{
strType = "No special types";
}
g_ofstream << format("Name offset: 0x%08X", m_ulNameOffset) << std::endl;
g_ofstream << format("Type: 0x%08X // %s", m_ulType, strType.c_str()) << std::endl;
g_ofstream << format("Time: 0x%08X // %d", m_ulTime, m_ulTime) << std::endl;
g_ofstream << format("Expression offset: 0x%08X", m_ulExpressionOffset) << std::endl;
g_ofstream << format("Body offset: 0x%08X", m_ulBodyOffset) << std::endl;
g_ofstream << format("Number of args: 0x%08X // %d", m_ulNumArgs, m_ulNumArgs) << std::endl;
}
CProcTable::CProcTable()
{
}
CProcTable::~CProcTable()
{
}
struct ProcBodyOffset {
uint32_t m_ulProcIndex;
uint32_t m_ulBodyOffset;
};
int compareProcBodyOffsets(const void* elem0, const void* elem1)
{
ProcBodyOffset offset0 = *((ProcBodyOffset*)elem0);
ProcBodyOffset offset1 = *((ProcBodyOffset*)elem1);
if (offset0.m_ulBodyOffset < offset1.m_ulBodyOffset)
{
return -1;
}
else if (offset0.m_ulBodyOffset > offset1.m_ulBodyOffset)
{
return 1;
}
else
{
if (offset0.m_ulProcIndex < offset1.m_ulProcIndex)
{
return -1;
}
else if (offset0.m_ulProcIndex > offset1.m_ulProcIndex)
{
return 1;
}
else
{
return 0;
}
}
}
void CProcTable::Serialize()
{
m_Table.clear();
m_ProcSize.clear();
uint32_t oldPosition = g_ifstream.tellg();
g_ifstream.seekg(0, std::ios_base::end);
uint32_t ulSizeOfFile = g_ifstream.tellg();
g_ifstream.seekg(oldPosition, std::ios_base::beg);
uint32_t ulSizeOfTable;
g_ifstream.read((char*)&ulSizeOfTable, sizeof(ulSizeOfTable));
std::reverse((char*)&ulSizeOfTable, (char*)&ulSizeOfTable + sizeof(ulSizeOfTable));
if (!g_ifstream)
{
printf("Error: Unable read size of procedures table\n");
throw std::exception();
}
m_Table.resize(ulSizeOfTable);
m_ProcSize.resize(ulSizeOfTable);
uint32_t ulIndexOfProcOffset = 0;
ProcBodyOffset* pOffsets = new ProcBodyOffset[ulSizeOfTable + 1];
if (pOffsets == NULL)
{
throw std::exception();
}
for(uint32_t i = 0; i < ulSizeOfTable; i++)
{
// printf("======== %u =========\n", i);
m_Table[i].Serialize();
m_ProcSize[i] = 0; // Initialize size of procedure
if (!(m_Table[i].m_ulType & P_IMPORT))
{
if ((m_Table[i].m_ulBodyOffset != 0) && (m_Table[i].m_ulBodyOffset != ulSizeOfFile))
{
pOffsets[ulIndexOfProcOffset].m_ulProcIndex = i;
pOffsets[ulIndexOfProcOffset].m_ulBodyOffset = m_Table[i].m_ulBodyOffset;
ulIndexOfProcOffset++;
}
else
{
m_Table[i].m_ulType |= P_NOTIMPLEMENTED;
}
}
}
pOffsets[ulIndexOfProcOffset].m_ulProcIndex = ulIndexOfProcOffset + 1;
pOffsets[ulIndexOfProcOffset].m_ulBodyOffset = ulSizeOfFile;
qsort(pOffsets, ulIndexOfProcOffset, sizeof(ProcBodyOffset), compareProcBodyOffsets);
// Sizes of procedures
for(uint32_t i = 0; i < ulIndexOfProcOffset; i++)
{
if (pOffsets[i + 1].m_ulBodyOffset >= pOffsets[i].m_ulBodyOffset)
{
m_ProcSize[pOffsets[i].m_ulProcIndex] = pOffsets[i + 1].m_ulBodyOffset - pOffsets[i].m_ulBodyOffset;
}
else
{
printf("Error: Procedures are not sorted in ascending address order\n");
throw std::exception();
}
}
delete [] pOffsets;
for(uint32_t i = 0; i < ulSizeOfTable; i++)
{
if (m_Table[i].m_ulBodyOffset != 0)
{
m_ulOffsetOfProcSection = m_Table[i].m_ulBodyOffset;
break;
}
}
// // For debugging only
// printf("Total: %d\n", m_Table.GetSize());
// for(i = 0; i < ulSizeOfTable; i++)
// {
// printf("Offset: 0x%08X, size: 0x%08X\n", m_Table[i].m_ulBodyOffset, m_ProcSize[i]);
// }
// printf("m_ulOffsetOfProcSection: 0x%08X\n", m_ulOffsetOfProcSection);
}
uint32_t CProcTable::GetSize()
{
return m_Table.size();
}
uint32_t CProcTable::GetSizeOfProc(uint32_t nIndex)
{
if (nIndex >= m_ProcSize.size())
{
printf("Warning: Invalid index of procedure (%d). Exception will be thrown\n", nIndex);
throw std::exception();
}
return m_ProcSize[nIndex];
}
uint32_t CProcTable::GetOffsetOfProcSection()
{
return m_ulOffsetOfProcSection;
}
void CProcTable::Dump()
{
for(unsigned int i = 0; i < m_Table.size(); i++)
{
g_ofstream << format("======== Procedure %d ========", i) << std::endl;
m_Table[i].Dump();
g_ofstream << std::endl;
}
}
CProcDescriptor& CProcTable::operator [] (uint32_t nIndex)
{
if (nIndex >= m_ProcSize.size())
{
printf("Warning: Invalid index of procedure (%d). Exception will be thrown\n", nIndex);
throw std::exception();
}
return m_Table[nIndex];
}