reknife 1.5.0

A reverse engineer's binary Swiss-army knife: parse, triage, and disassemble PE/ELF/Mach-O. Installs as `knife`.
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
//! Drawing. Reads `App` and never changes it.
//!
//! The palette is the same one the printed output uses, so the interactive view
//! and the reports look like the same tool.

use super::{App, Focus, Line};
use crate::analysis::engine::XrefKind;
use crate::listing::Annot;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line as TLine, Span};
use ratatui::widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph};
use ratatui::Frame;

fn accent() -> Color {
    Color::Rgb(0xe0, 0x55, 0x55)
}
fn muted() -> Color {
    Color::Rgb(0x9b, 0x94, 0xae)
}
fn faint() -> Color {
    Color::Rgb(0x79, 0x72, 0x8f)
}
fn mint() -> Color {
    Color::Rgb(0x7f, 0xd4, 0xc1)
}
fn amber() -> Color {
    Color::Rgb(0xe0, 0xa8, 0x78)
}

fn pane(title: &str, focused: bool) -> Block<'_> {
    Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(if focused { accent() } else { faint() }))
        .title(Span::styled(
            format!(" {title} "),
            Style::default().fg(if focused { accent() } else { faint() }),
        ))
}

pub fn draw(f: &mut Frame, app: &App) {
    let rows = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1), // header
            Constraint::Min(5),    // body
            Constraint::Length(1), // prompt / hints
        ])
        .split(f.area());

    header(f, rows[0], app);

    let cols = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([Constraint::Length(34), Constraint::Min(20)])
        .split(rows[1]);

    functions(f, cols[0], app);

    let right = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Min(5), Constraint::Length(8)])
        .split(cols[1]);

    listing(f, right[0], app);
    xrefs(f, right[1], app);

    footer(f, rows[2], app);

    if app.help {
        help(f, f.area());
    }
}

fn header(f: &mut Frame, area: Rect, app: &App) {
    let named = app.an.functions.iter().filter(|x| x.named).count();
    let line = TLine::from(vec![
        Span::styled(
            format!(" {} ", app.title),
            Style::default().fg(accent()).add_modifier(Modifier::BOLD),
        ),
        Span::styled(
            format!(
                "{} · {} · {} functions, {} named",
                app.bin.format.label(),
                app.bin.arch.label(),
                app.an.functions.len(),
                named
            ),
            Style::default().fg(faint()),
        ),
    ]);
    f.render_widget(Paragraph::new(line), area);
}

fn functions(f: &mut Frame, area: Rect, app: &App) {
    let focused = app.focus == Focus::Functions;
    // The function containing whatever the listing shows is marked, so moving
    // through code keeps the left pane in step even after following calls.
    let current = app.cur;
    let items: Vec<ListItem> = app
        .order
        .iter()
        .map(|&i| {
            let fun = &app.an.functions[i];
            let here = current == Some(fun.addr);
            let mut spans = Vec::new();
            if here {
                spans.push(Span::styled("·", Style::default().fg(amber())));
            }
            spans.push(Span::styled(
                format!("{:>10x} ", fun.addr + app.an.display_base),
                Style::default().fg(faint()),
            ));
            spans.push(Span::styled(
                truncate(&fun.name, 16),
                Style::default().fg(if fun.named { mint() } else { muted() }),
            ));
            spans.push(Span::styled(
                format!(" {:>3}", fun.incoming),
                Style::default().fg(faint()),
            ));
            ListItem::new(TLine::from(spans))
        })
        .collect();

    let title = if app.filter.is_empty() {
        format!("functions ({})", app.order.len())
    } else {
        format!("functions ({}) /{}", app.order.len(), app.filter)
    };

    let mut state = ListState::default();
    if !app.order.is_empty() {
        state.select(Some(app.sel));
    }
    f.render_stateful_widget(
        List::new(items)
            .block(pane(&title, focused))
            .highlight_style(Style::default().bg(Color::Rgb(0x2a, 0x26, 0x3a)))
            .highlight_symbol("›"),
        area,
        &mut state,
    );
}

