Madmouse

A program example to mark an executable with an md5 hash

Jan 28th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.22 KB | None | 0 0
  1.  
  2. //
  3. //  A program example to mark an executable with a specified unique identification number
  4. //  Written By: MadMouse
  5. ////////////////////////////////////////////////////////////////////////////////////////////
  6. // example implementation
  7. ////////////////////////////////////////////////////////////////////////////////////////////
  8. /*  #include <unistd.h>
  9.     main()
  10.     {
  11.         static const char poop[] = "\xDE\xAD\xBE\xEF!ca419c121f43ab9c91a23e5cb727cef6";
  12.         puts((char*)poop+5);
  13.     }
  14. */
  15. ////////////////////////////////////////////////////////////////////////////////////////////
  16. // Example run:
  17. ////////////////////////////////////////////////////////////////////////////////////////////
  18. // [user@n0 identifier]$ gcc test.c -o test
  19. // [user@n0 identifier]$ ./test
  20. // ca419c121f43ab9c91a23e5cb727cef6
  21. // [user@n0 identifier]$ g++ identifier.cc -o identifier
  22. // [user@n0 identifier]$ ./identifier test $(date|md5sum)
  23. // Writting version after magic sequence
  24. // Old: ca419c121f43ab9c91a23e5cb727cef6
  25. // New: e68658eacedafb864fbf1b3484781c1e
  26. // [user@n0 identifier]$ ./test
  27. // e68658eacedafb864fbf1b3484781c1e
  28. // [user@n0 identifier]$
  29.  
  30. #include <cstring>
  31. #include <fstream>
  32. #include <iostream>
  33. #include <stdlib.h>
  34.  
  35. typedef unsigned char BYTE;
  36.  
  37. // Get the size of a file
  38. long getfilesize(FILE *file)
  39. {
  40.     long end;
  41.     fseek(file, 0L, SEEK_END);
  42.     end = ftell(file);
  43.     fseek(file, 0L, SEEK_SET);
  44.     return end;
  45. }
  46.  
  47. void usage(const char* name)
  48. {
  49.     std::cout << "Usage: " << name << " <file> <md5hash>" << std::endl;
  50.     exit(1);
  51. }
  52.  
  53. int main(int argc,const char** argv)
  54. {
  55.     if(argc != 4) usage(argv[0]);
  56.     else if(strlen(argv[2])<32) usage(argv[0]);
  57.    
  58.     FILE* file = NULL;  // declare a file pointer
  59.     BYTE* buffer;   // buffer for file
  60.    
  61.     std::cout << "Writting version after magic sequence" << std::endl;
  62.     if ((file = fopen(argv[1], "rb")) == NULL)  // open the file for reading
  63.     {
  64.         std::cout << "Could not open file..." << std::endl;
  65.         return 1;       // return fail
  66.     }
  67.    
  68.     long fsize = getfilesize(file); // get the size of the exe
  69.     buffer = new BYTE[fsize];       // allocate space to store exe in mem
  70.     fread(buffer, fsize, 1, file);  // read the exe into memory
  71.  
  72.     long counter=0; // counter... lol
  73.     for(;counter<fsize;++counter)   // count through the file in memory
  74.     {
  75.         // if we find the magic sequence
  76.         if(buffer[counter]==0xDE&&buffer[counter+1]==0xAD&&buffer[counter+2]==0xBE&&buffer[counter+3]==0xEF&&buffer[counter+4]=='!')
  77.         {
  78.             std::cout << "Old: " <<(BYTE*) buffer+(counter+5) << std::endl;
  79.             break;
  80.         }
  81.     }
  82.     fclose(file);       // close the file
  83.     if(counter==fsize)
  84.     {
  85.         std::cerr << "We did not find the magic number..." << std::endl;
  86.         return 1;
  87.     }
  88.     for(unsigned int i=0;i<=strlen(argv[2]);++i)
  89.     {
  90.         buffer[(counter+5)+i]=argv[2][i];
  91.         if ((file = fopen(argv[1], "wb")) == NULL)  // open the file for writing
  92.         {
  93.             delete[] buffer;    // free buffer
  94.             std::cout << "Failed to write to file..." << std::endl;
  95.             return 1;       // return fail
  96.         }
  97.     }
  98.  
  99.     fwrite(buffer,sizeof(BYTE),fsize,file);  // write the new identifier
  100.     std::cout<<"New: " << argv[2] << std::endl;
  101.     delete[] buffer;    // free the buffer
  102.     fclose(file);       // close the file
  103.     return 0;        // return success
  104. }
Advertisement
Add Comment
Please, Sign In to add comment