keyhog-sources 0.5.42

keyhog-sources: pluggable input backends for KeyHog (git, S3, GCS, Azure Blob, Docker, Web)
Documentation
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
//! Ghidra headless process orchestration and decompiled-output parsing.

use std::ffi::OsString;
use std::io::{BufRead, Read};
use std::path::{Path, PathBuf};
use std::process::{Child, Command};

use keyhog_core::{Chunk, ChunkMetadata, SourceError};
use wait_timeout::ChildExt;

use super::{
    BinaryAnalysisDegradation, BinaryAnalysisOutcome, BinaryAnalysisRequest, BinaryAnalyzer,
};

const GHIDRA_STDERR_EXCERPT_BYTES: usize = 4096;

pub(in crate::binary) struct GhidraAnalyzer {
    executable: PathBuf,
    arguments: Vec<OsString>,
}

impl GhidraAnalyzer {
    pub(in crate::binary) fn new(executable: impl Into<PathBuf>) -> Self {
        Self::with_arguments(executable, std::iter::empty())
    }

    pub(super) fn with_arguments(
        executable: impl Into<PathBuf>,
        arguments: impl IntoIterator<Item = OsString>,
    ) -> Self {
        Self {
            executable: executable.into(),
            arguments: arguments.into_iter().collect(),
        }
    }
}

impl BinaryAnalyzer for GhidraAnalyzer {
    fn analyze(
        &self,
        request: BinaryAnalysisRequest<'_>,
    ) -> Result<BinaryAnalysisOutcome, SourceError> {
        let tmp_dir = tempfile::tempdir().map_err(SourceError::Io)?;
        let project_dir = tmp_dir.path().join("ghidra_project");
        std::fs::create_dir_all(&project_dir).map_err(SourceError::Io)?;

        let script_path = tmp_dir.path().join("ExportDecompiled.java");
        let output_path = tmp_dir.path().join("decompiled.c");
        write_ghidra_script(&script_path, &output_path)?;

        let mut command = Command::new(&self.executable);
        command
            .args(&self.arguments)
            .arg(&project_dir)
            .arg("keyhog_analysis")
            .arg("-import")
            .arg(request.path)
            .arg("-postScript")
            .arg(&script_path)
            .arg("-deleteProject")
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::piped());
        isolate_analyzer_process_tree(&mut command);
        let mut child = command.spawn().map_err(SourceError::Io)?;
        let stderr_capture = child.stderr.take().map(capture_ghidra_stderr_excerpt);
        let timeout = request.timeout;
        let status = match child.wait_timeout(timeout) {
            Ok(Some(status)) => Ok(status),
            Ok(None) => {
                let cleanup = kill_and_reap_ghidra_child(&mut child, "Ghidra timeout cleanup");
                let message = match cleanup {
                    Ok(()) => format!("Ghidra analysis timed out after {}s", timeout.as_secs()),
                    Err(cleanup_error) => format!(
                        "Ghidra analysis timed out after {}s; cleanup failed: {cleanup_error}",
                        timeout.as_secs()
                    ),
                };
                Err(std::io::Error::new(std::io::ErrorKind::TimedOut, message))
            }
            Err(error) => {
                let cleanup = kill_and_reap_ghidra_child(&mut child, "Ghidra wait-error cleanup");
                let message = match cleanup {
                    Ok(()) => format!("Ghidra process wait failed: {error}"),
                    Err(cleanup_error) => format!(
                        "Ghidra process wait failed: {error}; cleanup failed: {cleanup_error}"
                    ),
                };
                Err(std::io::Error::other(message))
            }
        };
        let stderr_excerpt = match stderr_capture {
            Some(handle) => match handle.join() {
                Ok(excerpt) => excerpt,
                Err(panic) => {
                    drop(panic);
                    eprintln!(
                        "keyhog: WARNING: internal Ghidra stderr capture failed; \
                         deep-analysis failure reporting will use process status only."
                    );
                    String::new()
                }
            },
            // Process status still makes the degradation visible when no pipe handle exists.
            None => String::new(),
        };

        match status {
            Ok(status) if status.success() && output_path.exists() => {
                parse_decompiled_output(&output_path, request)
            }
            other => {
                let reason = match &other {
                    Ok(status) => {
                        format!("exited unsuccessfully (status {status}) or produced no output")
                    }
                    Err(error) => error.to_string(),
                };
                Ok(BinaryAnalysisOutcome::Degraded(
                    BinaryAnalysisDegradation::ToolFailure {
                        reason,
                        stderr_excerpt,
                    },
                ))
            }
        }
    }
}

