![]() |
|
[C] Implement a simple VM [Challenge] - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] Implement a simple VM [Challenge] (/showthread.php?tid=6194018) Pages: Pages
1
2
|
Implement a simple VM [Challenge] - yuumi - Feb 17, 2022 I see a lot of posts regarding what people should do after learning the basics of C or asking questions regarding pointers and structures and I figured this would be a fun challenge to try. Implement a virtual machine which supports five registers: r0,r1,r2,r3,rax An instruction pointer register: rip Six operational instructions: move constant to a register move register to register add constant to a register add register to register subtract constant from a register subtract register from a register and the halt instruction which stops the vm execution. the semantics of the instructions should be: Instruction destination source Eg: Move rax 20 or Move rax, r0 Where destination is always a register source can be constant or register . and each instruction is 3 bytes. When you've completed it, you can push this bytecode through your VM to test Code uint8_t bytecode[] = {You can use these enums Code enum vm_op : uint8_t{RE: Implement a simple VM [Challenge] - movss - Feb 17, 2022 Implement a virtual machine that doesn't use the instruction cycle, and instead uses threaded coding to execute handlers pertaining to the machine's ISA. An example would include: mov eax, ecx > VM > tons of x86_64 opcodes that are responsible for handling your VMs opcodes. This would be utterly confusing to the reader, and in doing so would have to reverse each "handler" and figure out what they are doing. With dead code obfuscation, control flow obfuscation, opaque predicates this process becomes non trivial. But easily defeated with LLVM + MCSEMA RE: Implement a simple VM [Challenge] - yuumi - Feb 17, 2022 (Feb 17, 2022 02:53 PM)movss Wrote: Implement a virtual machine that doesn't use the instruction cycle, and instead uses threaded coding to execute handlers pertaining to the machine's ISA. It isn't meant to be an obfuscation challenge. It has nothing to do with manipulating the .text section at runtime and running the executable code through a separate handler which maps VM operations to actual instruction. It's a simple C based VM like a bytecode emulator. I'm not asking a beginner in C to write an entire obfuscation virtual machine. RE: Implement a simple VM [Challenge] - movss - Feb 17, 2022 (Feb 17, 2022 03:19 PM)yuumi Wrote: It isn't meant to be an obfuscation challenge. It has nothing to do with manipulating the .text section at runtime and running the executable code through a separate handler which maps VM operations to actual instruction. It's a simple C based VM like a bytecode emulator. doesn't pertain to obfuscation obfuscation is an additive obscurity measure for the aforementioned VM design using handlers that are threaded is far more a better learning experience than a trivial instruction cycle RE: Implement a simple VM [Challenge] - yuumi - Feb 17, 2022 (Feb 17, 2022 03:23 PM)movss Wrote: doesn't pertain to obfuscation The point is it's meant to be a trivial instruction cycle. If people want to do it they can. If they'd rather automate an entire function produce bytecode then compile byte code to be called and translate each instruction through a handler while the .text section of that function looks complete gibberish sure they can do that too. It wouldn't make sense to do that with what I asked, since you'd need to map almost the entire instruction set to a virtual instruction set and you'd probably need some disassembler engine to automate the process. RE: Implement a simple VM [Challenge] - EmacsSucksButILikeIt - Feb 17, 2022 I'm a bit out of touch with C and C++ but isn't the below C++ and this is a challenge for C. Code enum vm_op : uint8_t{RE: Implement a simple VM [Challenge] - movss - Feb 17, 2022 (Feb 17, 2022 10:04 PM)EmacsSucksButILikeIt Wrote: I'm a bit out of touch with C and C++ but isn't the below C++ and this is a challenge for C. semantics retard get the fuck out of this thread RE: Implement a simple VM [Challenge] - EmacsSucksButILikeIt - Feb 17, 2022 (Feb 17, 2022 10:22 PM)movss Wrote: semantics retard You understand this is for people that have a basic understanding of C and probably programming.... You shouldn't be posting confusing and conflicting information in your challenge. Nice challenge BTW. RE: Implement a simple VM [Challenge] - yuumi - Feb 17, 2022 (Feb 17, 2022 10:04 PM)EmacsSucksButILikeIt Wrote: I'm a bit out of touch with C and C++ but isn't the below C++ and this is a challenge for C. It's a C challenge. I guess you can use C++ but yes it's just semantics. You don't have to use those specific opcodes or even use enums. RE: Implement a simple VM [Challenge] - movss - Feb 17, 2022 (Feb 17, 2022 10:28 PM)EmacsSucksButILikeIt Wrote: You understand this is for people that have a basic understanding of C and probably programming.... You shouldn't be posting confusing and conflicting information in your challenge. bluh bluh bluh 😎 RE: Implement a simple VM [Challenge] - EmacsSucksButILikeIt - Feb 18, 2022 Might try this latter today. Quote:The return value register should be rax Does this mean you expect to support function calls? Also... Which direction are you moving your registers. Are you using AT&T or Intel style moves. RE: Implement a simple VM [Challenge] - yuumi - Feb 18, 2022 (Feb 18, 2022 03:36 AM)EmacsSucksButILikeIt Wrote: Might try this latter today. It’s intel syntax Semantic: Instruction destination source No it doesn't have to support function calls the result in rax portion was just something I threw in the bytecode example. You can consider that irrelevant. Maybe in the next challenge I can make it slightly more complicated by having a virtual stack, as well as a stack register rsp and an actual calling convention. I made some changes to the original design of the vm: have a look. RE: Implement a simple VM [Challenge] - EmacsSucksButILikeIt - Feb 18, 2022 (Feb 18, 2022 07:14 AM)yuumi Wrote: It’s intel syntax I'll use it(Rax) as an exit code for the byte code. This is so easy to implement in a functional language(OCaml)... In C its a bit of a mess. Code type instr_reg = R0 | R1 | Rax | RdxThe final register values. I think I got all the moves right. Quote:rax 9, rdx 0, r0 16, r1 0 I'll see if I can get a working C version soon. RE: Implement a simple VM [Challenge] - yuumi - Feb 18, 2022 (Feb 18, 2022 08:01 AM)EmacsSucksButILikeIt Wrote: I'll use it(Rax) as an exit code for the byte code. I did it in C which was relatively easy but that could just be because of my experience with C. Just to make it compatible to future changes , you can get rid of the rdx, and rcx register. Instead we'll just use registers r0,r1,r2,r3 and rax RE: Implement a simple VM [Challenge] - 0x9F - Feb 18, 2022 Did this in around 40 minutes (dont mind the dirt) enum/struct defs: Code enum vm_reg : unsigned char {mini-vm.c including execute/handler: Code #define MAX_OPCODE_STACK_SIZE 64 example usage: Code int main()Haven't tested this extensively and probably forgot small things but you get the point |