linux下system函數的簡單分析

来源:http://www.cnblogs.com/1oner/archive/2017/05/10/6837920.html
-Advertisement-
Play Games

代碼位於glibc/sysdeps/posix/system.c,這裡system是__libc_system的弱別名,而__libc_system是do_system的前端函數,進行了參數的檢查,接下來看do_system函數。 1 static int 2 do_system (const ch ...


 1 int
 2 __libc_system (const char *line)
 3 {
 4   if (line == NULL)
 5     /* Check that we have a command processor available.  It might
 6        not be available after a chroot(), for example.  */
 7     return do_system ("exit 0") == 0;
 8 
 9   return do_system (line);
10 }
11 weak_alias (__libc_system, system)

代碼位於glibc/sysdeps/posix/system.c,這裡system是__libc_system的弱別名,而__libc_system是do_system的前端函數,進行了參數的檢查,接下來看do_system函數。

  1 static int
  2 do_system (const char *line)
  3 {
  4   int status, save;
  5   pid_t pid;
  6   struct sigaction sa;
  7 #ifndef _LIBC_REENTRANT
  8   struct sigaction intr, quit;
  9 #endif
 10   sigset_t omask;
 11 
 12   sa.sa_handler = SIG_IGN;
 13   sa.sa_flags = 0;
 14   __sigemptyset (&sa.sa_mask);
 15 
 16   DO_LOCK ();
 17   if (ADD_REF () == 0)
 18     {
 19       if (__sigaction (SIGINT, &sa, &intr) < 0)
 20     {
 21       (void) SUB_REF ();
 22       goto out;
 23     }
 24       if (__sigaction (SIGQUIT, &sa, &quit) < 0)
 25     {
 26       save = errno;
 27       (void) SUB_REF ();
 28       goto out_restore_sigint;
 29     }
 30     }
 31   DO_UNLOCK ();
 32 
 33   /* We reuse the bitmap in the 'sa' structure.  */
 34   __sigaddset (&sa.sa_mask, SIGCHLD);
 35   save = errno;
 36   if (__sigprocmask (SIG_BLOCK, &sa.sa_mask, &omask) < 0)
 37     {
 38 #ifndef _LIBC
 39       if (errno == ENOSYS)
 40     __set_errno (save);
 41       else
 42 #endif
 43     {
 44       DO_LOCK ();
 45       if (SUB_REF () == 0)
 46         {
 47           save = errno;
 48           (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
 49         out_restore_sigint:
 50           (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL);
 51           __set_errno (save);
 52         }
 53     out:
 54       DO_UNLOCK ();
 55       return -1;
 56     }
 57     }
 58 
 59 #ifdef CLEANUP_HANDLER
 60   CLEANUP_HANDLER;
 61 #endif
 62 
 63 #ifdef FORK
 64   pid = FORK ();
 65 #else
 66   pid = __fork ();
 67 #endif
 68   if (pid == (pid_t) 0)
 69     {
 70       /* Child side.  */
 71       const char *new_argv[4];
 72       new_argv[0] = SHELL_NAME;
 73       new_argv[1] = "-c";
 74       new_argv[2] = line;
 75       new_argv[3] = NULL;
 76 
 77       /* Restore the signals.  */
 78       (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL);
 79       (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
 80       (void) __sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL);
 81       INIT_LOCK ();
 82 
 83       /* Exec the shell.  */
 84       (void) __execve (SHELL_PATH, (char *const *) new_argv, __environ);
 85       _exit (127);
 86     }
 87   else if (pid < (pid_t) 0)
 88     /* The fork failed.  */
 89     status = -1;
 90   else
 91     /* Parent side.  */
 92     {
 93       /* Note the system() is a cancellation point.  But since we call
 94      waitpid() which itself is a cancellation point we do not
 95      have to do anything here.  */
 96       if (TEMP_FAILURE_RETRY (__waitpid (pid, &status, 0)) != pid)
 97     status = -1;
 98     }
 99 
100 #ifdef CLEANUP_HANDLER
101   CLEANUP_RESET;
102 #endif
103 
104   save = errno;
105   DO_LOCK ();
106   if ((SUB_REF () == 0
107        && (__sigaction (SIGINT, &intr, (struct sigaction *) NULL)
108        | __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL)) != 0)
109       || __sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL) != 0)
110     {
111 #ifndef _LIBC
112       /* glibc cannot be used on systems without waitpid.  */
113       if (errno == ENOSYS)
114     __set_errno (save);
115       else
116 #endif
117     status = -1;
118     }
119   DO_UNLOCK ();
120 
121   return status;
122 }
do_system

首先函數設置了一些信號處理程式,來處理SIGINT和SIGQUIT信號,此處我們不過多關心,關鍵代碼段在這裡

 1 #ifdef FORK
 2   pid = FORK ();
 3 #else
 4   pid = __fork ();
 5 #endif
 6   if (pid == (pid_t) 0)
 7     {
 8       /* Child side.  */
 9       const char *new_argv[4];
10       new_argv[0] = SHELL_NAME;
11       new_argv[1] = "-c";
12       new_argv[2] = line;
13       new_argv[3] = NULL;
14 
15       /* Restore the signals.  */
16       (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL);
17       (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
18       (void) __sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL);
19       INIT_LOCK ();
20 
21       /* Exec the shell.  */
22       (void) __execve (SHELL_PATH, (char *const *) new_argv, __environ);
23       _exit (127);
24     }
25   else if (pid < (pid_t) 0)
26     /* The fork failed.  */
27     status = -1;
28   else
29     /* Parent side.  */
30     {
31       /* Note the system() is a cancellation point.  But since we call
32      waitpid() which itself is a cancellation point we do not
33      have to do anything here.  */
34       if (TEMP_FAILURE_RETRY (__waitpid (pid, &status, 0)) != pid)
35     status = -1;
36     }

