<div dir="ltr"><div dir="auto">As
all the string in python are immutable, is impossible to overwrite the
value or to make a "secure disposal" (overwrite-then-free) of a string
using something like:</div><div dir="auto"><br></div><div dir="auto">>>> a = "something to hide"<br></div><div dir="auto">>>> a = "x"*len(a)</div><div dir="auto"><br></div><div dir="auto">This will lead on the process memory "something to hide" and "x" repeated len(a) times.<br></div><div dir="auto"><br></div><div dir="auto">- Who cares? Why is this relevant?</div><div dir="auto">
Well if you handle some sensitive information like CC numbers,
Passwords, PINs, or other kind of information you wanna minimize the
chance of leaking any of it.<br><br>- How this "leak" can happen?</div><div dir="auto"> If you get a core/memory dump of an app handling sensitive information you will get all the information on that core exposed!<br></div><div dir="auto"><br></div><div dir="auto">- Well, so what we can do about this?<br></div><div dir="auto"><div dir="auto"> I propose to make the required changes on the string objects to add an option to overwrite the underlying buffer. To do so:<br></div><div><br></div><div> * Add a wiped as an attribute that is read-only to be set when the string is overwrited.</div><div> * Add a wipe() method that overwrite the internal string buffer.<br></div><div><br></div><div dir="auto">So this will work like this:<br></div><div dir="auto"><br></div><div>>>> pwd =getpass.getpass('Set your password:') # could be other sensitive data.<br></div><div>>>> encrypted_pwd = crypt.crypt(pwd) # crypt() just as example.</div><div><div dir="auto">>>> pwd.wiped # Check if pwd was wiped.<br></div><div dir="auto">False</div></div><div dir="auto">>>> pwd.wipe() # Overwrite the underlying buffer</div><div dir="auto">>>> pwd.wiped # Check if pwd was wiped.<br></div><div dir="auto">True<br></div><div>>>> print(pwd) # Print noise (or empty str?)<br></div><div>>>> del pwd # Now is in hands of the GC.<br><br></div><div dir="auto">The
wipe method immediately overwrite the underlying string buffer, setting
wiped as True for reference so if the string is further used this can
be checked to confirm that the change was made by a wipe and not by
another procedure. Also initially the idea is to use unicode NULL
datapoint to overwrite the string, but this could be change to let the
user parametrize it over wipe() method.<br></div><div dir="auto">An
alternative to this is to add a new exception "WipedError" that could be
throw where the string is accessed again, but I found this method too
disruptive to implement for a normal/standard string workflow usage.</div><div dir="auto"> </div><div dir="auto">Quick & Dirty FAQ:<br><br></div><div>- You do it wrong!, the correct code to do that in a secure way is:<br></div><div>>>> pwd = crypt.crypt(getpass.getpass('<wbr>Set your password'))</div><div dir="auto">Don't you know that fool?<br><br></div><div>
Well no, the code still generate a temporary string in memory to pass
to crypt. But now this string is lying there and can't be accessed for
an overwrite with wipe()<br><br></div><div dir="auto"><br></div><div dir="auto">- Why not create a new type like in C# or Java?</div><div dir="auto"><br></div><div>
I see that this tend to disrupt the usual workflow of string usage.
Also the idea here is not to offer secure storage of string in memory
because there is already a few mechanism to achieve with the current
Python base. I just want to have the hability to overwrite the buffer.<br><br></div><div dir="auto"><br></div><div dir="auto">- Why don't use one of the standard algorithms to overwrite like DoD5220 or MIL-STD-414?</div><div dir="auto"><br></div><div dir="auto"> This kind of <span style="font-family:sans-serif">standard</span> usually
are oriented for usage on persistent storage, specially on magnetic
media for where the data could be "easily" recoverd. But this could ve
an option that could be implemented adding the option to plug a function
that do the overwrite work inside the wipe method.</div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto">-
This is far beyond of the almost implementation-agnostic definition of
the python lang. How about to you make a module with this functionality
and left the lang as is?</div><div dir="auto"><br></div><div dir="auto"> Well I already do it:</div><div dir="auto"><br></div><div dir="auto"><a href="https://github.com/qlixed/python-memwiper/" rel="noreferrer" target="_blank">https://github.com/qlixed/<wbr>python-memwiper/</a><br></div><div dir="auto"><br></div><div dir="auto">
But i hit a lot of problems in the road, I was working on me free time
over the last year on this and make it "almost" work, but that is not
relevant to the proposal.</div><div dir="auto"> I think that this kind
of security things needs to be tackled from within the language itself
specially when the lang have GC. I firmly believe that the security and
protections needs to be part of the "with batteries" offer of Python.
And I think that this is one little thing that could help a lot to
secure our apps.</div></div><div> Let me know what do you think!</div><div><br></div><div><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>~ Ezequiel (Ezekiel) Brizuela [ aka Qlixed ] ~<br></div><br></div></div></div></div></div>
</div>