Skip to content

Instantly share code, notes, and snippets.

@Bl4ckSh4rk
Created November 2, 2024 11:53
Show Gist options
  • Save Bl4ckSh4rk/56385fe6c1072bf0a2d9cf7eb7a4ef7a to your computer and use it in GitHub Desktop.
Save Bl4ckSh4rk/56385fe6c1072bf0a2d9cf7eb7a4ef7a to your computer and use it in GitHub Desktop.
Finds bytes in a directory
from argparse import ArgumentParser
import os
def search_in_file(filepath, bytes) -> int | None:
try:
with open(filepath, 'rb') as file:
data = file.read()
offset = data.find(bytes)
if offset != -1:
return offset
except Exception as e:
print(f"Error reading {filepath}: {e}")
return None
def search_in_files(directory, bytes) -> list:
found_files = []
for root, _, files in os.walk(directory):
for file in files:
filepath = os.path.join(root, file)
offset = search_in_file(filepath, bytes)
if offset is not None:
found_files.append((filepath, offset))
return found_files
def main() -> None:
parser = ArgumentParser(description="Finds bytes in a directory")
parser.add_argument('directory', type=str, help="Directory to search in")
parser.add_argument('bytes_str', type=str, help="Bytes to find")
args = parser.parse_args()
found_files = search_in_files(args.directory, bytes.fromhex(args.bytes_str))
if found_files:
print(f"{len(found_files)} files found")
for file, offset in found_files:
print(f"[0x{offset:x}] {file}")
else:
print("No files found")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment