Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // by petter wahlman, http://www.twitter.com/badeip
- // solution to part 3 of canyoucrackit.co.uk
- //
- // hex.h is available here: http://pastebin.com/yj3xsZLW
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <termios.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <stdint.h>
- #include "hex.h"
- uint8_t mem[] = {
- 0x31, 0x04, 0x33, 0xaa, 0x40, 0x02, 0x80, 0x03, 0x52, 0x00, 0x72, 0x01, 0x73, 0x01, 0xb2, 0x50,
- 0x30, 0x14, 0xc0, 0x01, 0x80, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x98, 0xab, 0xd9, 0xa1, 0x9f, 0xa7, 0x83, 0x83, 0xf2, 0xb1, 0x34, 0xb6, 0xe4, 0xb7, 0xca, 0xb8,
- 0xc9, 0xb8, 0x0e, 0xbd, 0x7d, 0x0f, 0xc0, 0xf1, 0xd9, 0x03, 0xc5, 0x3a, 0xc6, 0xc7, 0xc8, 0xc9,
- 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9,
- 0xda, 0xdb, 0xa9, 0xcd, 0xdf, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
- 0x26, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
- 0x7d, 0x1f, 0x15,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0xf2,
- 0xe7, 0xc0, 0xeb, 0xff, 0xe9, 0xa3, 0xbf, 0xa1, 0xab, 0x8b, 0xbb, 0x9e, 0x9e, 0x8c, 0xa0, 0xc1,
- 0x9b, 0x5a, 0x2f, 0x2f, 0x4e, 0x4e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- };
- #define R0 0
- #define R1 1
- #define R2 2
- #define R3 3
- #define CS 4
- #define DS 5
- #define FL 6
- #define IP 7
- struct insn {
- union {
- uint8_t value;
- struct {
- uint8_t oper1 : 4;
- uint8_t mod : 1;
- uint8_t opcode: 3;
- };
- };
- uint8_t oper2;
- };
- struct vm {
- uint16_t r[8];
- uint8_t *mem;
- struct insn *insn;
- };
- void print_insn(struct vm *);
- void jmp(struct vm *);
- void movr(struct vm *);
- void movm(struct vm *);
- void add(struct vm *);
- void xor(struct vm *);
- void cmp(struct vm *);
- void jmpe(struct vm *);
- void hlt(struct vm *);
- enum { R00, IR0, SR0, RR0, RI0, RSR, SRR, NA0 };
- struct {
- uint8_t opcode;
- uint8_t w1, w2;
- const char *name;
- uint8_t fmt1, fmt2;
- void (*dispatch)(struct vm *vm);
- } mnemonic[] = {
- { 0, 0, 2, "jmp ", R00, IR0, jmp },
- { 1, 2, 2, "movr", RR0, RI0, movr },
- { 2, 2, 2, "movm", RSR, SRR, movm },
- { 3, 2, 2, "add ", RR0, RI0, add },
- { 4, 2, 2, "xor ", RR0, RI0, xor },
- { 5, 2, 2, "cmp ", RR0, RI0, cmp },
- { 6, 1, 2, "jmpe", R00, SR0, jmpe },
- { 7, 1, 1, "hlt ", NA0, NA0, hlt }
- };
- void print_insn(struct vm *vm)
- {
- struct insn *insn = vm->insn;
- int type;
- const char *reg[] = {
- "r0", "r1", "r2", "r3", "cs", "ds", "fl", "ip"
- };
- const char *fmt[] = {
- /* 0: R00 */ "%02X:%02X: %02X %s %s ",
- /* 1: IR0 */ "%02X:%02X: %02X %02X %s %02X:%s ",
- /* 1: SR0 */ "%02X:%02X: %02X %02X %s %s:%s ",
- /* 2: RR0 */ "%02X:%02X: %02X %02X %s %s, %s ",
- /* 3: RI0 */ "%02X:%02X: %02X %02X %s %s, %02X ",
- /* 4: RSR */ "%02X:%02X: %02X %02X %s %s, [%s:%s] ",
- /* 5: SRR */ "%02X:%02X: %02X %02X %s [%s:%s], %s ",
- /* 6: NA0 */ "%02X:%02X: %02X %s N/A ",
- };
- type = insn->mod ? mnemonic[insn->opcode].fmt2 : mnemonic[insn->opcode].fmt1;
- switch(type) {
- case R00:
- printf(fmt[type], vm->r[CS], vm->r[IP], vm->mem[vm->r[IP]], mnemonic[insn->opcode].name, reg[insn->oper1]);
- break;
- case IR0:
- printf(fmt[type], vm->r[CS], vm->r[IP], vm->mem[vm->r[IP]], vm->mem[vm->r[IP]+1], mnemonic[insn->opcode].name, insn->oper2, reg[insn->oper1]);
- break;
- case SR0:
- printf(fmt[type], vm->r[CS], vm->r[IP], vm->mem[vm->r[IP]], vm->mem[vm->r[IP]+1], mnemonic[insn->opcode].name, reg[insn->oper1], reg[insn->oper2]);
- break;
- case RR0:
- printf(fmt[type], vm->r[CS], vm->r[IP], vm->mem[vm->r[IP]], vm->mem[vm->r[IP]+1], mnemonic[insn->opcode].name, reg[insn->oper1], reg[insn->oper2]);
- break;
- case RI0:
- printf(fmt[type], vm->r[CS], vm->r[IP], vm->mem[vm->r[IP]], vm->mem[vm->r[IP]+1], mnemonic[insn->opcode].name, reg[insn->oper1], insn->oper2);
- break;
- case RSR:
- printf(fmt[type], vm->r[CS], vm->r[IP], vm->mem[vm->r[IP]], vm->mem[vm->r[IP]+1], mnemonic[insn->opcode].name, reg[insn->oper1], reg[DS], reg[insn->oper2]);
- break;
- case SRR:
- printf(fmt[type], vm->r[CS], vm->r[IP], vm->mem[vm->r[IP]], vm->mem[vm->r[IP]+1], mnemonic[insn->opcode].name, reg[DS], reg[insn->oper1], reg[insn->oper2]);
- break;
- case NA0:
- printf(fmt[type], vm->r[CS], vm->r[IP], vm->mem[vm->r[IP]], mnemonic[insn->opcode].name);
- break;
- default:
- printf("FUCKED\n");
- }
- fflush(stdout);
- }
- void jmp(struct vm *vm)
- {
- struct insn *insn = vm->insn;
- if (!insn->mod) // r1
- vm->r[IP] = vm->r[insn->oper1];
- else { // r2:r1
- vm->r[CS] = insn->oper2;
- vm->r[IP] = vm->r[insn->oper1];
- }
- }
- void movr(struct vm *vm)
- {
- struct insn *insn = vm->insn;
- if (!insn->mod) // r1, r2
- vm->r[insn->oper1] = vm->r[insn->oper2];
- else // rx, imm
- vm->r[insn->oper1] = insn->oper2;
- vm->r[IP] += 2;
- }
- void movm(struct vm *vm)
- {
- struct insn *insn = vm->insn;
- if (!insn->mod)
- vm->r[insn->oper1] = vm->mem[(vm->r[DS] << 4) + vm->r[insn->oper2]];
- else
- vm->mem[(vm->r[DS] << 4) + vm->r[insn->oper1]] = vm->r[insn->oper2];
- vm->r[IP] += 2;
- }
- void add(struct vm *vm)
- {
- struct insn *insn = vm->insn;
- if (!insn->mod) // r1, r2
- vm->r[insn->oper1] += vm->r[insn->oper2];
- else // r1, imm
- vm->r[insn->oper1] += vm->insn->oper2;
- vm->r[IP] += 2;
- }
- void xor(struct vm *vm)
- {
- struct insn *insn = vm->insn;
- if (!insn->mod) // r1, r2
- vm->r[insn->oper1] ^= vm->r[insn->oper2];
- else // r1, imm
- vm->r[insn->oper1] ^= vm->insn->oper2;
- vm->r[IP] += 2;
- }
- void cmp(struct vm *vm)
- {
- struct insn *insn = vm->insn;
- if (!insn->mod) { // r1, r2
- if (vm->r[insn->oper1] == vm->r[insn->oper2])
- vm->r[FL] = 0;
- else if (vm->r[insn->oper1] < vm->r[insn->oper2])
- vm->r[FL] = 0xff;
- else
- vm->r[FL] = 1;
- } else { // r1, imm
- if (vm->r[insn->oper1] == vm->insn->oper2)
- vm->r[FL] = 0;
- else if (vm->r[insn->oper1] < vm->insn->oper2)
- vm->r[FL] = 0xff;
- else
- vm->r[FL] = 1;
- }
- vm->r[IP] += 2;
- }
- void jmpe(struct vm *vm)
- {
- struct insn *insn = vm->insn;
- if (!vm->r[FL]) {
- if (!insn->mod) { // r1
- vm->r[IP] = vm->r[insn->oper1];
- } else { // r2:r1
- vm->r[CS] = insn->oper2;
- vm->r[IP] = (vm->r[CS] << 4) + vm->r[insn->oper1];
- }
- } else {
- vm->r[IP]++; // nop
- }
- }
- void hlt(struct vm *vm)
- {
- printf("\nmem:\n");
- print_hex(vm->mem, sizeof(mem), 20);
- exit(0);
- }
- static struct termios oldt;
- void stdin_restore(void)
- {
- tcsetattr(0, TCSANOW, &oldt);
- }
- void stdin_nowait(void)
- {
- struct termios newt;
- tcgetattr(0, &oldt);
- newt = oldt;
- newt.c_lflag &= ~(ICANON | ECHO);
- tcsetattr(0, TCSANOW, &newt);
- atexit(stdin_restore);
- }
- int main(int argc, char **argv)
- {
- uint8_t interactive = 0;
- uint8_t dump_mem = 0;
- uint8_t verbose = 0;
- uint32_t i;
- struct vm vm;
- system("clear");
- for (i = 1; i < argc; i++) {
- if (!strncmp(argv[i], "-m", 2))
- dump_mem = 1;
- else if (!strncmp(argv[i], "-v", 2))
- verbose = 1;
- else if (!strncmp(argv[i], "-i", 2)) {
- interactive = 1;
- stdin_nowait();
- }
- }
- memset(&vm, 0, sizeof(vm));
- vm.mem = mem;
- vm.r[DS] = 0x10;
- while (1) {
- uint8_t c;
- vm.insn = (struct insn * )&mem[(vm.r[CS] << 4) + vm.r[IP]];
- print_insn(&vm);
- if (verbose)
- printf(" - m:%d [ r0:%02X r1:%02X r2:%02X r3:%02X cs:%02X ds:%02X fl:%02X ]\n",
- vm.insn->mod, vm.r[R0], vm.r[R1], vm.r[R2], vm.r[R3], vm.r[CS], vm.r[DS], vm.r[FL]);
- else
- putchar('\n');
- mnemonic[vm.insn->opcode].dispatch(&vm);
- if (dump_mem)
- print_hex(mem + 256, 512, 0);
- if (interactive) {
- c = getchar();
- switch(c) {
- case 'm': print_hex(mem, sizeof(mem), 0); break;
- case 'c': interactive = 0; break;
- case 'q': goto out;
- }
- }
- }
- out:
- print_hex(mem, sizeof(mem), 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement