侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

linux下system函数的简单分析

linux  /  管理员 发布于 7年前   154

简单分析了linux下system函数的相关内容,具体内容如下

int__libc_system (const char *line){ if (line == NULL)  /* Check that we have a command processor available. It might    not be available after a chroot(), for example. */  return do_system ("exit 0") == 0; return do_system (line);}weak_alias (__libc_system, system)

代码位于glibc/sysdeps/posix/system.c,这里system是__libc_system的弱别名,而__libc_system是do_system的前端函数,进行了参数的检查,接下来看do_system函数。

static intdo_system (const char *line){ int status, save; pid_t pid; struct sigaction sa;#ifndef _LIBC_REENTRANT struct sigaction intr, quit;#endif sigset_t omask; sa.sa_handler = SIG_IGN; sa.sa_flags = 0; __sigemptyset (&sa.sa_mask); DO_LOCK (); if (ADD_REF () == 0)  {   if (__sigaction (SIGINT, &sa, &intr) < 0)  {   (void) SUB_REF ();   goto out;  }   if (__sigaction (SIGQUIT, &sa, &quit) < 0)  {   save = errno;   (void) SUB_REF ();   goto out_restore_sigint;  }  } DO_UNLOCK (); /* We reuse the bitmap in the 'sa' structure. */ __sigaddset (&sa.sa_mask, SIGCHLD); save = errno; if (__sigprocmask (SIG_BLOCK, &sa.sa_mask, &omask) < 0)  {#ifndef _LIBC   if (errno == ENOSYS)  __set_errno (save);   else#endif  {   DO_LOCK ();   if (SUB_REF () == 0)    {     save = errno;     (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);    out_restore_sigint:     (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL);     __set_errno (save);    }  out:   DO_UNLOCK ();   return -1;  }  }#ifdef CLEANUP_HANDLER CLEANUP_HANDLER;#endif#ifdef FORK pid = FORK ();#else pid = __fork ();#endif if (pid == (pid_t) 0)  {   /* Child side. */   const char *new_argv[4];   new_argv[0] = SHELL_NAME;   new_argv[1] = "-c";   new_argv[2] = line;   new_argv[3] = NULL;   /* Restore the signals. */   (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL);   (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);   (void) __sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL);   INIT_LOCK ();   /* Exec the shell. */   (void) __execve (SHELL_PATH, (char *const *) new_argv, __environ);   _exit (127);  } else if (pid < (pid_t) 0)  /* The fork failed. */  status = -1; else  /* Parent side. */  {   /* Note the system() is a cancellation point. But since we call   waitpid() which itself is a cancellation point we do not   have to do anything here. */   if (TEMP_FAILURE_RETRY (__waitpid (pid, &status, 0)) != pid)  status = -1;  }#ifdef CLEANUP_HANDLER CLEANUP_RESET;#endif save = errno; DO_LOCK (); if ((SUB_REF () == 0    && (__sigaction (SIGINT, &intr, (struct sigaction *) NULL)    | __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL)) != 0)   || __sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL) != 0)  {#ifndef _LIBC   /* glibc cannot be used on systems without waitpid. */   if (errno == ENOSYS)  __set_errno (save);   else#endif  status = -1;  } DO_UNLOCK (); return status;}do_system

首先函数设置了一些信号处理程序,来处理SIGINT和SIGQUIT信号,此处我们不过多关心,关键代码段在这里

#ifdef FORK pid = FORK ();#else pid = __fork ();#endif if (pid == (pid_t) 0)  {   /* Child side. */   const char *new_argv[4];   new_argv[0] = SHELL_NAME;   new_argv[1] = "-c";   new_argv[2] = line;   new_argv[3] = NULL;   /* Restore the signals. */   (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL);   (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);   (void) __sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL);   INIT_LOCK ();   /* Exec the shell. */   (void) __execve (SHELL_PATH, (char *const *) new_argv, __environ);   _exit (127);  } else if (pid < (pid_t) 0)  /* The fork failed. */  status = -1; else  /* Parent side. */  {   /* Note the system() is a cancellation point. But since we call   waitpid() which itself is a cancellation point we do not   have to do anything here. */   if (TEMP_FAILURE_RETRY (__waitpid (pid, &status, 0)) != pid)  status = -1;  }

首先通过前端函数调用系统调用fork产生一个子进程,其中fork有两个返回值,对父进程返回子进程的pid,对子进程返回0。所以子进程执行6-24行代码,父进程执行30-35行代码。

子进程的逻辑非常清晰,调用execve执行SHELL_PATH指定的程序,参数通过new_argv传递,环境变量为全局变量__environ。

其中SHELL_PATH和SHELL_NAME定义如下

#define  SHELL_PATH  "/bin/sh"  /* Path of the shell. */#define  SHELL_NAME  "sh"    /* Name to give it. */ 

其实就是生成一个子进程调用/bin/sh -c "命令"来执行向system传入的命令。 

下面其实是我研究system函数的原因与重点:

在CTF的pwn题中,通过栈溢出调用system函数有时会失败,听师傅们说是环境变量被覆盖,但是一直都是懵懂,今天深入学习了一下,总算搞明白了。

在这里system函数需要的环境变量储存在全局变量__environ中,那么这个变量的内容是什么呢。

__environ是在glibc/csu/libc-start.c中定义的,我们来看几个关键语句。

# define LIBC_START_MAIN __libc_start_main

__libc_start_main是_start调用的函数,这涉及到程序开始时的一些初始化工作,对这些名词不了解的话可以看一下这篇文章。接下来看LIBC_START_MAIN函数。

STATIC intLIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),     int argc, char **argv,#ifdef LIBC_START_MAIN_AUXVEC_ARG     ElfW(auxv_t) *auxvec,#endif     __typeof (main) init,     void (*fini) (void),     void (*rtld_fini) (void), void *stack_end){ /* Result of the 'main' function. */ int result; __libc_multiple_libcs = &_dl_starting_up && !_dl_starting_up;#ifndef SHARED char **ev = &argv[argc + 1]; __environ = ev; /* Store the lowest stack address. This is done in ld.so if this is   the code for the DSO. */ __libc_stack_end = stack_end;    ...... /* Nothing fancy, just call the function. */ result = main (argc, argv, __environ MAIN_AUXVEC_PARAM);#endif exit (result);}

