forked from killbug2004/ShellCodeFramwork
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShellCode.cpp
More file actions
149 lines (117 loc) · 3.66 KB
/
ShellCode.cpp
File metadata and controls
149 lines (117 loc) · 3.66 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
#include "stdafx.h"
#include "ShellCode.h"
#define offsetof(s,m) ((size_t)&reinterpret_cast<char const volatile&>((((s*)0)->m)))
void CShellCode::GetPEB()
{
PEB* p;
__asm
{
mov eax, fs:[30h]
mov p, eax
}
pPeb = p;
}
unsigned long CShellCode::GetHash(const char * str)
{
unsigned long dwHash = 0;
if (str == nullptr)
{
goto _exit;
}
// GetHash
while (*str)
{
dwHash = (dwHash >> 13) | (dwHash << (32 - 13)); // ROR dwHash, 13
dwHash += (*str >= 'a' ? *str - ('a' - 'A') : *str);
str++;
}
_exit:
return dwHash;
}
unsigned long CShellCode::GetFunctionHash(const char * szModuleName, const char * szFuncName)
{
return GetHash(szModuleName) + GetHash(szFuncName);
}
LDR_DATA_TABLE_ENTRY * CShellCode::GetLDRDataTableEntry(const LIST_ENTRY * ptr)
{
int iListEntryOffset = offsetof(LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks);
return (LDR_DATA_TABLE_ENTRY*)((BYTE*)ptr - iListEntryOffset);
}
void * CShellCode::GetFuncAddressByHash(unsigned long dwHash)
{
LIST_ENTRY* pFirst = nullptr;
LIST_ENTRY* ptr = nullptr;
void* pFuncAddress = nullptr;
if (pPeb == nullptr)
{
GetPEB();
}
pFirst = pPeb->Ldr->InMemoryOrderModuleList.Flink;
ptr = pFirst;
do
{
LDR_DATA_TABLE_ENTRY* pLdrDataTableEntry = GetLDRDataTableEntry(ptr);
ptr = ptr->Flink;
BYTE* BaseAddress = (BYTE*)pLdrDataTableEntry->DllBase;
if (!BaseAddress)
{
continue;
}
IMAGE_DOS_HEADER* pDosHeader = (IMAGE_DOS_HEADER*)BaseAddress;
IMAGE_NT_HEADERS* pNtHeader = (IMAGE_NT_HEADERS*)(BaseAddress + pDosHeader->e_lfanew);
DWORD dwIedRVA = pNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
if (!dwIedRVA)
{
continue;
}
IMAGE_EXPORT_DIRECTORY* pIed = (IMAGE_EXPORT_DIRECTORY*)(BaseAddress + dwIedRVA);
char* szModuleName = (char*)(BaseAddress + pIed->Name);
DWORD* pNameRVAs = (DWORD*)(BaseAddress + pIed->AddressOfNames);
for (DWORD i = 0; i < pIed->NumberOfNames; i++)
{
char* szFuncName = (char*)(BaseAddress + pNameRVAs[i]);
if (dwHash == GetFunctionHash(szModuleName, szFuncName))
{
WORD wOrdinal = ((WORD*)(BaseAddress + pIed->AddressOfNameOrdinals))[i];
pFuncAddress = BaseAddress + ((DWORD*)(BaseAddress + pIed->AddressOfFunctions))[wOrdinal];
break;
}
}
} while (ptr != pFirst);
return pFuncAddress;
}
void * CShellCode::GetAPIAddress(const char * szModuleName, const char * szFuncName)
{
unsigned long hModule = LoadLibraryA(szModuleName);
FARPROC pFunc = nullptr;
if (hModule != 0)
{
pFunc = GetProcAddress(hModule, szFuncName);
}
return pFunc;
}
void * CShellCode::memset(void * s, int c, size_t n)
{
const unsigned char uc = c;
unsigned char *su;
for (su = (unsigned char*)s; 0 < n; ++su, --n)
{
*su = uc;
}
return s;
}
CShellCode::CShellCode()
{
GetPEB();
char szKernel32Dll[] = { 'k', 'e', 'r', 'n', 'e', 'l', '3', '2', '.', 'd', 'l', 'l', 0 };
char szLoadLibraryA[] = { 'L', 'o', 'a', 'd', 'L', 'i', 'b', 'r', 'a', 'r', 'y', 'A', 0 };
char szGetProcAddress[] = { 'G', 'e', 't', 'P', 'r', 'o', 'c', 'A', 'd', 'd', 'r', 'e', 's', 's', 0 };
LoadLibraryA = (LOADLIBRARYA)GetFuncAddressByHash(GetFunctionHash(szKernel32Dll, szLoadLibraryA));
GetProcAddress = (GETPROCADDRESS)GetFuncAddressByHash(GetFunctionHash(szKernel32Dll, szGetProcAddress));
}
CShellCode::~CShellCode()
{
}
void CShellCode::Run()
{
}