Hide literal strings in compiled exe

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Hide literal strings in compiled exe

Post by MachineCode »

Quite often I find myself wanting to hide my strings in the final compiled executable, such as web addresses and such. I think it would be good if PureBasic would encrypt or hide them when creating the executable, so prying eyes with a hex editor can't take a peek around your app.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Hide literal strings in compiled exe

Post by c4s »

You could create encrypted strings yourself in a data section and decode/use them as needed. Remember that this is just a really basic hiding mechanism. Everyone with advanced knowledge knows how to retrieve any string out of your executable...
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Hide literal strings in compiled exe

Post by STARGÅTE »

Hide ?
String which is to use the EXE must be in the EXE, where else?

encrypt ?
see post from c4s
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Hide literal strings in compiled exe

Post by MachineCode »

I know I can encrypt strings in my app's code, but I'm talking about NO changes to my source code. I did actually write a pre-processor that does it, but natively would be nicer. Using my pre-processor, if I compile the following single command to an exe, the word "HIDDEN" can't be found in the exe with a hex editor. I'm not interested in stopping real crackers from finding the strings; I just want to stop people using a hex editor to take a look and to stop them modifying it with the hex editor (it's happened to me in the past).

Code: Select all

MessageRequester("","HIDDEN")
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Hide literal strings in compiled exe

Post by c4s »

MachineCode wrote:I just want to stop people using a hex editor to take a look and to stop them modifying it with the hex editor (it's happened to me in the past).
Happend to me as well. Actually it was a kind chinese guy who translated my freeware program into chinese by just replacing the english strings using an hex editor. :shock: At least he asked me afterwards if I allow him to release it... :)
Well, this was quite a "shock" to me so I created a little preprocessor that stores all strings in an encrypted file which I'm decrypting into my string array at program start-up. Anyway, I don't think PureBasic should do this.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
Didelphodon
PureBasic Expert
PureBasic Expert
Posts: 450
Joined: Sat Dec 18, 2004 11:56 am
Location: Vienna - Austria
Contact:

Re: Hide literal strings in compiled exe

Post by Didelphodon »

As a quick shot you can pack your executables with UPX. Though that's the most easiest hurdle to take for crackers (UPX is easily revertable by UPX itself) this step would prevent hex-viewers of finding your strings and you further won't have to change your sourcecode.

Cheers, Didel.
Go, tell it on the mountains.
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Hide literal strings in compiled exe

Post by MachineCode »

It's easy to stop UPX being reversed (just replace "UPX" in the exe with random bytes), but as you said, it doesn't stop real crackers. Maybe PureBasic could have some sort of "secure compilation" mode, where it encrypts such strings and has some other basic protections. Not to stop crackers, but to stop casual users from exploring the exe and ripping stuff out of it. I guess I know the answer already, though. Would anyone here be interested if I updated my pre-processor to do that sort of thing?
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Hide literal strings in compiled exe

Post by Shield »

Sounds interesting to hide a string from the average user's eyes. :)
But keep in mind that such methods shouldn't be used for sensitive data such as passwords.
As soon as your application starts working with those strings after decrypting them, the data can be read
form memory without any problems.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Hide literal strings in compiled exe

Post by MachineCode »

Shield wrote:such methods shouldn't be used for sensitive data such as passwords
No, of course not. In my case, I'm more concerned about someone changing the URL of my website in my exe, to a different URL of their choice, and getting payment from my app instead of me. It's happened to me before!
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Hide literal strings in compiled exe

Post by Tenaja »

MachineCode wrote:
Shield wrote:such methods shouldn't be used for sensitive data such as passwords
No, of course not. In my case, I'm more concerned about someone changing the URL of my website in my exe, to a different URL of their choice, and getting payment from my app instead of me. It's happened to me before!
The "simplest" way is just "encrypt" it yourself with xors and/or adds & shifts. That will prevent a simple hex editor, but not a debugger. Someone else also suggested zipping the text string file into the exe so you have to unzip them to use it, but I didn't bookmark it.
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Hide literal strings in compiled exe

Post by Tenaja »

This works for short & minimal text:
; set up a chart with all characters. I had to add 'my' because pb has some single-character constants predefined.

#myH = 'h' ! '1'
#myE = 'e' ! '1'
#myY = 'y' ! '1'


#Test = Chr(#myH ) + Chr(#myE ) + Chr(#myY) ;test string

Procedure.s ReXor(s.s)
x.i = 1
y.i = Len(s)
new.s = ""
Repeat
new = new + Chr(Asc(Mid(s, x, 1)) ! '1')
y-1
x+1
Until y = 0
ProcedureReturn new
EndProcedure

Debug ReXor(#Test)

I know there's a better way, but I just tossed this together as a starting point. Better would be to use a rolling key.
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: Hide literal strings in compiled exe

Post by kenmo »

I know this is an ugly workaround, BUT...

You could use something like this (NOT within your app)

Code: Select all

Procedure.s HideString(Input.s)
  Protected *C.Character
  Protected Output.s
  Protected n.i
  Protected Columns.i = 2
  
  *C = @Input
  While (*C\c)
    If (n % Columns = 0)
      Output + #LF$ + "    Data.a"
    EndIf
    Output + " $" + RSet(Hex(*C\c ! $FF), 2, "0")
    If (n % Columns < Columns - 1)
      Output + ","
    EndIf
    n + 1
    *C + 1
  Wend
  If (n % Columns = 0)
    Output + ", $FF"
  Else
    Output + " $FF"
  EndIf
  
  Output = ";DataSection" + #LF$ + "  StringLabel:" + Output + #LF$ + ";EndDataSection"
  
  ProcedureReturn Output
EndProcedure

SetClipboardText(HideString("Hello World!"))
to generate some "hidden" text like this

Code: Select all

DataSection
  StringLabel:
    Data.a $B7, $9A ; note this is garbage when viewed in a hex editor
    Data.a $93, $93
    Data.a $90, $DF
    Data.a $A8, $90
    Data.a $8D, $93
    Data.a $9B, $DE, $FF
EndDataSection
which can be included in your app, along with the Unhide function

Code: Select all

Procedure.s UnhideString(*Address.Character)
  Protected Output.s
  
  While (*Address\c <> $FF)
    Output + Chr(*Address\c ! $FF)
    *Address + 1
  Wend
  
  ProcedureReturn Output
EndProcedure

Debug UnhideString(?StringLabel)
It's a tedious way to do it, but the nice thing is it takes up no more space than the original string (except for the small Unhide function). Also, the terminating nulls are also "hidden" which might prevent obvious string boundaries when viewed.

It could easily be adapted for Unicode, for a variable "key" (rather than $FF), or some real encryption rather than XOR.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Hide literal strings in compiled exe

Post by skywalk »

Interesting topic. :?:

But, since encrypting sensitive strings in a DataSection is already available, what would the PB native approach do?

How would PB know what you want to encrypt?

Side note: I actually edit strings in the exe for small apps to save user state changes.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: Hide literal strings in compiled exe

Post by kenmo »

skywalk wrote:But, since encrypting sensitive strings in a DataSection is already available, what would the PB native approach do?

How would PB know what you want to encrypt?
It can be done many ways, but I think MachineCode is asking for a clean, native way. Maybe something like:

Code: Select all

EncryptSection
  Data.s "Secret String One"
  Data.s "Secret String Two"
EndEncryptSection
Then the question is, (a) what encryption does it use and (b) what syntax do you use to retrieve it?

Hmmm..... makes me want to write a pre-processor tool.......
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Hide literal strings in compiled exe

Post by MachineCode »

kenmo wrote:I think MachineCode is asking for a clean, native way
Yes, just like I said in my second post. No datasections, no extra work. Just type a string as normal, as in my example, but when compiled, it's hidden. My pre-processor does it, but an option to encrypt natively like that would be great.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
Post Reply