I have 2 files: shadow.txt and mytab2411.txt. "shadow.txt" contains password hashes while "mytab2411.txt" is a password lookup file containing passwords and their corresponding hashes. What I want to do is to use "mytab2411.txt" to find the passwords for "shadow.txt" by comparing hashes from both "shadow.txt" and "mytab2411.txt".
I have 2 problems:
1. I do not know how to obtain the password text from mytab2411.txt.
2. I get this output instead of the desired output:
Code:
user id: pyc 1 -password (NOT FOUND)
user id: pyc 2 -password (NOT FOUND)
user id: pyc 3 -password (NOT FOUND)
user id: pyc 4 -password (NOT FOUND)
user id: pyc 5 -password (NOT FOUND)
user id: pyc 6 -password (NOT FOUND)
shadow.txt
Code:
pyc1:$1$$Tnq7a6/C1wwyKyt0V/.BP/:17482:0:99999:7:::
pyc2:$6$$/xMg2/4CZwMUbah4IhNwCjqzZf0/OByfs6UHmq32jFbsdpbDw9bhLttC7n/bAVlM2NwJ7hBQ3d0H47leLXE6g1:17482:0:99999:7:::
pyc3:$1$$zZQKNjRd94GHyYOwXuStf0:17482:0:99999:7:::
pyc4:$6$iKYSRG68$STdY8TCgoCaNfSUcyCwSBlVekdjs0P3qXtwxSbgpQpMUnHJRRSHOT5amoR24IqZBTPNWuIfO.uhZEnGLuE4q/.:17482:0:99999:7:::
pyc5:$6$$FDqvMBbQOCyKP9uBL8E6TAEupCh72v.3/ow4fZ5HpZ/0NS7LBifFS9nJdzc/u2OEhUnRF9yC4Lw23hHjD1EmD.:17482:0:99999:7:::
pyc6:$6$$LfCuhKecDtIfX77LOTWD1PjhF1IC0hBzjxckEthmoT8mVbxKH3qJzFgEi/P9GN1mptR4WPiwuh69X/41M6pHW1:17482:0:99999:7:::
mytab2411.txt
Code:
apple:$1$$Tnq7a6/C1wwyKyt0V/.BP/
apple:$6$$vTqYXuMRNbK5N1xiTvUKcJuKVmEQyPtgUiyawaEBMwknJ3AQoOvPpr2RrANRxDTS.qo7rQuFvxZcUkT31W6uG/
banana:$1$$zZQKNjRd94GHyYOwXuStf0
banana:$6$$5iQBiKBv7vIGqC5iQJOVUpzgnSO0P.pMQ.Guwczcn9nQSu61IVKT9GU4IEYjb5WbsBLaIfZ3io59M4oac.W0/1
orange:$1$$Ro.kDk5GNLNQbdJyDEovy1
orange:$6$$/xMg2/4CZwMUbah4IhNwCjqzZf0/OByfs6UHmq32jFbsdpbDw9bhLttC7n/bAVlM2NwJ7hBQ3d0H47leLXE6g1
Desired output
Code:
user id : pyc1 – password found => apple
user id : pyc2 – password found => orange
user id : pyc3 – password found => banana
Data Error: Invalid entry found in the shadow file. (skipped)
user id : pyc5 – password (NOT FOUND)
user id : pyc6 – password (NOT FOUND)
The main code
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
int printDataError() {
printf("Data error: Invalid entry found in the shadow file. (Skipped)\n");
}
int main(void) {
char buf[1024];
char * * arr1 = NULL;
char * * arr2 = NULL;
int size1 = 0;
int size2 = 0;
FILE * f1, * f2;
f1 = fopen("shadow.txt", "r");
f2 = fopen("mytab2411.txt", "r");
// Allocate memory for shadow.txt
while (fgets(buf, 1024, f1)) {
size1++;
arr1 = realloc(arr1, sizeof(char * ) * size1);
arr1[size1 - 1] = strdup(buf);
}
// Allocate memory for mytab2411.txt
while (fgets(buf, 1024, f2)) {
size2++;
arr2 = realloc(arr2, sizeof(char * ) * size2);
arr2[size2 - 1] = strdup(buf);
}
char line[1000]; //Allocate max number of characters in a line for shadow.txt
char line2[1000]; //Allocate max number of characters in a line for mytab2411.txt
char hash[1000]; //Allocate max number of characters in a hash (substring of a line) for shadow.txt
char hash2[1000]; //Allocate max number of characters in a hash (substring of a line) for shadow.txt
char md5[5] = "$1$$"; // Define string to be searched for md5
char sha512[5] = "$6$$"; //Define string to be searched for sha512
char * ret; // Used for shadow.txt
char * ret2; //Used for shadow.txt
char * ret3; //Used for mytab2411.txt
char * ret4; //Used for mytab2411.txt
// Read shadow.txt line by line
for (int i = 0; i < size1; i++) {
memset(hash, '\0', sizeof(hash));
strcpy(line, arr1[i]);
//Search for md5 in shadow.txt
md5[4] = '\0';
ret = strstr(line, md5);
//Search for sha512 in shadow.txt
sha512[4] = '\0';
ret2 = strstr(line, sha512);
// Copies md5 hash to the variable hash if md5 is detected in shadow.txt
if (ret) {
strncpy(hash, line + 5, 26);
hash[26] = '\0';
}
// Copies sha512 hash to the variable hash if sha512 is detected in shadow.txt
else if (ret2) {
strncpy(hash, line + 5, 90);
hash[90] = '\0';
}
// Read mytab2411.txt line by line
for (int j = 0; j < size2; j++) {
memset(hash2, '\0', sizeof(hash2));
strcpy(line2, arr2[j]);
//Search for md5 in mytab2411.txt
md5[4] = '\0';
ret3 = strstr(line2, md5);
//Search for sha512 in mytab2411.txt
sha512[4] = '\0';
ret4 = strstr(line2, sha512);
// Copies sha512 hash to the variable hash if md5 is detected in mytab2411.txt
if (ret3) {
strcpy(hash2, & line2[strlen(line2) - 27]);
}
// Copies sha512 hash to the variable hash if sha512 is detected in mytab2411.txt
else if (ret4) {
strcpy(hash2, & line2[strlen(line2) - 91]);
//printf("Line %d hash: %s\n", j+1,hash2);
}
} //End of "for(int j = 0; j < size2; j++)" loop
// Compares the hash in shadow.txt (hash) and hash in mytab2411.txt (hash2).
if (strcmp(hash, hash2) == 0)
printf("user id: pyc %d - password found => \n", i + 1);
else if (strcmp(hash, hash2) != 0)
printf("user id: pyc %d -password <NOT FOUND>\n", i + 1);
else
printDataError();
} //End of "for (int i = 0; i < size1; i++)" loop
return 0;
} //End of main