-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathnewline_render_layer.py
More file actions
269 lines (212 loc) · 10.6 KB
/
newline_render_layer.py
File metadata and controls
269 lines (212 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from typing import List
import binaryninja
from binaryninja import RenderLayer, InstructionTextToken, \
InstructionTextTokenType, DisassemblyTextLine, log_info
"""
Render Layer that splits string literals containing newline characters into multiple lines.
Before:
char* s = "Hi, guys!\nWe all love Binja!";
After:
char* s = "Hi, guys!\n"
"We all love Binja!";
"""
class NewlineSplitRenderLayer(RenderLayer):
name = "Split Strings at Newlines"
def split_string_token(self, token: InstructionTextToken) -> List[InstructionTextToken]:
"""
Split a string token containing \n into multiple tokens.
Returns a list of tokens representing the split string.
"""
text = token.text
log_info(f"[NewlineSplitRenderLayer] Examining token: type={token.type}, text={repr(text)}")
# Check if this token contains \n (the token text doesn't include quotes)
if '\\n' not in text:
return [token]
log_info(f"[NewlineSplitRenderLayer] Found string with \\n: {repr(text)}")
# The text is the string content (no quotes in token.text)
content = text
# Split by \n but keep the \n with the preceding part
parts = []
current_part = ""
i = 0
while i < len(content):
if i < len(content) - 1 and content[i] == '\\' and content[i+1] == 'n':
current_part += '\\n'
parts.append(current_part)
current_part = ""
i += 2
else:
current_part += content[i]
i += 1
# Add any remaining content
if current_part:
parts.append(current_part)
# If we only have one part, return the original token
if len(parts) <= 1:
return [token]
log_info(f"[NewlineSplitRenderLayer] Splitting into {len(parts)} parts: {parts}")
# Create tokens for each part (token.text doesn't include quotes, so don't add them)
result = []
for i, part in enumerate(parts):
result.append(InstructionTextToken(token.type, part, token.value, token.size, token.operand, token.context, token.address, token.confidence))
return result
def apply_to_block(
self,
block: 'binaryninja.BasicBlock',
lines: List['binaryninja.DisassemblyTextLine']
):
log_info(f"[NewlineSplitRenderLayer] apply_to_block called with {len(lines)} lines")
new_lines = []
for line in lines:
# Look for string tokens
has_split = False
split_info = None
for i, token in enumerate(line.tokens):
if token.type == InstructionTextTokenType.StringToken:
log_info(f"[NewlineSplitRenderLayer] Found StringToken at index {i}")
split_tokens = self.split_string_token(token)
if len(split_tokens) > 1:
has_split = True
split_info = (i, split_tokens)
log_info(f"[NewlineSplitRenderLayer] Will split this line into {len(split_tokens)} parts")
break
if not has_split:
new_lines.append(line)
continue
# Create multiple lines for the split string
token_idx, split_tokens = split_info
# Find the indentation level by looking at the position of the string token
indent_count = 0
for i in range(token_idx):
if line.tokens[i].type == InstructionTextTokenType.TextToken:
indent_count += len(line.tokens[i].text)
else:
indent_count += len(line.tokens[i].text)
# Create first line with original tokens up to and including first split token
first_line = DisassemblyTextLine()
first_line.address = line.address
first_line.il_instruction = line.il_instruction
first_line.highlight = line.highlight
first_line.tokens = line.tokens[:token_idx] + [split_tokens[0]]
new_lines.append(first_line)
# Create continuation lines for remaining split tokens
for j in range(1, len(split_tokens)):
cont_line = DisassemblyTextLine()
cont_line.address = line.address
cont_line.il_instruction = line.il_instruction
cont_line.highlight = line.highlight
# Add indentation to align with the start of the string
indent_token = InstructionTextToken(InstructionTextTokenType.TextToken, ' ' * indent_count)
cont_line.tokens = [indent_token, split_tokens[j]]
# If this is the last split token, add the rest of the original tokens
if j == len(split_tokens) - 1:
cont_line.tokens.extend(line.tokens[token_idx + 1:])
new_lines.append(cont_line)
return new_lines
def apply_to_high_level_il_body(
self,
function: 'binaryninja.Function',
lines: List['binaryninja.LinearDisassemblyLine']
):
log_info(f"[NewlineSplitRenderLayer] apply_to_high_level_il_body called with {len(lines)} lines")
# Similar logic for HLIL linear view
new_lines = []
for line in lines:
# Look for string tokens
has_split = False
split_info = None
for i, token in enumerate(line.contents.tokens):
if token.type == InstructionTextTokenType.StringToken:
split_tokens = self.split_string_token(token)
if len(split_tokens) > 1:
has_split = True
split_info = (i, split_tokens)
break
if not has_split:
new_lines.append(line)
continue
# Create multiple lines for the split string
token_idx, split_tokens = split_info
# Find the indentation level
indent_count = 0
for i in range(token_idx):
indent_count += len(line.contents.tokens[i].text)
# Create first line - we need to create a new DisassemblyTextLine with modified tokens
first_tokens = line.contents.tokens[:token_idx] + [split_tokens[0]]
first_contents = DisassemblyTextLine(first_tokens, line.contents.address)
first_contents.highlight = line.contents.highlight
first_contents.il_instruction = line.contents.il_instruction
first_line = binaryninja.LinearDisassemblyLine(
line.type,
line.function,
line.block,
first_contents
)
new_lines.append(first_line)
# Create continuation lines
for j in range(1, len(split_tokens)):
# Add indentation
indent_token = InstructionTextToken(InstructionTextTokenType.TextToken, ' ' * indent_count)
cont_tokens = [indent_token, split_tokens[j]]
# If this is the last split token, add the rest of the original tokens
if j == len(split_tokens) - 1:
cont_tokens.extend(line.contents.tokens[token_idx + 1:])
cont_contents = DisassemblyTextLine(cont_tokens, line.contents.address)
cont_contents.highlight = line.contents.highlight
cont_contents.il_instruction = line.contents.il_instruction
cont_line = binaryninja.LinearDisassemblyLine(
line.type,
line.function,
line.block,
cont_contents
)
new_lines.append(cont_line)
return new_lines
def apply_to_flow_graph(self, graph: 'binaryninja.FlowGraph'):
log_info(f"[NewlineSplitRenderLayer] apply_to_flow_graph called with {len(graph.nodes)} nodes")
for node in graph.nodes:
lines = node.lines
new_lines = []
for line in lines:
# Look for string tokens
has_split = False
split_info = None
for i, token in enumerate(line.tokens):
if token.type == InstructionTextTokenType.StringToken:
log_info(f"[NewlineSplitRenderLayer] Found StringToken in flow graph node")
split_tokens = self.split_string_token(token)
if len(split_tokens) > 1:
has_split = True
split_info = (i, split_tokens)
log_info(f"[NewlineSplitRenderLayer] Will split this line in flow graph into {len(split_tokens)} parts")
break
if not has_split:
new_lines.append(line)
continue
# Create multiple lines for the split string
token_idx, split_tokens = split_info
# Find the indentation level by looking at the position of the string token
indent_count = 0
for i in range(token_idx):
indent_count += len(line.tokens[i].text)
# Create first line with original tokens up to and including first split token
first_tokens = line.tokens[:token_idx] + [split_tokens[0]]
first_line = DisassemblyTextLine(first_tokens, line.address)
first_line.highlight = line.highlight
first_line.il_instruction = line.il_instruction
new_lines.append(first_line)
# Create continuation lines for remaining split tokens
for j in range(1, len(split_tokens)):
# Add indentation to align with the start of the string
indent_token = InstructionTextToken(InstructionTextTokenType.TextToken, ' ' * indent_count)
cont_tokens = [indent_token, split_tokens[j]]
# If this is the last split token, add the rest of the original tokens
if j == len(split_tokens) - 1:
cont_tokens.extend(line.tokens[token_idx + 1:])
cont_line = DisassemblyTextLine(cont_tokens, line.address)
cont_line.highlight = line.highlight
cont_line.il_instruction = line.il_instruction
new_lines.append(cont_line)
# Update the node's lines
node.lines = new_lines
NewlineSplitRenderLayer.register()