从 python 程序中禁用哈希随机化 [英] Disable hash randomization from within python program

查看:17
本文介绍了从 python 程序中禁用哈希随机化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 Python 3.3 开始,散列算法是非确定性的加盐以避免某种攻击.这对网络服务器来说很好,但在尝试调试程序时很痛苦:每次我运行我的脚本时,dict 内容都以不同的顺序迭代.

Starting with Python 3.3, the hashing algorithm is non-deterministically salted to avoid a certain kind of attack. This is nice for webservers but it's a pain when trying to debug a program: Every time I run my script, dict contents are iterated in a different order.

一些早期版本的 python 有一个 -R 标志,用于启用散列随机化,但现在它是默认行为,该标志并没有被它的反面取代.可以通过设置环境变量 PYTHONHASHSEED 来禁用随机化:

Some earlier versions of python had a -R flag for enabling hash randomization, but now that it's the default behavior, the flag has not been replaced by its opposite. Randomization can be disabled by setting the environment variable PYTHONHASHSEED:

PYTHONHASHSEED

如果此变量未设置或设置为随机,则使用随机值作为 str、bytes 和 datetime 对象的散列的种子.
如果 PYTHONHASHSEED 设置为整数值,则将其用作固定种子,用于生成哈希随机化覆盖的类型的 hash().

If this variable is not set or set to random, a random value is used to seed the hashes of str, bytes and datetime objects.
If PYTHONHASHSEED is set to an integer value, it is used as a fixed seed for generating the hash() of the types covered by the hash randomization.

问题是这个变量必须在启动 python 进程之前设置.我尝试使用 os.putenv() 或在 os.environ 中设置它,但这些似乎对散列方法没有影响.这并不奇怪:我不希望 python 在每个集合或字典查找之前检查环境!所以,问题仍然存在:

The catch is that this variable must be set before launching the python process. I've tried to set it with os.putenv(), or in os.environ, but these seem to have no effect on the hashing method. This is not too surprising: I wouldn't expect python to check the environment before every single set or dictionary lookup! So, the question remains:

python 程序有没有办法禁用它自己的哈希随机化?

Is there a way for a python program to disable its own hash randomization?

推荐答案

不幸的是,我怀疑这是不可能的.查看 test_hash.py HashRandomizationTests 类及其后代被添加到 引入此行为的提交.他们通过修改环境并启动一个明确设置了 PYTHONHASHSEED 的新进程来测试散列行为.也许您可以尝试复制该模式.

I suspect this isn't possible, unfortunately. Looking at test_hash.py the HashRandomizationTests class and its descendants were added in the commit that introduced this behavior. They test the hashing behavior by modifying the environment and starting a new process with PYTHONHASHSEED explicitly set. You could try to copy that pattern, perhaps.

我也刚刚注意到您说每次我运行我的脚本时,dict 内容都以不同的顺序迭代." - 我假设您知道 collections.OrderedDict,对吧?这是获得可靠哈希迭代的正常方法.

I also just noticed you said "Every time I run my script, dict contents are iterated in a different order." - I assume you're aware of collections.OrderedDict, right? That's the normal way to get reliable hash iteration.

如果你愿意在你的 shell 环境中设置这个值,你也可以将你的 python 调用包装在一个 bash 脚本中,例如

If you're willing to set the value in your shell environment, you could also just wrap your python call in a bash script, e.g.

#! /bin/bash
export PYTHONHASHSEED=0

# call your python program here

这样就无需操作整个环境,只要您对包装脚本没问题.

That avoids needing to manipulate your whole environment, as long as you're ok with a wrapper script.

或者甚至只是在命令行上传递值:

Or even just pass the value on the command line:

$ PYTHONHASHSEED=0 python YOURSCRIPT.py

这篇关于从 python 程序中禁用哈希随机化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