我们可以看到,在没有define SHARED的情况下,在第19行定义了__environ的值。启动程序调用LIBC_START_MAIN之前,会先将环境变量和argv中的字符串保存起来(其实是保存到栈上),然后依次将环境变量中各项字符串的地址,argv中各项字符串的地址和argc入栈,所以环境变量数组一定位于argv数组的正后方,以一个空地址间隔。所以第17行的&argv[argc + 1]语句就是取环境变量数组在栈上的首地址,保存到ev中,最终保存到__environ中。第203行调用main函数,会将__environ的值入栈,这个被栈溢出覆盖掉没什么问题,只要保证__environ中的地址处不被覆盖即可。

所以,当栈溢出的长度过大,溢出的内容覆盖了__environ中地址中的重要内容时,调用system函数就会失败。具体环境变量距离溢出地址有多远,可以通过在_start中下断查看。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    linux共享上网设置实例详解
    下一条:
    详解Linux如何查看当前占用CPU或内存最多的几个进程
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Linux系统中使用Iptables实现流量转发功能流程步骤(0个评论)
    • vim学习笔记-入门级需要了解的一些快捷键(0个评论)
    • 在centos7系统中实现分区并格式化挂载一块硬盘到/data目录流程步骤(0个评论)
    • 在Linux系统种查看某一个进程所占用的内存命令(0个评论)
    • Linux中grep命令中的10种高级用法浅析(0个评论)
    • 近期文章
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2017-07
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-03
    • 2022-04
    • 2022-08
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-06
    • 2023-07
    • 2023-10
    • 2023-12
    • 2024-01
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客