vmspect 0.3.2

Blazing-fast static inspection, forensic analysis and information extraction library for virtual machine disk images (VMDK, RAW, QCOW2, VHD, VHDX, VDI).
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
//! Linux guest analysis using the `ext4` crate.
//!
//! Reads the file system in user space without mounting the partition on the host operating system.
use crate::error::Result;
use crate::models::traits::{AnalysisResult, OsInspector, VmDriver};
use crate::models::{FileSystem, GuestInfo, GuestTools, Options, Partition, Program};
use crate::vms::stream::VirtualDisk;
use ext4::SuperBlock;
use positioned_io::ReadAt;

use std::io::Read;

pub(crate) struct LinuxInspector;

impl OsInspector for LinuxInspector {
    fn analyze(
        &self,
        driver: &dyn VmDriver,
        partitions: &[Partition],
        chunk_size: u64,
        options: &Options,
    ) -> Result<AnalysisResult> {
        if !options.should_analyze_system() && !options.should_analyze_apps() {
            return Ok(AnalysisResult::default());
        }

        let mut candidates: Vec<_> = partitions
            .iter()
            .filter(|p| {
                matches!(
                    p.file_system,
                    FileSystem::Ext4 | FileSystem::Ext3 | FileSystem::Ext2
                )
            })
            .collect();

        candidates.sort_by_key(|p| std::cmp::Reverse(p.size));

        for partition in candidates {
            let mut disk = VirtualDisk::new(driver, partition.start, partition.size, chunk_size);

            if let Ok(sb) = SuperBlock::new(&mut disk) {
                if let Ok(result) = analyze_file_system(&sb, options) {
                    return Ok(result);
                }
            }
        }

        // Fallback
        Ok(AnalysisResult {
            guest_info: if options.should_analyze_system() {
                GuestInfo {
                    os_name: "Linux (rootfs could not be read)".to_string(),
                    ..GuestInfo::default()
                }
            } else {
                GuestInfo::default()
            },
            programs: Vec::new(),
            warnings: Vec::new(),
        })
    }
}

fn analyze_file_system<R: ReadAt>(sb: &SuperBlock<R>, options: &Options) -> Result<AnalysisResult> {
    let mut guest_info = GuestInfo::default();
    let mut programs = Vec::new();

    if options.should_analyze_system() {
        if let Ok(content) = read_text_file(sb, "/etc/os-release")
            .or_else(|_| read_text_file(sb, "/usr/lib/os-release"))
        {
            parse_os_release(&content, &mut guest_info);
        }

        if let Ok(hostname) = read_text_file(sb, "/etc/hostname") {
            let name = hostname.trim();
            if !name.is_empty() {
                guest_info.os_edition = format!("Host: {}", name);
            }
        }
    }

    if let Ok(dpkg_status) = read_text_file(sb, "/var/lib/dpkg/status") {
        let (pkgs, guest_tools) = parse_dpkg_status(&dpkg_status, options);
        if options.should_analyze_apps() {
            programs.extend(pkgs);
        }
        if options.should_analyze_system() {
            if let Some(tools) = guest_tools {
                guest_info.guest_tools = Some(tools);
            }
        }
    }

    if options.should_analyze_system() && guest_info.guest_tools.is_none() {
        guest_info.guest_tools = detect_guest_tools_linux_files(sb);
    }

    if options.should_analyze_apps() {
        // Sort and deduplicate respecting the Program structure
        programs.sort_by(|a, b| a.name.cmp(&b.name));
        programs.dedup_by(|a, b| a.name == b.name && a.version == b.version);
    }

    Ok(AnalysisResult {
        guest_info,
        programs,
        warnings: Vec::new(),
    })
}

