文件的二进制数据作为变量存储在Python文件中
假设有program.exe,我将创建一个python脚本,它将
- 以二进制模式读取program.exe并将其保存在变量数据中,
- 以二进制附加模式打开mypytonprog.py,
- 将数据附加到mypythonprog.py(?)
在此之前,mypythonprog .py 将准备为:
program_data='''
在它之后,mypythonprog.py 将继续为:(
'''
programs continues....
我想以某种方式将该程序代码放入 program_data 变量中)
当然,这不工作,但是可以吗以某种方式完成? 我想要的基本上是: mypythonfile.py 能够创建一个 exe 文件,其中存储了二进制数据。
我想我必须将该数据编码为 shellcode,以便能够将其放入 mypythonfile.py 中,
正确的 ?
Lets say a have program.exe , i will create a python script that will
- read program.exe in binary mode and save it in variable data
- open mypytonprog.py in binary append mode
- append data to mypythonprog.py (?)
before this, mypythonprog.py would be prepared as:
program_data='''
after it, mypythonprog.py would continue as:
'''
programs continues....
(i want somehow to put that program code into a program_data variable)
This, does not work,of course, but can it be done in some way ?
What i want basicly is: mypythonfile.py to be able to create an exe file with binary data stored inside it.
I suppouse i have to encode that data to a shellcode to by able to put it inside mypythonfile.py,
right ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:
这个想法是以方便的块的形式处理输入文件,而不是将可能很大的文件读入内存。
对块进行 base64 编码后,我将其拆分为 80 个字符的行(这基本上只是为了使其在文本编辑器中易于管理)并将其写入输出。
我将数据包装在program_data = """ .. """.decode("base64") 中,这样当Python 加载文件时,数据将被自动解密——program_data 将包含您想要的二进制数据。
8096 是个脑残——我本来打算使用 8192 字节 (8KB)。然后我意识到还有第二个问题;对长度不是 3 个字符的倍数的块进行编码会导致“=”填充输出,从而过早地截断解码。我已将块大小更改为 6KB = 512 字节(默认 NTFS 块大小)* 3 个字符* 4(任意倍数);这似乎按预期工作。
希望有帮助!
Edit:
The idea is to process the input file in convenient chunks rather than reading a potentially-huge file into memory.
After base64-encoding the chunk, I then split it into 80-char lines (this is basically just to keep it manageable in the text editor) and write it to output.
I wrap the data in program_data = """ .. """.decode("base64") such that when Python loads the file, the data will be automatically decrypted - program_data will contain the binary data you desire.
8096 was a brain fart - I meant to use 8192 bytes (8KB). Then I realized there was a second problem; encoding a chunk other than a multiple of 3 chars long results in '='-padded output, which prematurely truncates decoding. I have changed the chunk size to 6KB = 512 bytes (default NTFS block size) * 3 chars * 4 (arbitrary multiple); this seems to work as expected.
Hope that helps!