繁体   English   中英

如何在chroot监狱内执行shell命令

[英]How to execute shell command inside a chroot jail

我在chroot监狱内执行shell命令时遇到问题。 这是一个例子:

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>

int main()
{
   if (geteuid() == 0)    // check root privileges
   {
      chroot("/bin");
      chdir("/");

      execl("/ls", "ls", "-l",  (char *) NULL); // "/ls" should be equivalent to "/bin/ls"
      perror(strerror(errno));
   }

   else
      printf("Permission denied\n");

   return 0;
}

问题是执行程序:根据errno,错误是“没有这样的文件或目录”。 如果我使用exec(“ / bin / ls”,...),也会出现相同的错误

我认为由于chroot监狱,“ ls”无法使用他需要的共享库。

有解决这个问题的建议吗?

关于共享库不可访问,您可能是正确的。 设置chroot监狱通常涉及将/bin/usr/bin/lib/usr/lib部分复制到并行目录结构中。

一个更简单的选择是仅使用静态链接的可执行文件。 在许多linux系统上,将有一个称为busybox的静态链接可执行文件,它提供包括ls在内的许多Unix命令的基本功能。 busybox ls -l一样调用它可以提供与常规ls程序类似的输出,而无需访问chroot监狱之外的其他共享库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM