问题:当test.cgi
在shell
中运行时,这是输出:
Content-Type: text/html
ls: /test_3/: No such file or directory
test
但是,当它通过Web服务器运行时,这是输出:
test
任务 :使其也通过网络服务器打印ls: /test_3/: No such file or directory
。
编译和chmod
:(经过测试,可以通过Web服务器root
运行reboot
。
gcc test.c -o test.cgi && chmod u=rwx,go=xr,+s test.cgi
测试.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main (int argc, char **argv) {
fputs("Content-Type: text/htmlrnrn", stdout);
setuid (0);
FILE *pipe;
char buffer[100];
pipe = popen("ls /test_3/","r");
while (fgets(buffer, sizeof(buffer), pipe) != NULL)
fprintf(stdout, "%s<br>", buffer);
pclose(pipe);
fputs("test", stdout);
return 0;
}
错误字符串被打印到 stderr,您需要将其重定向到 stdout,因为这是您的 Web 服务器发送回浏览器的内容。
将 stderr 重定向到程序中的 stdout,如下所示:
fprintf(stderr, "To stderrn");
dup2(1, 2);
fprintf(stderr, "To stdoutn");
或者简单地说:
popen("ls /test_3/ 2>&1","r");