1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
use std::mem::size_of;
use std::cell::RefCell;
use std::process::Child;
use anyhow::bail;
use process_memory::{CopyAddress, ProcessHandle, ProcessHandleExt, TryIntoProcessHandle};
use sysinfo::{Pid, ProcessExt, System, SystemExt};
use crate::models::{StatsBlockWithFrames, StatsDataBlock, StatsFrame};
const DATA_BLOCK_SIZE: usize = size_of::<StatsDataBlock>();
const STATS_FRAME_SIZE: usize = size_of::<StatsFrame>();
thread_local! {
static BLOCK_BUF: RefCell<[u8; DATA_BLOCK_SIZE]> = RefCell::new([0_u8; DATA_BLOCK_SIZE]);
static FRAME_BUF: RefCell<[u8; STATS_FRAME_SIZE]> = RefCell::new([0_u8; STATS_FRAME_SIZE]);
}
pub struct OsInfo {
pub default_block_marker: usize,
pub default_process_name: String,
pub can_create_child: bool,
}
pub enum OperatingSystem {
Linux,
LinuxProton,
Windows
}
pub struct GameConnection {
pub pid: Pid,
pub path: String,
pub handle: ProcessHandle,
pub base_address: usize,
pub last_fetch: Option<StatsBlockWithFrames>,
pub child_handle: Option<Child>,
pub params: ConnectionParams,
pub(crate) pointers: Pointers
}
#[derive(Default)]
pub struct Pointers {
pub ddstats_block: Option<usize>,
pub base_address: Option<usize>
}
#[derive(Default)]
pub struct MemoryOverride {
pub block_marker: Option<usize>,
pub process_name: Option<String>,
}
pub struct ConnectionParams {
pub create_child: bool,
pub operating_system: OperatingSystem,
pub overrides: MemoryOverride,
}
impl OsInfo {
pub fn get_from_os(os: &OperatingSystem) -> Self {
match os {
OperatingSystem::Linux => Self {
can_create_child: true,
default_block_marker: 0x0053A850,
default_process_name: String::from("devildaggers")
},
OperatingSystem::Windows => Self {
can_create_child: false,
default_block_marker: 0x250DC0,
default_process_name: String::from("dd")
},
OperatingSystem::LinuxProton => Self {
can_create_child: false,
default_block_marker: 0x250DC0,
default_process_name: String::from("wine-preloader")
}
}
}
}
impl ConnectionParams {
pub fn empty() -> Self {
Self {
create_child: false,
operating_system: OperatingSystem::Linux,
overrides: MemoryOverride::default()
}
}
}
#[cfg(target_os = "windows")]
unsafe impl Send for GameConnection {}
#[cfg(target_os = "windows")]
unsafe impl Sync for GameConnection {}
impl GameConnection {
#[cfg(target_os = "windows")]
pub fn try_create(params: ConnectionParams) -> Result<Self, String> {
let os_info = OsInfo::get_from_os(¶ms.operating_system);
let proc_name = params.overrides.process_name.as_ref().unwrap_or(&os_info.default_process_name).clone();
let mut proc = get_proc(&proc_name);
if proc.is_none() { return Err("Process not found".into()); }
let mut pid = proc.as_ref().unwrap().1;
let mut handle = process_memory::Pid::from(pid as u32).try_into_process_handle().unwrap();
let mut c = None;
if let Err(e) = handle.copy_address(0, &mut [0u8]) {
if e.kind() == std::io::ErrorKind::PermissionDenied && os_info.can_create_child && params.create_child {
c = create_as_child(pid);
proc = get_proc(&proc_name);
pid = proc.as_ref().unwrap().1;
handle = process_memory::Pid::from(pid as u32).try_into_process_handle().unwrap();
}
}
let base_address = base_addr(handle, ¶ms);
if base_address.is_err() { return Err("Coudln't get base address".into()); }
let base_address = base_address.unwrap();
Ok(Self {
pid,
handle,
base_address,
path: proc.as_ref().unwrap().0.clone(),
child_handle: c,
last_fetch: None,
params,
pointers: Pointers::default()
})
}
#[cfg(target_os = "linux")]
pub fn try_create(params: ConnectionParams) -> Result<Self, String> {
let os_info = OsInfo::get_from_os(¶ms.operating_system);
let proc_name = params.overrides.process_name.as_ref().unwrap_or(&os_info.default_process_name).clone();
let mut proc = get_proc(&proc_name);
if proc.is_none() { return Err("Process not found".into()); }
let mut pid = proc.as_ref().unwrap().1;
let mut handle = process_memory::Pid::from(pid).try_into_process_handle().unwrap();
let mut c = None;
if let Err(e) = handle.copy_address(0, &mut [0u8]) {
if e.kind() == std::io::ErrorKind::PermissionDenied && os_info.can_create_child && params.create_child {
c = create_as_child(pid);
proc = get_proc(&proc_name);
pid = proc.as_ref().unwrap().1;
handle = process_memory::Pid::from(pid).try_into_process_handle().unwrap();
}
}
let base_address = base_addr(handle, ¶ms);
if base_address.is_err() { return Err("Coudln't get base address".into()); }
let base_address = base_address.unwrap();
Ok(Self {
pid,
handle,
base_address,
path: proc.as_ref().unwrap().0.clone(),
child_handle: c,
last_fetch: None,
params,
pointers: Pointers::default()
})
}
pub fn dead_connection() -> Self {
Self {
pid: 0,
base_address: 0,
last_fetch: None,
path: String::new(),
handle: ProcessHandle::null_type(),
child_handle: None,
params: ConnectionParams::empty(),
pointers: Pointers::default()
}
}
pub fn is_alive(&self) -> bool {
match self.handle.copy_address(self.base_address, &mut [0u8]) {
Ok(_) => true,
Err(_e) => false
}
}
pub fn is_alive_res(&self) -> Result<(), std::io::Error> {
match self.handle.copy_address(self.base_address, &mut [0u8]) {
Ok(_) => Ok(()),
Err(e) => Err(e),
}
}
pub fn read_stats_block(&mut self) -> Result<StatsDataBlock, std::io::Error> {
read_stats_data_block(self.handle, &self.params, &mut self.pointers)
}
pub fn read_mem(&self, addr: usize, buffer: &mut [u8]) -> Result<(), std::io::Error> {
self.handle.copy_address(addr, buffer)
}
#[cfg(target_os = "windows")]
pub fn maximize_dd(&self) {
use winapi::shared::windef::HWND;
use winapi::shared::minwindef::DWORD;
enumerate_windows(|hwnd: HWND| {
let mut pid: DWORD = DWORD::default();
unsafe { winapi::um::winuser::GetWindowThreadProcessId(hwnd, &mut pid); }
if pid as u32 != self.pid as u32 {
true
} else {
unsafe { winapi::um::winuser::ShowWindow(hwnd, 9); }
false
}
})
}
#[cfg(target_os = "linux")]
pub fn maximize_dd(&self) {
}
pub fn read_stats_block_with_frames(&mut self) -> Result<StatsBlockWithFrames, std::io::Error> {
if let Ok(data) = read_stats_data_block(self.handle, &self.params, &mut self.pointers) {
let res = StatsBlockWithFrames {
frames: self.stat_frames_from_block(&data)?,
block: data,
};
self.last_fetch = Some(res.clone());
return Ok(res);
}
Err(std::io::Error::new(std::io::ErrorKind::NotFound, "No data"))
}
pub fn stat_frames_from_block(
&mut self,
block: &StatsDataBlock,
) -> Result<Vec<StatsFrame>, std::io::Error> {
let (mut ptr, len) = (
block.get_stats_pointer(),
block.stats_frames_loaded as usize,
);
let mut res = Vec::with_capacity(len);
FRAME_BUF.with(|buf| {
let mut buf = buf.borrow_mut();
for _ in 0..len {
self.handle.copy_address(ptr, buf.as_mut())?;
let (_head, body, _tail) = unsafe { buf.align_to::<StatsFrame>() };
res.push(body[0].clone());
ptr += STATS_FRAME_SIZE;
}
return Ok(res);
})
}
pub fn replay_bin(&mut self) -> Result<Vec<u8>, std::io::Error> {
if let Some(block) = &self.last_fetch {
let (ptr, len) = (
block.block.get_replay_pointer(),
block.block.replay_buffer_length as usize,
);
let mut res = vec![0u8; len];
self.handle.copy_address(ptr, &mut res)?;
Ok(res)
} else {
Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Stats not available",
))
}
}
pub fn stat_frames(&self) -> Result<Vec<StatsFrame>, std::io::Error> {
use process_memory::*;
if let Some(last_data) = &self.last_fetch {
let (mut ptr, len) = (
last_data.block.get_stats_pointer(),
last_data.block.stats_frames_loaded as usize,
);
let mut res = Vec::with_capacity(len);
FRAME_BUF.with(|buf| {
let mut buf = buf.borrow_mut();
for _ in 0..len {
self.handle.copy_address(ptr, buf.as_mut())?;
let (_head, body, _tail) = unsafe { buf.align_to::<StatsFrame>() };
res.push(body[0].clone());
ptr += STATS_FRAME_SIZE;
}
return Ok(res);
})
} else {
Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Stats not available",
))
}
}
pub fn last_stat_frame(&self) -> Result<StatsFrame, std::io::Error> {
use process_memory::*;
if let Some(last_data) = &self.last_fetch {
let (mut ptr, len) = (
last_data.block.get_stats_pointer(),
last_data.block.stats_frames_loaded as usize,
);
ptr += STATS_FRAME_SIZE * (len - 1);
FRAME_BUF.with(|buf| {
let mut buf = buf.borrow_mut();
self.handle.copy_address(ptr, buf.as_mut())?;
let (_head, body, _tail) = unsafe { buf.align_to::<StatsFrame>() };
return Ok(body[0].clone());
})
} else {
Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Stats not available",
))
}
}
pub fn play_replay(&self, replay: Vec<u8>) -> anyhow::Result<()> {
use process_memory::*;
if let Some(last_data) = &self.last_fetch {
let ddstats_addr = self.pointers.ddstats_block.expect("last data can't exist without this also being set");
let replay_buffer_addr = last_data.block.get_replay_pointer();
let flag_addr = ddstats_addr + 316;
let len_addr = ddstats_addr + 312;
let len = replay.len() as i32;
self.handle.put_address(replay_buffer_addr, &replay)?;
self.handle.put_address(len_addr, &len.to_le_bytes())?;
self.handle.put_address(flag_addr, &[1])?;
Ok(())
} else {
bail!("No data found");
}
}
}
pub fn get_proc(process_name: &str) -> Option<(String, Pid)> {
let s = System::new_all();
for process in s.get_process_by_name(process_name) {
return Some((String::from(process.exe().to_str().unwrap()), process.pid()));
}
None
}
#[cfg(target_os = "windows")]
pub fn base_addr(handle: ProcessHandle, params: &ConnectionParams) -> Result<usize, std::io::Error> {
let os_info = OsInfo::get_from_os(¶ms.operating_system);
let proc_name = params.overrides.process_name.as_ref().unwrap_or(&os_info.default_process_name).clone();
let pid = unsafe { winapi::um::processthreadsapi::GetProcessId(handle.0) as usize };
get_base_address(pid, proc_name)
}
#[cfg(target_os = "linux")]
pub fn base_addr(handle: ProcessHandle, params: &ConnectionParams) -> Result<usize, std::io::Error> {
use scan_fmt::scan_fmt;
let os_info = OsInfo::get_from_os(¶ms.operating_system);
let proc_name = params.overrides.process_name.as_ref().unwrap_or(&os_info.default_process_name).clone();
let pid = handle.0;
match ¶ms.operating_system {
&OperatingSystem::Linux => get_base_address(pid, proc_name),
&OperatingSystem::Windows => get_base_address(pid, proc_name),
&OperatingSystem::LinuxProton => {
use std::{
fs::File,
io::{BufRead, BufReader},
};
let f = BufReader::new(File::open(format!("/proc/{}/maps", pid))?);
let mut magic_buf = [0u8; 4];
for line in f.lines() {
if let Ok(line) = line {
if let Ok((start, _end, perms, mod_path)) = scan_fmt!(&line, "{x}-{x} {} {*} {*} {*} {[^\t\n]}\n", [hex usize], [hex usize], String, String)
{
let r = handle.copy_address(start, &mut magic_buf);
if r.is_err() {
continue;
}
if mod_path.contains("steamclient.so") && perms.contains("x") {
return Ok(start);
}
}
}
}
Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"No base address",
))
}
}
}
pub fn is_elf(start_bytes: &[u8; 4]) -> bool {
let elf_signature: [u8; 4] = [0x7f, 0x45, 0x4c, 0x46];
elf_signature == *start_bytes
}
#[cfg(target_os = "linux")]
pub fn get_base_address(pid: Pid, proc_name: String) -> Result<usize, std::io::Error> {
use scan_fmt::scan_fmt;
use std::{
fs::File,
io::{BufRead, BufReader},
};
let f = BufReader::new(File::open(format!("/proc/{}/maps", pid))?);
let handle = pid.try_into_process_handle().expect("Coudln't create handle from PID");
let mut magic_buf = [0u8; 4];
for line in f.lines() {
if let Ok(line) = line {
if let Ok((start, _end, perms, mod_path)) = scan_fmt!(&line, "{x}-{x} {} {*} {*} {*} {[^\t\n]}\n", [hex usize], [hex usize], String, String)
{
let r = handle.copy_address(start, &mut magic_buf);
if r.is_err() {
continue;
}
if is_elf(&magic_buf) && mod_path.contains(&proc_name) && perms.contains("x") {
return Ok(start);
}
}
}
}
Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"No base address",
))
}
#[cfg(target_os = "windows")]
pub fn enumerate_windows<F>(mut callback: F)
where F: FnMut(winapi::shared::windef::HWND) -> bool
{
use winapi::shared::windef::HWND;
use winapi::shared::minwindef::LPARAM;
use winapi::um::winuser::EnumWindows;
use std::mem;
use winapi::ctypes::c_void;
let mut trait_obj: &mut dyn FnMut(HWND) -> bool = &mut callback;
let closure_pointer_pointer: *mut c_void = unsafe { mem::transmute(&mut trait_obj) };
let lparam = closure_pointer_pointer as LPARAM;
unsafe { EnumWindows(Some(enumerate_callback), lparam) };
}
#[cfg(target_os = "windows")]
unsafe extern "system" fn enumerate_callback(hwnd: winapi::shared::windef::HWND, lparam: winapi::shared::minwindef::LPARAM) -> winapi::shared::minwindef::BOOL {
use std::mem;
use winapi::shared::windef::HWND;
use winapi::shared::minwindef::{TRUE, FALSE};
use winapi::ctypes::c_void;
let closure: &mut &mut dyn FnMut(HWND) -> bool = mem::transmute(lparam as *mut c_void);
if closure(hwnd) { TRUE } else { FALSE }
}
#[cfg(target_os = "windows")]
pub fn get_base_address(pid: Pid, _proc_name: String) -> Result<usize, std::io::Error> {
use winapi::um::handleapi::CloseHandle;
use std::os::raw::c_ulong;
use winapi::um::tlhelp32::MODULEENTRY32;
let snapshot = unsafe {
winapi::um::tlhelp32::CreateToolhelp32Snapshot(
winapi::um::tlhelp32::TH32CS_SNAPMODULE | winapi::um::tlhelp32::TH32CS_SNAPMODULE32,
pid as u32 as c_ulong as winapi::shared::minwindef::DWORD,
)
};
let mut module = std::mem::MaybeUninit::<MODULEENTRY32>::uninit();
unsafe {
winapi::um::tlhelp32::Module32First(snapshot, module.as_mut_ptr());
CloseHandle(snapshot);
let module = module.assume_init();
return Ok(module.modBaseAddr as usize);
}
}
#[cfg(target_os = "windows")]
fn create_as_child(_pid: Pid) -> Option<Child> {
return None;
}
#[cfg(target_os = "linux")]
fn create_as_child(pid: Pid) -> Option<Child> {
use std::{
fs::File,
io::{BufReader, Read},
path::Path,
process::Command,
};
let mut exe = String::new();
BufReader::new(File::open(format!("/proc/{}/cmdline", pid)).expect("Coudln't read cmdline"))
.read_to_string(&mut exe)
.unwrap();
let cwd = Path::new(&format!("/proc/{}/cwd", pid)).to_owned();
let mut exe = exe.chars();
exe.next_back();
let exe = exe.as_str();
Command::new("kill")
.arg(format!("{}", pid))
.spawn()
.expect("Couldn't kill current DD process");
let old_cwd = std::env::current_dir().expect("Couldn't save cwd");
std::env::set_current_dir(&cwd).expect("Coudln't set cwd");
Command::new("sh")
.arg("-c")
.arg("echo")
.arg("422970 > steam_appid.txt")
.spawn()
.expect("Coudln't write steam appid");
Command::new("nohup")
.arg(exe)
.spawn()
.expect("Couldn't create DD child process");
std::env::set_current_dir(&old_cwd).expect("Couldn't set cwd");
return None;
}
pub fn mem_search(handle: ProcessHandle, to_find: &[u8]) -> Result<usize, std::io::Error> {
let mut big_ass_buffer = [0_u8; 1024 * 100];
let mut offset = 0x00010000;
loop {
handle.copy_address(offset, &mut big_ass_buffer)?;
for (i, w) in big_ass_buffer.windows(to_find.len()).enumerate() {
if w == to_find {
return Ok(offset + i);
}
}
offset += big_ass_buffer.len();
}
}
fn calc_pointer_ddstats_block(handle: ProcessHandle, params: &ConnectionParams, base_address: usize) -> Result<usize, std::io::Error> {
let os_info = OsInfo::get_from_os(¶ms.operating_system);
let block_start = params.overrides.block_marker.unwrap_or(os_info.default_block_marker);
match ¶ms.operating_system {
&OperatingSystem::Linux => {
handle.get_offset(&[base_address + block_start, 0x0, 0x1F10])
},
&OperatingSystem::Windows => {
handle.get_offset(&[base_address + block_start, 0])
},
&OperatingSystem::LinuxProton => {
mem_search(handle, b"__ddstats__")
}
}
}
pub fn read_stats_data_block(handle: ProcessHandle, params: &ConnectionParams, pointers: &mut Pointers) -> Result<StatsDataBlock, std::io::Error> {
use process_memory::*;
let base = if pointers.base_address.is_none() { base_addr(handle, params)? } else { pointers.base_address.as_ref().unwrap().clone() };
pointers.base_address = Some(base);
BLOCK_BUF.with(|buf| {
let pointer;
if let Some(ddstats_ptr) = pointers.ddstats_block {
pointer = ddstats_ptr;
} else {
pointers.ddstats_block = Some(calc_pointer_ddstats_block(handle, params, base)?);
pointer = pointers.ddstats_block.as_ref().unwrap().clone();
}
let mut buf = buf.borrow_mut();
handle.copy_address(pointer, buf.as_mut())?;
if !buf.starts_with(b"__ddstats__") {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "No ddstats block found at address"));
}
let (_head, body, _tail) = unsafe { buf.as_mut().align_to::<StatsDataBlock>() };
Ok(body[0].clone())
})
}