Skip to content
Karak Studio icon

Desktop · Windows

Karak Studio

How the engine works

Program::open in engine.rs does everything needed to make a file browsable. It runs on a helper thread so the window stays responsive.

pe::parse checks the MZ and PE markers, reads the headers and section table, and builds an image: the file laid out the way Windows would load it into memory. From then on every address in the app is a memory address, and reading an address is a plain array lookup.

It also reads:

  • imports: for each borrowed function, the address of the slot that will hold its location, and its name as DLL!Function,
  • exports: named functions the file offers,
  • the function table (64-bit only): the exact start and end of every function that needs stack unwinding information,
  • a few header facts: build timestamp, whether it is a DLL, whether it has windows, whether it carries a signature.

Every read is bounds-checked and returns “nothing” on failure. A section with no data stored in the file is treated as empty, so a hostile file cannot claim a gigantic one.

When the file starts with \x7fELF, elf::parse reads it instead and fills in the same structure, so nothing after this step needs to know which kind of file it is:

  • The image comes from the program headers (PT_LOAD segments), laid out at their memory addresses. A position-independent file is moved up by 0x100000 (64-bit) or 0x10000 (32-bit).
  • Sections come from the section headers when present (.text, .plt, .got, .bss, …), otherwise from the segments, named LOAD0 (r-x) and so on.
  • Imports come from the dynamic relocations: every JUMP_SLOT, GLOB_DAT, COPY and plain pointer relocation that names a symbol gives one slot address and its name, as libc.so.6!puts. The library is found through the GNU version tables. Calls reach those slots through PLT stubs, which Program::thunk_slot recognises in all three forms: jmp [rip+X], endbr64; bnd jmp [rip+X], and 32-bit jmp [ebx+X] (with ebx holding the GOT address from DT_PLTGOT).
  • Function names and exact sizes come from .symtab and .dynsym. Sizes are trusted only when the full symbol table is present. Plain labels (assembly) name functions that are found by following calls.
  • Protections: PIE from the file type and loader, NX from PT_GNU_STACK, RELRO from PT_GNU_RELRO plus BIND_NOW, the canary from an import of __stack_chk_fail.

After the functions are known, Program::find_main reads the first instructions of _start and takes the value put in rdi (64-bit) or the last value pushed (32-bit) before the first call: that is main.

If the file contains a UPX header, it is unpacked and the result replaces what was parsed. See How UPX unpacking works.

Starting points are added in this order, later ones overriding earlier names:

  1. every entry in the function table, named sub_<address>, with its exact end,
  2. every export, under its real name,
  3. the entry point, named entry_point.

Then the analysis pass adds every destination of a direct call that is not already inside a known function. For files with a function table, only the insides of listed functions are scanned, where the bytes are known to be code. For files without one (32-bit), whole code sections are scanned.

Finally, any function that consists only of jmp [import] takes the import’s name.

Every section is scanned for runs of five or more printable characters, as ordinary text and as wide text. After analysis, text found inside a code section is dropped unless an instruction refers to it and it is not inside a known function.

Each function is decoded once more. For every instruction the engine notes:

  • direct calls and jumps to a known function,
  • memory operands relative to the instruction pointer (64-bit) or at a fixed address (32-bit),
  • immediate values that are addresses inside the image,

but only when the target is an import, a known piece of text or a function. The result is a map from each target to the places that use it, capped at 500 per target.

function_lines(start) produces the lines for one function when it is opened. Each instruction is decoded by iced-x86, formatted, and annotated with:

  • a kind: call, jump, conditional jump, return, padding, or data,
  • a target when a call or jump goes to a fixed address,
  • a comment: the import, function or text the instruction refers to.

A function ends at its listed end if there is one. Otherwise it ends at the next known function or, failing that, at the first ret or unconditional jmp that no earlier jump in the function goes past.

See Packed programs and UPX for the signs. Randomness is Shannon entropy over a section’s bytes.