This commit is contained in:
Fijxu 2023-12-16 03:08:59 -03:00
parent f6defc402e
commit 7b5846e432
7 changed files with 173 additions and 72 deletions

View File

@ -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)

7
include/colors.h Normal file
View File

@ -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"

3
include/getSize.h Normal file
View File

@ -0,0 +1,3 @@
#include <stdio.h>
void getSize(FILE *file, int argc, char *argv[]);

12
include/readFile.h Normal file
View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include "colors.h"
#pragma once
typedef struct {
int start;
int end;
const char* color;
} ColorRange;
void readFile(FILE *file);

105
main.c
View File

@ -1,96 +1,59 @@
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <getopt.h>
#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);
}

20
src/getSize.c Normal file
View File

@ -0,0 +1,20 @@
#include <stdio.h>
#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);
}
}

89
src/readFile.c Normal file
View File

@ -0,0 +1,89 @@
#include <stdio.h>
#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("");
}
}
}
}