ZmEu

ZmEu encoder ;p;p

Mar 13th, 2011
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3.  
  4. /*
  5.  
  6. ZmEu v0.1 encoder ;p
  7.  
  8. gcc unu.c -o unu
  9.  
  10. (@)Encrypt:
  11.  
  12. ./unu e michael_jackson.mp3 > michael_jackson.unu
  13.  
  14. (@)Decrypt:
  15.  
  16. ./unu d michael_jackson.unu
  17.  
  18. #blackhats @ haqnet
  19.  
  20. */
  21.  
  22.  
  23.  
  24. #include <stdio.h>
  25.  
  26. #include <ctype.h>
  27.  
  28. #include <fcntl.h>
  29.  
  30. #include <stdlib.h>
  31.  
  32. #include <string.h>
  33.  
  34. #include <unistd.h>
  35.  
  36. #include <libgen.h>
  37.  
  38. #include <netinet/in.h>
  39.  
  40. #include <sys/types.h>
  41.  
  42. #include <sys/stat.h>
  43.  
  44. #include <errno.h>
  45.  
  46.  
  47.  
  48. #define BLACKHATS 16
  49.  
  50. #define COLUMNS  80
  51.  
  52. #define MAGIE 0x1DEADFED
  53.  
  54.  
  55.  
  56. int longestunu;
  57.  
  58.  
  59.  
  60. typedef struct {
  61.  
  62.   const char *string;
  63.  
  64.   unsigned int value;
  65.  
  66.   unsigned int length;
  67.  
  68. } UNU;
  69.  
  70.  
  71.  
  72. UNU unu[BLACKHATS] = {
  73.  
  74.   {"#", 0}, {"ZMEU~", 0}, {"!@$%", 0}, {"^&*", 0},
  75.  
  76.  {"()_+", 0}, {"[];", 0}, {",./?|=", 0}, {"0123", 0},
  77.  
  78.   {"456", 0}, {"789ab", 0}, {"cdef", 0}, {"ghi", 0},
  79.  
  80.   {"jklm", 0}, {"nop", 0}, {"rstu", 0}, {"UNU", 0}
  81.  
  82. };
  83.  
  84.  
  85.  
  86. void unu_init();
  87.  
  88. int unu_compare(const void *, const void *);
  89.  
  90.  
  91.  
  92. int unutoi(const char *);
  93.  
  94. const UNU *itounu(unsigned int);
  95.  
  96.  
  97.  
  98. void unu_encode(const char *);
  99.  
  100. int unu_encode_int(int, FILE *);
  101.  
  102. int unu_encode_string(const char *, FILE *);
  103.  
  104.  
  105.  
  106. void unu_decode(const char *);
  107.  
  108. int unu_decode_int(int *, FILE *);
  109.  
  110. char * unu_decode_string(FILE *);
  111.  
  112. int unu_decode_file(FILE *);
  113.  
  114.  
  115.  
  116. int unuputc(int, FILE *);
  117.  
  118. int unugetc(FILE *);
  119.  
  120.  
  121.  
  122. void usage(const char *);
  123.  
  124.  
  125.  
  126. int main(int argc, char **argv) {
  127.  
  128.  
  129.  
  130.   unu_init();
  131.  
  132.  
  133.  
  134.   int i;
  135.  
  136.   int mode;
  137.  
  138.  
  139.  
  140.   if (argc < 3) {
  141.  
  142.    usage(argv[0]);
  143.  
  144.    exit(EXIT_FAILURE);
  145.  
  146.  }
  147.  
  148.  
  149.  
  150.  if (strcmp(argv[1], "e") == 0) {
  151.  
  152.    mode = 'e';
  153.  
  154.  } else if (strcmp(argv[1], "d") == 0) {
  155.  
  156.    mode = 'd';
  157.  
  158.  } else {
  159.  
  160.    mode = -1;
  161.  
  162.  }
  163.  
  164.  
  165.  
  166.  i = 2;
  167.  
  168.  
  169.  
  170.  while (i < argc) {
  171.  
  172.    switch (mode) {
  173.  
  174.    case 'e':
  175.  
  176.      unu_encode(argv[i]);
  177.  
  178.      break;
  179.  
  180.    case 'd':
  181.  
  182.      unu_decode(argv[i]);
  183.  
  184.      break;
  185.  
  186.    }
  187.  
  188.  
  189.  
  190.    i++;
  191.  
  192.  }
  193.  
  194.  
  195.  
  196.  exit(EXIT_SUCCESS);
  197.  
  198. }
  199.  
  200.  
  201.  
  202. void usage(const char *arg) {
  203.  
  204.  printf("(@)Syntax(%s): [ed] filename1 [filename2 ... filenameN]\n", arg);
  205.  
  206. }
  207.  
  208.  
  209.  
  210. int unu_encode_int(int length, FILE * stream) {
  211.  
  212.  
  213.  
  214.  uint32_t netlen;
  215.  
  216.  unsigned char byte[sizeof(uint32_t)];
  217.  
  218.  int i;
  219.  
  220.  
  221.  
  222.  netlen = htonl(length);
  223.  
  224.  
  225.  
  226.  memcpy(byte, &netlen, sizeof(uint32_t));
  227.  
  228.  
  229.  
  230.  i = 0;
  231.  
  232.  while (i < sizeof(uint32_t)) {
  233.  
  234.    if (unuputc(byte[i], stream) == EOF) {
  235.  
  236.      return EOF;
  237.  
  238.    }
  239.  
  240.    i++;
  241.  
  242.  }
  243.  
  244.  
  245.  
  246.  return 0;
  247.  
  248. }
  249.  
  250.  
  251.  
  252. int unu_encode_string(const char *string, FILE * stream) {
  253.  
  254.    
  255.  
  256.  int length;
  257.  
  258.  int i;
  259.  
  260.  
  261.  
  262.  length = strlen(string);
  263.  
  264.  
  265.  
  266.  if (unu_encode_int(length, stream) == EOF) {
  267.  
  268.    return EOF;
  269.  
  270.  }
  271.  
  272.  
  273.  
  274.  i = 0;
  275.  
  276.  while (i < length) {
  277.  
  278.    if (unuputc(string[i], stream) == EOF) {
  279.  
  280.      return EOF;
  281.  
  282.    }
  283.  
  284.    i++;
  285.  
  286.  }
  287.  
  288.  
  289.  
  290.  return 0;
  291.  
  292. }
  293.  
  294.  
  295.  
  296. void unu_encode(const char *filename) {
  297.  
  298.  
  299.  
  300.  struct stat statinfo;
  301.  
  302.  FILE *f;
  303.  
  304.  int length;
  305.  
  306.  int c;
  307.  
  308.  char *base;
  309.  
  310.  char *ptr;
  311.  
  312.  
  313.  
  314.  fprintf(stderr, "(@)Encode: %s\n", filename);
  315.  
  316.  
  317.  
  318.  f = fopen(filename, "r");
  319.  
  320.  if (f == NULL) {
  321.  
  322.    fprintf(stderr, "(@)Failed to open %s: %s\n", filename, strerror(errno));
  323.  
  324.    exit(EXIT_FAILURE);
  325.  
  326.  }
  327.  
  328.  
  329.  
  330.  fseek(f, 0, SEEK_END);
  331.  
  332.  length = ftell(f);
  333.  
  334.  rewind(f);
  335.  
  336.  
  337.  
  338.  if (stat(filename, &statinfo) == -1) {
  339.  
  340.    fprintf(stderr, "(@)Stat failed on %s: %s\n", filename, strerror(errno));
  341.  
  342.    exit(EXIT_FAILURE);
  343.  
  344.  }
  345.  
  346.  
  347.  
  348.  ptr = strdup(filename);
  349.  
  350.  base = basename(ptr);
  351.  
  352.  
  353.  
  354.  unu_encode_int(MAGIE, stdout);
  355.  
  356.  unu_encode_string(base, stdout);
  357.  
  358.  unu_encode_int(statinfo.st_mode & 0777, stdout);
  359.  
  360.  unu_encode_int(length, stdout);
  361.  
  362.  
  363.  
  364.  free(ptr);
  365.  
  366.  
  367.  
  368.  while ((c = fgetc(f)) != EOF) {
  369.  
  370.    unuputc(c, stdout);
  371.  
  372.  }
  373.  
  374.  
  375.  
  376.  fclose(f);
  377.  
  378. }
  379.  
  380.  
  381.  
  382. char * unu_decode_string(FILE * stream) {
  383.  
  384.  
  385.  
  386.  int length;
  387.  
  388.  char *string;
  389.  
  390.  int i;
  391.  
  392.  int c;
  393.  
  394.  
  395.  
  396.  if (unu_decode_int(&length, stream) == EOF) {
  397.  
  398.    return NULL;
  399.  
  400.  }
  401.  
  402.  
  403.  
  404.  string = malloc(length + 1);
  405.  
  406.  
  407.  
  408.  i = 0;
  409.  
  410.  while (i < length) {
  411.  
  412.    c = unugetc(stream);
  413.  
  414.    if (c == EOF) {
  415.  
  416.      free(string);
  417.  
  418.      return NULL;
  419.  
  420.    }
  421.  
  422.    string[i] = c;
  423.  
  424.    i++;
  425.  
  426.  }
  427.  
  428.  
  429.  
  430.  string[i] = '\0';
  431.  
  432.  
  433.  
  434.  return (string);
  435.  
  436. }
  437.  
  438.  
  439.  
  440. int unu_decode_file(FILE * stream) {
  441.  
  442.  
  443.  
  444.  struct stat statinfo;
  445.  
  446.  FILE *f;
  447.  
  448.  char *filename;
  449.  
  450.  int magic;
  451.  
  452.  int length;
  453.  
  454.  int i;
  455.  
  456.  int c;
  457.  
  458.  int answer;
  459.  
  460.  char line[80];
  461.  
  462.  int perms;
  463.  
  464.  
  465.  
  466.  if (unu_decode_int(&magic, stream) == EOF) {
  467.  
  468.    return EOF;
  469.  
  470.  }
  471.  
  472.  
  473.  
  474.  if (magic != MAGIE) {
  475.  
  476.    fputs("(@)Invalid magic number\n", stderr);
  477.  
  478.    exit(EXIT_FAILURE);
  479.  
  480.  }
  481.  
  482.  
  483.  
  484.  filename = unu_decode_string(stream);
  485.  
  486.  
  487.  
  488.  if (filename == NULL) {
  489.  
  490.    fputs("(@)No filename found\n", stderr);
  491.  
  492.    exit(EXIT_FAILURE);
  493.  
  494.  }
  495.  
  496.  
  497.  
  498.  if (unu_decode_int(&perms, stream) == EOF) {
  499.  
  500.    fputs("(@)Cannot read file permissions\n", stderr);
  501.  
  502.    exit(EXIT_FAILURE);
  503.  
  504.  }
  505.  
  506.  
  507.  
  508.  printf("(@)Extract: %s\n", filename);
  509.  
  510.  
  511.  
  512.  if (unu_decode_int(&length, stream) == EOF) {
  513.  
  514.    fprintf(stderr, "(@)Couldn't determine length of %s\n", filename);
  515.  
  516.    exit(EXIT_FAILURE);
  517.  
  518.  }
  519.  
  520.  
  521.  
  522.  answer = 'Y';
  523.  
  524.  
  525.  
  526.  if (stat(filename, &statinfo) == 0) {
  527.  
  528.    do {
  529.  
  530.      printf("(@)%s already exists, overwrite? (Y/N) ", filename);
  531.  
  532.      fgets(line, sizeof(line) - 1, stdin);
  533.  
  534.      answer = toupper(line[0]);
  535.  
  536.    }
  537.  
  538.    while (answer != 'Y' && answer != 'N');
  539.  
  540.  
  541.  
  542.  }
  543.  
  544.  
  545.  
  546.  if (answer == 'Y') {
  547.  
  548.    f = fopen(filename, "w");
  549.  
  550.    if (f == NULL) {
  551.  
  552.      fprintf(stderr, "(@)Failed to open %s: %s\n", filename, strerror(errno));
  553.  
  554.      exit(EXIT_FAILURE);
  555.  
  556.    }
  557.  
  558.  } else if (answer == 'N') {
  559.  
  560.    f = NULL;
  561.  
  562.    printf("(@)Skipping %s\n", filename);
  563.  
  564.  }
  565.  
  566.  
  567.  
  568.  i = 0;
  569.  
  570.  while (i < length) {
  571.  
  572.    c = unugetc(stream);
  573.  
  574.  
  575.  
  576.    if (c == EOF) {
  577.  
  578.      perror("(@)Unexpected read error");
  579.  
  580.      exit(EXIT_FAILURE);
  581.  
  582.    }
  583.  
  584.  
  585.  
  586.    if (answer == 'Y') {
  587.  
  588.      if (fputc(c, f) == EOF) {
  589.  
  590.        perror("(@)Unexpected write error");
  591.  
  592.        exit(EXIT_FAILURE);
  593.  
  594.      }
  595.  
  596.    }
  597.  
  598.  
  599.  
  600.    i++;
  601.  
  602.  }
  603.  
  604.  
  605.  
  606.  if (answer == 'Y') {
  607.  
  608.    fclose(f);
  609.  
  610.    chmod(filename, perms);
  611.  
  612.  }
  613.  
  614.  
  615.  
  616.  free(filename);
  617.  
  618.  
  619.  
  620.  return 0;
  621.  
  622. }
  623.  
  624.  
  625.  
  626. void unu_decode(const char *filename) {
  627.  
  628.  
  629.  
  630.  FILE *f;
  631.  
  632.  
  633.  
  634.  printf("(@)Decode: %s\n", filename);
  635.  
  636.  
  637.  
  638.  f = fopen(filename, "r");
  639.  
  640.  
  641.  
  642.  while (unu_decode_file(f) != EOF) {
  643.  
  644.  }
  645.  
  646.  
  647.  
  648.  fclose(f);
  649.  
  650. }
  651.  
  652.  
  653.  
  654. int unu_compare(const void *a, const void *b) {
  655.  
  656.  
  657.  
  658.  const UNU *unu1 = (const UNU *)a;
  659.  
  660.  const UNU *unu2 = (const UNU *)b;
  661.  
  662.  return strcmp(unu2->string, unu1->string);
  663.  
  664. }
  665.  
  666.  
  667.  
  668. void unu_init() {
  669.  
  670.  
  671.  
  672.   unsigned int i;
  673.  
  674.   qsort(unu, BLACKHATS, sizeof(UNU), unu_compare);
  675.  
  676.  
  677.  
  678.   longestunu = 0;
  679.  
  680.   i = 0;
  681.  
  682.   while (i < BLACKHATS) {
  683.  
  684.    unu[i].value = i;
  685.  
  686.    unu[i].length = strlen(unu[i].string);
  687.  
  688.    if (longestunu < unu[i].length) {
  689.  
  690.      longestunu = unu[i].length;
  691.  
  692.    }
  693.  
  694.    i++;
  695.  
  696.  }
  697.  
  698. }
  699.  
  700.  
  701.  
  702. unsigned int column = 0;
  703.  
  704.  
  705.  
  706. int unugetnibble(FILE * stream) {
  707.  
  708.  
  709.  
  710.  char *someunu;
  711.  
  712.  char buf[2];
  713.  
  714.  
  715.  
  716.  int length;
  717.  
  718.  int value;
  719.  
  720.  int c;
  721.  
  722.  
  723.  
  724.  someunu = malloc(longestunu + 1);
  725.  
  726.  
  727.  
  728.  someunu[0] = '\0';
  729.  
  730.  memset(buf, 0, sizeof(buf));
  731.  
  732.  
  733.  
  734.  length = 0;
  735.  
  736.  
  737.  
  738.  do {
  739.  
  740.    c = fgetc(stream);
  741.  
  742.  
  743.  
  744.    if (isspace(c)) {
  745.  
  746.      continue;
  747.  
  748.    }
  749.  
  750.  
  751.  
  752.    if (c == EOF) {
  753.  
  754.      return -1;
  755.  
  756.    }
  757.  
  758.  
  759.  
  760.    buf[0] = c;
  761.  
  762.  
  763.  
  764.    strcat(someunu, buf);
  765.  
  766.  
  767.  
  768.    value = unutoi(someunu);
  769.  
  770.  
  771.  
  772.    if (value != -1) {
  773.  
  774.      free(someunu);
  775.  
  776.      return value;
  777.  
  778.    }
  779.  
  780.  
  781.  
  782.  }
  783.  
  784.  while (length < longestunu);
  785.  
  786.  
  787.  
  788.  free(someunu);
  789.  
  790.  return -1;
  791.  
  792. }
  793.  
  794.  
  795.  
  796. int unugetc(FILE * stream) {
  797.  
  798.  
  799.  
  800.  int hi, lo;
  801.  
  802.  unsigned char c;
  803.  
  804.  
  805.  
  806.  hi = unugetnibble(stream);
  807.  
  808.  if (hi == -1) {
  809.  
  810.    return EOF;
  811.  
  812.  }
  813.  
  814.  
  815.  
  816.  lo = unugetnibble(stream);
  817.  
  818.  if (lo == -1) {
  819.  
  820.    return EOF;
  821.  
  822.  }
  823.  
  824.  
  825.  
  826.  c = (hi << 4) | lo;
  827.  
  828.  
  829.  
  830.  return c;
  831.  
  832. }
  833.  
  834.  
  835.  
  836. int unu_decode_int(int *ptr, FILE * stream) {
  837.  
  838.  
  839.  
  840.  unsigned char byte[sizeof(uint32_t)];
  841.  
  842.  uint32_t netlen;
  843.  
  844.  
  845.  
  846.  int i;
  847.  
  848.  int c;
  849.  
  850.  
  851.  
  852.  i = 0;
  853.  
  854.  
  855.  
  856.  while (i < sizeof(uint32_t)) {
  857.  
  858.    c = unugetc(stream);
  859.  
  860.    if (c == EOF) {
  861.  
  862.      return EOF;
  863.  
  864.    }
  865.  
  866.  
  867.  
  868.    byte[i] = c;
  869.  
  870.  
  871.  
  872.    i++;
  873.  
  874.  }
  875.  
  876.  
  877.  
  878.  memcpy(&netlen, byte, sizeof(uint32_t));
  879.  
  880.  
  881.  
  882.  *ptr = ntohl(netlen);
  883.  
  884.  
  885.  
  886.  return 0;
  887.  
  888. }
  889.  
  890.  
  891.  
  892. int unuputc(int c, FILE * stream) {
  893.  
  894.  
  895.  
  896.  unsigned char ch;
  897.  
  898.  const UNU *dptr;
  899.  
  900.  
  901.  
  902.  ch = (unsigned char)c;
  903.  
  904.  
  905.  
  906.  dptr = itounu((ch >> 4) & 15);
  907.  
  908.  
  909.  
  910.   column += dptr->length;
  911.  
  912.  
  913.  
  914.   if (column > COLUMNS) {
  915.  
  916.     fputc('\n', stream);
  917.  
  918.     column = dptr->length;
  919.  
  920.   }
  921.  
  922.  
  923.  
  924.   if (fputs(dptr->string, stream) == EOF) {
  925.  
  926.     return EOF;
  927.  
  928.   }
  929.  
  930.  
  931.  
  932.   dptr = itounu(ch & 15);
  933.  
  934.  
  935.  
  936.   column += dptr->length;
  937.  
  938.  
  939.  
  940.   if (column > COLUMNS) {
  941.  
  942.     fputc('\n', stream);
  943.  
  944.     column = dptr->length;
  945.  
  946.   }
  947.  
  948.  
  949.  
  950.   if (fputs(dptr->string, stream) == EOF) {
  951.  
  952.     return EOF;
  953.  
  954.   }
  955.  
  956.  
  957.  
  958.   return 0;
  959.  
  960. }
  961.  
  962.  
  963.  
  964. const UNU * itounu(unsigned int i) {
  965.  
  966.  
  967.  
  968.   if (i >= BLACKHATS) {
  969.  
  970.     return NULL;
  971.  
  972.   }
  973.  
  974.  
  975.  
  976.   return &unu[i];
  977.  
  978. }
  979.  
  980.  
  981.  
  982. int unutoi(const char *string) {
  983.  
  984.  
  985.  
  986.   UNU key;
  987.  
  988.   UNU *found;
  989.  
  990.  
  991.  
  992.   if (string == NULL) {
  993.  
  994.     return -1;
  995.  
  996.   }
  997.  
  998.  
  999.  
  1000.   key.string = string;
  1001.  
  1002.  
  1003.  
  1004.   found = bsearch(&key, unu, BLACKHATS, sizeof(UNU), unu_compare);
  1005.  
  1006.  
  1007.  
  1008.   if (found == NULL) {
  1009.  
  1010.     return (-1);
  1011.  
  1012.   }
  1013.  
  1014.  
  1015.  
  1016.   return found->value;
  1017.  
  1018. }
Advertisement
Add Comment
Please, Sign In to add comment