簡體   English   中英

如何在 C 中訪問 system() 終端/進程的輸入和 output stream

[英]How to access input and output stream of system() terminal/process in C

我在c中使用system()命令,調用su命令,需要root密碼。

如何從 c 程序中提供該 root 密碼,而不是自己手動編寫它,以及如何使用生成的 root 終端?

如何從 c 程序中提供該 root 密碼

system("echo password | sudo -S program");

但是以明文形式提供密碼是一個安全漏洞,因為它可以使用stringshexdump等工具輕松提取,而是設置權限:

sudo chown root myprogram
sudo chmod ug+s myprogram

並使用setuid(0)

#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    setuid(0);
    system("./program_with_root_perms");
    return 0;
}

As pointed out by @Darth-CodeX, prefer the exec family functions over system , see how to combine exec<whatever> and setuid : https://0xdf.gitlab.io/2022/05/31/setuid-rabbithole.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM