Browse Source

Added words and virtual_network

Updated user and lib... main is currently setup to try and help me with,
making words, making a good average word difficulty, and an attempt at,
parsing the words to auto calculate easy|medium|hard levels.
david 6 months ago
parent
commit
a54a87cecf
11 changed files with 376 additions and 6 deletions
  1. 1 0
      .gitignore
  2. 116 0
      Cargo.lock
  3. 2 1
      Cargo.toml
  4. 1 1
      README.md
  5. 23 0
      rust_words.txt
  6. 7 0
      src/lib.rs
  7. 39 1
      src/main.rs
  8. 93 3
      src/user.rs
  9. 12 0
      src/virtual_network.rs
  10. 46 0
      src/words.rs
  11. 36 0
      try1_words.txt

+ 1 - 0
.gitignore

@@ -1 +1,2 @@
 /target
+/data

+ 116 - 0
Cargo.lock

@@ -32,27 +32,113 @@ dependencies = [
  "windows-targets",
 ]
 
+[[package]]
+name = "base64"
+version = "0.22.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
+
+[[package]]
+name = "bcrypt"
+version = "0.15.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e65938ed058ef47d92cf8b346cc76ef48984572ade631927e9937b5ffc7662c7"
+dependencies = [
+ "base64",
+ "blowfish",
+ "getrandom",
+ "subtle",
+ "zeroize",
+]
+
+[[package]]
+name = "blowfish"
+version = "0.9.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e412e2cd0f2b2d93e02543ceae7917b3c70331573df19ee046bcbc35e45e87d7"
+dependencies = [
+ "byteorder",
+ "cipher",
+]
+
+[[package]]
+name = "byteorder"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
+
 [[package]]
 name = "cfg-if"
 version = "1.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
 
+[[package]]
+name = "cipher"
+version = "0.4.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad"
+dependencies = [
+ "crypto-common",
+ "inout",
+]
+
+[[package]]
+name = "crypto-common"
+version = "0.1.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
+dependencies = [
+ "generic-array",
+ "typenum",
+]
+
 [[package]]
 name = "cyberknight"
 version = "0.1.0"
 dependencies = [
+ "bcrypt",
  "serde",
  "serde_json",
  "shared",
 ]
 
+[[package]]
+name = "generic-array"
+version = "0.14.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
+dependencies = [
+ "typenum",
+ "version_check",
+]
+
+[[package]]
+name = "getrandom"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "wasi",
+]
+
 [[package]]
 name = "gimli"
 version = "0.31.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "32085ea23f3234fc7846555e85283ba4de91e21016dc0455a16286d87a292d64"
 
+[[package]]
+name = "inout"
+version = "0.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5"
+dependencies = [
+ "generic-array",
+]
+
 [[package]]
 name = "itoa"
 version = "1.0.11"
@@ -165,6 +251,12 @@ dependencies = [
  "tokio",
 ]
 
+[[package]]
+name = "subtle"
+version = "2.6.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
+
 [[package]]
 name = "syn"
 version = "2.0.77"
@@ -198,12 +290,30 @@ dependencies = [
  "syn",
 ]
 
+[[package]]
+name = "typenum"
+version = "1.17.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
+
 [[package]]
 name = "unicode-ident"
 version = "1.0.13"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
 
+[[package]]
+name = "version_check"
+version = "0.9.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
+
+[[package]]
+name = "wasi"
+version = "0.11.0+wasi-snapshot-preview1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
+
 [[package]]
 name = "windows-targets"
 version = "0.52.6"
@@ -267,3 +377,9 @@ name = "windows_x86_64_msvc"
 version = "0.52.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
+
+[[package]]
+name = "zeroize"
+version = "1.8.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde"

+ 2 - 1
Cargo.toml

@@ -4,6 +4,7 @@ version = "0.1.0"
 edition = "2021"
 
 [dependencies]
+bcrypt = "0.15.1"
 serde = { version = "1.0.210", features = ["derive"] }
 serde_json = "1.0.128"
