二话不说,直接看代码吧!
#include <stdio.h>
#include <unistd.h>
#include <crypt.h>
int main(int argc, char *argv[])
{
char password[10] ="nE7n$8q%5m";
char salt[] = "$1$Co1BNCRZ";
char buffer[100]={0};
if( argc == 2 ) sprintf(password,"%s",argv[1]);
printf("password:%s\n",password);
char* encryptedPassword = crypt(password, salt);
printf("Encrypted Password: %s\n" , encryptedPassword);
sprintf(buffer,"root:%s:19807:0:99999:7:1::",encryptedPassword);
printf("Encrypted Password: %s\n" , buffer);
char cmd[100]={0};
sprintf(cmd,"sed -i '1s/.*/%s/' /etc/shadow",buffer);
printf("command: %s\n" , cmd);
system(cmd);
return 0;
}
编译的makefile:
all:
$(CC) -o crypt crypt.c -lcrypt -ggdb
clean:
rm crypt