1

I'm trying to make a simple encryption type of stuff. So what I wanna do is read an executable's contents, encrypt it and generate a header file which will contain a variable with the encrypted bytes/binaries, then it will decrypt it etc. So the question is how can I export that encrypted stuff onto a header file. Because for example if you try to print a byte representation of the contents you can do it with

printf("%x", byte);

But I don't think that you can use that kind of format to store the bytes in an unsigned char, since the usual format is

unsigned char bytes[] = {0x010, 0x038, 0x340 etc...}

In Python I can do it, but I can't seem to figure it out how to do it directly in C.

If you have recommendations of sources, please share them.

I'm trying to focus on Windows Executables at the moment, most likely I'll try to execute the binary code on a Virtually Allocated Memory, I've seen some code that does it, so I wanna try doing it myself.

3
  • 1
    To re-phrase and simplify it: You have binary data in your C program which you want to write out in the format of a C header file? Commented Jul 4, 2020 at 16:28
  • I read binary data from an executable file with the standard IO (fopen, fread) onto an unsigned char variable, I wanna encrypt then output that variable onto a C header format, so that I can access it on another C program and decrypt it. Commented Jul 4, 2020 at 16:41
  • 1
    OK, I assume reading the data is clear then it is just about writing e.g. `{ 0xFF, 0xFF, ... }'? Commented Jul 4, 2020 at 16:45

2 Answers 2

2

Do you want something like that :

#include <stdio.h>

int encode(int c)
{
  return (unsigned char) (c ^ 0xf);
}

int main(int argc, char ** argv)
{
  if (argc != 3) {
    fprintf(stderr, "usage: %s <file in> <file out>\n", *argv);
  }
  else {
    FILE * fpin;
    FILE * fpout;
    
    if ((fpin = fopen(argv[1], "rb")) == NULL) /* under Windows 'b' is necessary to read binary */
      perror("cannot open inpout file");
    else if ((fpout = fopen(argv[2], "w")) == NULL)
      perror("cannot open inpout file");
    else {
      const char * sep = "unsigned char bytes[] = {";
      int c;
     
      while ((c = fgetc(fpin)) != EOF) {
        fprintf(fpout, "%s0x%x", sep, encode(c));
        sep = ", ";
      }
      
      fputs("};\n", fpout);
      fclose(fpin);
      fclose(fpout);
    }
  }
  
  return 0;
}

of course modifying encode

Compilation and execution :

pi@raspberrypi:/tmp $ gcc -Wall e.c
pi@raspberrypi:/tmp $ ./a.out ./a.out h
pi@raspberrypi:/tmp $ cat h
unsigned char bytes[] = {0x70, 0x4a, 0x43, 0x49, 0xe, 0xe, 0xe, 0xf ... 0xf, 0xf, 0xf, 0xf, 0xf};
pi@raspberrypi:/tmp $ ls -l h
-rw-r--r-- 1 pi pi 43677 juil.  4 18:44 h

(I cut cat h result to only show its begin and end)

Sign up to request clarification or add additional context in comments.

18 Comments

@СелятинИсмет c ^0xf is just the xor between the byte and 15, I use it as example, I do not know what kind of encoding you want, xor is commutative to decode you can do again value ^0xf
@СелятинИсмет but if the goal is to have something secret do not use that xor ^^
Beautify header: Use %02x.
@reichhart I am not sure the goal is to read the content, I wanted to show the result for the small program int main() { return 0;} but even for it the executable size using gcc is 7908 so the generated header has 41433 characters ! ^^
@bruno In this case you should of course avoid to beautify, we don't want to increase the bloat even more. ;-) (I actually never thought about how much the size would be multiplicated by simply "asciifying" data. :-O)
|
2

Quick and dirty, unsafe and untested. Reads the file defined in INPUT_FILE, and outputs it to OUTPUT_FILE in the format of: unsigned char var[] = { 0xXX, 0xXX ... }; The name of the variable is controlled by VARIABLE_NAME. You should add your own sanity checks, i.e. check the returns from fopen() and the likes.

#include <stdio.h>
#include <stdlib.h>

#define INPUT_FILE "file.exe"
#define OUTPUT_FILE "out.txt"
#define VARIABLE_NAME "bytes"

int main(int argc, char *argv[]) {
    FILE *fp = fopen(INPUT_FILE, "rb");

    // Get file size
    fseek(fp, 0, SEEK_END);
    long size = ftell(fp);
    fseek(fp, 0, SEEK_SET);

    // Alloc, read
    unsigned char *buf = malloc(size);
    fread(buf, size, 1, fp);
    fclose(fp);

    // Write the data out
    fp = fopen(OUTPUT_FILE, "wb");
    fprintf(fp, "unsigned char %s[] = { ", VARIABLE_NAME);
    for (long i = 0; i < size; i++) {
        fprintf(fp, "0x%02x%s", buf[i], (i == size-1) ? " };" : ", ");
    }
    fclose(fp);
    free(buf);
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.