简体   繁体   English

Python Ctypes中的SecureZeroMemory

[英]SecureZeroMemory in Python Ctypes

I'm trying to translate the following line of C code into ctypes. 我正在尝试将C代码的以下行转换为ctypes。 Here's a snippet from the C program I'm trying to translate: 这是我要翻译的C程序的片段:

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

(Note that MIB_IF_ROW2 is a struct, defined in Netioapi.h) (请注意, MIB_IF_ROW2是在Netioapi.h中定义的结构)

Anyway, I can translate the first line fine in ctypes, assuming MIB_IF_ROW2 has already been defined as a ctypes struct: 无论如何,假设MIB_IF_ROW2已被定义为ctypes结构,则我可以将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... ?

But when I get to the second line, I get stuck. 但是当我到达第二行时,我陷入了困境。 I can't find anything in the docs or online with a ctypes equivalent for the function. 我在文档或在线中找不到与该功能等效的ctypes的任何内容。 What is the best way to go about this? 最好的方法是什么?

SecureZeroMemory will just fill the memory you pass it with zeroes. SecureZeroMemory只会用零填充您传递给它的内存。 You should get the exact same result with ZeroMemory / memset or a plain loop in python. 您应该使用ZeroMemory / memset或python中的普通循环获得完全相同的结果。 The thing that makes it "secure" is that it is not supposed to be optimized away by the compiler (when programming at a lower level like C/C++). 使它“安全”的是,编译器不应该对其进行优化(当在较低级别(如C / C ++)进行编程时)。

Using it on memory you just malloc'ed is not its intended purpose (not harmful though), it is supposed to be used like this: 在刚刚分配的内存上使用它不是预期的目的(虽然没有害处),应该按以下方式使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM