简体   繁体   English

在 python 中存储数据

[英]Storing data in python

I need help storing data within a python program.我需要帮助在 python 程序中存储数据。 I don't want the user to be able to touch the data at all.我根本不希望用户能够触摸数据。 I've looked into pickle but many posts say it is "insecure".我研究过泡菜,但很多帖子都说它“不安全”。

You can't stop the user from touching data.您无法阻止用户触摸数据。 If they're running a program on their system, they can do whatever they want with the bits after you write them to disk.如果他们在他们的系统上运行一个程序,他们可以在你将它们写入磁盘之后对这些位做任何他们想做的事情。 You can obfuscate the data in various ways, possibly even encrypting it, but they can still, eventually get to it if they're determined.您可以以各种方式混淆数据,甚至可能对其进行加密,但如果他们下定决心,他们仍然可以最终得到它。 If you want proof, look at the absolute failure of every copy protection/DRM system ever invented.如果您想要证明,请查看曾经发明的每个复制保护/DRM 系统的绝对失败。 There are solutions that are 'good enough' but, without knowing what problem you're actually trying to solve there's no good way to start providing realistic options.有一些“足够好”的解决方案,但是,在不知道您实际尝试解决什么问题的情况下,没有好的方法可以开始提供现实的选择。

...and Pickle's great if you can trust your data. ...如果您可以信任您的数据,Pickle 就很棒。 If you can be reasonably certain that your program wrote a file to disk and that malicious programs aren't actively targeting your application, it's safe.如果您可以合理地确定您的程序将文件写入磁盘并且恶意程序没有主动针对您的应用程序,那么它是安全的。 I'd never trust a pickle sent across the network, however - a pickle can potentially execute arbitrary commands during deserialization.但是,我永远不会相信通过网络发送的泡菜——泡菜可能会在反序列化期间执行任意命令。

You can try to stop the program from running if it's been tampered with, for example by comparing its (md5) hash to a known good value.如果程序被篡改,您可以尝试阻止程序运行,例如通过将其 (md5) hash 与已知的良好值进行比较。 Check out the Chrome OS project for an example of a system which does roughly this.查看 Chrome OS 项目以获取大致执行此操作的系统示例。

You can try to stop the user from understanding your program and data written to the disk, for example by encrypting it and hiding the decryption subroutine, or by obfuscating the source code.可以尝试阻止用户理解您写入磁盘的程序和数据,例如通过对其进行加密和隐藏解密子例程,或通过混淆源代码。

But you can't stop a determined user from destroying your program and its data or from interrupting it.但是您无法阻止坚定的用户破坏您的程序及其数据或中断它。 Once your program is in the air, I think its memory segment is protected from access by other processes.一旦你的程序运行起来,我认为它的 memory 段受到保护,不会被其他进程访问。 This doesn't stop a user from decompiling and trying to make sense of your program before running it, though.不过,这并不能阻止用户在运行程序之前反编译并尝试理解您的程序。

Security which aims to protect software from its owner is bound to rely on clever hacks.旨在保护软件免受其所有者侵害的安全性必然依赖于巧妙的黑客攻击。 Remeber that your clever hacks are subject to circumvention by other clever hacks.请记住,您的聪明黑客会受到其他聪明黑客的规避。 Python was designed as an open language, so you might have better luck with other languages if you intend to design "sneaky" programs. Python 被设计为一种开放语言,因此如果您打算设计“偷偷摸摸”的程序,那么使用其他语言可能会更好。

If the user will be running your program on his machine you just can't hide the data.如果用户将在他的机器上运行您的程序,您就无法隐藏数据。

The user running the program can access everything the program can, is just a matter of knowing where to look.运行程序的用户可以访问程序可以访问的所有内容,只需知道在哪里查看即可。

If you are dealing with end-users then just encrypt the data and decrypt it at the last point before using it.如果您正在与最终用户打交道,那么只需在使用它之前的最后一点加密数据并解密它。 Just keep in mind that, at some point, the data has to be decrypted on memory and then the user could see it.请记住,在某些时候,必须在 memory 上解密数据,然后用户才能看到它。

If you need to protect the data from tampering (but you are not worried about someone seeing the data), then deploying your project with a custom loader is the way to go such as signet ( http://jamercee.github.io/signet/ ). If you need to protect the data from tampering (but you are not worried about someone seeing the data), then deploying your project with a custom loader is the way to go such as signet ( http://jamercee.github.io/signet / )。 Put your data blob in your script, and it's values will be incorporated in it's sha1 calculations.将您的数据 blob 放入您的脚本中,它的值将被合并到它的 sha1 计算中。 If anyone were to modify the data blob, the loader will detect it as tampering, and refuse to run it.如果有人要修改数据块,加载程序会检测到它是篡改的,并拒绝运行它。

Maybe you should look at CouchDB?也许你应该看看 CouchDB? You basically store data in JSON as document.您基本上将数据存储在 JSON 作为文档。 It works perfectly.它完美地工作。

http://code.google.com/p/couchdb-python/ http://code.google.com/p/couchdb-python/

Do you mean within as in within memory, and not on disk?您的意思是在 memory 内,而不是在磁盘上? A really simple solution to storing data in memory and making it easily accessible throughout the program would be to make a python file containing a dictionary in your program's root directory.在 memory 中存储数据并使其在整个程序中易于访问的一个非常简单的解决方案是在程序的根目录中创建一个包含字典的 python 文件。

for example, py/storage.py would contain:例如,py/storage.py 将包含:

data = {}

Then you could write to it like so:然后你可以这样写:

import storage
storage.data['foo'] = 'bar'

and then read the same value in another file like this:然后在另一个文件中读取相同的值,如下所示:

import storage
foo = storage.data['foo']

The data in the storage module will be accessible to all other modules within the program, but will be erased when the program exits.存储模块中的数据将可供程序中的所有其他模块访问,但在程序退出时将被擦除。 The user will not be able to touch it without modifying the program.如果不修改程序,用户将无法触摸它。 If you need something more database related, sqlite allows you to create databases in memory that only stay in memory as long as the program is running如果您需要更多与数据库相关的内容,sqlite 允许您在 memory 中创建数据库,只要程序正在运行,该数据库仅保留在 memory 中

Have a quick look at this , and this .快速浏览一下这个这个 Those libraries may be of some help.这些库可能会有所帮助。 I feel compelled to note that an experienced computer software engineer will probably find a way to circumnavigate whatever implemented method of data concealing by simply reading your source file, deconstruction of your compiled code, or even possibly a feasible brute-force decryption (I could be wrong).我不得不指出,一个有经验的计算机软件工程师可能会找到一种方法来绕过任何实施的数据隐藏方法,只需读取您的源文件,解构您的编译代码,甚至可能是可行的蛮力解密(我可能是错误的)。

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

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