首先通過前端函數調用系統調用fork產生一個子進程,其中fork有兩個返回值,對父進程返回子進程的pid,對子進程返回0。所以子進程執行6-24行代碼,父進程執行30-35行代碼。

子進程的邏輯非常清晰,調用execve執行SHELL_PATH指定的程式,參數通過new_argv傳遞,環境變數為全局變數__environ。

其中SHELL_PATH和SHELL_NAME定義如下

1 #define    SHELL_PATH    "/bin/sh"    /* Path of the shell.  */
2 #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函數。

  1 STATIC int
  2 LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
  3          int argc, char **argv,
  4 #ifdef LIBC_START_MAIN_AUXVEC_ARG
  5          ElfW(auxv_t) *auxvec,
  6 #endif
  7          __typeof (main) init,
  8          void (*fini) (void),
  9          void (*rtld_fini) (void), void *stack_end)
 10 {
 11   /* Result of the 'main' function.  */
 12   int result;
 13 
 14   __libc_multiple_libcs = &_dl_starting_up && !_dl_starting_up;
 15 
 16 #ifndef SHARED
 17   char **ev = &argv[argc + 1];
 18 
 19   __environ = ev;
 20 
 21   /* Store the lowest stack address.  This is done in ld.so if this is
 22      the code for the DSO.  */
 23   __libc_stack_end = stack_end;
    ......
202 /* Nothing fancy, just call the function. */ 203 result = main (argc, argv, __environ MAIN_AUXVEC_PARAM); 204 #endif 205 206 exit (result); 207 }

我們可以看到,在沒有define SHARED的情況下,在第19行定義了__environ的值。啟動程式調用LIBC_START_MAIN之前,會先將環境變數和argv中的字元串保存起來(其實是保存到棧上),然後依次將環境變數中各項字元串的地址,argv中各項字元串的地址和argc入棧,所以環境變數數組一定位於argv數組的正後方,以一個空地址間隔。所以第17行的&argv[argc + 1]語句就是取環境變數數組在棧上的首地址,保存到ev中,最終保存到__environ中。第203行調用main函數,會將__environ的值入棧,這個被棧溢出覆蓋掉沒什麼問題,只要保證__environ中的地址處不被覆蓋即可。

所以,當棧溢出的長度過大,溢出的內容覆蓋了__environ中地址中的重要內容時,調用system函數就會失敗。具體環境變數距離溢出地址有多遠,可以通過在_start中下斷查看。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 背景簡介: 本文為針對一次windows平臺RAC資料庫遷移至Linux平臺RAC的筆記,基本步驟為: 1.搭建windows RAC到Linux 單實例資料庫的DataGuard 2.做switchover,將備庫IP修改為原RAC資料庫的scanip 3.搭建單實例到Linux RAC的Data ...
  • ZooKeeper是一個分散式開源框架,提供了協調分散式應用的基本服務,它向外部應用暴露一組通用服務——分散式同步(Distributed Synchronization).命名服務(Naming Service).集群維護(Group Maintenance)等,簡化分散式應用協調及其管理的難度, ...
  • ElasticSearch 2.4版本支持Java正則表達式查詢,但是,在對大段的文本(Text Block)進行挖掘之前,必須瞭解正則表達式查詢的特殊之處。由於分析器會對文本欄位進行分詞,移除停用詞,小寫轉換等操作,最終存儲在倒轉索引中的是小寫的標記流(Token Stream),預設情況下,每一... ...
  • 以下摘自官方文檔: 語法: Or: If you declare an alias for a table, you must use the alias when referring to the table: Correct: ...
  • 一、最小化安裝 1、進入系統之後,要配置network網路。 首先ping www.baidu.com (Ctrl+z 推出正在執行的命令) 如果ping不通,則修改: vi /etc/sysconfig/network-scripts/ifcfg-ens33 ONBOOT=yes 修改之後重啟ne ...
  • 先說說他們的關係,Nginx和uWSGI都是Web伺服器,Nginx負責靜態內容,uWSGI負責Python這樣的動態內容,二者配合共同提供Web服務以實現提高效率和負載均衡等目的。uWSGI實現了多個協議,如WSGI,HTTP協議,還有它自己的uwsgi協議,想瞭解更多關於uWSGI和uwsgi協... ...
  • 一 安裝FTP 1 檢測是否已經安裝FTP 2 若沒有,則進行安裝 二 設置vsftpd開機啟動 三 配置FTP伺服器(開啟基於用戶的訪問控制) 1 配置文件的修改 2 修改selinux 若報錯,getsebool: SELinux is disabled。則 修改 SELINUX=1。 然後重啟 ...
  • 1. Cobbler常用命令 1.1 查看cobbler幫助 # cobbler --help usage cobbler <distro|profile|system|repo|image|mgmtclass|package|file> ... [add|edit|copy|getks*|list ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...