-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjinsock.c
More file actions
386 lines (357 loc) · 11.7 KB
/
jinsock.c
File metadata and controls
386 lines (357 loc) · 11.7 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include "jinsock.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/select.h>
#include <linux/limits.h>
#include <linux/net.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#ifndef __NR_pidfd_open
#define __NR_pidfd_open 434
#endif
#ifndef __NR_pidfd_getfd
#define __NR_pidfd_getfd 438
#endif
SocketEntry entries[MAX_ENTRIES];
int entry_count = 0;
void trim_newline(char *s) {
size_t len = strlen(s);
if (len > 0 && s[len-1] == '\n') s[len-1] = 0;
}
void cmd_help() {
printf(
"Commands:\n"
" help - Show this help\n"
" search [pattern] - List sockets, optionally filter by pid or process name\n"
" select <index> - Select a socket from the search results\n"
" send <string> - Send string to selected socket\n"
" sendf <file> - Send file content to selected socket\n"
" rec [file] - Receive from socket with timeout, output to stdout or file\n"
" timeout <seconds> - Set receive timeout (default 5 sec)\n"
" quit - Exit\n"
);
}
int parse_ip_port(const char *hexipport, char *ipbuf, size_t ipbuflen, int *port) {
// hexipport format: "0100007F:1F90" (IP:port in hex)
unsigned int p;
if (strlen(hexipport) < 13) return -1;
char ip_hex[9] = {0};
strncpy(ip_hex, hexipport, 8);
sscanf(hexipport, "%8X:%X", &p, &p);
if (sscanf(hexipport, "%8X:%X", &p, &p) != 2) return -1;
// Parse IP and port:
unsigned int ipval, portval;
if (sscanf(hexipport, "%8X:%X", &ipval, &portval) != 2) return -1;
// ipval is in little endian hex, convert to IP string:
unsigned char bytes[4];
bytes[0] = (ipval >> 0) & 0xFF;
bytes[1] = (ipval >> 8) & 0xFF;
bytes[2] = (ipval >> 16) & 0xFF;
bytes[3] = (ipval >> 24) & 0xFF;
snprintf(ipbuf, ipbuflen, "%u.%u.%u.%u", bytes[0], bytes[1], bytes[2], bytes[3]);
*port = portval;
return 0;
}
int parse_ipv6_port(const char *hexipport, char *ipbuf, size_t ipbuflen, int *port) {
// Format: 32 hex digits for IPv6 + :port (e.g. "00000000000000000000000000000001:1F90")
if (strlen(hexipport) < 37) return -1;
char iphex[33];
strncpy(iphex, hexipport, 32);
iphex[32] = 0;
sscanf(hexipport + 33, "%X", port);
unsigned char ip[16];
for (int i=0; i<16; i++) {
char bytehex[3] = {iphex[i*2], iphex[i*2+1], 0};
ip[15-i] = (unsigned char)strtol(bytehex, NULL, 16);
}
inet_ntop(AF_INET6, ip, ipbuf, ipbuflen);
return 0;
}
// Check if the socket inode matches one from /proc/pid/fd/<fd>
int get_socket_inode_from_fd(pid_t pid, int fd, unsigned long long *inode) {
char fdlink[PATH_MAX];
char linktarget[PATH_MAX];
snprintf(fdlink, sizeof(fdlink), "/proc/%d/fd/%d", pid, fd);
ssize_t r = readlink(fdlink, linktarget, sizeof(linktarget)-1);
if (r < 0) return -1;
linktarget[r] = 0;
// Expected format: "socket:[1234567]"
unsigned long long ino = 0;
if (sscanf(linktarget, "socket:[%llu]", &ino) == 1) {
*inode = ino;
return 0;
}
return -1;
}
int load_proc_name(pid_t pid, char *buf, size_t buflen) {
char path[PATH_MAX];
snprintf(path, sizeof(path), "/proc/%d/comm", pid);
FILE *f = fopen(path, "r");
if (!f) return -1;
if (!fgets(buf, buflen, f)) {
fclose(f);
return -1;
}
trim_newline(buf);
fclose(f);
return 0;
}
// Extract remote IP/port from /proc/<pid>/net/tcp or tcp6 by inode
int get_remote_addr_from_inode(pid_t pid, unsigned long long inode, char *ipbuf, size_t ipbuflen, int *port) {
char path[PATH_MAX];
snprintf(path, sizeof(path), "/proc/%d/net/tcp", pid);
FILE *f = fopen(path, "r");
if (!f) {
// Try tcp6
snprintf(path, sizeof(path), "/proc/%d/net/tcp6", pid);
f = fopen(path, "r");
if (!f) return -1;
}
char line[512];
fgets(line, sizeof(line), f); // skip header
while (fgets(line, sizeof(line), f)) {
unsigned int sl;
char local_addr[64], rem_addr[64], st[8], tx_queue[16], rx_queue[16], tr[8], tm_when[16], retrnsmt[16];
unsigned long long local_inode;
// Format from /proc/net/tcp:
// sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
// We only need rem_address, inode
if (sscanf(line,
"%u: %63s %63s %7s %15s %15s %7s %15s %15s %*u %*u %llu",
&sl, local_addr, rem_addr, st, tx_queue, rx_queue, tr, tm_when, retrnsmt, &local_inode) == 10) {
if (local_inode == inode) {
fclose(f);
// Parse rem_address: port is hex after ':'
if (strlen(rem_addr) == 13) {
// IPv4 case
return parse_ip_port(rem_addr, ipbuf, ipbuflen, port);
} else if (strlen(rem_addr) == 37) {
return parse_ipv6_port(rem_addr, ipbuf, ipbuflen, port);
}
return -1;
}
}
}
fclose(f);
return -1;
}
void cmd_search(const char *pattern) {
entry_count = 0;
DIR *proc = opendir("/proc");
if (!proc) {
perror("opendir /proc");
return;
}
struct dirent *dent;
while ((dent = readdir(proc)) != NULL) {
if (!isdigit(dent->d_name[0])) continue;
pid_t pid = atoi(dent->d_name);
char proc_name[256] = {0};
load_proc_name(pid, proc_name, sizeof(proc_name));
if (pattern && *pattern) {
// Filter by pid or process name substring (case-insensitive)
int match = 0;
if (strstr(proc_name, pattern)) match = 1;
else {
char pidstr[32];
snprintf(pidstr, sizeof(pidstr), "%d", pid);
if (strstr(pidstr, pattern)) match = 1;
}
if (!match) continue;
}
// List fd entries
char fd_dir[PATH_MAX];
snprintf(fd_dir, sizeof(fd_dir), "/proc/%d/fd", pid);
DIR *fdp = opendir(fd_dir);
if (!fdp) continue;
struct dirent *fdent;
while ((fdent = readdir(fdp)) != NULL) {
if (fdent->d_name[0] == '.') continue;
int fd = atoi(fdent->d_name);
unsigned long long inode;
if (get_socket_inode_from_fd(pid, fd, &inode) == 0) {
if (entry_count >= MAX_ENTRIES) {
closedir(fdp);
closedir(proc);
printf("Too many entries, truncated\n");
return;
}
SocketEntry *e = &entries[entry_count];
e->pid = pid;
e->fd = fd;
strncpy(e->proc_name, proc_name, sizeof(e->proc_name)-1);
e->proc_name[sizeof(e->proc_name)-1] = 0;
if (get_remote_addr_from_inode(pid, inode, e->rem_addr, sizeof(e->rem_addr), &e->rem_port) == 0) {
// Success, leave rem_addr, rem_port set
} else {
strncpy(e->rem_addr, "?", sizeof(e->rem_addr));
e->rem_port = 0;
}
entry_count++;
}
}
closedir(fdp);
}
closedir(proc);
printf("Found %d socket(s):\n", entry_count);
for (int i=0; i<entry_count; i++) {
SocketEntry *e = &entries[i];
printf("[%d] PID=%d (%s) FD=%d -> %s:%d\n", i, e->pid, e->proc_name, e->fd, e->rem_addr, e->rem_port);
}
}
int dup_socket_and_send(int pid, int fd, const char *data, size_t datalen) {
if (pid <= 0 || fd < 0) {
fprintf(stderr, "Invalid pid/fd\n");
return -1;
}
int pidfd = pidfd_open(pid, 0);
if (pidfd < 0) {
perror("pidfd_open");
return -1;
}
int sockfd = pidfd_getfd(pidfd, fd, 0);
if (sockfd < 0) {
perror("pidfd_getfd");
close(pidfd);
return -1;
}
ssize_t sent = send(sockfd, data, datalen, 0);
if (sent < 0) perror("send");
close(sockfd);
close(pidfd);
return sent;
}
int dup_socket_and_sendfile(int pid, int fd, const char *filepath) {
int f = open(filepath, O_RDONLY);
if (f < 0) {
perror("open file");
return -1;
}
int pidfd = pidfd_open(pid, 0);
if (pidfd < 0) {
perror("pidfd_open");
close(f);
return -1;
}
int sockfd = pidfd_getfd(pidfd, fd, 0);
if (sockfd < 0) {
perror("pidfd_getfd");
close(f);
close(pidfd);
return -1;
}
char buf[4096];
ssize_t n;
ssize_t total_sent = 0;
while ((n = read(f, buf, sizeof(buf))) > 0) {
ssize_t sent = send(sockfd, buf, n, 0);
if (sent < 0) {
perror("send");
close(f);
close(sockfd);
close(pidfd);
return -1;
}
total_sent += sent;
}
close(f);
close(sockfd);
close(pidfd);
return total_sent;
}
int dup_socket_and_recv(int pid, int fd, const char *outfile) {
int pidfd = pidfd_open(pid, 0);
if (pidfd < 0) {
perror("pidfd_open");
return -1;
}
int sockfd = pidfd_getfd(pidfd, fd, 0);
if (sockfd < 0) {
perror("pidfd_getfd");
close(pidfd);
return -1;
}
fd_set readfds;
struct timeval tv;
tv.tv_sec = recv_timeout_sec;
tv.tv_usec = 0;
FILE *outf = NULL;
if (outfile) {
outf = fopen(outfile, "wb");
if (!outf) {
perror("fopen output");
close(sockfd);
close(pidfd);
return -1;
}
}
ssize_t total_received = 0;
while (1) {
FD_ZERO(&readfds);
FD_SET(sockfd, &readfds);
int rv = select(sockfd + 1, &readfds, NULL, NULL, &tv);
if (rv == 0) {
printf("Timeout expired (%d seconds)\n", recv_timeout_sec);
break;
} else if (rv < 0) {
perror("select");
break;
}
if (FD_ISSET(sockfd, &readfds)) {
char buf[4096];
ssize_t n = recv(sockfd, buf, sizeof(buf), 0);
if (n < 0) {
perror("recv");
break;
} else if (n == 0) {
printf("Connection closed by peer\n");
break;
} else {
total_received += n;
if (outf) {
fwrite(buf, 1, n, outf);
fflush(outf);
} else {
fwrite(buf, 1, n, stdout);
fflush(stdout);
}
tv.tv_sec = recv_timeout_sec;
tv.tv_usec = 0;
}
}
}
if (outf) fclose(outf);
close(sockfd);
close(pidfd);
printf("Received %zd bytes\n", total_received);
return 0;
}
void print_usage() {
printf(
"Usage:\n"
" %s [options]\n"
"Options:\n"
" -p, --pid PID Specify PID\n"
" -s, --socket FD Specify socket fd\n"
" -S, --send STRING Send string to socket\n"
" -F, --sendf FILE Send file content to socket\n"
" -r, --rec [FILE] Receive from socket, output to stdout or FILE if specified\n"
" search [pattern] Search sockets optionally filtering by pattern\n"
" -h, --help Show this help\n"
"\n"
"If no arguments are provided, starts interactive shell.\n",
"program"
);
}
// ... ici tu mets toutes les fonctions copiées du js5.c sauf main() et le shell interactif ...
// (tu copies tout de trim_newline à dup_socket_and_recv, y compris les helpers, et adapte les "static" si besoin)