在 python 中将数据标记为敏感

发布于 2024-07-24 07:35:41 字数 93 浏览 4 评论 0原文

我需要在内存中短期存储用户的密码。 我怎样才能做到这一点而又不会在核心转储或回溯中意外泄露此类信息? 有没有办法将值标记为“敏感”,这样调试器就不会将其保存在任何地方?

I need to store a user's password for a short period of time in memory. How can I do so yet not have such information accidentally disclosed in coredumps or tracebacks? Is there a way to mark a value as "sensitive", so it's not saved anywhere by a debugger?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

沫离伤花 2024-07-31 07:35:41

编辑

我制定了一个使用 ctypes(反过来使用 C)将内存归零的解决方案。

import sys
import ctypes

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.
    # memset =  ctypes.CDLL("libc.so.6").memset

    print "Clearing 0x%08x size %i bytes" % (location, size)

    memset(location, 0, size)

我不保证此代码的安全性。 它经过测试可在 x86 和 CPython 2.6.2 上运行。 更长的文章是 这里

在Python中解密和加密是行不通的。 字符串和整数是被保留和持久的,这意味着您会在各处留下混乱的密码信息。

散列是标准答案,尽管明文最终需要在某个地方进行处理。

正确的解决方案是将敏感进程作为 C 模块来执行。

但如果你的记忆不断受到损害,我会重新考虑你的安全设置。

Edit

I have made a solution that uses ctypes (which in turn uses C) to zero memory.

import sys
import ctypes

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.
    # memset =  ctypes.CDLL("libc.so.6").memset

    print "Clearing 0x%08x size %i bytes" % (location, size)

    memset(location, 0, size)

I make no guarantees of the safety of this code. It is tested to work on x86 and CPython 2.6.2. A longer writeup is here.

Decrypting and encrypting in Python will not work. Strings and Integers are interned and persistent, which means you are leaving a mess of password information all over the place.

Hashing is the standard answer, though of course the plaintext eventually needs to be processed somewhere.

The correct solution is to do the sensitive processes as a C module.

But if your memory is constantly being compromised, I would rethink your security setup.

抚笙 2024-07-31 07:35:41

...唯一的解决方案是使用可变数据结构。
也就是说,您只能使用允许您动态地
替换元素。 例如,在 Python 中,您可以使用列表来存储
字符数组。 但是,每次添加或删除元素时
从列表中,该语言可能会在你背后复制整个列表

取决于实施细节。 为了安全起见,如果必须的话
动态调整数据结构的大小,您应该创建一个新的,复制
数据,然后覆盖旧数据
。 例如:

def paranoid_add_character_to_list(ch, l):
  """Copy l, adding a new character, ch.  Erase l.  Return the result."""
  new_list = []
  for i in range(len(l)):
    new_list.append(0)
  new_list.append(ch)
  for i in range(len(l)):
    new_list[i] = l[i]
    l[i] = 0
  return new_list

来源:http://www.ibm.com/developerworks /library/s-data.html

  • 作者:John Viega ( [电子邮件受保护])是《构建安全》的合著者
    软件(Addison-Wesley,2001)和 Java 企业架构
    (奥莱利及其同事,2001 年)。 约翰撰写了 50 多部
    技术出版物,主要是软件安全领域的技术出版物。
    他还编写了 Mailman(GNU 邮件列表管理器)和 ITS4(一种工具)
    用于查找 C 和 C++ 代码中的安全漏洞。

... The only solution to this is to use mutable data structures. That
is, you must only use data structures that allow you to dynamically
replace elements. For example, in Python you can use lists to store an
array of characters. However, every time you add or remove an element
from a list, the language might copy the entire list behind your back
,
depending on the implementation details. To be safe, if you have to
dynamically resize a data structure, you should create a new one, copy
data, and then write over the old one
. For example:

def paranoid_add_character_to_list(ch, l):
  """Copy l, adding a new character, ch.  Erase l.  Return the result."""
  new_list = []
  for i in range(len(l)):
    new_list.append(0)
  new_list.append(ch)
  for i in range(len(l)):
    new_list[i] = l[i]
    l[i] = 0
  return new_list

Source: http://www.ibm.com/developerworks/library/s-data.html

  • Author: John Viega ([email protected]) is co-author of Building Secure
    Software (Addison-Wesley, 2001) and Java Enterprise Architecture
    (O'Reilly and Associates, 2001). John has authored more than 50
    technical publications, primarily in the area of software security.
    He also wrote Mailman, the GNU Mailing List Manager and ITS4, a tool
    for finding security vulnerabilities in C and C++ code.
美人骨 2024-07-31 07:35:41

无法“标记为敏感”,但您可以对内存中的数据进行加密,并在需要使用时再次解密——这不是完美的解决方案,但是我能想到的最好的解决方案。

No way to "mark as sensitive", but you could encrypt the data in memory and decrypt it again when you need to use it -- not a perfect solution but the best I can think of.

千紇 2024-07-31 07:35:41
  • 单独存储的一次性密码本的异
  • 或总是存储加盐哈希而不是密码本身

,或者,如果您对转储非常偏执,请将唯一的随机密钥存储在其他地方,例如在不同的线程中,在注册表中,在您的服务器上, ETC。

  • XOR with a one-time pad stored separately
  • always store salted hash rather than password itself

or, if you're very paranoid about dumps, store unique random key in some other place, e.g. i a different thread, in a registry, on your server, etc.

煞人兵器 2024-07-31 07:35:41

基于 culix 的回答:以下适用于 Linux 64 位架构。
在基于 Debian 的系统上进行了测试。

import sys 
import ctypes

def erase(var_to_erase):
    strlen = len(var_to_erase)
    offset = sys.getsizeof(var_to_erase) - strlen - 1
    ctypes.memset(id(var_to_erase) + offset, 0, strlen)
    del var_to_erase               # derefrencing the pointer.

based on culix's answer: the following works with Linux 64-bit architecture.
Tested on Debian based systems.

import sys 
import ctypes

def erase(var_to_erase):
    strlen = len(var_to_erase)
    offset = sys.getsizeof(var_to_erase) - strlen - 1
    ctypes.memset(id(var_to_erase) + offset, 0, strlen)
    del var_to_erase               # derefrencing the pointer.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文