xur_

> module

std::re

Xur's reverse engineering standard library module

use std::re; in the browser

std::re is a toolkit for binary analysis, live process interaction, machine code disassembly, and .NET assembly manipulation. Each part is backed by a dedicated Rust crate and lives in a submodule: std::re::pe, std::re::elf (binary files, via pewter), std::re::proc (live processes, via procodile), std::re::asm::x86 (machine code, via iced-x86), and std::re::dotnet, with its il2cpp and runtime submodules (.NET, via hao). Each has its own page; see SEE ALSO.

std::re itself holds AnyImage, which reads a PE or ELF file the way a loader would map it, and the demangling functions (see xur.std(7)).

> functions

# demangle(name: str) -> Option<str>

Demangle a C++ symbol name: tries the Itanium scheme (GCC/Clang), then MSVC. None if it is neither.

# demangle_itanium(name: str) -> Option<str>

Demangle an Itanium (GCC/Clang) name, one starting with _Z. None if it isn't a valid Itanium symbol.

# demangle_msvc(name: str) -> Option<str>

Demangle an MSVC name, one starting with ?. None if it isn't a valid MSVC symbol.

> types

┌ enum AnyImage

An AnyImage is a PE or ELF file read by virtual address, as it will be once loaded at its preferred base and relocated. Its reads take virtual addresses and return None for addresses nothing maps. It can also be edited in place and written back out, the same way for either format.

A PE or ELF image, as detected by [`AnyImage::load`].

AnyImage::Elf(ElfImage)
AnyImage::Pe(PeImage)
# AnyImage::load(data: bytes) -> Result<AnyImage, str>

Parse a PE or ELF file held in memory.

# AnyImage::open(path: str) -> Result<AnyImage, str>

Read the file at path and detect whether it is PE or ELF.

# any_image.add_section(name: str, data: bytes, perms: str) -> Result<u64, str>

Append a loaded section holding data and return its virtual address. perms is "ro" (read-only data), "rw" (writable data) or "rx" (code); case is ignored.

# any_image.as_elf() -> Option<ElfFile>

For an ELF, the file as an elf::ElfFile (see xur.std.re.elf(7)); None for PE.

# any_image.as_pe() -> Option<ImageDef>

For a PE, an editable copy as a pe::ImageDef (see xur.std.re.pe(7)); None for ELF.

# any_image.bytes(vaddr: u64, len: usize) -> Option<bytes>

len bytes at vaddr.

# any_image.c_str(vaddr: u64) -> Option<str>

The NUL-terminated string at vaddr, decoded as UTF-8 with invalid bytes replaced.

# any_image.data_segments() -> list<(u64, u64)>

The (start,\end) virtual-address ranges where data lives: a PE's non-code sections, or an ELF's non-executable loaded segments.

# any_image.exports() -> Result<list<Export>, str>

The exported symbols: a PE's export table, or an ELF's defined dynamic functions and objects. Each Export has a name, an ordinal (PE only) and a location, which is an ExportLocation (an address, or a PE forwarder such as "NTDLL.RtlAllocateHeap").

# any_image.format() -> str

"pe" or "elf".

# any_image.image_base() -> Option<u64>

A PE's preferred load address; None for ELF.

# any_image.is_64() -> bool

Whether pointers are 64-bit.

# any_image.is_mapped(vaddr: u64) -> bool

Whether vaddr is non-zero and mapped.

# any_image.machine() -> Option<ImageFileMachine>

The target machine as a PE machine type, or None for an ELF machine PE has no name for.

# any_image.pointer(vaddr: u64) -> Option<u64>

The pointer-sized value at vaddr, as it will be after relocation.

# any_image.pointer_at(base: u64, index: u64) -> Option<u64>

The indexth pointer of an array at base: the pointer at base\+\index\*\pointer_size().

# any_image.pointer_size() -> u64

The size of a pointer in bytes: 8 or 4.

# any_image.pointers(vaddr: u64, count: u64) -> Option<list<u64>>

count consecutive pointers from vaddr, or None if any of them is unmapped.

# any_image.u32(vaddr: u64) -> Option<u32>

The little-endian 32-bit value at vaddr.

# any_image.u8(vaddr: u64) -> Option<u8>

The byte at vaddr.

# any_image.write() -> Result<bytes, str>

Serialize the image with its edits. A PE is rebuilt (header sizes, section offsets and checksum recomputed); an ELF keeps its layout, with patches applied and new sections appended.

# any_image.write_at_vaddr(vaddr: u64, data: bytes) -> Result<(), str>

Overwrite the bytes at vaddr in place. Fails if vaddr is unmapped or the data would run past what is there.

┌ struct Export

A symbol exported by an image.

nameOption<str>`None` for a PE export by ordinal only.
ordinalOption<u32>The PE ordinal. ELF symbols have none.
locationExportLocation
┌ enum ExportLocation
ExportLocation::Address(u64)A virtual address in this image.
ExportLocation::Forwarder(str)A PE forwarder to a symbol in another DLL, such as `"NTDLL.RtlAllocateHeap"`.
┌ struct Table
┌ struct TableExportAddress
┌ struct TableExportNamePointer
┌ struct TableExportOrtinal
┌ struct TableImportTableDataDirectoryEntry
┌ struct TableImportTableRow
┌ struct TableString
0list<str>

> examples

use std::io;
use std::re::AnyImage;

fn main() -> Result<(), str> {
    let image = AnyImage::open("/bin/ls")?;
    io::println("{} {}-bit", image.format(), image.pointer_size() * 8);
    for export in image.exports()? {
        if let Some(name) = export.name {
            io::println("{}", name);
        }
    }
    Ok(())
}
use std::fs;
use std::re::AnyImage;

fn main() -> Result<(), str> {
    let image = AnyImage::open("game.dll")?;
    let vaddr = image.add_section(".patch", b"\\x90\\xc3", "rx")?;
    fs::write_bytes("game_patched.dll", image.write()?);
    Ok(())
}