在Python中实现Hexdump

发布于 2025-02-10 05:19:42 字数 156 浏览 1 评论 0原文

如何在Python 3中获得以下内容?

CAT文件| SIGTOOL –HEX-DUMP | Head -C 2048

我正在尝试读取我创建的.NDB数据库,以检查恶意PHP文件,但是我需要能够在Python中创建文件的六角签名此数据库。

How can I achieve the following in Python 3?

cat file | sigtool –hex-dump | head -c 2048

I’m attempting to read a .ndb database which I have created in order to check for malicious PHP files, however I need to be able to create hex signatures of files in python in order to check against this database.

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

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

发布评论

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

评论(1

涙—继续流 2025-02-17 05:19:42

尝试使用binascii.hexlify函数:

import binascii

# get the filename from somewhere

# read the raw bytes
with open(filename, 'rb') as f:
    raw_bytes = f.read(1024)       # two hex digits per byte, so read 1024

# convert to hex
hex_bytes = binascii.hexlify(raw_bytes)  # this will be 2048 bytes long

# use the hex, as desired
hex_str = hexbytes.decode('ascii') # hexidecimal uses a subset of ASCII
print(hex_str)                     # or put this in a database, or whatever

我将hex_bytes转换为字符串,以便我可以将它们打印出来,但是您可能会忽略,如果您可以使用bytes对象,则Hexlify直接(例如以二进制模式写入文件,或将其直接保存到数据库中)。

Try using the binascii.hexlify function:

import binascii

# get the filename from somewhere

# read the raw bytes
with open(filename, 'rb') as f:
    raw_bytes = f.read(1024)       # two hex digits per byte, so read 1024

# convert to hex
hex_bytes = binascii.hexlify(raw_bytes)  # this will be 2048 bytes long

# use the hex, as desired
hex_str = hexbytes.decode('ascii') # hexidecimal uses a subset of ASCII
print(hex_str)                     # or put this in a database, or whatever

I converted the hex_bytes into a string so I could print them out, but you might omit that if you can use the bytes object returned by hexlify directly (e.g. writing it to a file in binary mode, or saving it directly into a database).

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