Recompile binary program written in "new" language

239 views Asked by At

So I have the following challenge: I receive binary file. When opened in notepad++, first line says:

This program is developed with specific programming language. Opcodes: 0x3F byte - gotox byte, 0x40 byte - gotoy byte, 0x2400 - print ' ', 0x2401 - print ','

Iv'e never done anything like that but I would like to know how to crack this question. Basicaly the binary file is a function, I need to recompile it and extract the output.

My idea was opening the file with C in binary format, and reading each byte. But I'm not sure how to exactly do it.. what should be the procedure. Read each byte and then what?

Thank you for any help :)

EDIT: Thanks to clbx I made some real progress, But I'm stuck at the end! This is my simple C code:

#include <stdio.h>
#include <stdlib.h>

void gotoxy(int x, int y)
{
    printf("%c[%d;%df", 0x1B, y, x);
}

int main(int argc, char const *argv[])
{
    FILE *fp = fopen("elbitsystems.elbit", "rb");
    unsigned char byte, x, y, print_code;
    while (fread(&byte, 1, 1, fp) != 0)
    {
        switch (byte)
        {
        case 0x3F: // gotox byte
            fread(&x, 1, 1, fp);
            break;
        case 0x40: // gotoy byte
            fread(&y, 1, 1, fp);
            break;
        case 0x24: // print
            fread(&print_code, 1, 1, fp);
            if (print_code == 1)
            {
                gotoxy(x, y);
                printf(",");
            }
            break;
        default:
            break;
        }
    }
    fclose(fp);
    printf("\n");
    return 0;
}

And this is the output I get: enter image description here

I can really see the answer ("Sysco..?" But some of it is messed up and I dont know how. I found the gotoxy() function on line and it seems to work.. but not quite. I tried not using it and instead created 2d array size 255,255 and wrote it to a text file after while loop, result was the same as picture above. Any idea what I can do? I feel so close to the finish.. and don't know what to do haha. THANKS!!

2

There are 2 answers

3
clbx On BEST ANSWER

Each Opcode is an instruction, the byte given determines what the program should do.

Looks like you have only 4 opcodes, makes it pretty easy:

0x3F byte - gotox byte,

0x40 byte - gotoy byte,

0x2400 - print ' ',

0x2401 - print ','

You're on the right track, open the file and read byte by byte. When you get a byte(s) that you know (0x3F, 0x40, 0x2400, 0x2401), execute its correleated function (goto x, goto y, print ' ', print '.'

1
jonathan On

I tryed to solve this challange also, you did nice job on it! notice that in the middle of the file there is more opcode char. "2=%, 4=,, 16=#, 32=(, 64=/, 128=*" add them to your code and you will succsesfully finish this challange!