How to delete a password string variable?

Everything else that doesn't fall into one of the other PB categories.
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 664
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

How to delete a password string variable?

Post by Kurzer »

Hello fellows,

what is the best way to completely delete a string variable that contains, for example, a decoded password that is no longer needed?

Would a sPassword = "" be enough?

Kurzer
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2023: 56y
"Happiness is a pet." | "Never run a changing system!"
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: How to delete a password string variable?

Post by Josh »

If you really don't want it in memory anymore, write something about it and then reset the string with "".

I think this has been discussed here in the forum several times.
sorry for my bad english
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 664
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: How to delete a password string variable?

Post by Kurzer »

Do you mean overwriting the string?

Code: Select all

sPassword = Space(100)
sPassword = ""
I think this has been discussed here in the forum several times.
That's what I thought and searched for, but I didn't find a really meaningful result.

It is described that the memory for strings is not really freed, but reused. Therefore also my question... I suspect/fear that sPassword = "" simply uses a new string memory and the password is still visible in the memory.
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2023: 56y
"Happiness is a pet." | "Never run a changing system!"
User avatar
STARGÅTE
Addict
Addict
Posts: 2063
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: How to delete a password string variable?

Post by STARGÅTE »

An overwrite with sPassword = "" wouldn't help for sure:

Code: Select all

Define String.s = "Hallo World!"
Define *Buffer = @String

String = ""

ShowMemoryViewer(*Buffer, 24)
0000000001EE1310 00 00 61 00 6C 00 6C 00 6F 00 20 00 57 00 6F 00 ..a.l.l.o. .W.o.
0000000001EE1320 72 00 6C 00 64 00 21 00 r.l.d.!.
Also if you overwrite it with a string larger than the password, the new string could be stored at an other place and leave the old unchanged:

Code: Select all

Define String.s = "Hallo World!"
Define *Buffer = @String

String = Space(100)

ShowMemoryViewer(*Buffer, 24)
0000000001D31310 48 00 61 00 6C 00 6C 00 6F 00 20 00 57 00 6F 00 H.a.l.l.o. .W.o.
0000000001D31320 72 00 6C 00 64 00 21 00 r.l.d.!.
I think you have to overwrite the password with a string with the same length
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
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: How to delete a password string variable?

Post by Josh »

STARGÅTE wrote:I think you have to overwrite the password with a string with the same length
I'm not sure whether overwriting with a string of the same length always works. I would overwrite the password with PokeS.

Code: Select all

PokeS (@MyPassword$, Space (Len (MyPassword$)))
MyPsssword$ = ""
sorry for my bad english
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: How to delete a password string variable?

Post by Marc56us »

Josh wrote:I'm not sure whether overwriting with a string of the same length always works.
For PB 5.71 on Windows 10 x64, this seems to work. For the other versions I haven't tested.

Based on the code of STARGÅTE and idea of Josh

Code: Select all

Define MyPassword$ = "Hello World!"
Define *Buffer = @MyPassword$

ShowMemoryViewer(*Buffer, 24)

MessageRequester("", "Hit Enter to continue")

PokeS (@MyPassword$, Space (Len (MyPassword$)))

ShowMemoryViewer(*Buffer, 24)

Code: Select all

00000000023C0880  48 00 65 00 6C 00 6C 00 6F 00 20 00 57 00 6F 00  H.e.l.l.o. .W.o.
00000000023C0890  72 00 6C 00 64 00 21 00                          r.l.d.!.
After Enter

Code: Select all

00000000023C0880  20 00 20 00 20 00 20 00 20 00 20 00 20 00 20 00   . . . . . . . .
00000000023C0890  20 00 20 00 20 00 20 00                           . . . .
This also works, but I don't know what it can do in some cases ?

Code: Select all

MyPassword$ = Space(Len(MyPassword$) - 1)
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 664
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: How to delete a password string variable?

Post by Kurzer »

Thank you all for your input. Image
The version with poking into the string memeory looks very reliable to me. I'll have it that way.
Thanks a lot!

Kurzer
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2023: 56y
"Happiness is a pet." | "Never run a changing system!"
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: How to delete a password string variable?

Post by Mijikai »

Really clear/delete a string :)

Code (x64):

Code: Select all

Macro DeleteString(String)
  EnableASM
  mov rax,String
  DisableASM
  !test rax,rax
  !jz dels_error
  !dels_clear:
  !mov word[rax],0x0
  !add rax,0x2
  !cmp word[rax],0x0
  !jne dels_clear
  !dels_error:
  String = #Null$
EndMacro
Only fully works with PB 5.62 and earlier Versions
as Null$ is broken on all newer versions of PB!
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 664
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: How to delete a password string variable?

Post by Kurzer »

Thanks Mijikai,

but what a pity that it is not working in PB 5.71. :-(
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2023: 56y
"Happiness is a pet." | "Never run a changing system!"
User avatar
skywalk
Addict
Addict
Posts: 3960
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: How to delete a password string variable?

Post by skywalk »

What is wrong with Josh's PokeS()?
Works for me.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: How to delete a password string variable?

Post by Mijikai »

kurzer wrote:Thanks Mijikai,

but what a pity that it is not working in PB 5.71. :-(
On newer versions this is the current work around:

Code: Select all

String = "!_PB_NullConstant_!"
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to delete a password string variable?

Post by wilbert »

FillMemory could also be used as an alternative for PokeS and is probably faster.

Code: Select all

MyPassword.s = "MyPassword"
FillMemory(@MyPassword, StringByteLength(MyPassword))
ShowMemoryViewer(@MyPassword, 22)
Mijikai wrote:Really clear/delete a string :)

Code (x64):

Code: Select all

Macro DeleteString(String)
  EnableASM
  mov rax,String
  DisableASM
  !test rax,rax
  !jz dels_error
  !dels_clear:
  !mov word[rax],0x0
  !add rax,0x2
  !cmp word[rax],0x0
  !jne dels_clear
  !dels_error:
  String = #Null$
EndMacro
This code will give problems when the macro is called multiple times.
In that case you will end up with multiple labels that have the same name.
To fix this you could use MacroExpandedCount.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 664
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: How to delete a password string variable?

Post by Kurzer »

skywalk wrote:What is wrong with Josh's PokeS()?
It is nothing wrong, I use his Version in my code. :)
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2023: 56y
"Happiness is a pet." | "Never run a changing system!"
Post Reply