fn listing(f: &mut Frame, area: Rect, app: &App) {
    if app.pseudo {
        pseudo(f, area, app);
        return;
    }
    let focused = app.focus == Focus::Listing;
    let title = match app.cur {
        Some(a) => format!(
            "{} @ 0x{:x}{}",
            app.an.label(a),
            a + app.an.display_base,
            position(app.cursor, app.lines.len())
        ),
        None => "listing".into(),
    };

    let items: Vec<ListItem> = app
        .lines
        .iter()
        .map(|l| match l {
            Line::Label { text, .. } => ListItem::new(TLine::from(Span::styled(
                format!("  {text}:"),
                Style::default().fg(amber()),
            ))),
            Line::Data { addr, text } => ListItem::new(TLine::from(vec![
                Span::styled(
                    format!("{:012x}  ", addr + app.an.display_base),
                    Style::default().fg(faint()),
                ),
                Span::styled(text.clone(), Style::default().fg(muted())),
            ])),
            Line::Insn {
                addr,
                mnemonic,
                operands,
                annot,
                ..
            } => {
                let mut spans = vec![
                    Span::styled(
                        format!("{:012x}  ", addr + app.an.display_base),
                        Style::default().fg(faint()),
                    ),
                    Span::styled(format!("{mnemonic:<7} "), Style::default().fg(accent())),
                    Span::styled(operands.clone(), Style::default().fg(muted())),
                ];
                if let Some(a) = annot {
                    let (text, color) = match a {
                        // A note is the one annotation a person wrote, so it is
                        // the one that should catch the eye.
                        Annot::Note(t) => (t.clone(), amber()),
                        Annot::Symbol(t) => (t.clone(), mint()),
                        Annot::Local(t) => (t.clone(), faint()),
                        // A string literal is a quote, so it is drawn quoted.
                        Annot::Text(t) => (format!("\"{t}\""), amber()),
                    };
                    spans.push(Span::styled(
                        format!("  ; {text}"),
                        Style::default().fg(color),
                    ));
                }
                ListItem::new(TLine::from(spans))
            }
        })
        .collect();

    let mut state = ListState::default();
    if !app.lines.is_empty() {
        state.select(Some(app.cursor));
    }
    f.render_stateful_widget(
        List::new(items)
            .block(pane(&title, focused))
            .highlight_style(Style::default().bg(Color::Rgb(0x2a, 0x26, 0x3a)))
            .highlight_symbol("›"),
        area,
        &mut state,
    );
}

/// Colour a line of pseudocode: keywords stand out, everything else is muted.
fn pseudo_spans(text: &str) -> Vec<Span<'static>> {
    const KW: &[&str] = &[
        "if", "else", "while", "switch", "case", "break", "continue", "goto", "return", "do", "for",
    ];
    let mut spans = Vec::new();
    let mut buf = String::new();
    let mut buf_word = false;
    let flush = |buf: &mut String, word: bool, spans: &mut Vec<Span<'static>>| {
        if buf.is_empty() {
            return;
        }
        let color = if word && KW.contains(&buf.as_str()) {
            accent()
        } else {
            muted()
        };
        spans.push(Span::styled(
            std::mem::take(buf),
            Style::default().fg(color),
        ));
    };
    for ch in text.chars() {
        let word = ch.is_alphanumeric() || ch == '_';
        if word != buf_word {
            flush(&mut buf, buf_word, &mut spans);
            buf_word = word;
        }
        buf.push(ch);
    }
    flush(&mut buf, buf_word, &mut spans);
    spans
}

fn pseudo(f: &mut Frame, area: Rect, app: &App) {
    let focused = app.focus == Focus::Listing;
    let title = match app.cur {
        Some(a) => format!(
            "pseudocode · {} @ 0x{:x}{}",
            app.an.label(a),
            a + app.an.display_base,
            position(app.cursor, app.pseudo_lines.len())
        ),
        None => "pseudocode".into(),
    };

    let mut items: Vec<ListItem> = app
        .pseudo_lines
        .iter()
        .map(|l| {
            if l.label {
                // A jump label reads like one; the signature and braces are the
                // structure, so they take the accent.
                let color = if l.text.trim_end().ends_with(':') {
                    amber()
                } else {
                    accent()
                };
                ListItem::new(TLine::from(Span::styled(
                    l.text.clone(),
                    Style::default().fg(color),
                )))
            } else {
                ListItem::new(TLine::from(pseudo_spans(&l.text)))
            }
        })
        .collect();
    if items.is_empty() {
        items.push(ListItem::new(TLine::from(Span::styled(
            "  (no pseudocode for this view)",
            Style::default().fg(faint()),
        ))));
    }

    let mut state = ListState::default();
    if !app.pseudo_lines.is_empty() {
        state.select(Some(app.cursor.min(app.pseudo_lines.len() - 1)));
    }
    f.render_stateful_widget(
        List::new(items)
            .block(pane(&title, focused))
            .highlight_style(Style::default().bg(Color::Rgb(0x2a, 0x26, 0x3a)))
            .highlight_symbol("›"),
        area,
        &mut state,
    );
}