pub(super) fn parse_decompiled_output(
    output_path: &Path,
    request: BinaryAnalysisRequest<'_>,
) -> Result<BinaryAnalysisOutcome, SourceError> {
    // Safe-open first, then size the opened descriptor so path swaps cannot bypass the cap.
    let file = crate::filesystem::open_file_safe(output_path).map_err(SourceError::Io)?;
    let metadata = file.metadata().map_err(SourceError::Io)?;
    if metadata.len() > request.decompiled_bytes_limit {
        return Ok(BinaryAnalysisOutcome::Degraded(
            BinaryAnalysisDegradation::OutputTooLarge {
                actual_bytes: metadata.len(),
                limit_bytes: request.decompiled_bytes_limit,
            },
        ));
    }

    let reader = std::io::BufReader::new(file);
    let mut decompiled_text = String::new();
    let mut string_literals = Vec::new();

    for line in reader.lines() {
        let line = line.map_err(SourceError::Io)?;
        decompiled_text.push_str(&line);
        decompiled_text.push('\n');
        super::super::literals::extract_string_literals(&line, &mut string_literals);
    }

    let path = Some(crate::filesystem::display_path(request.path).into());
    let mut chunks = Vec::new();
    if !decompiled_text.is_empty() {
        chunks.push(Chunk {
            data: decompiled_text.into(),
            metadata: ChunkMetadata {
                base_offset: 0,
                base_line: 0,
                source_type: "binary:ghidra:decompiled".into(),
                path: path.clone(),
                commit: None,
                author: None,
                date: None,
                mtime_ns: None,
                size_bytes: None,
                decoded_span: None,
            },
        });
    }
    if !string_literals.is_empty() {
        chunks.push(Chunk {
            data: string_literals.join("\n").into(),
            metadata: ChunkMetadata {
                base_offset: 0,
                base_line: 0,
                source_type: "binary:ghidra:strings".into(),
                path,
                commit: None,
                author: None,
                date: None,
                mtime_ns: None,
                size_bytes: None,
                decoded_span: None,
            },
        });
    }

    Ok(BinaryAnalysisOutcome::Complete(chunks))
}

fn kill_and_reap_ghidra_child(child: &mut Child, context: &str) -> std::io::Result<()> {
    let kill_result = terminate_analyzer_process_tree(child);
    let wait_result = child.wait();
    match (kill_result, wait_result) {
        (Ok(()), Ok(_)) => Ok(()),
        (Err(kill_error), Ok(_))
            if matches!(
                kill_error.kind(),
                std::io::ErrorKind::InvalidInput | std::io::ErrorKind::NotFound
            ) =>
        {
            Ok(())
        }
        (Err(kill_error), Ok(status)) => Err(std::io::Error::other(format!(
            "{context}: failed to kill child before reap: {kill_error}; reap status: {status}"
        ))),
        (Ok(()), Err(wait_error)) => Err(std::io::Error::other(format!(
            "{context}: killed child but failed to reap it: {wait_error}"
        ))),
        (Err(kill_error), Err(wait_error)) => Err(std::io::Error::other(format!(
            "{context}: failed to kill child: {kill_error}; failed to reap child: {wait_error}"
        ))),
    }
}

#[cfg(unix)]
fn isolate_analyzer_process_tree(command: &mut Command) {
    use std::os::unix::process::CommandExt;

    command.process_group(0);
}

#[cfg(not(unix))]
fn isolate_analyzer_process_tree(_command: &mut Command) {}

#[cfg(unix)]
fn terminate_analyzer_process_tree(child: &mut Child) -> std::io::Result<()> {
    let process_group = match i32::try_from(child.id()) {
        Ok(process_group) if process_group > 1 => process_group,
        Ok(process_group) => {
            return kill_direct_after_group_failure(
                child,
                format!("refusing unsafe Ghidra process-group ID {process_group}"),
            );
        }
        Err(_) => {
            // LAW10: direct-child termination still runs and the returned error is loud and operator-visible when process-tree termination is incomplete.
            return kill_direct_after_group_failure(
                child,
                "Ghidra process ID does not fit the platform process-group type".into(),
            );
        }
    };
    // SAFETY: the negative, nonzero PID targets only the process group created at spawn.
    if unsafe { libc::kill(-process_group, libc::SIGKILL) } == 0 {
        return Ok(());
    }

    let group_error = std::io::Error::last_os_error();
    if group_error.raw_os_error() == Some(libc::ESRCH) {
        return child.kill();
    }
    kill_direct_after_group_failure(
        child,
        format!("failed to kill Ghidra process group {process_group}: {group_error}"),
    )
}

#[cfg(unix)]
fn kill_direct_after_group_failure(child: &mut Child, group_error: String) -> std::io::Result<()> {
    match child.kill() {
        Ok(()) => Err(std::io::Error::other(format!(
            "{group_error}; killed direct child only"
        ))),
        Err(child_error) => Err(std::io::Error::other(format!(
            "{group_error}; failed to kill direct child: {child_error}"
        ))),
    }
}

#[cfg(not(unix))]
fn terminate_analyzer_process_tree(child: &mut Child) -> std::io::Result<()> {
    child.kill()
}

