public string Init(string[] files, string[] usernames) { players.Clear(); Console.WriteLine("r2path = " + r2paths.r2); if (File.Exists(r2paths.r2) || !OperatingSystem.IsWindows()) { string cone = string.Format("-w malloc://{0}", memsize); // inicializamos las 2 instancias de r2, si ya estan inicializadas restauramos el estado inicial nPlayers = files.Count(); int addr = 0; int anotherAddr = 512; string [] arenas = { "", "" }; string[] newsrc = { "", "" }; string[] newrasm2params = { "", "" }; string[] newfile = { "", "" }; string[] newr2params = { "", "" }; r2archs.eArch[] newarch = { r2archs.eArch.unknow, r2archs.eArch.unknow }; Console.WriteLine("==== LOADING WARRIOR ===="); for (int x = 0; x < this.nPlayers; x++) { newfile[x] = files[x]; newarch [x] = r2archs.archfromfileext(newfile[x]); newr2params[x] = r2archs.r2param(newarch[x]); newrasm2params[x] = r2archs.rasm2param(newarch[x]); Console.WriteLine(" rasm params = {0} {1}", newrasm2params[x], newfile[x]); newsrc[x] = RunAndGetOutput(r2paths.rasm2, string.Format("{0} -f {1}", newrasm2params[x], newfile[x])).Replace("\r", "").Replace("\n", ""); Console.WriteLine(" Rasm Output = {0}", newsrc[x]); Console.WriteLine(" Rasm size = {0}", newsrc[x].Length / 2); Console.WriteLine("========================="); if (newsrc[x] == "") { return("Invalid source"); } if (newsrc[x].Length / 2 > 512) { return("Invalid source bot >512 bytes"); } } // calculamos un valor aleatorio para determinar la posicion del primer jugador 0=arriba, 1=abajo Random rnd = new Random(Environment.TickCount & Int32.MaxValue); int pos = rnd.Next(0, 2); Console.WriteLine("Initial position Player 0 {0}={1}", pos, pos == 0 ? "arriba" : "abajo"); for (int x = 0; x < this.nPlayers; x++) { if (this.r2[x] == null) { this.r2[x] = new R2Pipe(cone, r2paths.r2); } // seleccionamos la arch this.r2[x].RunCommand(newr2params[x]); this.r2[x].RunCommand("e scr.color=false"); this.r2[x].RunCommand("e asm.lines=false"); this.r2[x].RunCommand("e asm.flags=false"); this.r2[x].RunCommand("e asm.comments=false"); this.r2[x].RunCommand("e asm.bytes=false"); this.r2[x].RunCommand("e cfg.r2wars=true"); this.r2[x].RunCommand("pd"); // guardamos el estado inicial de esil para esta arch if (initstate[(int)newarch[x]] == "") { this.r2[x].RunCommand("aei"); this.r2[x].RunCommand("aeim"); initstate[(int)newarch[x]] = this.r2[x].RunCommand("aerR").Replace("\r", "").Replace("\n", ";"); } // si ya tenemos estado guardado restauramos los registros else { this.r2[x].RunCommand(initstate[(int)newarch[x]]); this.r2[x].RunCommand("aei"); this.r2[x].RunCommand("aeim"); } // reseteamos la memoria this.r2[x].RunCommand(string.Format("w0 {0} @ 0", memsize)); // calculamos donde poner el codigo en la arena if (pos == 0) { pos = 1; // jugador entre 0 y 512; while (true) { if (newsrc[x].Length == 1024) { anotherAddr = 0; addr = 0; break; } addr = rnd.Next(0, anotherAddr - newsrc[x].Length / 2); if (addr % 4 == 0) { anotherAddr = addr + newsrc[x].Length / 2; break; } } } else { pos = 0; //jugador jugador entre 512 y 1024 while (true) { if (newsrc[x].Length == 1024) { anotherAddr = 512; addr = 512; break; } addr = rnd.Next(anotherAddr, 1024 - newsrc[x].Length / 2); if (addr % 4 == 0) { anotherAddr = addr; break; } } } Console.WriteLine("Player {0} position {1}-{2} ({3})", x, addr, addr + newsrc[x].Length / 2, newsrc[x].Length / 2); // Añadimos el codigo del jugador string cmd = string.Format("wx {0} @ {1}", newsrc[x], addr); this.r2[x].RunCommand(cmd); arenas[x] = cmd; // Configuramos PC cmd = string.Format("aer PC={0}", addr); this.r2[x].RunCommand(cmd); // Configuramos STACK cmd = string.Format("aer SP=SP+{0}", addr); this.r2[x].RunCommand(cmd); // Obtenemos los registros para esta partida/jugador string initRegs = this.r2[x].RunCommand("aerR").Replace("\r", "").Replace("\n", ";"); // Iniciamos el jugador añadiendo sus datos de programa player p = new player(usernames[x], addr, newsrc[x].Length / 2, newsrc[x], initRegs); // lo añadimos a la lista de jugadores players.Add(p); int ciclos = GetPCInstructionCycles(x); players[x].actual.cycles = ciclos; players[x].actual.ins = GetPCInstruction(x); players[x].actual.dasm = GetFullProgram(x); players[x].actual.regs = GetRegs(x); players[x].actual.pc = "0x" + GetPC(x).ToString("X8"); players[x].actual.pcsize = GetPCSize(x); players[x].actual.oldpc = 4096; players[x].actual.oldpcsize = 0; players[x].actual.mem = GetMemAccessRAW(x); // borramos el log p.log.Clear(); this.r2[x].RunCommand("e cmd.esil.todo=f theend=1"); this.r2[x].RunCommand("e cmd.esil.trap=f theend=2"); this.r2[x].RunCommand("e cmd.esil.intr=f theend=3"); this.r2[x].RunCommand("e cmd.esil.ioer=f theend=4"); this.r2[x].RunCommand("f theend=0"); this.r2[x].RunCommand(string.Format("b {0}", memsize)); } Console.WriteLine(""); // sincronizamos las arenas this.r2[1].RunCommand(arenas[0]); this.r2[0].RunCommand(arenas[1]); if (players.Count < 2) { return("You need at least 2 users"); } return("OK"); } return("NOK"); }
/*List<int> get_random_offsets(int nRandoms) * { * System.Random rnd = new System.Random(); * List<int> rand = new List<int>(); * int addr = rnd.Next(0, memsize - maxprogsize); * if (addr % 4 != 0) * addr -= addr % 4; * rand.Add(addr); * int ini = addr; * int fin = addr + maxprogsize; * while (true) * { * addr = rnd.Next(0, memsize - maxprogsize); * if (addr % 4 != 0) * addr -= addr % 4; * if ((addr < ini && addr + maxprogsize < ini) || (addr > fin)) * { * rand.Add(addr); * break; * } * } * return rand; * }*/ public string Init(string[] files, string[] usernames) { players.Clear(); Console.WriteLine("r2path = " + r2paths.r2); if (File.Exists(r2paths.r2) || !OperatingSystem.IsWindows()) { //List<int> offsets = null;4 string file = ""; string strname = ""; string cone = string.Format("-w malloc://{0}", memsize); // inicializamos las 2 instancias de r2, si ya estan inicializadas restauramos el estado inicial nPlayers = files.Count(); //offsets = get_random_offsets(this.nPlayers); ///// Random rnd = new Random(Environment.TickCount & Int32.MaxValue); int finrango = 0; int addr = 0; int lastlen = 0; // TODO, get size of both bots, if one is too big Console.WriteLine("\nInit Battle"); Console.WriteLine("Players = {0}", this.nPlayers); for (int x = 0; x < this.nPlayers; x++) { file = files[x]; //int addr = offsets[x]; r2archs.eArch arch = r2archs.archfromfileext(file); string r2params = r2archs.r2param(arch); string rasm2params = r2archs.rasm2param(arch); if (this.r2[x] == null) { this.r2[x] = new R2Pipe(cone, r2paths.r2); } // seleccionamos la arch this.r2[x].RunCommand(r2params); //this.r2[x].RunCommand("e cfg.sandbox=true"); //this.r2[x].RunCommand("e asm.bytes=0"); this.r2[x].RunCommand("e scr.color=false"); this.r2[x].RunCommand("e asm.lines=false"); this.r2[x].RunCommand("e asm.flags=false"); this.r2[x].RunCommand("e asm.comments=false"); this.r2[x].RunCommand("e cfg.r2wars=true"); this.r2[x].RunCommand("pd"); // guardamos el estado inicial de esil para esta arch if (initstate[(int)arch] == "") { this.r2[x].RunCommand("aei"); this.r2[x].RunCommand("aeim"); initstate[(int)arch] = this.r2[x].RunCommand("aerR").Replace("\r", "").Replace("\n", ";"); } // si ya tenemos estado guardado restauramos los registros else { this.r2[x].RunCommand(initstate[(int)arch]); this.r2[x].RunCommand("aei"); this.r2[x].RunCommand("aeim"); } // reseteamos la memoria this.r2[x].RunCommand(string.Format("w0 {0} @ 0", memsize)); // compilamos el programa del usuario string src = RunAndGetOutput(r2paths.rasm2, string.Format("{0} -f {1}", rasm2params, file)).Replace("\r", "").Replace("\n", ""); if (src == "") { return("Invalid source"); } int lenSource = (src.Length / 2); Console.WriteLine("==== LOADING WARRIOR ===="); Console.WriteLine(" rasm params = {0} {1}", rasm2params, file); Console.WriteLine(" Rasm Output = {0}", src); Console.WriteLine(" Rasm size = {0}", lenSource); Console.WriteLine("========================="); // Trimm max length if (lenSource > 341) { Console.WriteLine("Bot exceeds 341 Bytes limit -> trim"); lenSource = 341; src = src.Substring(0, 341 * 2); } // calculamos donde poner el codigo en la arena // Wir berechnen, wo der Code in den Sand gelegt werden soll List <Range <int> > intRanges = new List <Range <int> >(); if (finrango == 0) { finrango = 1024 - lenSource; intRanges.Add(new Range <int>(0, finrango)); addr = rnd.NextFromRanges(intRanges); if (addr % 4 != 0) { addr -= addr % 4; } lastlen = lenSource; } else { int inicioProgramaAnterior = addr; int finProgramaAnterior = addr + lastlen; while (true) { finrango = 1024 - lenSource; intRanges.Add(new Range <int>(0, finrango)); addr = rnd.NextFromRanges(intRanges); if (addr % 4 != 0) { addr -= addr % 4; } if ((addr + lenSource < 1024)) { if (addr + lenSource < inicioProgramaAnterior || addr > finProgramaAnterior) { break; } } } } // Añadimos el codigo del jugador string cmd = string.Format("wx {0} @ {1}", src, addr); this.r2[x].RunCommand(cmd); // Configuramos PC cmd = string.Format("aer PC={0}", addr); this.r2[x].RunCommand(cmd); // Configuramos STACK cmd = string.Format("aer SP=SP+{0}", addr); this.r2[x].RunCommand(cmd); // Obtenemos los registros para esta partida/jugador string initRegs = this.r2[x].RunCommand("aerR").Replace("\r", "").Replace("\n", ";"); // generamos el jugador strname = usernames[x]; // Iniciamos el jugador añadiendo sus datos de programa player p = new player(strname, addr, src.Length / 2, src, initRegs); // lo añadimos a la lista de jugadores players.Add(p); int ciclos = GetPCInstructionCycles(x); players[x].actual.cycles = ciclos; players[x].actual.ins = GetPCInstruction(x); players[x].actual.dasm = GetFullProgram(x); players[x].actual.regs = GetRegs(x); players[x].actual.pc = "0x" + GetPC(x).ToString("X8"); players[x].actual.pcsize = GetPCSize(x); players[x].actual.oldpc = 4096; players[x].actual.oldpcsize = 0; players[x].actual.mem = GetMemAccessRAW(x); // borramos el log p.log.Clear(); this.r2[x].RunCommand("e cmd.esil.todo=f theend=1"); this.r2[x].RunCommand("e cmd.esil.trap=f theend=1"); this.r2[x].RunCommand("e cmd.esil.intr=f theend=1"); this.r2[x].RunCommand("e cmd.esil.ioer=f theend=1"); this.r2[x].RunCommand("f theend=0"); this.r2[x].RunCommand(string.Format("b {0}", memsize)); } if (players.Count < 2) { return("You need at least 2 users"); } return("OK"); } return("NOK"); }
public string Init(string[] files, string[] usernames) { players.Clear(); initstate[0] = ""; initstate[1] = ""; Console.WriteLine("r2path = " + r2paths.r2); if (File.Exists(r2paths.r2) || !OperatingSystem.IsWindows()) { List <int> offsets; string file = ""; string strname = ""; string cone = string.Format("-w malloc://{0}", memsize); // inicializamos las 2 instancias de r2, si ya estan inicializadas restauramos el estado inicial nPlayers = files.Count(); offsets = get_random_offsets(this.nPlayers); for (int x = 0; x < this.nPlayers; x++) { file = files[x]; int addr = offsets[x]; r2archs.eArch arch = r2archs.archfromfileext(file); string r2params = r2archs.r2param(arch); string rasm2params = r2archs.rasm2param(arch); if (this.r2[x] == null) { this.r2[x] = new R2Pipe(cone, r2paths.r2); } this.r2[x].RunCommand("pd"); // seleccionamos la arch this.r2[x].RunCommand(r2params); this.r2[x].RunCommand("e scr.color=false"); this.r2[x].RunCommand("e asm.lines=false"); this.r2[x].RunCommand("e asm.flags=false"); this.r2[x].RunCommand("e asm.comments=false"); this.r2[x].RunCommand("e cfg.r2wars=true"); this.r2[x].RunCommand("aei"); this.r2[x].RunCommand("aeim"); initstate[x] = this.r2[x].RunCommand("aerR").Replace("\r", "").Replace("\n", ";"); // reseteamos la memoria this.r2[x].RunCommand(string.Format("w0 {0} @ 0", memsize)); string src = RunAndGetOutput(r2paths.rasm2, string.Format("{0} -f {1}", rasm2params, file)).Replace("\r", "").Replace("\n", ""); if (src == "") { return("Invalid source"); } Console.WriteLine("Rasm Output = " + src); // Añadimos el codigo a radare2 string cmd = string.Format("wx {0} @ {1}", src, addr); this.r2[x].RunCommand(cmd); cmd = string.Format("aer PC={0}", addr); this.r2[x].RunCommand(cmd); cmd = string.Format("aer SP=SP+{0}", addr); this.r2[x].RunCommand(cmd); string initRegs = this.r2[x].RunCommand("aerR").Replace("\r", "").Replace("\n", ";"); // generamos el jugador strname = usernames[x]; // Iniciamos el jugador añadiendo sus datos de programa, ciclos player p = new player(strname, addr, src.Length / 2, src, initRegs); // lo añadimos a la lista de jugadores players.Add(p); int ciclos = GetPCInstructionCycles(x); players[x].actual.cycles = ciclos; players[x].actual.ins = GetPCInstruction(x); players[x].actual.dasm = GetFullProgram(x); players[x].actual.regs = GetRegs(x); players[x].actual.pc = "0x" + GetPC(x).ToString("X8"); players[x].actual.mem = GetMemAccessRAW(x); // borramos el log p.log.Clear(); this.r2[x].RunCommand("e cmd.esil.todo=f theend=1"); this.r2[x].RunCommand("e cmd.esil.trap=f theend=1"); this.r2[x].RunCommand("e cmd.esil.intr=f theend=1"); this.r2[x].RunCommand("e cmd.esil.ioer=f theend=1"); this.r2[x].RunCommand("f theend=0"); this.r2[x].RunCommand(string.Format("b {0}", memsize)); } string arenap0 = string.Format("wx {0}@{1}", players[0].code, players[0].orig); string arenap1 = string.Format("wx {0}@{1}", players[1].code, players[1].orig); this.r2[0].RunCommand(arenap1); this.r2[1].RunCommand(arenap0); //string t1 = r2[0].RunCommand("px 1024@0"); //string t2 = r2[1].RunCommand(string.Format("px 1024@0")); //if (t2 != t1) // return "error de memoria"; if (players.Count < 2) { return("You need at least 2 users"); } // sincronizamos ambas arenas con el codigo de cada warrior return("OK"); } return("NOK"); }