PatternBuilder

Struct PatternBuilder 

Source
pub struct PatternBuilder { /* private fields */ }
Expand description

Builder for the Pattern struct.
The builder is used to create a Pattern struct with the desired settings.

When building a pattern, (of any kind) the following rules apply:

  • The signature must be a valid hexadecimal string. (case-insensitive)
  • It might contain wildcards at the beginning and/or end of the string. (which are ignored during the scan)
    • In the case of one or more wildcard bytes at the beginning of the string, the resulting address will be offset by the number of wildcards. (resulting in the address of the first non-wildcard byte minus the wildcard bytes at the beginning)
    • End wildcards are ignored during the scan, so the match will be found even if the full pattern would not fit in the remaining bytes.

§Examples

§Multi-threaded scan for an IDA-style pattern

let data = std::fs::read("some.bin").unwrap();
let found = aobscan::PatternBuilder::from_ida_style("48 8B 05 ? ? ? ? 48 8B 88 ? ? ? ?")
    .unwrap()
    .with_all_threads()
    .build()
    .scan(&data, move |offset| {
        println!("Match at offset {:#02x}", offset);
        true // Return true to continue scanning for other matches
    });

§Single-threaded scan for a code-style pattern

let data = std::fs::read("some.bin").unwrap();
let found = aobscan::PatternBuilder::from_code_style(
    b"\x48\x8B\x05\x00\x00\x00\x00\x48\x8B\x88\x00\x00\x00\x00",
    "...????...????"
).unwrap()
    .with_threads(1)
    .unwrap()
    .build()
    .scan(&data, move |offset| {
        println!("Match at offset {:#02x}", offset);
        true // Return true to continue scanning for other matches
    });

Implementations§

Source§

impl PatternBuilder

Source

pub fn from_code_style( signature: &[u8], mask: &str, ) -> Result<Self, BuilderError>

Creates a pattern builder from a code-style signature.

A code-style signature is characterized by a byte array and a mask string.
The mask string is a list of characters, where each character represents whether the corresponding byte in the byte array is a wildcard or not.
You must use a ? for each wildcard byte, but you can choose any other character for non-wildcard bytes.

The length of the mask string must be equal to the length of the byte array, and the byte array can contain any hexadecimal value in the place of a wildcard.

This pattern representation is usually the most safe and reliable, but it is also the most verbose and tedious to write.

§Arguments
  • signature - The byte array containing the bytes to search for.
  • mask - The mask string.
§Returns

The current instance of the pattern, or None if the parameters are invalid.

§Good Practices
  • In the mask string, use a [?] for each wildcard byte, and [.] for each non-wildcard byte.
  • In the byte array, use \x00 for each wildcard byte, and the actual byte value for each non-wildcard byte.
§Errors
  • BuilderError::SizeMismatch - The size of the signature and mask do not match.
§Format
signature:  `b"\x48\x8B\x05\x00\x00\x00\x00"`
mask:       `"...????"`
Source

pub fn from_ida_style(pattern: &str) -> Result<Self, BuilderError>

Creates a pattern builder from an IDA-style signature.

An IDA-style signature is characterized by a single string of hexadecimal values separated by spaces.
In this string, you can use ? or ?? to represent a wildcard byte.

It is generally preferred as it is shorter and easier to read, but it may introduce some overhead as it is ultimately converted to a code-style like AOB.

§Arguments
  • pattern - The IDA-style pattern string.
§Returns

The current instance of the builder, or None if the parameters are invalid.

§Errors
  • BuilderError::InvalidSignature - The pattern string is empty.
  • BuilderError::ParseError - The pattern string contains invalid hexadecimal values.
§Format
pattern:    "48 8B 05 ? ? ? ?" // or "48 8B 05 ?? ?? ?? ??"
Source

pub fn from_hex_string(pattern: &str) -> Result<Self, BuilderError>

Creates a pattern builder from a string of non-spaced, case-insensitive hex bytes.

The string must contain only hexadecimal characters (or ’??’s for wildcard bytes), and its length must be a multiple of 2.
Single-char wildcards are not supported!

§Arguments
  • pattern - The pattern string.
§Returns

The current instance of the builder, or None if the parameter is invalid.

§Errors
  • BuilderError::InvalidSignature - The pattern is empty, its length is odd, contains invalid characters or single-char wildcards.
  • BuilderError::ParseError - The pattern contains invalid hexadecimal characters.
§Format
pattern:    "488b05????????488b88??" // a pair of '??'s represents a wildcard byte
Source

pub fn with_threads(self, threads: usize) -> Result<Self, BuilderError>

Sets the number of threads to use for scanning.
The number of threads is considered invalid if it is set to 0 or greater than the number of logical CPU cores.

§Arguments
  • threads - The number of threads to use.
§Returns

The current instance of the builder if the number of threads is valid, otherwise None.

Source

pub fn with_all_threads(self) -> Self

Sets the number of threads to use for scanning to the number of logical CPU cores.

§Returns

The current instance of the builder.

Source

pub fn build(self) -> Pattern

Builds a new pattern instance with the specified settings.

§Returns

The created pattern instance.

Trait Implementations§

Source§

impl Clone for PatternBuilder

Source§

fn clone(&self) -> PatternBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PatternBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for PatternBuilder

Source§

fn eq(&self, other: &PatternBuilder) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for PatternBuilder

Source§

impl StructuralPartialEq for PatternBuilder

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.