[英]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.