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
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...