GAHH,代码不工作确实是错误的代码!
in RemoveRETNs toOutput [currentLoc - 0x00400000] = b'\ xCC'TypeError:'bytes'对象不支持项目赋值
我怎样才能解决这个问题:
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)
Run Code Online (Sandbox Code Playgroud)
我做错了什么,我该怎么做才能解决它?代码示例?
Ale*_*lli 19
将return声明更改GetFileContents为
return bytearray(fileContents)
Run Code Online (Sandbox Code Playgroud)
其余的应该工作.你需要使用bytearray而不是bytes简单地因为前者是读/写,后者(你现在正在使用的)是只读的.
Bytestrings(和一般的字符串)是Python中的不可变对象.创建它们后,您无法更改它们.相反,你必须创建一个碰巧有一些旧内容的新的.(例如,使用基本字符串newString = oldString[:offset] + newChar + oldString[offset+1:]等.)
相反,您可能希望首先将bytestring转换为字节列表,或者将bytearray转换为bytearray,然后在完成所有操作后将bytearray/list转换回静态字符串.这避免了为每个替换操作创建新字符串.