Nebula level02

There is a vulnerability in the below program that allows arbitrary programs to be executed, can you find it?

To do this level, log in as the level02 account with the password level02. Files for this level can be found in /home/flag02.

看起來跟上一關很像…

以下為 /home/flag02/flag02 的 code

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <stdio.h>

int main(int argc, char **argv, char **envp)
{
  char *buffer;

  gid_t gid;
  uid_t uid;

  gid = getegid();
  uid = geteuid();

  setresgid(gid, gid, gid);
  setresuid(uid, uid, uid);

  buffer = NULL;

  asprintf(&buffer, "/bin/echo %s is cool", getenv("USER"));
  printf("about to call system(\"%s\")\n", buffer);
  
  system(buffer);
}

跟上次的架構很像…又有沒看過得函式,直接 man 下去

asprintf(char **strp, const char *format) 就是 printf 的變形, printf 輸出到標準輸出(STDOUT), asprintf 則是輸出到第一個 string pointer

getenv(const char *name) 會根據參數(環境變數名)回傳變數值。


程式有3步驟:

  1. "%s" 是環境變數 USER 的值, buffer 中會儲存之後給 system 執行的指令
  2. 印出即將執行的指令
  3. system 執行指令

跟上一關類似,先要先修改環境變數 USER,使得 echo 不會被執行,而是執行我們的 getflag

Unix 指令有一個很方便的功能叫做 pipe (|),可以將前一個指令的輸出導到下一個指令的輸入,於是我們可以把 USER 改成

export USER="${USER}|getflag"

接著…

成功 0w0