#!/usr/bin/env python3 """ Toggle the bit at the specified offset. Syntax: chall byte-offset bit-offset https://unix.stackexchange.com/questions/196251/change-only-one-bit-in-a-file """ from tempfile import NamedTemporaryFile from os import chmod from subprocess import CalledProcessError, TimeoutExpired, check_call from shutil import copyfile yourFile = NamedTemporaryFile(delete=True) copyfile("/home/chall/chall",yourFile.name) chmod(yourFile.name,0o777) print("Welcome to the super Flipper.") print("====================================================") try: bytepos = int(input("Please enter the byte-offset you want to flip (0x80-0x139): "),16) bitpos = int(input("Please enter the bitposition you want to flip at byte-offset(7-0): "),16) except ValueError: print("numbers only please...;)") exit(-1) if(bytepos < 0x80 or bytepos > 0x139 or bitpos < 0 or bitpos > 7 ): print("behave kid, behave...ò.ó") exit(-1) patch = open(yourFile.name,"r+b") patch.seek(bytepos, 0) c = patch.read(1) toggled = bytes( [ ord(c)^(1< ============") except TimeoutExpired: print("\n============= got the flag?:> =============")