/// Inspects binaries or service units of the Linux guest OS to detect integration tool
/// suites (VMware Tools, VirtualBox Guest Additions, QEMU Guest Agent and Hyper-V
/// Integration Services) when they are not provided by the package manager.
fn detect_guest_tools_linux_files<R: ReadAt>(sb: &SuperBlock<R>) -> Option<GuestTools> {
    // 1. VMware
    let vmware_paths = [
        "/usr/bin/vmtoolsd",
        "/usr/sbin/vmtoolsd",
        "/bin/vmtoolsd",
        "/sbin/vmtoolsd",
        "/etc/vmware-tools",
        "/lib/systemd/system/open-vm-tools.service",
        "/etc/systemd/system/open-vm-tools.service",
    ];
    for path in &vmware_paths {
        if sb.resolve_path(path).is_ok() {
            return Some(GuestTools {
                kind: "VMware Tools".to_string(),
                version: None,
                present: true,
            });
        }
    }

    // 2. VirtualBox
    let vbox_paths = [
        "/usr/sbin/VBoxService",
        "/usr/bin/VBoxService",
        "/usr/sbin/vboxservice",
        "/usr/bin/vboxservice",
        "/sbin/VBoxService",
        "/lib/systemd/system/vboxadd-service.service",
        "/etc/systemd/system/vboxadd-service.service",
        "/opt/VBoxGuestAdditions",
    ];
    for path in &vbox_paths {
        if sb.resolve_path(path).is_ok() {
            return Some(GuestTools {
                kind: "VirtualBox Guest Additions".to_string(),
                version: None,
                present: true,
            });
        }
    }

    // 3. QEMU Guest Agent
    let qemu_paths = [
        "/usr/bin/qemu-ga",
        "/usr/sbin/qemu-ga",
        "/lib/systemd/system/qemu-guest-agent.service",
        "/etc/systemd/system/qemu-guest-agent.service",
    ];
    for path in &qemu_paths {
        if sb.resolve_path(path).is_ok() {
            return Some(GuestTools {
                kind: "QEMU Guest Agent".to_string(),
                version: None,
                present: true,
            });
        }
    }

    // 4. Hyper-V
    let hyperv_paths = [
        "/usr/sbin/hv_kvp_daemon",
        "/usr/bin/hv_kvp_daemon",
        "/usr/sbin/hv_vss_daemon",
        "/usr/sbin/hv_fcopy_daemon",
        "/lib/systemd/system/hv-kvp-daemon.service",
        "/lib/systemd/system/hypervkvp.service",
    ];
    for path in &hyperv_paths {
        if sb.resolve_path(path).is_ok() {
            return Some(GuestTools {
                kind: "Hyper-V Integration Services".to_string(),
                version: None,
                present: true,
            });
        }
    }

    None
}

