forked from NytroRST/ShellcodeCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugUtils.cpp
More file actions
96 lines (74 loc) · 2.17 KB
/
DebugUtils.cpp
File metadata and controls
96 lines (74 loc) · 2.17 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
#include "DebugUtils.h"
#if defined(_WIN32)
#include "SEHUtils.h"
#else
#include <cstdlib>
#include <cstring>
#include <sys/mman.h>
#endif
// Dump all data - debug purposes
void DebugUtils::DumpAllData()
{
cout << endl;
for (size_t i = 0; i < DeclaredFunctions::AllDeclaredFunctions.size(); i++)
{
cout << "Declared:" << DeclaredFunctions::AllDeclaredFunctions[i].Name << " @ " << DeclaredFunctions::AllDeclaredFunctions[i].DLL << endl;
}
cout << endl;
for (size_t i = 0; i < FunctionCalls::AllFunctionCalls.size(); i++)
{
cout << "Call function: " << FunctionCalls::AllFunctionCalls[i].Name << endl;
for (size_t j = 0; j < FunctionCalls::AllFunctionCalls[i].Parameters.size(); j++)
{
cout << ((FunctionCalls::AllFunctionCalls[i].Parameters[j].Type == FunctionCalls::PARAMETER_TYPE_STRING) ? "String" : "Int") << " parameter: ";
if (FunctionCalls::AllFunctionCalls[i].Parameters[j].Type == FunctionCalls::PARAMETER_TYPE_STRING) cout << FunctionCalls::AllFunctionCalls[i].Parameters[j].StringValue << endl;
else cout << FunctionCalls::AllFunctionCalls[i].Parameters[j].IntValue << endl;
}
}
cout << endl;
}
// Test the generated shellcode
void DebugUtils::TestShellcode(string p_sFilename)
{
unsigned char *p = NULL;
size_t size = 0;
p = Utils::ReadBinaryFile(p_sFilename, &size);
// Check if successful read
if (size == 0 || p == NULL)
{
cout << "Error: Cannot read shellcode file!" << endl;
return;
}
#if defined(_WIN32)
// Get space for shellcode
void *sc = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (sc == NULL)
{
cout << "Error: Cannot allocate space for shellcode!" << endl;
return;
}
// Copy shellcode and execute it
try
{
_set_se_translator(CxxTranslateSehException);
memcpy(sc, p, size);
(*(int(*)()) sc)();
}
catch (const seh_exception& e)
{
cout << "Error when executing shellcode: "
<< e.what() << endl;
}
#else
// Test shellcode on Linux
unsigned char *sc = (unsigned char*)valloc(size);
if (sc == NULL)
{
cout << "Error: Cannot allocate space for shellcode!" << endl;
return;
}
memcpy(sc, p, size);
mprotect(sc, size, PROT_READ | PROT_EXEC);
(*(int(*)())sc)();
#endif
}