From 47f066f4d9d9a20b7f6c8ee963641e3b73650e2d Mon Sep 17 00:00:00 2001 From: Emile Date: Sat, 22 Feb 2020 16:03:52 +0100 Subject: super basci tournament --- src/r2pipe.go | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 72 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/r2pipe.go b/src/r2pipe.go index c6ad3fc..013478a 100644 --- a/src/r2pipe.go +++ b/src/r2pipe.go @@ -2,23 +2,83 @@ package main import ( "fmt" + "log" + "time" +) - "github.com/radare/r2pipe-go" +var ( + verbose *bool ) func main() { - // allocate 1024 bytes of memory - r2p, err := r2pipe.NewPipe("malloc://1024") - if err != nil { - panic(err) + + log.Println("---") + log.Println("[i] Parse the config") + config := parseConfig() + + // build the bots + log.Println("---") + log.Println("[i] Build the bots") + + for i := 0; i < 2; i++ { + bot := buildBot(config, "bots/warrior.asm") + config.Bots = append(config.Bots, bot) } - defer r2p.Close() + // bot2 := buildBot(config, "bots/warrior.asm") + // config.Bots = append(config.Bots, bot2) + // bot3 := buildBot(config, "bots/warrior.asm") + // config.Bots = append(config.Bots, bot3) + + // initialize the arena + log.Println("---") + log.Println("[i] Initialize the Arena") + r2p := initArena(config) - // get a hexdump of the first 100 bytes allocated - hexdump := r2cmd(r2p, "px 100") - fmt.Println(hexdump) + randomOffsets := getRandomOffsets(config) + + // place each bot in the arena + log.Println("---") + log.Println("[i] Place the bots") + for bot := 0; bot < len(config.Bots); bot++ { + // Place the bot in the arena + log.Printf("[i] Placing bot %d", bot) + address := randomOffsets[bot] + placeBot(r2p, config.Bots[bot], address) + + // define the instruction point and the stack pointer + log.Printf("[i] setting up the PC and SP for bot %d", bot) + _ = r2cmd(r2p, fmt.Sprintf("aer PC=%d", address)) + _ = r2cmd(r2p, fmt.Sprintf("aer SP=SP+%d", address)) + + // print the instruction point and the stack pointer + botStackPointer := r2cmd(r2p, "aerR~esp[2]") + log.Printf("[i] bot %d esp = %s", bot, botStackPointer) + botInstructionPointer := r2cmd(r2p, "aerR~eip[2]") + log.Printf("[i] bot %d eip = %s", bot, botInstructionPointer) + } + + // handle errors in esil + _ = r2cmd(r2p, "e cmd.esil.todo=f theend=1") + _ = r2cmd(r2p, "e cmd.esil.trap=f theend=1") + _ = r2cmd(r2p, "e cmd.esil.intr=f theend=1") + _ = r2cmd(r2p, "e cmd.esil.ioer=f theend=1") + _ = r2cmd(r2p, "f theend=0") + + fmt.Println(r2cmd(r2p, fmt.Sprintf("b %d", config.Memsize))) + + // start the competition + i := 0 + for true { + + // clear the screen + registers := stepIn(r2p) + + fmt.Println(user(r2p, i, registers, config)) + + i = switchPlayer(i, config) + + time.Sleep(2 * time.Second) + } - // compile a warrior using rasm2 - bot := r2cmd(r2p, "rasm2 -a x86 -b 32 -f bots/warrior.asm") - fmt.Println(bot) + r2p.Close() } -- cgit 1.4.1