fn parse_dpkg_status(content: &str, options: &Options) -> (Vec<Program>, Option<GuestTools>) {
    let mut packages = Vec::new();
    let mut guest_tools: Option<GuestTools> = None;

    let mut current_pkg = String::new();
    let mut current_ver = String::new();
    let mut current_section = String::new();
    let mut installed = false;

    let should_apps = options.should_analyze_apps();

    let process_package = |pkg: &str,
                           ver: &str,
                           sec: &str,
                           inst: bool,
                           pkgs: &mut Vec<Program>,
                           tools: &mut Option<GuestTools>| {
        if inst && !pkg.is_empty() {
            // Capture hypervisor-agnostic integration tools
            if tools.is_none() {
                let ver_opt = if !ver.is_empty() {
                    Some(ver.to_string())
                } else {
                    None
                };

                if pkg == "open-vm-tools" || pkg == "open-vm-tools-desktop" {
                    *tools = Some(GuestTools {
                        kind: "VMware Tools".to_string(),
                        version: ver_opt,
                        present: true,
                    });
                } else if pkg == "virtualbox-guest-utils"
                    || pkg == "virtualbox-guest-x11"
                    || pkg == "virtualbox-guest-dkms"
                    || pkg == "virtualbox-guest-additions-iso"
                {
                    *tools = Some(GuestTools {
                        kind: "VirtualBox Guest Additions".to_string(),
                        version: ver_opt,
                        present: true,
                    });
                } else if pkg == "qemu-guest-agent" {
                    *tools = Some(GuestTools {
                        kind: "QEMU Guest Agent".to_string(),
                        version: ver_opt,
                        present: true,
                    });
                } else if pkg == "hyperv-daemons"
                    || pkg == "hv-kvp-daemon-init"
                    || pkg == "linux-cloud-tools-virtual"
                {
                    *tools = Some(GuestTools {
                        kind: "Hyper-V Integration Services".to_string(),
                        version: ver_opt,
                        present: true,
                    });
                }
            }

            if should_apps {
                let version_opt = if !ver.is_empty() {
                    Some(ver.to_string())
                } else {
                    None
                };

                let publisher_opt = if !sec.is_empty() {
                    Some(sec.to_string())
                } else {
                    None
                };

                pkgs.push(Program {
                    name: pkg.to_string(),
                    version: version_opt,
                    publisher: publisher_opt,
                    source: None,
                });
            }
        }
    };

    for line in content.lines() {
        if let Some(cancel) = &options.cancel_token {
            if cancel.load(std::sync::atomic::Ordering::Relaxed) {
                break;
            }
        }

        if line.starts_with("Package: ") {
            current_pkg = line.trim_start_matches("Package: ").trim().to_string();
        } else if line.starts_with("Status: ") {
            installed = line.contains("install ok installed");
        } else if line.starts_with("Version: ") {
            current_ver = line.trim_start_matches("Version: ").trim().to_string();
        } else if line.starts_with("Section: ") {
            current_section = line.trim_start_matches("Section: ").trim().to_string();
        } else if line.trim().is_empty() {
            process_package(
                &current_pkg,
                &current_ver,
                &current_section,
                installed,
                &mut packages,
                &mut guest_tools,
            );
            current_pkg.clear();
            current_ver.clear();
            current_section.clear();
            installed = false;
        }
    }

    // Process the last package if there was no blank line at the end.
    process_package(
        &current_pkg,
        &current_ver,
        &current_section,
        installed,
        &mut packages,
        &mut guest_tools,
    );

    (packages, guest_tools)
}

/// Reads a text file from the ext4 partition and returns it as a String.
fn read_text_file<R: ReadAt>(sb: &SuperBlock<R>, path: &str) -> Result<String> {
    let entry = sb
        .resolve_path(path)
        .map_err(|e| crate::error::VmSpectError::FileSystem(format!("{:?}", e)))?;
    let inode = sb
        .load_inode(entry.inode)
        .map_err(|e| crate::error::VmSpectError::FileSystem(format!("{:?}", e)))?;
    let mut reader = sb
        .open(&inode)
        .map_err(|e| crate::error::VmSpectError::FileSystem(format!("{:?}", e)))?;
    let mut buffer = Vec::new();
    reader.read_to_end(&mut buffer)?;
    Ok(String::from_utf8_lossy(&buffer).to_string())
}

