我需要通过应用程序更改散列部分.

下图是我的影子文件:

enter image description here

运行应用程序后,哈希部分将像下面的图像改变.

enter image description here

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

#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:当我的哈希值小于以前时,哈希值未被完全替换

他说:我需要一个通用的解决方案,我想我的代码是脏的.

谢谢你的广告.赏识

推荐答案

下面的代码是我需要的全部.

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;
}

C++相关问答推荐

理解C中的指针定义

位屏蔽对于无符号转换是强制的吗?

ATTiny1606定时器TCA 0中断未触发

手动矢量化性能差异较大

ARM64 ASIMD固有的加载uint8_t* 到uint16x8(x3)?

用C宏替换strncMP函数中的参数

在C++中使用函数指针的正确语法

在每种If-Else情况下执行语句的最佳方式

每次除以或乘以整数都会得到0.0000

有什么方法可以将字符串与我们 Select 的子字符串分开吗?喜欢:SIN(LOG(10))

在移动数组元素时获得意外输出

C11/C17标准允许编译器清除复合文字内存吗?

我应该在递归中使用全局变量吗

循环中的静态变量与块中的变量和循环

为四维数组中的Dim-1和Dim-3重新分配空间

浮动目标文件,数据段

如何在MSVC中使用intSafe.h函数?

RISC-V GCC编译器错误编译ASM代码

我可以使用Windows SDK';s IN6_IS_ADDR_LOOPBACK等,尽管没有文档?

程序打印一些随机空行