From 74a37cd6403be409dba8786b2d72217a583854bb Mon Sep 17 00:00:00 2001 From: Pin Date: Tue, 5 Oct 2021 00:32:06 -0400 Subject: [PATCH] wip --- crc.c | 154 +++++++++++++++++++++++++++++++++-------------------- crc_util.c | 43 +++++++++++++++ crc_util.h | 6 +++ 3 files changed, 146 insertions(+), 57 deletions(-) create mode 100644 crc_util.c create mode 100644 crc_util.h diff --git a/crc.c b/crc.c index a9c1f78..9771842 100644 --- a/crc.c +++ b/crc.c @@ -1,38 +1,81 @@ #include +#include +#include +#include #include #include #include #include +#include "crc_util.h" #include "CRCLib.h" #include "crc.h" -const long png_signature[8] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a }; +#define CHUNK 8 const long idat_signature = 1229209940; const long iend_signature = 1229278788; -const int working = 1; -int check_file_header(char *addr) { - int signature_match = 0; - for( int i = 0; i < 8; i++ ) { - if (addr[i] != png_signature[i]) { - signature_match = 1; - } +int zlib_compress_data(unsigned char *data_chunk, size_t file_length) { + int ret; + unsigned int have; + z_stream strm; + unsigned char out[CHUNK]; + unsigned char in[CHUNK]; + + printf("Len: %zu\n", file_length); + + errno=0; + FILE *data_stream = fmemopen(data_chunk, file_length-1, "r"); + FILE *of = fopen("wow.wow", "w"); + if(data_stream == NULL) { + perror("F MEM OPEN"); } - printf("Sig Match: %d\n", signature_match); - return signature_match; -} - - -int check_header_length(unsigned char *addr, long offset) { - unsigned int res = 0; - for( int i = 0; i < 4; i++ ) { - res |= addr[offset+i]; - if (i < 3) { - res <<= 8; - } + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + strm.avail_in = 0; + strm.next_in = Z_NULL; + ret = inflateInit(&strm); + if(ret != Z_OK) { + return ret; } - return res; + + do { + strm.avail_in = fread(in, 1, CHUNK, data_stream); + if(ferror(data_stream)) { + (void)inflateEnd(&strm); + return Z_ERRNO; + } + if(strm.avail_in == 0) { + break; + } + strm.next_in = in; + + do { + strm.avail_out = CHUNK; + strm.next_out = out; + ret = inflate(&strm, Z_NO_FLUSH); + assert(ret != Z_STREAM_ERROR); + switch(ret) { + case Z_NEED_DICT: + ret = Z_DATA_ERROR; + case Z_DATA_ERROR: + case Z_MEM_ERROR: + (void)inflateEnd(&strm); + printf("Error: %d\n", ret); + printf("MSG: %s\n", (char*)strm.msg); + return ret; + } + have = CHUNK - strm.avail_out; + fwrite(out, 1, have, of); + } while(strm.avail_out == 0); + } while(ret != Z_STREAM_END); + + (void)inflateEnd(&strm); + + printf("String: %s\n", out); + + return 1; } unsigned long first_idat(unsigned char *addr) { @@ -83,7 +126,7 @@ int update_file_crc(unsigned char *addr, unsigned long offset , unsigned int crc } int change_idat_content(unsigned char *addr, char *message, int accuracy, unsigned long offset) { - printf("Starting IDAT Tranform\n"); + //printf("Starting IDAT Tranform\n"); if(accuracy > 4) { printf("Warning, accuracy cannot be larger than 4"); return EXIT_FAILURE; @@ -97,20 +140,24 @@ int change_idat_content(unsigned char *addr, char *message, int accuracy, unsign } int idat_length = check_header_length(addr, offset); - printf("IDAT: %d\n", idat_length); + printf("IDAT Length: %d\n", idat_length); int prop_found = 0; long size = 1; long rounds = 0; - unsigned int* idat_data = malloc(size * sizeof(unsigned int)); - for(int i = 0; i < idat_length; i++) { + size_t idat_byte_length = 0; + unsigned char* idat_data = calloc(size, sizeof(unsigned char)); + for(size_t i = 0; i <= idat_length; i++) { + if(i == size) { + size *= 2; + idat_data = reallocarray(idat_data, size, sizeof(unsigned char)); + } idat_data[i] = addr[i+offset+8]; - size++; - int* new_idat_data = realloc(idat_data, size * sizeof(unsigned int)); + idat_byte_length = i; } - unsigned int temp_idat_data[size]; + unsigned char temp_idat_data[idat_byte_length]; while(prop_found == 0) { - for(int i = 0; i < idat_length; i++) { + for(int i = 0; i <= idat_length; i++) { temp_idat_data[i] = idat_data[i]; } int r = randombytes_uniform(5) + 1; @@ -122,7 +169,7 @@ int change_idat_content(unsigned char *addr, char *message, int accuracy, unsign for(int i = 0; i < 4; i++) { crc_check[i] = idat_header[i]; } - for(int i = 0; i < size; i++) { + for(int i = 0; i < idat_byte_length; i++) { crc_check[i] = temp_idat_data[i+4]; } unsigned int crcnum = crc(crc_check, idat_length); @@ -131,10 +178,11 @@ int change_idat_content(unsigned char *addr, char *message, int accuracy, unsign unsigned int checked_crc = crcnum >> (8*3); rounds++; if(checked_crc == 61) { + zlib_compress_data(temp_idat_data, idat_byte_length); printf("Found %d in %d rounds\n", checked_crc ,rounds); - printf("Full CRC: %08X\n", crcnum); - printf("Original: %02X\n", idat_data[j]); - printf("Change offset: %d to hex: %02X\n", j, temp_idat_data[j]); + //printf("Full CRC: %08X\n", crcnum); + //printf("Original: %02X\n", idat_data[j]); + //printf("Change offset: %d to hex: %02X\n", j, temp_idat_data[j]); addr[offset+8+j] = temp_idat_data[j]; update_file_crc(addr, offset, crcnum); prop_found = 1; @@ -144,39 +192,31 @@ int change_idat_content(unsigned char *addr, char *message, int accuracy, unsign return 0; } -int create_cc_file(unsigned char *addr, unsigned long file_length) { - FILE *fp; - fp = fopen("png2.png", "w"); - - if(fp == NULL) { - return EXIT_FAILURE; - } - - for(int i = 0; i < file_length; i++){ - fputc(addr[i], fp); - } - fclose(fp); -} - int main() { FILE *fp; unsigned int c; - unsigned char* myArray = calloc(1000, sizeof(unsigned char)); - unsigned long i = 0; + unsigned long file_data_cap = 8; + unsigned char* file_data = calloc(file_data_cap, sizeof(unsigned char)); + size_t i = 0; unsigned long offset = 0; char message[1]; if(sodium_init() == -1) { return EXIT_FAILURE; } fp = fopen("./1.png", "rt"); - while((c = fgetc(fp)) != EOF) { - myArray[i] = c; - i++; + for(size_t i = 0;(c = fgetc(fp)) != EOF; i++) { + if(i == file_data_cap) { + file_data_cap *= 2; + file_data = reallocarray(file_data, file_data_cap, sizeof(unsigned char)); + if(file_data == NULL) { + perror("FAILED ARRAY RESIZE"); + return EXIT_FAILURE; + } + } + file_data[i] = c; } fclose(fp); - offset = first_idat(myArray); - change_idat_content(myArray, message, 2, offset); - create_cc_file(myArray, i); - //int crcnum = crc(myArray, 19); - //printf("%08X\n", crcnum); + offset = first_idat(file_data); + change_idat_content(file_data, message, 1, offset); + //create_cc_file(file_data, i); } diff --git a/crc_util.c b/crc_util.c new file mode 100644 index 0000000..0f18bf3 --- /dev/null +++ b/crc_util.c @@ -0,0 +1,43 @@ +#include +#include +#include "crc_util.h" + +const long png_signature[8] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a }; + + +int check_header_length(unsigned char *addr, long offset) { + unsigned int res = 0; + for( int i = 0; i < 4; i++ ) { + res |= addr[offset+i]; + if (i < 3) { + res <<= 8; + } + } + return res; +} + +int check_file_header(char *addr) { + int signature_match = 0; + for( int i = 0; i < 8; i++ ) { + if (addr[i] != png_signature[i]) { + signature_match = 1; + } + } + printf("Sig Match: %d\n", signature_match); + return signature_match; + +} + +int create_cc_file(unsigned char *addr, unsigned long file_length) { + FILE *fp; + fp = fopen("png2.png", "w"); + + if(fp == NULL) { + return EXIT_FAILURE; + } + + for(int i = 0; i < file_length; i++){ + fputc(addr[i], fp); + } + fclose(fp); +} diff --git a/crc_util.h b/crc_util.h new file mode 100644 index 0000000..9edfc34 --- /dev/null +++ b/crc_util.h @@ -0,0 +1,6 @@ + +extern const long png_signature[8]; + +int check_header_length(unsigned char *addr, long offset); +int check_file_header(char *addr); +int create_cc_file(unsigned char *addr, unsigned long file_length);