C反向shell问题

C reverse shell issues

我需要设置反向 shell 以便连接到通过 GPRS 调制解调器连接到互联网的设备。

当出现特殊情况时,我在public固定ip的服务器上启动这个命令

nc -l 65535

然后我将编写此代码 运行(现在我通过电缆直接连接到设备以进行测试)(是的,叉子在这种情况下没用,但我需要它在我的最终场景中,所以我保留了它)

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int reverse_shell()
{
    pid_t p = 0;

    /* fork */
    p = fork();

    if (p == 0)
    {
        char *shell[2];

        int i,fd;
        struct sockaddr_in sin;

        /* open socket */
        fd = socket(AF_INET, SOCK_STREAM, 0);
        sin.sin_family = AF_INET;
        sin.sin_addr.s_addr = inet_addr("MY SERVER PUBLIC IP ADDRESS");
        sin.sin_port = htons(65535);

        /* connect! */
        connect(fd, (struct sockaddr *)&sin,sizeof(struct sockaddr_in));

        /* assign three first fd (input/output/err) to open socket */
        for(i=0; i<3; i++)
            dup2(fd, i);

        /* build array */
        shell[0] = "/bin/bash";
        shell[1] = 0;

        /* start the reverse shell */
        if (execve(shell[0], shell, NULL) == -1)
            printf("error\n");

        exit(0);
    }

    return 0;
}

int main()
{
    reverse_shell();
}

反向 shell 已设置,但如您所见,我没有收到任何提示,看起来有点混乱。

[root@public-server tmp]# nc -lv 65535
Connection from yyy.yyy.yyy.yyy port 65535 [tcp/*] accepted
cd /etc
ls *hosts*
hosts
hosts.allow
hosts.deny

另外,我需要使用 scp 但消息不断出现在设备提示符上而不是反向连接的服务器上

反向连接的服务器:

[root@public-server tmp]# nc -lv 65535
Connection from yyy.yyy.yyy.yyy port 65535 [tcp/*] accepted
ls /etc/hosts
/etc/hosts
scp /etc/hosts xxx.xxx.xxx.xxx:/tmp/
Host key verification failed.
lost connection

设备提示:

root@device:/tmp# ./a.out
root@device:/tmp# The authenticity of host 'xxx.xxx.xxx.xxx (xxx.xxx.xxx.xxx)' can't be established.
RSA key fingerprint is aa:e6:aa:1d:aa:a5:c2:fd:aa:4c:4f:e7:aa:34:aa:78.
Are you sure you want to continue connecting (yes/no)?

我该怎么做才能解决这个问题并获得稳定可用的反向 shell?

问题是您的 shell 只得到一个普通的文件描述符。这样,它就只能像执行脚本一样运行。要以交互方式操作,它需要一个允许它执行所有 termios 操作的终端。这就是伪终端 (pty) 的用途。快速 google 搜索 this guide,我没有完全阅读它,也许有更好的来源 -- 祝你好运。

PS:我没有使用 pty 的经验,所以这可能是错误的,但我 假设 你应该以某种方式设置 TERM 在启动 shell 之前将客户端的环境变量设置为服务器的环境变量,因此建议实现您自己的服务器(而不是 nc)并在启动之前有一些初始化协议shell.