-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathbofnet_test.cpp
185 lines (137 loc) · 4.88 KB
/
bofnet_test.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
#include <windows.h>
#include <stdio.h>
#include <vector>
#include <cinttypes>
#include <sstream>
#include <iostream>
#include <map>
#define CALLBACK_OUTPUT 0x0
#define CALLBACK_OUTPUT_OEM 0x1e
#define CALLBACK_ERROR 0x0d
#define CALLBACK_OUTPUT_UTF8 0x20
#define REVERSE_SHORT(n) ((unsigned short) (((n & 0xFF) << 8) | \
((n & 0xFF00) >> 8)))
#define REVERSE_LONG(n) ((unsigned long) (((n & 0xFF) << 24) | \
((n & 0xFF00) << 8) | \
((n & 0xFF0000) >> 8) | \
((n & 0xFF000000) >> 24)))
typedef std::vector<char> ByteArray;
std::map<const char*,void*> values;
extern "C" {
void go(char* args , int len);
void BeaconPrintf(int type, const char* fmt, ...){
switch(type){
case CALLBACK_OUTPUT:
case CALLBACK_OUTPUT_OEM:
case CALLBACK_ERROR:
case CALLBACK_OUTPUT_UTF8:{
va_list args;
int len;
char * buffer;
va_start( args, fmt );
len = _vscprintf( fmt, args ) // _vscprintf doesn't count
+ 1; // terminating '\0'
buffer = (char *) malloc( len * sizeof(char));
if ( NULL != buffer )
{
vsprintf_s( buffer, len, fmt, args );
puts(buffer);
free( buffer );
}
va_end( args );
}
}
}
void BeaconOutput(int type, const char* data, int len){
BeaconPrintf(type, data);
}
int toWideChar(char* src, wchar_t* dst, int max){
auto result = MultiByteToWideChar(CP_UTF8, 0, src, strlen(src), dst, max);
return result;
}
BOOL BeaconUseToken(HANDLE token){
return ImpersonateLoggedOnUser(token);
}
void BeaconRevertToken(){
RevertToSelf();
}
void BeaconInjectProcess(HANDLE hProc, int pid, char* payload, int p_len, int p_offset, char* arg, int a_len){
}
void BeaconGetSpawnTo(BOOL x86, char* buffer, int length){
}
void* BeaconGetValue(const char* name){
if (values.find(name) == values.end()) {
return nullptr;
} else {
return values[name];
}
}
void BeaconAddValue(const char* name, void* value){
values[name] = value;
}
void BeaconRemoveValue(const char* name){
values.erase(name);
}
}
struct PrefixedBuffer{
int len;
char buffer[0];
};
ByteArray readAllData(const char* fileName){
FILE* fp = fopen(fileName, "rb");
fseek(fp, 0L, SEEK_END);
int sz = ftell(fp);
fseek(fp, 0L, SEEK_SET);
ByteArray data;
data.resize(sz);
fread(&data[0], 1, sz, fp);
fclose(fp);
return data;
}
ByteArray loadAssembly(const char* filename, const std::wstring& loaderId){
std::stringstream ss;
ByteArray assemblyData = readAllData(filename);
const char commandName[] = "BOFNET.Bofs.AssemblyLoader";
int nameLen = REVERSE_LONG(loaderId.length());
int chunkSize = REVERSE_LONG(assemblyData.size() + 1);
int dataSize = REVERSE_LONG(assemblyData.size());
ss.write(commandName, sizeof(commandName));
ss.write((char*)&nameLen, 4);
ss.write((char*)loaderId.data(), (loaderId.length() + 1) * 2);
ss.write((char*)&chunkSize, 4);
ss.write((char*)&dataSize, 4);
ss.write(&assemblyData[0], assemblyData.size());
std::string result = ss.str();
return ByteArray(result.begin(), result.end());
}
ByteArray executeBoo(const char* booScript, const wchar_t* scriptArgs){
std::stringstream ss;
const char commandName[] = "BOFNET.Bofs.Boo.BooRunner";
int scriptLen = REVERSE_LONG(strlen(booScript));
int argsLen = REVERSE_LONG(wcslen(scriptArgs) + 1);
ss.write(commandName, sizeof(commandName));
ss.write((char*)&scriptLen, 4);
ss.write((char*)booScript, strlen(booScript));
ss.write((char*)&argsLen, 4);
ss.write((char*)scriptArgs, (wcslen(scriptArgs) + 1) * 2);
std::string result = ss.str();
return ByteArray(result.begin(), result.end());
}
ByteArray execute(const std::string& bofnetCmd){
return ByteArray(bofnetCmd.begin(), bofnetCmd.end() + 1);
}
void goBA(const ByteArray& data){
go((char*)data.data(), data.size());
}
int main(int argc, char** argv){
const char init[] = "BOFNET.Bofs.Initializer\x00";
ByteArray runtime = readAllData("..\\dist\\net40\\bofnet.dll");
ByteArray args;
args.resize(sizeof(init) + runtime.size());
memcpy(&args[0], init, sizeof(init));
memcpy(&args[sizeof(init)-1], &runtime[0], runtime.size());
go(args.data(), args.size());
goBA(executeBoo("print 'Yo!'", L"args"));
goBA(execute("HelloWorld Yolo"));
getchar();
}