Search code examples
cgccstaticmsys2

Hiding strings from strings tool in c language


The goal is to declare char arrays that are not show when using a tool like strings

The approaches

#include <stdio.h>

// method 1 
#define PRIVATE __attribute__((visibility("hidden")))
#define PUBLIC __attribute__((visibility("default")))

PUBLIC char visible_msg[] = "can you see me";
PRIVATE char not_visible[] = "this is hidden ";

// method 2
static char secret[] = "1337h4x";

int main()
{
    printf("%s\n", secret);
    printf("%s\n", visible_msg);
    printf("%s\n", not_visible);
}

method 2 work fine on linux but on windows this is useless code and I get the warning visibility attribute not supported in this configuration; ignored [-Wattributes] and i tried to use -visibility=hidden but this does not have an effect and i can still dump the strings

I am compiling using MINGW64 in msys2


Solution

  • The approaches

    Neither of these approaches can work reliably -- the compiler will still put a string literal into the read-only section of the binary (at least for sufficiently long strings).

    Here is an approach which hides the string from strings:

    #include <stdio.h>
    
    int main()
    {
      char letters[] = "abcdefghijklmnopqrstuvwxyz";
    
      // "this is hidden " in ASCII
      // 74 68 69 73 20 69 73 20 68 69 64 64 65 6e 20
      int offsets[] = { 0x74 - 'a', 0x68 - 'a', 0x69 - 'a', 0x73 - 'a',
                        0x20 - 'a', 0x69 - 'a', 0x73 - 'a', 0x20 - 'a',
                        0x68 - 'a', 0x69 - 'a', 0x64 - 'a', 0x64 - 'a',
                        0x65 - 'a', 0x6e - 'a', 0x20 - 'a'
      };
    
      for (int j = 0; j < sizeof(offsets) / sizeof(offsets[0]); j++) {
        const int offset = offsets[j];
        if (offset < 0)
          putc(' ', stdout);
        else
          putc(letters[offset], stdout);
      }
      puts("");
      return 0;
    }
    

    You can re-order letters[] and/or remove unused letters to make it even more obscure.