Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/ptrace.h>
  5. #include <time.h>
  6. #include <errno.h>
  7. #include <limits.h>
  8. #include <getopt.h>
  9.  
  10.  
  11. void welcome() {
  12.     printf("\n");
  13.     printf("\n");
  14.     printf("     .... NO! ...                  ... MNO! ...\n");
  15.     printf("   ..... MNO!! ...................... MNNOO! ...\n");
  16.     printf(" ..... MMNO! ......................... MNNOO!! .\n");
  17.     printf("..... MNOONNOO!   MMMMMMMMMMPPPOII!   MNNO!!!! .\n");
  18.     printf(" ... !O! NNO! MMMMMMMMMMMMMPPPOOOII!! NO! ....\n");
  19.     printf("    ...... ! MMMMMMMMMMMMMPPPPOOOOIII! ! ...\n");
  20.     printf("   ........ MMMMMMMMMMMMPPPPPOOOOOOII!! .....\n");
  21.     printf("   ........ MMMMMOOOOOOPPPPPPPPOOOOMII! ...\n");
  22.     printf("    ....... MMMMM..    OPPMMP    .,OMI! ....\n");
  23.     printf("     ...... MMMM::   o.,OPMP,.o   ::I!! ...\n");
  24.     printf("         .... NNM:::.,,OOPM!P,.::::!! ....\n");
  25.     printf("          .. MMNNNNNOOOOPMO!!IIPPO!!O! .....\n");
  26.     printf("         ... MMMMMNNNNOO:!!:!!IPPPPOO! ....\n");
  27.     printf("           .. MMMMMNNOOMMNNIIIPPPOO!! ......\n");
  28.     printf("          ...... MMMONNMMNNNIIIOO!..........\n");
  29.     printf("       ....... MN MOMMMNNNIIIIIO! OO ..........\n");
  30.     printf("    ......... MNO! IiiiiiiiiiiiI OOOO ...........\n");
  31.     printf("  ...... NNN.MNO! . O!!!!!!!!!O . OONO NO! ........\n");
  32.     printf("   .... MNNNNNO! ...OOOOOOOOOOO .  MMNNON!........\n");
  33.     printf("   ...... MNNNNO! .. PPPPPPPPP .. MMNON!........\n");
  34.     printf("      ...... OO! ................. ON! .......\n");
  35.     printf("         ................................\n\n");
  36. }
  37.  
  38. void randompass(char* s, const int len) {
  39.     static const char alphanum[] =
  40.         "0123456789"
  41.         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  42.         "abcdefghijklmnopqrstuvwxyz";
  43.     int i;
  44.  
  45.     for (i = 0; i < len; ++i) {
  46.         s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
  47.     }
  48.  
  49.     s[len] = 0;
  50. }
  51.  
  52. unsigned int BKDRHash(char *str){
  53.     unsigned int seed = 131;
  54.     unsigned int hash = 0;
  55.     unsigned int i = 0;
  56.     char buff[60];
  57.     unsigned int len = 0;
  58.     strcpy(buff, str);
  59.     len = strlen(buff);
  60.     if(len >40)
  61.         len = 40;
  62.  
  63.     for(i = 0; i < len; (*buff)++, i++)
  64.     {
  65.         hash = (hash * seed) + (*buff);
  66.     }
  67.     return hash;
  68. }
  69.  
  70. void help_() {
  71.     printf("--hash password : hashes a password with the BKDR hash, eg: ./psst --hash qwerty\n");
  72.     printf("--password len : generates a random password, eg: ./psst --password 10\n");
  73.     printf("--help : prints this information\n");
  74. }
  75.  
  76.  
  77. void handle(int argc, char** argv) {
  78.     char line[124];
  79.     int option = 0;
  80.     int long_index = 0;
  81.     int password = -1;
  82.     char *endptr;
  83.     int hv=-1, pv=-1, tv=-1;
  84.  
  85.     static struct option long_options[] = {
  86.         {"help",      no_argument,       0,  'h' },
  87.         {"password",  required_argument, 0,  'p' },
  88.         {"hash",      required_argument, 0,  't' },
  89.         {0,           0,                 0,  0   }
  90.     };
  91.  
  92.     while((option = getopt_long(argc, argv, "hp:t:", long_options, &long_index)) != -1) {
  93.         switch (option) {
  94.             case 'h' :
  95.                 hv=0;
  96.                 break;
  97.             case 'p' :
  98.                 password = strtol(optarg, &endptr, 10);
  99.                 if ((errno == ERANGE && (password == LONG_MAX || password == LONG_MIN)) || (errno != 0 && password == 0)) {
  100.                     printf("error\n");
  101.                     exit(EXIT_FAILURE);
  102.                 }
  103.  
  104.                 if (endptr == optarg) {
  105.                     printf("error\n");
  106.                     exit(EXIT_FAILURE);
  107.                 }
  108.                 if (password>1337) {
  109.                     printf("you are too paranoid for me\n");
  110.                     exit(EXIT_FAILURE);
  111.                 }  
  112.                 pv=0;
  113.                 break;
  114.             case 't' :
  115.                 strncpy(line, optarg, sizeof(line));
  116.                 tv=0;
  117.                 break;
  118.             default:
  119.                 printf("invalid command\n");
  120.                 exit(EXIT_FAILURE);
  121.         }
  122.     }
  123.  
  124.     if (!hv) {
  125.         welcome();
  126.         help_();
  127.     } else if(!pv) {
  128.         char *s = malloc(sizeof(char)*password);
  129.         if(s!= NULL) {
  130.             randompass(s,password);
  131.             printf("%s\n", s);
  132.             free(s);
  133.         } else {
  134.             printf("internal error");
  135.         }
  136.     } else if(!tv) {
  137.         printf("%d\n",BKDRHash(line));
  138.     } else {
  139.         welcome();
  140.         help_();
  141.     }
  142.  
  143. }
  144.  
  145. int main(int argc, char** argv){
  146.     srand(time(NULL));
  147.     handle(argc, argv);
  148.     return EXIT_SUCCESS;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement