1 /*
2 * This is desock_dup.c from the amazing preeny project
3 * https://github.com/zardus/preeny
4 *
5 * It is packaged in afl++ to have it at hand if needed
6 *
7 */
8
9 #define _GNU_SOURCE
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <sys/types.h> //
16 #include <sys/socket.h> //
17 #include <sys/stat.h> //
18 #include <fcntl.h> //
19 #include <netinet/in.h>
20 #include <pthread.h>
21 #include <signal.h>
22 #include <dlfcn.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <poll.h>
26 // #include "logging.h" // switched from preeny_info() to fprintf(stderr, "Info:
27 // "
28
29 //
30 // originals
31 //
32 int (*original_close)(int);
33 int (*original_dup2)(int, int);
preeny_desock_dup_orig()34 __attribute__((constructor)) void preeny_desock_dup_orig() {
35
36 original_close = dlsym(RTLD_NEXT, "close");
37 original_dup2 = dlsym(RTLD_NEXT, "dup2");
38
39 }
40
close(int sockfd)41 int close(int sockfd) {
42
43 if (sockfd <= 2) {
44
45 fprintf(stderr, "Info: Disabling close on %d\n", sockfd);
46 return 0;
47
48 } else {
49
50 return original_close(sockfd);
51
52 }
53
54 }
55
dup2(int old,int new)56 int dup2(int old, int new) {
57
58 if (new <= 2) {
59
60 fprintf(stderr, "Info: Disabling dup from %d to %d\n", old, new);
61 return 0;
62
63 } else {
64
65 return original_dup2(old, new);
66
67 }
68
69 }
70
accept(int sockfd,struct sockaddr * addr,socklen_t * addrlen)71 int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
72
73 (void)sockfd;
74 (void)addr;
75 (void)addrlen;
76 fprintf(stderr, "Info: Emulating accept on %d\n", sockfd);
77 return 0;
78
79 }
80
bind(int sockfd,const struct sockaddr * addr,socklen_t addrlen)81 int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
82
83 (void)sockfd;
84 (void)addr;
85 (void)addrlen;
86 fprintf(stderr, "Info: Emulating bind on port %d\n",
87 ntohs(((struct sockaddr_in *)addr)->sin_port));
88 return 0;
89
90 }
91
listen(int sockfd,int backlog)92 int listen(int sockfd, int backlog) {
93
94 (void)sockfd;
95 (void)backlog;
96 return 0;
97
98 }
99
setsockopt(int sockfd,int level,int optid,const void * optdata,socklen_t optdatalen)100 int setsockopt(int sockfd, int level, int optid, const void *optdata,
101 socklen_t optdatalen) {
102
103 (void)sockfd;
104 (void)level;
105 (void)optid;
106 (void)optdata;
107 (void)optdatalen;
108 return 0;
109
110 }
111
112