/// Parses `/etc/os-release`, extracting the OS name and version.
fn parse_os_release(content: &str, info: &mut GuestInfo) {
    for line in content.lines() {
        let line = line.trim();
        if line.starts_with('#') || !line.contains('=') {
            continue;
        }

        let mut parts = line.splitn(2, '=');
        let key = parts.next().unwrap_or("").trim();
        let value = parts
            .next()
            .unwrap_or("")
            .trim()
            .trim_matches('"')
            .trim_matches('\'');

        match key {
            "PRETTY_NAME" => info.os_name = value.to_string(),
            "VERSION_ID" if info.os_build.is_empty() => info.os_build = value.to_string(),
            "NAME" if info.os_name.is_empty() => info.os_name = value.to_string(),
            _ => {}
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_os_release() {
        let os_release = r#"
NAME="Ubuntu"
VERSION="22.04.3 LTS (Jammy Jellyfish)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 22.04.3 LTS"
VERSION_ID="22.04"
"#;
        let mut info = GuestInfo::default();
        parse_os_release(os_release, &mut info);
        assert_eq!(info.os_name, "Ubuntu 22.04.3 LTS");
        assert_eq!(info.os_build, "22.04");
    }

    #[test]
    fn test_parse_dpkg_status_agnostic() {
        let dpkg = r#"
Package: open-vm-tools
Status: install ok installed
Priority: optional
Section: admin
Installed-Size: 3120
Maintainer: Ubuntu Developers
Architecture: amd64
Version: 2:12.1.5-1ubuntu0.22.04.4

Package: mosquitto
Status: install ok installed
Priority: optional
Section: net
Version: 2.0.11-1ubuntu1.1

Package: libc6
Status: install ok installed
Priority: required
Section: libs
Version: 2.35-0ubuntu3.4

Package: python3-minimal
Status: install ok installed
Priority: optional
Section: python
Version: 3.10.6-1~22.04
"#;
        let options = Options::default();
        let (packages, tools) = parse_dpkg_status(dpkg, &options);
        assert_eq!(
            tools,
            Some(GuestTools {
                kind: "VMware Tools".to_string(),
                version: Some("2:12.1.5-1ubuntu0.22.04.4".to_string()),
                present: true,
            })
        );
        // Must include ALL packages without filtering by libraries, prefixes or suffixes
        assert_eq!(packages.len(), 4);
        assert!(packages.iter().any(|p| p.name == "open-vm-tools"));
        assert!(packages.iter().any(|p| p.name == "mosquitto"));
        assert!(packages.iter().any(|p| p.name == "libc6"));
        assert!(packages.iter().any(|p| p.name == "python3-minimal"));
    }

    #[test]
    fn test_parse_dpkg_status_no_apps() {
        let dpkg = r#"
Package: open-vm-tools
Status: install ok installed
Priority: optional
Section: admin
Version: 2:12.1.5-1ubuntu0.22.04.4

Package: mosquitto
Status: install ok installed
Priority: optional
Section: net
Version: 2.0.11-1ubuntu1.1
"#;
        let options = Options {
            no_apps: true,
            ..Options::default()
        };
        let (packages, tools) = parse_dpkg_status(dpkg, &options);
        assert_eq!(
            tools,
            Some(GuestTools {
                kind: "VMware Tools".to_string(),
                version: Some("2:12.1.5-1ubuntu0.22.04.4".to_string()),
                present: true,
            })
        );
        assert!(packages.is_empty());
    }

    #[test]
    fn test_parse_dpkg_status_multiple_hypervisors() {
        let dpkg_vbox = r#"
Package: virtualbox-guest-utils
Status: install ok installed
Priority: optional
Section: admin
Version: 7.0.12-dfsg-1
"#;
        let (_, tools_vbox) = parse_dpkg_status(dpkg_vbox, &Options::default());
        assert_eq!(
            tools_vbox,
            Some(GuestTools {
                kind: "VirtualBox Guest Additions".to_string(),
                version: Some("7.0.12-dfsg-1".to_string()),
                present: true,
            })
        );

        let dpkg_qemu = r#"
Package: qemu-guest-agent
Status: install ok installed
Priority: optional
Section: admin
Version: 1:8.2.2+ds-0ubuntu1
"#;
        let (_, tools_qemu) = parse_dpkg_status(dpkg_qemu, &Options::default());
        assert_eq!(
            tools_qemu,
            Some(GuestTools {
                kind: "QEMU Guest Agent".to_string(),
                version: Some("1:8.2.2+ds-0ubuntu1".to_string()),
                present: true,
            })
        );

        let dpkg_hyperv = r#"
Package: hyperv-daemons
Status: install ok installed
Priority: optional
Section: admin
Version: 5.15.0-91.101
"#;
        let (_, tools_hyperv) = parse_dpkg_status(dpkg_hyperv, &Options::default());
        assert_eq!(
            tools_hyperv,
            Some(GuestTools {
                kind: "Hyper-V Integration Services".to_string(),
                version: Some("5.15.0-91.101".to_string()),
                present: true,
            })
        );
    }
}