#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; 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; 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'; if(sodium_init() == -1) { return EXIT_FAILURE; } 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); 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); //create_cc_file(file_data, i); }