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

[bug] Prevent assets.copy() truncating file when fetch_asset fails #642

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions implants/lib/eldritch/src/assets/copy_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ fn copy_local(src: String, dst: String) -> Result<()> {
}

fn copy_remote(rx: Receiver<FetchAssetResponse>, dst_path: String) -> Result<()> {
// Wait for our first chunk
let resp = rx.recv()?;

// Truncate file
let mut dst = OpenOptions::new()
.create(true)
Expand All @@ -40,6 +43,10 @@ fn copy_remote(rx: Receiver<FetchAssetResponse>, dst_path: String) -> Result<()>
.open(&dst_path)
.context(format!("failed to open file for writing: {}", &dst_path))?;

// Write our first chunk
dst.write_all(&resp.chunk)
.context(format!("failed to write file chunk: {}", &dst_path))?;

// Listen for downloaded chunks and write them
for resp in rx {
dst.write_all(&resp.chunk)
Expand Down
5 changes: 1 addition & 4 deletions implants/lib/eldritch/src/crypto/hash_file_impl.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use std::fs::File;
use std::io::Read;

use sha1::{Digest, Sha1};
use sha2::{Sha256, Sha512};

use anyhow::{anyhow, Result};

pub fn hash_file(file: String, algo: String) -> Result<String> {
let mut file_data = std::fs::read(file)?;
let file_data = std::fs::read(file)?;
match algo.to_lowercase().as_str() {
"md5" => Ok(format!("{:02x}", md5::compute(file_data))),
"sha1" => {
Expand Down
Loading