Python Forum
Creating Disassembler for a bin file(total beginner)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating Disassembler for a bin file(total beginner)
#1
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?
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  dictionary output to text file (beginner) Delg_Dankil 2 1,210 Jul-12-2023, 11:45 AM
Last Post: deanhystad
  Creating/Moving file DeadlyKnight 3 1,531 Mar-25-2022, 06:25 PM
Last Post: ibreeden
  creating dict out of CSV file without the headers ranbarr 6 2,674 May-09-2021, 08:26 PM
Last Post: ranbarr
  recursive procedure(total beginner) boris602 5 3,771 Dec-13-2017, 01:35 PM
Last Post: mpd
  Creating a file with variable name but distinct extension Moeniac 1 2,274 Nov-27-2017, 05:47 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020