Thread: Problem with comparing hashes/substrings from 2 files

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    5

    Problem with comparing hashes/substrings from 2 files

    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
    Last edited by wei; 12-30-2017 at 06:53 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by wei
    1. I do not know how to obtain the password text from mytab2411.txt.
    From what I see, both files are colon-delimited, so you would use the same approach for mytab2411.txt as you would use for shadow.txt, e.g., use strchr to find the next ':', and from there determine the portion of the string that you wish to extract as the plaintext password or password hash in mytab2411.txt, and as the username or password hash in shadow.txt.

    It seems to me that you don't have to care about the password hash algorithm, so your code shouldn't be trying to determine it as if you were say, trying to calculate the hash to confirm it.

    EDIT:
    You should break up your program into functions that do one thing and do it well. I would also declare two structures:
    Code:
    struct UsernamePasswordHashEntry {
        char username[1000];
        char passwordHash[1000];
    };
    
    struct PasswordLookupEntry {
        char password[1000];
        char passwordHash[1000];
    };
    This would make it easier for you to read the data and perform the matching.
    Last edited by laserlight; 12-30-2017 at 07:44 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with comparing file names stored in two files.
    By Nyah Check in forum C Programming
    Replies: 9
    Last Post: 01-22-2013, 04:11 PM
  2. Comparing two files
    By 843 in forum C Programming
    Replies: 7
    Last Post: 11-24-2010, 12:50 PM
  3. comparing 2 txt files
    By norhos in forum C Programming
    Replies: 9
    Last Post: 03-13-2008, 06:18 AM
  4. Comparing Files.
    By dannyp in forum Tech Board
    Replies: 3
    Last Post: 09-12-2004, 03:12 PM
  5. Comparing Files
    By snowy101 in forum C++ Programming
    Replies: 3
    Last Post: 06-19-2002, 10:43 AM

Tags for this Thread