fn capture_ghidra_stderr_excerpt(
    mut stderr: std::process::ChildStderr,
) -> std::thread::JoinHandle<String> {
    std::thread::spawn(move || {
        let mut captured = Vec::with_capacity(GHIDRA_STDERR_EXCERPT_BYTES);
        let mut buffer = [0_u8; 1024];
        loop {
            match stderr.read(&mut buffer) {
                Ok(0) => break,
                Ok(n) => {
                    let remaining = GHIDRA_STDERR_EXCERPT_BYTES.saturating_sub(captured.len());
                    if remaining > 0 {
                        captured.extend_from_slice(&buffer[..n.min(remaining)]);
                    }
                }
                Err(error) => {
                    let suffix = format!(" [stderr capture read failed: {error}]");
                    let remaining = GHIDRA_STDERR_EXCERPT_BYTES.saturating_sub(captured.len());
                    if remaining > 0 {
                        let bytes = suffix.as_bytes();
                        captured.extend_from_slice(&bytes[..bytes.len().min(remaining)]);
                    }
                    break;
                }
            }
        }
        sanitize_ghidra_stderr_excerpt(&captured)
    })
}

fn sanitize_ghidra_stderr_excerpt(bytes: &[u8]) -> String {
    let text = String::from_utf8_lossy(bytes);
    let mut out = String::new();
    let mut pending_space = false;
    for ch in text.chars() {
        if ch.is_whitespace() {
            pending_space = !out.is_empty();
            continue;
        }
        if pending_space {
            out.push(' ');
            pending_space = false;
        }
        if ch.is_control() {
            continue;
        }
        out.push(ch);
    }
    out
}

/// Search standard locations for Ghidra's `analyzeHeadless` script.
pub(in crate::binary) fn find_ghidra_headless() -> Option<PathBuf> {
    // Non-standard installs must enter through the configured trusted-bin boundary.
    if let Some(path) = keyhog_core::resolve_safe_bin("analyzeHeadless") {
        return Some(path);
    }

    for pattern in &[
        "/opt/ghidra*/support/analyzeHeadless",
        "/usr/share/ghidra/support/analyzeHeadless",
        "/usr/local/share/ghidra/support/analyzeHeadless",
    ] {
        let paths = match glob::glob(pattern) {
            Ok(paths) => paths,
            Err(error) => {
                tracing::warn!(
                    pattern,
                    %error,
                    "Ghidra discovery glob pattern failed; skipping pattern"
                );
                continue;
            }
        };
        for entry in paths {
            match entry {
                Ok(entry) => {
                    if entry.exists() {
                        return Some(entry);
                    }
                }
                Err(error) => {
                    tracing::warn!(
                        pattern,
                        %error,
                        "Ghidra discovery glob entry failed; skipping entry"
                    );
                }
            }
        }
    }

    None
}

/// Write a Ghidra postScript that runs analysis and exports decompiled C.
fn write_ghidra_script(script_path: &Path, output_path: &Path) -> Result<(), SourceError> {
    let script = format!(
        r#"// KeyHog Ghidra export script - runs full analysis then decompiles all functions.
// @category KeyHog
import ghidra.app.decompiler.DecompInterface;
import ghidra.app.decompiler.DecompileResults;
import ghidra.app.script.GhidraScript;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.FunctionIterator;
import java.io.FileWriter;
import java.io.PrintWriter;

public class ExportDecompiled extends GhidraScript {{
    @Override
    public void run() throws Exception {{
        // Run full analysis first
        analyzeAll(currentProgram);

        DecompInterface decomp = new DecompInterface();
        decomp.openProgram(currentProgram);

        PrintWriter writer = new PrintWriter(new FileWriter("{output}"));

        // Export all string data from the program
        var dataIterator = currentProgram.getListing().getDefinedData(true);
        while (dataIterator.hasNext()) {{
            var data = dataIterator.next();
            if (data.hasStringValue()) {{
                writer.println("// DATA @ " + data.getAddress() + ": " + data.getValue());
            }}
        }}

        // Decompile all functions
        FunctionIterator funcs = currentProgram.getListing().getFunctions(true);
        while (funcs.hasNext()) {{
            Function func = funcs.next();
            DecompileResults results = decomp.decompileFunction(func, 30, monitor);
            if (results != null && results.decompileCompleted()) {{
                String decompiled = results.getDecompiledFunction().getC();
                if (decompiled != null) {{
                    writer.println("// FUNCTION: " + func.getName() + " @ " + func.getEntryPoint());
                    writer.println(decompiled);
                    writer.println();
                }}
            }}
        }}

        decomp.dispose();
        writer.close();
    }}
}}
"#,
        output = output_path
            .display()
            .to_string()
            .replace('\\', "\\\\")
            .replace('"', "\\\"")
    );

    std::fs::write(script_path, script).map_err(SourceError::Io)
}