1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| #include <iostream> #include <cstdint> #include <string.h> class VirtualMachine { public: static const int MEM_SIZE = 256; static const int NUM_REGISTERS = 8; uint8_t MEMORY[MEM_SIZE] = {0}; uint8_t REGISTERS[NUM_REGISTERS] = {0}; uint8_t* COMMANDS; int PC = 0; bool ZF = false; void run() { bool running = true; while (running) { uint8_t opcode = COMMANDS[PC]; switch (opcode) { case 0x01: { uint8_t reg = COMMANDS[PC + 1]; uint8_t imm = COMMANDS[PC + 2]; REGISTERS[reg] = imm; PC += 3; } break; case 0x02: { uint8_t reg1 = COMMANDS[PC + 1]; uint8_t addr = COMMANDS[PC + 2]; uint8_t reg2 = COMMANDS[PC + 3]; REGISTERS[reg1] = MEMORY[addr+REGISTERS[reg2]]; PC += 4; } break; case 0x03: { uint8_t addr = COMMANDS[PC + 1]; uint8_t reg1 = COMMANDS[PC + 2]; uint8_t reg2 = COMMANDS[PC + 3]; MEMORY[addr+REGISTERS[reg1]] = REGISTERS[reg2]; PC += 4; } break; case 0x04: { uint8_t reg1 = COMMANDS[PC + 1]; uint8_t reg2 = COMMANDS[PC + 2]; uint8_t dest = COMMANDS[PC + 3]; REGISTERS[dest] = REGISTERS[reg1] + REGISTERS[reg2]; PC += 4; } break; case 0x05: { uint8_t reg1 = COMMANDS[PC + 1]; uint8_t imm = COMMANDS[PC + 2]; uint8_t dest = COMMANDS[PC + 3]; REGISTERS[dest] = REGISTERS[reg1] % imm; PC += 4; } break; case 0x06: { uint8_t reg1 = COMMANDS[PC + 1]; uint8_t reg2 = COMMANDS[PC + 2]; uint8_t dest = COMMANDS[PC + 3]; REGISTERS[dest] = REGISTERS[reg1] ^ REGISTERS[reg2]; PC += 4; } break; case 0x07: { uint8_t reg = COMMANDS[PC + 1]; REGISTERS[reg]++; PC += 2; } break; case 0x08: { uint8_t reg1 = COMMANDS[PC + 1]; uint8_t imm = COMMANDS[PC + 2]; ZF = (REGISTERS[reg1] == imm); PC += 3; } break; case 0x09: { uint8_t addr = COMMANDS[PC + 1]; PC = addr; } break; case 0x0A: { uint8_t addr = COMMANDS[PC + 1]; if (ZF) { PC = addr; } else { PC += 2; } } break; case 0x0B: { uint8_t reg = COMMANDS[PC + 1]; uint8_t addr = COMMANDS[PC + 2]; REGISTERS[reg] = MEMORY[REGISTERS[addr]]; PC += 3; } break; case 0xFF: { running = false; } break; default: std::cerr << "Unknown opcode: " << static_cast<int>(opcode) << std::endl; running = false; break; } } } }; unsigned char array[128];
void __attribute__((constructor)) before_main() { for (int i = 0; i < 128; i++) { array[i] = i; } srand(0x114514); for (int i = 127; i > 0; i--) { int j = rand() % (i + 1); int temp = array[i]; array[i] = array[j]; array[j] = temp; } }
uint8_t program[] = { 0x01, 0x00, 0x00, 0x08, 0x00, 0x18, 0x0A, 35, 0x02, 0x01, 0x80, 0x00, 0x04, 0x01, 0x00, 0x01, 0x05, 0x01, 0x80, 0x01, 0x0B, 0x02, 0x01, 0x06, 0x02, 0x00, 0x02, 0x03, 0x80, 0x00, 0x02, 0x07, 0x00, 0x09, 0x03, 0xFF };
int main() { VirtualMachine vm; uint8_t flag[25]; uint8_t pw[24]={0x28,0x79,0x17,0x4,0xc,0x73,0x26,0x36,0x50,0x39,0x7e,0x24,0x51,0x17,0x44,0x25,0x6,0x70,0x4d,0x40,0x79,0x35,0x73,0x21}; printf("\033[31mCongratulations on persevering until the fifth week of the Newstar CTF!!!\033[0m\n"); printf("\033[32mWhether or not you can create this VM, I believe it can give you something.\033[0m\n"); printf("\033[33m\033[44mPlease input your flag:\033[0m");
scanf("%24s",flag); if (getchar() != '\n') { printf("no"); return 0; } for (int i = 0; i < 128; i++) { vm.MEMORY[i] = array[i]; } for (int i = 0; i < 24; i++) { vm.MEMORY[128 + i] = flag[i]; }
vm.COMMANDS = program; vm.run(); int jd=0; for (int i = 0; i < 24; i++) { if(vm.MEMORY[128 + i]==pw[i]) { jd++;
} if(jd==24) { printf("yes"); return 0; }
} printf("no"); return 0; }
|