如何使用c语言(以编程方式)更改shadow文件的哈希部分?

问题描述 投票:0回答:1

我需要通过应用程序更改哈希部分。

下图是我的影子文件:

运行应用程序后,哈希部分将发生如下图所示的变化。

下面的代码可以工作,但我需要两件事:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <pwd.h>

int main() {
    system("cp /etc/shadow /etc/shadow_old");
    FILE* shadow = fopen("/etc/shadow_old","r+");
    if (shadow == NULL)
    { 
        printf ("Cannot open shadow file \n");
        return -1; 
    } 

    char * line = NULL;
    size_t len = 0; 
    char* delimiter= ":";
    ssize t read;
    int ret = 0; 
    char key[] = "root:";
    char value[] = "M0686fd1d172ba1:!!:118 ..... 9::::";
    char* pch = NULL; 

    while ((read = getline(&line, &len, shadow)) != -1)
    { 
        pch = NULL; 
        pch = strstr(line, key);
        printf("pos: U\n",ftell(shadow)); 
        if(pch) 
        { 
            printf("pch--> %s\n",pch);
            printf("current password--> %s\n",pch+(strlen(key)+strlen(delimiter)));
            fseek(shadow,-strlen(line),SEEK_CUR);
            printf("pos after fseek: U\n",ftell(shadow));
            break: 
        }
    }

    if (line) 
    { 
        int lenOfLine=strlen(line); 
        char* str=(char*) malloc (lenOfLine);
        memset(str,' ',lenOfLine); 
        if(value!=NULL)
        { 
            fwrite(key,1,sizeof(key),shadow);
            fwrite(value,1,sizeof(value),shadow);
        }
        free(str); 
    }

    fclose(shadow); 

    return 0;
}

1:当我的哈希值小于以前时,哈希值没有完全替换

2:我需要通用解决方案,我认为我的代码很脏。

谢谢你的建议。欣赏

c file hash
1个回答
0
投票

下面的代码就是我所需要的。

1:我根据关键参数找到了具体的行。 2:我倒回文件指针 3:读取除特定行之外的所有字符并将其写入副本文件。 4:重命名副本文件名。

const char* filename="/etc/shadow_old";  //just change shadow_old to shadow.

const char* filenameReplica="/etc/shadow_replica";

int renamePassword(const char* key,const char* value){

    system("cp /etc/shadow /etc/shadow_old");
    
    FILE* shadow = fopen(filename,"r+");
    FILE* replica = fopen(filenameReplica,"w");;
    if (shadow == NULL  || replica == NULL)
        {
            printf ("Cannot open shadow file \n");
            return -1;
        }

    

    int lineCounter=1;
    int lineNumber=0;

    char * line = NULL;
        size_t len = 0;
        char* delimiter= ":";
        ssize_t read;
        int ret = 0;
        char* pch = NULL;
    char* token = NULL;
    short userFound=0;
    
    while ((read = getline(&line, &len, shadow)) != -1)
        {
        pch = NULL;
            pch = strstr(line, key);
        
            if(pch)
        {   
            lineNumber=lineCounter;
            userFound=1;
        }else lineCounter++;
    }

    char ch='A';
    int tmp=1;
    rewind(shadow);
    while((ch =getc(shadow))!=EOF){
        //ch =getc(shadow);
        //printf("ch: %c\n",ch);
        if(tmp != lineNumber)
        {
            putc(ch,replica);
        }
        if(ch=='\n')
        {   
            tmp++;
        }
    }

    int lenOfReplacement=strlen(value); 
    int lenOfKey = strlen(key);
    char* tmpValue=(char*) malloc (lenOfKey+lenOfReplacement+1);
    strncpy(tmpValue,key,lenOfKey);
    strncpy(tmpValue + lenOfKey,value,lenOfReplacement);
    tmpValue[lenOfKey+lenOfReplacement+1]='\0';

    printf("tmpValue-->  %s\n",tmpValue);
    //fwrite("\n",1,strlen("\n"),replica); //uncomment this line if you want to change multiple user not only one user. this line create new line in shadow file(no bad effect).
    fwrite(tmpValue,1,strlen(tmpValue),replica);
    free(tmpValue);
    

    fclose(shadow);
    fclose(replica);
    remove(filename);
    rename(filenameReplica,filename);
    
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.