![]() |
|
[C] My file crypter has no trouble crypting text file - but fails at binaries? - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] My file crypter has no trouble crypting text file - but fails at binaries? (/showthread.php?tid=6005068) |
My file crypter has no trouble crypting text file - but fails at binaries? - pebblak - Sep 4, 2019 Hey all. I'm really new to C and crypting binaries for that matter so I'm probably missing something obvious. The issue I'm having is, my file crypter can encrypt and decrypt sample text files with no problem (files containing a line of text), however when tried on a binary, the decrypted binary throws this: Code ./unencrypted_test_program: line 1: $'\177ELF\002\001\001': command not foundThe plain binary is, in fact, 12 KB in size. The encrypted version is 7 bytes, and so is the decrypted version of the encrypted binary. Naturally this doesn't make sense, because I'm using a simple XOR. Here is my code: Code int xorFile(char *infile, char *outfile){UPDATE: So I figured out the problem. My xorString() function looks like this: Code void xorString (char *s){Which means that when it encounters a 00 byte it stops XORing. How do I in that case make the loop stop at the end of the file without using a 0 byte to do this? I'm at a loss. RE: My file crypter has no trouble crypting text file - but fails at binaries? - hadesgod7979 - Sep 4, 2019 Hello, corrected code: void xorString (char * s, int nLen) { for ( int i = 0; i < nLen; i ++) s[i] ^= XOR_BYTE; } int xorFile(char *infile, char *outfile){ FILE *fp; FILE *fp2; char buf[1025] = {0}; fp = fopen(infile,"rb"); if (fp == NULL) return 0; fp2 = fopen(outfile,"wb"); if (fp2 == NULL) return 0; while(!feof(fp)) { int nRead = fread(buf,1, 1024,fp); xorString(buf, nRead); } fclose(fp); fclose(fp2); return 1; } RE: My file crypter has no trouble crypting text file - but fails at binaries? - myselfandi - Sep 5, 2019 (Sep 4, 2019 11:31 AM)pebblak Wrote: Hey all. I'm really new to C and crypting binaries for that matter so I'm probably missing something obvious. You need tot get the size of the file first, So something like that would do Code static long GetFileSize(FILE* f)and your xor function should like like this Code void xorString (char *s, const size_t size){Code long fsize = GetFileSize(f);Code char* buf = (char*)malloc(fsize *sizeof(char));RE: My file crypter has no trouble crypting text file - but fails at binaries? - HFDone - Sep 5, 2019 you need to read the file bytes to unsigned char pointer RE: My file crypter has no trouble crypting text file - but fails at binaries? - pebblak - Sep 8, 2019 Hey, thanks for the responses. This is my modified code: Code static long GetFileSize(FILE* f)The original test binary I'm testing this on is 12KB in size. After running this, the "crypted" version is 4KB. As you can see I added a couple debug statements to see what the size of 'buf' is at two different points. This size prints out as '8'. I don't know what to make of this. The code clearly isn't working. I have no idea why. RE: My file crypter has no trouble crypting text file - but fails at binaries? - jebb - Sep 9, 2019 fp = fopen(infile,"r"); change the "r" to "rb". The b stands for binary. RE: My file crypter has no trouble crypting text file - but fails at binaries? - Joseph Ducreux - Oct 15, 2019 huge difference between text strings and binary data, you will need to read exe or dat byte by byte it into a byte array |