在Apache下运行SUID C脚本

时间:2011-10-02 23:38:57

标签: c linux apache suid

我在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

谢谢!

2 个答案:

答案 0 :(得分:1)

可以配置Apache,以便它可以从chroot jail运行。在那种情况下/ etc / shadow将不可用。

http://www.faqs.org/docs/securing/chap29sec254.html

答案 1 :(得分:0)

使用setenforce 0停止selinux停止可解决此问题。

相关问题