-shared = {git = "https://git.red-green.com/david/shared", version = "0.1.1", features = ["cast_into"]}
+shared = {git = "https://git.red-green.com/david/shared", version = "0.1.1"}

+ 1 - 1
README.md

@@ -1,3 +1,3 @@
 # cyberknight
 
-Type words to "hack" opponents and take over virtual machines
+Type words to "hack" opponents and take over virtual machines

+ 23 - 0
rust_words.txt

@@ -0,0 +1,23 @@
+if
+if let
+else if
+else
+match
+matches!
+format!
+print!
+println!
+panic!
+let
+let mut
+const
+impl 
+impl From<T>
+impl Into<T>
+impl Display
+impl Default
+impl TryFrom<T>
+impl TryInto<T>
+use
+mod
+pub

+ 7 - 0
src/lib.rs

@@ -1,2 +1,9 @@
 
+mod virtual_network;
+pub use virtual_network::Ip;
+
+mod words;
+pub use words::WordBank;
+
 mod user;
+pub use user::User;

+ 39 - 1
src/main.rs

@@ -1,4 +1,42 @@
+use cyberknight::{User, WordBank};
+
 
 fn main() {
-    println!("Hello, world!");
+    println!("Hello, world!");/*
+    if let Some(u) = User::load(127) {
+        println!("Name: {:?}", u.name);
+        println!("Password: ({}) {}", u.password_cost(), u.password);
+        if u.verify_password("password") {
+            println!("Password: Verified");
+        } else {
+            println!("Password: Invalid");
+        }
+        println!("Ip: {}", u.ip);
+        println!("Ports: ({}) {:?}", u.ports.len(), u.ports);
+    } else {
+        let mut u: User = User::new("Bob".to_string(), 127);
+        u.set_password("password");
+        u.ip.3 = 1;
+        println!("Name: {:?}", u.name);
+        println!("Password: ({}) {}", u.password_cost(), u.password);
+        println!("Ip: {}", u.ip);
+        println!("Ports: ({}) {:?}", u.ports.len(), u.ports);
+        u.save();
+    }*/
+    if let Some(wb) = WordBank::load() {
+        println!("{} Words", wb.words.len());
+        let mut sum: usize = 0;
+        let mut idx: usize = 0;
+        for word in wb.words.iter() {
+            let diff = wb.get_difficulty(idx);
+            if diff != word.len() {
+                println!("{:4}: {:30} diff={:3} len={}", idx, word, diff, word.len());
+            } else {
+                println!("{:4}: {:30} diff={}", idx, word, diff);
+            }
+            sum += diff;
+            idx += 1;
+        }
+        println!("Average Difficulty: {:.2}", sum as f32 / wb.words.len() as f32);
+    }
 }

+ 93 - 3
src/user.rs

@@ -1,10 +1,100 @@
+use std::{fs, path::PathBuf};
+
 use serde::{Deserialize, Serialize};
-use shared::Data;
-use std::collections::HashMap;
+use shared::Shared;
+
+use crate::virtual_network::Ip;
 
 #[derive(Debug, PartialEq, Clone, Default, Serialize, Deserialize)]
 pub struct User {
     pub name: String,
     pub password: String,
-    attrs: HashMap<String, Data>,
+    pub ip: Ip,
+    pub ports: Vec<u16>,
 }
