Run SUID C scripts under Apache
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, ":");
pri开发者_高级运维ntf("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!
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
This problem can solved with setenforce 0
to stop selinux stop.
精彩评论