diff --git a/src/client/client.c b/src/client/client.c index 008c922..4ba94c0 100644 --- a/src/client/client.c +++ b/src/client/client.c @@ -1,3 +1,4 @@ +#define _GNU_SOURCE #include "lib/RawTCP.h" #include #include @@ -10,6 +11,8 @@ #include #include #include +#include +#include #include "../common/constants.h" #include "../common/c&c.h" @@ -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){ diff --git a/src/client/client.o b/src/client/client.o index ab69829..d5d1e70 100644 Binary files a/src/client/client.o and b/src/client/client.o differ diff --git a/src/client/injector b/src/client/injector index 61ceb37..073dbe2 100755 Binary files a/src/client/injector and b/src/client/injector differ