我试图从内存中删除密码字符串就像这里建议的那样。
我写了这个小代码段:
import ctypes, sys
def zerome(string):
location = id(string) + 20
size = sys.getsizeof(string) - 20
#memset = ctypes.cdll.msvcrt.memset
# For Linux, use the following. Change the 6 to whatever it is on your computer.
print ctypes.string_at(location, size)
memset = ctypes.CDLL("libc.so.6").memset
memset(location, 0, size)
print "Clearing 0x%08x size %i bytes" % (location, size)
print ctypes.string_at(location, size)
a = "asdasd"
zerome(a)
奇怪的是,这段代码在IPython上运行良好,
[7] oz123@yenitiny:~ $ ipython a.py
Clearing 0x02275b84 size 23 bytes
但是使用Python时会出现崩溃:
[8] oz123@yenitiny:~ $ python a.py
Segmentation fault
[9] oz123@yenitiny:~ $
有什么想法吗?
我在Debian Wheezy上测试,使用Python 2.7.3。
小更新...
代码在CentOS 6.2上使用Python 2.6.6可以正常运行。 代码在Debian上使用Python 2.6.8崩溃了。 我试着思考为什么它在CentOS上可以工作而在Debian上不行。唯一的原因是, 我的Debian是多架构的,而CentOS则在我的旧笔记本电脑上运行,它具有i686 CPU。
因此,我重新启动了我的CentOS笔记本电脑并加载了Debian Wheezy。 代码可以在非多架构的Debian Wheezy上工作。 因此,我怀疑我的Debian配置存在问题...
+20
的 hack 在任何 CPython 上都能正常工作吗?毕竟它甚至没有被记录在文档中。你确定它在不会崩溃的配置下确实清除了正确的值吗? - wRAR