Class KxdbParser
Defined in File KxdbParser.h
Inheritance Relationships
Base Type
protected BinaryReader
(Class BinaryReader)
Class Documentation
-
class KxdbParser : protected BinaryReader
Public Functions
-
KxdbParser(const uint8_t *buffer, size_t size)
Constructs a KxdbParser from a buffer.
- Parameters:
buffer – The buffer containing the KXDB data.
size – The size of the buffer.
-
KxdbParser(const vector<uint8_t> data)
Constructs a KxdbParser from a vector of bytes.
- Parameters:
data – The vector containing the KXDB data.
-
void ParseHeader()
Parses the header section of the KXDB file.
Reads the magic number, version, and the offsets to the different data sections (symbols, ROP actions, structs, targets). Optionally parses known metadata.
- Throws:
ExpKitError – if the magic number is invalid, the version is unsupported, or there’s an error reading the binary data.
-
optional<Target> GetTarget(const string &distro, const string &release_name, bool throw_on_missing = false)
Retrieves a target by its distribution and release name.
- Parameters:
distro – The distribution name of the target.
release_name – The release name of the target.
throw_on_missing – If true, throws an exception if no target or multiple targets are found.
- Throws:
ExpKitError – if no target or multiple targets are found for the given distro and release name, and throw_on_missing is true.
- Returns:
An optional containing the matched Target object, or nullopt if no target is found and throw_on_missing is false.
-
optional<Target> GetTarget(const string &version, bool throw_on_missing = false)
Retrieves a target by its full version string.
- Parameters:
version – The full version string of the target.
throw_on_missing – If true, throws an exception if no target or multiple targets are found.
- Throws:
ExpKitError – if no target or multiple targets are found for the given version, and throw_on_missing is true.
- Returns:
An optional containing the matched Target object, or nullopt if no target is found and throw_on_missing is false.
Public Members
-
vector<RopActionMeta> rop_action_meta_
Public Static Functions
-
static KxdbParser FromFile(const string &filename)
Constructs a KxdbParser by reading data from a file.
- Parameters:
filename – The path to the KXDB file.
- Throws:
ExpKitError – if the file cannot be read.
- Returns:
A KxdbParser object initialized with the file’s content.
Protected Functions
-
void ParseSymbolsHeader()
Parses the symbols header section of the KXDB file.
Reads the number of symbols and their metadata, storing the symbol names.
- Throws:
ExpKitError – if there’s an error reading the binary data.
-
void ParseSymbols(Target &target)
Parses the symbols section for a given target.
Reads the symbol values based on the parsed symbol names and populates the target’s symbols map.
- Parameters:
target – The Target object to populate with symbols.
- Throws:
ExpKitError – if there’s an error reading the binary data.
-
void ParseRopActionsHeader()
Parses the ROP actions header section of the KXDB file.
Reads the number of ROP actions and their metadata, storing the ROP action IDs and optionally parsing detailed metadata.
- Throws:
ExpKitError – if there’s an error reading the binary data.
-
void ParseRopActions(Target &target)
Parses the ROP actions section for a given target.
Reads the ROP item sequences for each ROP action based on the parsed ROP action IDs and populates the target’s rop_actions map.
- Parameters:
target – The Target object to populate with ROP actions.
- Throws:
ExpKitError – if there’s an error reading the binary data.
-
RegisterUsage ReadRegisterUsage()
Reads RegisterUsage data from the binary stream.
- Throws:
ExpKitError – if there’s an error reading the binary data.
- Returns:
A RegisterUsage object containing the parsed register and used offsets.
-
void ParsePivots(Target &target)
Parses the pivots section for a given target.
Reads various types of pivots (one-gadgets, push indirects, pop rsp, stack shifts) and populates the target’s pivots structure.
- Parameters:
target – The Target object to populate with pivots.
- Throws:
ExpKitError – if there’s an error reading the binary data.
-
void ParseStructsHeader()
Parses the structs header section of the KXDB file.
Reads the metadata for each struct, including its name and fields, and stores it. Also reads the offset to the struct layouts.
- Throws:
ExpKitError – if there’s an error reading the binary data.
-
Struct &ParseStructLayout(uint64_t layout_idx)
Parses a specific struct layout from the binary stream.
Seeks to the specified layout index, reads the struct’s size, name, and field offsets and sizes.
- Parameters:
layout_idx – The index of the struct layout to parse.
- Throws:
ExpKitError – if there’s an error reading the binary data or if a non-optional field is missing.
- Returns:
A reference to the parsed Struct object.
-
Struct &GetStructLayout(uint64_t layout_idx)
Retrieves a struct layout, parsing it if necessary.
- Parameters:
layout_idx – The index of the struct layout to retrieve.
- Throws:
ExpKitError – if there’s an error parsing the struct layout.
- Returns:
A reference to the Struct object.
-
void ParseStructs(Target &target)
Parses the structs section for a given target.
Reads the struct layout indices for each struct metadata entry and retrieves or parses the corresponding struct layouts, populating the target’s structs map.
- Parameters:
target – The Target object to populate with structs.
-
vector<Target> ParseTargets(optional<const string> distro, optional<const string> release_name, optional<const string> version)
Parses targets from the KXDB file that match the optional filter criteria.
- Parameters:
distro – Optional filter for the distribution name.
release_name – Optional filter for the release name.
version – Optional filter for the version string.
- Throws:
ExpKitError – if there’s an error parsing the binary data.
- Returns:
A vector of Target objects that match the specified criteria.
-
optional<Target> ParseTarget(optional<const string> distro, optional<const string> release_name, optional<const string> version, bool throw_on_missing)
Parses and retrieves a single target matching the specified criteria.
- Parameters:
distro – Optional filter for the distribution name.
release_name – Optional filter for the release name.
version – Optional filter for the version string.
throw_on_missing – If true, throws an exception if no target or multiple targets are found.
- Throws:
ExpKitError – if no target or multiple targets are found and throw_on_missing is true.
- Returns:
An optional containing the matched Target object, or nullopt if no target is found and throw_on_missing is false.
-
uint64_t Uint(int size)
Reads an unsigned integer of a specified size.
- Parameters:
size – The size of the unsigned integer to read (1, 2, 4, or 8 bytes).
- Returns:
The unsigned integer value read from the buffer.
-
uint64_t EndOffset()
Returns the end offset of the current structure or the entire data if seeking is in progress.
- Returns:
The end offset.
-
uint64_t RemainingBytes()
Returns the number of remaining bytes in the current structure or until the end of the data if seeking is in progress.
- Returns:
The number of remaining bytes.
-
void SizeCheck(uint64_t len)
Checks if there are enough remaining bytes to read a specified length.
- Parameters:
len – The length to check against the remaining bytes.
- Throws:
ExpKitError – if there are not enough remaining bytes.
-
void Skip(uint64_t len)
Skip len amount of bytes.
- Parameters:
len – The number of bytes to skip.
- Throws:
ExpKitError – if there are not enough remaining bytes.
-
void SeekTo(uint64_t offset)
Seek to offset.
- Parameters:
offset – The offset within the file to seek.
- Throws:
ExpKitError – if the offset is out-of-bounds.
-
uint8_t *Read(uint16_t len)
Reads a block of raw bytes from the buffer.
- Parameters:
len – The number of bytes to read.
- Throws:
ExpKitError – if reading beyond the buffer limits.
- Returns:
A pointer to the read bytes within the internal buffer.
-
uint8_t ReadU8()
Reads a single byte (uint8_t) from the buffer.
- Returns:
The byte value.
-
uint16_t ReadU16()
Reads a 16-bit unsigned integer (uint16_t) from the buffer.
- Returns:
The 16-bit unsigned integer value.
-
uint32_t ReadU32()
Reads a 32-bit unsigned integer (uint32_t) from the buffer.
- Returns:
The 32-bit unsigned integer value.
-
uint64_t ReadU64()
Reads a 64-bit unsigned integer (uint64_t) from the buffer.
- Returns:
The 64-bit unsigned integer value.
-
int64_t ReadInt(bool signed_ = true)
Reads a variable-length integer from the buffer.
- Parameters:
signed_ – Whether the integer is signed. Defaults to true.
- Returns:
The integer value.
-
uint64_t ReadUInt()
Reads a variable-length unsigned integer from the buffer.
- Returns:
The unsigned integer value.
-
uint64_t SeekableListCount()
Reads the count of a seekable list and skips the seek list data.
- Returns:
The number of items in the seekable list.
-
std::vector<uint64_t> IndexableIntList()
-
std::vector<uint64_t> SeekableListSizes()
-
bool IsSeekingInProgress()
Checks if a seek operation is currently in progress.
- Returns:
True if seeking is in progress, false otherwise.
-
void SeekToItem(uint64_t seeklist_offset, uint64_t item_idx)
Seeks to a specific item within a seekable list.
- Parameters:
seeklist_offset – The offset of the seekable list.
item_idx – The index of the item to seek to.
- Throws:
ExpKitError – if seeking is already in progress or the item index is out of bounds.
-
void EndSeek()
Ends a seek operation and returns to the original offset.
- Throws:
ExpKitError – if no seek operation is in progress.
-
template<typename ...Args>
inline void DebugLog(const char *format, const Args&... args) Logs a debug message with the current offset.
- Template Parameters:
Args – The types of the arguments.
- Parameters:
format – The format string for the log message.
-
bool BeginStruct(uint64_t struct_size)
Limits the reader to struct_size.
- Parameters:
struct_size – The structure size in bytes.
- Returns:
struct_size is not zero.
-
void EndStruct()
Ends parsing a structure. Jumps the offset to the end of the current structure.
- Throws:
ExpKitError – if EndStruct() is called without a corresponding BeginStruct().
-
const char *ZStr(uint16_t len)
Reads a null-terminated string with a specified maximum length.
- Parameters:
len – The maximum length of the string (excluding the null terminator).
- Returns:
A pointer to the null-terminated string within the internal buffer.
-
const char *ZStr()
Reads a null-terminated string where the length is encoded as a variable-length unsigned integer before the string data.
- Returns:
A pointer to the null-terminated string within the internal buffer.
Protected Attributes
-
uint64_t offset_targets_ = 0
-
uint64_t offset_struct_layouts_ = 0
-
vector<string> symbol_names_
-
vector<StructMeta> structs_meta_
-
map<Section, SectionInfo> sections_
-
std::vector<uint8_t> data_
-
uint64_t offset_ = 0
-
std::vector<uint64_t> struct_ends_
-
int64_t seek_origin_offset_ = -1
-
unsigned int log_padding = 0
Protected Static Functions
-
static BinaryReader FromFile(const char *filename)
Creates a BinaryReader by reading data from a file.
- Parameters:
filename – The path to the file.
- Returns:
A BinaryReader instance with the file data.
-
KxdbParser(const uint8_t *buffer, size_t size)