diff --git a/Makefile b/Makefile index 4d7f488..7ae08f5 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,9 @@ +INCLUDES = include +SOURCES = src +OUTPUT = main +CFLAGS=-g -o main -Wall -I$(INCLUDES) + +CFILES := $(foreach dir,$(SOURCES),$(wildcard $(dir)/*.c)) + all: - gcc -g -o main main.c + gcc main.c $(CFILES) $(CFLAGS) diff --git a/include/colors.h b/include/colors.h new file mode 100644 index 0000000..c69a23e --- /dev/null +++ b/include/colors.h @@ -0,0 +1,7 @@ +#define ANSI_COLOR_RED "\x1b[31m" +#define ANSI_COLOR_GREEN "\x1b[32m" +#define ANSI_COLOR_YELLOW "\x1b[33m" +#define ANSI_COLOR_BLUE "\x1b[34m" +#define ANSI_COLOR_MAGENTA "\x1b[35m" +#define ANSI_COLOR_CYAN "\x1b[36m" +#define ANSI_COLOR_RESET "\x1b[0m" diff --git a/include/getSize.h b/include/getSize.h new file mode 100644 index 0000000..b0fd1f7 --- /dev/null +++ b/include/getSize.h @@ -0,0 +1,3 @@ +#include + +void getSize(FILE *file, int argc, char *argv[]); diff --git a/include/readFile.h b/include/readFile.h new file mode 100644 index 0000000..c3b76de --- /dev/null +++ b/include/readFile.h @@ -0,0 +1,12 @@ +#include +#include "colors.h" + +#pragma once + +typedef struct { + int start; + int end; + const char* color; +} ColorRange; + +void readFile(FILE *file); diff --git a/main.c b/main.c index a9e998e..5c45997 100644 --- a/main.c +++ b/main.c @@ -1,96 +1,59 @@ +#include #include #include #include #include -#include -#define ANSI_COLOR_RED "\x1b[31m" -#define ANSI_COLOR_GREEN "\x1b[32m" -#define ANSI_COLOR_YELLOW "\x1b[33m" -#define ANSI_COLOR_BLUE "\x1b[34m" -#define ANSI_COLOR_MAGENTA "\x1b[35m" -#define ANSI_COLOR_CYAN "\x1b[36m" -#define ANSI_COLOR_RESET "\x1b[0m" +#include "readFile.h" +#include "getSize.h" -void read_file(FILE *file) { - int count = 0; - unsigned char buffer[1000000]; - size_t bytesRead; - while ((bytesRead = fread(buffer, 1, sizeof(buffer), file)) > 0) { - for (size_t i = 0; i < bytesRead; i++) { - printf("%02x ", buffer[i]); - if(++count % 16 == 0) { - puts(""); - } - } - } -} +int main(int argc, char *argv[]) { + short c; +/* if (argc <= 1) { + printf("Usage:\n %s [OPTION]... [FILE]\n\n", argv[0]); + puts("Tool to modify Mifare Classic dump files"); + puts("\nOptions:"); + puts("\t-s display the size of the file in bytes\n"); + exit(EXIT_SUCCESS); + } + */ -void get_size(FILE *file, int argc, char *argv[]) { - fseek(file, 0L, SEEK_END); - long bytes = ftell(file); - fseek(file, 0L, SEEK_SET); - if (bytes == 1024) { - printf("Bytes of %s: %ld\n", argv[argc-1], bytes); - puts("This looks like a Mifare Classic 1K dump file"); - } else if (bytes == 4096) { - printf("Bytes of %s: %ld\n", argv[argc-1], bytes); - puts("This lokks like a Mifare Classic 4K dump file"); - } else { - printf("Bytes of %s: %ld\n", argv[argc-1], bytes); - printf(ANSI_COLOR_RED "This doesn't look like a Mifare Classic 1K or 4K at all\n" ANSI_COLOR_RESET); - } -} - -int main (int argc, char *argv[]) { - if (argc <= 1) - { - printf("Usage:\n %s [OPTION]... [FILE]\n\n", argv[0]); - puts("Tool to modify Mifare Classic dump files"); - puts("\nOptions:"); - puts("\t-s display the size of the file in bytes\n"); - exit(EXIT_SUCCESS); - } - - struct option longopts[] = { - {"size", no_argument, NULL, 's'}, - {"help", no_argument, NULL, 'h'}, - {"version", no_argument, NULL, 'V'}, - {NULL, no_argument, NULL, 0} - }; - - FILE *file; - unsigned char ch; + FILE *file; char *binfile = malloc(64 * sizeof(char)); - if ( binfile == NULL ) { + if (binfile == NULL) { puts("Failed to allocate memory"); exit(EXIT_FAILURE); } - file = fopen(argv[argc-1], "rb"); - if( file == NULL ) - { + file = fopen(argv[argc - 1], "rb"); + if (file == NULL) { perror("Error while opening the file"); exit(EXIT_FAILURE); } + static const struct option long_opts[] = { + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; - int args; - while ((args = getopt_long(argc, argv, "s", longopts, NULL)) != -1) { - switch (args) { - case 's': - { - get_size(file, argc, argv); - } - default: - exit(EXIT_SUCCESS); + while ((c = getopt_long(argc, argv, "hVs", + long_opts, NULL)) != -1) { + switch (c) { + case 'h': + case 'V': + case 's': + getSize(file, argc, argv); + exit(EXIT_SUCCESS); + default: + printf("Try '%s --help' for more information.\n", argv[0]); + exit(EXIT_FAILURE); } } - read_file(file); + readFile(file); fclose(file); free(binfile); exit(EXIT_SUCCESS); - } diff --git a/src/getSize.c b/src/getSize.c new file mode 100644 index 0000000..cfd274e --- /dev/null +++ b/src/getSize.c @@ -0,0 +1,20 @@ +#include + +#include "colors.h" + +void getSize(FILE *file, int argc, char *argv[]) { + fseek(file, 0L, SEEK_END); + long bytes = ftell(file); + fseek(file, 0L, SEEK_SET); + if (bytes == 1024) { + printf("Bytes of %s: %ld\n", argv[argc - 1], bytes); + puts("This looks like a Mifare Classic 1K dump file"); + } else if (bytes == 4096) { + printf("Bytes of %s: %ld\n", argv[argc - 1], bytes); + puts("This lokks like a Mifare Classic 4K dump file"); + } else { + printf("Bytes of %s: %ld\n", argv[argc - 1], bytes); + printf(ANSI_COLOR_RED "This doesn't look like a Mifare Classic 1K or 4K at " + "all\n" ANSI_COLOR_RESET); + } +} diff --git a/src/readFile.c b/src/readFile.c new file mode 100644 index 0000000..877bb33 --- /dev/null +++ b/src/readFile.c @@ -0,0 +1,89 @@ +#include +#include "readFile.h" + +/* The worst array ever */ +/* I guess it's better than doing a cycle */ +ColorRange colorRanges[] = { + {0, 4, ANSI_COLOR_MAGENTA}, + {4, 16, ANSI_COLOR_CYAN}, + /* Key A */ + {48, 54, ANSI_COLOR_GREEN}, + {48 + 64, 54 + 64, ANSI_COLOR_GREEN}, + {48 + 64 * 2, 54 + 64 * 2, ANSI_COLOR_GREEN}, + {48 + 64 * 3, 54 + 64 * 3, ANSI_COLOR_GREEN}, + {48 + 64 * 4, 54 + 64 * 4, ANSI_COLOR_GREEN}, + {48 + 64 * 5, 54 + 64 * 5, ANSI_COLOR_GREEN}, + {48 + 64 * 6, 54 + 64 * 6, ANSI_COLOR_GREEN}, + {48 + 64 * 7, 54 + 64 * 7, ANSI_COLOR_GREEN}, + {48 + 64 * 8, 54 + 64 * 8, ANSI_COLOR_GREEN}, + {48 + 64 * 9, 54 + 64 * 9, ANSI_COLOR_GREEN}, + {48 + 64 * 10, 54 + 64 * 10, ANSI_COLOR_GREEN}, + {48 + 64 * 11, 54 + 64 * 11, ANSI_COLOR_GREEN}, + {48 + 64 * 12, 54 + 64 * 12, ANSI_COLOR_GREEN}, + {48 + 64 * 13, 54 + 64 * 13, ANSI_COLOR_GREEN}, + {48 + 64 * 14, 54 + 64 * 14, ANSI_COLOR_GREEN}, + {48 + 64 * 15, 54 + 64 * 15, ANSI_COLOR_GREEN}, + /* Access Bits */ + {54, 58, ANSI_COLOR_RED}, + {54 + 64, 58 + 64, ANSI_COLOR_RED}, + {54 + 64 * 2, 58 + 64 * 2, ANSI_COLOR_RED}, + {54 + 64 * 3, 58 + 64 * 3, ANSI_COLOR_RED}, + {54 + 64 * 4, 58 + 64 * 4, ANSI_COLOR_RED}, + {54 + 64 * 5, 58 + 64 * 5, ANSI_COLOR_RED}, + {54 + 64 * 6, 58 + 64 * 6, ANSI_COLOR_RED}, + {54 + 64 * 7, 58 + 64 * 7, ANSI_COLOR_RED}, + {54 + 64 * 8, 58 + 64 * 8, ANSI_COLOR_RED}, + {54 + 64 * 9, 58 + 64 * 9, ANSI_COLOR_RED}, + {54 + 64 * 10, 58 + 64 * 10, ANSI_COLOR_RED}, + {54 + 64 * 11, 58 + 64 * 11, ANSI_COLOR_RED}, + {54 + 64 * 12, 58 + 64 * 12, ANSI_COLOR_RED}, + {54 + 64 * 13, 58 + 64 * 13, ANSI_COLOR_RED}, + {54 + 64 * 14, 58 + 64 * 14, ANSI_COLOR_RED}, + {54 + 64 * 15, 58 + 64 * 15, ANSI_COLOR_RED}, + /* Key B */ + {58, 64, ANSI_COLOR_YELLOW}, + {58 + 64, 64 + 64, ANSI_COLOR_YELLOW}, + {58 + 64 * 2, 64 + 64 * 2, ANSI_COLOR_YELLOW}, + {58 + 64 * 3, 64 + 64 * 3, ANSI_COLOR_YELLOW}, + {58 + 64 * 4, 64 + 64 * 4, ANSI_COLOR_YELLOW}, + {58 + 64 * 5, 64 + 64 * 5, ANSI_COLOR_YELLOW}, + {58 + 64 * 6, 64 + 64 * 6, ANSI_COLOR_YELLOW}, + {58 + 64 * 7, 64 + 64 * 7, ANSI_COLOR_YELLOW}, + {58 + 64 * 8, 64 + 64 * 8, ANSI_COLOR_YELLOW}, + {58 + 64 * 9, 64 + 64 * 9, ANSI_COLOR_YELLOW}, + {58 + 64 * 10, 64 + 64 * 10, ANSI_COLOR_YELLOW}, + {58 + 64 * 11, 64 + 64 * 11, ANSI_COLOR_YELLOW}, + {58 + 64 * 12, 64 + 64 * 12, ANSI_COLOR_YELLOW}, + {58 + 64 * 13, 64 + 64 * 13, ANSI_COLOR_YELLOW}, + {58 + 64 * 14, 64 + 64 * 14, ANSI_COLOR_YELLOW}, + {58 + 64 * 15, 64 + 64 * 15, ANSI_COLOR_YELLOW}, +}; + +int colorRangesCount = sizeof(colorRanges) / sizeof(ColorRange); + +void readFile(FILE *file) { + int count = 0; + unsigned char buffer[256]; + size_t bytesRead; + puts("\nColor meaning:"); + printf(ANSI_COLOR_MAGENTA "\tUID of the Card\n"); + printf(ANSI_COLOR_CYAN "\tManufacturer Info\n"); + printf(ANSI_COLOR_GREEN "\tKey A\n"); + printf(ANSI_COLOR_YELLOW "\tKey B\n"); + printf(ANSI_COLOR_RED "\tAccess Bits\n\n"); + while ((bytesRead = fread(buffer, 1, sizeof(buffer), file)) > 0) { + for (size_t i = 0; i < bytesRead; i++) { + const char* color = ANSI_COLOR_RESET; + for (int j = 0; j < colorRangesCount; j++) { + if (count >= colorRanges[j].start && count < colorRanges[j].end) { + color = colorRanges[j].color; + break; + } + } + printf("%s%02x ", color, buffer[i]); + if(++count % 16 == 0) { + puts(""); + } + } + } +}