c语言实现linux下高危函数system (简易V1.0版本)
system这个函数真的是要慎用,一不小心就会留下漏洞。
下面是用c语言简易的实现了一下system函数
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
#include<sys/types.h>
int main(int argc, char *argv[])
{
pid_t sonpid;
printf("please enter the command: ");
char cmdstring[32];
gets(cmdstring);//但是编译器提示这里用gets并不安全
sonpid = fork();
if(sonpid < 0)
{
perror("fork!");
}
else if(sonpid == 0)
{
char * command[] = {"/bin/sh","-c",cmdstring,NULL};
execvp(command[0],command);
}
else
{
waitpid(sonpid,NULL,0);
_exit(0);
}
return 0;
}
代码运行截图如下: