使用环境变量执行其他文件

问题描述 投票:0回答:4

我有点卡住了。我有一个C程序,其中包含环境变量$ USER。目标是使用环境变量通过命令注入来执行其他文件。

我已经尝试了其他方法来声明USER。例如:将USER声明为

env USER=">/dev/null ; cat /home/Steve/Public/file2.txt". 

不幸的是,这没有用。

C程序:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

// Execute any shell command
void execute(char *cmd)
{
   execl("/bin/bash", "bash", "-p", "-c", cmd, NULL);
}

void sanitise(char *password)
{
  int i,j;
  char tmp[15];

  // remove non-alphabet  characters from passwords
  j=0;
  for(i=0; i < 15; ++i) 
    if(password[i] >= 'a' && password[i] <= 'z') {
      tmp[j]=password[i];
      ++j;
    } else break; 
  tmp[j] = '\0';

  strcpy(password, tmp);

}


int authenticate(char *str)
{
  char stored_password[15]="";
  char pass[15];
  char path[128] = "/home/Steve/private/password";
  int i;

  FILE *fpp; 
  int auth=0;

  fpp = fopen(path, "r");

  if(fpp == NULL)
  {
     printf("Password file %s not found\n", path);
     exit(1);
  }

  fgets(stored_password, 15, fpp);
  sanitise(stored_password);

  strcpy(pass, str);
  sanitise(pass); 

  if(strcmp(stored_password,pass) == 0)
     auth=1;
  else {
     auth=0;
  }

  fclose(fpp);

  return auth; 
}

int main(int argc, char* argv[], char *envp[])
{
  char error[256] = "/home/Steve/Public/errormessage.sh $USER ";
  char pass[15];

  if(argc < 2) 
  {
    printf("Usage: %s password\n", argv[0]);
    return 0; 
  }

  // copy only 15 characters from user input, to prevent stack smashing
  strncpy(pass, argv[1], 15);
  pass[14]='\0';

  if(!authenticate(pass)) {
    // Log all failed attempts
    printf("Wrong password. This incident has been logged.\n");
    strcat(error, pass);
    execute(error); // Execute script to log events
    return 0;
  }

  // Display 'secret-file2'
  execute("cat /home/Steve/Public/file2.txt");

  return 0;
}

目标是使程序从变量USER输出文件,而不是从错误char中声明的初始文件路径输出文件。理想情况下,根本无需更改C程序。

谁能告诉我我在这里想念的东西吗?

c linux bash environment-variables code-injection
4个回答
0
投票

如果要使用带有新设置的$ {USER}变量“ env -i”的C程序,则应使用。例如:env -i USER='injected code' name_of_c_program


0
投票

如果程序正在执行此操作,它将执行以下命令:

/home/Steve/Public/errormessage.sh $USER <provided-password>

由于您控制了提供的密码(也就是说,您可以在argv[1]中传递您想要的密码,只要最多14个字符),您就可以只执行另一个bash shell,然后使用它运行任何您想要的命令。确实不需要篡改$USER

$ ./program '; bash'
Wrong password. This incident has been logged.
$ <--- another bash shell has now started
$ cat /home/Steve/Public/file2.txt
...contents of file2.txt...

如果要使用单个命令运行它,这应该可以工作:

echo 'cat /home/Steve/Public/file2.txt' | ./program '; bash'

-1
投票

使用'getenv'库函数获取环境变量的值(USER)。然后使用'system(path)'运行命令。


-1
投票
#include <stdlib.h>

...
char path[256] = "/home/Steve/Public/file1.sh ";
strcat( path, getenv("USER") );

//char path[256];
//sprintf(file,"%s%s","/home/Steve/Public/file1.sh ",getenv("USER"));
...
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.