0

How to run an executable file from a protected uncompressed archive? I read on various forums, but I did not find an answer in Rust. Found some code but libraries were removed

use std::process::{Command, Stdio};
use std::path::Path;
use std::io::{Read, Write};
use byteorder::{LittleEndian, ByteOrder};
use std::fs::File;
use rust_7z::SevenZipArchive;

fn main() -> std::io::Result<()> {
    let archive_path = "/path/to/archive.7z";
    let password = "password";

    let mut file = File::open(archive_path)?;
    let mut buffer = Vec::new();
    file.read_to_end(&mut buffer)?;

    let archive = SevenZipArchive::new(&buffer[..], password)?;

    for entry in archive.iter() {
        let file_name = entry.filename();

        if file_name.ends_with(".exe") {
            let file_info = entry.get_reader()?;
            let size = file_info.size()?;

            let mut stream_reader = file_info.into_read();

            let mut bytes = vec![0; size as usize];
            stream_reader.read_exact(&mut bytes)?;

            let mut command = Command::new(Path::new(file_name));
            command.stdin(Stdio::inherit());
            command.stdout(Stdio::inherit());
            command.stderr(Stdio::inherit());
            let _ = command.spawn();
        }
    }

    Ok(())
}

The archive contains 3 files, and you need to run one of the files without unpacking it.

  • Write the bytes of the file to a new file on the file system, then launch that. Your code has everything but saving the bytes to an new file. – Paul Dempsey Mar 28 '23 at 23:44
  • Of course, this is effectively unpacking it. Without OS wizardry you can't run a binary executable from bytes in memory. (Note this is not really a Rust-specific question). – Paul Dempsey Mar 28 '23 at 23:51
  • This is also typical malware behavior, so some OSs might be picky – MeetTitan Mar 29 '23 at 22:23

0 Answers0