+
+impl Into<Shared<User>> for User {
+    fn into(self) -> Shared<User> {
+        Shared::new(self)
+    }
+}
+
+impl User {
+    pub fn new(name: String, ip_block: u8) -> Self {
+        Self {
+            name,
+            password: "".to_string(),
+            ip: Ip(ip_block, 0, 0, 0),
+            ports: Vec::new()
+        }
+    }
+    pub fn set_password(&mut self, password: &str) -> bool {
+        if let Ok(hash) = bcrypt::hash(password, bcrypt::DEFAULT_COST) {
+            self.password = hash;
+            true
+        } else {
+            false
+        }
+    }
+    pub fn verify_password(&self, password: &str) -> bool {
+        if let Ok(ok) = bcrypt::verify(password, &self.password) {
+            ok
+        } else {
+            false
+        }
+    }
+    pub fn password_cost(&self) -> u32 {
+        let parts: Vec<&str> = self.password.split("$").collect();
+        if parts.len() < 2 {
+            return 0;
+        }
+        if let Ok(cost) = parts[2].parse::<u32>() {
+            cost
+        } else {
+            0
+        }
+    }
+    pub fn open_port(&mut self, port: u16) -> bool {
+        if self.ports.contains(&port) {
+            return false;
+        }
+        self.ports.push(port);
+        true
+    }
+    pub fn close_port(&mut self, port: u16) -> bool {
+        if !self.ports.contains(&port) {
+            return false;
+        }
+        let mut idx: usize = 0;
+        for p in self.ports.iter() {
+            if p == &port {
+                break;
+            }
+            idx += 1;
+        }
+        self.ports.remove(idx);
+        true
+    }
+    pub fn save(&self) -> bool {
+        let p = PathBuf::from("data/users".to_string());
+        let fp = PathBuf::from(format!("data/users/{}.json", self.ip.0));
+        _ = fs::create_dir_all(p);
+        if let Ok(content) = serde_json::to_string(self) {
+            let e = fs::write(fp, content);
+            e.is_ok()
+        } else {
+            false
+        }
+    }
+    pub fn load(ip_block: u8) -> Option<Self> {
+        let p = PathBuf::from("data/users".to_string());
+        let fp = PathBuf::from(format!("data/users/{}.json", ip_block));
+        _ = fs::create_dir_all(p);
+        if let Ok(content) = fs::read_to_string(fp) {
+            if let Ok(u) = serde_json::from_str::<Self>(&content) {
+                return Some(u);
+            }
+        }
+        None
+    }
+}

+ 12 - 0
src/virtual_network.rs

@@ -0,0 +1,12 @@
+use std::fmt::Display;
+
+use serde::{Deserialize, Serialize};
+
+#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Default)]
+pub struct Ip(pub u8, pub u8, pub u8, pub u8);
+
+impl Display for Ip {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        f.write_str(format!("{}.{}.{}.{}", self.0, self.1, self.2, self.3).as_str())
+    }
+}

+ 46 - 0
src/words.rs

@@ -0,0 +1,46 @@
+use std::{fs, path::PathBuf};
+
+
+#[derive(Debug, PartialEq, Clone)]
+pub struct WordBank {
+    pub words: Vec<String>
+}
+
+impl WordBank {
+    pub fn load() -> Option<Self> {
+        let fp = PathBuf::from("words.txt");
+        if let Ok(content) = fs::read_to_string(fp) {
+            let parts: Vec<&str> = content.split("\n").collect();
+            let mut wb: WordBank = WordBank { words: Vec::with_capacity(parts.len()) };
+            for line in parts {
+                if line.to_string().replace("\r", "").is_empty() {
+                    continue;
+                }
+                wb.words.push(line.to_string().replace("\r", ""));
+            }
+            Some(wb)
+        } else {
+            None
+        }
+    }
+    pub fn get_difficulty(&self, line: usize) -> usize {
+        if let Some(word) = self.words.get(line) {
+            let mut diff = word.len();
+            if word.contains(|c: char| {
+                // Make Uppercase, numbers, and spaces worth double
+                c.is_uppercase() | c.is_numeric() | c.is_whitespace()
+            }) {
+                diff += 1;
+            }
+            if word.contains(|c: char| {
+                // Make punctuation triple
+                c.is_ascii_punctuation()
+            }) {
+                diff += 2;
+            }
+            diff
+        } else {
+            0
+        }        
+    }
+}

+ 36 - 0
try1_words.txt

@@ -0,0 +1,36 @@
+scan
+info
+dir
+copy
+delete
+move
+upload
+download
+rescan
+rewrite
+override
+sudo
+vim
+emacs
+explorer
+import
+export
+hyperlink
+clone
+cipher
+encrypt
+decrypt
+encode
+decode
+python3
+rustc
+cargo
+pip
+rewriteCache
+writeCache
+readCache
+emptyCache
+scanCache
+mine
+remine
+remove