Python Ctypes中的SecureZeroMemory

时间:2017-06-27 23:39:22

标签: python windows ctypes securezeromemory

我试图将以下C代码行转换为ctypes。这是我尝试翻译的C程序的片段:

pIfRow = (MIB_IF_ROW2 *) malloc(sizeof(MIB_IF_ROW2));
SecureZeroMemory((PVOID)pIfRow, sizeof(MIB_IF_ROW2));

(注意MIB_IF_ROW2是结构,在Netioapi.h中定义)

无论如何,我可以在ctypes中翻译第一行,假设MIB_IF_ROW2已被定义为ctypes结构:

from ctypes import *

# Translate first line of C Code
buff = create_string_buffer(sizeof(MIB_IF_ROW2))
p_if_row = cast(buff, POINTER(MIB_IF_ROW2))

# Second Line... ?

但是当我到达第二行时,我被困住了。我无法在文档中找到任何内容,也无法使用与该函数等效的ctypes。最好的方法是什么?

1 个答案:

答案 0 :(得分:1)

SecureZeroMemory只会用零填充你传递的内存。您应该使用ZeroMemory / memset或python中的普通循环获得完全相同的结果。使它"安全"是不应该被编译器优化掉(当在较低级别编程时,如C / C ++)。

在内存中使用它你只是malloc' ed不是它的预期目的(虽然没有害处),它应该像这样使用:

char password[100];
AskUserForPassword(password);
DoSomething(password);
SecureZeroMemory(password, sizeof(password)); // Make sure password is no longer visible in memory in case the application is paged out or creates a memory dump in a crash