from_buffer_copy()的语法

发布于 2025-02-09 02:08:09 字数 1127 浏览 1 评论 0原文

我正在读一本名为 Black Hat的书 Python Python编程针对黑客和五旬节的编程 贾斯汀·塞兹(Justin Seitz)。

这是本书中的代码:

import socket
import os
import struct
from ctypes import *

# host to listen
host = "10.0.2.15"


# our IP header
class IP(Structure):
    _fields_ = [
        ("ihl", c_ubyte, 4),
        ("version", c_ubyte,4),
        ("tos", c_ubyte),
        ("len", c_ushort),
        ("id", c_ushort),
        ("offset", c_ubyte),
        ("ttl", c_ubyte),
        ("protocol_num", c_ubyte),
        ("sum", c_ubyte),
        ("src", c_ulong),
        ("dst", c_ulong)
    ]
    
    def __new__(cls,socket_buffer=None):
        return cls.from_buffer_copy(socket_buffer)

在此代码中,在函数新的内部,有名为from_buffer_copy()的函数。它是ctypes python库的功能。

我想知道它的作用以及使用它的语法是什么?

Python文档说该语法是from_buffer_copy(source [,offset])。我发现了使用此语法的示例,但是文档提供的用法定义对我来说是模糊的。

Python文档 仅用一个参数看到了此功能,那么它如何实现?

任何帮助将不胜感激。

I'm reading a book called Black Hat
Python Python Programming for Hackers and Pentesters

by Justin Seitz.

This is code from the book:

import socket
import os
import struct
from ctypes import *

# host to listen
host = "10.0.2.15"


# our IP header
class IP(Structure):
    _fields_ = [
        ("ihl", c_ubyte, 4),
        ("version", c_ubyte,4),
        ("tos", c_ubyte),
        ("len", c_ushort),
        ("id", c_ushort),
        ("offset", c_ubyte),
        ("ttl", c_ubyte),
        ("protocol_num", c_ubyte),
        ("sum", c_ubyte),
        ("src", c_ulong),
        ("dst", c_ulong)
    ]
    
    def __new__(cls,socket_buffer=None):
        return cls.from_buffer_copy(socket_buffer)

In this code, inside the function new, there is function called from_buffer_copy(). It is a function from the ctypes Python library.

I want to know what it does and what is the syntax to use it?

The Python documentation says the syntax is from_buffer_copy(source[, offset]). I have found examples that use this syntax, but the usage definition provided by the documentation is blurry to me.

Python documentation: from_buffer_copy()

In this book I saw this function with only one argument, so how is it being implemented?

Any help will be greatly appreciated.

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

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

发布评论

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

评论(1

阿楠 2025-02-16 02:08:09

在文档from_buffer_copy(源[,offset])中,方括号表示第二个参数是可选的。在代码中,它如下所示,其中给出了可选第二个参数的默认值:

def from_buffer_copy(self, source, offset=0): ...

这是.from_buffer_copy 和.from_buffer带有说明的工作示例。 。这些功能用于创建基于接收到字节数据的内存结构的python类:

import ctypes as ct

# A simple ctypes structure with two 4-byte fields.
# It will need at least 8 bytes of data to build this structure.
class Demo(ct.Structure):

    _fields_ = (('first', ct.c_uint32),
                ('second', ct.c_uint32))

    # defines the print representation to easily look at the fields
    def __repr__(self):
        return f'Demo(first=0x{self.first:08x}. second=0x{self.second:08x})'

# A 10-byte read-only bytes buffer.
# Extra bytes to demonstrate the offset parameter.
buf = bytes([1,2,3,4,5,6,7,8,9,10])

try:
    # This syntax constructs a Demo object that refers to the *same*
    # memory that buffer occupies, starting from the default offset of 0,
    # but to do so requires a writeable buffer
    demo1 = Demo.from_buffer(buf)
except TypeError as e:
    print(e)  # This error will print

# This builds the Demo object from a copy of the memory,
# so a read-only buffer can be used, starting from default offset 0.
demo2 = Demo.from_buffer_copy(buf)
print(demo2)

# This builds the object from offset 2, so the values of "first" and
# "second" will be different.
demo3 = Demo.from_buffer_copy(buf,2)
print(demo3)

# This is a writable buffer, so from_buffer will work
buf2 = bytearray([1,2,3,4,5,6,7,8])
demo4 = Demo.from_buffer(buf2)
print(demo4)

# Since demo4 references the *same* memory, changing writable buf2
# also changes demo4
buf2[1] = 0xff
print(demo4)

输出:

underlying buffer is not writable               # error message from demo1 attempt
Demo(first=0x04030201. second=0x08070605)       # demo2 copied from offset 0
Demo(first=0x06050403. second=0x0a090807)       # demo3 copied from offset 2
Demo(first=0x04030201. second=0x08070605)       # demo4
Demo(first=0x0403ff01. second=0x08070605)       # demo4 after write to buf2

In the documentation from_buffer_copy(source[, offset]), square brackets means the 2nd parameter is optional. In code it is written like the following, where a default value for the optional second parameter is given:

def from_buffer_copy(self, source, offset=0): ...

Here's a working example of the difference between .from_buffer_copy and .from_buffer with explanations. The functions are used to create Python classes based on memory structures that are received as byte data:

import ctypes as ct

# A simple ctypes structure with two 4-byte fields.
# It will need at least 8 bytes of data to build this structure.
class Demo(ct.Structure):

    _fields_ = (('first', ct.c_uint32),
                ('second', ct.c_uint32))

    # defines the print representation to easily look at the fields
    def __repr__(self):
        return f'Demo(first=0x{self.first:08x}. second=0x{self.second:08x})'

# A 10-byte read-only bytes buffer.
# Extra bytes to demonstrate the offset parameter.
buf = bytes([1,2,3,4,5,6,7,8,9,10])

try:
    # This syntax constructs a Demo object that refers to the *same*
    # memory that buffer occupies, starting from the default offset of 0,
    # but to do so requires a writeable buffer
    demo1 = Demo.from_buffer(buf)
except TypeError as e:
    print(e)  # This error will print

# This builds the Demo object from a copy of the memory,
# so a read-only buffer can be used, starting from default offset 0.
demo2 = Demo.from_buffer_copy(buf)
print(demo2)

# This builds the object from offset 2, so the values of "first" and
# "second" will be different.
demo3 = Demo.from_buffer_copy(buf,2)
print(demo3)

# This is a writable buffer, so from_buffer will work
buf2 = bytearray([1,2,3,4,5,6,7,8])
demo4 = Demo.from_buffer(buf2)
print(demo4)

# Since demo4 references the *same* memory, changing writable buf2
# also changes demo4
buf2[1] = 0xff
print(demo4)

Output:

underlying buffer is not writable               # error message from demo1 attempt
Demo(first=0x04030201. second=0x08070605)       # demo2 copied from offset 0
Demo(first=0x06050403. second=0x0a090807)       # demo3 copied from offset 2
Demo(first=0x04030201. second=0x08070605)       # demo4
Demo(first=0x0403ff01. second=0x08070605)       # demo4 after write to buf2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文