|
|
@ -8,11 +8,10 @@ typedef struct PipeThreadInfo_t {
|
|
|
|
SOCKET ClientSocket;
|
|
|
|
SOCKET ClientSocket;
|
|
|
|
} PipeThreadInfo;
|
|
|
|
} PipeThreadInfo;
|
|
|
|
|
|
|
|
|
|
|
|
void CreateChildProcess(HANDLE g_hChildStd_IN_Rd, HANDLE g_hChildStd_OUT_Wr);
|
|
|
|
HANDLE CreateChildProcess(HANDLE g_hChildStd_IN_Rd, HANDLE g_hChildStd_OUT_Wr);
|
|
|
|
DWORD WINAPI WorkWithClient(LPVOID lpParam);
|
|
|
|
DWORD WINAPI WorkWithClient(LPVOID lpParam);
|
|
|
|
DWORD WINAPI WriteToPipe(LPVOID lpParam);
|
|
|
|
DWORD WINAPI WriteToPipe(LPVOID lpParam);
|
|
|
|
DWORD WINAPI ReadFromPipe(LPVOID lpParam);
|
|
|
|
DWORD WINAPI ReadFromPipe(LPVOID lpParam);
|
|
|
|
void ErrorExit(PCTSTR);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void StartShellServer() {
|
|
|
|
void StartShellServer() {
|
|
|
|
printf("\n->Start of shell server execution.\n");
|
|
|
|
printf("\n->Start of shell server execution.\n");
|
|
|
@ -90,7 +89,7 @@ void StartShellServer() {
|
|
|
|
ClientSocket = accept(ListenSocket, NULL, NULL);
|
|
|
|
ClientSocket = accept(ListenSocket, NULL, NULL);
|
|
|
|
if (ClientSocket == INVALID_SOCKET) {
|
|
|
|
if (ClientSocket == INVALID_SOCKET) {
|
|
|
|
printf("accept failed: %d\n", WSAGetLastError());
|
|
|
|
printf("accept failed: %d\n", WSAGetLastError());
|
|
|
|
break;
|
|
|
|
//break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("Client connected\n");
|
|
|
|
printf("Client connected\n");
|
|
|
|
// Create separate thread to process client connection
|
|
|
|
// Create separate thread to process client connection
|
|
|
@ -100,7 +99,6 @@ void StartShellServer() {
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Finalization
|
|
|
|
* Finalization
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
// TODO: properly close all handles
|
|
|
|
|
|
|
|
closesocket(ListenSocket);
|
|
|
|
closesocket(ListenSocket);
|
|
|
|
WSACleanup();
|
|
|
|
WSACleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -116,7 +114,7 @@ DWORD WINAPI WorkWithClient(LPVOID lpParam) {
|
|
|
|
HANDLE g_hChildStd_OUT_Wr = NULL;
|
|
|
|
HANDLE g_hChildStd_OUT_Wr = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
SECURITY_ATTRIBUTES saAttr;
|
|
|
|
SECURITY_ATTRIBUTES saAttr;
|
|
|
|
HANDLE PipeThreads[2];
|
|
|
|
HANDLE PipeThreads[3];
|
|
|
|
|
|
|
|
|
|
|
|
SOCKET ClientSocket = (SOCKET)lpParam;
|
|
|
|
SOCKET ClientSocket = (SOCKET)lpParam;
|
|
|
|
|
|
|
|
|
|
|
@ -129,38 +127,61 @@ DWORD WINAPI WorkWithClient(LPVOID lpParam) {
|
|
|
|
saAttr.lpSecurityDescriptor = NULL;
|
|
|
|
saAttr.lpSecurityDescriptor = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
// Create a pipe for the child process's STDOUT.
|
|
|
|
// Create a pipe for the child process's STDOUT.
|
|
|
|
if (!CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0))
|
|
|
|
if (!CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0)) {
|
|
|
|
ErrorExit(TEXT("StdoutRd CreatePipe"));
|
|
|
|
printf("StdoutRd CreatePipe\n");
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Ensure the read handle to the pipe for STDOUT is not inherited.
|
|
|
|
// Ensure the read handle to the pipe for STDOUT is not inherited.
|
|
|
|
if (!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
|
|
|
|
if (!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)) {
|
|
|
|
ErrorExit(TEXT("Stdout SetHandleInformation"));
|
|
|
|
printf("Stdout SetHandleInformation\n");
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create a pipe for the child process's STDIN.
|
|
|
|
// Create a pipe for the child process's STDIN.
|
|
|
|
if (!CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0))
|
|
|
|
if (!CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0)) {
|
|
|
|
ErrorExit(TEXT("Stdin CreatePipe"));
|
|
|
|
printf("Stdin CreatePipe\n");
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Ensure the write handle to the pipe for STDIN is not inherited.
|
|
|
|
// Ensure the write handle to the pipe for STDIN is not inherited.
|
|
|
|
if (!SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0))
|
|
|
|
if (!SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0)) {
|
|
|
|
ErrorExit(TEXT("Stdin SetHandleInformation"));
|
|
|
|
printf("Stdin SetHandleInformation\n");
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Create child process cmd.exe
|
|
|
|
* Create child process cmd.exe
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
CreateChildProcess(g_hChildStd_IN_Rd, g_hChildStd_OUT_Wr);
|
|
|
|
PipeThreads[0] = CreateChildProcess(g_hChildStd_IN_Rd, g_hChildStd_OUT_Wr);
|
|
|
|
|
|
|
|
if (!PipeThreads[0]) {
|
|
|
|
|
|
|
|
printf("Failed to create child process\n");
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create threads for STDIN/STDOUT
|
|
|
|
// Create threads for STDIN/STDOUT
|
|
|
|
PipeThreadInfo WriteThreadInfo = { g_hChildStd_IN_Wr, ClientSocket };
|
|
|
|
PipeThreadInfo WriteThreadInfo = { g_hChildStd_IN_Wr, ClientSocket };
|
|
|
|
PipeThreadInfo ReadThreadInfo = { g_hChildStd_OUT_Rd, ClientSocket };
|
|
|
|
PipeThreadInfo ReadThreadInfo = { g_hChildStd_OUT_Rd, ClientSocket };
|
|
|
|
PipeThreads[0] = CreateThread(NULL, 0, WriteToPipe, &WriteThreadInfo, 0, NULL);
|
|
|
|
PipeThreads[1] = CreateThread(NULL, 0, WriteToPipe, &WriteThreadInfo, 0, NULL);
|
|
|
|
PipeThreads[1] = CreateThread(NULL, 0, ReadFromPipe, &ReadThreadInfo, 0, NULL);
|
|
|
|
PipeThreads[2] = CreateThread(NULL, 0, ReadFromPipe, &ReadThreadInfo, 0, NULL);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!(PipeThreads[1] && PipeThreads[2])) {
|
|
|
|
|
|
|
|
printf("Failed to create pipe threads\n");
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WaitForMultipleObjects(3, PipeThreads, FALSE, INFINITE);
|
|
|
|
|
|
|
|
|
|
|
|
WaitForMultipleObjects(2, PipeThreads, TRUE, INFINITE);
|
|
|
|
for (int i = 1; i < 3; TerminateThread(PipeThreads[i++], 0));
|
|
|
|
|
|
|
|
TerminateProcess(PipeThreads[0], 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; CloseHandle(PipeThreads[i++]));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
closesocket(ClientSocket);
|
|
|
|
printf("\n->Client disconnected.\n");
|
|
|
|
printf("\n->Client disconnected.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CreateChildProcess(HANDLE g_hChildStd_IN_Rd, HANDLE g_hChildStd_OUT_Wr)
|
|
|
|
HANDLE CreateChildProcess(HANDLE g_hChildStd_IN_Rd, HANDLE g_hChildStd_OUT_Wr)
|
|
|
|
// Create a child process that uses the previously created pipes for STDIN and STDOUT.
|
|
|
|
// Create a child process that uses the previously created pipes for STDIN and STDOUT.
|
|
|
|
{
|
|
|
|
{
|
|
|
|
TCHAR szCmdline[] = TEXT("C:\\Windows\\System32\\cmd.exe");
|
|
|
|
TCHAR szCmdline[] = TEXT("C:\\Windows\\System32\\cmd.exe");
|
|
|
@ -196,15 +217,17 @@ void CreateChildProcess(HANDLE g_hChildStd_IN_Rd, HANDLE g_hChildStd_OUT_Wr)
|
|
|
|
&piProcInfo); // receives PROCESS_INFORMATION
|
|
|
|
&piProcInfo); // receives PROCESS_INFORMATION
|
|
|
|
|
|
|
|
|
|
|
|
// If an error occurs, exit the application.
|
|
|
|
// If an error occurs, exit the application.
|
|
|
|
if (!bSuccess)
|
|
|
|
if (!bSuccess) {
|
|
|
|
ErrorExit(TEXT("CreateProcess"));
|
|
|
|
printf("CreateProcess\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Close handles to the child process and its primary thread.
|
|
|
|
// Close handles to the child process and its primary thread.
|
|
|
|
// Some applications might keep these handles to monitor the status
|
|
|
|
// Some applications might keep these handles to monitor the status
|
|
|
|
// of the child process, for example.
|
|
|
|
// of the child process, for example.
|
|
|
|
|
|
|
|
|
|
|
|
CloseHandle(piProcInfo.hProcess);
|
|
|
|
//CloseHandle(piProcInfo.hProcess);
|
|
|
|
CloseHandle(piProcInfo.hThread);
|
|
|
|
CloseHandle(piProcInfo.hThread);
|
|
|
|
|
|
|
|
|
|
|
|
// Close handles to the stdin and stdout pipes no longer needed by the child process.
|
|
|
|
// Close handles to the stdin and stdout pipes no longer needed by the child process.
|
|
|
@ -213,6 +236,7 @@ void CreateChildProcess(HANDLE g_hChildStd_IN_Rd, HANDLE g_hChildStd_OUT_Wr)
|
|
|
|
CloseHandle(g_hChildStd_OUT_Wr);
|
|
|
|
CloseHandle(g_hChildStd_OUT_Wr);
|
|
|
|
CloseHandle(g_hChildStd_IN_Rd);
|
|
|
|
CloseHandle(g_hChildStd_IN_Rd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return piProcInfo.hProcess;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DWORD WINAPI WriteToPipe(LPVOID lpParam)
|
|
|
|
DWORD WINAPI WriteToPipe(LPVOID lpParam)
|
|
|
@ -239,22 +263,21 @@ DWORD WINAPI WriteToPipe(LPVOID lpParam)
|
|
|
|
if (iResult > 0) {
|
|
|
|
if (iResult > 0) {
|
|
|
|
bSuccess = WriteFile(g_hChildStd_IN_Wr, recvbuf, iResult, &dwWritten, NULL);
|
|
|
|
bSuccess = WriteFile(g_hChildStd_IN_Wr, recvbuf, iResult, &dwWritten, NULL);
|
|
|
|
if (!bSuccess) break;
|
|
|
|
if (!bSuccess) break;
|
|
|
|
printf("Received command: %s\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (iResult == 0)
|
|
|
|
else if (iResult == 0)
|
|
|
|
printf("Connection closing...\n");
|
|
|
|
printf("Connection closing...\n");
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
printf("recv failed: %d\n", WSAGetLastError());
|
|
|
|
printf("recv failed: %d\n", WSAGetLastError());
|
|
|
|
closesocket(ClientSocket);
|
|
|
|
closesocket(ClientSocket);
|
|
|
|
WSACleanup();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (iResult > 0);
|
|
|
|
} while (iResult > 0);
|
|
|
|
|
|
|
|
|
|
|
|
WriteToPipe_end:
|
|
|
|
WriteToPipe_end:
|
|
|
|
// Closing STDIN => cmd.exe exit
|
|
|
|
if (!CloseHandle(g_hChildStd_IN_Wr)) {
|
|
|
|
if (!CloseHandle(g_hChildStd_IN_Wr))
|
|
|
|
printf("StdInWr CloseHandle\n");
|
|
|
|
ErrorExit(TEXT("StdInWr CloseHandle"));
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DWORD WINAPI ReadFromPipe(LPVOID lpParam)
|
|
|
|
DWORD WINAPI ReadFromPipe(LPVOID lpParam)
|
|
|
@ -263,7 +286,6 @@ DWORD WINAPI ReadFromPipe(LPVOID lpParam)
|
|
|
|
CHAR chBuf[BUFSIZE + 1];
|
|
|
|
CHAR chBuf[BUFSIZE + 1];
|
|
|
|
|
|
|
|
|
|
|
|
BOOL bSuccess = FALSE;
|
|
|
|
BOOL bSuccess = FALSE;
|
|
|
|
//HANDLE hParentStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
|
|
|
|
SOCKET ClientSocket = ((PipeThreadInfo*)lpParam)->ClientSocket;
|
|
|
|
SOCKET ClientSocket = ((PipeThreadInfo*)lpParam)->ClientSocket;
|
|
|
|
HANDLE g_hChildStd_OUT_Rd = ((PipeThreadInfo*)lpParam)->Pipe;
|
|
|
|
HANDLE g_hChildStd_OUT_Rd = ((PipeThreadInfo*)lpParam)->Pipe;
|
|
|
|
|
|
|
|
|
|
|
@ -274,49 +296,14 @@ DWORD WINAPI ReadFromPipe(LPVOID lpParam)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
bSuccess = ReadFile(g_hChildStd_OUT_Rd, chBuf, BUFSIZE, &dwRead, NULL);
|
|
|
|
bSuccess = ReadFile(g_hChildStd_OUT_Rd, chBuf, BUFSIZE, &dwRead, NULL);
|
|
|
|
if (!bSuccess || dwRead == 0) break;
|
|
|
|
if (!bSuccess || dwRead == 0) break;
|
|
|
|
printf("STDOUT: %s\n", chBuf);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
chBuf[dwRead] = '\0';
|
|
|
|
chBuf[dwRead] = '\0';
|
|
|
|
|
|
|
|
|
|
|
|
iSendResult = send(ClientSocket, chBuf, dwRead, 0);
|
|
|
|
iSendResult = send(ClientSocket, chBuf, dwRead, 0);
|
|
|
|
if (iSendResult == SOCKET_ERROR) {
|
|
|
|
if (iSendResult == SOCKET_ERROR) {
|
|
|
|
printf("send failed: %d\n", WSAGetLastError());
|
|
|
|
printf("send failed: %d\n", WSAGetLastError());
|
|
|
|
closesocket(ClientSocket);
|
|
|
|
closesocket(ClientSocket);
|
|
|
|
WSACleanup();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!bSuccess) break;
|
|
|
|
if (!bSuccess) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ErrorExit(PCTSTR lpszFunction)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Format a readable error message, display a message box,
|
|
|
|
|
|
|
|
// and exit from the application.
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
LPVOID lpMsgBuf;
|
|
|
|
|
|
|
|
LPVOID lpDisplayBuf;
|
|
|
|
|
|
|
|
DWORD dw = GetLastError();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FormatMessage(
|
|
|
|
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
|
|
|
|
NULL,
|
|
|
|
|
|
|
|
dw,
|
|
|
|
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
|
|
|
|
(LPTSTR)&lpMsgBuf,
|
|
|
|
|
|
|
|
0, NULL);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
|
|
|
|
|
|
|
|
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
|
|
|
|
|
|
|
|
StringCchPrintf((LPTSTR)lpDisplayBuf,
|
|
|
|
|
|
|
|
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
|
|
|
|
|
|
|
|
TEXT("%s failed with error %d: %s"),
|
|
|
|
|
|
|
|
lpszFunction, dw, lpMsgBuf);
|
|
|
|
|
|
|
|
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LocalFree(lpMsgBuf);
|
|
|
|
|
|
|
|
LocalFree(lpDisplayBuf);
|
|
|
|
|
|
|
|
ExitProcess(1);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|