44#
55
66import os , re
7- from typing import Any , Callable , List , Mapping , MutableSequence , Optional , Sequence , Tuple
7+ from typing import Any , Callable , Iterator , List , Mapping , MutableSequence , Optional , Pattern , Sequence , Tuple , Union
88
99from unicorn import UC_PROT_NONE , UC_PROT_READ , UC_PROT_WRITE , UC_PROT_EXEC , UC_PROT_ALL
1010
@@ -371,18 +371,18 @@ def write_ptr(self, addr: int, value: int, size: int=None) -> None:
371371
372372 self .write (addr , __pack (value ))
373373
374- def search (self , needle : bytes , begin : int = None , end : int = None ) -> Sequence [int ]:
374+ def search (self , needle : Union [ bytes , Pattern [ bytes ]] , begin : int = None , end : int = None ) -> Sequence [int ]:
375375 """Search for a sequence of bytes in memory.
376376
377377 Args:
378- needle: bytes sequence to look for
378+ needle: bytes sequence or regex pattern to look for
379379 begin: search starting address (or None to start at lowest avaiable address)
380380 end: search ending address (or None to end at highest avaiable address)
381381
382382 Returns: addresses of all matches
383383 """
384384
385- # if starting point not set, search from the first mapped region
385+ # if starting point not set, search from the first mapped region
386386 if begin is None :
387387 begin = self .map_info [0 ][0 ]
388388
@@ -396,6 +396,10 @@ def search(self, needle: bytes, begin: int = None, end: int = None) -> Sequence[
396396 ranges = [(max (begin , lbound ), min (ubound , end )) for lbound , ubound , _ , _ , is_mmio in self .map_info if not (end < lbound or ubound < begin or is_mmio )]
397397 results = []
398398
399+ # if needle is a bytes sequence use it verbatim, not as a pattern
400+ if type (needle ) is bytes :
401+ needle = re .escape (needle )
402+
399403 for lbound , ubound in ranges :
400404 haystack = self .read (lbound , ubound - lbound )
401405 local_results = (match .start (0 ) + lbound for match in re .finditer (needle , haystack ))
0 commit comments