Skip to content

Instantly share code, notes, and snippets.

@timnew
Created March 31, 2018 13:26
Show Gist options
  • Save timnew/ff1d491b462755a18b232a8da6ecdb97 to your computer and use it in GitHub Desktop.
Save timnew/ff1d491b462755a18b232a8da6ecdb97 to your computer and use it in GitHub Desktop.
TimNew's BitSlicer Multiple Code Block Script
#Edit Infinite Aether!
#Introduction to scripting: https://github.com/zorgiepoo/Bit-Slicer/wiki/Introduction-to-Scripting
from bitslicer import VirtualMemoryError, DebuggerError
def locateSignature(signature):
debug.log("Searching signature...")
scanResult = vm.scanByteString(signature)
if len(scanResult) == 0:
debug.log("Signature cannot be found.")
return None
debug.log("Found address: " + str(scanResult))
return scanResult
def assembleCodeWithSize(code, size):
codeBuffer = bytes(debug.assemble(code))
nopCount = size - len(codeBuffer)
if nopCount < 0:
raise ValueError('spaceOverflow')
nops = b'\x90' * nopCount
fullCode = codeBuffer + nops
return fullCode
def codeSizeFromSignature(signature):
return (len(signature) + 1) // 3
class SignatureNotFound(Exception):
"""Signautre block cannot be found"""
pass
class CodeBlock(object):
SIGNATURE = ''
CODE_SIZE = 0
HIJACK_CODE = """ """
CRITICAL = True
def name(self):
return self.__class__.__name__
def scan(self):
debug.log("Scanning " + self.name() + " ..." )
self.addresses = locateSignature(self.SIGNATURE)
if self.CRITICAL and self.addresses is None:
raise SignatureNotFound
def backupCode(self):
self.originalCodes = [debug.readBytes(address, self.CODE_SIZE) for address in self.addresses]
def restoreCode(self):
for address, code in zip(self.addresses, self.originalCodes):
debug.writeBytes(address, code)
def overwriteCode(self):
code = assembleCodeWithSize(self.HIJACK_CODE, self.CODE_SIZE)
for address in self.addresses:
debug.writeBytes(address, code)
def enable(self, scan = True):
debug.log("Enabling " + self.name() + " ..." )
if scan:
self.scan()
if self.addresses is None:
debug.log("[Warning] Non critical block" + self.name() + " cannot be found.")
debug.notify(self.name(), "Non critical block not found.")
return
self.backupCode()
self.overwriteCode()
debug.log(self.name() + " is enabled.")
def disable(self):
debug.log("Disabling " + self.name() + " ...")
if self.addresses is None:
debug.log(self.name() + "is ignored")
return
self.restoreCode()
debug.log(self.name() + " is disabled")
class MultiBlockScript(object):
codeBlocks = [
]
def enable(self):
debug.log("Enabling...")
debug.log("Scanning....")
for block in self.codeBlocks:
block.scan()
debug.log("Apply....")
for block in self.codeBlocks:
block.enable(False)
debug.log("Enabled")
def disable(self):
debug.log("Disabling...")
for block in self.codeBlocks:
block.disable()
debug.log("Disabled")
def __init__(self):
self.enable()
def finish(self):
self.disable()
class Script(MultiBlockScript):
class CriticalBlock(CodeBlock):
"""
<debugger dump>
"""
SIGNATURE = '<signature>'
CODE_SIZE = codeSizeFromSignature(SIGNATURE)
HIJACK_CODE = """
<code>
"""
CRITICAL = True
class NoneCriticalBlockA(CodeBlock):
"""
<debugger dump>
"""
SIGNATURE = '<signature>'
CODE_SIZE = codeSizeFromSignature(SIGNATURE)
HIJACK_CODE = """
<code>
"""
CRITICAL = False
class NoneCriticalBlockB(CodeBlock):
"""
<debugger dump>
"""
SIGNATURE = '<signature>'
CODE_SIZE = codeSizeFromSignature(SIGNATURE)
HIJACK_CODE = """
<code>
"""
CRITICAL = False
codeBlocks = [
CriticalBlock(),
NoneCriticalBlockA(),
NoneCriticalBlockB()
]
def __init__(self):
super().__init__()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment