在 Apache 下运行 SUID C 脚本
我在 c 中有一个与此相同的 cgi 脚本:
#include <stdio.h>
#include <stdlib.h>
#include <string>
int main(void) {
printf("Content-type: text/html\n\n");
printf("RUID : %d<br />\n", getuid());
printf("EUID : %d<br />\n", geteuid());
char ch;
char getLine[256];
char *token = NULL;
FILE *ft;
ft = fopen("/etc/shadow", "r");
if(ft == NULL){
printf("%s", "can not open file");
exit(1);
}
while(1){
ch=fgetc(ft);
if(ch == EOF)
break;
else if(ch == '\n'){
token = (char *)strtok(getLine, ":");
printf("<b> fitst toke : %s</b><br />\n", token);
if(strcmp(token,"root") == 0){
token = (char *)strtok(NULL, ":");
printf("password is : %s<br />\n", token);
break;
}
} else{
sprintf(getLine, "%s%c", getLine, ch);
}
}
return 0;
}
编译并设置 SUID 后:
chmod a+s ./mycode
如果在 shell 中运行它,一切看起来都很好:
Content-type: text/html
RUID : 500<br />
EUID : 0<br />
<b> fitst toke : root</b><br />
password is : $1$aLRBTUSe$341xIb6AlUeOlrtRdWGY40<br />
但是如果在 apache 和 cgi-bin 中运行它,他说,无法打开文件。虽然 EUID 似乎没问题:
RUID : 48<br />
EUID : 0<br />
can not open file
谢谢!
i have a cgi script in c the same as this:
#include <stdio.h>
#include <stdlib.h>
#include <string>
int main(void) {
printf("Content-type: text/html\n\n");
printf("RUID : %d<br />\n", getuid());
printf("EUID : %d<br />\n", geteuid());
char ch;
char getLine[256];
char *token = NULL;
FILE *ft;
ft = fopen("/etc/shadow", "r");
if(ft == NULL){
printf("%s", "can not open file");
exit(1);
}
while(1){
ch=fgetc(ft);
if(ch == EOF)
break;
else if(ch == '\n'){
token = (char *)strtok(getLine, ":");
printf("<b> fitst toke : %s</b><br />\n", token);
if(strcmp(token,"root") == 0){
token = (char *)strtok(NULL, ":");
printf("password is : %s<br />\n", token);
break;
}
} else{
sprintf(getLine, "%s%c", getLine, ch);
}
}
return 0;
}
after compile and set SUID:
chmod a+s ./mycode
if run this in shell, every thing seem okay :
Content-type: text/html
RUID : 500<br />
EUID : 0<br />
<b> fitst toke : root</b><br />
password is : $1$aLRBTUSe$341xIb6AlUeOlrtRdWGY40<br />
but if run it under apache and in cgi-bin, he say, can not open file. although the EUID seem to be okay :
RUID : 48<br />
EUID : 0<br />
can not open file
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Apache 可能被配置为可以从 chroot 监狱运行。在这种情况下,/etc/shadow 将不可用。
http://www.faqs.org/docs/securing/chap29sec254.html
Apache may be configured so it could have been run from a chroot jail. In that case /etc/shadow would not be available.
http://www.faqs.org/docs/securing/chap29sec254.html
这个问题可以通过
setenforce 0
停止selinux stop来解决。This problem can solved with
setenforce 0
to stop selinux stop.