-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterfacer.c
77 lines (70 loc) · 2.22 KB
/
interfacer.c
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
/*
* Project Name: Interfacer
* Author: Alex Popovici @ Anvil Secure
* Date: October 2024
*
* This file is part of the Interfacer project.
*
* Interfacer is licensed under the BSD 3-Clause License.
* You may obtain a copy of the license at:
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Alternatively, the license can be found in the LICENSE file
* in the root directory of this project.
*
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>
#include <net/if.h>
// Define a prototype for the setmyinterface function
void setmyinterface(int sockfd);
struct ifreq ifr;
int ifr_init = 0;
// Function pointers for the original functions
int (*original_socket)(int domain, int type, int protocol) = NULL;
int (*original_setsockopt)(int sockfd, int level, int optname, const void *optval, socklen_t optlen) = NULL;
void ensurehooks()
{
if(!ifr_init) {
const char *interface_name = getenv("INTERFACE_NAME");
if(interface_name)
{
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, interface_name, strlen(interface_name));
original_socket = (int (*)(int, int, int)) dlsym(RTLD_NEXT, "socket");
original_setsockopt = (int (*)(int, int, int, const void *, socklen_t)) dlsym(RTLD_NEXT, "setsockopt");
}
ifr_init = 1;
}
}
// Hooked socket() function
int socket(int domain, int type, int protocol) {
ensurehooks();
int sockfd = original_socket(domain, type, protocol);
setmyinterface(sockfd);
return sockfd;
}
// Hooked setsockopt() function
int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen) {
ensurehooks();
if (optname == SO_BINDTODEVICE) {
setmyinterface(sockfd);
}
return original_setsockopt(sockfd, level, optname, optval, optlen);
}
void setmyinterface(int sockfd) {
if (original_setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, (void*)&ifr, sizeof(ifr)) < 0) {
perror("Error setting SO_BINDTODEVICE");
} else {
printf("Socket %d bound to interface %s\n", sockfd, ifr.ifr_name);
}
}