cephurs icon

mtb

cephurs | PRO | 04/26/18 12:06:06 AM UTC | 0 ⭐ | 887 👁️ | Never ⏰ | []
Python |

1.26 KB

|

None

|

0 👍

/

0 👎

class VirtualMachine:
    def __init__(self, ram):
        self.reg1 = 0
        self.ram = ram
        self.cs = 0xFF
        self.eip = 0
 
    def run(self):
        while 1:
            inst = self.ram[self.cs+self.eip+0]
            offset = self.ram[self.cs+self.eip+1]
            value = self.ram[self.cs+self.eip+2]
 
            if self.instruction_handler(inst, offset, value) == 1:
                break
 
            self.eip += 3
 
    def instruction_handler(self, inst, offset, value):
        if inst == 1:
            self.ram[offset] = value
            print("moved value (0x%0.2X) to ram offset: 0x%0.2X" % (value, offset))
 
        elif inst == 2:
            self.reg1 = self.ram[offset]
            print("moved value (0x%0.2X) to reg1" % self.ram[offset])
 
        elif inst == 3:
            self.ram[offset] ^= self.reg1
            print("xor'd value at ram offset (0x%0.2X) with content of reg1 (0x%0.2X)" % (offset, self.reg1))
 
        elif inst == 4:
            print("got stop instruction, extracting flag...")
            print("%s" % self.ram.split('\0')[0])
            return 1
 
        return 0
 
 
f = open("ram.bin", "rb")
initial_state = bytearray(f.read())
f.close()
 
vm = VirtualMachine(initial_state)
vm.run()

Comments