-1

I want to hide strings in my executable binary because it's really easy to hack. So I want to do some encoding during compile time

I want to change string at compile time, for example if i have a string

a = "abc"

and i want to change a into

a = "bcd" (plus '\1' to every element of the string array).

Even more, i want to change a into SHA256(a) during compile time, is it possible?

demonguy
  • 1,977
  • 5
  • 22
  • 34
  • 3
    You use a text editor, position the cursor just past the last `c`, hit backspace 3 times and then type `bcd`. Seriously, what do you mean "during compile time?" You can't modify your source "during compile time". You can generate source code using whatever tool you want, that writes data to a *.c file. – Mark Lakata Jul 09 '15 at 05:19
  • Why do you ask, what is the use case? Please edit your question to improve it. – Basile Starynkevitch Jul 09 '15 at 08:50
  • Your question is really unclear. Please **edit your question** to motivate it and give a real example.... – Basile Starynkevitch Jul 09 '15 at 08:59
  • I don't know what confuse you, I just want to write a compile function with which I can convert string without lowing the code readability – demonguy Jul 09 '15 at 09:40
  • You should explain **why** you are asking. Smells a lot like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Basile Starynkevitch Jul 09 '15 at 11:16
  • I just want to hide string in my executable binary file. It will be easily hacked. And I don't want to lose the code readability – demonguy Jul 09 '15 at 11:45
  • Very probably you won't (should not, cannot so don't want) to hide *every* string, only some of them. – Basile Starynkevitch Jul 09 '15 at 11:49
  • Yes, only some strings – demonguy Jul 09 '15 at 11:51

1 Answers1

1

You cannot do easily that, but you could edit every source file to change every occurrence of your strings. If using emacs as your editor, you could code some Emacs Lisp code to automate that editing.

Of course, you can generate your C code from something else, e.g. using some other preprocessor (gpp, m4) and or some (e.g. awk) script.

You might consider adopting the convention that every such string is used in unique macro, e.g. MY_STRANGE_STRING("abc") instead of simply "abc"; then you might use some preprocessor macro tricks -concatanation, __LINE__, perhaps stringification- .... (perhaps using gpp preprocessor to generate some header, etc...)

A possibility could be to process your source twice. First by some gpp or m4 preprocessing which would transform MY_STRANGE_STRING("abc") occurring at line 123 into a #define MY_STRING_CONSTANT_123 "cde" in some generated header. Then the C compiler would use ordinary preprocessing tricks to get that.

You could also transform the preprocessed form of your source code by an ad-hoc tool or script.

If compiling with recent GCC (specifically) you could use MELT to customize your compiler to do such tricks. You'll then insert some optimization passes to do the transformation. You might define the MY_STRANGE_STRING to be your application specific __my_strange_string_builtin and add that additional builtin into GCC, etc...

Notice that you probably don't want to transform every literal strings (e.g. you probably don't want to transform the literal control string of most fprintf calls..., or the expansion of __FILE__ or __DATE__ which is happening in some system macros, e.g. assert), but only some of them.

I cannot help more, since you don't motivate your question.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547