Good evening, I have a problem with this C code I'm making for my uni project.
It's a cryptography class, and I'm making a script that will grab the hashed password and its salt from a shadow.txt, then compare the hashed password with possible passwords from the .txt. It all works fine up to the point where I call the decryptwithTXT() function. It just gives me a core dump problem. I've put some prints inside the function, to see where it crashes but it never prints anything, meaning it just crashes as it calls the function, but I can't figure out why.
If you have any tips regarding what I'm doing wrong and causing it to crash, I would really appreciate it. If you have also ANY other tips regarding the quality of my code, please feel free to give me some tips, I'm trying to get better at C so I'm very open to criticism.
This is my main.
#include <stdio.h>
#include <crypt.h>
#include <string.h>
int getSaltnPassword(char* name,char* salt,char* pwd);
int decryptwithTXT(char* pwd,char* hpwd,char* salt,char* file);
int main()
{
int x;
char salt[8],hashedpwd[18],pwd[255],name[255],answer,flag,filename[25];
do{
printf(" Give username of targeted account.\n");
scanf("%s",name);
if (getSaltnPassword(name,salt,hashedpwd)==1){
printf("Salt: %s\nHashed Password: %s\n",salt,hashedpwd);
printf(" Proceed with attack on selected user? Y/N \n");
scanf("%c",&answer);
scanf("%c",&answer);
}
else
printf("User not found, try again.\n");
}while (answer!='Y'&&answer!='y');
printf(" Choose one of the following:\n");
printf(" a) Attack using datamined passwords.\n");
printf(" b) Dictionary attack.\n");
printf(" c) 4 character brute-force attack\n");
scanf("%c",&flag);
scanf("%c",&flag);
switch(flag){
case 'a':
printf("Datamine\n");
x=decryptwithTXT(pwd,hashedpwd,salt,"tom_datamine.txt");
case 'b':
printf("Dictionary.\n");
case 'c':
printf("Brute force.\n");
}
return 0;
}
This is my decryptwithTXT function. For now I just want it to print all the passwords in the tom_datamine.txt
int decryptwithTXT(char* pwd,char* hpwd,char* salt,char* file){
printf("entered function");
int flag=0;
FILE *fp;
char temppwd[255];
printf("declarations");
fp=fopen(file,"r");
printf("fopen");
while(!feof(fp)){
printf("loop");
fgets(temppwd,255,(FILE*) fp);
printf("fgets");
printf("%s",temppwd);
printf("print pwd");
}
fclose(fp);
return 0;
}
And this is the function that gets the salt and hashed password from the shadow.txt. It works as intended for now.
int getSaltnPassword(char* name,char* salt, char* pwd){
FILE *fp;
char line[255],name_txt[255];
int i,salta,saltb,pwda,pwdb;
int j=0;
fp=fopen("my_shadow.txt","r");
while(!feof(fp)){
i=0;
fgets(line,255,(FILE*) fp);
while (line[i]!=':'){
name_txt[i]=line[i];
i++;
}
name_txt[i] = '\0';
if (strcmp(name_txt,name)==0){
printf("User found.\n");
salta=i+4;
saltb=i+12;
for (i=salta;i<saltb;i++){
salt[j]=line[i];
j++;
}
salt[8]='\0';
pwda=saltb+1;
pwdb=pwda+18;
j=0;
for (i=pwda;i<pwdb;i++){
pwd[j]=line[i];
j++;
}
pwd[18]='\0';
fclose(fp);
return 1;
}
}
fclose(fp);
return 0;
}
This is what my shadow.txt looks like
babis:$1$asff83lt$gggggg9nvR6civ7fZP.tt/:
anna:$1$kwif83lt$ZaNUkA9nvR6civ7fZP.tt/:
And this is what my tom_datamine.txt is like.
tom
marousi
anna
pinkfloyd
bowling
tommarousi
tomarousi
tomanna
tompf
tompinkfloyd
bowlingfloyd
pink floyd
tombowling