Skip to content

Commit

Permalink
Updated client to work with multiple network interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
h3xduck committed May 15, 2022
1 parent d509f20 commit e6cbe7c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/client/client.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define _GNU_SOURCE
#include "lib/RawTCP.h"
#include <stdio.h>
#include <unistd.h>
Expand All @@ -10,6 +11,8 @@
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <ifaddrs.h>
#include <linux/if_link.h>

#include "../common/constants.h"
#include "../common/c&c.h"
Expand Down Expand Up @@ -57,11 +60,81 @@ void check_ip_address_format(char* address){
}
}

/**
* @brief Improved version of getting local IP
* Based on the man page: https://man7.org/linux/man-pages/man3/getifaddrs.3.html
*
* @return char*
*/
char* getLocalIpAddress(){
char hostbuffer[256];
char* IPbuffer = calloc(256, sizeof(char));
struct hostent *host_entry;
int hostname;

char buf[BUFSIZ];
printf(">> Which network interface do you want to use?>: ");
fgets(buf, BUFSIZ, stdin);
if ((strlen(buf)>0) && (buf[strlen(buf)-1] == '\n')){
buf[strlen(buf)-1] = '\0';
}

struct ifaddrs *ifaddr;
int family, s;
char host[NI_MAXHOST];

if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
exit(EXIT_FAILURE);
}

/* Walk through linked list, maintaining head pointer so we
can free list later. */

for (struct ifaddrs *ifa = ifaddr; ifa != NULL;ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL)
continue;

family = ifa->ifa_addr->sa_family;

/* Display interface name and family (including symbolic
form of the latter for the common families). */

//printf("%-8s %s (%d)\n",ifa->ifa_name,(family == AF_PACKET) ? "AF_PACKET" :(family == AF_INET) ? "AF_INET" :(family == AF_INET6) ? "AF_INET6" : "???",family);
/* For an AF_INET* interface address, display the address. */

if (family == AF_INET || family == AF_INET6) {
s = getnameinfo(ifa->ifa_addr,
(family == AF_INET) ? sizeof(struct sockaddr_in) :
sizeof(struct sockaddr_in6),
host, NI_MAXHOST,
NULL, 0, NI_NUMERICHOST);
if (s != 0) {
printf("getnameinfo() failed: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}

//printf("\t\taddress: <%s>\n", host);
if(strcmp(ifa->ifa_name, buf)==0){
//Interface we chose
printf("["KBLU"INFO"RESET"]""Attacker IP selected: %s (%s)\n", ifa->ifa_name, host);
return IPbuffer;
}
}

}
printf("["KRED"ERROR"RESET"]""That was not a valid interface\n");

freeifaddrs(ifaddr);

exit(FAIL);
}

char* getLocalIpAddress_old(){
char hostbuffer[256];
char* IPbuffer = calloc(256, sizeof(char));
struct hostent *host_entry;
int hostname;

hostname = gethostname(hostbuffer, sizeof(hostbuffer));
if(hostname==-1){
Expand Down
Binary file modified src/client/client.o
Binary file not shown.
Binary file modified src/client/injector
Binary file not shown.

0 comments on commit e6cbe7c

Please sign in to comment.