6 context.log_level = "debug"
8 instructions_before = """
9 #### create literal as a function
15 instructions_to_encode = """
19 #00 # end instructions # bottom of the stack
21 instructions_after = """
22 01 # index of function
23 a0 # upon detection of a0, the index is popped as well (= 0x01), everything is copied into the function stack until 0xa1 is reached on ->data, then ->data is empty
30 # avoid 00 by encoding it as 0x11 0x11 and XORing it
31 11 # 0x11 is on ->data
32 11 # 0x11 0x11 is on ->data
33 84 # 0x00 is on ->data
34 80 # 0x80 is on ->data
37 # copy literal from function stack to ->data (i.e. call the function)
38 # literal must end with 0x00 for the print ping pong
40 #### print the encoded instructions with a print ping pong
42 c1 # the whole literal function is pushed to ->code; if the function only contains instructions < 0x80, they will be copied to ->data
47 80 # 0xa1 is on ->data (end of function)
50 80 # 0xe3 is on ->data (call function 3)
53 80 # 0xb0 is on ->data (print)
56 80 # 0x91 is on ->data (duplicate)
59 a0 # create function (i.e. print and call 0xe3; if 0x00 was popped [which was duplicated], function is not called)
64 80 # 0xa1 is on ->data (end of function)
67 80 # 0xe2 is on ->data (call function 2)
70 80 # 0xb0 is on ->data (print)
73 80 # 0x91 is on ->data (duplicate)
76 a0 # create function (i.e. print and call 0xe2; if 0x00 was popped [which was duplicated], function is not called)
78 # print encoded instructions
80 c2 # call the print function (prints everything until and including 0x00 in vm->data)
82 #### decode the instructions and print them with a print ping pong
84 c1 # the whole literal function is pushed to ->code; if the function only contains instructions < 0x80, they will be copied to ->data
89 80 # 0xa1 is on ->data (end of function)
92 80 # 0xe5 is on ->data (call function 5)
95 80 # 0x91 is on ->data (duplicate)
98 a0 # create function (i.e. print and call 0xe5; if 0x00 was popped [which was duplicated], function is not called)
103 80 # 0xa1 is on ->data (end of function)
106 80 # 0xc4 is on ->data (call function 4)
109 80 # 0xb0 is on ->data (print)
112 80 # 0x84 is on ->data
115 80 # 0x81 is on ->data
118 80 # 0x84 is on ->data
121 a0 # create function (i.e. print and call 0xe4; if 0x00 was popped [which was duplicated], function is not called)
123 # decode and print instructions
128 def assemble(instructions):
129 # print(instructions)
131 instructions = re.sub(r"#.*", "", instructions)
133 instructions = re.sub(r"\s+", "", instructions)
134 # print(instructions)
135 return binascii.unhexlify(instructions)
138 def encode(bytecode):
143 raise ValueError("0x00 is not supported!")
151 result.append(b ^ 0xff)
153 result.append(0x00) # end of encoded sequence
159 assemble(instructions_before)
160 + encode(assemble(instructions_after))
161 #+ encode(assemble(instructions_to_encode))
162 + assemble(instructions_after)
165 print(binascii.hexlify(bytecode))
167 #vm = process("./vm-chal")
168 vm = remote('3.93.128.89', 1214)
169 vm.recvuntil("Length of")
170 vm.sendline(str(len(bytecode)))
171 vm.recvuntil("Enter your")
179 #with open("bytecode.bin", "wb") as f:
182 #with open("input.bin", "wb") as f:
183 # f.write(p32(len(bytecode)))