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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
use std::collections::HashMap;
use std::mem::size_of;
use std::cell::RefCell;
use std::process::Child;
use anyhow::bail;
use sysinfo::{Pid, ProcessExt, System, SystemExt};
use crate::models::{StatsBlockWithFrames, StatsDataBlock, StatsFrame};
use self::proc_mem_wrapper::Handle;
pub mod proc_mem_wrapper;
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]);
static SYSTEM: RefCell<System> = RefCell::new(System::new_all());
}
pub struct OsInfo {
pub default_block_marker: usize,
pub default_process_name: String,
pub can_create_child: bool,
pub offsets: HashMap<String, Vec<usize>>
}
#[derive(Debug)]
pub enum OperatingSystem {
Linux,
LinuxProton,
Windows
}
pub struct GameConnection {
pub pid: Pid,
pub path: String,
pub handle: Handle,
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,
}
#[repr(C)]
#[derive(Debug)]
pub struct Vec3 {
pub x: f32,
pub y: f32,
pub z: f32,
}
impl OsInfo {
pub fn get_from_os(os: &OperatingSystem) -> Self {
match os {
OperatingSystem::Linux => Self {
can_create_child: true,
default_block_marker: 0x00521C98,
default_process_name: String::from("devildaggers"),
offsets: HashMap::new()
},
OperatingSystem::Windows => Self {
can_create_child: false,
default_block_marker: 0x250DC0,
default_process_name: String::from("dd.exe"),
offsets: HashMap::new()
},
OperatingSystem::LinuxProton => Self {
can_create_child: false,
default_block_marker: 0x250DC0,
default_process_name: String::from("wine-preloader"),
offsets: HashMap::new()
}
}
}
}
impl ConnectionParams {
pub fn empty() -> Self {
Self {
create_child: false,
operating_system: OperatingSystem::Linux,
overrides: MemoryOverride::default()
}
}
}
impl GameConnection {
#[cfg(target_os = "windows")]
pub fn try_create(params: ConnectionParams) -> anyhow::Result<Self> {
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 proc = get_proc(&proc_name);
if proc.is_none() { anyhow::bail!("Process not found") }
let pid = proc.as_ref().unwrap().1;
let handle = Handle::new(pid)?;
let base_address = base_addr(&handle, ¶ms);
if base_address.is_err() { anyhow::bail!("Couldn't get base address") }
let base_address = base_address.unwrap();
let mut ptrs = Pointers::default();
ptrs.base_address = Some(base_address);
Ok(Self {
pid,
handle,
base_address,
path: proc.as_ref().unwrap().0.clone(),
child_handle: None,
last_fetch: None,
params,
pointers: ptrs
})
}
#[cfg(target_os = "linux")]
pub fn try_create(params: ConnectionParams) -> anyhow::Result<Self> {
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() { anyhow::bail!("Process not found") }
let mut pid = proc.as_ref().unwrap().1;
let mut handle = Handle::new(pid as usize)?;
let mut c = None;
if let Err(_) = handle.copy_address(0, &mut [0u8]) {
if params.create_child {
c = create_as_child(pid);
proc = get_proc(&proc_name);
pid = proc.as_ref().unwrap().1;
handle = Handle::new(pid as usize)?;
}
}
let base_address = base_addr(&handle, ¶ms);
if base_address.is_err() { anyhow::bail!("Couldn't get base address") }
let base_address = base_address.unwrap();
let mut ptrs = Pointers::default();
ptrs.base_address = Some(base_address);
Ok(Self {
pid,
handle,
base_address,
path: proc.as_ref().unwrap().0.clone(),
child_handle: c,
last_fetch: None,
params,
pointers: ptrs
})
}
pub fn dead_connection() -> Self {
Self {
pid: 0,
base_address: 0,
last_fetch: None,
path: String::new(),
handle: Handle::null_type(),
child_handle: None,
params: ConnectionParams::empty(),
pointers: Pointers::default()
}
}
pub fn is_alive(&self) -> bool {
let mut buf = [0u8, 0];
match self.handle.copy_address(self.base_address, &mut buf) {
Ok(_) => true,
Err(_e) => false
}
}
pub fn is_alive_res(&self) -> anyhow::Result<()> {
let mut buf = [0u8];
match self.handle.copy_address(self.base_address, &mut buf) {
Ok(_) => Ok(()),
Err(e) => Err(e),
}
}
pub fn read_stats_block(&mut self) -> anyhow::Result<StatsDataBlock> {
read_stats_data_block(&self.handle, &self.params, &mut self.pointers)
}
pub fn read_mem(&self, addr: usize, buffer: &mut [u8]) -> anyhow::Result<()> {
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) -> anyhow::Result<StatsBlockWithFrames> {
match read_stats_data_block(&self.handle, &self.params, &mut self.pointers) {
Ok(data) => {
let res = StatsBlockWithFrames {
frames: self.stat_frames_from_block(&data)?,
block: data,
};
self.last_fetch = Some(res.clone());
Ok(res)
},
Err(e) => {
log::info!("{e:?}");
Err(anyhow::anyhow!(e))
}
}
}
pub fn stat_frames_from_block(
&mut self,
block: &StatsDataBlock,
) -> anyhow::Result<Vec<StatsFrame>> {
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) -> anyhow::Result<Vec<u8>> {
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(anyhow::anyhow!(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Stats not available",
)))
}
}
pub fn stat_frames(&self) -> anyhow::Result<Vec<StatsFrame>> {
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(anyhow::anyhow!(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Stats not available",
)))
}
}
pub fn last_stat_frame(&self) -> anyhow::Result<StatsFrame> {
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(anyhow::anyhow!(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Stats not available",
)))
}
}
pub fn play_replay(&self, replay: std::sync::Arc<Vec<u8>>) -> anyhow::Result<()> {
if let Some(last_data) = &self.last_fetch {
#[cfg(feature = "logger")]
log::info!("Attempting to load replay");
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;
#[cfg(feature = "logger")]
log::info!("{:X} {:X} {:X}", ddstats_addr, replay_buffer_addr, flag_addr);
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)> {
SYSTEM.with(|s| {
let mut s = s.borrow_mut();
s.refresh_processes();
for process in s.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: &Handle, params: &ConnectionParams) -> anyhow::Result<usize> {
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();
#[cfg(feature = "logger")]
log::info!("reading base address: {} {}", handle.pid, proc_name);
let addr = unsafe { get_base_address(handle.pid, proc_name) };
#[cfg(feature = "logger")]
log::info!("base address: {:?}", addr);
addr
}
#[cfg(target_os = "linux")]
pub fn base_addr(handle: &Handle, params: &ConnectionParams) -> anyhow::Result<usize> {
use std::io::Read;
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.pid as i32;
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 mut stat = String::new();
BufReader::new(File::open(format!("/proc/{}/stat", pid))?).read_to_string(&mut stat)?;
if !stat.contains("dd.exe") {
return Err(anyhow::anyhow!(std::io::Error::new(std::io::ErrorKind::NotFound, "Not the correct process")));
}
let f = BufReader::new(File::open(format!("/proc/{}/maps", pid))?);
let mut magic_buf = [0u8; 2];
#[cfg(feature = "logger")]
log::info!("Found LinuxProton dd.exe {:?}", pid);
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() {
#[cfg(feature = "logger")]
log::info!("Failed to read memory {:?} {} {:X}", r.err(), pid, start);
continue;
}
if mod_path.contains("dd.exe") && is_windows_exe(&magic_buf) {
return Ok(start);
}
}
}
}
Err(anyhow::anyhow!(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
}
pub fn is_windows_exe(start_bytes: &[u8; 2]) -> bool {
let elf_signature: [u8; 2] = [0x4D, 0x5A];
elf_signature == *start_bytes
}
#[cfg(target_os = "linux")]
pub fn get_base_address(pid: Pid, proc_name: String) -> anyhow::Result<usize> {
use scan_fmt::scan_fmt;
use std::{
fs::File,
io::{BufRead, BufReader},
};
let f = BufReader::new(File::open(format!("/proc/{}/maps", pid))?);
let handle = Handle::new(pid as usize)?;
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(anyhow::anyhow!(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 unsafe fn get_base_address(pid: Pid, _proc_name: String) -> anyhow::Result<usize> {
use winapi::um::handleapi::CloseHandle;
use std::{mem::size_of_val, os::raw::c_ulong};
let snapshot = winapi::um::tlhelp32::CreateToolhelp32Snapshot(
winapi::um::tlhelp32::TH32CS_SNAPMODULE | winapi::um::tlhelp32::TH32CS_SNAPMODULE32,
pid as winapi::shared::minwindef::DWORD,
);
let mut me = winapi::um::tlhelp32::MODULEENTRY32::default();
me.dwSize = size_of_val(&me) as c_ulong as winapi::shared::minwindef::DWORD;
winapi::um::tlhelp32::Module32First(snapshot, &mut me);
let res = me.modBaseAddr.clone() as usize;
CloseHandle(snapshot);
Ok(res)
}
#[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: &Handle, to_find: &[u8]) -> anyhow::Result<usize> {
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: &Handle, params: &ConnectionParams, base_address: usize) -> anyhow::Result<usize> {
let os_info = OsInfo::get_from_os(¶ms.operating_system);
let block_start = params.overrides.block_marker.unwrap_or(os_info.default_block_marker);
log::info!("block start {block_start}");
match ¶ms.operating_system {
&OperatingSystem::Linux => {
handle.get_offset(&[base_address + block_start, 0])
},
&OperatingSystem::Windows => {
handle.get_offset(&[base_address + block_start, 0])
},
&OperatingSystem::LinuxProton => {
mem_search(&handle, b"__ddstats__")
}
}
}
pub fn read_stats_data_block(handle: &Handle, params: &ConnectionParams, pointers: &mut Pointers) -> anyhow::Result<StatsDataBlock> {
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(anyhow::anyhow!(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())
})
}
#[cfg(target_os = "windows")]
pub fn start_dd() -> anyhow::Result<()> {
use std::process::Command;
Command::new("cmd").arg("/c start steam://run/422970").output()?;
Ok(())
}
#[cfg(target_os = "linux")]
pub fn start_dd() -> anyhow::Result<()> {
use std::process::Command;
Command::new("steam").arg("steam://run/422970").output()?;
Ok(())
}