Python Forum

Full Version: Creating Disassembler for a bin file(total beginner)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I have a reverse engineering assignment which I need to understand the Stack data structure and thus find a flag within a Bin file

The problem is, there is not one disassembler that can give me the assembly code, so I'm trying to make one on my own even due I'm a total beginner and this is the first time I'm trying write on anything, I'm doing it on Python


here is a small part of the Structure instructions:
INSTRUCTION SET
IMPORTANT!
IP is incremented as the instruction is read (before decode/execute).
This increment is not mentioned in the instruction pseudo-code. Therefore, every instruction that adds an offset to IP will result in IP = IP + offset + 1.
An instruction that resets IP as IP = new_value discards the increment.
INSTRUCTION PSEUDO CODE NOTATIONS
stack.push([value]) - pushes the value to the stack
stack.pop() - dequeue the last value pushed to the stack .
a = stack.pop() - dequeue the last value pushed to the stack, save value to pseudo-variable ‘a’.
stack.empty() - true if there are no more values on the stack, false otherwise
stack[N] - the value of the Nth element on the stack
IP - the instruction pointer.
STACK INSTRUCTIONS:
Push <value>
•	opcode is 0x80 + value
•	Pushes the value to the stack, stack[0] is now , stack[1] is now the previous stack[0] value, and so on.
•	value <= 0x7f
•	Push 0x32 is encoded as 0xB2.
stack.push(value)
________________________________________
Load <offset>
•	opcode is 0x40 + offset
•	Pushes the value at stack[offset] to the stack.
•	value <= 0x3f
•	Load 0x12 is encoded as 0x52.
•	Loading from an offset out of bounds (i.e pushing 10 values and loading from offset 12) will cause a fault and execution will terminate.
stack.push(stack[offset])
Someone was trying to help me but the communication was cut off

So this is what I made up until now:

program = '95 E8 8F E1 88 E4 88 E7 B5 D2 BC D5 B9 D5 BA E8 8F E1 88 E4 88 E7 B5 D2 BC D5 B9 D5 BA E8 BA D5 8D F6 91 F0 9C FA 8B 18 08 8C 11 41 8A 80 01 14 B0 81 10 B1 09 AF 10 42 42 80 A5 14 42 21 80 A0 14 80 21 44 9B 14 20 82 42 02 82 45 02 21 22 00 82 21 02 21 20 42 42 A4 80 01 11 82 03 00 22 20 20 23 20 21 20 12'

for  opCode in program.split(' '):
  print(opCode)

 if b == 0x80
    print 'PUSH'
  else if b == 0x40
   print 'LOAD'
My question is if it will indeed be able to tell me which which byte is push or load and how do i include the value and the offset to the equation?
You may want to take a look at: https://pypi.org/project/kcshell/
to get some ideas.
Source code here: https://github.com/fdiskyou/kcshell