Here's a minimal test case:
#include <stdio.h>
#include <stdlib.h>
int main ( int argc , char **argv )
{
const char abc [15] = "abcdefg\0";
printf ("%s\n" , abc);
return 0;
}
And you do strings test
, you should see abcdefg
, as it's stored in read only area.
So , what's the best way to prevent user from reading this string , with "strings" command , e.g I don't want users to know my SQL phrase
One solution would be to write an additional program that runs as another user, and read credentials from a location where it is not accessible by users you want to protect credentials from. This program would expose an API (through TCP/IP or any message passing interface or remote procedure call) that do not need to connect to the database directly, but responds only to requests you're interested in.
Another approach is to set the setuid
bit on your program, and read credentials from a location where users have no read access. Give the program an owner that is allowed to read the file containing the query, using chown
. When executed, your program will obtain privileges to read the file.
Like said in Nawaz answer (and Binyamin Sharet), you could use obfuscation techniques to make it harder to read the query (in particular, it would not work with strings
anymore), but keep in mind that someone with more knowledge will be able to find the string using a deassembler or a debugger, or simply by running your program in strace
.
It makes this approach unsuitable to store sensitive information, like connection credentials: as long as a binary can connect, it contains credential, anyone with some knowledge in computer security know that and may reverse engineer your program to retrieve your password.
As a general guideline, if you need to protect information from a user executing your program, never give this information to the program. It is the only way to make sure it can't be read.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With