项目分配给字节对象? [英] Item assignment to bytes object?

查看:42
本文介绍了项目分配给字节对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GAHH,不工作的代码确实是糟糕的代码!

GAHH, code not working is bad code indeed!

  in RemoveRETNs
      toOutput[currentLoc - 0x00400000] = b'\xCC' TypeError: 'bytes' object does not support item assignment

我该如何解决这个问题?

How can I fix this?

inputFile = 'original.exe'
outputFile = 'output.txt'
patchedFile = 'original_patched.exe'

def GetFileContents(filename):
    f = open(filename, 'rb')
    fileContents = f.read()
    f.close()

    return fileContents

def FindAll(fileContents, strToFind):
    found = []

    lastOffset = -1

    while True:
        lastOffset += 1
        lastOffset = fileContents.find(b'\xC3\xCC\xCC\xCC\xCC', lastOffset)

        if lastOffset != -1:
            found.append(lastOffset)
        else:
            break

    return found

def FixOffsets(offsetList):
    for current in range(0, len(offsetList)):
        offsetList[current] += 0x00400000
    return offsetList

def AbsentFromList(toFind, theList):
    for i in theList:
        if i == toFind:
            return True
    return False

# Outputs the original file with all RETNs replaced with INT3s.
def RemoveRETNs(locationsOfRETNs, oldFilesContents, newFilesName):
    target = open(newFilesName, 'wb')

    toOutput = oldFilesContents

    for currentLoc in locationsOfRETNs:
        toOutput[currentLoc - 0x00400000] = b'\xCC'

    target.write(toOutput)

    target.close()

fileContents = GetFileContents(inputFile)
offsets = FixOffsets(FindAll(fileContents, '\xC3\xCC\xCC\xCC\xCC'))
RemoveRETNs(offsets, fileContents, patchedFile)

我做错了什么,我能做些什么来解决它?代码示例?

What am I doing wrong, and what can I do to fix it? Code sample?

推荐答案

GetFileContentsreturn语句改成

return bytearray(fileContents)

其余的应该可以工作.您需要使用 bytearray 而不是 bytes 仅仅因为前者是可变的(读/写),后者(这是您现在使用的)是不可变的(读- 仅).

and the rest should work. You need to use bytearray rather than bytes simply because the former is mutable (read/write), the latter (which is what you're using now) is immutable (read-only).

这篇关于项目分配给字节对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