簡體   English   中英

用 C 語言設置 SUID 是不夠的

[英]Setting up SUID in C language is not enough

出於教學目的,我想在 C 中設置基本命令注入。 我有以下代碼:

#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv) {
    char cat[] = "cat ";
    char *command;
    size_t commandLength;

    commandLength = strlen(cat) + strlen(argv[1]) + 1;
    command = (char *) malloc(commandLength);

    strncpy(command, cat, commandLength);
    strncat(command, argv[1], (commandLength - strlen(cat)) );

    system(command);
    return (0);
}

我編譯它,將二進制文件設置為root擁有並將SUID設置為1,如下所示:

gcc injectionos.c -o injectionos
sudo chown root:root injectionos
sudo chmod +s injectionos

我得到以下結果:

ls -la
total 40
drwxr-xr-x 2 olive olive  4096 Jan  6 13:17 .
drwxr-xr-x 3 olive olive  4096 Jan  6 12:15 ..
-rwsr-sr-x 1 root  root  16824 Jan  6 13:17 injectionos
-rw-r--r-- 1 olive olive   415 Jan  6 13:17 injectionos.c
-rwx------ 1 root  root      9 Jan  6 12:43 titi.txt
-rw-r--r-- 1 olive olive     9 Jan  6 12:16 toto.txt`

因此,基本上,將 SUID 設置為 1,我應該能夠通過執行以下注入來打開 toto.txt 和 titi.txt 文件:

./injectionos "toto.txt;cat titi.txt"

但是執行這個命令,我在訪問titi.txt時得到了一個permission denied 最后,當我添加一個setuid(geteuid()); 在我的代碼中,注入工作正常,我可以訪問 titi.txt 文件。

鑒於 injectionos 以 root 身份運行,而titi.txt 屬於 root,我認為這就足夠了,但顯然不是。 我在這里想念什么?

作為system()調用的一部分執行的/bin/sh正在刪除特權。 請參閱 bash 和-p選項的手冊頁

如果 shell 以不等於實際用戶(組) id 的有效用戶(組) id 啟動,並且未提供 -p 選項,則不讀取啟動文件,shell 函數未從環境繼承,SHELLOPTS 、BASHOPTS、CDPATH 和 GLOBIGNORE 變量,如果它們出現在環境中,將被忽略,有效用戶 id 設置為真實用戶 id。 如果在調用時提供了 -p 選項,則啟動行為是相同的,但不會重置有效用戶 ID。

好吧,技術上 debian 默認使用dash ,但它做同樣的事情。

因此,shell 的默認行為已進行了調整,以至少在一定程度上減輕這種注入。

暫無
暫無

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

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