2017-05-25 70 views
0

我想了解缓冲区溢出和setuid。我用这个来源:试图理解缓冲区溢出和setuid。我没有获得特权

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int main(void){ 
    char prog[]="/bin/ls -hal"; 
    char in[8]={0}; 
    printf("Name of a dir to list : "); 
    gets(in); 
    char *cmd; 
    cmd=malloc(strlen(prog)+strlen(in)+2); 
    strcat(cmd, prog); 
    strcat(cmd, " "); 
    strcat(cmd, in); 
    return system(cmd); 
} 

编译它之后,我改变与店主:

sudo chown root:root a.out 

我设置的权限:

sudo chmod 4755 a.out 

现在a.out的样子:

-rwsr-xr-x 1 root root 7544 mai 01:24 a.out 

我启动它与我curren t用户(而不是root)和ps aux | grep a.out:

root 4656 0.0 0.0 4084 684 pts/0 S+ 01:52 0:00 ./a.out 

所以这没问题。 如果我输入的是:

aaaaaaaaaaaaaaaa/bin/bash; 

我得到一个新的外壳,但我不就行了根我登录我的当前用户,我不明白为什么。因为所有者是root用户,所以我把setuid放在了这个新的bash上,并且它的root权限不会启动?

+0

缓冲区溢出在炭[8] = {0};我把几个“a”,我用/ bin/bash覆盖/ bin/ls。在我的电脑上,我必须写16“a”和/ bin/bash; – wammder

+3

'bash'检测它是否正在运行setuid并删除权限。 – Barmar

+1

请参阅https://unix.stackexchange.com/questions/74527/setuid-bit-seems-to-have-no-effect-on-bash – Barmar

回答

1

可执行问题上的setuid标志设置euid(有效的UID)。你的uid,而不是你的euid被传递给子进程。调用system()命令之前做

setuid(geteuid());