首页 > 解决方案 > 将 RDPY 移植到 Python3 - AttribueError

问题描述

我决定自己接手一个项目来解决一些原作者似乎已经放弃的问题。努力是将代码移植到 Python3 并相应地更新库。

下面是一段我似乎无法弄清楚的代码块。

我已经尝试过 StringsIO 和 BytesIO ,但似乎都不起作用。

class Stream(BytesIO):
    """
    @summary:  Stream use to read all types
    """

    def dataLen(self):
        """
        @return: not yet read length
        """
        return self.len - self.pos

    def readLen(self):
        """
        @summary: compute already read size
        @return: read size of stream
        """
        return self.pos

    def readType(self, value):
        """
        @summary:  call specific read on type object
                    or iterate over tuple elements
                    rollback read if error occurred during read value
        @param value: (tuple | Type) object
        """
        # read each tuple
        if isinstance(value, tuple) or isinstance(value, list):
            for element in value:
                try:
                    self.readType(element)
                except Exception as e:
                    # rollback already readed elements
                    for tmpElement in value:
                        if tmpElement == element:
                            break
                        self.pos -= sizeof(tmpElement)
                    raise e
            return

        # optional value not present
        if self.dataLen() == 0 and value._optional:
            return

        value.read(self)

    def readNextType(self, t):
        """
        @summary: read next type but didn't consume it
        @param t: Type element
        """
        self.readType(t)
        self.pos -= sizeof(t)

    def writeType(self, value):
        """
        @summary:  Call specific write on type object
                    or iterate over tuple element
        @param value: (tuple | Type)
        """
        # write each element of tuple
        if isinstance(value, tuple) or isinstance(value, list):
            for element in value:
                self.writeType(element)
            return
        value.write(self)

错误:

[*] DEBUG:  Build size map
Traceback (most recent call last):
  File "/usr/local/bin/rdpy-rdphoneypot.py", line 197, in <module>
    size = readSize(arg)
  File "/usr/local/bin/rdpy-rdphoneypot.py", line 154, in readSize
    e = r.nextEvent()
  File "/usr/local/lib/python3.7/dist-packages/rdpy/core/rss.py", line 303, in nextEvent
    if self._s.dataLen() == 0:
  File "/usr/local/lib/python3.7/dist-packages/rdpy/core/type.py", line 914, in dataLen
    return self.len - self.pos
AttributeError: 'Stream' object has no attribute 'len'

所有代码都在这里:https ://github.com/hackdefendr/rdpy.git

任何关于为什么这不起作用的指针?

标签: pythonpython-3.x

解决方案


错误信息很清楚。没有self.len在一个BytesIOStringIO对象上这样的东西。Python 2 的StringIO对象上有,但文档中没有。也没有self.pos, 这对您来说将是一个更大的问题,因为该代码假定您不仅可以从中读取,还可以对其进行写入。

seek()您可以从和函数中获得许多相同的tell()功能。tell()相当于获取 的值posseek()相当于设置 的值pos,再加上可以seek()用来获取长度:

from io import StringIO
s = StringIO('test')

s.seek(2)              # seek to absolute position (pos = 2)
s.seek(4, 1)           # seek to relative position (pos += 4)

current_pos = s.tell()
length = s.seek(0, 2)  # seeks to end and returns the position
s.seek(current_pos)    # returns to previous position

推荐阅读