about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--Cargo.lock310
-rw-r--r--Cargo.toml10
-rw-r--r--bots/warrior0.asm9
-rw-r--r--bots/warrior1.asm9
-rw-r--r--flake.nix44
-rw-r--r--src/main.rs179
7 files changed, 562 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ea8c4bf
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/target
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..5224a82
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,310 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "ansi_term"
+version = "0.12.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
+dependencies = [
+ "winapi",
+]
+
+[[package]]
+name = "atty"
+version = "0.2.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
+dependencies = [
+ "hermit-abi",
+ "libc",
+ "winapi",
+]
+
+[[package]]
+name = "bitflags"
+version = "1.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
+
+[[package]]
+name = "clap"
+version = "2.34.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
+dependencies = [
+ "ansi_term",
+ "atty",
+ "bitflags",
+ "strsim",
+ "textwrap",
+ "unicode-width",
+ "vec_map",
+]
+
+[[package]]
+name = "heck"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
+dependencies = [
+ "unicode-segmentation",
+]
+
+[[package]]
+name = "hermit-abi"
+version = "0.1.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "itoa"
+version = "1.0.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
+
+[[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
+[[package]]
+name = "libc"
+version = "0.2.147"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
+
+[[package]]
+name = "proc-macro-error"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
+dependencies = [
+ "proc-macro-error-attr",
+ "proc-macro2",
+ "quote",
+ "syn 1.0.109",
+ "version_check",
+]
+
+[[package]]
+name = "proc-macro-error-attr"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "version_check",
+]
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.65"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "92de25114670a878b1261c79c9f8f729fb97e95bac93f6312f583c60dd6a1dfe"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5907a1b7c277254a8b15170f6e7c97cfa60ee7872a3217663bb81151e48184bb"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "r2pipe"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "63443078dfdb83f59820ed161863e38df38e8c6a2c3b85733ebac1bb65b4af2b"
+dependencies = [
+ "libc",
+ "serde",
+ "serde_derive",
+ "serde_json",
+ "thiserror",
+]
+
+[[package]]
+name = "r2pipe-rs"
+version = "0.1.0"
+dependencies = [
+ "r2pipe",
+ "structopt",
+]
+
+[[package]]
+name = "ryu"
+version = "1.0.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
+
+[[package]]
+name = "serde"
+version = "1.0.171"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "30e27d1e4fd7659406c492fd6cfaf2066ba8773de45ca75e855590f856dc34a9"
+
+[[package]]
+name = "serde_derive"
+version = "1.0.171"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "389894603bd18c46fa56231694f8d827779c0951a667087194cf9de94ed24682"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.26",
+]
+
+[[package]]
+name = "serde_json"
+version = "1.0.103"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d03b412469450d4404fe8499a268edd7f8b79fecb074b0d812ad64ca21f4031b"
+dependencies = [
+ "itoa",
+ "ryu",
+ "serde",
+]
+
+[[package]]
+name = "strsim"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
+
+[[package]]
+name = "structopt"
+version = "0.3.26"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10"
+dependencies = [
+ "clap",
+ "lazy_static",
+ "structopt-derive",
+]
+
+[[package]]
+name = "structopt-derive"
+version = "0.4.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0"
+dependencies = [
+ "heck",
+ "proc-macro-error",
+ "proc-macro2",
+ "quote",
+ "syn 1.0.109",
+]
+
+[[package]]
+name = "syn"
+version = "1.0.109"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "syn"
+version = "2.0.26"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "45c3457aacde3c65315de5031ec191ce46604304d2446e803d71ade03308d970"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "textwrap"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
+dependencies = [
+ "unicode-width",
+]
+
+[[package]]
+name = "thiserror"
+version = "1.0.43"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a35fc5b8971143ca348fa6df4f024d4d55264f3468c71ad1c2f365b0a4d58c42"
+dependencies = [
+ "thiserror-impl",
+]
+
+[[package]]
+name = "thiserror-impl"
+version = "1.0.43"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "463fe12d7993d3b327787537ce8dd4dfa058de32fc2b195ef3cde03dc4771e8f"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.26",
+]
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c"
+
+[[package]]
+name = "unicode-segmentation"
+version = "1.10.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36"
+
+[[package]]
+name = "unicode-width"
+version = "0.1.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
+
+[[package]]
+name = "vec_map"
+version = "0.8.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
+
+[[package]]
+name = "version_check"
+version = "0.9.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
+
+[[package]]
+name = "winapi"
+version = "0.3.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
+dependencies = [
+ "winapi-i686-pc-windows-gnu",
+ "winapi-x86_64-pc-windows-gnu",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..4524ba0
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,10 @@
+[package]
+name = "r2pipe-rs"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+r2pipe = "0.7.0"
+structopt = "0.3.26"
diff --git a/bots/warrior0.asm b/bots/warrior0.asm
new file mode 100644
index 0000000..29e2271
--- /dev/null
+++ b/bots/warrior0.asm
@@ -0,0 +1,9 @@
+call label
+label:
+	pop eax
+loop:
+	sub eax, 10
+	cmp [eax], 0
+je loop
+	mov [eax], 0
+	jmp loop
diff --git a/bots/warrior1.asm b/bots/warrior1.asm
new file mode 100644
index 0000000..6627aae
--- /dev/null
+++ b/bots/warrior1.asm
@@ -0,0 +1,9 @@
+call label
+label:
+	pop eax
+loop:
+	sub eax, 20
+	cmp [eax], 0
+je loop
+	mov [eax], 0
+	jmp loop
diff --git a/flake.nix b/flake.nix
new file mode 100644
index 0000000..bc3308a
--- /dev/null
+++ b/flake.nix
@@ -0,0 +1,44 @@
+{
+  inputs = {
+    flake-utils.url = "github:numtide/flake-utils";
+    naersk.url = "github:nix-community/naersk";
+    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
+  };
+
+  outputs = { self, flake-utils, naersk, nixpkgs }:
+    let
+      pkgs = (import nixpkgs) {
+        system = "x86_64-linux";
+      };
+
+      naersk' = pkgs.callPackage naersk {};
+      
+    in rec {
+      packages."x86_64-linux".r2wars-rs = naersk'.buildPackage {
+        src = ./.;
+
+        meta = with pkgs.lib; {
+          description = "yet another r2wars implementation";
+          homepage    = "https://git.emile.space/hanemile/r2wars-rs";
+          license     = licenses.mit;
+          platforms   = platforms.all;
+          maintainers = with maintainers; [
+            hanemile
+          ];
+        };
+      };
+    
+      # For `nix build` & `nix run`:
+      defaultPackage = packages."x86_64-linux".r2wars-rs;
+
+      # For `nix develop` (optional, can be skipped):
+      devShell = pkgs.mkShell {
+        nativeBuildInputs = with pkgs; [ rustc cargo ];
+      };
+
+      # hydraJobs."<attr>"."<system>" = derivation;
+      hydraJobs = {
+        build."x86_64-linux" = packages."x86_64-linux".r2wars-r2;
+      };
+    };
+}
\ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..34f73e1
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,179 @@
+use r2pipe::R2Pipe;
+use structopt::StructOpt;
+use std::path::PathBuf;
+use std::time::Duration;
+
+// we're using a custom int type here, as we want to set defaults using
+// structopt which leads to a problem, as structopt only allows strings to be
+// set as default values ()
+#[derive(Debug)]
+struct Int(u64);
+
+impl std::str::FromStr for Int {
+    type Err = Box<dyn std::error::Error>;
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        Ok(Int(s.parse::<u64>().unwrap()))
+    }
+}
+impl std::fmt::Display for Int {
+    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+        write!(f, "{}", self.0)
+    }
+}
+
+// we're using a custom "Delay" type here, as we want to define the duration
+// #of a game round, but structopt (as with the int stuff above) can't use time
+// durations (it's time, not easy!). [derive(Debug)]
+#[derive(Debug)]
+struct Delay(Duration);
+
+impl std::str::FromStr for Delay {
+    type Err = Box<dyn std::error::Error>;
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        Ok(Delay(Duration::from_secs(s.parse::<u64>().unwrap())))
+    }
+}
+
+#[derive(Debug)]
+struct Bot {
+	// Path defines the path to the source of the bot
+    path: PathBuf,
+
+	// Source defines the source of the bot after being compiled with rasm2
+    source: Option<String>,
+
+	// Addr defines the initial address the bot is placed at
+    addr: Option<u64>,
+
+	// Regs defines the state of the registers of the bot
+	// It is used to store the registers after each round and restore them in the
+	// next round when the bot's turn has come
+    regs: Option<String>,
+}
+
+#[derive(Debug, PartialEq, Clone)]
+struct BotPaths(Vec<String>);
+
+impl std::str::FromStr for BotPaths {
+    type Err = Box<dyn std::error::Error>;
+
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        Ok(BotPaths(s.split(",").map(|x| x.trim().to_owned()).collect()))
+    }
+}
+
+// these are the options you can use, list them nicely using
+// `cargo run -- --help`
+#[derive(Debug, StructOpt)]
+#[structopt(about = "Yet another r2wars implementation, made with <3 @ AFRA Berlin")]
+struct Opt {
+
+    #[structopt(short = "a", long = "arch", default_value = "x86")]
+    architecture: String,
+
+    #[structopt(short = "b", long = "bits", default_value = "64")]
+    bits: Int,
+
+    #[structopt(long = "memsize", default_value = "4096")]
+    memory_size: Int,
+
+    #[structopt(long = "maxbotsize", default_value = "256")]
+    max_bot_size: Int,
+
+    #[structopt(long = "bots", default_value = "bots/warrior0.asm, bots/warrior1.asm")]
+    bots: BotPaths,
+
+    #[structopt(long = "delay", default_value = "1")]
+    game_round_duration_seconds: Delay,
+}
+
+fn init_arena(r2p: &mut R2Pipe, opt: &Opt) {
+    println!("[ ] Initializing the arena");
+    println!("[ ] Allocating {} bytes of memory...", opt.memory_size);
+
+    let _ = r2p.cmd(format!("malloc://{}", opt.memory_size).as_str())
+                .expect("could not allocate memory");
+    
+    println!("[ ] General configuration...");
+    let _ = r2p.cmd(format!("e asm.arch = {}", opt.architecture).as_str())
+                .expect("could not set the desired architecture");
+    let _ = r2p.cmd(format!("e asm.bits = {}", opt.bits).as_str())
+                .expect("could not set the desired bits");
+    println!("[ ]     ...Done");
+
+    println!("[ ] Theming...");
+    let _ = r2p.cmd("e scr.color = 3").expect("set color err");
+    let _ = r2p.cmd("e scr.color.args = true").expect("set color err");
+    let _ = r2p.cmd("e scr.color.bytes = true").expect("set color err");
+    let _ = r2p.cmd("e scr.color.grep = true").expect("set color err");
+    let _ = r2p.cmd("e scr.color.ops = true").expect("set color err");
+    let _ = r2p.cmd("e scr.color.pipe = true").expect("set color err");
+    let _ = r2p.cmd("e scr.bgfill = true").expect("set color err");
+    let _ = r2p.cmd("e scr.utf8 = true").expect("set color err");
+    let _ = r2p.cmd("e hex.cols = 32").expect("set hex col width err");
+    println!("[ ]     ...Done");
+
+    println!("[ ] ESIL...");
+    let _ = r2p.cmd("aei").expect("init esil vm failed"); // init ESIL VM
+    let _ = r2p.cmd("aeim").expect("init esil vm stack failed"); // init ESIL VM stack
+    println!("[ ]     ...Done");
+}
+
+fn build_bots(opt: &Opt, bots: &mut Vec<Bot>) {
+    for bot_path in opt.bots.0.clone().into_iter() {
+        bots.push(
+            build_bot(PathBuf::from(bot_path))
+                .expect("could not build a bot!")
+        )
+    }
+}
+
+fn build_bot(bot_path: PathBuf) -> Result<Bot, ()> {
+
+    let mut r2p = r2pipe::open_pipe!(Some("--").to_owned()).expect("could not open r2pipe");
+
+    let source = r2p.cmd(format!("rasm2 -a {} -b {} -f {:?}", "x86", "32", bot_path).as_str())
+        .expect("failed to build the bot!");
+    
+    let new_bot: Bot = Bot {
+        path: bot_path,
+        source: Some(source),
+        addr: None, 
+        regs: None, 
+    };
+    
+    Ok(new_bot)
+}
+
+fn main() -> Result<(), Box<dyn std::error::Error>> {
+    let mut bots: Vec<Bot> = vec![];
+    
+    let opt = Opt::from_args();
+
+    println!("{:?}", opt.architecture);
+    println!("{:?}", opt.bits);
+    println!("{:?}", opt.memory_size);
+    println!("{:?}", opt.max_bot_size);
+    println!("{:?}", opt.bots);
+    println!("{:?}", opt.game_round_duration_seconds);
+
+    let path = Some("/Users/emilehansmaennel/Documents/CTF/2023/indore/Lengan/main_fixed".to_owned());
+    let mut r2p = r2pipe::open_pipe!(path).expect("could not open r2pipe");
+
+    init_arena(&mut r2p, &opt);
+
+    build_bots(&opt, &mut bots);
+
+    for bot in bots {
+        println!("{:?}", bot);
+    }
+    
+    // println!("{}", r2p.cmd("?e Hello World").expect("could not exec command"));
+    // println!("{}", r2p.cmd("aaa").expect("could not exec command"));
+    // println!("---");
+    // println!("{}", r2p.cmd("afl").expect("could not exec command"));
+    // println!("---");
+    r2p.close();
+
+    Ok(())
+}