Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

626 support globbing in rust #627

Merged
merged 9 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/_docs/user-guide/eldritch.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,21 @@ The <b>file.is_file</b> method checks if a path exists and is a file. If it does
`file.list(path: str) -> List<Dict>`

The <b>file.list</b> method returns a list of files at the specified path. The path is relative to your current working directory and can be traversed with `../`.
This function also supports globbing with `*` for example:

```python
file.list("/home/*/.bash_history") # List all files called .bash_history in sub dirs of `/home/`
file.list("/etc/*ssh*") # List the contents of all dirs that have `ssh` in the name and all files in etc with `ssh` in the name
```

Each file is represented by a Dict type.
Here is an example of the Dict layout:

```json
[
{
"file_name": "implants",
"absolute_path": "/workspace/realm/implants",
"size": 4096,
"owner": "root",
"group": "0",
Expand All @@ -311,6 +319,7 @@ Here is an example of the Dict layout:
},
{
"file_name": "README.md",
"absolute_path": "/workspace/realm/README.md",
"size": 750,
"owner": "root",
"group": "0",
Expand All @@ -320,6 +329,7 @@ Here is an example of the Dict layout:
},
{
"file_name": ".git",
"absolute_path": "/workspace/realm/.git",
"size": 4096,
"owner": "root",
"group": "0",
Expand Down Expand Up @@ -347,6 +357,12 @@ The <b>file.moveto</b> method moves a file or directory from `src` to `dst`. If
`file.read(path: str) -> str`

The <b>file.read</b> method will read the contents of a file. If the file or directory doesn't exist the method will error to avoid this ensure the file exists, and you have permission to read it.
This function supports globbing with `*` for example:

```python
file.read("/home/*/.bash_history") # Read all files called .bash_history in sub dirs of `/home/`
file.read("/etc/*ssh*") # Read the contents of all files that have `ssh` in the name. Will error if a dir is found.
```

### file.remove

Expand Down
1 change: 1 addition & 0 deletions implants/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ derive_more = "0.99.17"
eval = "0.4.3"
flate2 = "1.0.24"
gazebo = "0.8.1"
glob = "0.3.1"
graphql_client = "0.12.0"
hex = "0.4.2"
hex-literal = "0.4.1"
Expand Down
1 change: 1 addition & 0 deletions implants/lib/eldritch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ derive_more = { workspace = true }
eval = { workspace = true }
flate2 = { workspace = true }
gazebo = { workspace = true }
glob = { workspace = true }
hex = { workspace = true }
hex-literal = { workspace = true }
ipnetwork = { workspace = true }
Expand Down
161 changes: 110 additions & 51 deletions implants/lib/eldritch/src/file/list_impl.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use super::super::insert_dict_kv;
use super::{File, FileType};
use anyhow::Result;
use anyhow::{Context, Result};
use chrono::{DateTime, NaiveDateTime, Utc};
use glob::glob;
use starlark::{
collections::SmallMap,
const_frozen_string,
values::{dict::Dict, Heap, Value},
};
use std::fs::DirEntry;
use std::fs::{self};
#[cfg(target_os = "freebsd")]
use std::os::freebsd::fs::MetadataExt;
#[cfg(target_os = "linux")]
use std::os::linux::fs::MetadataExt;
#[cfg(target_os = "macos")]
Expand All @@ -16,10 +19,9 @@ use std::os::macos::fs::MetadataExt;
use std::os::unix::fs::PermissionsExt;
#[cfg(target_os = "windows")]
use std::os::windows::fs::MetadataExt;
#[cfg(target_os = "freebsd")]
use std::os::freebsd::fs::MetadataExt;
use sysinfo::{System, SystemExt, UserExt};
use std::path::{Path, PathBuf};

use sysinfo::{System, SystemExt, UserExt};
const UNKNOWN: &str = "UNKNOWN";

// https://stackoverflow.com/questions/6161776/convert-windows-filetime-to-second-in-unix-linux
Expand All @@ -30,40 +32,36 @@ fn windows_tick_to_unix_tick(windows_tick: u64) -> i64 {
return (windows_tick / WINDOWS_TICK - SEC_TO_UNIX_EPOCH) as i64;
}

fn create_file_from_dir_entry(dir_entry: DirEntry) -> Result<File> {
let mut sys = System::new();
fn create_file_from_pathbuf(path_entry: PathBuf) -> Result<File> {
let mut sys: System = System::new();
sys.refresh_users_list();
let file_name = path_entry
.file_name()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤮

.context("file.list: Failed to get filename")?
.to_str()
.context("file.list: Unable to convert file name to string")?
.to_string();

let file_name = match dir_entry.file_name().into_string() {
Ok(local_file_name) => local_file_name,
Err(_) => {
return Err(anyhow::anyhow!(
"file.list: Unable to convert file name to string."
))
}
};
let absolute_path = fs::canonicalize(&path_entry)?
.to_str()
.context("file.list: Failed to canonicalize path")?
.to_string();

let file_type = match dir_entry.file_type() {
Ok(tmp_file_type) => {
if tmp_file_type.is_dir() {
FileType::Directory
} else if tmp_file_type.is_file() {
FileType::File
} else if tmp_file_type.is_symlink() {
FileType::Link
} else {
FileType::Unknown
}
}
Err(_) => FileType::Unknown,
let file_type = if path_entry.is_dir() {
hulto marked this conversation as resolved.
Show resolved Hide resolved
FileType::Directory
} else if path_entry.is_file() {
FileType::File
} else if path_entry.is_symlink() {
FileType::Link
} else {
FileType::Unknown
};

let dir_entry_metadata = dir_entry.metadata()?;

let file_size = dir_entry_metadata.len();
let file_metadata = path_entry.metadata()?;
let file_size = file_metadata.len();

#[cfg(unix)]
let owner_username = match sysinfo::Uid::try_from(dir_entry_metadata.st_uid() as usize) {
let owner_username = match sysinfo::Uid::try_from(file_metadata.st_uid() as usize) {
Ok(local_uid) => match sys.get_user_by_id(&local_uid) {
Some(user_name_string) => user_name_string.name().to_string(),
None => UNKNOWN.to_string(),
Expand All @@ -75,22 +73,22 @@ fn create_file_from_dir_entry(dir_entry: DirEntry) -> Result<File> {
let owner_username = { UNKNOWN.to_string() };

#[cfg(unix)]
let group_id = { dir_entry_metadata.st_gid().to_string() };
let group_id = { file_metadata.st_gid().to_string() };
#[cfg(not(unix))]
let group_id = {
UNKNOWN // This is bad but windows file ownership is very different.
};

#[cfg(unix)]
let timestamp = { dir_entry_metadata.st_mtime() };
let timestamp = { file_metadata.st_mtime() };
#[cfg(not(unix))]
let timestamp = {
let win_timestamp = dir_entry_metadata.last_write_time();
let win_timestamp = file_metadata.last_write_time();
windows_tick_to_unix_tick(win_timestamp)
};

#[cfg(unix)]
let permissions = { format!("{:o}", dir_entry_metadata.permissions().mode()) };
let permissions = { format!("{:o}", file_metadata.permissions().mode()) };
#[cfg(target_os = "windows")]
let permissions = {
if dir_entry.metadata()?.permissions().readonly() {
Expand All @@ -113,6 +111,7 @@ fn create_file_from_dir_entry(dir_entry: DirEntry) -> Result<File> {

Ok(File {
name: file_name,
absolute_path,
file_type,
size: file_size,
owner: owner_username,
Expand All @@ -123,11 +122,20 @@ fn create_file_from_dir_entry(dir_entry: DirEntry) -> Result<File> {
}

fn handle_list(path: String) -> Result<Vec<File>> {
let paths = std::fs::read_dir(path)?;
let mut final_res = Vec::new();
for path in paths {
final_res.push(create_file_from_dir_entry(path?)?);
let input_path = Path::new(&path);

if input_path.is_dir() {
let paths = std::fs::read_dir(path)?;
for dirent in paths {
let pathbuf = dirent?.path();
final_res.push(create_file_from_pathbuf(pathbuf)?);
}
} else {
let res = create_file_from_pathbuf(PathBuf::from(path))?;
final_res.push(res);
}

Ok(final_res)
}

Expand All @@ -136,6 +144,13 @@ fn create_dict_from_file(starlark_heap: &Heap, file: File) -> Result<Dict> {
let mut dict_res = Dict::new(res);

insert_dict_kv!(dict_res, starlark_heap, "file_name", &file.name, String);
insert_dict_kv!(
dict_res,
starlark_heap,
"absolute_path",
&file.absolute_path,
String
);
insert_dict_kv!(dict_res, starlark_heap, "size", file.size, u64);
insert_dict_kv!(dict_res, starlark_heap, "owner", &file.owner, String);
insert_dict_kv!(dict_res, starlark_heap, "group", &file.group, String);
Expand Down Expand Up @@ -166,18 +181,30 @@ fn create_dict_from_file(starlark_heap: &Heap, file: File) -> Result<Dict> {

pub fn list(starlark_heap: &Heap, path: String) -> Result<Vec<Dict>> {
let mut final_res: Vec<Dict> = Vec::new();
let file_list = match handle_list(path) {
Ok(local_file_list) => local_file_list,
Err(local_err) => {
return Err(anyhow::anyhow!(
"Failed to get file list: {}",
local_err.to_string()
))
for entry in glob(&path)? {
match entry {
Ok(entry_path) => {
let file_list = match handle_list(
entry_path
.to_str()
.context("Failed to convert string")?
.to_string(),
) {
Ok(local_file_list) => local_file_list,
Err(local_err) => {
return Err(anyhow::anyhow!(
"Failed to get file list: {}",
local_err.to_string()
))
}
};
for file in file_list {
let tmp_res = create_dict_from_file(starlark_heap, file)?;
final_res.push(tmp_res);
}
}
Err(e) => println!("{:?}", e),
}
};
for file in file_list {
let tmp_res = create_dict_from_file(starlark_heap, file)?;
final_res.push(tmp_res);
}
Ok(final_res)
}
Expand Down Expand Up @@ -205,9 +232,41 @@ mod tests {

let binding = Heap::new();
let list_res = list(&binding, test_dir.path().to_str().unwrap().to_string())?;
println!("{:?}", list_res);
assert_eq!(list_res.len(), (expected_dirs.len() + expected_files.len()));

Ok(())
}
#[test]
fn test_file_list_glob() -> anyhow::Result<()> {
let test_dir = tempdir()?;
let expected_dir = "down the";
let nested_dir = "rabbit hole";
let file = "win";

let test_dir_to_create = test_dir.path().join(expected_dir);
std::fs::create_dir(test_dir_to_create)?;
let test_nested_dir_to_create = test_dir.path().join(expected_dir).join(nested_dir);
std::fs::create_dir(test_nested_dir_to_create)?;
let test_file = test_dir
.path()
.join(expected_dir)
.join(nested_dir)
.join(file);
std::fs::File::create(test_file)?;

// /tmpdir/down the/*
let binding = Heap::new();
let list_res = list(
&binding,
test_dir
.path()
.join("*")
.join("win")
.to_str()
.unwrap()
.to_string(),
)?;
println!("{:?}", list_res);
Ok(())
}
}
4 changes: 3 additions & 1 deletion implants/lib/eldritch/src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ enum FileType {

#[derive(Debug, Display)]
#[display(
fmt = "{} {} {} {} {} {} {}",
fmt = "{} {} {} {} {} {} {} {}",
name,
absolute_path,
file_type,
size,
owner,
Expand All @@ -48,6 +49,7 @@ enum FileType {
)]
struct File {
name: String,
absolute_path: String,
file_type: FileType,
size: u64,
owner: String,
Expand Down
Loading
Loading