Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ZmEu v0.1 encoder ;p
- gcc unu.c -o unu
- (@)Encrypt:
- ./unu e michael_jackson.mp3 > michael_jackson.unu
- (@)Decrypt:
- ./unu d michael_jackson.unu
- #blackhats @ haqnet
- */
- #include <stdio.h>
- #include <ctype.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <libgen.h>
- #include <netinet/in.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- #define BLACKHATS 16
- #define COLUMNS 80
- #define MAGIE 0x1DEADFED
- int longestunu;
- typedef struct {
- const char *string;
- unsigned int value;
- unsigned int length;
- } UNU;
- UNU unu[BLACKHATS] = {
- {"#", 0}, {"ZMEU~", 0}, {"!@$%", 0}, {"^&*", 0},
- {"()_+", 0}, {"[];", 0}, {",./?|=", 0}, {"0123", 0},
- {"456", 0}, {"789ab", 0}, {"cdef", 0}, {"ghi", 0},
- {"jklm", 0}, {"nop", 0}, {"rstu", 0}, {"UNU", 0}
- };
- void unu_init();
- int unu_compare(const void *, const void *);
- int unutoi(const char *);
- const UNU *itounu(unsigned int);
- void unu_encode(const char *);
- int unu_encode_int(int, FILE *);
- int unu_encode_string(const char *, FILE *);
- void unu_decode(const char *);
- int unu_decode_int(int *, FILE *);
- char * unu_decode_string(FILE *);
- int unu_decode_file(FILE *);
- int unuputc(int, FILE *);
- int unugetc(FILE *);
- void usage(const char *);
- int main(int argc, char **argv) {
- unu_init();
- int i;
- int mode;
- if (argc < 3) {
- usage(argv[0]);
- exit(EXIT_FAILURE);
- }
- if (strcmp(argv[1], "e") == 0) {
- mode = 'e';
- } else if (strcmp(argv[1], "d") == 0) {
- mode = 'd';
- } else {
- mode = -1;
- }
- i = 2;
- while (i < argc) {
- switch (mode) {
- case 'e':
- unu_encode(argv[i]);
- break;
- case 'd':
- unu_decode(argv[i]);
- break;
- }
- i++;
- }
- exit(EXIT_SUCCESS);
- }
- void usage(const char *arg) {
- printf("(@)Syntax(%s): [ed] filename1 [filename2 ... filenameN]\n", arg);
- }
- int unu_encode_int(int length, FILE * stream) {
- uint32_t netlen;
- unsigned char byte[sizeof(uint32_t)];
- int i;
- netlen = htonl(length);
- memcpy(byte, &netlen, sizeof(uint32_t));
- i = 0;
- while (i < sizeof(uint32_t)) {
- if (unuputc(byte[i], stream) == EOF) {
- return EOF;
- }
- i++;
- }
- return 0;
- }
- int unu_encode_string(const char *string, FILE * stream) {
- int length;
- int i;
- length = strlen(string);
- if (unu_encode_int(length, stream) == EOF) {
- return EOF;
- }
- i = 0;
- while (i < length) {
- if (unuputc(string[i], stream) == EOF) {
- return EOF;
- }
- i++;
- }
- return 0;
- }
- void unu_encode(const char *filename) {
- struct stat statinfo;
- FILE *f;
- int length;
- int c;
- char *base;
- char *ptr;
- fprintf(stderr, "(@)Encode: %s\n", filename);
- f = fopen(filename, "r");
- if (f == NULL) {
- fprintf(stderr, "(@)Failed to open %s: %s\n", filename, strerror(errno));
- exit(EXIT_FAILURE);
- }
- fseek(f, 0, SEEK_END);
- length = ftell(f);
- rewind(f);
- if (stat(filename, &statinfo) == -1) {
- fprintf(stderr, "(@)Stat failed on %s: %s\n", filename, strerror(errno));
- exit(EXIT_FAILURE);
- }
- ptr = strdup(filename);
- base = basename(ptr);
- unu_encode_int(MAGIE, stdout);
- unu_encode_string(base, stdout);
- unu_encode_int(statinfo.st_mode & 0777, stdout);
- unu_encode_int(length, stdout);
- free(ptr);
- while ((c = fgetc(f)) != EOF) {
- unuputc(c, stdout);
- }
- fclose(f);
- }
- char * unu_decode_string(FILE * stream) {
- int length;
- char *string;
- int i;
- int c;
- if (unu_decode_int(&length, stream) == EOF) {
- return NULL;
- }
- string = malloc(length + 1);
- i = 0;
- while (i < length) {
- c = unugetc(stream);
- if (c == EOF) {
- free(string);
- return NULL;
- }
- string[i] = c;
- i++;
- }
- string[i] = '\0';
- return (string);
- }
- int unu_decode_file(FILE * stream) {
- struct stat statinfo;
- FILE *f;
- char *filename;
- int magic;
- int length;
- int i;
- int c;
- int answer;
- char line[80];
- int perms;
- if (unu_decode_int(&magic, stream) == EOF) {
- return EOF;
- }
- if (magic != MAGIE) {
- fputs("(@)Invalid magic number\n", stderr);
- exit(EXIT_FAILURE);
- }
- filename = unu_decode_string(stream);
- if (filename == NULL) {
- fputs("(@)No filename found\n", stderr);
- exit(EXIT_FAILURE);
- }
- if (unu_decode_int(&perms, stream) == EOF) {
- fputs("(@)Cannot read file permissions\n", stderr);
- exit(EXIT_FAILURE);
- }
- printf("(@)Extract: %s\n", filename);
- if (unu_decode_int(&length, stream) == EOF) {
- fprintf(stderr, "(@)Couldn't determine length of %s\n", filename);
- exit(EXIT_FAILURE);
- }
- answer = 'Y';
- if (stat(filename, &statinfo) == 0) {
- do {
- printf("(@)%s already exists, overwrite? (Y/N) ", filename);
- fgets(line, sizeof(line) - 1, stdin);
- answer = toupper(line[0]);
- }
- while (answer != 'Y' && answer != 'N');
- }
- if (answer == 'Y') {
- f = fopen(filename, "w");
- if (f == NULL) {
- fprintf(stderr, "(@)Failed to open %s: %s\n", filename, strerror(errno));
- exit(EXIT_FAILURE);
- }
- } else if (answer == 'N') {
- f = NULL;
- printf("(@)Skipping %s\n", filename);
- }
- i = 0;
- while (i < length) {
- c = unugetc(stream);
- if (c == EOF) {
- perror("(@)Unexpected read error");
- exit(EXIT_FAILURE);
- }
- if (answer == 'Y') {
- if (fputc(c, f) == EOF) {
- perror("(@)Unexpected write error");
- exit(EXIT_FAILURE);
- }
- }
- i++;
- }
- if (answer == 'Y') {
- fclose(f);
- chmod(filename, perms);
- }
- free(filename);
- return 0;
- }
- void unu_decode(const char *filename) {
- FILE *f;
- printf("(@)Decode: %s\n", filename);
- f = fopen(filename, "r");
- while (unu_decode_file(f) != EOF) {
- }
- fclose(f);
- }
- int unu_compare(const void *a, const void *b) {
- const UNU *unu1 = (const UNU *)a;
- const UNU *unu2 = (const UNU *)b;
- return strcmp(unu2->string, unu1->string);
- }
- void unu_init() {
- unsigned int i;
- qsort(unu, BLACKHATS, sizeof(UNU), unu_compare);
- longestunu = 0;
- i = 0;
- while (i < BLACKHATS) {
- unu[i].value = i;
- unu[i].length = strlen(unu[i].string);
- if (longestunu < unu[i].length) {
- longestunu = unu[i].length;
- }
- i++;
- }
- }
- unsigned int column = 0;
- int unugetnibble(FILE * stream) {
- char *someunu;
- char buf[2];
- int length;
- int value;
- int c;
- someunu = malloc(longestunu + 1);
- someunu[0] = '\0';
- memset(buf, 0, sizeof(buf));
- length = 0;
- do {
- c = fgetc(stream);
- if (isspace(c)) {
- continue;
- }
- if (c == EOF) {
- return -1;
- }
- buf[0] = c;
- strcat(someunu, buf);
- value = unutoi(someunu);
- if (value != -1) {
- free(someunu);
- return value;
- }
- }
- while (length < longestunu);
- free(someunu);
- return -1;
- }
- int unugetc(FILE * stream) {
- int hi, lo;
- unsigned char c;
- hi = unugetnibble(stream);
- if (hi == -1) {
- return EOF;
- }
- lo = unugetnibble(stream);
- if (lo == -1) {
- return EOF;
- }
- c = (hi << 4) | lo;
- return c;
- }
- int unu_decode_int(int *ptr, FILE * stream) {
- unsigned char byte[sizeof(uint32_t)];
- uint32_t netlen;
- int i;
- int c;
- i = 0;
- while (i < sizeof(uint32_t)) {
- c = unugetc(stream);
- if (c == EOF) {
- return EOF;
- }
- byte[i] = c;
- i++;
- }
- memcpy(&netlen, byte, sizeof(uint32_t));
- *ptr = ntohl(netlen);
- return 0;
- }
- int unuputc(int c, FILE * stream) {
- unsigned char ch;
- const UNU *dptr;
- ch = (unsigned char)c;
- dptr = itounu((ch >> 4) & 15);
- column += dptr->length;
- if (column > COLUMNS) {
- fputc('\n', stream);
- column = dptr->length;
- }
- if (fputs(dptr->string, stream) == EOF) {
- return EOF;
- }
- dptr = itounu(ch & 15);
- column += dptr->length;
- if (column > COLUMNS) {
- fputc('\n', stream);
- column = dptr->length;
- }
- if (fputs(dptr->string, stream) == EOF) {
- return EOF;
- }
- return 0;
- }
- const UNU * itounu(unsigned int i) {
- if (i >= BLACKHATS) {
- return NULL;
- }
- return &unu[i];
- }
- int unutoi(const char *string) {
- UNU key;
- UNU *found;
- if (string == NULL) {
- return -1;
- }
- key.string = string;
- found = bsearch(&key, unu, BLACKHATS, sizeof(UNU), unu_compare);
- if (found == NULL) {
- return (-1);
- }
- return found->value;
- }
Advertisement
Add Comment
Please, Sign In to add comment