mirror of
https://github.com/rustdesk/rustdesk.git
synced 2026-03-18 02:31:00 +03:00
fix: windows, dll, pre-loading attack (#10608)
Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
@@ -31,7 +31,10 @@ macro_rules! my_println{
|
||||
pub fn core_main() -> Option<Vec<String>> {
|
||||
crate::load_custom_client();
|
||||
#[cfg(windows)]
|
||||
crate::platform::windows::bootstrap();
|
||||
if !crate::platform::windows::bootstrap() {
|
||||
// return None to terminate the process
|
||||
return None;
|
||||
}
|
||||
let mut args = Vec::new();
|
||||
let mut flutter_args = Vec::new();
|
||||
let mut i = 0;
|
||||
|
||||
@@ -19,6 +19,7 @@ use serde_json::json;
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
ffi::CString,
|
||||
io::{Error as IoError, ErrorKind as IoErrorKind},
|
||||
os::raw::{c_char, c_int, c_void},
|
||||
str::FromStr,
|
||||
sync::{
|
||||
@@ -50,7 +51,7 @@ lazy_static::lazy_static! {
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
lazy_static::lazy_static! {
|
||||
pub static ref TEXTURE_RGBA_RENDERER_PLUGIN: Result<Library, LibError> = Library::open("texture_rgba_renderer_plugin.dll");
|
||||
pub static ref TEXTURE_RGBA_RENDERER_PLUGIN: Result<Library, LibError> = load_plugin_in_app_path("texture_rgba_renderer_plugin.dll");
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
@@ -65,7 +66,37 @@ lazy_static::lazy_static! {
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
lazy_static::lazy_static! {
|
||||
pub static ref TEXTURE_GPU_RENDERER_PLUGIN: Result<Library, LibError> = Library::open("flutter_gpu_texture_renderer_plugin.dll");
|
||||
pub static ref TEXTURE_GPU_RENDERER_PLUGIN: Result<Library, LibError> = load_plugin_in_app_path("flutter_gpu_texture_renderer_plugin.dll");
|
||||
}
|
||||
|
||||
// Move this function into `src/platform/windows.rs` if there're more calls to load plugins.
|
||||
// Load dll with full path.
|
||||
#[cfg(target_os = "windows")]
|
||||
fn load_plugin_in_app_path(dll_name: &str) -> Result<Library, LibError> {
|
||||
match std::env::current_exe() {
|
||||
Ok(exe_file) => {
|
||||
if let Some(cur_dir) = exe_file.parent() {
|
||||
let full_path = cur_dir.join(dll_name);
|
||||
if !full_path.exists() {
|
||||
Err(LibError::OpeningLibraryError(IoError::new(
|
||||
IoErrorKind::NotFound,
|
||||
format!("{} not found", dll_name),
|
||||
)))
|
||||
} else {
|
||||
Library::open(full_path)
|
||||
}
|
||||
} else {
|
||||
Err(LibError::OpeningLibraryError(IoError::new(
|
||||
IoErrorKind::Other,
|
||||
format!(
|
||||
"Invalid exe parent for {}",
|
||||
exe_file.to_string_lossy().as_ref()
|
||||
),
|
||||
)))
|
||||
}
|
||||
}
|
||||
Err(e) => Err(LibError::OpeningLibraryError(e)),
|
||||
}
|
||||
}
|
||||
|
||||
/// FFI for rustdesk core's main entry.
|
||||
@@ -2076,11 +2107,7 @@ pub mod sessions {
|
||||
}
|
||||
|
||||
pub(super) mod async_tasks {
|
||||
use hbb_common::{
|
||||
bail,
|
||||
tokio::{self, select},
|
||||
ResultType,
|
||||
};
|
||||
use hbb_common::{bail, tokio, ResultType};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{
|
||||
|
||||
@@ -18,12 +18,11 @@ use hbb_common::{
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
ffi::{CString, OsString},
|
||||
fs, io,
|
||||
io::prelude::*,
|
||||
fs,
|
||||
io::{self, prelude::*},
|
||||
mem,
|
||||
os::windows::process::CommandExt,
|
||||
path::*,
|
||||
process::{Command, Stdio},
|
||||
ptr::null_mut,
|
||||
sync::{atomic::Ordering, Arc, Mutex},
|
||||
time::{Duration, Instant},
|
||||
@@ -32,11 +31,13 @@ use wallpaper;
|
||||
use winapi::{
|
||||
ctypes::c_void,
|
||||
shared::{minwindef::*, ntdef::NULL, windef::*, winerror::*},
|
||||
um::sysinfoapi::{GetNativeSystemInfo, SYSTEM_INFO},
|
||||
um::{
|
||||
errhandlingapi::GetLastError,
|
||||
handleapi::CloseHandle,
|
||||
libloaderapi::{GetProcAddress, LoadLibraryA},
|
||||
libloaderapi::{
|
||||
GetProcAddress, LoadLibraryExA, LoadLibraryExW, LOAD_LIBRARY_SEARCH_SYSTEM32,
|
||||
LOAD_LIBRARY_SEARCH_USER_DIRS,
|
||||
},
|
||||
minwinbase::STILL_ACTIVE,
|
||||
processthreadsapi::{
|
||||
GetCurrentProcess, GetCurrentProcessId, GetExitCodeProcess, OpenProcess,
|
||||
@@ -44,6 +45,7 @@ use winapi::{
|
||||
},
|
||||
securitybaseapi::GetTokenInformation,
|
||||
shellapi::ShellExecuteW,
|
||||
sysinfoapi::{GetNativeSystemInfo, SYSTEM_INFO},
|
||||
winbase::*,
|
||||
wingdi::*,
|
||||
winnt::{
|
||||
@@ -1563,10 +1565,63 @@ pub fn is_win_10_or_greater() -> bool {
|
||||
unsafe { is_windows_10_or_greater() > 0 }
|
||||
}
|
||||
|
||||
pub fn bootstrap() {
|
||||
pub fn bootstrap() -> bool {
|
||||
if let Ok(lic) = get_license_from_exe_name() {
|
||||
*config::EXE_RENDEZVOUS_SERVER.write().unwrap() = lic.host.clone();
|
||||
}
|
||||
|
||||
set_safe_load_dll()
|
||||
}
|
||||
|
||||
fn set_safe_load_dll() -> bool {
|
||||
if !unsafe { set_default_dll_directories() } {
|
||||
return false;
|
||||
}
|
||||
|
||||
// `SetDllDirectoryW` should never fail.
|
||||
// https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setdlldirectoryw
|
||||
if unsafe { SetDllDirectoryW(wide_string("").as_ptr()) == FALSE } {
|
||||
eprintln!("SetDllDirectoryW failed: {}", io::Error::last_os_error());
|
||||
return false;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
// https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-setdefaultdlldirectories
|
||||
unsafe fn set_default_dll_directories() -> bool {
|
||||
let module = LoadLibraryExW(
|
||||
wide_string("Kernel32.dll").as_ptr(),
|
||||
0 as _,
|
||||
LOAD_LIBRARY_SEARCH_SYSTEM32,
|
||||
);
|
||||
if module.is_null() {
|
||||
return false;
|
||||
}
|
||||
|
||||
match CString::new("SetDefaultDllDirectories") {
|
||||
Err(e) => {
|
||||
eprintln!("CString::new failed: {}", e);
|
||||
return false;
|
||||
}
|
||||
Ok(func_name) => {
|
||||
let func = GetProcAddress(module, func_name.as_ptr());
|
||||
if func.is_null() {
|
||||
eprintln!("GetProcAddress failed: {}", io::Error::last_os_error());
|
||||
return false;
|
||||
}
|
||||
type SetDefaultDllDirectories = unsafe extern "system" fn(DWORD) -> BOOL;
|
||||
let func: SetDefaultDllDirectories = std::mem::transmute(func);
|
||||
if func(LOAD_LIBRARY_SEARCH_SYSTEM32 | LOAD_LIBRARY_SEARCH_USER_DIRS) == FALSE {
|
||||
eprintln!(
|
||||
"SetDefaultDllDirectories failed: {}",
|
||||
io::Error::last_os_error()
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
pub fn create_shortcut(id: &str) -> ResultType<()> {
|
||||
@@ -2530,7 +2585,11 @@ pub fn try_kill_rustdesk_main_window_process() -> ResultType<()> {
|
||||
fn nt_terminate_process(process_id: DWORD) -> ResultType<()> {
|
||||
type NtTerminateProcess = unsafe extern "system" fn(HANDLE, DWORD) -> DWORD;
|
||||
unsafe {
|
||||
let h_module = LoadLibraryA(CString::new("ntdll.dll")?.as_ptr());
|
||||
let h_module = LoadLibraryExA(
|
||||
CString::new("ntdll.dll")?.as_ptr(),
|
||||
std::ptr::null_mut(),
|
||||
LOAD_LIBRARY_SEARCH_SYSTEM32,
|
||||
);
|
||||
if !h_module.is_null() {
|
||||
let f_nt_terminate_process: NtTerminateProcess = std::mem::transmute(GetProcAddress(
|
||||
h_module,
|
||||
|
||||
Reference in New Issue
Block a user