Writing a bad programming language in rust
”Backstory”
I was doing a “very hard” reversing challenge for DDC and figured i’d try writing my own “programming language”.
Sorry for all the quotation marks, nothing of this is supposed to be super serious.
I recently tried rust and thought it was cool and wanted to write some code with it. Perfect storm.
What was the challenge
You get zip with the binary and this python
Python code:
How to write a dumb language (for eel this time)
Steps:
- Find a way to parse input fx
instruction<space>char<space>char
so your computer knows what the text means in terms of structs // objects - Write what each instruction does with the data on some allocated memory
- Step through an array of instructions and data
- Know you have dumb language
I will now dump the source so you can see for yourself :)
TLDR - pattern matching OP
Solving it
Basically just write the GCD algorithm in the stupid language.
A simple python solution for GCD could be this
Not super fast, but very short and concise - neat.
Reversing rust sucks and takes lots of time. Since I made the source - this would be silly for me to act like i havent seen it before. I probably could write the whole thing up, but I dont think it would be worth it.
But retyping everything or stepping through it in GDB with some guesswork about what each instruction does would probably give you a pretty nice result.
How the langauge works
The program has a list of instructions, and a “virtual memory”.
A simple program could look like this
This code reads input, increments mempointer, reads input again, goes back to the 0th mem pointer, then prints, then goes forward again, then prints.
it just werks.
Here is a list of all the instructions and what they do.
- r: reads an integer to current memory pointer
- p: prints integer in current memory pointer
->
increments memory pointer<-
decrements memory pointer#
exits+<num>
adds number to current value-<num>
j subtracts number to current value\*<num>
multiplies number to current value/<num>,<num>
divides number with current value and stores remainder in address of second param=<num>
compares current value with number and stores result in the cmp_flagj<num>
jumps to that index of instructions so in the above example. ‘j0’ would jump to the first read!<num>
jump not equal. if the cmp flag is false, jump like above example.<num>
move. Moves the value of the number specified into the current value.^
clear. sets current value to 0:<instruction>,<times_to_loop>,<store_increment>
. Loop. example is easier here.:2,3,4
jumps 2 instructions back and increments address 4 one time. if address 4 has a value of 3 then dont do it anymore.
How to do GCD then?
Description
Now you can write marlang. Hope you will use this dumb langauge for another stupid esoteric CTF challenge wink wink.