【发布时间】:2020-04-11 18:54:49
【问题描述】:
我一直在研究反向shell(不用于恶意用途)并开始学习如何使用popen函数并使用stdout获取输出。我已经开始测试它,它工作正常,直到我尝试使用终端命令“ls”。谁能指出(我假设是)我的错误并告诉我如何解决它?
这是C程序的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
while (1){
char* command = (char *) malloc(15*sizeof(char));
char* output = (char *) malloc(2048);
printf(">> ");
scanf("%s", command);
FILE* cmd = popen(command, "r");
fputs(output, stdout);
pclose(cmd);
if (strlen(output) != 0){
printf("\n%s\n", output);
}
}
}
这是我为程序提供的输入代码,它是结果:
>> cd /Users/
sh: /Users/: is a directory //output from previous command
>> >> ls //also why did the program print '>>' twice?
>>
另一个问题:为什么程序会打印两次>>?
【问题讨论】: