advanced shell

This commit is contained in:
nora 2024-08-26 03:27:16 +02:00
parent c7965e404a
commit 5f203d0f5b
4 changed files with 67 additions and 22 deletions

1
Cargo.lock generated
View file

@ -355,6 +355,7 @@ dependencies = [
"eyre", "eyre",
"hex-literal", "hex-literal",
"rand", "rand",
"shlex",
"tokio", "tokio",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",

View file

@ -14,3 +14,4 @@ tokio = { version = "1.39.2", features = ["full"] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "json"] } tracing-subscriber = { version = "0.3.18", features = ["env-filter", "json"] }
tracing.workspace = true tracing.workspace = true
shlex = "1.3.0"

View file

@ -261,30 +261,69 @@ fn execute_command(command: &[u8]) -> ProcessOutput {
stdout: b"what the hell".to_vec(), stdout: b"what the hell".to_vec(),
}; };
}; };
// Some hardcoded commands
match command.trim() { match command.trim() {
"uname -s -v -n -r -m" => ProcessOutput { "uname -s -v -n -r -m" => {
status: 0, return ProcessOutput {
stdout: UNAME_SVNRM.to_vec(), status: 0,
}, stdout: UNAME_SVNRM.to_vec(),
"uname -a" => ProcessOutput { }
status: 0, }
stdout: UNAME_A.to_vec(), "uname -a" => {
}, return ProcessOutput {
"cat /proc/cpuinfo|grep name|cut -f2 -d':'|uniq -c ; uname -a" => ProcessOutput { status: 0,
status: 0, stdout: UNAME_A.to_vec(),
stdout: CPUINFO_UNAME_A.to_vec(), }
}, }
"cat /proc/cpuinfo|grep name|cut -f2 -d':'|uniq -c ; uname -a" => {
return ProcessOutput {
status: 0,
stdout: CPUINFO_UNAME_A.to_vec(),
}
}
_ => {}
}
// Now, lex the string and do this nicely
let Some(parts) = shlex::split(command) else {
return ProcessOutput {
status: 1,
stdout: b"bash: invalid input\r\n".to_vec(),
};
};
let Some(argv0) = parts.first() else {
return ProcessOutput {
status: 1,
stdout: b"bash: invalid input\r\n".to_vec(),
};
};
match argv0.as_str().trim() {
"true" => ProcessOutput { "true" => ProcessOutput {
status: 0, status: 0,
stdout: b"".to_vec(), stdout: b"".to_vec(),
}, },
_ => { "cd" => ProcessOutput {
let argv0 = command.split_ascii_whitespace().next().unwrap_or(""); status: 0,
stdout: b"".to_vec(),
ProcessOutput { },
status: 127, "ls" => ProcessOutput {
stdout: format!("bash: line 1: {argv0}: command not found\r\n").into_bytes(), status: 0,
} stdout: b"hpasswd index.php secrets.php\r\n".to_vec(),
} },
"whoami" => ProcessOutput {
status: 0,
stdout: b"root\r\n".to_vec(),
},
"id" => ProcessOutput {
status: 0,
stdout: b"uid=0(root) gid=0(root) groups=0(root)\r\n".to_vec(),
},
_ => ProcessOutput {
status: 127,
stdout: format!("bash: line 1: {argv0}: command not found\r\n").into_bytes(),
},
} }
} }

View file

@ -25,10 +25,14 @@ impl InteractiveShell {
return; return;
} }
b'\r' => { b'\r' => {
let output = super::execute_command(&self.line_buf); let output = if !self.line_buf.is_empty() {
super::execute_command(&self.line_buf).stdout
} else {
Vec::new()
};
self.line_buf.clear(); self.line_buf.clear();
self.write(b"\r\n"); self.write(b"\r\n");
self.write(&output.stdout); self.write(&output);
self.prompt(); self.prompt();
} }
// ESC // ESC