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/osx/x86/src/test/server.c
Views: 11784
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <strings.h>
4
#include <unistd.h>
5
#include <pthread.h>
6
7
#include <sys/mman.h>
8
#include <sys/types.h>
9
#include <sys/socket.h>
10
#include <netinet/ip.h>
11
12
void usage(char* argv0)
13
{
14
fprintf(stderr, "usage: %s [ -t ] [ -p <port> ]\n", argv0);
15
}
16
17
int read_and_exec(int s)
18
{
19
int n, length;
20
int (*payload)(void);
21
22
fprintf(stderr, "Reading length... ");
23
if ((n = recv(s, &length, sizeof(length), 0)) != sizeof(length)) {
24
if (n < 0)
25
perror("recv");
26
else
27
fprintf(stderr, "recv: short read\n");
28
return -1;
29
}
30
fprintf(stderr, "%d\n", length);
31
32
fprintf(stderr, "Allocating buffer... ");
33
if ((payload = mmap(NULL, length, PROT_READ | PROT_WRITE | PROT_EXEC,
34
MAP_ANON | MAP_PRIVATE, -1, 0)) == (void*)-1) {
35
perror("mmap");
36
return -1;
37
}
38
fprintf(stderr, "0x%x\n", payload);
39
40
fprintf(stderr, "Reading payload... ");
41
if ((n = recv(s, payload, length, 0)) != length) {
42
if (n < 0)
43
perror("recv");
44
else
45
fprintf(stderr, "recv: short read\n");
46
return -1;
47
}
48
fprintf(stderr, "read %d bytes\n", n);
49
50
fprintf(stderr, "Executing payload...\n");
51
52
(void*)(*payload)();
53
54
return 0;
55
}
56
57
void* read_and_exec_thread(void* arg)
58
{
59
return (void*)read_and_exec((int)arg);
60
}
61
62
int create_read_and_exec_thread(int c)
63
{
64
int err;
65
pthread_t pthread;
66
void* return_value;
67
68
if ((err = pthread_create(&pthread, NULL,
69
read_and_exec_thread, (void*)c)) != 0) {
70
fprintf(stderr, "pthread_create: %s\n", strerror(err));
71
return -1;
72
}
73
74
if ((err = pthread_join(pthread, &return_value)) != 0) {
75
fprintf(stderr, "pthread_join: %s\n", strerror(err));
76
return -1;
77
}
78
}
79
80
int main(int argc, char* argv[])
81
{
82
int c, s, val, threaded = 0;
83
socklen_t salen;
84
struct sockaddr_in saddr, client_saddr;
85
short port = 1234;
86
87
while ((c = getopt(argc, argv, "tp:")) != EOF) {
88
switch (c) {
89
case 'p':
90
port = atoi(optarg);
91
break;
92
case 't':
93
threaded = 1;
94
break;
95
default:
96
usage(argv[0]);
97
exit(EXIT_FAILURE);
98
}
99
}
100
101
if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) < 0) {
102
perror("socket");
103
exit(EXIT_FAILURE);
104
}
105
106
val = 1;
107
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
108
perror("setsockopt");
109
exit(EXIT_FAILURE);
110
}
111
112
bzero(&saddr, sizeof(saddr));
113
saddr.sin_family = AF_INET;
114
saddr.sin_port = htons(port);
115
saddr.sin_addr.s_addr = INADDR_ANY;
116
117
if (bind(s, (struct sockaddr*)&saddr, sizeof(saddr)) < 0) {
118
perror("bind");
119
exit(EXIT_FAILURE);
120
}
121
122
if (listen(s, 5) < 0) {
123
perror("listen");
124
exit(EXIT_FAILURE);
125
}
126
127
if ((c = accept(s, (struct sockaddr*)&client_saddr, &salen)) < 0) {
128
perror("accept");
129
exit(EXIT_FAILURE);
130
}
131
132
if (threaded)
133
exit(create_read_and_exec_thread(c));
134
else
135
exit(read_and_exec(c));
136
}
137
138