fn xrefs(f: &mut Frame, area: Rect, app: &App) {
    let (at, refs) = app.xrefs();
    let focused = app.focus == Focus::Xrefs;
    let title = format!("xrefs to 0x{:x} ({})", at + app.an.display_base, refs.len());

    let items: Vec<ListItem> = if refs.is_empty() {
        vec![ListItem::new(TLine::from(Span::styled(
            " no references",
            Style::default().fg(faint()),
        )))]
    } else {
        refs.iter()
            .map(|x| {
                let site = match app.an.function_at(x.from) {
                    Some(fun) => {
                        let off = x.from.saturating_sub(fun.addr);
                        if off == 0 {
                            fun.name.clone()
                        } else {
                            format!("{}+0x{off:x}", fun.name)
                        }
                    }
                    None => "-".into(),
                };
                ListItem::new(TLine::from(vec![
                    Span::styled(
                        format!(" {:>12x}  ", x.from + app.an.display_base),
                        Style::default().fg(faint()),
                    ),
                    Span::styled(
                        format!("{:<7}", x.kind.label()),
                        Style::default().fg(match x.kind {
                            XrefKind::Call => mint(),
                            XrefKind::Data => muted(),
                            _ => amber(),
                        }),
                    ),
                    Span::styled(site, Style::default().fg(muted())),
                ]))
            })
            .collect()
    };

    let mut state = ListState::default();
    if !refs.is_empty() {
        state.select(Some(app.xsel.min(refs.len() - 1)));
    }
    f.render_stateful_widget(
        List::new(items)
            .block(pane(&title, focused))
            .highlight_style(Style::default().bg(Color::Rgb(0x2a, 0x26, 0x3a)))
            .highlight_symbol("›"),
        area,
        &mut state,
    );
}

fn footer(f: &mut Frame, area: Rect, app: &App) {
    if let Some(p) = &app.prompt {
        let line = TLine::from(vec![
            Span::styled(
                format!(" {}: ", p.ask.label()),
                Style::default().fg(accent()).add_modifier(Modifier::BOLD),
            ),
            Span::raw(p.input.clone()),
            Span::styled("█", Style::default().fg(accent())),
        ]);
        f.render_widget(Paragraph::new(line), area);
        return;
    }

    if !app.status.is_empty() {
        f.render_widget(
            Paragraph::new(TLine::from(Span::styled(
                format!(" {}", app.status),
                Style::default().fg(amber()),
            ))),
            area,
        );
        return;
    }

    f.render_widget(
        Paragraph::new(TLine::from(Span::styled(
            format!(
                " db {} · ↵ open/follow{jump} ⌫ back   / filter   g goto   d {mode}   n name   c note   ? help   q quit",
                app.db.len(),
                jump = if app.focus == Focus::Xrefs {
                    " (jump to ref)"
                } else {
                    ""
                },
                mode = if app.pseudo { "asm" } else { "pseudo" },
            ),
            Style::default().fg(faint()),
        ))),
        area,
    );
}

fn help(f: &mut Frame, area: Rect) {
    let text = vec![
        "",
        "  ↑ ↓ / j k      move            tab            switch pane",
        "                  (functions / listing / xrefs)",
        "  pgup pgdn      page            home end       first / last",
        "  mouse          wheel scrolls, click picks a pane and an entry",
        "  ↵              open a function, follow the call under the cursor,",
        "                 or jump to a reference in the xrefs pane; following a",
        "                 string operand opens its bytes as a hex dump",
        "  ⌫              back to where you followed from",
        "",
        "  /              filter the function list by name",
        "  g              go to an address or a symbol",
        "  d              toggle decompiled pseudocode for the current function",
        "  n              name what is under the cursor (empty clears it)",
        "  c              note what is under the cursor (empty clears it)",
        "  r              re-analyse",
        "",
        "  Names and notes are written to the database as you make them, so",
        "  they are already saved when you quit.",
        "",
        "  any key to dismiss",
    ];
    let w = 74.min(area.width.saturating_sub(4));
    let h = (text.len() as u16 + 2).min(area.height);
    let box_area = Rect {
        x: area.x + (area.width.saturating_sub(w)) / 2,
        y: area.y + (area.height.saturating_sub(h)) / 2,
        width: w,
        height: h,
    };
    f.render_widget(Clear, box_area);
    f.render_widget(
        Paragraph::new(
            text.into_iter()
                .map(|l| TLine::from(Span::styled(l, Style::default().fg(muted()))))
                .collect::<Vec<_>>(),
        )
        .block(pane("keys", true)),
        box_area,
    );
}

/// A ` · row/total` suffix for a pane title, so the position in a long listing
/// is visible; empty when there is nothing to place.
fn position(cursor: usize, total: usize) -> String {
    if total == 0 {
        String::new()
    } else {
        format!(" · {}/{}", cursor + 1, total)
    }
}

fn truncate(s: &str, max: usize) -> String {
    if s.chars().count() <= max {
        format!("{s:<max$}")
    } else {
        let t: String = s.chars().take(max - 1).collect();
        format!("{t}…")
    }
}