|
2 | 2 | from typing import List
|
3 | 3 |
|
4 | 4 | from binaryninja.binaryview import BinaryView, DataVariable
|
5 |
| -from binaryninja.enums import MediumLevelILOperation |
| 5 | +from binaryninja.enums import MediumLevelILOperation, SectionSemantics |
6 | 6 | from binaryninja.log import Logger
|
7 | 7 | from binaryninja.mediumlevelil import MediumLevelILConst
|
8 | 8 | from binaryninja.plugin import BackgroundTaskThread
|
@@ -74,29 +74,45 @@ def run(self):
|
74 | 74 | self.bv.segments,
|
75 | 75 | )
|
76 | 76 | )
|
77 |
| - if len(readonly_segments) == 0: |
78 |
| - logger.log_error("Could not find any read-only segment in binary, exiting") |
| 77 | + |
| 78 | + readonly_sections = list( |
| 79 | + filter( |
| 80 | + lambda section: section.semantics |
| 81 | + == SectionSemantics.ReadOnlyDataSectionSemantics, |
| 82 | + self.bv.sections.values(), |
| 83 | + ) |
| 84 | + ) |
| 85 | + |
| 86 | + if len(readonly_segments) == 0 and len(readonly_sections) == 0: |
| 87 | + logger.log_error( |
| 88 | + "Could not find any read-only segments or sections in binary, exiting" |
| 89 | + ) |
79 | 90 | return
|
80 | 91 |
|
81 | 92 | self.bv.begin_undo_actions()
|
82 |
| - # Obtain all data vars which are pointers to data in readonly data segments |
83 |
| - data_vars_to_ro_segment_data: List[DataVariable] = [] |
| 93 | + # Obtain all data vars which are pointers to data in read-only data segments or sections |
| 94 | + data_vars_to_readonly_data: List[DataVariable] = [] |
84 | 95 | for (
|
85 | 96 | _data_var_addr,
|
86 | 97 | candidate_string_slice_data_ptr,
|
87 | 98 | ) in self.bv.data_vars.items():
|
88 | 99 | if isinstance(candidate_string_slice_data_ptr.type, PointerType):
|
89 |
| - for readonly_segment in readonly_segments: |
90 |
| - if candidate_string_slice_data_ptr.value in readonly_segment: |
91 |
| - data_vars_to_ro_segment_data.append( |
| 100 | + for readonly_segment_or_section in ( |
| 101 | + readonly_segments + readonly_sections |
| 102 | + ): |
| 103 | + if ( |
| 104 | + candidate_string_slice_data_ptr.value |
| 105 | + in readonly_segment_or_section |
| 106 | + ): |
| 107 | + data_vars_to_readonly_data.append( |
92 | 108 | candidate_string_slice_data_ptr
|
93 | 109 | )
|
94 | 110 | logger.log_debug(
|
95 | 111 | f"Found pointer var at {candidate_string_slice_data_ptr.address:#x} ({candidate_string_slice_data_ptr}) pointing to {candidate_string_slice_data_ptr.value:#x} "
|
96 | 112 | )
|
97 | 113 |
|
98 | 114 | recovered_string_slices: List[RustStringSlice] = []
|
99 |
| - for candidate_string_slice_data_ptr in data_vars_to_ro_segment_data: |
| 115 | + for candidate_string_slice_data_ptr in data_vars_to_readonly_data: |
100 | 116 | # Try to read an integer following the data var,
|
101 | 117 | # and treat it as a candidate for a string slice length.
|
102 | 118 | candidate_string_slice_len_addr = (
|
|
0 commit comments