This commit is contained in:
Pin
2021-10-21 00:53:06 -04:00
parent a2f023d76b
commit 968ca756c3
4 changed files with 83 additions and 47 deletions

113
cmd/crc.c
View File

@@ -1,4 +1,4 @@
#pragma GCC optimize("Ofast") #pragma GCC optimize("O0")
#include <endian.h> #include <endian.h>
#include <stdio.h> #include <stdio.h>
@@ -18,8 +18,8 @@ const long iend_signature = 1229278788;
unsigned long first_idat(unsigned char *addr) { unsigned long first_idat(unsigned char *addr) {
int idat_found = 0; int idat_found = 0;
unsigned long offset = 8; unsigned long offset = 8;
int jump_offset = 0; long jump_offset = 0;
int header_type = 0; unsigned long header_type = 0;
while(idat_found == 0) { while(idat_found == 0) {
jump_offset = check_header_length(addr, offset); jump_offset = check_header_length(addr, offset);
header_type = check_header_length(addr, offset+4); header_type = check_header_length(addr, offset+4);
@@ -33,21 +33,23 @@ unsigned long first_idat(unsigned char *addr) {
} }
int total_idat(unsigned char *addr) { int total_idat(unsigned char *addr) {
int iend_found = 0; int searching = 0;
int found_idat = 0; int found_idat = 0;
unsigned long offset = 8; unsigned long offset = 0;
int jump_offset = 0; unsigned long idat_length = 0;
int header_type = 0; unsigned long header_type = 0;
while(iend_found == 0) {
jump_offset = check_header_length(addr, offset); unsigned long first_idat_offset = first_idat(addr);
header_type = check_header_length(addr, offset+4); offset = first_idat_offset;
if(header_type == iend_signature) {
iend_found = 1; while(searching == 0) {
} else {
if(header_type == idat_signature) {
found_idat++; found_idat++;
} idat_length = check_header_length(addr, offset);
offset = offset + jump_offset + 12; // Jumping Offset + IDAT LENGTH + 3 bytes for 2 CRCs and 1 LEN fields
offset = offset+12+idat_length;
header_type = check_header_length(addr, offset+4);
if(header_type != idat_signature) {
searching = 1;
} }
} }
return found_idat; return found_idat;
@@ -63,40 +65,62 @@ int update_file_crc(unsigned char *addr, unsigned long offset , unsigned int crc
return 0; return 0;
} }
void random_data_change(unsigned char *color_data, int width, int length) { void random_data_change(unsigned char *color_data, unsigned char *width, size_t length) {
int searching = 1; int searching = 1;
size_t rounds = 0; size_t rounds = 0;
width = 16; unsigned char* full_data;
int data_array_size = 0;
// Needs to be turned into a variable
int color_range = 3; int color_range = 3;
unsigned char temp_color_data[length]; // Union for width type cast
union {
uint32_t width_int;
unsigned char width_array[4];
}w;
// Temp data array for crc testing
unsigned char* temp_color_data = calloc(length, sizeof(unsigned char));
memcpy(w.width_array, width, 4);
do { do {
rounds++; rounds++;
// Creating temporary data set // Creating temporary data set
memcpy(temp_color_data, color_data, length); memcpy(temp_color_data, color_data, length);
// Generating random byte to change // Generating random byte to change
int random_num = randombytes_uniform(length); size_t random_num = randombytes_uniform(100000);
//size_t random_num = randombytes_uniform(length);
// Checking for index break // Checking for index break
if(random_num % ((width * color_range) + 1)) { if(random_num % ((be32toh(w.width_int) * color_range) + 1)) {
if(color_data[random_num] == 255) { if(color_data[random_num] == 255) {
temp_color_data[random_num]--; temp_color_data[random_num]--;
} else { } else {
temp_color_data[random_num]++; temp_color_data[random_num]++;
} }
// Compressing data for test
unsigned char *check_data_buff = NULL; unsigned char *check_data_buff = NULL;
size_t check_data_length = 0; size_t check_data_length = 0;
zlib_compress_data(temp_color_data, length, &check_data_buff, &check_data_length); zlib_compress_data(temp_color_data, 50000, &check_data_buff, &check_data_length);
//zlib_compress_data(temp_color_data, length, &check_data_buff, &check_data_length);
if(check_data_length > 8192) {
data_array_size = 8192;
full_data = calloc(8196, sizeof(unsigned char));
} else {
data_array_size = check_data_length;
full_data = calloc(check_data_length+4, sizeof(unsigned char));
}
unsigned char full_data[check_data_length+4];
full_data[0] = 0x49; full_data[0] = 0x49;
full_data[1] = 0x44; full_data[1] = 0x44;
full_data[2] = 0x41; full_data[2] = 0x41;
full_data[3] = 0x54; full_data[3] = 0x54;
for(int i = 0; i < check_data_length; i++) { for(int i = 0; i < data_array_size; i++) {
full_data[i+4] = check_data_buff[i]; full_data[i+4] = check_data_buff[i];
} }
unsigned int temp_crc = crc(full_data, check_data_length); unsigned int temp_crc = crc(full_data, data_array_size);
printf("%08X\n", temp_crc);
if ((temp_crc >> (8*3)) == 10 ) { if ((temp_crc >> (8*3)) == 10 ) {
printf("RAND Key: %zu\n", random_num);
printf("Found in %zu rounds!\n", rounds); printf("Found in %zu rounds!\n", rounds);
memcpy(color_data, temp_color_data, length); memcpy(color_data, temp_color_data, length);
searching = 0; searching = 0;
@@ -105,6 +129,9 @@ void random_data_change(unsigned char *color_data, int width, int length) {
} }
} while(searching == 1); } while(searching == 1);
free(temp_color_data);
return;
} }
void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) { void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) {
@@ -119,6 +146,8 @@ void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) {
start_data.png_data = png_file->png_start_data; start_data.png_data = png_file->png_start_data;
printf("IHDR SIZE: %zu\n", sizeof(struct PNG_START_FILE_STRUCT));
// IHDR Data // IHDR Data
for(int i = 0; i < sizeof(start_data.data); i++) { for(int i = 0; i < sizeof(start_data.data); i++) {
fputc(start_data.data[i], fp); fputc(start_data.data[i], fp);
@@ -158,6 +187,7 @@ void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) {
} }
int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, char *message, int accuracy, unsigned long offset, char *out_file_name) { int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, char *message, int accuracy, unsigned long offset, char *out_file_name) {
size_t idat_length = 0;
if(accuracy > 4) { if(accuracy > 4) {
printf("Warning, accuracy cannot be larger than 4"); printf("Warning, accuracy cannot be larger than 4");
return EXIT_FAILURE; return EXIT_FAILURE;
@@ -170,31 +200,36 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, c
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
int idat_length = check_header_length(addr, offset); size_t size = 1;
printf("IDAT Length: %d\n", idat_length);
long size = 1;
size_t idat_byte_length = 0; size_t idat_byte_length = 0;
unsigned char* idat_data = calloc(size, sizeof(unsigned char)); unsigned char* idat_data = calloc(size, sizeof(unsigned char));
for(size_t i = 0; i <= idat_length; i++) { for(size_t i = 0; i < total_idat(addr); i++) {
if(i == size) { idat_length = check_header_length(addr, offset);
for(size_t j = 0; j < idat_length; j++) {
if(idat_byte_length == size) {
size *= 2; size *= 2;
idat_data = reallocarray(idat_data, size, sizeof(unsigned char)); idat_data = reallocarray(idat_data, size, sizeof(unsigned char));
} }
idat_data[i] = addr[i+offset+8]; idat_data[idat_byte_length] = addr[offset+8+j];
idat_byte_length = i; idat_byte_length++;
} }
unsigned char temp_idat_data[idat_byte_length]; // Offset 3 bytes for 2 length fields and 1 crc
for(int i = 0; i <= idat_length; i++) { offset = offset + idat_length + 12;
temp_idat_data[i] = idat_data[i];
} }
unsigned char* temp_idat_data = calloc(idat_byte_length, sizeof(unsigned char));
memcpy(temp_idat_data, idat_data, idat_byte_length);
// Decompressing Data // Decompressing Data
unsigned char *uncom_data_buff = NULL; unsigned char *uncom_data_buff = NULL;
size_t uncom_data_size = 0; size_t uncom_data_size = 0;
zlib_decompress_data(temp_idat_data, idat_byte_length, &uncom_data_buff, &uncom_data_size); zlib_decompress_data(temp_idat_data, idat_byte_length, &uncom_data_buff, &uncom_data_size);
random_data_change(uncom_data_buff, 16, uncom_data_size); // Start data testing
random_data_change(uncom_data_buff, png_file->png_start_data.file_width, uncom_data_size);
return 0;
// Compress Data // Compress Data
unsigned char *com_data_buff; unsigned char *com_data_buff;
size_t com_data_size = 0; size_t com_data_size = 0;
@@ -209,11 +244,13 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, c
} }
// Build PNG File // Build PNG File
build_png_file(png_file, out_file_name); //build_png_file(png_file, out_file_name);
// Freeing used memory
free(uncom_data_buff); free(uncom_data_buff);
free(com_data_buff); free(com_data_buff);
free(idat_data); free(idat_data);
free(temp_idat_data);
free(png_file->png_idat_data.idat_data); free(png_file->png_idat_data.idat_data);
return 0; return 0;

View File

@@ -1,7 +1,6 @@
#pragma once #pragma once
int check_file_header(char *addr); int check_file_header(char *addr);
int check_header_length(unsigned char *addr, long offset);
unsigned long first_idat(unsigned char *addr); unsigned long first_idat(unsigned char *addr);
int total_idat(unsigned char *addr); int total_idat(unsigned char *addr);
void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name); void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name);

View File

@@ -35,7 +35,7 @@ struct PNG_FILE_STRUCT {
extern const long png_signature[8]; extern const long png_signature[8];
int check_header_length(unsigned char *addr, long offset); unsigned long check_header_length(unsigned char *addr, long offset);
int check_file_header(char *addr); int check_file_header(char *addr);
int create_cc_file(unsigned char *addr, unsigned long file_length); int create_cc_file(unsigned char *addr, unsigned long file_length);
unsigned char* file_to_char_array(FILE *in_file, size_t* size); unsigned char* file_to_char_array(FILE *in_file, size_t* size);

View File

@@ -4,8 +4,8 @@
const long png_signature[8] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a }; const long png_signature[8] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
int check_header_length(unsigned char *addr, long offset) { unsigned long check_header_length(unsigned char *addr, long offset) {
unsigned int res = 0; unsigned long res = 0;
for( int i = 0; i < 4; i++ ) { for( int i = 0; i < 4; i++ ) {
res |= addr[offset+i]; res |= addr[offset+i];
if (i < 3) { if (i < 3) {
@@ -71,7 +71,7 @@ int create_cc_file(unsigned char *addr, unsigned long file_length) {
} }
unsigned char* file_to_char_array(FILE *in_file, size_t* size) { unsigned char* file_to_char_array(FILE *in_file, size_t* size) {
unsigned int c; unsigned long c;
unsigned long file_data_cap = 8; unsigned long file_data_cap = 8;
unsigned char* file_data = calloc(file_data_cap, sizeof(unsigned char)); unsigned char* file_data = calloc(file_data_cap, sizeof(unsigned char));