Explaining/translating Python class 对于 .NET

Explaining/translating Python class for .NET

老实说,多年来我一直在编程,我从来没有太多理由使用字节数组(除非你计算 copy/paste 用于特定目的的代码)并且在大多数情况下,我承认我不完全了解如何正确处理它们。现在,我正在尝试解析 Active Directory dnsNode 对象的 dnsRecord 属性,这是一个具有以下结构的字节数组:

  • DataLength (2 bytes): An unsigned binary integer containing the length, in bytes, of the Data field.
  • Type (2 bytes): The resource record's type. See DNS_RECORD_TYPE (section 2.2.2.1.1).
  • Version (1 byte): The version number associated with the resource record attribute. The value MUST be 0x05.
  • Rank (1 byte): The least-significant byte of one of the RANK* flag values. See dwFlags (section 2.2.2.2.5).
  • Flags (2 bytes): Not used. The value MUST be 0x0000.
  • Serial (4 bytes): The serial number of the SOA record of the zone containing this resource record. See DNS_RPC_RECORD_SOA (section 2.2.2.2.4.3).
  • TtlSeconds (4 bytes): See dwTtlSeconds (section 2.2.2.2.5). This field uses big-endian byte order.
  • Reserved (4 bytes): This field is reserved for future use. The value MUST be 0x00000000.
  • TimeStamp (4 bytes): See dwTimeStamp (section 2.2.2.2.5).
  • Data (variable): The resource record's data. See DNS_RPC_RECORD_DATA (section 2.2.2.2.4).

查看来自我的一个 AD 对象的数据样本,我相信我至少知道我正在查看什么以及如何找到我需要的信息。

04  00  01  00  05  240  00  00  36  43  78  120  00  00  14  16  00  00  00  00  00  00  00  00  192  168  1  120

即便如此,我真正想要的是为我正在从事的项目对这些信息进行某种形式的处理。就像我说的,我可能可以根据我正在查看的示例数据拼凑出一些可以“完成工作”的东西,但我真的很想“正确地”构建它。

我找到了一个 example written in Python,它定义了一个通用 DNS_RECORD class 以及一些更具体的 class 个别记录类型(A , AAAA, SRV, etc.) 我了解基本功能,但是,因为我的理解是 Python 并没有真正使用显式对象类型定义等等 - 因为我通常在 VB.NET 中进行编程 - 我对 class 定义有点困惑:

class DNS_RECORD(Structure):
    """
    dnsRecord - used in LDAP
    [MS-DNSP] section 2.3.2.2
    """
    structure = (
        ('DataLength', '<H-Data'),
        ('Type', '<H'),
        ('Version', 'B=5'),
        ('Rank', 'B'),
        ('Flags', '<H=0'),
        ('Serial', '<L'),
        ('TtlSeconds', '>L'),
        ('Reserved', '<L=0'),
        ('TimeStamp', '<L=0'),
        ('Data', ':')
    )

据我了解,我知道这是在定义对象的属性(DataLengthVersionData ).根据我可以通过基本上下文线索和我对上述标准定义的阅读来“翻译”自己的内容,正在设置 VersionFlagsReservedTimeStamp 属性到恒定值。这是我对这个结构的“了解”所能得到的(毫无疑问,这仍然可能是错误的)。

然而,我最困惑的是,那里的尖括号是干什么用的(例如<H>L)?我认为它有助于解析出各个字节,也许通过识别 endianness? HLB 是另一种指定字节 read/written 的方式吗?我只是不确定如何在 .NET 中复制这些特定指令。

我正在尝试阅读 Python,但如果能解释这个 class 定义以便我翻译它,我们将不胜感激。我真的很想在这里自学一下。

您定义的 DNS_RECORD class 继承自 Structure(大写 S)class,它在 impacket 库中定义(请参阅 import语句)。 structure(小写 s)成员的参数特定于 library/class,它们的表达方式也是如此。不幸的是,它似乎没有在线记录(也许如果你安装库你会得到一些文档)。

无论如何,Structure 看起来像是 Python 标准库 struct 模块的特殊版本。您可能需要 check it 获取一般信息