Skip to content

Instantly share code, notes, and snippets.

@skull-squadron
Created August 29, 2011 07:53
Show Gist options
  • Save skull-squadron/1177963 to your computer and use it in GitHub Desktop.
Save skull-squadron/1177963 to your computer and use it in GitHub Desktop.
Shred the contents of python objects.
"""
(c) 2011 Barry Allard <[email protected]>
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
http://www.opensource.org/licenses/mit-license.php
"""
import sys
import ctypes
import platform
def is_linux():
return platform.system() == 'Linux'
def is_mac():
return platform.system() == 'Darwin'
def is_windows():
platf = platform.system()
return platf == 'windows' or platf == 'Windows'
def which_unicode():
return sys.maxunicode == 0xffff and 'UCS2' or 'UCS4'
TYPE_SIZES = {
str: sys.getsizeof('')-1,
long: sys.getsizeof(0L),
}
def base_sizeof(x):
return TYPE_SIZES[type(x)]
def get_data_ptr(x):
"""
Returns the memory address and size of the actual CPython object.
Tested with Python 2.x on Mac and Linux only. May not work on Windows.
Unlikely to work on PyPy, Cython, Pyrex, Stackless, Jython, Python 3 or others.
"""
multiplier = 1
if is_windows():
location_delta = 20
size_delta = -20
elif is_linux():
if type(x) == long:
multiplier = 2
location_delta = 16
else:
location_delta = 36
size_delta = -TYPE_SIZES[type(x)]
elif is_mac():
location_delta = TYPE_SIZES[type(x)]
size_delta = -TYPE_SIZES[type(x)]
else:
raise Exception('Unknown platform')
location = id(x) + location_delta
size = (sys.getsizeof(x) + size_delta)*multiplier
return (location, size)
def print_wrapper(*args):
"""
Since print is not really a fun, it cant be map()ed.
"""
for arg in args[:-1]:
print arg[0],
print args[-1:][0]
def shred_object(x):
"""
Destroys data securely in memory.
TODO: int and unicode (UCS2 and UCS4) support.
Add VMM support for swapping back in, locking the page and purging swap.
Does not address virtualization swap, etc.
>>> x = [11290320194129841294180924980120894128904128091902489012849018290409812489023415894012814L, 'abcdef', 's']
>>> map (print_wrapper,map(repr,x))
11290320194129841294180924980120894128904128091902489012849018290409812489023415894012814L
'abcdef'
's'
[None, None, None]
>>> map(shred_object, x)
[None, None, None]
>>> map (print_wrapper,map(repr,x))
0L
'\\x00\\x00\\x00\\x00\\x00\\x00'
'\\x00'
[None, None, None]
"""
location, size = get_data_ptr(x)
ctypes.memset(location, 0, size)
if __name__ == "__main__":
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment