如何在二进制文件中查找字节序列并修改指定比特位?
Hey there! The issue you're hitting is pretty straightforward: the array.index() method only works for finding single elements in the array, not an entire sub-sequence of bytes like your abc array (which holds the ASCII values for "ABC"). That's why you're getting the ValueError: array.index(x): x not in list error—even if "ABC" exists in your binary file, the method doesn't know how to look for a group of elements at once.
Let's fix this step by step:
1. Implement a function to find the sub-sequence
First, we'll write a helper function that scans the array to find the starting index of your target byte sequence. This checks each position in the array to see if the following elements match your sub-array:
def find_subarray(main_array, sub_array): sub_len = len(sub_array) main_len = len(main_array) if sub_len > main_len: return -1 # Subsequence can't be longer than the main array for i in range(main_len - sub_len + 1): if main_array[i:i+sub_len] == sub_array: return i return -1 # Subsequence not found
2. Update your original code
Now replace the problematic content.index(abc) call with our helper function. We'll also fix a couple of file handling issues you might run into later:
- After reading the entire file, the file pointer is at the end—we need to move it back to the start with
file.seek(0)before writing. - Use
file.truncate()after writing to make sure any extra bytes from the original file are removed (in case your modified content is shorter, though it shouldn't be here).
Here's the revised code:
import os from array import array def find_subarray(main_array, sub_array): sub_len = len(sub_array) main_len = len(main_array) if sub_len > main_len: return -1 for i in range(main_len - sub_len + 1): if main_array[i:i+sub_len] == sub_array: return i return -1 filename = "your_binary_file.bin" # Replace with your actual file path content = array('B') with open(filename, mode="r+b") as file: # Read the entire file into the array content.fromfile(file, os.fstat(file.fileno()).st_size) # Prepare the target byte sequence (ASCII "ABC") abc = array('B', [ord(letter) for letter in "ABC"]) # Find the starting index of the sequence i = find_subarray(content, abc) if i == -1: raise ValueError("Byte sequence 'ABC' not found in the file") # Modify the target bit (set the 0th bit of the byte at i + 0x16) content[i + 0x16] |= 1 # Move file pointer to start and write the modified content file.seek(0) content.tofile(file) file.truncate() # Remove any leftover bytes from the original file
Key Notes:
- We're using
array('B')for both the main content and the target sequence to ensure we're comparing like-for-like (since slicing an array returns another array, not a list). - The
find_subarrayfunction returns-1if the sequence isn't found—we added a check to raise a clear error in that case, so you know exactly what went wrong. - The bit modification
content[i + 0x16] |= 1correctly sets the least significant bit of the target byte—if you need to set a different bit, you can adjust the mask (e.g.,0x20for the 5th bit).
内容的提问来源于stack exchange,提问作者Tar

