Skip to content

Instantly share code, notes, and snippets.

@heinrich5991
Created November 19, 2014 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heinrich5991/0e37dcac4e54f83fcc98 to your computer and use it in GitHub Desktop.
Save heinrich5991/0e37dcac4e54f83fcc98 to your computer and use it in GitHub Desktop.
Teeworlds <0.6.3 Patcher
from shutil import copyfile
PATTERN=b"\x80\x03\x00\x00"
PATTERN_JG=b"\x0f\x8f"
PATTERN_JL=b"\x0f\x8c"
PATTERN_JA=b"\x0f\x87"
PATTERN_JB=b"\x0f\x82"
PATTERN_JG_S=b"\x7f"
PATTERN_JL_S=b"\x7c"
PATTERN_JA_S=b"\x77"
PATTERN_JB_S=b"\x72"
PATTERN_MOV=b"\xc7"
PATTERN_IMUL=b"\x69"
def main(argv):
if len(argv) != 2:
print("USAGE: {} <filename>".format(argv[0]))
return 1
filename = argv[1] + '_patched.exe'
copyfile(argv[1], filename)
file = open(filename, 'rb+')
contents = file.read()
new_offset = contents.find(PATTERN, 0)
while new_offset != -1:
offset = new_offset
new_offset = contents.find(PATTERN, new_offset + 1)
offset_prev_mov = contents.rfind(PATTERN_MOV, offset - 10, offset)
offset_prev_imul = contents.rfind(PATTERN_IMUL, offset - 10, offset)
if offset_prev_mov != -1: offset_prev_mov = offset - offset_prev_mov
if offset_prev_imul != -1: offset_prev_imul = offset - offset_prev_imul
print("{:2d}, {:2d}".format(offset_prev_mov, offset_prev_imul), end=", ")
if offset_prev_mov == -1 and offset_prev_imul == -1:
print("end")
continue
print("{:02x} {:02x} {:02x}".format(contents[offset - 3], contents[offset - 2], contents[offset - 1]), end=", ")
offset_jg = contents.find(PATTERN_JG, offset, offset+128)
offset_jl = contents.find(PATTERN_JL, offset, offset+128)
offset_jg_s = contents.find(PATTERN_JG_S, offset, offset+128)
offset_jl_s = contents.find(PATTERN_JL_S, offset, offset+128)
if offset_jg != -1: offset_jg -= offset
if offset_jl != -1: offset_jl -= offset
if offset_jg_s != -1: offset_jg_s -= offset
if offset_jl_s != -1: offset_jl_s -= offset
print("found magic sequence, offset={:08x} next={:2d},{:2d},{:2d},{:2d}".format(
offset,
offset_jg,
offset_jl,
offset_jg_s,
offset_jl_s,
), end=", ")
if offset_jg != -1:
print("patching JG to JA", end=", ")
file.seek(offset + offset_jg)
file.write(PATTERN_JA)
elif offset_jl != -1:
print("patching JL to JB", end=", ")
file.seek(offset + offset_jl)
file.write(PATTERN_JB)
elif offset_jg_s != -1:
print("patching JG_S to JA_S", end=", ")
file.seek(offset + offset_jg_s)
file.write(PATTERN_JA_S)
elif offset_jl_s != -1:
print("patching JL_S to JB_S", end=", ")
file.seek(offset + offset_jl_s)
file.write(PATTERN_JB_S)
print("end")
offset = contents.find(PATTERN, offset + 1)
print()
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment