文件的二进制数据作为变量存储在Python文件中

发布于 2024-10-16 11:35:25 字数 541 浏览 0 评论 0原文

假设有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 技术交流群。

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

发布评论

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

评论(2

你曾走过我的故事 2024-10-23 11:35:25
>>> 'some binary data'.encode('base64')
'c29tZSBiaW5hcnkgZGF0YQ==\n'
>>> 'c29tZSBiaW5hcnkgZGF0YQ==\n'.decode('base64')
'some binary data'
>>> 'some binary data'.encode('base64')
'c29tZSBiaW5hcnkgZGF0YQ==\n'
>>> 'c29tZSBiaW5hcnkgZGF0YQ==\n'.decode('base64')
'some binary data'
折戟 2024-10-23 11:35:25
import base64

exe = 'c:/users/me/program.exe'
script = 'c:/users/me/newpython.py'
chunksize = 6144
linelength = 80

with open(exe,'rb') as inf, open(script,'w') as outf:
    outf.write('program_data = """')

    while True:
        progdata = inf.read(chunksize)
        if len(progdata):
            progdata = progdata.encode("base64")
            outf.writelines(progdata[i:i+linelength] for i in range(0, len(progdata), linelength))
        else:
            break

    outf.write('""".decode("base64")')

编辑:

这个想法是以方便的块的形式处理输入文件,而不是将可能很大的文件读入内存。

对块进行 base64 编码后,我将其拆分为 80 个字符的行(这基本上只是为了使其在文本编辑器中易于管理)并将其写入输出。

我将数据包装在program_data = """ .. """.decode("base64") 中,这样当Python 加载文件时,数据将被自动解密——program_data 将包含您想要的二进制数据。

8096 是个脑残——我本来打算使用 8192 字节 (8KB)。然后我意识到还有第二个问题;对长度不是 3 个字符的倍数的块进行编码会导致“=”填充输出,从而过早地截断解码。我已将块大小更改为 6KB = 512 字节(默认 NTFS 块大小)* 3 个字符* 4(任意倍数);这似乎按预期工作。

希望有帮助!

import base64

exe = 'c:/users/me/program.exe'
script = 'c:/users/me/newpython.py'
chunksize = 6144
linelength = 80

with open(exe,'rb') as inf, open(script,'w') as outf:
    outf.write('program_data = """')

    while True:
        progdata = inf.read(chunksize)
        if len(progdata):
            progdata = progdata.encode("base64")
            outf.writelines(progdata[i:i+linelength] for i in range(0, len(progdata), linelength))
        else:
            break

    outf.write('""".decode("base64")')

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文