From 3b8fd9c901942a11bac0eafbbe07da2829dbe610 Mon Sep 17 00:00:00 2001 From: Pin Date: Tue, 21 Sep 2021 01:15:03 -0400 Subject: [PATCH 01/17] wip Signed-off-by: Pin --- .gitignore | 5 ++ Makefile | 16 ++++ cmd/crc.c | 190 ++++++++++++++++++++++++++++++++++++++++ include/CRCLib.h | 51 +++++++++++ include/compress_util.h | 8 ++ include/crc.h | 6 ++ include/crc_util.h | 11 +++ src/compress_util.c | 126 ++++++++++++++++++++++++++ src/crc_util.c | 64 ++++++++++++++ 9 files changed, 477 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 cmd/crc.c create mode 100644 include/CRCLib.h create mode 100644 include/compress_util.h create mode 100644 include/crc.h create mode 100644 include/crc_util.h create mode 100644 src/compress_util.c create mode 100644 src/crc_util.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..961fffd --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.png +*.out +*.woo +*.wow +bin/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b06726e --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +LIBRARIES = `pkg-config --libs zlib libsodium` -Iinclude +SOURCES = ./src/* ./cmd/crc.c +OUTPUT_DIR = ./bin +OUTPUT = -o ${OUTPUT_DIR}/PROG + +build: output_dir + gcc -Wall ${LIBRARIES} ${SOURCES} ${OUTPUT:PROG=crc} + +debug: output_dir + gcc -Wall -g ${LIBRARIES} ${SOURCES} ${OUTPUT:PROG=crc} + +output_dir: + mkdir -p ${OUTPUT_DIR} + +clean: + rm -rf $(OUTPUT_DIR) **.h.gch diff --git a/cmd/crc.c b/cmd/crc.c new file mode 100644 index 0000000..435a44d --- /dev/null +++ b/cmd/crc.c @@ -0,0 +1,190 @@ +#include +#include +#include +#include +#include +#include "crc_util.h" +#include "CRCLib.h" +#include "crc.h" +#include "compress_util.h" + +const long idat_signature = 1229209940; +const long iend_signature = 1229278788; + +unsigned long first_idat(unsigned char *addr) { + int idat_found = 0; + unsigned long offset = 8; + int jump_offset = 0; + int header_type = 0; + while(idat_found == 0) { + jump_offset = check_header_length(addr, offset); + header_type = check_header_length(addr, offset+4); + if(header_type == idat_signature) { + idat_found = 1; + } else { + offset = offset + jump_offset + 12; + } + } + return offset; +} + +int total_idat(unsigned char *addr) { + int iend_found = 0; + int found_idat = 0; + unsigned long offset = 8; + int jump_offset = 0; + int header_type = 0; + while(iend_found == 0) { + jump_offset = check_header_length(addr, offset); + header_type = check_header_length(addr, offset+4); + if(header_type == iend_signature) { + iend_found = 1; + } else { + if(header_type == idat_signature) { + found_idat++; + } + offset = offset + jump_offset + 12; + } + } + return found_idat; +} + +int update_file_crc(unsigned char *addr, unsigned long offset , unsigned int crc_num) { + int startCRC = 8 + offset + check_header_length(addr, offset); + unsigned char new_crc; + for(int i = 0; i < 4; i++) { + new_crc = crc_num >> (8*(3-i)) & 0xFF; + addr[startCRC+i] = new_crc; + } + return 0; +} + +void random_data_change(unsigned char *color_data, int width, int length) { + int searching = 1; + size_t rounds = 0; + width = 16; + int color_range = 3; + unsigned char temp_color_data[length]; + + memcpy(temp_color_data, color_data, length); + + do { + rounds++; + // Creating temporary data set + memcpy(temp_color_data, color_data, length); + // Generating random byte to change + int random_num = randombytes_uniform(length); + // Checking for index break + if(random_num % ((width * color_range) + 1)) { + if(color_data[random_num] == 255) { + temp_color_data[random_num]--; + } else { + temp_color_data[random_num]++; + } + unsigned char *check_data_buff = NULL; + size_t check_data_length = 0; + zlib_compress_data(temp_color_data, length, &check_data_buff, &check_data_length); + + unsigned char full_data[check_data_length+4]; + full_data[0] = 0x49; + full_data[1] = 0x44; + full_data[2] = 0x41; + full_data[3] = 0x54; + for(int i = 0; i < check_data_length; i++) { + full_data[i+4] = check_data_buff[i]; + } + unsigned int temp_crc = crc(full_data, check_data_length); + if ((temp_crc >> (8*3)) == 10 ) { + printf("Found in %zu rounds!\n", rounds); + searching = 0; + } + free(check_data_buff); + } + + } while(searching == 1); +} + +int change_idat_content(unsigned char *addr, unsigned char *message, int accuracy, unsigned long offset) { + //printf("Starting IDAT Tranform\n"); + if(accuracy > 4) { + printf("Warning, accuracy cannot be larger than 4"); + return EXIT_FAILURE; + } + if(accuracy > 2) { + printf("Notice, this could take a long time..."); + } + if(total_idat(addr) < strlen(message)) { + printf("Warning, message exceeds IDAT amount"); + return EXIT_FAILURE; + } + + int idat_length = check_header_length(addr, offset); + printf("IDAT Length: %d\n", idat_length); + + int prop_found = 0; + long size = 1; + long rounds = 0; + 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]; + idat_byte_length = i; + } + unsigned char temp_idat_data[idat_byte_length]; + while(prop_found == 0) { + for(int i = 0; i <= idat_length; i++) { + temp_idat_data[i] = idat_data[i]; + } + // Decompressing Data + unsigned char *uncom_data_buff = NULL; + size_t uncom_data_size = 0; + 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); + + free(uncom_data_buff); + + //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]); + //addr[offset+8+j] = temp_idat_data[j]; + //update_file_crc(addr, offset, crcnum); + prop_found = 1; + } + + free(idat_data); + + return 0; +} + +// This is where it all starts +int main() { + FILE *fp; + size_t i = 0; + unsigned long offset = 0; + unsigned char *message = malloc(sizeof(char)); + message[0] = '\0'; + + if(sodium_init() == -1) { + return EXIT_FAILURE; + } + + fp = fopen("./1.png", "rt"); + if (fp == NULL) { + return EXIT_FAILURE; + } + + unsigned char *file_data = file_to_char_array(fp, &i); + fclose(fp); + + offset = first_idat(file_data); + change_idat_content(file_data, message, 1, offset); + free(file_data); + free(message); + //create_cc_file(file_data, i); +} diff --git a/include/CRCLib.h b/include/CRCLib.h new file mode 100644 index 0000000..39530ff --- /dev/null +++ b/include/CRCLib.h @@ -0,0 +1,51 @@ +/* Table of CRCs of all 8-bit messages. */ +unsigned long crc_table[256]; + +/* Flag: has the table been computed? Initially false. */ +int crc_table_computed = 0; + +/* Make the table for a fast CRC. */ +void make_crc_table(void) +{ + unsigned long c; + int n, k; + + for (n = 0; n < 256; n++) { + c = (unsigned long) n; + for (k = 0; k < 8; k++) { + if (c & 1) + c = 0xedb88320L ^ (c >> 1); + else + c = c >> 1; + } + crc_table[n] = c; + } + crc_table_computed = 1; +} + + +/* Update a running CRC with the bytes buf[0..len-1]--the CRC + should be initialized to all 1's, and the transmitted value + is the 1's complement of the final running CRC (see the + crc() routine below). */ + +unsigned long update_crc(unsigned long crc, unsigned char *buf, + int len) +{ + unsigned long c = crc; + int n; + + if (!crc_table_computed) + make_crc_table(); + for (n = 0; n < len; n++) { + c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8); + } + return c; +} + +/* Return the CRC of the bytes buf[0..len-1]. */ +unsigned long crc(unsigned char *buf, int len) +{ + return update_crc(0xffffffffL, buf, len) ^ 0xffffffffL; +} + diff --git a/include/compress_util.h b/include/compress_util.h new file mode 100644 index 0000000..f0c623f --- /dev/null +++ b/include/compress_util.h @@ -0,0 +1,8 @@ +#include +#include +#include +#include +#include + +void zlib_decompress_data(unsigned char *data_chunk, size_t file_length, unsigned char **buff, size_t *sz); +void zlib_compress_data(unsigned char *data_chunk, size_t file_length, unsigned char **buff, size_t *sz); diff --git a/include/crc.h b/include/crc.h new file mode 100644 index 0000000..acaba82 --- /dev/null +++ b/include/crc.h @@ -0,0 +1,6 @@ + +int check_file_header(char *addr); +int check_header_length(unsigned char *addr, long offset); +unsigned long first_idat(unsigned char *addr); +int total_idat(unsigned char *addr); +int change_idat_content(unsigned char *addr, unsigned char *message, int accuracy, unsigned long offset); diff --git a/include/crc_util.h b/include/crc_util.h new file mode 100644 index 0000000..6eae8e2 --- /dev/null +++ b/include/crc_util.h @@ -0,0 +1,11 @@ +#include + +// PNG File Struct +struct PNG + +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); +unsigned char* file_to_char_array(FILE *in_file, size_t* size); diff --git a/src/compress_util.c b/src/compress_util.c new file mode 100644 index 0000000..ebf3663 --- /dev/null +++ b/src/compress_util.c @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#include "compress_util.h" + +#define CHUNK 1024 + +void zlib_decompress_data(unsigned char *data_chunk, size_t file_length, unsigned char **buff, size_t *sz) { + int ret; + unsigned int have; + z_stream strm; + // Adding one to zero here + unsigned char out[CHUNK]; + unsigned char in[CHUNK]; + + errno=0; + FILE *data_stream = fmemopen(data_chunk, file_length, "r"); + FILE *of = open_memstream((char**)buff, sz); + if(data_stream == NULL) { + perror("F MEM OPEN"); + } + + 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; + } + + do { + strm.avail_in = fread(in, 1, CHUNK, data_stream); + if(ferror(data_stream)) { + (void)inflateEnd(&strm); + return; + } + 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; + } + have = CHUNK - strm.avail_out; + fwrite(out, 1, have, of); + } while(strm.avail_out == 0); + } while(ret != Z_STREAM_END); + + (void)inflateEnd(&strm); + + fclose(data_stream); + fclose(of); +} + +void zlib_compress_data(unsigned char *data_chunk, size_t file_length, unsigned char **buff, size_t *sz) { + int ret, flush; + unsigned int have; + z_stream strm; + unsigned char in[CHUNK]; + unsigned char out[CHUNK]; + int level = 9; + int method = Z_DEFLATED; + int windowBits = 10; + int memLevel = 9; + //int strategy = Z_DEFAULT_STRATEGY; + int strategy = Z_FILTERED; + + FILE *data_stream = fmemopen(data_chunk, file_length, "r"); + FILE *out_data_stream = NULL; + out_data_stream = open_memstream((char**)buff, sz); + + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + ret = deflateInit2(&strm, level, method, windowBits, memLevel, strategy); + if (ret != Z_OK) { + return; + } + + do { + strm.avail_in = fread(in, 1, CHUNK, data_stream); + if (ferror(data_stream)) { + (void)deflateEnd(&strm); + return; + } + flush = feof(data_stream) ? Z_FINISH : Z_NO_FLUSH; + strm.next_in = in; + + do { + strm.avail_out = CHUNK; + strm.next_out = out; + + ret = deflate(&strm, flush); + assert(ret != Z_STREAM_ERROR); + have = CHUNK - strm.avail_out; + if(fwrite(out, 1, have, out_data_stream) != have || ferror(out_data_stream)) { + (void)deflateEnd(&strm); + return; + } + } while(strm.avail_out == 0); + assert(strm.avail_in == 0); + + } while(flush != Z_FINISH); + assert(ret == Z_STREAM_END); + + fclose(data_stream); + fclose(out_data_stream); + + (void)deflateEnd(&strm); +} diff --git a/src/crc_util.c b/src/crc_util.c new file mode 100644 index 0000000..5df74ca --- /dev/null +++ b/src/crc_util.c @@ -0,0 +1,64 @@ +#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); + return 0; +} + +unsigned char* file_to_char_array(FILE *in_file, size_t* size) { + unsigned int c; + unsigned long file_data_cap = 8; + unsigned char* file_data = calloc(file_data_cap, sizeof(unsigned char)); + + for(size_t i = 0;(c = fgetc(in_file)) != 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 NULL; + } + } + file_data[i] = c; + *size += 1; + } + return file_data; +} + From a781d77d8dc7d686d49f5f629a5215126eee5982 Mon Sep 17 00:00:00 2001 From: Pin Date: Mon, 11 Oct 2021 23:07:14 -0400 Subject: [PATCH 02/17] Giving Robert Credit --- cmd/crc.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/crc.c b/cmd/crc.c index 435a44d..dab6025 100644 --- a/cmd/crc.c +++ b/cmd/crc.c @@ -175,9 +175,11 @@ int main() { } fp = fopen("./1.png", "rt"); + // Written by Robert Zambito if (fp == NULL) { return EXIT_FAILURE; } + // No longer written by Robert Zambito unsigned char *file_data = file_to_char_array(fp, &i); fclose(fp); From 2f51491db9e44d6fc0aeb6167974fd1d610fe7a7 Mon Sep 17 00:00:00 2001 From: Pin Date: Mon, 11 Oct 2021 23:18:17 -0400 Subject: [PATCH 03/17] wip --- include/crc_util.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/include/crc_util.h b/include/crc_util.h index 6eae8e2..3dfe9c2 100644 --- a/include/crc_util.h +++ b/include/crc_util.h @@ -1,7 +1,16 @@ #include // PNG File Struct -struct PNG +struct PNG_FILE_STRUCT { + unsigned char file_sig[8]; + int file_width; + int file_height; + int bit_depth; + int color_type; + int compression_method; + int filter_method; + int interlace_method; +}; extern const long png_signature[8]; From 753415032619f70d5fb6cd06555f4c1ffb22dbc1 Mon Sep 17 00:00:00 2001 From: Pin Date: Mon, 11 Oct 2021 23:48:52 -0400 Subject: [PATCH 04/17] code atrib --- include/CRCLib.h | 4 ++++ src/compress_util.c | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/include/CRCLib.h b/include/CRCLib.h index 39530ff..f451ffb 100644 --- a/include/CRCLib.h +++ b/include/CRCLib.h @@ -1,3 +1,7 @@ +/* + * Code Implemented From: https://www.w3.org/TR/PNG/#D-CRCAppendix +*/ + /* Table of CRCs of all 8-bit messages. */ unsigned long crc_table[256]; diff --git a/src/compress_util.c b/src/compress_util.c index ebf3663..9407c8a 100644 --- a/src/compress_util.c +++ b/src/compress_util.c @@ -6,6 +6,9 @@ #define CHUNK 1024 +/* + * Built from exmaple provided in: https://zlib.net/zlib_how.html +*/ void zlib_decompress_data(unsigned char *data_chunk, size_t file_length, unsigned char **buff, size_t *sz) { int ret; unsigned int have; @@ -68,6 +71,9 @@ void zlib_decompress_data(unsigned char *data_chunk, size_t file_length, unsigne fclose(of); } +/* + * Built from exmaple provided in: https://zlib.net/zlib_how.html +*/ void zlib_compress_data(unsigned char *data_chunk, size_t file_length, unsigned char **buff, size_t *sz) { int ret, flush; unsigned int have; From 6166af2b41e222daefae4d506a7c8eccadc53b1d Mon Sep 17 00:00:00 2001 From: Pin Date: Thu, 14 Oct 2021 01:27:38 -0400 Subject: [PATCH 05/17] wip --- .gitignore | 1 + cmd/crc.c | 9 +++++- include/crc_util.h | 32 +++++++++++++++------ src/crc_util.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 961fffd..25b3266 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ *.out *.woo *.wow +*.gch bin/ diff --git a/cmd/crc.c b/cmd/crc.c index dab6025..4c40bc4 100644 --- a/cmd/crc.c +++ b/cmd/crc.c @@ -123,7 +123,6 @@ int change_idat_content(unsigned char *addr, unsigned char *message, int accurac int prop_found = 0; long size = 1; - long rounds = 0; size_t idat_byte_length = 0; unsigned char* idat_data = calloc(size, sizeof(unsigned char)); for(size_t i = 0; i <= idat_length; i++) { @@ -167,6 +166,8 @@ int main() { FILE *fp; size_t i = 0; unsigned long offset = 0; + struct PNG_START_FILE_STRUCT png_start_file_data; + struct PNG_IDAT_FILE_STRUCT png_idat_file_data; unsigned char *message = malloc(sizeof(char)); message[0] = '\0'; @@ -184,7 +185,13 @@ int main() { unsigned char *file_data = file_to_char_array(fp, &i); fclose(fp); + populate_start_png(file_data, &png_start_file_data); + offset = first_idat(file_data); + printf("Off: %ld\n", offset); + + populate_idat_png(file_data, &png_idat_file_data, offset); + change_idat_content(file_data, message, 1, offset); free(file_data); free(message); diff --git a/include/crc_util.h b/include/crc_util.h index 3dfe9c2..f83a534 100644 --- a/include/crc_util.h +++ b/include/crc_util.h @@ -1,15 +1,29 @@ #include // PNG File Struct -struct PNG_FILE_STRUCT { +struct PNG_START_FILE_STRUCT { unsigned char file_sig[8]; - int file_width; - int file_height; - int bit_depth; - int color_type; - int compression_method; - int filter_method; - int interlace_method; + unsigned char ihdr_length[4]; + unsigned char ihdr_header[4]; + unsigned char file_width[4]; + unsigned char file_height[4]; + unsigned char bit_depth[1]; + unsigned char color_type[1]; + unsigned char compression_method[1]; + unsigned char filter_method[1]; + unsigned char interlace_method[1]; + unsigned char ihdr_crc[4]; +}; + +struct PNG_IDAT_FILE_STRUCT { + unsigned char idat_length[4]; + unsigned char idat_header[4]; + unsigned char idat_crc[4]; + unsigned char idat_data[]; +}; + +struct PNG_FILE_STRUCT { + struct PNG_START_FILE_STRUCT png_start_data; }; extern const long png_signature[8]; @@ -18,3 +32,5 @@ 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); unsigned char* file_to_char_array(FILE *in_file, size_t* size); +void populate_start_png(unsigned char *addr, struct PNG_START_FILE_STRUCT *png_data); +void populate_idat_png(unsigned char *addr, struct PNG_IDAT_FILE_STRUCT *png_data, unsigned long offset); diff --git a/src/crc_util.c b/src/crc_util.c index 5df74ca..271cbed 100644 --- a/src/crc_util.c +++ b/src/crc_util.c @@ -27,6 +27,77 @@ int check_file_header(char *addr) { } +void populate_idat_png(unsigned char *addr, struct PNG_IDAT_FILE_STRUCT *png_data, unsigned long offset) { + unsigned long cur_idat_length = 0; + for(int i = 0; i < 4; i++){ + png_data->idat_length[i] = addr[i+offset]; + } + + for(int i = 0; i < 4; i++){ + cur_idat_length += (png_data->idat_length[i] << (24-(8*i))); + } + + printf("\n%ld\n", cur_idat_length); +} + +void populate_start_png(unsigned char *addr, struct PNG_START_FILE_STRUCT *png_data) { + for(int i = 0; i < 8; i++) { + png_data->file_sig[i] = addr[i]; + printf("%02X ", png_data->file_sig[i]); + } + printf("\n"); + for(int i = 0; i < 4; i++) { + png_data->ihdr_length[i] = addr[i+8]; + printf("%02X ", png_data->ihdr_length[i]); + } + printf("\n"); + for(int i = 0; i < 4; i++) { + png_data->ihdr_header[i] = addr[i+12]; + printf("%02X ", png_data->ihdr_header[i]); + } + printf("\n"); + for(int i = 0; i < 4; i++) { + png_data->file_width[i] = addr[i+16]; + printf("%02X ", png_data->file_width[i]); + } + printf("\n"); + for(int i = 0; i < 4; i++) { + png_data->file_height[i] = addr[i+20]; + printf("%02X ", png_data->file_height[i]); + } + printf("\n"); + for(int i = 0; i < 1; i++) { + png_data->bit_depth[i] = addr[i+24]; + printf("%02X ", png_data->bit_depth[i]); + } + printf("\n"); + for(int i = 0; i < 1; i++) { + png_data->color_type[i] = addr[i+25]; + printf("%02X ", png_data->color_type[i]); + } + printf("\n"); + for(int i = 0; i < 1; i++) { + png_data->compression_method[i] = addr[i+26]; + printf("%02X ", png_data->compression_method[i]); + } + printf("\n"); + for(int i = 0; i < 1; i++) { + png_data->filter_method[i] = addr[i+27]; + printf("%02X ", png_data->filter_method[i]); + } + printf("\n"); + for(int i = 0; i < 1; i++) { + png_data->interlace_method[i] = addr[i+28]; + printf("%02X ", png_data->interlace_method[i]); + } + printf("\n"); + for(int i = 0; i < 4; i++) { + png_data->ihdr_crc[i] = addr[i+29]; + printf("%02X ", png_data->ihdr_crc[i]); + } + printf("\n"); +} + int create_cc_file(unsigned char *addr, unsigned long file_length) { FILE *fp; fp = fopen("png2.png", "w"); From 3c6c08429d49c2ff273e278c4f27f968d308a826 Mon Sep 17 00:00:00 2001 From: Pin Date: Fri, 15 Oct 2021 21:17:47 -0400 Subject: [PATCH 06/17] wip --- .gitignore | 1 + cmd/crc.c | 115 ++++++++++++++++++++++++++++++++++----------- include/crc.h | 3 +- include/crc_util.h | 9 +++- src/crc_util.c | 67 +++++--------------------- 5 files changed, 109 insertions(+), 86 deletions(-) diff --git a/.gitignore b/.gitignore index 25b3266..c7a6301 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ *.woo *.wow *.gch +vgcore.* bin/ diff --git a/cmd/crc.c b/cmd/crc.c index 4c40bc4..5fc9efb 100644 --- a/cmd/crc.c +++ b/cmd/crc.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -66,7 +67,7 @@ void random_data_change(unsigned char *color_data, int width, int length) { int color_range = 3; unsigned char temp_color_data[length]; - memcpy(temp_color_data, color_data, length); + //memcpy(temp_color_data, color_data, length); do { rounds++; @@ -96,6 +97,7 @@ void random_data_change(unsigned char *color_data, int width, int length) { unsigned int temp_crc = crc(full_data, check_data_length); if ((temp_crc >> (8*3)) == 10 ) { printf("Found in %zu rounds!\n", rounds); + memcpy(color_data, temp_color_data, length); searching = 0; } free(check_data_buff); @@ -104,8 +106,59 @@ void random_data_change(unsigned char *color_data, int width, int length) { } while(searching == 1); } -int change_idat_content(unsigned char *addr, unsigned char *message, int accuracy, unsigned long offset) { - //printf("Starting IDAT Tranform\n"); +void build_png_file(struct PNG_FILE_STRUCT *png_file) { + FILE *fp; + + fp = fopen("TESTPNG.png", "w"); + + union{ + unsigned char data[sizeof(struct PNG_START_FILE_STRUCT)]; + struct PNG_START_FILE_STRUCT png_data; + }start_data; + + start_data.png_data = png_file->png_start_data; + + // IHDR Data + for(int i = 0; i < sizeof(struct PNG_START_FILE_STRUCT); i++) { + fputc(start_data.data[i], fp); + } + // IDAT Data + for(int i = 0; i < 4; i++) { + fputc(png_file->png_idat_data.idat_length[i], fp); + } + for(int i = 0; i < 4; i++) { + fputc(png_file->png_idat_data.idat_header[i], fp); + } + for(int i = 0; i < be32toh(png_file->png_idat_data.idat_data_length); i++) { + fputc(png_file->png_idat_data.idat_data[i], fp); + } + // Generating CRC + unsigned char full_data[be32toh(png_file->png_idat_data.idat_data_length)+4]; + for(int i = 0; i < 4; i++) { + full_data[i] = png_file->png_idat_data.idat_header[i]; + } + for(int i = 0; i < be32toh(png_file->png_idat_data.idat_data_length); i++) { + full_data[i+4] = png_file->png_idat_data.idat_data[i]; + } + + unsigned int int_crc = crc(full_data, be32toh(png_file->png_idat_data.idat_data_length)); + unsigned char new_crc[4]; + + for(int i = 0; i < 4; i++) { + new_crc[i] = int_crc >> (8*(3-i)) & 0xFF; + fputc(new_crc[i], fp); + } + + // IEND Data + unsigned char IEND_DATA[12] = { 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82}; + for(int i = 0; i < 12; i++) { + fputc(IEND_DATA[i], fp); + } + + fclose(fp); +} + +int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, unsigned char *message, int accuracy, unsigned long offset) { if(accuracy > 4) { printf("Warning, accuracy cannot be larger than 4"); return EXIT_FAILURE; @@ -113,7 +166,7 @@ int change_idat_content(unsigned char *addr, unsigned char *message, int accurac if(accuracy > 2) { printf("Notice, this could take a long time..."); } - if(total_idat(addr) < strlen(message)) { + if(total_idat(addr) < strlen((char*)message)) { printf("Warning, message exceeds IDAT amount"); return EXIT_FAILURE; } @@ -121,7 +174,6 @@ int change_idat_content(unsigned char *addr, unsigned char *message, int accurac int idat_length = check_header_length(addr, offset); printf("IDAT Length: %d\n", idat_length); - int prop_found = 0; long size = 1; size_t idat_byte_length = 0; unsigned char* idat_data = calloc(size, sizeof(unsigned char)); @@ -134,29 +186,37 @@ int change_idat_content(unsigned char *addr, unsigned char *message, int accurac idat_byte_length = i; } unsigned char temp_idat_data[idat_byte_length]; - while(prop_found == 0) { - for(int i = 0; i <= idat_length; i++) { - temp_idat_data[i] = idat_data[i]; - } - // Decompressing Data - unsigned char *uncom_data_buff = NULL; - size_t uncom_data_size = 0; - zlib_decompress_data(temp_idat_data, idat_byte_length, &uncom_data_buff, &uncom_data_size); + for(int i = 0; i <= idat_length; i++) { + temp_idat_data[i] = idat_data[i]; + } + // Decompressing Data + unsigned char *uncom_data_buff = NULL; + size_t uncom_data_size = 0; + 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); + random_data_change(uncom_data_buff, 16, uncom_data_size); + + // Compress Data + unsigned char *com_data_buff; + size_t com_data_size = 0; + zlib_compress_data(uncom_data_buff, uncom_data_size, &com_data_buff, &com_data_size); - free(uncom_data_buff); + png_file->png_idat_data.idat_data = calloc(com_data_size, sizeof(unsigned char)); - //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]); - //addr[offset+8+j] = temp_idat_data[j]; - //update_file_crc(addr, offset, crcnum); - prop_found = 1; + // TEMP FIX FOR IDAT SIZE ISSUES + png_file->png_idat_data.idat_length[3] = (unsigned char)com_data_size; + + for(size_t i = 0; i < com_data_size; i++) { + png_file->png_idat_data.idat_data[i] = com_data_buff[i]; } + // Build PNG File + build_png_file(png_file); + + free(uncom_data_buff); + free(com_data_buff); free(idat_data); + free(png_file->png_idat_data.idat_data); return 0; } @@ -166,8 +226,7 @@ int main() { FILE *fp; size_t i = 0; unsigned long offset = 0; - struct PNG_START_FILE_STRUCT png_start_file_data; - struct PNG_IDAT_FILE_STRUCT png_idat_file_data; + struct PNG_FILE_STRUCT png_file_data; unsigned char *message = malloc(sizeof(char)); message[0] = '\0'; @@ -185,15 +244,15 @@ int main() { unsigned char *file_data = file_to_char_array(fp, &i); fclose(fp); - populate_start_png(file_data, &png_start_file_data); + populate_start_png(file_data, &png_file_data.png_start_data); offset = first_idat(file_data); printf("Off: %ld\n", offset); - populate_idat_png(file_data, &png_idat_file_data, offset); + populate_idat_png(file_data, &png_file_data.png_idat_data, offset); + + change_idat_content(file_data, &png_file_data, message, 1, offset); - change_idat_content(file_data, message, 1, offset); free(file_data); free(message); - //create_cc_file(file_data, i); } diff --git a/include/crc.h b/include/crc.h index acaba82..64ef786 100644 --- a/include/crc.h +++ b/include/crc.h @@ -3,4 +3,5 @@ int check_file_header(char *addr); int check_header_length(unsigned char *addr, long offset); unsigned long first_idat(unsigned char *addr); int total_idat(unsigned char *addr); -int change_idat_content(unsigned char *addr, unsigned char *message, int accuracy, unsigned long offset); +void build_png_file(struct PNG_FILE_STRUCT *png_file);;; +int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, unsigned char *message, int accuracy, unsigned long offset); diff --git a/include/crc_util.h b/include/crc_util.h index f83a534..be0a513 100644 --- a/include/crc_util.h +++ b/include/crc_util.h @@ -1,4 +1,5 @@ #include +#include // PNG File Struct struct PNG_START_FILE_STRUCT { @@ -16,14 +17,18 @@ struct PNG_START_FILE_STRUCT { }; struct PNG_IDAT_FILE_STRUCT { - unsigned char idat_length[4]; + union{ + unsigned char idat_length[4]; + uint32_t idat_data_length; + }; unsigned char idat_header[4]; unsigned char idat_crc[4]; - unsigned char idat_data[]; + unsigned char *idat_data; }; struct PNG_FILE_STRUCT { struct PNG_START_FILE_STRUCT png_start_data; + struct PNG_IDAT_FILE_STRUCT png_idat_data; }; extern const long png_signature[8]; diff --git a/src/crc_util.c b/src/crc_util.c index 271cbed..2b99d11 100644 --- a/src/crc_util.c +++ b/src/crc_util.c @@ -37,65 +37,22 @@ void populate_idat_png(unsigned char *addr, struct PNG_IDAT_FILE_STRUCT *png_dat cur_idat_length += (png_data->idat_length[i] << (24-(8*i))); } - printf("\n%ld\n", cur_idat_length); + for(int i = 0; i < 4; i++){ + png_data->idat_header[i] = addr[i+offset+4]; + } } void populate_start_png(unsigned char *addr, struct PNG_START_FILE_STRUCT *png_data) { - for(int i = 0; i < 8; i++) { - png_data->file_sig[i] = addr[i]; - printf("%02X ", png_data->file_sig[i]); + union{ + unsigned char *data; + struct PNG_START_FILE_STRUCT *png_data; + }png; + + png.png_data = png_data; + + for(int i = 0; i < sizeof(struct PNG_START_FILE_STRUCT); i++) { + png.data[i] = addr[i]; } - printf("\n"); - for(int i = 0; i < 4; i++) { - png_data->ihdr_length[i] = addr[i+8]; - printf("%02X ", png_data->ihdr_length[i]); - } - printf("\n"); - for(int i = 0; i < 4; i++) { - png_data->ihdr_header[i] = addr[i+12]; - printf("%02X ", png_data->ihdr_header[i]); - } - printf("\n"); - for(int i = 0; i < 4; i++) { - png_data->file_width[i] = addr[i+16]; - printf("%02X ", png_data->file_width[i]); - } - printf("\n"); - for(int i = 0; i < 4; i++) { - png_data->file_height[i] = addr[i+20]; - printf("%02X ", png_data->file_height[i]); - } - printf("\n"); - for(int i = 0; i < 1; i++) { - png_data->bit_depth[i] = addr[i+24]; - printf("%02X ", png_data->bit_depth[i]); - } - printf("\n"); - for(int i = 0; i < 1; i++) { - png_data->color_type[i] = addr[i+25]; - printf("%02X ", png_data->color_type[i]); - } - printf("\n"); - for(int i = 0; i < 1; i++) { - png_data->compression_method[i] = addr[i+26]; - printf("%02X ", png_data->compression_method[i]); - } - printf("\n"); - for(int i = 0; i < 1; i++) { - png_data->filter_method[i] = addr[i+27]; - printf("%02X ", png_data->filter_method[i]); - } - printf("\n"); - for(int i = 0; i < 1; i++) { - png_data->interlace_method[i] = addr[i+28]; - printf("%02X ", png_data->interlace_method[i]); - } - printf("\n"); - for(int i = 0; i < 4; i++) { - png_data->ihdr_crc[i] = addr[i+29]; - printf("%02X ", png_data->ihdr_crc[i]); - } - printf("\n"); } int create_cc_file(unsigned char *addr, unsigned long file_length) { From 184fc5fac459d0ca4e98280e0dc9b9fa73912604 Mon Sep 17 00:00:00 2001 From: Pin Date: Fri, 15 Oct 2021 23:30:29 -0400 Subject: [PATCH 07/17] wip --- Makefile | 3 ++ cmd/crc.c | 80 +++++++++++++++++++++++++++++++++++++++++---------- include/crc.h | 4 +-- 3 files changed, 70 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index b06726e..9d3cb0d 100644 --- a/Makefile +++ b/Makefile @@ -14,3 +14,6 @@ output_dir: clean: rm -rf $(OUTPUT_DIR) **.h.gch + +memcheck: debug + valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes -s ${OUTPUT_DIR}/crc diff --git a/cmd/crc.c b/cmd/crc.c index 5fc9efb..197b4c3 100644 --- a/cmd/crc.c +++ b/cmd/crc.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "crc_util.h" #include "CRCLib.h" #include "crc.h" @@ -106,10 +107,10 @@ void random_data_change(unsigned char *color_data, int width, int length) { } while(searching == 1); } -void build_png_file(struct PNG_FILE_STRUCT *png_file) { +void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) { FILE *fp; - fp = fopen("TESTPNG.png", "w"); + fp = fopen(out_file_name, "w"); union{ unsigned char data[sizeof(struct PNG_START_FILE_STRUCT)]; @@ -158,7 +159,7 @@ void build_png_file(struct PNG_FILE_STRUCT *png_file) { fclose(fp); } -int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, unsigned char *message, int accuracy, unsigned long offset) { +int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, char *message, int accuracy, unsigned long offset, char *out_file_name) { if(accuracy > 4) { printf("Warning, accuracy cannot be larger than 4"); return EXIT_FAILURE; @@ -167,8 +168,8 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, u printf("Notice, this could take a long time..."); } if(total_idat(addr) < strlen((char*)message)) { - printf("Warning, message exceeds IDAT amount"); - return EXIT_FAILURE; + printf("Warning, message exceeds IDAT amount\n"); + exit(EXIT_FAILURE); } int idat_length = check_header_length(addr, offset); @@ -211,7 +212,7 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, u } // Build PNG File - build_png_file(png_file); + build_png_file(png_file, out_file_name); free(uncom_data_buff); free(com_data_buff); @@ -222,24 +223,74 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, u } // This is where it all starts -int main() { +int main(int argc, char **argv) { FILE *fp; size_t i = 0; unsigned long offset = 0; struct PNG_FILE_STRUCT png_file_data; - unsigned char *message = malloc(sizeof(char)); - message[0] = '\0'; + char *in_file_name = NULL; + char *out_file_name = NULL; + char *message = NULL; + + static const struct option long_options[] = { + {"help", no_argument, NULL, 'h'}, + {"file", required_argument, NULL, 'f'}, + {"outfile", required_argument, NULL, 'o'}, + {"message", required_argument, NULL, 'm'}, + {0, 0, 0, 0} + }; + + const char* usage = + "Usage: crc [options]\n" + " -h, --help Shows help message\n" + " -f, --file Denotes input file\n" + " -o, --outfile Denotes output file\n" + " -m, --message Encoded message\n" + "\n"; + + int c; + while (1) { + int option_index = 0; + c = getopt_long(argc, argv, "hf:o:m:", long_options ,&option_index); + if(c == -1) { + break; + } + switch(c) { + case 'h': + printf("%s", usage); + exit(EXIT_SUCCESS); + case 'f': + in_file_name = optarg; + break; + case 'o': + out_file_name = optarg; + break; + case 'm': + message = optarg; + break; + } + } + + if(in_file_name == NULL) { + printf("Input file required!\n"); + exit(EXIT_FAILURE); + } else if(out_file_name == NULL) { + printf("Output file required!\n"); + exit(EXIT_FAILURE); + } else if(message == NULL) { + printf("Message required!\n"); + exit(EXIT_FAILURE); + } if(sodium_init() == -1) { return EXIT_FAILURE; } - fp = fopen("./1.png", "rt"); - // Written by Robert Zambito + fp = fopen(in_file_name, "rt"); if (fp == NULL) { - return EXIT_FAILURE; + printf("File error\n"); + exit(EXIT_FAILURE); } - // No longer written by Robert Zambito unsigned char *file_data = file_to_char_array(fp, &i); fclose(fp); @@ -251,8 +302,7 @@ int main() { populate_idat_png(file_data, &png_file_data.png_idat_data, offset); - change_idat_content(file_data, &png_file_data, message, 1, offset); + change_idat_content(file_data, &png_file_data, message, 1, offset, out_file_name); free(file_data); - free(message); } diff --git a/include/crc.h b/include/crc.h index 64ef786..d33f013 100644 --- a/include/crc.h +++ b/include/crc.h @@ -3,5 +3,5 @@ int check_file_header(char *addr); int check_header_length(unsigned char *addr, long offset); unsigned long first_idat(unsigned char *addr); int total_idat(unsigned char *addr); -void build_png_file(struct PNG_FILE_STRUCT *png_file);;; -int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, unsigned char *message, int accuracy, unsigned long offset); +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); From 677c34d3677714a768c933b0861c4e3e54ba3c6f Mon Sep 17 00:00:00 2001 From: Pin Date: Sat, 16 Oct 2021 00:38:10 -0400 Subject: [PATCH 08/17] wip --- cmd/crc.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/cmd/crc.c b/cmd/crc.c index 197b4c3..4aedef5 100644 --- a/cmd/crc.c +++ b/cmd/crc.c @@ -68,8 +68,6 @@ void random_data_change(unsigned char *color_data, int width, int length) { int color_range = 3; unsigned char temp_color_data[length]; - //memcpy(temp_color_data, color_data, length); - do { rounds++; // Creating temporary data set @@ -120,7 +118,7 @@ void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) { start_data.png_data = png_file->png_start_data; // IHDR Data - for(int i = 0; i < sizeof(struct PNG_START_FILE_STRUCT); i++) { + for(int i = 0; i < sizeof(start_data.data); i++) { fputc(start_data.data[i], fp); } // IDAT Data @@ -149,13 +147,11 @@ void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) { new_crc[i] = int_crc >> (8*(3-i)) & 0xFF; fputc(new_crc[i], fp); } - // IEND Data unsigned char IEND_DATA[12] = { 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82}; for(int i = 0; i < 12; i++) { fputc(IEND_DATA[i], fp); } - fclose(fp); } @@ -204,8 +200,7 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, c png_file->png_idat_data.idat_data = calloc(com_data_size, sizeof(unsigned char)); - // TEMP FIX FOR IDAT SIZE ISSUES - png_file->png_idat_data.idat_length[3] = (unsigned char)com_data_size; + png_file->png_idat_data.idat_data_length = be32toh(com_data_size); for(size_t i = 0; i < com_data_size; i++) { png_file->png_idat_data.idat_data[i] = com_data_buff[i]; @@ -298,7 +293,6 @@ int main(int argc, char **argv) { populate_start_png(file_data, &png_file_data.png_start_data); offset = first_idat(file_data); - printf("Off: %ld\n", offset); populate_idat_png(file_data, &png_file_data.png_idat_data, offset); From a2f023d76b9de51e29b1e81ac8d5aa98ec181eae Mon Sep 17 00:00:00 2001 From: Pin Date: Sat, 16 Oct 2021 00:49:10 -0400 Subject: [PATCH 09/17] wip --- cmd/crc.c | 2 ++ include/crc.h | 1 + include/crc_util.h | 2 ++ 3 files changed, 5 insertions(+) diff --git a/cmd/crc.c b/cmd/crc.c index 4aedef5..b872e6c 100644 --- a/cmd/crc.c +++ b/cmd/crc.c @@ -1,3 +1,5 @@ +#pragma GCC optimize("Ofast") + #include #include #include diff --git a/include/crc.h b/include/crc.h index d33f013..bf3b74a 100644 --- a/include/crc.h +++ b/include/crc.h @@ -1,3 +1,4 @@ +#pragma once int check_file_header(char *addr); int check_header_length(unsigned char *addr, long offset); diff --git a/include/crc_util.h b/include/crc_util.h index be0a513..5670cdb 100644 --- a/include/crc_util.h +++ b/include/crc_util.h @@ -1,6 +1,8 @@ #include #include +#pragma once + // PNG File Struct struct PNG_START_FILE_STRUCT { unsigned char file_sig[8]; From 968ca756c34215e96e7a6151a39899a7c4d75ce9 Mon Sep 17 00:00:00 2001 From: Pin Date: Thu, 21 Oct 2021 00:53:06 -0400 Subject: [PATCH 10/17] wip --- cmd/crc.c | 121 +++++++++++++++++++++++++++++---------------- include/crc.h | 1 - include/crc_util.h | 2 +- src/crc_util.c | 6 +-- 4 files changed, 83 insertions(+), 47 deletions(-) diff --git a/cmd/crc.c b/cmd/crc.c index b872e6c..0708625 100644 --- a/cmd/crc.c +++ b/cmd/crc.c @@ -1,4 +1,4 @@ -#pragma GCC optimize("Ofast") +#pragma GCC optimize("O0") #include #include @@ -18,8 +18,8 @@ const long iend_signature = 1229278788; unsigned long first_idat(unsigned char *addr) { int idat_found = 0; unsigned long offset = 8; - int jump_offset = 0; - int header_type = 0; + long jump_offset = 0; + unsigned long header_type = 0; while(idat_found == 0) { jump_offset = check_header_length(addr, offset); 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 iend_found = 0; + int searching = 0; int found_idat = 0; - unsigned long offset = 8; - int jump_offset = 0; - int header_type = 0; - while(iend_found == 0) { - jump_offset = check_header_length(addr, offset); + unsigned long offset = 0; + unsigned long idat_length = 0; + unsigned long header_type = 0; + + unsigned long first_idat_offset = first_idat(addr); + offset = first_idat_offset; + + while(searching == 0) { + found_idat++; + idat_length = check_header_length(addr, offset); + // 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 == iend_signature) { - iend_found = 1; - } else { - if(header_type == idat_signature) { - found_idat++; - } - offset = offset + jump_offset + 12; + if(header_type != idat_signature) { + searching = 1; } } return found_idat; @@ -63,40 +65,62 @@ int update_file_crc(unsigned char *addr, unsigned long offset , unsigned int crc 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; size_t rounds = 0; - width = 16; - int color_range = 3; - unsigned char temp_color_data[length]; + unsigned char* full_data; + int data_array_size = 0; + // Needs to be turned into a variable + int color_range = 3; + // 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 { rounds++; // Creating temporary data set memcpy(temp_color_data, color_data, length); // 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 - if(random_num % ((width * color_range) + 1)) { + if(random_num % ((be32toh(w.width_int) * color_range) + 1)) { if(color_data[random_num] == 255) { temp_color_data[random_num]--; } else { temp_color_data[random_num]++; } + // Compressing data for test unsigned char *check_data_buff = NULL; 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[1] = 0x44; full_data[2] = 0x41; 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]; } - 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 ) { + printf("RAND Key: %zu\n", random_num); printf("Found in %zu rounds!\n", rounds); memcpy(color_data, temp_color_data, length); searching = 0; @@ -105,6 +129,9 @@ void random_data_change(unsigned char *color_data, int width, int length) { } } while(searching == 1); + + free(temp_color_data); + return; } 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; + printf("IHDR SIZE: %zu\n", sizeof(struct PNG_START_FILE_STRUCT)); + // IHDR Data for(int i = 0; i < sizeof(start_data.data); i++) { 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) { + size_t idat_length = 0; if(accuracy > 4) { printf("Warning, accuracy cannot be larger than 4"); return EXIT_FAILURE; @@ -170,31 +200,36 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, c exit(EXIT_FAILURE); } - int idat_length = check_header_length(addr, offset); - printf("IDAT Length: %d\n", idat_length); - - long size = 1; + size_t size = 1; 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)); + for(size_t i = 0; i < total_idat(addr); i++) { + idat_length = check_header_length(addr, offset); + for(size_t j = 0; j < idat_length; j++) { + if(idat_byte_length == size) { + size *= 2; + idat_data = reallocarray(idat_data, size, sizeof(unsigned char)); + } + idat_data[idat_byte_length] = addr[offset+8+j]; + idat_byte_length++; } - idat_data[i] = addr[i+offset+8]; - idat_byte_length = i; - } - unsigned char temp_idat_data[idat_byte_length]; - for(int i = 0; i <= idat_length; i++) { - temp_idat_data[i] = idat_data[i]; + // Offset 3 bytes for 2 length fields and 1 crc + offset = offset + idat_length + 12; + } + + unsigned char* temp_idat_data = calloc(idat_byte_length, sizeof(unsigned char)); + memcpy(temp_idat_data, idat_data, idat_byte_length); + // Decompressing Data unsigned char *uncom_data_buff = NULL; size_t uncom_data_size = 0; 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 unsigned char *com_data_buff; 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(png_file, out_file_name); + //build_png_file(png_file, out_file_name); + // Freeing used memory free(uncom_data_buff); free(com_data_buff); free(idat_data); + free(temp_idat_data); free(png_file->png_idat_data.idat_data); return 0; diff --git a/include/crc.h b/include/crc.h index bf3b74a..645c63b 100644 --- a/include/crc.h +++ b/include/crc.h @@ -1,7 +1,6 @@ #pragma once int check_file_header(char *addr); -int check_header_length(unsigned char *addr, long offset); unsigned long first_idat(unsigned char *addr); int total_idat(unsigned char *addr); void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name); diff --git a/include/crc_util.h b/include/crc_util.h index 5670cdb..695146b 100644 --- a/include/crc_util.h +++ b/include/crc_util.h @@ -35,7 +35,7 @@ struct PNG_FILE_STRUCT { 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 create_cc_file(unsigned char *addr, unsigned long file_length); unsigned char* file_to_char_array(FILE *in_file, size_t* size); diff --git a/src/crc_util.c b/src/crc_util.c index 2b99d11..bd59941 100644 --- a/src/crc_util.c +++ b/src/crc_util.c @@ -4,8 +4,8 @@ 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; +unsigned long check_header_length(unsigned char *addr, long offset) { + unsigned long res = 0; for( int i = 0; i < 4; i++ ) { res |= addr[offset+i]; 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 int c; + unsigned long c; unsigned long file_data_cap = 8; unsigned char* file_data = calloc(file_data_cap, sizeof(unsigned char)); From 83dda0ef00196973258a02955f3d1c747f3c9138 Mon Sep 17 00:00:00 2001 From: Pin Date: Fri, 22 Oct 2021 10:01:22 -0400 Subject: [PATCH 11/17] wip --- Makefile | 4 +- cmd/crc.c | 187 +++++++++++++++++++++++++++++++-------------- include/crc_util.h | 12 +++ 3 files changed, 145 insertions(+), 58 deletions(-) diff --git a/Makefile b/Makefile index 9d3cb0d..d048279 100644 --- a/Makefile +++ b/Makefile @@ -4,10 +4,10 @@ OUTPUT_DIR = ./bin OUTPUT = -o ${OUTPUT_DIR}/PROG build: output_dir - gcc -Wall ${LIBRARIES} ${SOURCES} ${OUTPUT:PROG=crc} + gcc -Wall -pthread ${LIBRARIES} ${SOURCES} ${OUTPUT:PROG=crc} debug: output_dir - gcc -Wall -g ${LIBRARIES} ${SOURCES} ${OUTPUT:PROG=crc} + gcc -Wall -g -pthread ${LIBRARIES} ${SOURCES} ${OUTPUT:PROG=crc} output_dir: mkdir -p ${OUTPUT_DIR} diff --git a/cmd/crc.c b/cmd/crc.c index 0708625..147f183 100644 --- a/cmd/crc.c +++ b/cmd/crc.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "crc_util.h" #include "CRCLib.h" #include "crc.h" @@ -65,11 +66,118 @@ int update_file_crc(unsigned char *addr, unsigned long offset , unsigned int crc return 0; } +static void random_window_bit_change(unsigned char *data, int width, int rounds, int color_range, size_t length) { + size_t random_num = 0; + for (int i = 0; i < rounds; i++) { + random_num = randombytes_uniform(length); + do { + random_num = randombytes_uniform(length); + } while((random_num % ((width * color_range) + 1)) == 0); + + if(data[random_num] == 255) { + data[random_num]--; + } else { + data[random_num]++; + } + } + return; +} + +static int verify_crc_chunks(unsigned char *data, size_t data_length, int crc_depth, unsigned int max_data_length) { + unsigned char* testing_chunk; + unsigned int crc_check = 0; + unsigned int test_data_length = 0; + + for(int i = 0; i < crc_depth; i++) { + if((max_data_length * (i+1)) > data_length) { + test_data_length = (data_length - (max_data_length * i)); + testing_chunk = calloc(test_data_length+4, sizeof(unsigned char)); + } else { + test_data_length = max_data_length; + testing_chunk = calloc(test_data_length+4, sizeof(unsigned char)); + } + testing_chunk[0] = 0x49; + testing_chunk[1] = 0x44; + testing_chunk[2] = 0x41; + testing_chunk[3] = 0x54; + for(int j = 0; j < test_data_length; j++) { + testing_chunk[j+4] = data[(max_data_length*i)+j]; + } + crc_check = crc(testing_chunk, test_data_length); + + if(i == 2) { + printf("THIRD: %08X\n", crc_check); + } else if(i == 1) { + printf("SECOND: %08X\n", crc_check); + } + if ((crc_check >> (8*3)) != 10 ) { + free(testing_chunk); + return 1; + } + } + free(testing_chunk); + printf("FOUND: %08X\n", crc_check); + return 0; +} + +static int crc_embed_data(unsigned char *data, unsigned int data_length, int bit_width, int color_range, unsigned int sliding_window) { + unsigned char *check_data; + size_t check_data_length = 0; + + random_window_bit_change(data, bit_width, 3, color_range, sliding_window); + + // Compressing data for test + zlib_compress_data(data, data_length, &check_data, &check_data_length); + + int match_crc = verify_crc_chunks(check_data, check_data_length, 3, 8192); + + if(match_crc == 0) { + free(check_data); + return 0; + } + free(check_data); + return 1; +} + +void *random_data_change_thread_call(void *w) { + struct EMBED_THREAD_STRUCT *data = w; + unsigned char* temp_color_data = calloc(data->uncom_data_len, sizeof(unsigned char)); + int searching = 1; + do { + // Creating temporary data set + memcpy(temp_color_data, data->data, data->uncom_data_len); + + if(data->uncom_data_len > 800000) { + data->data_len = 150000; + } else { + data->data_len = data->uncom_data_len; + } + if(data->uncom_data_len > 16000) { + data->win_size = 9000; + } else { + data->win_size = data->uncom_data_len; + } + + searching = crc_embed_data(temp_color_data, data->data_len, data->width, data->color_range, data->win_size); + + if (searching == 0) { + pthread_mutex_lock(data->mutex_lock); + //pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); + *data->searching = searching; + memcpy(data->data, temp_color_data, data->uncom_data_len); + //pthread_cancel(data->thread_id); + pthread_mutex_unlock(data->mutex_lock); + } + + } while(*data->searching == 1); + free(temp_color_data); + return NULL; +} + void random_data_change(unsigned char *color_data, unsigned char *width, size_t length) { int searching = 1; - size_t rounds = 0; - unsigned char* full_data; - int data_array_size = 0; + unsigned int sliding_window = 0; + unsigned int compress_data_length = 0; // Needs to be turned into a variable int color_range = 3; // Union for width type cast @@ -77,60 +185,27 @@ void random_data_change(unsigned char *color_data, unsigned char *width, size_t 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 { - rounds++; - // Creating temporary data set - memcpy(temp_color_data, color_data, length); - // Generating random byte to change - size_t random_num = randombytes_uniform(100000); - //size_t random_num = randombytes_uniform(length); - // Checking for index break - if(random_num % ((be32toh(w.width_int) * color_range) + 1)) { - if(color_data[random_num] == 255) { - temp_color_data[random_num]--; - } else { - temp_color_data[random_num]++; - } - // Compressing data for test - unsigned char *check_data_buff = NULL; - size_t check_data_length = 0; - 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); + // Temp data array for crc testing + //unsigned char* temp_color_data = calloc(length, sizeof(unsigned char)); - 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)); - } + //int num_threads = 1; + struct EMBED_THREAD_STRUCT t_data; + t_data.searching = &searching; + t_data.data = color_data; + t_data.data_len = compress_data_length; + t_data.uncom_data_len = length; + t_data.width = be32toh(w.width_int); + t_data.color_range = color_range; + t_data.win_size = sliding_window; + pthread_t tid; + t_data.thread_id = tid; + for(int i = 0; i < 28; i++) { + pthread_create(&tid, NULL, random_data_change_thread_call, &t_data); + } + pthread_join(tid, NULL); - full_data[0] = 0x49; - full_data[1] = 0x44; - full_data[2] = 0x41; - full_data[3] = 0x54; - for(int i = 0; i < data_array_size; i++) { - full_data[i+4] = check_data_buff[i]; - } - unsigned int temp_crc = crc(full_data, data_array_size); - printf("%08X\n", temp_crc); - if ((temp_crc >> (8*3)) == 10 ) { - printf("RAND Key: %zu\n", random_num); - printf("Found in %zu rounds!\n", rounds); - memcpy(color_data, temp_color_data, length); - searching = 0; - } - free(check_data_buff); - } - - } while(searching == 1); - - free(temp_color_data); return; } @@ -200,7 +275,7 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, c exit(EXIT_FAILURE); } - size_t size = 1; + size_t size = 8; size_t idat_byte_length = 0; unsigned char* idat_data = calloc(size, sizeof(unsigned char)); for(size_t i = 0; i < total_idat(addr); i++) { @@ -229,7 +304,7 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, c // Start data testing random_data_change(uncom_data_buff, png_file->png_start_data.file_width, uncom_data_size); - return 0; + //return 0; // Compress Data unsigned char *com_data_buff; size_t com_data_size = 0; @@ -244,7 +319,7 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, c } // 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); diff --git a/include/crc_util.h b/include/crc_util.h index 695146b..49189f0 100644 --- a/include/crc_util.h +++ b/include/crc_util.h @@ -33,6 +33,18 @@ struct PNG_FILE_STRUCT { struct PNG_IDAT_FILE_STRUCT png_idat_data; }; +struct EMBED_THREAD_STRUCT { + pthread_mutex_t *mutex_lock; + pthread_t thread_id; + int *searching; + unsigned char *data; + unsigned int data_len; + size_t uncom_data_len; + int width; + int color_range; + unsigned int win_size; +}; + extern const long png_signature[8]; unsigned long check_header_length(unsigned char *addr, long offset); From dff9e4aea8e0956aa79d28a07edd074c801c87bd Mon Sep 17 00:00:00 2001 From: Pin Date: Sun, 24 Oct 2021 22:50:42 -0400 Subject: [PATCH 12/17] multi idat write support --- cmd/crc.c | 157 +++++++++++++++++++++++++++++---------------- include/crc_util.h | 2 +- 2 files changed, 102 insertions(+), 57 deletions(-) diff --git a/cmd/crc.c b/cmd/crc.c index 147f183..90f8f80 100644 --- a/cmd/crc.c +++ b/cmd/crc.c @@ -13,6 +13,9 @@ #include "crc.h" #include "compress_util.h" +#define MAX_IDAT_SIZE 16384 +//#define MAX_IDAT_SIZE 8192 + const long idat_signature = 1229209940; const long iend_signature = 1229278788; @@ -87,36 +90,43 @@ static int verify_crc_chunks(unsigned char *data, size_t data_length, int crc_de unsigned char* testing_chunk; unsigned int crc_check = 0; unsigned int test_data_length = 0; + unsigned int crc_check_length; + int rounds = 0; for(int i = 0; i < crc_depth; i++) { if((max_data_length * (i+1)) > data_length) { test_data_length = (data_length - (max_data_length * i)); testing_chunk = calloc(test_data_length+4, sizeof(unsigned char)); + // If this is met we need to stop verification + i = crc_depth; } else { test_data_length = max_data_length; testing_chunk = calloc(test_data_length+4, sizeof(unsigned char)); } + // Setting first chunk to IDAT testing_chunk[0] = 0x49; testing_chunk[1] = 0x44; testing_chunk[2] = 0x41; testing_chunk[3] = 0x54; for(int j = 0; j < test_data_length; j++) { - testing_chunk[j+4] = data[(max_data_length*i)+j]; + testing_chunk[j+4] = data[(test_data_length*rounds)+j]; + } + crc_check_length = test_data_length+4; + crc_check = crc(testing_chunk, crc_check_length); + + if(i == 1) { + printf("ATTEMPT: %08X\n", crc_check); } - crc_check = crc(testing_chunk, test_data_length); - if(i == 2) { - printf("THIRD: %08X\n", crc_check); - } else if(i == 1) { - printf("SECOND: %08X\n", crc_check); - } if ((crc_check >> (8*3)) != 10 ) { free(testing_chunk); return 1; } + // Used as an alternative to i + // Since i needs to be changed it max data length is met + rounds++; } free(testing_chunk); - printf("FOUND: %08X\n", crc_check); return 0; } @@ -129,9 +139,10 @@ static int crc_embed_data(unsigned char *data, unsigned int data_length, int bit // Compressing data for test zlib_compress_data(data, data_length, &check_data, &check_data_length); - int match_crc = verify_crc_chunks(check_data, check_data_length, 3, 8192); + int match_crc = verify_crc_chunks(check_data, check_data_length, 1, MAX_IDAT_SIZE); if(match_crc == 0) { + printf("COM SIZE: %zu\n", check_data_length); free(check_data); return 0; } @@ -147,8 +158,9 @@ void *random_data_change_thread_call(void *w) { // Creating temporary data set memcpy(temp_color_data, data->data, data->uncom_data_len); + // Look into testing these values more if(data->uncom_data_len > 800000) { - data->data_len = 150000; + data->data_len = 90000; } else { data->data_len = data->uncom_data_len; } @@ -160,22 +172,23 @@ void *random_data_change_thread_call(void *w) { searching = crc_embed_data(temp_color_data, data->data_len, data->width, data->color_range, data->win_size); - if (searching == 0) { - pthread_mutex_lock(data->mutex_lock); - //pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); + pthread_mutex_lock(&data->mutex_lock); + if (searching == 0 && *data->searching == 1) { *data->searching = searching; memcpy(data->data, temp_color_data, data->uncom_data_len); - //pthread_cancel(data->thread_id); - pthread_mutex_unlock(data->mutex_lock); + pthread_mutex_unlock(&data->mutex_lock); + break; } + pthread_mutex_unlock(&data->mutex_lock); } while(*data->searching == 1); free(temp_color_data); - return NULL; + pthread_exit(0); } void random_data_change(unsigned char *color_data, unsigned char *width, size_t length) { int searching = 1; + int core_count = 16; unsigned int sliding_window = 0; unsigned int compress_data_length = 0; // Needs to be turned into a variable @@ -187,24 +200,31 @@ void random_data_change(unsigned char *color_data, unsigned char *width, size_t }w; memcpy(w.width_array, width, 4); - // Temp data array for crc testing - //unsigned char* temp_color_data = calloc(length, sizeof(unsigned char)); + struct EMBED_THREAD_STRUCT *t_data = malloc(sizeof(struct EMBED_THREAD_STRUCT)); - //int num_threads = 1; - struct EMBED_THREAD_STRUCT t_data; - t_data.searching = &searching; - t_data.data = color_data; - t_data.data_len = compress_data_length; - t_data.uncom_data_len = length; - t_data.width = be32toh(w.width_int); - t_data.color_range = color_range; - t_data.win_size = sliding_window; - pthread_t tid; - t_data.thread_id = tid; - for(int i = 0; i < 28; i++) { - pthread_create(&tid, NULL, random_data_change_thread_call, &t_data); + if(pthread_mutex_init(&t_data->mutex_lock, NULL) != 0) { + printf("Mutex Lock Error\n"); + return; } - pthread_join(tid, NULL); + t_data->searching = &searching; + t_data->data = color_data; + t_data->data_len = compress_data_length; + t_data->uncom_data_len = length; + t_data->width = be32toh(w.width_int); + t_data->color_range = color_range; + t_data->win_size = sliding_window; + pthread_t tid; + t_data->thread_id = tid; + for(int i = 0; i < core_count; i++) { + pthread_create(&tid, NULL, random_data_change_thread_call, t_data); + } + // Waiting for all threads to complete + int waiting = 0; + do { + pthread_join(tid, NULL); + waiting++; + } while(waiting!=core_count); + pthread_mutex_destroy(&t_data->mutex_lock); return; } @@ -227,32 +247,57 @@ void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) { for(int i = 0; i < sizeof(start_data.data); i++) { fputc(start_data.data[i], fp); } - // IDAT Data - for(int i = 0; i < 4; i++) { - fputc(png_file->png_idat_data.idat_length[i], fp); - } - for(int i = 0; i < 4; i++) { - fputc(png_file->png_idat_data.idat_header[i], fp); - } - for(int i = 0; i < be32toh(png_file->png_idat_data.idat_data_length); i++) { - fputc(png_file->png_idat_data.idat_data[i], fp); - } - // Generating CRC - unsigned char full_data[be32toh(png_file->png_idat_data.idat_data_length)+4]; - for(int i = 0; i < 4; i++) { - full_data[i] = png_file->png_idat_data.idat_header[i]; - } - for(int i = 0; i < be32toh(png_file->png_idat_data.idat_data_length); i++) { - full_data[i+4] = png_file->png_idat_data.idat_data[i]; - } - unsigned int int_crc = crc(full_data, be32toh(png_file->png_idat_data.idat_data_length)); - unsigned char new_crc[4]; + // Generating IDAT CHUNKS + int idat_loop = 0; + uint32_t current_len = 0; + do { + // Setting IDAT length chunk variable + if ((be32toh(png_file->png_idat_data.idat_data_length)-(MAX_IDAT_SIZE*idat_loop)) > MAX_IDAT_SIZE) { + current_len = MAX_IDAT_SIZE; + } else { + current_len = (be32toh(png_file->png_idat_data.idat_data_length)-(MAX_IDAT_SIZE*idat_loop)); + } + + // IDAT LENGTH WRITE + for(int i = 0; i < 4; i++) { + //fputc(png_file->png_idat_data.idat_length[i], fp); + fputc(current_len >> (8*(3-i)), fp); + } + // IDAT HEADER WRITE + for(int i = 0; i < 4; i++) { + fputc(png_file->png_idat_data.idat_header[i], fp); + } + // IDAT DATA WRITE + //for(int i = 0; i < be32toh(png_file->png_idat_data.idat_data_length); i++) { + for(int i = 0; i < current_len; i++) { + fputc(png_file->png_idat_data.idat_data[i+(MAX_IDAT_SIZE*idat_loop)], fp); + } + + // Generating CRC + //unsigned char *full_data = malloc(be32toh(png_file->png_idat_data.idat_data_length)+4); + unsigned char *full_data = malloc(current_len+4); + for(int i = 0; i < 4; i++) { + full_data[i] = png_file->png_idat_data.idat_header[i]; + } + for(int i = 0; i < current_len; i++) { + full_data[i+4] = png_file->png_idat_data.idat_data[i+(MAX_IDAT_SIZE*idat_loop)]; + } + + unsigned int int_crc = crc(full_data, current_len+4); + unsigned char new_crc[4]; + + // IDAT CRC WRITE + for(int i = 0; i < 4; i++) { + new_crc[i] = int_crc >> (8*(3-i)) & 0xFF; + fputc(new_crc[i], fp); + } + + // Adding to loop count + printf("Loop: %d\n", idat_loop); + idat_loop++; + } while(idat_loop < (be32toh(png_file->png_idat_data.idat_data_length) / MAX_IDAT_SIZE)); - for(int i = 0; i < 4; i++) { - new_crc[i] = int_crc >> (8*(3-i)) & 0xFF; - fputc(new_crc[i], fp); - } // IEND Data unsigned char IEND_DATA[12] = { 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82}; for(int i = 0; i < 12; i++) { diff --git a/include/crc_util.h b/include/crc_util.h index 49189f0..e97952b 100644 --- a/include/crc_util.h +++ b/include/crc_util.h @@ -34,7 +34,7 @@ struct PNG_FILE_STRUCT { }; struct EMBED_THREAD_STRUCT { - pthread_mutex_t *mutex_lock; + pthread_mutex_t mutex_lock; pthread_t thread_id; int *searching; unsigned char *data; From 1d3c6d321269ba697ed3542e6b5a96ede877bb95 Mon Sep 17 00:00:00 2001 From: Pin Date: Mon, 25 Oct 2021 01:52:32 -0400 Subject: [PATCH 13/17] fixed segfault --- cmd/crc.c | 140 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 100 insertions(+), 40 deletions(-) diff --git a/cmd/crc.c b/cmd/crc.c index 90f8f80..dc501ae 100644 --- a/cmd/crc.c +++ b/cmd/crc.c @@ -13,8 +13,9 @@ #include "crc.h" #include "compress_util.h" -#define MAX_IDAT_SIZE 16384 -//#define MAX_IDAT_SIZE 8192 +//#define MAX_IDAT_SIZE 16384 +#define MAX_WINDOW_SIZE 32768 +#define MAX_IDAT_SIZE 8192 const long idat_signature = 1229209940; const long iend_signature = 1229278788; @@ -77,6 +78,7 @@ static void random_window_bit_change(unsigned char *data, int width, int rounds, random_num = randombytes_uniform(length); } while((random_num % ((width * color_range) + 1)) == 0); + if(data[random_num] == 255) { data[random_num]--; } else { @@ -113,10 +115,6 @@ static int verify_crc_chunks(unsigned char *data, size_t data_length, int crc_de } crc_check_length = test_data_length+4; crc_check = crc(testing_chunk, crc_check_length); - - if(i == 1) { - printf("ATTEMPT: %08X\n", crc_check); - } if ((crc_check >> (8*3)) != 10 ) { free(testing_chunk); @@ -134,7 +132,7 @@ static int crc_embed_data(unsigned char *data, unsigned int data_length, int bit unsigned char *check_data; size_t check_data_length = 0; - random_window_bit_change(data, bit_width, 3, color_range, sliding_window); + random_window_bit_change(data, bit_width, 2, color_range, sliding_window); // Compressing data for test zlib_compress_data(data, data_length, &check_data, &check_data_length); @@ -175,6 +173,11 @@ void *random_data_change_thread_call(void *w) { pthread_mutex_lock(&data->mutex_lock); if (searching == 0 && *data->searching == 1) { *data->searching = searching; + for(size_t i = 0; i < data->uncom_data_len; i++) { + if (temp_color_data[i] != data->data[i]) { + printf("LEN: %zu\nDIFF: %zu\nNEW: %02X\nOLD: %02X\n", data->uncom_data_len, i, temp_color_data[i], data->data[i]); + } + } memcpy(data->data, temp_color_data, data->uncom_data_len); pthread_mutex_unlock(&data->mutex_lock); break; @@ -188,7 +191,7 @@ void *random_data_change_thread_call(void *w) { void random_data_change(unsigned char *color_data, unsigned char *width, size_t length) { int searching = 1; - int core_count = 16; + int core_count = 1; unsigned int sliding_window = 0; unsigned int compress_data_length = 0; // Needs to be turned into a variable @@ -231,6 +234,10 @@ void random_data_change(unsigned char *color_data, unsigned char *width, size_t void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) { FILE *fp; + unsigned char *full_data; + unsigned int int_crc; + unsigned char new_crc[4]; + unsigned char IEND_DATA[12] = { 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82}; fp = fopen(out_file_name, "w"); @@ -276,7 +283,7 @@ void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) { // Generating CRC //unsigned char *full_data = malloc(be32toh(png_file->png_idat_data.idat_data_length)+4); - unsigned char *full_data = malloc(current_len+4); + full_data = malloc(current_len+4); for(int i = 0; i < 4; i++) { full_data[i] = png_file->png_idat_data.idat_header[i]; } @@ -284,8 +291,7 @@ void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) { full_data[i+4] = png_file->png_idat_data.idat_data[i+(MAX_IDAT_SIZE*idat_loop)]; } - unsigned int int_crc = crc(full_data, current_len+4); - unsigned char new_crc[4]; + int_crc = crc(full_data, current_len+4); // IDAT CRC WRITE for(int i = 0; i < 4; i++) { @@ -294,20 +300,41 @@ void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) { } // Adding to loop count - printf("Loop: %d\n", idat_loop); idat_loop++; - } while(idat_loop < (be32toh(png_file->png_idat_data.idat_data_length) / MAX_IDAT_SIZE)); + } while((idat_loop-1) < (be32toh(png_file->png_idat_data.idat_data_length) / MAX_IDAT_SIZE)); // IEND Data - unsigned char IEND_DATA[12] = { 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82}; for(int i = 0; i < 12; i++) { fputc(IEND_DATA[i], fp); } + free(full_data); fclose(fp); } -int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, char *message, int accuracy, unsigned long offset, char *out_file_name) { +unsigned char* populate_idat_array(unsigned char *addr, unsigned long offset, size_t *idat_byte_length) { + size_t size = 8; + //size_t idat_byte_length = 0; size_t idat_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 < total_idat(addr); i++) { + idat_length = check_header_length(addr, offset); + for(size_t j = 0; j < idat_length; j++) { + if(*idat_byte_length == size) { + size *= 2; + idat_data = reallocarray(idat_data, size, sizeof(unsigned char)); + } + idat_data[*idat_byte_length] = addr[offset+8+j]; + *idat_byte_length += 1; + } + // Offset 3 bytes for 2 length fields and 1 crc + offset = offset + idat_length + 12; + } + return idat_data; +} + +int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, char *message, int accuracy, unsigned long offset, char *out_file_name) { if(accuracy > 4) { printf("Warning, accuracy cannot be larger than 4"); return EXIT_FAILURE; @@ -320,23 +347,8 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, c exit(EXIT_FAILURE); } - size_t size = 8; size_t idat_byte_length = 0; - unsigned char* idat_data = calloc(size, sizeof(unsigned char)); - for(size_t i = 0; i < total_idat(addr); i++) { - idat_length = check_header_length(addr, offset); - for(size_t j = 0; j < idat_length; j++) { - if(idat_byte_length == size) { - size *= 2; - idat_data = reallocarray(idat_data, size, sizeof(unsigned char)); - } - idat_data[idat_byte_length] = addr[offset+8+j]; - idat_byte_length++; - } - // Offset 3 bytes for 2 length fields and 1 crc - offset = offset + idat_length + 12; - - } + unsigned char *idat_data = populate_idat_array(addr, offset, &idat_byte_length); unsigned char* temp_idat_data = calloc(idat_byte_length, sizeof(unsigned char)); memcpy(temp_idat_data, idat_data, idat_byte_length); @@ -346,6 +358,8 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, c size_t uncom_data_size = 0; zlib_decompress_data(temp_idat_data, idat_byte_length, &uncom_data_buff, &uncom_data_size); + printf("ORIG UNCOM LEN: %zu\n", uncom_data_size); + // Start data testing random_data_change(uncom_data_buff, png_file->png_start_data.file_width, uncom_data_size); @@ -355,6 +369,8 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, c size_t com_data_size = 0; zlib_compress_data(uncom_data_buff, uncom_data_size, &com_data_buff, &com_data_size); + printf("COM LEN: %zu\n", com_data_size); + png_file->png_idat_data.idat_data = calloc(com_data_size, sizeof(unsigned char)); png_file->png_idat_data.idat_data_length = be32toh(com_data_size); @@ -381,6 +397,8 @@ int main(int argc, char **argv) { FILE *fp; size_t i = 0; unsigned long offset = 0; + int uncompress_call = 0; + int compress_call = 0; struct PNG_FILE_STRUCT png_file_data; char *in_file_name = NULL; char *out_file_name = NULL; @@ -391,11 +409,14 @@ int main(int argc, char **argv) { {"file", required_argument, NULL, 'f'}, {"outfile", required_argument, NULL, 'o'}, {"message", required_argument, NULL, 'm'}, + {"compress", no_argument, NULL, 'c'}, + {"uncompress", no_argument, NULL, 'h'}, {0, 0, 0, 0} }; const char* usage = "Usage: crc [options]\n" + " -c --compress Compress Message\n" " -h, --help Shows help message\n" " -f, --file Denotes input file\n" " -o, --outfile Denotes output file\n" @@ -405,7 +426,7 @@ int main(int argc, char **argv) { int c; while (1) { int option_index = 0; - c = getopt_long(argc, argv, "hf:o:m:", long_options ,&option_index); + c = getopt_long(argc, argv, "hf:o:m:uc", long_options ,&option_index); if(c == -1) { break; } @@ -422,18 +443,28 @@ int main(int argc, char **argv) { case 'm': message = optarg; break; + case 'c': + compress_call = 1; + break; + case 'u': + uncompress_call = 1; + break; } } if(in_file_name == NULL) { printf("Input file required!\n"); exit(EXIT_FAILURE); - } else if(out_file_name == NULL) { - printf("Output file required!\n"); - exit(EXIT_FAILURE); - } else if(message == NULL) { - printf("Message required!\n"); - exit(EXIT_FAILURE); + } + + if(compress_call == 1) { + if(out_file_name == NULL) { + printf("Output file required!\n"); + exit(EXIT_FAILURE); + } else if(message == NULL) { + printf("Message required!\n"); + exit(EXIT_FAILURE); + } } if(sodium_init() == -1) { @@ -446,6 +477,7 @@ int main(int argc, char **argv) { exit(EXIT_FAILURE); } + unsigned char *file_data = file_to_char_array(fp, &i); fclose(fp); @@ -453,9 +485,37 @@ int main(int argc, char **argv) { offset = first_idat(file_data); - populate_idat_png(file_data, &png_file_data.png_idat_data, offset); + if(uncompress_call == 1) { + size_t idat_byte_length = 0; + unsigned char* idat_data = populate_idat_array(file_data, offset, &idat_byte_length); + + for(int i = 0; i < idat_byte_length; i++) { + printf("%02X ", idat_data[i]); + } + printf("\nDecompressed Data:\n\n"); + // Decompressing data + unsigned char *uncom_data_buff = NULL; + size_t uncom_data_size = 0; + zlib_decompress_data(idat_data, idat_byte_length, &uncom_data_buff, &uncom_data_size); + for(int i = 0; i < uncom_data_size; i++) { + printf("%02X ", uncom_data_buff[i]); + } + printf("\n"); - change_idat_content(file_data, &png_file_data, message, 1, offset, out_file_name); + free(idat_data); + free(file_data); + return EXIT_SUCCESS; + } + + if(compress_call == 1) { + populate_idat_png(file_data, &png_file_data.png_idat_data, offset); + + change_idat_content(file_data, &png_file_data, message, 1, offset, out_file_name); + + free(file_data); + return EXIT_SUCCESS; + } free(file_data); + return EXIT_FAILURE; } From e0c035a4cf763854c1e130c9da26241391d9b8a9 Mon Sep 17 00:00:00 2001 From: Pin Date: Wed, 27 Oct 2021 21:30:41 -0400 Subject: [PATCH 14/17] added support for multi-idat --- cmd/crc.c | 88 ++++++++++++++++++++++++++++++++-------------- include/crc_util.h | 3 ++ 2 files changed, 64 insertions(+), 27 deletions(-) diff --git a/cmd/crc.c b/cmd/crc.c index dc501ae..98d4f4c 100644 --- a/cmd/crc.c +++ b/cmd/crc.c @@ -70,12 +70,11 @@ int update_file_crc(unsigned char *addr, unsigned long offset , unsigned int crc return 0; } -static void random_window_bit_change(unsigned char *data, int width, int rounds, int color_range, size_t length) { +static void random_window_bit_change(unsigned char *data, int width, int rounds, int color_range, size_t length, size_t offset) { size_t random_num = 0; for (int i = 0; i < rounds; i++) { - random_num = randombytes_uniform(length); do { - random_num = randombytes_uniform(length); + random_num = randombytes_uniform(length) + offset; } while((random_num % ((width * color_range) + 1)) == 0); @@ -88,7 +87,7 @@ static void random_window_bit_change(unsigned char *data, int width, int rounds, return; } -static int verify_crc_chunks(unsigned char *data, size_t data_length, int crc_depth, unsigned int max_data_length) { +static int verify_crc_chunks(unsigned char *data, size_t data_length, int crc_depth, unsigned int max_data_length, char message, size_t iteration) { unsigned char* testing_chunk; unsigned int crc_check = 0; unsigned int test_data_length = 0; @@ -111,15 +110,16 @@ static int verify_crc_chunks(unsigned char *data, size_t data_length, int crc_de testing_chunk[2] = 0x41; testing_chunk[3] = 0x54; for(int j = 0; j < test_data_length; j++) { - testing_chunk[j+4] = data[(test_data_length*rounds)+j]; + testing_chunk[j+4] = data[(test_data_length*rounds)+j+(MAX_WINDOW_SIZE*iteration)]; } crc_check_length = test_data_length+4; crc_check = crc(testing_chunk, crc_check_length); - if ((crc_check >> (8*3)) != 10 ) { + if ((crc_check >> (8*3)) != message ) { free(testing_chunk); return 1; } + printf("FOUND: %c\n", message); // Used as an alternative to i // Since i needs to be changed it max data length is met rounds++; @@ -128,16 +128,16 @@ static int verify_crc_chunks(unsigned char *data, size_t data_length, int crc_de return 0; } -static int crc_embed_data(unsigned char *data, unsigned int data_length, int bit_width, int color_range, unsigned int sliding_window) { +static int crc_embed_data(unsigned char *data, unsigned int data_length, int bit_width, int color_range, unsigned int sliding_window, char message, size_t offset , size_t iteration) { unsigned char *check_data; size_t check_data_length = 0; - random_window_bit_change(data, bit_width, 2, color_range, sliding_window); + random_window_bit_change(data, bit_width, 2, color_range, sliding_window, offset); // Compressing data for test zlib_compress_data(data, data_length, &check_data, &check_data_length); - int match_crc = verify_crc_chunks(check_data, check_data_length, 1, MAX_IDAT_SIZE); + int match_crc = verify_crc_chunks(check_data, check_data_length, 1, MAX_IDAT_SIZE, message, iteration); if(match_crc == 0) { printf("COM SIZE: %zu\n", check_data_length); @@ -151,6 +151,7 @@ static int crc_embed_data(unsigned char *data, unsigned int data_length, int bit void *random_data_change_thread_call(void *w) { struct EMBED_THREAD_STRUCT *data = w; unsigned char* temp_color_data = calloc(data->uncom_data_len, sizeof(unsigned char)); + char cur_message; int searching = 1; do { // Creating temporary data set @@ -158,21 +159,24 @@ void *random_data_change_thread_call(void *w) { // Look into testing these values more if(data->uncom_data_len > 800000) { - data->data_len = 90000; + data->data_len = 90000 + (90000 * data->cur_iteration); } else { data->data_len = data->uncom_data_len; } if(data->uncom_data_len > 16000) { - data->win_size = 9000; + data->win_size = 9000 + (9000 * data->cur_iteration); } else { data->win_size = data->uncom_data_len; } - searching = crc_embed_data(temp_color_data, data->data_len, data->width, data->color_range, data->win_size); + cur_message = data->message[data->cur_iteration]; + + searching = crc_embed_data(temp_color_data, data->data_len, data->width, data->color_range, data->win_size, cur_message, data->cur_offset, data->cur_iteration); pthread_mutex_lock(&data->mutex_lock); if (searching == 0 && *data->searching == 1) { *data->searching = searching; + printf("CUR MESSAGE: %c\n", cur_message); for(size_t i = 0; i < data->uncom_data_len; i++) { if (temp_color_data[i] != data->data[i]) { printf("LEN: %zu\nDIFF: %zu\nNEW: %02X\nOLD: %02X\n", data->uncom_data_len, i, temp_color_data[i], data->data[i]); @@ -189,13 +193,32 @@ void *random_data_change_thread_call(void *w) { pthread_exit(0); } -void random_data_change(unsigned char *color_data, unsigned char *width, size_t length) { +size_t generate_offset(unsigned char *data, size_t data_len, size_t iteration) { + unsigned char *com_data_buff = NULL; + unsigned char *uncom_data_buff = NULL; + size_t com_data_size = 0; + size_t uncom_data_size = 0; + + printf("Gen Offset\n"); + zlib_compress_data(data, data_len, &com_data_buff, &com_data_size); + + zlib_decompress_data(com_data_buff, (MAX_WINDOW_SIZE * (iteration+1)), &uncom_data_buff, &uncom_data_size); + + printf("NEW SIZE: %zu\n", uncom_data_size); + + free(com_data_buff); + free(uncom_data_buff); + return uncom_data_size; +} + +void random_data_change(unsigned char *color_data, unsigned char *width, size_t length, char *message) { int searching = 1; int core_count = 1; unsigned int sliding_window = 0; unsigned int compress_data_length = 0; // Needs to be turned into a variable int color_range = 3; + int waiting = 0; // Union for width type cast union { uint32_t width_int; @@ -210,25 +233,36 @@ void random_data_change(unsigned char *color_data, unsigned char *width, size_t return; } t_data->searching = &searching; + t_data->message = message; t_data->data = color_data; t_data->data_len = compress_data_length; t_data->uncom_data_len = length; + t_data->cur_offset = 0; t_data->width = be32toh(w.width_int); t_data->color_range = color_range; t_data->win_size = sliding_window; pthread_t tid; t_data->thread_id = tid; - for(int i = 0; i < core_count; i++) { - pthread_create(&tid, NULL, random_data_change_thread_call, t_data); - } - // Waiting for all threads to complete - int waiting = 0; - do { - pthread_join(tid, NULL); - waiting++; - } while(waiting!=core_count); - pthread_mutex_destroy(&t_data->mutex_lock); + for(int j = 0; j < strlen(message); j++) { + // Setting Iteration bit and searching + t_data->cur_iteration = j; + *t_data->searching = 1; + for(int i = 0; i < core_count; i++) { + pthread_create(&tid, NULL, random_data_change_thread_call, t_data); + } + // Waiting for all threads to complete + waiting = 0; + do { + pthread_join(tid, NULL); + waiting++; + } while(waiting!=core_count); + pthread_mutex_destroy(&t_data->mutex_lock); + // Only generate new offset if not last char in message + if(j != (strlen(message) - 1)) { + t_data->cur_offset = generate_offset(color_data, t_data->uncom_data_len, j); + } + } return; } @@ -342,7 +376,7 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, c if(accuracy > 2) { printf("Notice, this could take a long time..."); } - if(total_idat(addr) < strlen((char*)message)) { + if(total_idat(addr) < strlen(message)) { printf("Warning, message exceeds IDAT amount\n"); exit(EXIT_FAILURE); } @@ -361,7 +395,7 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, c printf("ORIG UNCOM LEN: %zu\n", uncom_data_size); // Start data testing - random_data_change(uncom_data_buff, png_file->png_start_data.file_width, uncom_data_size); + random_data_change(uncom_data_buff, png_file->png_start_data.file_width, uncom_data_size, message); //return 0; // Compress Data @@ -410,7 +444,7 @@ int main(int argc, char **argv) { {"outfile", required_argument, NULL, 'o'}, {"message", required_argument, NULL, 'm'}, {"compress", no_argument, NULL, 'c'}, - {"uncompress", no_argument, NULL, 'h'}, + {"uncompress", no_argument, NULL, 'u'}, {0, 0, 0, 0} }; @@ -490,7 +524,7 @@ int main(int argc, char **argv) { unsigned char* idat_data = populate_idat_array(file_data, offset, &idat_byte_length); for(int i = 0; i < idat_byte_length; i++) { - printf("%02X ", idat_data[i]); + //printf("%02X ", idat_data[i]); } printf("\nDecompressed Data:\n\n"); // Decompressing data diff --git a/include/crc_util.h b/include/crc_util.h index e97952b..41ffc40 100644 --- a/include/crc_util.h +++ b/include/crc_util.h @@ -40,9 +40,12 @@ struct EMBED_THREAD_STRUCT { unsigned char *data; unsigned int data_len; size_t uncom_data_len; + size_t cur_offset; + size_t cur_iteration; int width; int color_range; unsigned int win_size; + char *message; }; extern const long png_signature[8]; From 7649feee82293b7c36e1da23b40790b948a068bb Mon Sep 17 00:00:00 2001 From: Pin Date: Sun, 31 Oct 2021 18:57:11 -0400 Subject: [PATCH 15/17] filter support --- cmd/crc.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 10 deletions(-) diff --git a/cmd/crc.c b/cmd/crc.c index 98d4f4c..5466869 100644 --- a/cmd/crc.c +++ b/cmd/crc.c @@ -72,17 +72,60 @@ int update_file_crc(unsigned char *addr, unsigned long offset , unsigned int crc static void random_window_bit_change(unsigned char *data, int width, int rounds, int color_range, size_t length, size_t offset) { size_t random_num = 0; + size_t filter_chunk_byte = 0; + size_t change_num = 0; for (int i = 0; i < rounds; i++) { + random_num = 0; do { random_num = randombytes_uniform(length) + offset; + filter_chunk_byte = (random_num - (random_num % ((width * color_range) + 1))); } while((random_num % ((width * color_range) + 1)) == 0); - - if(data[random_num] == 255) { - data[random_num]--; + if(data[filter_chunk_byte] == 0) { + // None Filter Type Change + if(data[random_num] == 255) { + data[random_num]--; + } else { + data[random_num]++; + } + } else if(data[filter_chunk_byte] == 1) { + // Sub Filter Type Change + if(data[random_num] == 255) { + data[random_num]--; + change_num = 1; + } else { + data[random_num]++; + change_num = -1; + } + // Starting after random change + for(int j = 1; j < 2; j++) { + data[random_num+(color_range * j)] = (data[random_num+(color_range * j)] + change_num) % 256; + } + } else if(data[filter_chunk_byte] == 2) { + printf("2\n"); + if(data[random_num] == 255) { + data[random_num]--; + } else { + data[random_num]++; + } + } else if(data[filter_chunk_byte] == 3) { + printf("3\n"); + if(data[random_num] == 255) { + data[random_num]--; + } else { + data[random_num]++; + } + } else if(data[filter_chunk_byte] == 4) { + printf("4\n"); + if(data[random_num] == 255) { + data[random_num]--; + } else { + data[random_num]++; + } } else { - data[random_num]++; + printf("ERROR ;( %ld\n", filter_chunk_byte); } + } return; } @@ -177,11 +220,6 @@ void *random_data_change_thread_call(void *w) { if (searching == 0 && *data->searching == 1) { *data->searching = searching; printf("CUR MESSAGE: %c\n", cur_message); - for(size_t i = 0; i < data->uncom_data_len; i++) { - if (temp_color_data[i] != data->data[i]) { - printf("LEN: %zu\nDIFF: %zu\nNEW: %02X\nOLD: %02X\n", data->uncom_data_len, i, temp_color_data[i], data->data[i]); - } - } memcpy(data->data, temp_color_data, data->uncom_data_len); pthread_mutex_unlock(&data->mutex_lock); break; @@ -213,7 +251,7 @@ size_t generate_offset(unsigned char *data, size_t data_len, size_t iteration) { void random_data_change(unsigned char *color_data, unsigned char *width, size_t length, char *message) { int searching = 1; - int core_count = 1; + int core_count = 24; unsigned int sliding_window = 0; unsigned int compress_data_length = 0; // Needs to be turned into a variable @@ -535,6 +573,15 @@ int main(int argc, char **argv) { printf("%02X ", uncom_data_buff[i]); } printf("\n"); + //for(int k=0;k<20;k++) { + // printf("%d: %d\n", (0+k), uncom_data_buff[0+k]); + //} + //printf("\n"); + //printf("\n"); + //for(int k=0;k<20;k++) { + // printf("%d: %d\n", (24000+k), uncom_data_buff[24000+k]); + //} + printf("\n"); free(idat_data); free(file_data); From cbee739c910aed1336910fcaf1ecc04153fd5832 Mon Sep 17 00:00:00 2001 From: Pin Date: Tue, 2 Nov 2021 22:42:49 -0400 Subject: [PATCH 16/17] changed support --- Makefile | 12 +++++--- cmd/{crc.c => pspng.c} | 58 +++++++++++++++++++++++++------------- include/{crc.h => pspng.h} | 0 3 files changed, 46 insertions(+), 24 deletions(-) rename cmd/{crc.c => pspng.c} (92%) rename include/{crc.h => pspng.h} (100%) diff --git a/Makefile b/Makefile index d048279..c06d92d 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,17 @@ LIBRARIES = `pkg-config --libs zlib libsodium` -Iinclude -SOURCES = ./src/* ./cmd/crc.c +SOURCES = ./src/* ./cmd/pspng.c OUTPUT_DIR = ./bin OUTPUT = -o ${OUTPUT_DIR}/PROG +INSTALL_OUTPUT = ${OUTPUT_DIR}/PROG build: output_dir - gcc -Wall -pthread ${LIBRARIES} ${SOURCES} ${OUTPUT:PROG=crc} + gcc -Wall -pthread ${LIBRARIES} ${SOURCES} ${OUTPUT:PROG=pspng} debug: output_dir - gcc -Wall -g -pthread ${LIBRARIES} ${SOURCES} ${OUTPUT:PROG=crc} + gcc -Wall -g -pthread ${LIBRARIES} ${SOURCES} ${OUTPUT:PROG=pspng} + +install: + mv ${INSTALL_OUTPUT:PROG=pspng} /usr/bin/ output_dir: mkdir -p ${OUTPUT_DIR} @@ -16,4 +20,4 @@ clean: rm -rf $(OUTPUT_DIR) **.h.gch memcheck: debug - valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes -s ${OUTPUT_DIR}/crc + valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes -s ${OUTPUT_DIR}/pspng diff --git a/cmd/crc.c b/cmd/pspng.c similarity index 92% rename from cmd/crc.c rename to cmd/pspng.c index 5466869..6d69b2d 100644 --- a/cmd/crc.c +++ b/cmd/pspng.c @@ -10,7 +10,7 @@ #include #include "crc_util.h" #include "CRCLib.h" -#include "crc.h" +#include "pspng.h" #include "compress_util.h" //#define MAX_IDAT_SIZE 16384 @@ -74,6 +74,7 @@ static void random_window_bit_change(unsigned char *data, int width, int rounds, size_t random_num = 0; size_t filter_chunk_byte = 0; size_t change_num = 0; + long prior_chuck_check = 0; for (int i = 0; i < rounds; i++) { random_num = 0; do { @@ -109,7 +110,15 @@ static void random_window_bit_change(unsigned char *data, int width, int rounds, data[random_num]++; } } else if(data[filter_chunk_byte] == 3) { - printf("3\n"); + // Average Filter Type Change + //printf("HEE: %d\n", data[(filter_chunk_byte - (width * color_range))-1]); + prior_chuck_check = filter_chunk_byte; + do { + //printf("C: %d\n", data[prior_chuck_check]); + prior_chuck_check = (prior_chuck_check - (width * color_range)-1); + //printf("CH: %ld\n", prior_chuck_check); + } while(prior_chuck_check >= 0); + if(data[random_num] == 255) { data[random_num]--; } else { @@ -487,8 +496,9 @@ int main(int argc, char **argv) { }; const char* usage = - "Usage: crc [options]\n" + "Usage: pspng [options]\n\n" " -c --compress Compress Message\n" + " -u --uncompress Currently used for debugging\n" " -h, --help Shows help message\n" " -f, --file Denotes input file\n" " -o, --outfile Denotes output file\n" @@ -559,29 +569,37 @@ int main(int argc, char **argv) { if(uncompress_call == 1) { size_t idat_byte_length = 0; + unsigned long cur_idat_len = 0; + //unsigned char message_data[8192]; unsigned char* idat_data = populate_idat_array(file_data, offset, &idat_byte_length); - - for(int i = 0; i < idat_byte_length; i++) { + + //for(int i = 0; i < idat_byte_length; i++) { //printf("%02X ", idat_data[i]); - } + //} printf("\nDecompressed Data:\n\n"); // Decompressing data - unsigned char *uncom_data_buff = NULL; - size_t uncom_data_size = 0; - zlib_decompress_data(idat_data, idat_byte_length, &uncom_data_buff, &uncom_data_size); - for(int i = 0; i < uncom_data_size; i++) { - printf("%02X ", uncom_data_buff[i]); - } - printf("\n"); - //for(int k=0;k<20;k++) { - // printf("%d: %d\n", (0+k), uncom_data_buff[0+k]); + //unsigned char *uncom_data_buff = NULL; + //size_t uncom_data_size = 0; + //zlib_decompress_data(idat_data, idat_byte_length, &uncom_data_buff, &uncom_data_size); + //for(int i = 0; i < uncom_data_size; i++) { + // printf("%02X ", uncom_data_buff[i]); //} //printf("\n"); - //printf("\n"); - //for(int k=0;k<20;k++) { - // printf("%d: %d\n", (24000+k), uncom_data_buff[24000+k]); - //} - printf("\n"); + + do { + for(int j = 0; j < 64; j++) { + cur_idat_len = 0; + for(int i = 0; i < 4; i++) { + cur_idat_len += (file_data[offset+i] << (24-(8*i))); + } + //printf("%c", file_data[offset+cur_idat_len+8]); + if((j % 4) == 0) { + printf("%c", file_data[offset+cur_idat_len+8]); + } + // Offset Plus idat length + 3 bytes + offset += cur_idat_len + 12; + } + } while(offset < idat_byte_length); free(idat_data); free(file_data); diff --git a/include/crc.h b/include/pspng.h similarity index 100% rename from include/crc.h rename to include/pspng.h From b4e42a75b5289a4fedff9cab6506f0febab312e7 Mon Sep 17 00:00:00 2001 From: Pin Date: Tue, 2 Nov 2021 23:45:57 -0400 Subject: [PATCH 17/17] wip --- cmd/pspng.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/pspng.c b/cmd/pspng.c index 6d69b2d..59c8699 100644 --- a/cmd/pspng.c +++ b/cmd/pspng.c @@ -497,12 +497,12 @@ int main(int argc, char **argv) { const char* usage = "Usage: pspng [options]\n\n" - " -c --compress Compress Message\n" - " -u --uncompress Currently used for debugging\n" - " -h, --help Shows help message\n" - " -f, --file Denotes input file\n" - " -o, --outfile Denotes output file\n" - " -m, --message Encoded message\n" + " -c --compress Compress Message\n" + " -u --uncompress Currently used for debugging\n" + " -h, --help Shows help message\n" + " -f, --file Denotes input file\n" + " -o, --outfile Denotes output file\n" + " -m, --message Encoded message\n" "\n"; int c;